| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.32.0. By combining all the individual C code files into this |
| 3 | +** version 3.32.1. 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. |
| | @@ -215,10 +215,13 @@ |
| 215 | 215 | #if SQLITE_ENABLE_ATOMIC_WRITE |
| 216 | 216 | "ENABLE_ATOMIC_WRITE", |
| 217 | 217 | #endif |
| 218 | 218 | #if SQLITE_ENABLE_BATCH_ATOMIC_WRITE |
| 219 | 219 | "ENABLE_BATCH_ATOMIC_WRITE", |
| 220 | +#endif |
| 221 | +#if SQLITE_ENABLE_BYTECODE_VTAB |
| 222 | + "ENABLE_BYTECODE_VTAB", |
| 220 | 223 | #endif |
| 221 | 224 | #if SQLITE_ENABLE_CEROD |
| 222 | 225 | "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD), |
| 223 | 226 | #endif |
| 224 | 227 | #if SQLITE_ENABLE_COLUMN_METADATA |
| | @@ -534,13 +537,10 @@ |
| 534 | 537 | "OMIT_BETWEEN_OPTIMIZATION", |
| 535 | 538 | #endif |
| 536 | 539 | #if SQLITE_OMIT_BLOB_LITERAL |
| 537 | 540 | "OMIT_BLOB_LITERAL", |
| 538 | 541 | #endif |
| 539 | | -#if SQLITE_OMIT_BTREECOUNT |
| 540 | | - "OMIT_BTREECOUNT", |
| 541 | | -#endif |
| 542 | 542 | #if SQLITE_OMIT_CAST |
| 543 | 543 | "OMIT_CAST", |
| 544 | 544 | #endif |
| 545 | 545 | #if SQLITE_OMIT_CHECK |
| 546 | 546 | "OMIT_CHECK", |
| | @@ -1160,13 +1160,13 @@ |
| 1160 | 1160 | ** |
| 1161 | 1161 | ** See also: [sqlite3_libversion()], |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | | -#define SQLITE_VERSION "3.32.0" |
| 1166 | | -#define SQLITE_VERSION_NUMBER 3032000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2020-04-20 17:35:32 2fc80ef16ce5878311ab88a0c64631813572ffbb71f75363b4619c9667e0926b" |
| 1165 | +#define SQLITE_VERSION "3.32.1" |
| 1166 | +#define SQLITE_VERSION_NUMBER 3032001 |
| 1167 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350" |
| 1168 | 1168 | |
| 1169 | 1169 | /* |
| 1170 | 1170 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1171 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1172 | ** |
| | @@ -1336,30 +1336,26 @@ |
| 1336 | 1336 | ** for the [sqlite3] object. |
| 1337 | 1337 | ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if |
| 1338 | 1338 | ** the [sqlite3] object is successfully destroyed and all associated |
| 1339 | 1339 | ** resources are deallocated. |
| 1340 | 1340 | ** |
| 1341 | +** Ideally, applications should [sqlite3_finalize | finalize] all |
| 1342 | +** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and |
| 1343 | +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated |
| 1344 | +** with the [sqlite3] object prior to attempting to close the object. |
| 1341 | 1345 | ** ^If the database connection is associated with unfinalized prepared |
| 1342 | | -** statements or unfinished sqlite3_backup objects then sqlite3_close() |
| 1343 | | -** will leave the database connection open and return [SQLITE_BUSY]. |
| 1344 | | -** ^If sqlite3_close_v2() is called with unfinalized prepared statements |
| 1345 | | -** and/or unfinished sqlite3_backups, then the database connection becomes |
| 1346 | | -** an unusable "zombie" which will automatically be deallocated when the |
| 1347 | | -** last prepared statement is finalized or the last sqlite3_backup is |
| 1348 | | -** finished. The sqlite3_close_v2() interface is intended for use with |
| 1349 | | -** host languages that are garbage collected, and where the order in which |
| 1350 | | -** destructors are called is arbitrary. |
| 1351 | | -** |
| 1352 | | -** Applications should [sqlite3_finalize | finalize] all [prepared statements], |
| 1353 | | -** [sqlite3_blob_close | close] all [BLOB handles], and |
| 1354 | | -** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated |
| 1355 | | -** with the [sqlite3] object prior to attempting to close the object. ^If |
| 1356 | | -** sqlite3_close_v2() is called on a [database connection] that still has |
| 1357 | | -** outstanding [prepared statements], [BLOB handles], and/or |
| 1358 | | -** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation |
| 1359 | | -** of resources is deferred until all [prepared statements], [BLOB handles], |
| 1360 | | -** and [sqlite3_backup] objects are also destroyed. |
| 1346 | +** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then |
| 1347 | +** sqlite3_close() will leave the database connection open and return |
| 1348 | +** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared |
| 1349 | +** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups, |
| 1350 | +** it returns [SQLITE_OK] regardless, but instead of deallocating the database |
| 1351 | +** connection immediately, it marks the database connection as an unusable |
| 1352 | +** "zombie" and makes arrangements to automatically deallocate the database |
| 1353 | +** connection after all prepared statements are finalized, all BLOB handles |
| 1354 | +** are closed, and all backups have finished. The sqlite3_close_v2() interface |
| 1355 | +** is intended for use with host languages that are garbage collected, and |
| 1356 | +** where the order in which destructors are called is arbitrary. |
| 1361 | 1357 | ** |
| 1362 | 1358 | ** ^If an [sqlite3] object is destroyed while a transaction is open, |
| 1363 | 1359 | ** the transaction is automatically rolled back. |
| 1364 | 1360 | ** |
| 1365 | 1361 | ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)] |
| | @@ -1544,22 +1540,25 @@ |
| 1544 | 1540 | #define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) |
| 1545 | 1541 | #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) |
| 1546 | 1542 | #define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8)) |
| 1547 | 1543 | #define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8)) |
| 1548 | 1544 | #define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8)) |
| 1545 | +#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8)) |
| 1549 | 1546 | #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) |
| 1550 | 1547 | #define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8)) |
| 1551 | 1548 | #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) |
| 1552 | 1549 | #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) |
| 1550 | +#define SQLITE_BUSY_TIMEOUT (SQLITE_BUSY | (3<<8)) |
| 1553 | 1551 | #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) |
| 1554 | 1552 | #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) |
| 1555 | 1553 | #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) |
| 1556 | 1554 | #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) |
| 1557 | 1555 | #define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */ |
| 1558 | 1556 | #define SQLITE_CANTOPEN_SYMLINK (SQLITE_CANTOPEN | (6<<8)) |
| 1559 | 1557 | #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) |
| 1560 | 1558 | #define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8)) |
| 1559 | +#define SQLITE_CORRUPT_INDEX (SQLITE_CORRUPT | (3<<8)) |
| 1561 | 1560 | #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) |
| 1562 | 1561 | #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) |
| 1563 | 1562 | #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) |
| 1564 | 1563 | #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) |
| 1565 | 1564 | #define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8)) |
| | @@ -2150,10 +2149,15 @@ |
| 2150 | 2149 | ** a single attached database that occur due to other database connections, |
| 2151 | 2150 | ** but omits changes implemented by the database connection on which it is |
| 2152 | 2151 | ** called. This file control is the only mechanism to detect changes that |
| 2153 | 2152 | ** happen either internally or externally and that are associated with |
| 2154 | 2153 | ** a particular attached database. |
| 2154 | +** |
| 2155 | +** <li>[[SQLITE_FCNTL_CKPT_START]] |
| 2156 | +** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint |
| 2157 | +** in wal mode before the client starts to copy pages from the wal |
| 2158 | +** file to the database file. |
| 2155 | 2159 | ** |
| 2156 | 2160 | ** <li>[[SQLITE_FCNTL_CKPT_DONE]] |
| 2157 | 2161 | ** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint |
| 2158 | 2162 | ** in wal mode after the client has finished copying pages from the wal |
| 2159 | 2163 | ** file to the database file, but before the *-shm file is updated to |
| | @@ -2195,10 +2199,11 @@ |
| 2195 | 2199 | #define SQLITE_FCNTL_LOCK_TIMEOUT 34 |
| 2196 | 2200 | #define SQLITE_FCNTL_DATA_VERSION 35 |
| 2197 | 2201 | #define SQLITE_FCNTL_SIZE_LIMIT 36 |
| 2198 | 2202 | #define SQLITE_FCNTL_CKPT_DONE 37 |
| 2199 | 2203 | #define SQLITE_FCNTL_RESERVE_BYTES 38 |
| 2204 | +#define SQLITE_FCNTL_CKPT_START 39 |
| 2200 | 2205 | |
| 2201 | 2206 | /* deprecated names */ |
| 2202 | 2207 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 2203 | 2208 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 2204 | 2209 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -4573,12 +4578,23 @@ |
| 4573 | 4578 | ** |
| 4574 | 4579 | ** These are utility routines, useful to [VFS|custom VFS implementations], |
| 4575 | 4580 | ** that check if a database file was a URI that contained a specific query |
| 4576 | 4581 | ** parameter, and if so obtains the value of that query parameter. |
| 4577 | 4582 | ** |
| 4578 | | -** If F is the database filename pointer passed into the xOpen() method of |
| 4579 | | -** a VFS implementation or it is the return value of [sqlite3_db_filename()] |
| 4583 | +** The first parameter to these interfaces (hereafter referred to |
| 4584 | +** as F) must be one of: |
| 4585 | +** <ul> |
| 4586 | +** <li> A database filename pointer created by the SQLite core and |
| 4587 | +** passed into the xOpen() method of a VFS implemention, or |
| 4588 | +** <li> A filename obtained from [sqlite3_db_filename()], or |
| 4589 | +** <li> A new filename constructed using [sqlite3_create_filename()]. |
| 4590 | +** </ul> |
| 4591 | +** If the F parameter is not one of the above, then the behavior is |
| 4592 | +** undefined and probably undesirable. Older versions of SQLite were |
| 4593 | +** more tolerant of invalid F parameters than newer versions. |
| 4594 | +** |
| 4595 | +** If F is a suitable filename (as described in the previous paragraph) |
| 4580 | 4596 | ** and if P is the name of the query parameter, then |
| 4581 | 4597 | ** sqlite3_uri_parameter(F,P) returns the value of the P |
| 4582 | 4598 | ** parameter if it exists or a NULL pointer if P does not appear as a |
| 4583 | 4599 | ** query parameter on F. If P is a query parameter of F and it |
| 4584 | 4600 | ** has no explicit value, then sqlite3_uri_parameter(F,P) returns |
| | @@ -4657,10 +4673,29 @@ |
| 4657 | 4673 | */ |
| 4658 | 4674 | SQLITE_API const char *sqlite3_filename_database(const char*); |
| 4659 | 4675 | SQLITE_API const char *sqlite3_filename_journal(const char*); |
| 4660 | 4676 | SQLITE_API const char *sqlite3_filename_wal(const char*); |
| 4661 | 4677 | |
| 4678 | +/* |
| 4679 | +** CAPI3REF: Database File Corresponding To A Journal |
| 4680 | +** |
| 4681 | +** ^If X is the name of a rollback or WAL-mode journal file that is |
| 4682 | +** passed into the xOpen method of [sqlite3_vfs], then |
| 4683 | +** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file] |
| 4684 | +** object that represents the main database file. |
| 4685 | +** |
| 4686 | +** This routine is intended for use in custom [VFS] implementations |
| 4687 | +** only. It is not a general-purpose interface. |
| 4688 | +** The argument sqlite3_file_object(X) must be a filename pointer that |
| 4689 | +** has been passed into [sqlite3_vfs].xOpen method where the |
| 4690 | +** flags parameter to xOpen contains one of the bits |
| 4691 | +** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL]. Any other use |
| 4692 | +** of this routine results in undefined and probably undesirable |
| 4693 | +** behavior. |
| 4694 | +*/ |
| 4695 | +SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*); |
| 4696 | + |
| 4662 | 4697 | /* |
| 4663 | 4698 | ** CAPI3REF: Create and Destroy VFS Filenames |
| 4664 | 4699 | ** |
| 4665 | 4700 | ** These interfces are provided for use by [VFS shim] implementations and |
| 4666 | 4701 | ** are not useful outside of that context. |
| | @@ -4691,11 +4726,11 @@ |
| 4691 | 4726 | ** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may |
| 4692 | 4727 | ** be NULL pointers, though they can be empty strings. |
| 4693 | 4728 | ** |
| 4694 | 4729 | ** The sqlite3_free_filename(Y) routine releases a memory allocation |
| 4695 | 4730 | ** previously obtained from sqlite3_create_filename(). Invoking |
| 4696 | | -** sqlite3_free_filename(Y) is a NULL pointer is a harmless no-op. |
| 4731 | +** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op. |
| 4697 | 4732 | ** |
| 4698 | 4733 | ** If the Y parameter to sqlite3_free_filename(Y) is anything other |
| 4699 | 4734 | ** than a NULL pointer or a pointer previously acquired from |
| 4700 | 4735 | ** sqlite3_create_filename(), then bad things such as heap |
| 4701 | 4736 | ** corruption or segfaults may occur. The value Y should be |
| | @@ -6497,11 +6532,11 @@ |
| 6497 | 6532 | ** when first called if N is less than or equal to zero or if a memory |
| 6498 | 6533 | ** allocate error occurs. |
| 6499 | 6534 | ** |
| 6500 | 6535 | ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is |
| 6501 | 6536 | ** determined by the N parameter on first successful call. Changing the |
| 6502 | | -** value of N in any subsequents call to sqlite3_aggregate_context() within |
| 6537 | +** value of N in any subsequent call to sqlite3_aggregate_context() within |
| 6503 | 6538 | ** the same aggregate function instance will not resize the memory |
| 6504 | 6539 | ** allocation.)^ Within the xFinal callback, it is customary to set |
| 6505 | 6540 | ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no |
| 6506 | 6541 | ** pointless memory allocations occur. |
| 6507 | 6542 | ** |
| | @@ -14502,11 +14537,10 @@ |
| 14502 | 14537 | typedef struct BusyHandler BusyHandler; |
| 14503 | 14538 | struct BusyHandler { |
| 14504 | 14539 | int (*xBusyHandler)(void *,int); /* The busy callback */ |
| 14505 | 14540 | void *pBusyArg; /* First arg to busy callback */ |
| 14506 | 14541 | int nBusy; /* Incremented with each busy call */ |
| 14507 | | - u8 bExtraFileArg; /* Include sqlite3_file as callback arg */ |
| 14508 | 14542 | }; |
| 14509 | 14543 | |
| 14510 | 14544 | /* |
| 14511 | 14545 | ** Name of the master database table. The master database table |
| 14512 | 14546 | ** is a special table that holds the names and attributes of all |
| | @@ -15022,13 +15056,11 @@ |
| 15022 | 15056 | #ifndef NDEBUG |
| 15023 | 15057 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| 15024 | 15058 | #endif |
| 15025 | 15059 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*); |
| 15026 | 15060 | |
| 15027 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 15028 | 15061 | SQLITE_PRIVATE int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*); |
| 15029 | | -#endif |
| 15030 | 15062 | |
| 15031 | 15063 | #ifdef SQLITE_TEST |
| 15032 | 15064 | SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int); |
| 15033 | 15065 | SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*); |
| 15034 | 15066 | #endif |
| | @@ -15599,10 +15631,13 @@ |
| 15599 | 15631 | |
| 15600 | 15632 | SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); |
| 15601 | 15633 | SQLITE_PRIVATE int sqlite3VdbeHasSubProgram(Vdbe*); |
| 15602 | 15634 | |
| 15603 | 15635 | SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*); |
| 15636 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 15637 | +SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3*); |
| 15638 | +#endif |
| 15604 | 15639 | |
| 15605 | 15640 | /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on |
| 15606 | 15641 | ** each VDBE opcode. |
| 15607 | 15642 | ** |
| 15608 | 15643 | ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op |
| | @@ -15884,17 +15919,25 @@ |
| 15884 | 15919 | SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); |
| 15885 | 15920 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); |
| 15886 | 15921 | SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); |
| 15887 | 15922 | SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); |
| 15888 | 15923 | # ifdef SQLITE_ENABLE_SNAPSHOT |
| 15889 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot); |
| 15890 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15924 | +SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager*, sqlite3_snapshot **ppSnapshot); |
| 15925 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager*, sqlite3_snapshot *pSnapshot); |
| 15891 | 15926 | SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager); |
| 15892 | 15927 | SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot); |
| 15893 | 15928 | SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager); |
| 15894 | 15929 | # endif |
| 15895 | 15930 | #endif |
| 15931 | + |
| 15932 | +#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_ENABLE_SETLK_TIMEOUT) |
| 15933 | +SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager*, int); |
| 15934 | +SQLITE_PRIVATE void sqlite3PagerWalDb(Pager*, sqlite3*); |
| 15935 | +#else |
| 15936 | +# define sqlite3PagerWalWriteLock(y,z) SQLITE_OK |
| 15937 | +# define sqlite3PagerWalDb(x,y) |
| 15938 | +#endif |
| 15896 | 15939 | |
| 15897 | 15940 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 15898 | 15941 | SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno); |
| 15899 | 15942 | #endif |
| 15900 | 15943 | |
| | @@ -15917,15 +15960,10 @@ |
| 15917 | 15960 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 15918 | 15961 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 15919 | 15962 | SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *); |
| 15920 | 15963 | SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*); |
| 15921 | 15964 | SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *); |
| 15922 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 15923 | | -SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager); |
| 15924 | | -#else |
| 15925 | | -# define sqlite3PagerResetLockTimeout(X) |
| 15926 | | -#endif |
| 15927 | 15965 | |
| 15928 | 15966 | /* Functions used to truncate the database file. */ |
| 15929 | 15967 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 15930 | 15968 | |
| 15931 | 15969 | SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16); |
| | @@ -16855,10 +16893,11 @@ |
| 16855 | 16893 | Hash aFunc; /* Hash table of connection functions */ |
| 16856 | 16894 | Hash aCollSeq; /* All collating sequences */ |
| 16857 | 16895 | BusyHandler busyHandler; /* Busy callback */ |
| 16858 | 16896 | Db aDbStatic[2]; /* Static space for the 2 default backends */ |
| 16859 | 16897 | Savepoint *pSavepoint; /* List of active savepoints */ |
| 16898 | + int nAnalysisLimit; /* Number of index rows to ANALYZE */ |
| 16860 | 16899 | int busyTimeout; /* Busy handler timeout, in msec */ |
| 16861 | 16900 | int nSavepoint; /* Number of non-transaction savepoints */ |
| 16862 | 16901 | int nStatement; /* Number of nested statement-transactions */ |
| 16863 | 16902 | i64 nDeferredCons; /* Net deferred constraints this transaction. */ |
| 16864 | 16903 | i64 nDeferredImmCons; /* Net deferred immediate constraints */ |
| | @@ -17080,11 +17119,11 @@ |
| 17080 | 17119 | #define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */ |
| 17081 | 17120 | #define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/ |
| 17082 | 17121 | #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */ |
| 17083 | 17122 | #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ |
| 17084 | 17123 | #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ |
| 17085 | | -#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ |
| 17124 | +/* 0x0200 -- available for reuse */ |
| 17086 | 17125 | #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ |
| 17087 | 17126 | #define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ |
| 17088 | 17127 | #define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */ |
| 17089 | 17128 | #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a |
| 17090 | 17129 | ** single query - might change over time */ |
| | @@ -17101,10 +17140,11 @@ |
| 17101 | 17140 | #define INLINEFUNC_coalesce 0 |
| 17102 | 17141 | #define INLINEFUNC_implies_nonnull_row 1 |
| 17103 | 17142 | #define INLINEFUNC_expr_implies_expr 2 |
| 17104 | 17143 | #define INLINEFUNC_expr_compare 3 |
| 17105 | 17144 | #define INLINEFUNC_affinity 4 |
| 17145 | +#define INLINEFUNC_iif 5 |
| 17106 | 17146 | #define INLINEFUNC_unlikely 99 /* Default case */ |
| 17107 | 17147 | |
| 17108 | 17148 | /* |
| 17109 | 17149 | ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are |
| 17110 | 17150 | ** used to create the initializers for the FuncDef structures. |
| | @@ -17801,11 +17841,11 @@ |
| 17801 | 17841 | /* |
| 17802 | 17842 | ** An instance of this structure contains information needed to generate |
| 17803 | 17843 | ** code for a SELECT that contains aggregate functions. |
| 17804 | 17844 | ** |
| 17805 | 17845 | ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a |
| 17806 | | -** pointer to this structure. The Expr.iColumn field is the index in |
| 17846 | +** pointer to this structure. The Expr.iAgg field is the index in |
| 17807 | 17847 | ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate |
| 17808 | 17848 | ** code for that node. |
| 17809 | 17849 | ** |
| 17810 | 17850 | ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the |
| 17811 | 17851 | ** original Select structure that describes the SELECT statement. These |
| | @@ -19040,10 +19080,13 @@ |
| 19040 | 19080 | SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*); |
| 19041 | 19081 | SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*); |
| 19042 | 19082 | SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*); |
| 19043 | 19083 | SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*); |
| 19044 | 19084 | SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*); |
| 19085 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker*,Select*); |
| 19086 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker*,Select*); |
| 19087 | + |
| 19045 | 19088 | #ifdef SQLITE_DEBUG |
| 19046 | 19089 | SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*); |
| 19047 | 19090 | #endif |
| 19048 | 19091 | |
| 19049 | 19092 | /* |
| | @@ -19900,11 +19943,11 @@ |
| 19900 | 19943 | SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*); |
| 19901 | 19944 | SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*); |
| 19902 | 19945 | SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); |
| 19903 | 19946 | SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*); |
| 19904 | 19947 | SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); |
| 19905 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*); |
| 19948 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); |
| 19906 | 19949 | SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); |
| 19907 | 19950 | SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); |
| 19908 | 19951 | SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); |
| 19909 | 19952 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); |
| 19910 | 19953 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); |
| | @@ -20025,12 +20068,14 @@ |
| 20025 | 20068 | # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) |
| 20026 | 20069 | #endif |
| 20027 | 20070 | SQLITE_PRIVATE int sqlite3ReadOnlyShadowTables(sqlite3 *db); |
| 20028 | 20071 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 20029 | 20072 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName); |
| 20073 | +SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3*,Table*,const char*); |
| 20030 | 20074 | #else |
| 20031 | 20075 | # define sqlite3ShadowTableName(A,B) 0 |
| 20076 | +# define sqlite3IsShadowTableOf(A,B,C) 0 |
| 20032 | 20077 | #endif |
| 20033 | 20078 | SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*); |
| 20034 | 20079 | SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*); |
| 20035 | 20080 | SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); |
| 20036 | 20081 | SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int); |
| | @@ -20636,11 +20681,12 @@ |
| 20636 | 20681 | /* |
| 20637 | 20682 | ** VDBE_DISPLAY_P4 is true or false depending on whether or not the |
| 20638 | 20683 | ** "explain" P4 display logic is enabled. |
| 20639 | 20684 | */ |
| 20640 | 20685 | #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
| 20641 | | - || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
| 20686 | + || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \ |
| 20687 | + || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 20642 | 20688 | # define VDBE_DISPLAY_P4 1 |
| 20643 | 20689 | #else |
| 20644 | 20690 | # define VDBE_DISPLAY_P4 0 |
| 20645 | 20691 | #endif |
| 20646 | 20692 | |
| | @@ -21101,11 +21147,18 @@ |
| 21101 | 21147 | |
| 21102 | 21148 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 21103 | 21149 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); |
| 21104 | 21150 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); |
| 21105 | 21151 | SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 21106 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 21152 | +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 21153 | +SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**); |
| 21154 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*); |
| 21155 | +#endif |
| 21156 | +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) |
| 21157 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*); |
| 21158 | +#endif |
| 21159 | +#if !defined(SQLITE_OMIT_EXPLAIN) |
| 21107 | 21160 | SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 21108 | 21161 | #endif |
| 21109 | 21162 | SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| 21110 | 21163 | SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); |
| 21111 | 21164 | SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); |
| | @@ -21143,11 +21196,11 @@ |
| 21143 | 21196 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 21144 | 21197 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 21145 | 21198 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 21146 | 21199 | SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); |
| 21147 | 21200 | #endif |
| 21148 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 21201 | +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 21149 | 21202 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 21150 | 21203 | #endif |
| 21151 | 21204 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 21152 | 21205 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); |
| 21153 | 21206 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| | @@ -27316,11 +27369,11 @@ |
| 27316 | 27369 | if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){ |
| 27317 | 27370 | n = mem0.hardLimit; |
| 27318 | 27371 | } |
| 27319 | 27372 | mem0.alarmThreshold = n; |
| 27320 | 27373 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27321 | | - mem0.nearlyFull = (n>0 && n<=nUsed); |
| 27374 | + AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed); |
| 27322 | 27375 | sqlite3_mutex_leave(mem0.mutex); |
| 27323 | 27376 | excess = sqlite3_memory_used() - n; |
| 27324 | 27377 | if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); |
| 27325 | 27378 | return priorLimit; |
| 27326 | 27379 | } |
| | @@ -27384,11 +27437,11 @@ |
| 27384 | 27437 | ** Return true if the heap is currently under memory pressure - in other |
| 27385 | 27438 | ** words if the amount of heap used is close to the limit set by |
| 27386 | 27439 | ** sqlite3_soft_heap_limit(). |
| 27387 | 27440 | */ |
| 27388 | 27441 | SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ |
| 27389 | | - return mem0.nearlyFull; |
| 27442 | + return AtomicLoad(&mem0.nearlyFull); |
| 27390 | 27443 | } |
| 27391 | 27444 | |
| 27392 | 27445 | /* |
| 27393 | 27446 | ** Deinitialize the memory allocation subsystem. |
| 27394 | 27447 | */ |
| | @@ -27448,21 +27501,21 @@ |
| 27448 | 27501 | |
| 27449 | 27502 | sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); |
| 27450 | 27503 | if( mem0.alarmThreshold>0 ){ |
| 27451 | 27504 | sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27452 | 27505 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 27453 | | - mem0.nearlyFull = 1; |
| 27506 | + AtomicStore(&mem0.nearlyFull, 1); |
| 27454 | 27507 | sqlite3MallocAlarm(nFull); |
| 27455 | 27508 | if( mem0.hardLimit ){ |
| 27456 | 27509 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27457 | 27510 | if( nUsed >= mem0.hardLimit - nFull ){ |
| 27458 | 27511 | *pp = 0; |
| 27459 | 27512 | return; |
| 27460 | 27513 | } |
| 27461 | 27514 | } |
| 27462 | 27515 | }else{ |
| 27463 | | - mem0.nearlyFull = 0; |
| 27516 | + AtomicStore(&mem0.nearlyFull, 0); |
| 27464 | 27517 | } |
| 27465 | 27518 | } |
| 27466 | 27519 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 27467 | 27520 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27468 | 27521 | if( p==0 && mem0.alarmThreshold>0 ){ |
| | @@ -27687,14 +27740,16 @@ |
| 27687 | 27740 | if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 27688 | 27741 | mem0.alarmThreshold-nDiff ){ |
| 27689 | 27742 | sqlite3MallocAlarm(nDiff); |
| 27690 | 27743 | } |
| 27691 | 27744 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27745 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27692 | 27746 | if( pNew==0 && mem0.alarmThreshold>0 ){ |
| 27693 | 27747 | sqlite3MallocAlarm((int)nBytes); |
| 27694 | 27748 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27695 | 27749 | } |
| 27750 | +#endif |
| 27696 | 27751 | if( pNew ){ |
| 27697 | 27752 | nNew = sqlite3MallocSize(pNew); |
| 27698 | 27753 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 27699 | 27754 | } |
| 27700 | 27755 | sqlite3_mutex_leave(mem0.mutex); |
| | @@ -27875,11 +27930,11 @@ |
| 27875 | 27930 | } |
| 27876 | 27931 | }else{ |
| 27877 | 27932 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 27878 | 27933 | assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 27879 | 27934 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 27880 | | - pNew = sqlite3_realloc64(p, n); |
| 27935 | + pNew = sqlite3Realloc(p, n); |
| 27881 | 27936 | if( !pNew ){ |
| 27882 | 27937 | sqlite3OomFault(db); |
| 27883 | 27938 | } |
| 27884 | 27939 | sqlite3MemdebugSetType(pNew, |
| 27885 | 27940 | (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| | @@ -28222,10 +28277,17 @@ |
| 28222 | 28277 | #ifndef SQLITE_PRINT_BUF_SIZE |
| 28223 | 28278 | # define SQLITE_PRINT_BUF_SIZE 70 |
| 28224 | 28279 | #endif |
| 28225 | 28280 | #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 28226 | 28281 | |
| 28282 | +/* |
| 28283 | +** Hard limit on the precision of floating-point conversions. |
| 28284 | +*/ |
| 28285 | +#ifndef SQLITE_PRINTF_PRECISION_LIMIT |
| 28286 | +# define SQLITE_FP_PRECISION_LIMIT 100000000 |
| 28287 | +#endif |
| 28288 | + |
| 28227 | 28289 | /* |
| 28228 | 28290 | ** Render a string given by "fmt" into the StrAccum object. |
| 28229 | 28291 | */ |
| 28230 | 28292 | SQLITE_API void sqlite3_str_vappendf( |
| 28231 | 28293 | sqlite3_str *pAccum, /* Accumulate results here */ |
| | @@ -28422,10 +28484,12 @@ |
| 28422 | 28484 | ** precision The specified precision. The default |
| 28423 | 28485 | ** is -1. |
| 28424 | 28486 | ** xtype The class of the conversion. |
| 28425 | 28487 | ** infop Pointer to the appropriate info struct. |
| 28426 | 28488 | */ |
| 28489 | + assert( width>=0 ); |
| 28490 | + assert( precision>=(-1) ); |
| 28427 | 28491 | switch( xtype ){ |
| 28428 | 28492 | case etPOINTER: |
| 28429 | 28493 | flag_long = sizeof(char*)==sizeof(i64) ? 2 : |
| 28430 | 28494 | sizeof(char*)==sizeof(long int) ? 1 : 0; |
| 28431 | 28495 | /* Fall through into the next case */ |
| | @@ -28543,10 +28607,15 @@ |
| 28543 | 28607 | } |
| 28544 | 28608 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 28545 | 28609 | length = 0; |
| 28546 | 28610 | #else |
| 28547 | 28611 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 28612 | +#ifdef SQLITE_FP_PRECISION_LIMIT |
| 28613 | + if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 28614 | + precision = SQLITE_FP_PRECISION_LIMIT; |
| 28615 | + } |
| 28616 | +#endif |
| 28548 | 28617 | if( realvalue<0.0 ){ |
| 28549 | 28618 | realvalue = -realvalue; |
| 28550 | 28619 | prefix = '-'; |
| 28551 | 28620 | }else{ |
| 28552 | 28621 | prefix = flag_prefix; |
| | @@ -28825,11 +28894,11 @@ |
| 28825 | 28894 | }else{ |
| 28826 | 28895 | escarg = va_arg(ap,char*); |
| 28827 | 28896 | } |
| 28828 | 28897 | isnull = escarg==0; |
| 28829 | 28898 | if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 28830 | | - /* For %q, %Q, and %w, the precision is the number of byte (or |
| 28899 | + /* For %q, %Q, and %w, the precision is the number of bytes (or |
| 28831 | 28900 | ** characters if the ! flags is present) to use from the input. |
| 28832 | 28901 | ** Because of the extra quoting characters inserted, the number |
| 28833 | 28902 | ** of output characters may be larger than the precision. |
| 28834 | 28903 | */ |
| 28835 | 28904 | k = precision; |
| | @@ -28952,11 +29021,11 @@ |
| 28952 | 29021 | p->nAlloc = (int)szNew; |
| 28953 | 29022 | } |
| 28954 | 29023 | if( p->db ){ |
| 28955 | 29024 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 28956 | 29025 | }else{ |
| 28957 | | - zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 29026 | + zNew = sqlite3Realloc(zOld, p->nAlloc); |
| 28958 | 29027 | } |
| 28959 | 29028 | if( zNew ){ |
| 28960 | 29029 | assert( p->zText!=0 || p->nChar==0 ); |
| 28961 | 29030 | if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 28962 | 29031 | p->zText = zNew; |
| | @@ -29294,11 +29363,11 @@ |
| 29294 | 29363 | ** and segfaults if you give it a long long int. |
| 29295 | 29364 | */ |
| 29296 | 29365 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 29297 | 29366 | va_list ap; |
| 29298 | 29367 | StrAccum acc; |
| 29299 | | - char zBuf[500]; |
| 29368 | + char zBuf[SQLITE_PRINT_BUF_SIZE*10]; |
| 29300 | 29369 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 29301 | 29370 | va_start(ap,zFormat); |
| 29302 | 29371 | sqlite3_str_vappendf(&acc, zFormat, ap); |
| 29303 | 29372 | va_end(ap); |
| 29304 | 29373 | sqlite3StrAccumFinish(&acc); |
| | @@ -29910,12 +29979,13 @@ |
| 29910 | 29979 | #else |
| 29911 | 29980 | pWin = 0; |
| 29912 | 29981 | #endif |
| 29913 | 29982 | } |
| 29914 | 29983 | if( pExpr->op==TK_AGG_FUNCTION ){ |
| 29915 | | - sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s", |
| 29916 | | - pExpr->op2, pExpr->u.zToken, zFlgs); |
| 29984 | + sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q%s iAgg=%d agg=%p", |
| 29985 | + pExpr->op2, pExpr->u.zToken, zFlgs, |
| 29986 | + pExpr->iAgg, pExpr->pAggInfo); |
| 29917 | 29987 | }else if( pExpr->op2!=0 ){ |
| 29918 | 29988 | const char *zOp2; |
| 29919 | 29989 | char zBuf[8]; |
| 29920 | 29990 | sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); |
| 29921 | 29991 | zOp2 = zBuf; |
| | @@ -30804,10 +30874,11 @@ |
| 30804 | 30874 | /* UTF-16 Little-endian -> UTF-8 */ |
| 30805 | 30875 | while( zIn<zTerm ){ |
| 30806 | 30876 | c = *(zIn++); |
| 30807 | 30877 | c += (*(zIn++))<<8; |
| 30808 | 30878 | if( c>=0xd800 && c<0xe000 ){ |
| 30879 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30809 | 30880 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30810 | 30881 | c = 0xfffd; |
| 30811 | 30882 | }else{ |
| 30812 | 30883 | int c2 = *(zIn++); |
| 30813 | 30884 | c2 += (*(zIn++))<<8; |
| | @@ -30816,19 +30887,27 @@ |
| 30816 | 30887 | c = 0xfffd; |
| 30817 | 30888 | }else{ |
| 30818 | 30889 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30819 | 30890 | } |
| 30820 | 30891 | } |
| 30892 | +#else |
| 30893 | + if( zIn<zTerm ){ |
| 30894 | + int c2 = (*zIn++); |
| 30895 | + c2 += ((*zIn++)<<8); |
| 30896 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30897 | + } |
| 30898 | +#endif |
| 30821 | 30899 | } |
| 30822 | 30900 | WRITE_UTF8(z, c); |
| 30823 | 30901 | } |
| 30824 | 30902 | }else{ |
| 30825 | 30903 | /* UTF-16 Big-endian -> UTF-8 */ |
| 30826 | 30904 | while( zIn<zTerm ){ |
| 30827 | 30905 | c = (*(zIn++))<<8; |
| 30828 | 30906 | c += *(zIn++); |
| 30829 | 30907 | if( c>=0xd800 && c<0xe000 ){ |
| 30908 | +#ifdef SQLITE_REPLACE_INVALID_UTF |
| 30830 | 30909 | if( c>=0xdc00 || zIn>=zTerm ){ |
| 30831 | 30910 | c = 0xfffd; |
| 30832 | 30911 | }else{ |
| 30833 | 30912 | int c2 = (*(zIn++))<<8; |
| 30834 | 30913 | c2 += *(zIn++); |
| | @@ -30837,10 +30916,17 @@ |
| 30837 | 30916 | c = 0xfffd; |
| 30838 | 30917 | }else{ |
| 30839 | 30918 | c = ((c&0x3ff)<<10) + (c2&0x3ff) + 0x10000; |
| 30840 | 30919 | } |
| 30841 | 30920 | } |
| 30921 | +#else |
| 30922 | + if( zIn<zTerm ){ |
| 30923 | + int c2 = ((*zIn++)<<8); |
| 30924 | + c2 += (*zIn++); |
| 30925 | + c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); |
| 30926 | + } |
| 30927 | +#endif |
| 30842 | 30928 | } |
| 30843 | 30929 | WRITE_UTF8(z, c); |
| 30844 | 30930 | } |
| 30845 | 30931 | } |
| 30846 | 30932 | pMem->n = (int)(z - zOut); |
| | @@ -34928,20 +35014,21 @@ |
| 34928 | 35014 | static int osSetPosixAdvisoryLock( |
| 34929 | 35015 | int h, /* The file descriptor on which to take the lock */ |
| 34930 | 35016 | struct flock *pLock, /* The description of the lock */ |
| 34931 | 35017 | unixFile *pFile /* Structure holding timeout value */ |
| 34932 | 35018 | ){ |
| 35019 | + int tm = pFile->iBusyTimeout; |
| 34933 | 35020 | int rc = osFcntl(h,F_SETLK,pLock); |
| 34934 | | - while( rc<0 && pFile->iBusyTimeout>0 ){ |
| 35021 | + while( rc<0 && tm>0 ){ |
| 34935 | 35022 | /* On systems that support some kind of blocking file lock with a timeout, |
| 34936 | 35023 | ** make appropriate changes here to invoke that blocking file lock. On |
| 34937 | 35024 | ** generic posix, however, there is no such API. So we simply try the |
| 34938 | 35025 | ** lock once every millisecond until either the timeout expires, or until |
| 34939 | 35026 | ** the lock is obtained. */ |
| 34940 | 35027 | usleep(1000); |
| 34941 | 35028 | rc = osFcntl(h,F_SETLK,pLock); |
| 34942 | | - pFile->iBusyTimeout--; |
| 35029 | + tm--; |
| 34943 | 35030 | } |
| 34944 | 35031 | return rc; |
| 34945 | 35032 | } |
| 34946 | 35033 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 34947 | 35034 | |
| | @@ -37679,17 +37766,24 @@ |
| 37679 | 37766 | |
| 37680 | 37767 | /* Locks are within range */ |
| 37681 | 37768 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 37682 | 37769 | |
| 37683 | 37770 | if( pShmNode->hShm>=0 ){ |
| 37771 | + int res; |
| 37684 | 37772 | /* Initialize the locking parameters */ |
| 37685 | 37773 | f.l_type = lockType; |
| 37686 | 37774 | f.l_whence = SEEK_SET; |
| 37687 | 37775 | f.l_start = ofst; |
| 37688 | 37776 | f.l_len = n; |
| 37689 | | - rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37690 | | - rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 37777 | + res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37778 | + if( res==-1 ){ |
| 37779 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 37780 | + rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); |
| 37781 | +#else |
| 37782 | + rc = SQLITE_BUSY; |
| 37783 | +#endif |
| 37784 | + } |
| 37691 | 37785 | } |
| 37692 | 37786 | |
| 37693 | 37787 | /* Update the global lock state and do debug tracing */ |
| 37694 | 37788 | #ifdef SQLITE_DEBUG |
| 37695 | 37789 | { u16 mask; |
| | @@ -38182,26 +38276,27 @@ |
| 38182 | 38276 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 38183 | 38277 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 38184 | 38278 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 38185 | 38279 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
| 38186 | 38280 | |
| 38187 | | - /* Check that, if this to be a blocking lock, that locks have been |
| 38188 | | - ** obtained in the following order. |
| 38281 | + /* Check that, if this to be a blocking lock, no locks that occur later |
| 38282 | + ** in the following list than the lock being obtained are already held: |
| 38189 | 38283 | ** |
| 38190 | 38284 | ** 1. Checkpointer lock (ofst==1). |
| 38191 | | - ** 2. Recover lock (ofst==2). |
| 38285 | + ** 2. Write lock (ofst==0). |
| 38192 | 38286 | ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK). |
| 38193 | | - ** 4. Write lock (ofst==0). |
| 38194 | 38287 | ** |
| 38195 | 38288 | ** In other words, if this is a blocking lock, none of the locks that |
| 38196 | 38289 | ** occur later in the above list than the lock being obtained may be |
| 38197 | 38290 | ** held. */ |
| 38198 | 38291 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 38199 | | - assert( pDbFd->iBusyTimeout==0 |
| 38200 | | - || (flags & SQLITE_SHM_UNLOCK) || ofst==0 |
| 38201 | | - || ((p->exclMask|p->sharedMask)&~((1<<ofst)-2))==0 |
| 38202 | | - ); |
| 38292 | + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 38293 | + (ofst!=2) /* not RECOVER */ |
| 38294 | + && (ofst!=1 || (p->exclMask|p->sharedMask)==0) |
| 38295 | + && (ofst!=0 || (p->exclMask|p->sharedMask)<3) |
| 38296 | + && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst)) |
| 38297 | + )); |
| 38203 | 38298 | #endif |
| 38204 | 38299 | |
| 38205 | 38300 | mask = (1<<(ofst+n)) - (1<<ofst); |
| 38206 | 38301 | assert( n>1 || mask==(1<<ofst) ); |
| 38207 | 38302 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
| | @@ -44990,10 +45085,11 @@ |
| 44990 | 45085 | } |
| 44991 | 45086 | |
| 44992 | 45087 | /* Forward references to VFS helper methods used for temporary files */ |
| 44993 | 45088 | static int winGetTempname(sqlite3_vfs *, char **); |
| 44994 | 45089 | static int winIsDir(const void *); |
| 45090 | +static BOOL winIsLongPathPrefix(const char *); |
| 44995 | 45091 | static BOOL winIsDriveLetterAndColon(const char *); |
| 44996 | 45092 | |
| 44997 | 45093 | /* |
| 44998 | 45094 | ** Control and query of the open file handle. |
| 44999 | 45095 | */ |
| | @@ -46759,11 +46855,13 @@ |
| 46759 | 46855 | pFile->pVfs = pVfs; |
| 46760 | 46856 | pFile->h = h; |
| 46761 | 46857 | if( isReadonly ){ |
| 46762 | 46858 | pFile->ctrlFlags |= WINFILE_RDONLY; |
| 46763 | 46859 | } |
| 46764 | | - if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 46860 | + if( (flags & SQLITE_OPEN_MAIN_DB) |
| 46861 | + && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) |
| 46862 | + ){ |
| 46765 | 46863 | pFile->ctrlFlags |= WINFILE_PSOW; |
| 46766 | 46864 | } |
| 46767 | 46865 | pFile->lastErrno = NO_ERROR; |
| 46768 | 46866 | pFile->zPath = zName; |
| 46769 | 46867 | #if SQLITE_MAX_MMAP_SIZE>0 |
| | @@ -46968,10 +47066,21 @@ |
| 46968 | 47066 | *pResOut = rc; |
| 46969 | 47067 | OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 46970 | 47068 | zFilename, pResOut, *pResOut)); |
| 46971 | 47069 | return SQLITE_OK; |
| 46972 | 47070 | } |
| 47071 | + |
| 47072 | +/* |
| 47073 | +** Returns non-zero if the specified path name starts with the "long path" |
| 47074 | +** prefix. |
| 47075 | +*/ |
| 47076 | +static BOOL winIsLongPathPrefix( |
| 47077 | + const char *zPathname |
| 47078 | +){ |
| 47079 | + return ( zPathname[0]=='\\' && zPathname[1]=='\\' |
| 47080 | + && zPathname[2]=='?' && zPathname[3]=='\\' ); |
| 47081 | +} |
| 46973 | 47082 | |
| 46974 | 47083 | /* |
| 46975 | 47084 | ** Returns non-zero if the specified path name starts with a drive letter |
| 46976 | 47085 | ** followed by a colon character. |
| 46977 | 47086 | */ |
| | @@ -47033,14 +47142,15 @@ |
| 47033 | 47142 | DWORD nByte; |
| 47034 | 47143 | void *zConverted; |
| 47035 | 47144 | char *zOut; |
| 47036 | 47145 | #endif |
| 47037 | 47146 | |
| 47038 | | - /* If this path name begins with "/X:", where "X" is any alphabetic |
| 47039 | | - ** character, discard the initial "/" from the pathname. |
| 47147 | + /* If this path name begins with "/X:" or "\\?\", where "X" is any |
| 47148 | + ** alphabetic character, discard the initial "/" from the pathname. |
| 47040 | 47149 | */ |
| 47041 | | - if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ |
| 47150 | + if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1) |
| 47151 | + || winIsLongPathPrefix(zRelative+1)) ){ |
| 47042 | 47152 | zRelative++; |
| 47043 | 47153 | } |
| 47044 | 47154 | |
| 47045 | 47155 | #if defined(__CYGWIN__) |
| 47046 | 47156 | SimulateIOError( return SQLITE_ERROR ); |
| | @@ -47792,11 +47902,11 @@ |
| 47792 | 47902 | if( newSz>p->szMax ){ |
| 47793 | 47903 | return SQLITE_FULL; |
| 47794 | 47904 | } |
| 47795 | 47905 | newSz *= 2; |
| 47796 | 47906 | if( newSz>p->szMax ) newSz = p->szMax; |
| 47797 | | - pNew = sqlite3_realloc64(p->aData, newSz); |
| 47907 | + pNew = sqlite3Realloc(p->aData, newSz); |
| 47798 | 47908 | if( pNew==0 ) return SQLITE_NOMEM; |
| 47799 | 47909 | p->aData = pNew; |
| 47800 | 47910 | p->szAlloc = newSz; |
| 47801 | 47911 | return SQLITE_OK; |
| 47802 | 47912 | } |
| | @@ -48239,14 +48349,15 @@ |
| 48239 | 48349 | */ |
| 48240 | 48350 | SQLITE_PRIVATE int sqlite3MemdbInit(void){ |
| 48241 | 48351 | sqlite3_vfs *pLower = sqlite3_vfs_find(0); |
| 48242 | 48352 | int sz = pLower->szOsFile; |
| 48243 | 48353 | memdb_vfs.pAppData = pLower; |
| 48244 | | - /* In all known configurations of SQLite, the size of a default |
| 48245 | | - ** sqlite3_file is greater than the size of a memdb sqlite3_file. |
| 48246 | | - ** Should that ever change, remove the following NEVER() */ |
| 48247 | | - if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile); |
| 48354 | + /* The following conditional can only be true when compiled for |
| 48355 | + ** Windows x86 and SQLITE_MAX_MMAP_SIZE=0. We always leave |
| 48356 | + ** it in, to be safe, but it is marked as NO_TEST since there |
| 48357 | + ** is no way to reach it under most builds. */ |
| 48358 | + if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/ |
| 48248 | 48359 | memdb_vfs.szOsFile = sz; |
| 48249 | 48360 | return sqlite3_vfs_register(&memdb_vfs, 0); |
| 48250 | 48361 | } |
| 48251 | 48362 | #endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 48252 | 48363 | |
| | @@ -51505,10 +51616,15 @@ |
| 51505 | 51616 | SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal); |
| 51506 | 51617 | #endif |
| 51507 | 51618 | |
| 51508 | 51619 | /* Return the sqlite3_file object for the WAL file */ |
| 51509 | 51620 | SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal); |
| 51621 | + |
| 51622 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 51623 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock); |
| 51624 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db); |
| 51625 | +#endif |
| 51510 | 51626 | |
| 51511 | 51627 | #endif /* ifndef SQLITE_OMIT_WAL */ |
| 51512 | 51628 | #endif /* SQLITE_WAL_H */ |
| 51513 | 51629 | |
| 51514 | 51630 | /************** End of wal.h *************************************************/ |
| | @@ -54026,13 +54142,16 @@ |
| 54026 | 54142 | } |
| 54027 | 54143 | if( exists ){ |
| 54028 | 54144 | /* One of the journals pointed to by the master journal exists. |
| 54029 | 54145 | ** Open it and check if it points at the master journal. If |
| 54030 | 54146 | ** so, return without deleting the master journal file. |
| 54147 | + ** NB: zJournal is really a MAIN_JOURNAL. But call it a |
| 54148 | + ** MASTER_JOURNAL here so that the VFS will not send the zJournal |
| 54149 | + ** name into sqlite3_database_file_object(). |
| 54031 | 54150 | */ |
| 54032 | 54151 | int c; |
| 54033 | | - int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 54152 | + int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 54034 | 54153 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
| 54035 | 54154 | if( rc!=SQLITE_OK ){ |
| 54036 | 54155 | goto delmaster_out; |
| 54037 | 54156 | } |
| 54038 | 54157 | |
| | @@ -56232,10 +56351,11 @@ |
| 56232 | 56351 | ** Pager object (sizeof(Pager) bytes) |
| 56233 | 56352 | ** PCache object (sqlite3PcacheSize() bytes) |
| 56234 | 56353 | ** Database file handle (pVfs->szOsFile bytes) |
| 56235 | 56354 | ** Sub-journal file handle (journalFileSize bytes) |
| 56236 | 56355 | ** Main journal file handle (journalFileSize bytes) |
| 56356 | + ** Ptr back to the Pager (sizeof(Pager*) bytes) |
| 56237 | 56357 | ** \0\0\0\0 database prefix (4 bytes) |
| 56238 | 56358 | ** Database file name (nPathname+1 bytes) |
| 56239 | 56359 | ** URI query parameters (nUriByte bytes) |
| 56240 | 56360 | ** Journal filename (nPathname+8+1 bytes) |
| 56241 | 56361 | ** WAL filename (nPathname+4+1 bytes) |
| | @@ -56271,10 +56391,11 @@ |
| 56271 | 56391 | pPtr = (u8 *)sqlite3MallocZero( |
| 56272 | 56392 | ROUND8(sizeof(*pPager)) + /* Pager structure */ |
| 56273 | 56393 | ROUND8(pcacheSize) + /* PCache object */ |
| 56274 | 56394 | ROUND8(pVfs->szOsFile) + /* The main db file */ |
| 56275 | 56395 | journalFileSize * 2 + /* The two journal files */ |
| 56396 | + sizeof(pPager) + /* Space to hold a pointer */ |
| 56276 | 56397 | 4 + /* Database prefix */ |
| 56277 | 56398 | nPathname + 1 + /* database filename */ |
| 56278 | 56399 | nUriByte + /* query parameters */ |
| 56279 | 56400 | nPathname + 8 + 1 + /* Journal filename */ |
| 56280 | 56401 | #ifndef SQLITE_OMIT_WAL |
| | @@ -56291,10 +56412,11 @@ |
| 56291 | 56412 | pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize); |
| 56292 | 56413 | pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile); |
| 56293 | 56414 | pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; |
| 56294 | 56415 | pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; |
| 56295 | 56416 | assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 56417 | + memcpy(pPtr, &pPager, sizeof(pPager)); pPtr += sizeof(pPager); |
| 56296 | 56418 | |
| 56297 | 56419 | /* Fill in the Pager.zFilename and pPager.zQueryParam fields */ |
| 56298 | 56420 | pPtr += 4; /* Skip zero prefix */ |
| 56299 | 56421 | pPager->zFilename = (char*)pPtr; |
| 56300 | 56422 | if( nPathname>0 ){ |
| | @@ -56491,10 +56613,23 @@ |
| 56491 | 56613 | |
| 56492 | 56614 | *ppPager = pPager; |
| 56493 | 56615 | return SQLITE_OK; |
| 56494 | 56616 | } |
| 56495 | 56617 | |
| 56618 | +/* |
| 56619 | +** Return the sqlite3_file for the main database given the name |
| 56620 | +** of the corresonding WAL or Journal name as passed into |
| 56621 | +** xOpen. |
| 56622 | +*/ |
| 56623 | +SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){ |
| 56624 | + Pager *pPager; |
| 56625 | + while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){ |
| 56626 | + zName--; |
| 56627 | + } |
| 56628 | + pPager = *(Pager**)(zName - 4 - sizeof(Pager*)); |
| 56629 | + return pPager->fd; |
| 56630 | +} |
| 56496 | 56631 | |
| 56497 | 56632 | |
| 56498 | 56633 | /* |
| 56499 | 56634 | ** This function is called after transitioning from PAGER_UNLOCK to |
| 56500 | 56635 | ** PAGER_SHARED state. It tests if there is a hot journal present in |
| | @@ -57176,11 +57311,10 @@ |
| 57176 | 57311 | Pager *pPager; |
| 57177 | 57312 | assert( pPg!=0 ); |
| 57178 | 57313 | assert( pPg->pgno==1 ); |
| 57179 | 57314 | assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */ |
| 57180 | 57315 | pPager = pPg->pPager; |
| 57181 | | - sqlite3PagerResetLockTimeout(pPager); |
| 57182 | 57316 | sqlite3PcacheRelease(pPg); |
| 57183 | 57317 | pagerUnlockIfUnused(pPager); |
| 57184 | 57318 | } |
| 57185 | 57319 | |
| 57186 | 57320 | /* |
| | @@ -58469,20 +58603,10 @@ |
| 58469 | 58603 | */ |
| 58470 | 58604 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 58471 | 58605 | return pPager->fd; |
| 58472 | 58606 | } |
| 58473 | 58607 | |
| 58474 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 58475 | | -/* |
| 58476 | | -** Reset the lock timeout for pager. |
| 58477 | | -*/ |
| 58478 | | -SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){ |
| 58479 | | - int x = 0; |
| 58480 | | - sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x); |
| 58481 | | -} |
| 58482 | | -#endif |
| 58483 | | - |
| 58484 | 58608 | /* |
| 58485 | 58609 | ** Return the file handle for the journal file (if it exists). |
| 58486 | 58610 | ** This will be either the rollback journal or the WAL file. |
| 58487 | 58611 | */ |
| 58488 | 58612 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ |
| | @@ -58892,11 +59016,10 @@ |
| 58892 | 59016 | (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 58893 | 59017 | pPager->pBusyHandlerArg, |
| 58894 | 59018 | pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| 58895 | 59019 | pnLog, pnCkpt |
| 58896 | 59020 | ); |
| 58897 | | - sqlite3PagerResetLockTimeout(pPager); |
| 58898 | 59021 | } |
| 58899 | 59022 | return rc; |
| 58900 | 59023 | } |
| 58901 | 59024 | |
| 58902 | 59025 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){ |
| | @@ -59057,11 +59180,35 @@ |
| 59057 | 59180 | } |
| 59058 | 59181 | } |
| 59059 | 59182 | return rc; |
| 59060 | 59183 | } |
| 59061 | 59184 | |
| 59185 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59186 | +/* |
| 59187 | +** If pager pPager is a wal-mode database not in exclusive locking mode, |
| 59188 | +** invoke the sqlite3WalWriteLock() function on the associated Wal object |
| 59189 | +** with the same db and bLock parameters as were passed to this function. |
| 59190 | +** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. |
| 59191 | +*/ |
| 59192 | +SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){ |
| 59193 | + int rc = SQLITE_OK; |
| 59194 | + if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){ |
| 59195 | + rc = sqlite3WalWriteLock(pPager->pWal, bLock); |
| 59196 | + } |
| 59197 | + return rc; |
| 59198 | +} |
| 59062 | 59199 | |
| 59200 | +/* |
| 59201 | +** Set the database handle used by the wal layer to determine if |
| 59202 | +** blocking locks are required. |
| 59203 | +*/ |
| 59204 | +SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){ |
| 59205 | + if( pagerUseWal(pPager) ){ |
| 59206 | + sqlite3WalDb(pPager->pWal, db); |
| 59207 | + } |
| 59208 | +} |
| 59209 | +#endif |
| 59063 | 59210 | |
| 59064 | 59211 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59065 | 59212 | /* |
| 59066 | 59213 | ** If this is a WAL database, obtain a snapshot handle for the snapshot |
| 59067 | 59214 | ** currently open. Otherwise, return an error. |
| | @@ -59077,11 +59224,14 @@ |
| 59077 | 59224 | /* |
| 59078 | 59225 | ** If this is a WAL database, store a pointer to pSnapshot. Next time a |
| 59079 | 59226 | ** read transaction is opened, attempt to read from the snapshot it |
| 59080 | 59227 | ** identifies. If this is not a WAL database, return an error. |
| 59081 | 59228 | */ |
| 59082 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ |
| 59229 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen( |
| 59230 | + Pager *pPager, |
| 59231 | + sqlite3_snapshot *pSnapshot |
| 59232 | +){ |
| 59083 | 59233 | int rc = SQLITE_OK; |
| 59084 | 59234 | if( pPager->pWal ){ |
| 59085 | 59235 | sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); |
| 59086 | 59236 | }else{ |
| 59087 | 59237 | rc = SQLITE_ERROR; |
| | @@ -59622,10 +59772,13 @@ |
| 59622 | 59772 | u8 lockError; /* True if a locking error has occurred */ |
| 59623 | 59773 | #endif |
| 59624 | 59774 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59625 | 59775 | WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ |
| 59626 | 59776 | #endif |
| 59777 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59778 | + sqlite3 *db; |
| 59779 | +#endif |
| 59627 | 59780 | }; |
| 59628 | 59781 | |
| 59629 | 59782 | /* |
| 59630 | 59783 | ** Candidate values for Wal.exclusiveMode. |
| 59631 | 59784 | */ |
| | @@ -59720,11 +59873,11 @@ |
| 59720 | 59873 | |
| 59721 | 59874 | /* Enlarge the pWal->apWiData[] array if required */ |
| 59722 | 59875 | if( pWal->nWiData<=iPage ){ |
| 59723 | 59876 | sqlite3_int64 nByte = sizeof(u32*)*(iPage+1); |
| 59724 | 59877 | volatile u32 **apNew; |
| 59725 | | - apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); |
| 59878 | + apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte); |
| 59726 | 59879 | if( !apNew ){ |
| 59727 | 59880 | *ppPage = 0; |
| 59728 | 59881 | return SQLITE_NOMEM_BKPT; |
| 59729 | 59882 | } |
| 59730 | 59883 | memset((void*)&apNew[pWal->nWiData], 0, |
| | @@ -59841,29 +59994,47 @@ |
| 59841 | 59994 | |
| 59842 | 59995 | aOut[0] = s1; |
| 59843 | 59996 | aOut[1] = s2; |
| 59844 | 59997 | } |
| 59845 | 59998 | |
| 59999 | +/* |
| 60000 | +** If there is the possibility of concurrent access to the SHM file |
| 60001 | +** from multiple threads and/or processes, then do a memory barrier. |
| 60002 | +*/ |
| 59846 | 60003 | static void walShmBarrier(Wal *pWal){ |
| 59847 | 60004 | if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){ |
| 59848 | 60005 | sqlite3OsShmBarrier(pWal->pDbFd); |
| 59849 | 60006 | } |
| 59850 | 60007 | } |
| 59851 | 60008 | |
| 60009 | +/* |
| 60010 | +** Add the SQLITE_NO_TSAN as part of the return-type of a function |
| 60011 | +** definition as a hint that the function contains constructs that |
| 60012 | +** might give false-positive TSAN warnings. |
| 60013 | +** |
| 60014 | +** See tag-20200519-1. |
| 60015 | +*/ |
| 60016 | +#if defined(__clang__) && !defined(SQLITE_NO_TSAN) |
| 60017 | +# define SQLITE_NO_TSAN __attribute__((no_sanitize_thread)) |
| 60018 | +#else |
| 60019 | +# define SQLITE_NO_TSAN |
| 60020 | +#endif |
| 60021 | + |
| 59852 | 60022 | /* |
| 59853 | 60023 | ** Write the header information in pWal->hdr into the wal-index. |
| 59854 | 60024 | ** |
| 59855 | 60025 | ** The checksum on pWal->hdr is updated before it is written. |
| 59856 | 60026 | */ |
| 59857 | | -static void walIndexWriteHdr(Wal *pWal){ |
| 60027 | +static SQLITE_NO_TSAN void walIndexWriteHdr(Wal *pWal){ |
| 59858 | 60028 | volatile WalIndexHdr *aHdr = walIndexHdr(pWal); |
| 59859 | 60029 | const int nCksum = offsetof(WalIndexHdr, aCksum); |
| 59860 | 60030 | |
| 59861 | 60031 | assert( pWal->writeLock ); |
| 59862 | 60032 | pWal->hdr.isInit = 1; |
| 59863 | 60033 | pWal->hdr.iVersion = WALINDEX_MAX_VERSION; |
| 59864 | 60034 | walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); |
| 60035 | + /* Possible TSAN false-positive. See tag-20200519-1 */ |
| 59865 | 60036 | memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59866 | 60037 | walShmBarrier(pWal); |
| 59867 | 60038 | memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); |
| 59868 | 60039 | } |
| 59869 | 60040 | |
| | @@ -59995,11 +60166,11 @@ |
| 59995 | 60166 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 59996 | 60167 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 59997 | 60168 | SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); |
| 59998 | 60169 | WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, |
| 59999 | 60170 | walLockName(lockIdx), rc ? "failed" : "ok")); |
| 60000 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60171 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60001 | 60172 | return rc; |
| 60002 | 60173 | } |
| 60003 | 60174 | static void walUnlockShared(Wal *pWal, int lockIdx){ |
| 60004 | 60175 | if( pWal->exclusiveMode ) return; |
| 60005 | 60176 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| | @@ -60011,11 +60182,11 @@ |
| 60011 | 60182 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 60012 | 60183 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 60013 | 60184 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 60014 | 60185 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 60015 | 60186 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
| 60016 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60187 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60017 | 60188 | return rc; |
| 60018 | 60189 | } |
| 60019 | 60190 | static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ |
| 60020 | 60191 | if( pWal->exclusiveMode ) return; |
| 60021 | 60192 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| | @@ -60283,15 +60454,10 @@ |
| 60283 | 60454 | int rc; /* Return Code */ |
| 60284 | 60455 | i64 nSize; /* Size of log file */ |
| 60285 | 60456 | u32 aFrameCksum[2] = {0, 0}; |
| 60286 | 60457 | int iLock; /* Lock offset to lock for checkpoint */ |
| 60287 | 60458 | |
| 60288 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 60289 | | - int tmout = 0; |
| 60290 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 60291 | | -#endif |
| 60292 | | - |
| 60293 | 60459 | /* Obtain an exclusive lock on all byte in the locking range not already |
| 60294 | 60460 | ** locked by the caller. The caller is guaranteed to have locked the |
| 60295 | 60461 | ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte. |
| 60296 | 60462 | ** If successful, the same bytes that are locked here are unlocked before |
| 60297 | 60463 | ** this function returns. |
| | @@ -60835,10 +61001,93 @@ |
| 60835 | 61001 | p = 0; |
| 60836 | 61002 | } |
| 60837 | 61003 | *pp = p; |
| 60838 | 61004 | return rc; |
| 60839 | 61005 | } |
| 61006 | + |
| 61007 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61008 | +/* |
| 61009 | +** Attempt to enable blocking locks. Blocking locks are enabled only if (a) |
| 61010 | +** they are supported by the VFS, and (b) the database handle is configured |
| 61011 | +** with a busy-timeout. Return 1 if blocking locks are successfully enabled, |
| 61012 | +** or 0 otherwise. |
| 61013 | +*/ |
| 61014 | +static int walEnableBlocking(Wal *pWal){ |
| 61015 | + int res = 0; |
| 61016 | + if( pWal->db ){ |
| 61017 | + int tmout = pWal->db->busyTimeout; |
| 61018 | + if( tmout ){ |
| 61019 | + int rc; |
| 61020 | + rc = sqlite3OsFileControl( |
| 61021 | + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout |
| 61022 | + ); |
| 61023 | + res = (rc==SQLITE_OK); |
| 61024 | + } |
| 61025 | + } |
| 61026 | + return res; |
| 61027 | +} |
| 61028 | + |
| 61029 | +/* |
| 61030 | +** Disable blocking locks. |
| 61031 | +*/ |
| 61032 | +static void walDisableBlocking(Wal *pWal){ |
| 61033 | + int tmout = 0; |
| 61034 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 61035 | +} |
| 61036 | + |
| 61037 | +/* |
| 61038 | +** If parameter bLock is true, attempt to enable blocking locks, take |
| 61039 | +** the WRITER lock, and then disable blocking locks. If blocking locks |
| 61040 | +** cannot be enabled, no attempt to obtain the WRITER lock is made. Return |
| 61041 | +** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not |
| 61042 | +** an error if blocking locks can not be enabled. |
| 61043 | +** |
| 61044 | +** If the bLock parameter is false and the WRITER lock is held, release it. |
| 61045 | +*/ |
| 61046 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){ |
| 61047 | + int rc = SQLITE_OK; |
| 61048 | + assert( pWal->readLock<0 || bLock==0 ); |
| 61049 | + if( bLock ){ |
| 61050 | + assert( pWal->db ); |
| 61051 | + if( walEnableBlocking(pWal) ){ |
| 61052 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61053 | + if( rc==SQLITE_OK ){ |
| 61054 | + pWal->writeLock = 1; |
| 61055 | + } |
| 61056 | + walDisableBlocking(pWal); |
| 61057 | + } |
| 61058 | + }else if( pWal->writeLock ){ |
| 61059 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61060 | + pWal->writeLock = 0; |
| 61061 | + } |
| 61062 | + return rc; |
| 61063 | +} |
| 61064 | + |
| 61065 | +/* |
| 61066 | +** Set the database handle used to determine if blocking locks are required. |
| 61067 | +*/ |
| 61068 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){ |
| 61069 | + pWal->db = db; |
| 61070 | +} |
| 61071 | + |
| 61072 | +/* |
| 61073 | +** Take an exclusive WRITE lock. Blocking if so configured. |
| 61074 | +*/ |
| 61075 | +static int walLockWriter(Wal *pWal){ |
| 61076 | + int rc; |
| 61077 | + walEnableBlocking(pWal); |
| 61078 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61079 | + walDisableBlocking(pWal); |
| 61080 | + return rc; |
| 61081 | +} |
| 61082 | +#else |
| 61083 | +# define walEnableBlocking(x) 0 |
| 61084 | +# define walDisableBlocking(x) |
| 61085 | +# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1) |
| 61086 | +# define sqlite3WalDb(pWal, db) |
| 61087 | +#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 61088 | + |
| 60840 | 61089 | |
| 60841 | 61090 | /* |
| 60842 | 61091 | ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and |
| 60843 | 61092 | ** n. If the attempt fails and parameter xBusy is not NULL, then it is a |
| 60844 | 61093 | ** busy-handler function. Invoke it and retry the lock until either the |
| | @@ -60853,10 +61102,16 @@ |
| 60853 | 61102 | ){ |
| 60854 | 61103 | int rc; |
| 60855 | 61104 | do { |
| 60856 | 61105 | rc = walLockExclusive(pWal, lockIdx, n); |
| 60857 | 61106 | }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); |
| 61107 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61108 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 61109 | + walDisableBlocking(pWal); |
| 61110 | + rc = SQLITE_BUSY; |
| 61111 | + } |
| 61112 | +#endif |
| 60858 | 61113 | return rc; |
| 60859 | 61114 | } |
| 60860 | 61115 | |
| 60861 | 61116 | /* |
| 60862 | 61117 | ** The cache of the wal-index header must be valid to call this function. |
| | @@ -60890,11 +61145,11 @@ |
| 60890 | 61145 | pWal->nCkpt++; |
| 60891 | 61146 | pWal->hdr.mxFrame = 0; |
| 60892 | 61147 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 60893 | 61148 | memcpy(&pWal->hdr.aSalt[1], &salt1, 4); |
| 60894 | 61149 | walIndexWriteHdr(pWal); |
| 60895 | | - pInfo->nBackfill = 0; |
| 61150 | + AtomicStore(&pInfo->nBackfill, 0); |
| 60896 | 61151 | pInfo->nBackfillAttempted = 0; |
| 60897 | 61152 | pInfo->aReadMark[1] = 0; |
| 60898 | 61153 | for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 60899 | 61154 | assert( pInfo->aReadMark[0]==0 ); |
| 60900 | 61155 | } |
| | @@ -60965,36 +61220,17 @@ |
| 60965 | 61220 | ** cannot be backfilled from the WAL. |
| 60966 | 61221 | */ |
| 60967 | 61222 | mxSafeFrame = pWal->hdr.mxFrame; |
| 60968 | 61223 | mxPage = pWal->hdr.nPage; |
| 60969 | 61224 | for(i=1; i<WAL_NREADER; i++){ |
| 60970 | | - /* Thread-sanitizer reports that the following is an unsafe read, |
| 60971 | | - ** as some other thread may be in the process of updating the value |
| 60972 | | - ** of the aReadMark[] slot. The assumption here is that if that is |
| 60973 | | - ** happening, the other client may only be increasing the value, |
| 60974 | | - ** not decreasing it. So assuming either that either the "old" or |
| 60975 | | - ** "new" version of the value is read, and not some arbitrary value |
| 60976 | | - ** that would never be written by a real client, things are still |
| 60977 | | - ** safe. |
| 60978 | | - ** |
| 60979 | | - ** Astute readers have pointed out that the assumption stated in the |
| 60980 | | - ** last sentence of the previous paragraph is not guaranteed to be |
| 60981 | | - ** true for all conforming systems. However, the assumption is true |
| 60982 | | - ** for all compilers and architectures in common use today (circa |
| 60983 | | - ** 2019-11-27) and the alternatives are both slow and complex, and |
| 60984 | | - ** so we will continue to go with the current design for now. If this |
| 60985 | | - ** bothers you, or if you really are running on a system where aligned |
| 60986 | | - ** 32-bit reads and writes are not atomic, then you can simply avoid |
| 60987 | | - ** the use of WAL mode, or only use WAL mode together with |
| 60988 | | - ** PRAGMA locking_mode=EXCLUSIVE and all will be well. |
| 60989 | | - */ |
| 60990 | | - u32 y = pInfo->aReadMark[i]; |
| 61225 | + u32 y = AtomicLoad(pInfo->aReadMark+i); |
| 60991 | 61226 | if( mxSafeFrame>y ){ |
| 60992 | 61227 | assert( y<=pWal->hdr.mxFrame ); |
| 60993 | 61228 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); |
| 60994 | 61229 | if( rc==SQLITE_OK ){ |
| 60995 | | - pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61230 | + u32 iMark = (i==1 ? mxSafeFrame : READMARK_NOT_USED); |
| 61231 | + AtomicStore(pInfo->aReadMark+i, iMark); |
| 60996 | 61232 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 60997 | 61233 | }else if( rc==SQLITE_BUSY ){ |
| 60998 | 61234 | mxSafeFrame = y; |
| 60999 | 61235 | xBusy = 0; |
| 61000 | 61236 | }else{ |
| | @@ -61008,11 +61244,11 @@ |
| 61008 | 61244 | rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter); |
| 61009 | 61245 | assert( rc==SQLITE_OK || pIter==0 ); |
| 61010 | 61246 | } |
| 61011 | 61247 | |
| 61012 | 61248 | if( pIter |
| 61013 | | - && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61249 | + && (rc = walBusyLock(pWal,xBusy,pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK |
| 61014 | 61250 | ){ |
| 61015 | 61251 | u32 nBackfill = pInfo->nBackfill; |
| 61016 | 61252 | |
| 61017 | 61253 | pInfo->nBackfillAttempted = mxSafeFrame; |
| 61018 | 61254 | |
| | @@ -61023,10 +61259,11 @@ |
| 61023 | 61259 | ** about the eventual size of the db file to the VFS layer. |
| 61024 | 61260 | */ |
| 61025 | 61261 | if( rc==SQLITE_OK ){ |
| 61026 | 61262 | i64 nReq = ((i64)mxPage * szPage); |
| 61027 | 61263 | i64 nSize; /* Current size of database file */ |
| 61264 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0); |
| 61028 | 61265 | rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); |
| 61029 | 61266 | if( rc==SQLITE_OK && nSize<nReq ){ |
| 61030 | 61267 | sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); |
| 61031 | 61268 | } |
| 61032 | 61269 | } |
| | @@ -61050,10 +61287,11 @@ |
| 61050 | 61287 | iOffset = (iDbpage-1)*(i64)szPage; |
| 61051 | 61288 | testcase( IS_BIG_INT(iOffset) ); |
| 61052 | 61289 | rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset); |
| 61053 | 61290 | if( rc!=SQLITE_OK ) break; |
| 61054 | 61291 | } |
| 61292 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0); |
| 61055 | 61293 | |
| 61056 | 61294 | /* If work was actually accomplished... */ |
| 61057 | 61295 | if( rc==SQLITE_OK ){ |
| 61058 | 61296 | if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ |
| 61059 | 61297 | i64 szDb = pWal->hdr.nPage*(i64)szPage; |
| | @@ -61062,15 +61300,11 @@ |
| 61062 | 61300 | if( rc==SQLITE_OK ){ |
| 61063 | 61301 | rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); |
| 61064 | 61302 | } |
| 61065 | 61303 | } |
| 61066 | 61304 | if( rc==SQLITE_OK ){ |
| 61067 | | - rc = sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0); |
| 61068 | | - if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 61069 | | - } |
| 61070 | | - if( rc==SQLITE_OK ){ |
| 61071 | | - pInfo->nBackfill = mxSafeFrame; |
| 61305 | + AtomicStore(&pInfo->nBackfill, mxSafeFrame); |
| 61072 | 61306 | } |
| 61073 | 61307 | } |
| 61074 | 61308 | |
| 61075 | 61309 | /* Release the reader lock held while backfilling */ |
| 61076 | 61310 | walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); |
| | @@ -61225,11 +61459,11 @@ |
| 61225 | 61459 | ** and *pChanged is set to 1. |
| 61226 | 61460 | ** |
| 61227 | 61461 | ** If the checksum cannot be verified return non-zero. If the header |
| 61228 | 61462 | ** is read successfully and the checksum verified, return zero. |
| 61229 | 61463 | */ |
| 61230 | | -static int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61464 | +static SQLITE_NO_TSAN int walIndexTryHdr(Wal *pWal, int *pChanged){ |
| 61231 | 61465 | u32 aCksum[2]; /* Checksum on the header content */ |
| 61232 | 61466 | WalIndexHdr h1, h2; /* Two copies of the header content */ |
| 61233 | 61467 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
| 61234 | 61468 | |
| 61235 | 61469 | /* The first page of the wal-index must be mapped at this point. */ |
| | @@ -61238,17 +61472,23 @@ |
| 61238 | 61472 | /* Read the header. This might happen concurrently with a write to the |
| 61239 | 61473 | ** same area of shared memory on a different CPU in a SMP, |
| 61240 | 61474 | ** meaning it is possible that an inconsistent snapshot is read |
| 61241 | 61475 | ** from the file. If this happens, return non-zero. |
| 61242 | 61476 | ** |
| 61477 | + ** tag-20200519-1: |
| 61243 | 61478 | ** There are two copies of the header at the beginning of the wal-index. |
| 61244 | 61479 | ** When reading, read [0] first then [1]. Writes are in the reverse order. |
| 61245 | 61480 | ** Memory barriers are used to prevent the compiler or the hardware from |
| 61246 | | - ** reordering the reads and writes. |
| 61481 | + ** reordering the reads and writes. TSAN and similar tools can sometimes |
| 61482 | + ** give false-positive warnings about these accesses because the tools do not |
| 61483 | + ** account for the double-read and the memory barrier. The use of mutexes |
| 61484 | + ** here would be problematic as the memory being accessed is potentially |
| 61485 | + ** shared among multiple processes and not all mutex implementions work |
| 61486 | + ** reliably in that environment. |
| 61247 | 61487 | */ |
| 61248 | 61488 | aHdr = walIndexHdr(pWal); |
| 61249 | | - memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); |
| 61489 | + memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */ |
| 61250 | 61490 | walShmBarrier(pWal); |
| 61251 | 61491 | memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); |
| 61252 | 61492 | |
| 61253 | 61493 | if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ |
| 61254 | 61494 | return 1; /* Dirty read */ |
| | @@ -61334,32 +61574,36 @@ |
| 61334 | 61574 | badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); |
| 61335 | 61575 | |
| 61336 | 61576 | /* If the first attempt failed, it might have been due to a race |
| 61337 | 61577 | ** with a writer. So get a WRITE lock and try again. |
| 61338 | 61578 | */ |
| 61339 | | - assert( badHdr==0 || pWal->writeLock==0 ); |
| 61340 | 61579 | if( badHdr ){ |
| 61341 | 61580 | if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ |
| 61342 | 61581 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 61343 | 61582 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 61344 | 61583 | rc = SQLITE_READONLY_RECOVERY; |
| 61345 | 61584 | } |
| 61346 | | - }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ |
| 61347 | | - pWal->writeLock = 1; |
| 61348 | | - if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 61349 | | - badHdr = walIndexTryHdr(pWal, pChanged); |
| 61350 | | - if( badHdr ){ |
| 61351 | | - /* If the wal-index header is still malformed even while holding |
| 61352 | | - ** a WRITE lock, it can only mean that the header is corrupted and |
| 61353 | | - ** needs to be reconstructed. So run recovery to do exactly that. |
| 61354 | | - */ |
| 61355 | | - rc = walIndexRecover(pWal); |
| 61356 | | - *pChanged = 1; |
| 61357 | | - } |
| 61358 | | - } |
| 61359 | | - pWal->writeLock = 0; |
| 61360 | | - walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61585 | + }else{ |
| 61586 | + int bWriteLock = pWal->writeLock; |
| 61587 | + if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){ |
| 61588 | + pWal->writeLock = 1; |
| 61589 | + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 61590 | + badHdr = walIndexTryHdr(pWal, pChanged); |
| 61591 | + if( badHdr ){ |
| 61592 | + /* If the wal-index header is still malformed even while holding |
| 61593 | + ** a WRITE lock, it can only mean that the header is corrupted and |
| 61594 | + ** needs to be reconstructed. So run recovery to do exactly that. |
| 61595 | + */ |
| 61596 | + rc = walIndexRecover(pWal); |
| 61597 | + *pChanged = 1; |
| 61598 | + } |
| 61599 | + } |
| 61600 | + if( bWriteLock==0 ){ |
| 61601 | + pWal->writeLock = 0; |
| 61602 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61603 | + } |
| 61604 | + } |
| 61361 | 61605 | } |
| 61362 | 61606 | } |
| 61363 | 61607 | |
| 61364 | 61608 | /* If the header is read successfully, check the version number to make |
| 61365 | 61609 | ** sure the wal-index was not constructed with some future format that |
| | @@ -61685,11 +61929,11 @@ |
| 61685 | 61929 | } |
| 61686 | 61930 | |
| 61687 | 61931 | assert( pWal->nWiData>0 ); |
| 61688 | 61932 | assert( pWal->apWiData[0]!=0 ); |
| 61689 | 61933 | pInfo = walCkptInfo(pWal); |
| 61690 | | - if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame |
| 61934 | + if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame |
| 61691 | 61935 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61692 | 61936 | && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0) |
| 61693 | 61937 | #endif |
| 61694 | 61938 | ){ |
| 61695 | 61939 | /* The WAL has been completely backfilled (or it is empty). |
| | @@ -61852,11 +62096,11 @@ |
| 61852 | 62096 | void *pBuf2 = sqlite3_malloc(szPage); |
| 61853 | 62097 | if( pBuf1==0 || pBuf2==0 ){ |
| 61854 | 62098 | rc = SQLITE_NOMEM; |
| 61855 | 62099 | }else{ |
| 61856 | 62100 | u32 i = pInfo->nBackfillAttempted; |
| 61857 | | - for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){ |
| 62101 | + for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){ |
| 61858 | 62102 | WalHashLoc sLoc; /* Hash table location */ |
| 61859 | 62103 | u32 pgno; /* Page number in db file */ |
| 61860 | 62104 | i64 iDbOff; /* Offset of db file entry */ |
| 61861 | 62105 | i64 iWalOff; /* Offset of wal file entry */ |
| 61862 | 62106 | |
| | @@ -61907,26 +62151,40 @@ |
| 61907 | 62151 | ** needs to be flushed. |
| 61908 | 62152 | */ |
| 61909 | 62153 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 61910 | 62154 | int rc; /* Return code */ |
| 61911 | 62155 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 61912 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61913 | | - int tmout = 0; |
| 61914 | | -#endif |
| 61915 | | - |
| 61916 | 62156 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61917 | 62157 | int bChanged = 0; |
| 61918 | 62158 | WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 61919 | | - if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 61920 | | - bChanged = 1; |
| 61921 | | - } |
| 61922 | 62159 | #endif |
| 61923 | 62160 | |
| 61924 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61925 | | - /* Disable blocking locks. They are not useful when trying to open a |
| 61926 | | - ** read-transaction, and blocking may cause deadlock anyway. */ |
| 61927 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 62161 | + assert( pWal->ckptLock==0 ); |
| 62162 | + |
| 62163 | +#ifdef SQLITE_ENABLE_SNAPSHOT |
| 62164 | + if( pSnapshot ){ |
| 62165 | + if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 62166 | + bChanged = 1; |
| 62167 | + } |
| 62168 | + |
| 62169 | + /* It is possible that there is a checkpointer thread running |
| 62170 | + ** concurrent with this code. If this is the case, it may be that the |
| 62171 | + ** checkpointer has already determined that it will checkpoint |
| 62172 | + ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 62173 | + ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 62174 | + ** its intent. To avoid the race condition this leads to, ensure that |
| 62175 | + ** there is no checkpointer process by taking a shared CKPT lock |
| 62176 | + ** before checking pInfo->nBackfillAttempted. */ |
| 62177 | + (void)walEnableBlocking(pWal); |
| 62178 | + rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 62179 | + walDisableBlocking(pWal); |
| 62180 | + |
| 62181 | + if( rc!=SQLITE_OK ){ |
| 62182 | + return rc; |
| 62183 | + } |
| 62184 | + pWal->ckptLock = 1; |
| 62185 | + } |
| 61928 | 62186 | #endif |
| 61929 | 62187 | |
| 61930 | 62188 | do{ |
| 61931 | 62189 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 61932 | 62190 | }while( rc==WAL_RETRY ); |
| | @@ -61933,20 +62191,10 @@ |
| 61933 | 62191 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 61934 | 62192 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 61935 | 62193 | testcase( rc==SQLITE_PROTOCOL ); |
| 61936 | 62194 | testcase( rc==SQLITE_OK ); |
| 61937 | 62195 | |
| 61938 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61939 | | - /* If they were disabled earlier and the read-transaction has been |
| 61940 | | - ** successfully opened, re-enable blocking locks. This is because the |
| 61941 | | - ** connection may attempt to upgrade to a write-transaction, which does |
| 61942 | | - ** benefit from using blocking locks. */ |
| 61943 | | - if( rc==SQLITE_OK ){ |
| 61944 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 61945 | | - } |
| 61946 | | -#endif |
| 61947 | | - |
| 61948 | 62196 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61949 | 62197 | if( rc==SQLITE_OK ){ |
| 61950 | 62198 | if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 61951 | 62199 | /* At this point the client has a lock on an aReadMark[] slot holding |
| 61952 | 62200 | ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr |
| | @@ -61964,52 +62212,46 @@ |
| 61964 | 62212 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 61965 | 62213 | |
| 61966 | 62214 | assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); |
| 61967 | 62215 | assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); |
| 61968 | 62216 | |
| 61969 | | - /* It is possible that there is a checkpointer thread running |
| 61970 | | - ** concurrent with this code. If this is the case, it may be that the |
| 61971 | | - ** checkpointer has already determined that it will checkpoint |
| 61972 | | - ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 61973 | | - ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 61974 | | - ** its intent. To avoid the race condition this leads to, ensure that |
| 61975 | | - ** there is no checkpointer process by taking a shared CKPT lock |
| 61976 | | - ** before checking pInfo->nBackfillAttempted. |
| 61977 | | - ** |
| 61978 | | - ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing |
| 61979 | | - ** this already? |
| 61980 | | - */ |
| 61981 | | - rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 61982 | | - |
| 61983 | | - if( rc==SQLITE_OK ){ |
| 61984 | | - /* Check that the wal file has not been wrapped. Assuming that it has |
| 61985 | | - ** not, also check that no checkpointer has attempted to checkpoint any |
| 61986 | | - ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 61987 | | - ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 61988 | | - ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 61989 | | - ** snapshot. */ |
| 61990 | | - if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 61991 | | - && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 61992 | | - ){ |
| 61993 | | - assert( pWal->readLock>0 ); |
| 61994 | | - memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 61995 | | - *pChanged = bChanged; |
| 61996 | | - }else{ |
| 61997 | | - rc = SQLITE_ERROR_SNAPSHOT; |
| 61998 | | - } |
| 61999 | | - |
| 62000 | | - /* Release the shared CKPT lock obtained above. */ |
| 62001 | | - walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 62002 | | - pWal->minFrame = 1; |
| 62003 | | - } |
| 62004 | | - |
| 62217 | + /* Check that the wal file has not been wrapped. Assuming that it has |
| 62218 | + ** not, also check that no checkpointer has attempted to checkpoint any |
| 62219 | + ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 62220 | + ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 62221 | + ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 62222 | + ** snapshot. */ |
| 62223 | + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 62224 | + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 62225 | + ){ |
| 62226 | + assert( pWal->readLock>0 ); |
| 62227 | + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 62228 | + *pChanged = bChanged; |
| 62229 | + }else{ |
| 62230 | + rc = SQLITE_ERROR_SNAPSHOT; |
| 62231 | + } |
| 62232 | + |
| 62233 | + /* A client using a non-current snapshot may not ignore any frames |
| 62234 | + ** from the start of the wal file. This is because, for a system |
| 62235 | + ** where (minFrame < iSnapshot < maxFrame), a checkpointer may |
| 62236 | + ** have omitted to checkpoint a frame earlier than minFrame in |
| 62237 | + ** the file because there exists a frame after iSnapshot that |
| 62238 | + ** is the same database page. */ |
| 62239 | + pWal->minFrame = 1; |
| 62005 | 62240 | |
| 62006 | 62241 | if( rc!=SQLITE_OK ){ |
| 62007 | 62242 | sqlite3WalEndReadTransaction(pWal); |
| 62008 | 62243 | } |
| 62009 | 62244 | } |
| 62010 | 62245 | } |
| 62246 | + |
| 62247 | + /* Release the shared CKPT lock obtained above. */ |
| 62248 | + if( pWal->ckptLock ){ |
| 62249 | + assert( pSnapshot ); |
| 62250 | + walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 62251 | + pWal->ckptLock = 0; |
| 62252 | + } |
| 62011 | 62253 | #endif |
| 62012 | 62254 | return rc; |
| 62013 | 62255 | } |
| 62014 | 62256 | |
| 62015 | 62257 | /* |
| | @@ -62085,26 +62327,28 @@ |
| 62085 | 62327 | for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){ |
| 62086 | 62328 | WalHashLoc sLoc; /* Hash table location */ |
| 62087 | 62329 | int iKey; /* Hash slot index */ |
| 62088 | 62330 | int nCollide; /* Number of hash collisions remaining */ |
| 62089 | 62331 | int rc; /* Error code */ |
| 62332 | + u32 iH; |
| 62090 | 62333 | |
| 62091 | 62334 | rc = walHashGet(pWal, iHash, &sLoc); |
| 62092 | 62335 | if( rc!=SQLITE_OK ){ |
| 62093 | 62336 | return rc; |
| 62094 | 62337 | } |
| 62095 | 62338 | nCollide = HASHTABLE_NSLOT; |
| 62096 | | - for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 62097 | | - u32 iH = sLoc.aHash[iKey]; |
| 62339 | + iKey = walHash(pgno); |
| 62340 | + while( (iH = AtomicLoad(&sLoc.aHash[iKey]))!=0 ){ |
| 62098 | 62341 | u32 iFrame = iH + sLoc.iZero; |
| 62099 | 62342 | if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 62100 | 62343 | assert( iFrame>iRead || CORRUPT_DB ); |
| 62101 | 62344 | iRead = iFrame; |
| 62102 | 62345 | } |
| 62103 | 62346 | if( (nCollide--)==0 ){ |
| 62104 | 62347 | return SQLITE_CORRUPT_BKPT; |
| 62105 | 62348 | } |
| 62349 | + iKey = walNextHash(iKey); |
| 62106 | 62350 | } |
| 62107 | 62351 | if( iRead ) break; |
| 62108 | 62352 | } |
| 62109 | 62353 | |
| 62110 | 62354 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| | @@ -62175,10 +62419,20 @@ |
| 62175 | 62419 | ** |
| 62176 | 62420 | ** There can only be a single writer active at a time. |
| 62177 | 62421 | */ |
| 62178 | 62422 | SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ |
| 62179 | 62423 | int rc; |
| 62424 | + |
| 62425 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 62426 | + /* If the write-lock is already held, then it was obtained before the |
| 62427 | + ** read-transaction was even opened, making this call a no-op. |
| 62428 | + ** Return early. */ |
| 62429 | + if( pWal->writeLock ){ |
| 62430 | + assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) ); |
| 62431 | + return SQLITE_OK; |
| 62432 | + } |
| 62433 | +#endif |
| 62180 | 62434 | |
| 62181 | 62435 | /* Cannot start a write transaction without first holding a read |
| 62182 | 62436 | ** transaction. */ |
| 62183 | 62437 | assert( pWal->readLock>=0 ); |
| 62184 | 62438 | assert( pWal->writeLock==0 && pWal->iReCksum==0 ); |
| | @@ -62751,50 +63005,57 @@ |
| 62751 | 63005 | ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ |
| 62752 | 63006 | assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 62753 | 63007 | |
| 62754 | 63008 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 62755 | 63009 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 63010 | + |
| 63011 | + /* Enable blocking locks, if possible. If blocking locks are successfully |
| 63012 | + ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */ |
| 63013 | + sqlite3WalDb(pWal, db); |
| 63014 | + (void)walEnableBlocking(pWal); |
| 62756 | 63015 | |
| 62757 | 63016 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 62758 | | - ** "checkpoint" lock on the database file. */ |
| 63017 | + ** "checkpoint" lock on the database file. |
| 63018 | + ** EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 63019 | + ** checkpoint operation at the same time, the lock cannot be obtained and |
| 63020 | + ** SQLITE_BUSY is returned. |
| 63021 | + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 63022 | + ** it will not be invoked in this case. |
| 63023 | + */ |
| 62759 | 63024 | rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 62760 | | - if( rc ){ |
| 62761 | | - /* EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 62762 | | - ** checkpoint operation at the same time, the lock cannot be obtained and |
| 62763 | | - ** SQLITE_BUSY is returned. |
| 62764 | | - ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 62765 | | - ** it will not be invoked in this case. |
| 62766 | | - */ |
| 62767 | | - testcase( rc==SQLITE_BUSY ); |
| 62768 | | - testcase( xBusy!=0 ); |
| 62769 | | - return rc; |
| 62770 | | - } |
| 62771 | | - pWal->ckptLock = 1; |
| 62772 | | - |
| 62773 | | - /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 62774 | | - ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 62775 | | - ** file. |
| 62776 | | - ** |
| 62777 | | - ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 62778 | | - ** immediately, and a busy-handler is configured, it is invoked and the |
| 62779 | | - ** writer lock retried until either the busy-handler returns 0 or the |
| 62780 | | - ** lock is successfully obtained. |
| 62781 | | - */ |
| 62782 | | - if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 62783 | | - rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1); |
| 62784 | | - if( rc==SQLITE_OK ){ |
| 62785 | | - pWal->writeLock = 1; |
| 62786 | | - }else if( rc==SQLITE_BUSY ){ |
| 62787 | | - eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 62788 | | - xBusy2 = 0; |
| 62789 | | - rc = SQLITE_OK; |
| 62790 | | - } |
| 62791 | | - } |
| 63025 | + testcase( rc==SQLITE_BUSY ); |
| 63026 | + testcase( rc!=SQLITE_OK && xBusy2!=0 ); |
| 63027 | + if( rc==SQLITE_OK ){ |
| 63028 | + pWal->ckptLock = 1; |
| 63029 | + |
| 63030 | + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 63031 | + ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 63032 | + ** file. |
| 63033 | + ** |
| 63034 | + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 63035 | + ** immediately, and a busy-handler is configured, it is invoked and the |
| 63036 | + ** writer lock retried until either the busy-handler returns 0 or the |
| 63037 | + ** lock is successfully obtained. |
| 63038 | + */ |
| 63039 | + if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 63040 | + rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1); |
| 63041 | + if( rc==SQLITE_OK ){ |
| 63042 | + pWal->writeLock = 1; |
| 63043 | + }else if( rc==SQLITE_BUSY ){ |
| 63044 | + eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 63045 | + xBusy2 = 0; |
| 63046 | + rc = SQLITE_OK; |
| 63047 | + } |
| 63048 | + } |
| 63049 | + } |
| 63050 | + |
| 62792 | 63051 | |
| 62793 | 63052 | /* Read the wal-index header. */ |
| 62794 | 63053 | if( rc==SQLITE_OK ){ |
| 63054 | + walDisableBlocking(pWal); |
| 62795 | 63055 | rc = walIndexReadHdr(pWal, &isChanged); |
| 63056 | + (void)walEnableBlocking(pWal); |
| 62796 | 63057 | if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ |
| 62797 | 63058 | sqlite3OsUnfetch(pWal->pDbFd, 0, 0); |
| 62798 | 63059 | } |
| 62799 | 63060 | } |
| 62800 | 63061 | |
| | @@ -62821,16 +63082,24 @@ |
| 62821 | 63082 | ** next time the pager opens a snapshot on this database it knows that |
| 62822 | 63083 | ** the cache needs to be reset. |
| 62823 | 63084 | */ |
| 62824 | 63085 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
| 62825 | 63086 | } |
| 63087 | + |
| 63088 | + walDisableBlocking(pWal); |
| 63089 | + sqlite3WalDb(pWal, 0); |
| 62826 | 63090 | |
| 62827 | 63091 | /* Release the locks. */ |
| 62828 | 63092 | sqlite3WalEndWriteTransaction(pWal); |
| 62829 | | - walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 62830 | | - pWal->ckptLock = 0; |
| 63093 | + if( pWal->ckptLock ){ |
| 63094 | + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 63095 | + pWal->ckptLock = 0; |
| 63096 | + } |
| 62831 | 63097 | WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); |
| 63098 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 63099 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 63100 | +#endif |
| 62832 | 63101 | return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); |
| 62833 | 63102 | } |
| 62834 | 63103 | |
| 62835 | 63104 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 62836 | 63105 | ** number of frames in the WAL at the point of the last commit since |
| | @@ -62943,11 +63212,14 @@ |
| 62943 | 63212 | return rc; |
| 62944 | 63213 | } |
| 62945 | 63214 | |
| 62946 | 63215 | /* Try to open on pSnapshot when the next read-transaction starts |
| 62947 | 63216 | */ |
| 62948 | | -SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ |
| 63217 | +SQLITE_PRIVATE void sqlite3WalSnapshotOpen( |
| 63218 | + Wal *pWal, |
| 63219 | + sqlite3_snapshot *pSnapshot |
| 63220 | +){ |
| 62949 | 63221 | pWal->pSnapshot = (WalIndexHdr*)pSnapshot; |
| 62950 | 63222 | } |
| 62951 | 63223 | |
| 62952 | 63224 | /* |
| 62953 | 63225 | ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if |
| | @@ -63462,11 +63734,11 @@ |
| 63462 | 63734 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 63463 | 63735 | u8 bDoTruncate; /* True to truncate db on commit */ |
| 63464 | 63736 | #endif |
| 63465 | 63737 | u8 inTransaction; /* Transaction state */ |
| 63466 | 63738 | u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ |
| 63467 | | - u8 nReserveWanted; /* 1 more than desired number of extra bytes per page */ |
| 63739 | + u8 nReserveWanted; /* Desired number of extra bytes per page */ |
| 63468 | 63740 | u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
| 63469 | 63741 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 63470 | 63742 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 63471 | 63743 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 63472 | 63744 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| | @@ -66356,12 +66628,11 @@ |
| 66356 | 66628 | */ |
| 66357 | 66629 | static int btreeInvokeBusyHandler(void *pArg){ |
| 66358 | 66630 | BtShared *pBt = (BtShared*)pArg; |
| 66359 | 66631 | assert( pBt->db ); |
| 66360 | 66632 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 66361 | | - return sqlite3InvokeBusyHandler(&pBt->db->busyHandler, |
| 66362 | | - sqlite3PagerFile(pBt->pPager)); |
| 66633 | + return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 66363 | 66634 | } |
| 66364 | 66635 | |
| 66365 | 66636 | /* |
| 66366 | 66637 | ** Open a database file. |
| 66367 | 66638 | ** |
| | @@ -66908,23 +67179,21 @@ |
| 66908 | 67179 | ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size |
| 66909 | 67180 | ** and autovacuum mode can no longer be changed. |
| 66910 | 67181 | */ |
| 66911 | 67182 | SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
| 66912 | 67183 | int rc = SQLITE_OK; |
| 67184 | + int x; |
| 66913 | 67185 | BtShared *pBt = p->pBt; |
| 66914 | | - assert( nReserve>=-1 && nReserve<=254 ); |
| 67186 | + assert( nReserve>=0 && nReserve<=255 ); |
| 66915 | 67187 | sqlite3BtreeEnter(p); |
| 66916 | | - if( nReserve>=0 ){ |
| 66917 | | - pBt->nReserveWanted = nReserve + 1; |
| 66918 | | - } |
| 67188 | + pBt->nReserveWanted = nReserve; |
| 67189 | + x = pBt->pageSize - pBt->usableSize; |
| 67190 | + if( nReserve<x ) nReserve = x; |
| 66919 | 67191 | if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
| 66920 | 67192 | sqlite3BtreeLeave(p); |
| 66921 | 67193 | return SQLITE_READONLY; |
| 66922 | 67194 | } |
| 66923 | | - if( nReserve<0 ){ |
| 66924 | | - nReserve = pBt->pageSize - pBt->usableSize; |
| 66925 | | - } |
| 66926 | 67195 | assert( nReserve>=0 && nReserve<=255 ); |
| 66927 | 67196 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 66928 | 67197 | ((pageSize-1)&pageSize)==0 ){ |
| 66929 | 67198 | assert( (pageSize & 7)==0 ); |
| 66930 | 67199 | assert( !pBt->pCursor ); |
| | @@ -66965,18 +67234,22 @@ |
| 66965 | 67234 | |
| 66966 | 67235 | /* |
| 66967 | 67236 | ** Return the number of bytes of space at the end of every page that |
| 66968 | 67237 | ** are intentually left unused. This is the "reserved" space that is |
| 66969 | 67238 | ** sometimes used by extensions. |
| 67239 | +** |
| 67240 | +** The value returned is the larger of the current reserve size and |
| 67241 | +** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. |
| 67242 | +** The amount of reserve can only grow - never shrink. |
| 66970 | 67243 | */ |
| 66971 | 67244 | SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){ |
| 66972 | | - int n; |
| 67245 | + int n1, n2; |
| 66973 | 67246 | sqlite3BtreeEnter(p); |
| 66974 | | - n = ((int)p->pBt->nReserveWanted) - 1; |
| 66975 | | - if( n<0 ) n = sqlite3BtreeGetReserveNoMutex(p); |
| 67247 | + n1 = (int)p->pBt->nReserveWanted; |
| 67248 | + n2 = sqlite3BtreeGetReserveNoMutex(p); |
| 66976 | 67249 | sqlite3BtreeLeave(p); |
| 66977 | | - return n; |
| 67250 | + return n1>n2 ? n1 : n2; |
| 66978 | 67251 | } |
| 66979 | 67252 | |
| 66980 | 67253 | |
| 66981 | 67254 | /* |
| 66982 | 67255 | ** Set the maximum page count for a database if mxPage is positive. |
| | @@ -67422,10 +67695,11 @@ |
| 67422 | 67695 | ** when A already has a read lock, we encourage A to give up and let B |
| 67423 | 67696 | ** proceed. |
| 67424 | 67697 | */ |
| 67425 | 67698 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ |
| 67426 | 67699 | BtShared *pBt = p->pBt; |
| 67700 | + Pager *pPager = pBt->pPager; |
| 67427 | 67701 | int rc = SQLITE_OK; |
| 67428 | 67702 | |
| 67429 | 67703 | sqlite3BtreeEnter(p); |
| 67430 | 67704 | btreeIntegrity(p); |
| 67431 | 67705 | |
| | @@ -67437,11 +67711,11 @@ |
| 67437 | 67711 | goto trans_begun; |
| 67438 | 67712 | } |
| 67439 | 67713 | assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); |
| 67440 | 67714 | |
| 67441 | 67715 | if( (p->db->flags & SQLITE_ResetDatabase) |
| 67442 | | - && sqlite3PagerIsreadonly(pBt->pPager)==0 |
| 67716 | + && sqlite3PagerIsreadonly(pPager)==0 |
| 67443 | 67717 | ){ |
| 67444 | 67718 | pBt->btsFlags &= ~BTS_READ_ONLY; |
| 67445 | 67719 | } |
| 67446 | 67720 | |
| 67447 | 67721 | /* Write transactions are not possible on a read-only database */ |
| | @@ -67485,10 +67759,22 @@ |
| 67485 | 67759 | if( SQLITE_OK!=rc ) goto trans_begun; |
| 67486 | 67760 | |
| 67487 | 67761 | pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; |
| 67488 | 67762 | if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; |
| 67489 | 67763 | do { |
| 67764 | + sqlite3PagerWalDb(pPager, p->db); |
| 67765 | + |
| 67766 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67767 | + /* If transitioning from no transaction directly to a write transaction, |
| 67768 | + ** block for the WRITER lock first if possible. */ |
| 67769 | + if( pBt->pPage1==0 && wrflag ){ |
| 67770 | + assert( pBt->inTransaction==TRANS_NONE ); |
| 67771 | + rc = sqlite3PagerWalWriteLock(pPager, 1); |
| 67772 | + if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break; |
| 67773 | + } |
| 67774 | +#endif |
| 67775 | + |
| 67490 | 67776 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 67491 | 67777 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 67492 | 67778 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 67493 | 67779 | ** reading page 1 it discovers that the page-size of the database |
| 67494 | 67780 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| | @@ -67498,11 +67784,11 @@ |
| 67498 | 67784 | |
| 67499 | 67785 | if( rc==SQLITE_OK && wrflag ){ |
| 67500 | 67786 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ |
| 67501 | 67787 | rc = SQLITE_READONLY; |
| 67502 | 67788 | }else{ |
| 67503 | | - rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
| 67789 | + rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db)); |
| 67504 | 67790 | if( rc==SQLITE_OK ){ |
| 67505 | 67791 | rc = newDatabase(pBt); |
| 67506 | 67792 | }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ |
| 67507 | 67793 | /* if there was no transaction opened when this function was |
| 67508 | 67794 | ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error |
| | @@ -67511,15 +67797,19 @@ |
| 67511 | 67797 | } |
| 67512 | 67798 | } |
| 67513 | 67799 | } |
| 67514 | 67800 | |
| 67515 | 67801 | if( rc!=SQLITE_OK ){ |
| 67802 | + (void)sqlite3PagerWalWriteLock(pPager, 0); |
| 67516 | 67803 | unlockBtreeIfUnused(pBt); |
| 67517 | 67804 | } |
| 67518 | 67805 | }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
| 67519 | 67806 | btreeInvokeBusyHandler(pBt) ); |
| 67520 | | - sqlite3PagerResetLockTimeout(pBt->pPager); |
| 67807 | + sqlite3PagerWalDb(pPager, 0); |
| 67808 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67809 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 67810 | +#endif |
| 67521 | 67811 | |
| 67522 | 67812 | if( rc==SQLITE_OK ){ |
| 67523 | 67813 | if( p->inTrans==TRANS_NONE ){ |
| 67524 | 67814 | pBt->nTransaction++; |
| 67525 | 67815 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -67567,11 +67857,11 @@ |
| 67567 | 67857 | if( wrflag ){ |
| 67568 | 67858 | /* This call makes sure that the pager has the correct number of |
| 67569 | 67859 | ** open savepoints. If the second parameter is greater than 0 and |
| 67570 | 67860 | ** the sub-journal is not already open, then it will be opened here. |
| 67571 | 67861 | */ |
| 67572 | | - rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 67862 | + rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint); |
| 67573 | 67863 | } |
| 67574 | 67864 | } |
| 67575 | 67865 | |
| 67576 | 67866 | btreeIntegrity(p); |
| 67577 | 67867 | sqlite3BtreeLeave(p); |
| | @@ -71203,11 +71493,11 @@ |
| 71203 | 71493 | |
| 71204 | 71494 | /* Remove cells from the start and end of the page */ |
| 71205 | 71495 | assert( nCell>=0 ); |
| 71206 | 71496 | if( iOld<iNew ){ |
| 71207 | 71497 | int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
| 71208 | | - if( nShift>nCell ) return SQLITE_CORRUPT_BKPT; |
| 71498 | + if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT; |
| 71209 | 71499 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 71210 | 71500 | nCell -= nShift; |
| 71211 | 71501 | } |
| 71212 | 71502 | if( iNewEnd < iOldEnd ){ |
| 71213 | 71503 | int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| | @@ -73560,11 +73850,10 @@ |
| 73560 | 73850 | } |
| 73561 | 73851 | sqlite3BtreeLeave(p); |
| 73562 | 73852 | return rc; |
| 73563 | 73853 | } |
| 73564 | 73854 | |
| 73565 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 73566 | 73855 | /* |
| 73567 | 73856 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 73568 | 73857 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 73569 | 73858 | ** |
| 73570 | 73859 | ** SQLITE_OK is returned if the operation is successfully executed. |
| | @@ -73633,11 +73922,10 @@ |
| 73633 | 73922 | } |
| 73634 | 73923 | |
| 73635 | 73924 | /* An error has occurred. Return an error code. */ |
| 73636 | 73925 | return rc; |
| 73637 | 73926 | } |
| 73638 | | -#endif |
| 73639 | 73927 | |
| 73640 | 73928 | /* |
| 73641 | 73929 | ** Return the pager associated with a BTree. This routine is used for |
| 73642 | 73930 | ** testing and debugging only. |
| 73643 | 73931 | */ |
| | @@ -74684,11 +74972,11 @@ |
| 74684 | 74972 | ** Attempt to set the page size of the destination to match the page size |
| 74685 | 74973 | ** of the source. |
| 74686 | 74974 | */ |
| 74687 | 74975 | static int setDestPgsz(sqlite3_backup *p){ |
| 74688 | 74976 | int rc; |
| 74689 | | - rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 74977 | + rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0); |
| 74690 | 74978 | return rc; |
| 74691 | 74979 | } |
| 74692 | 74980 | |
| 74693 | 74981 | /* |
| 74694 | 74982 | ** Check that there is no open read-transaction on the b-tree passed as the |
| | @@ -78709,24 +78997,23 @@ |
| 78709 | 78997 | ** "PX" -> "r[X]" |
| 78710 | 78998 | ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 |
| 78711 | 78999 | ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 |
| 78712 | 79000 | ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x |
| 78713 | 79001 | */ |
| 78714 | | -static int displayComment( |
| 79002 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayComment( |
| 79003 | + sqlite3 *db, /* Optional - Oom error reporting only */ |
| 78715 | 79004 | const Op *pOp, /* The opcode to be commented */ |
| 78716 | | - const char *zP4, /* Previously obtained value for P4 */ |
| 78717 | | - char *zTemp, /* Write result here */ |
| 78718 | | - int nTemp /* Space available in zTemp[] */ |
| 79005 | + const char *zP4 /* Previously obtained value for P4 */ |
| 78719 | 79006 | ){ |
| 78720 | 79007 | const char *zOpName; |
| 78721 | 79008 | const char *zSynopsis; |
| 78722 | 79009 | int nOpName; |
| 78723 | 79010 | int ii; |
| 78724 | 79011 | char zAlt[50]; |
| 78725 | 79012 | StrAccum x; |
| 78726 | | - sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); |
| 78727 | 79013 | |
| 79014 | + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); |
| 78728 | 79015 | zOpName = sqlite3OpcodeName(pOp->opcode); |
| 78729 | 79016 | nOpName = sqlite3Strlen30(zOpName); |
| 78730 | 79017 | if( zOpName[nOpName+1] ){ |
| 78731 | 79018 | int seenCom = 0; |
| 78732 | 79019 | char c; |
| | @@ -78789,14 +79076,16 @@ |
| 78789 | 79076 | sqlite3_str_appendf(&x, "; %s", pOp->zComment); |
| 78790 | 79077 | } |
| 78791 | 79078 | }else if( pOp->zComment ){ |
| 78792 | 79079 | sqlite3_str_appendall(&x, pOp->zComment); |
| 78793 | 79080 | } |
| 78794 | | - sqlite3StrAccumFinish(&x); |
| 78795 | | - return x.nChar; |
| 79081 | + if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){ |
| 79082 | + sqlite3OomFault(db); |
| 79083 | + } |
| 79084 | + return sqlite3StrAccumFinish(&x); |
| 78796 | 79085 | } |
| 78797 | | -#endif /* SQLITE_DEBUG */ |
| 79086 | +#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */ |
| 78798 | 79087 | |
| 78799 | 79088 | #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) |
| 78800 | 79089 | /* |
| 78801 | 79090 | ** Translate the P4.pExpr value for an OP_CursorHint opcode into text |
| 78802 | 79091 | ** that can be displayed in the P4 column of EXPLAIN output. |
| | @@ -78873,15 +79162,15 @@ |
| 78873 | 79162 | #if VDBE_DISPLAY_P4 |
| 78874 | 79163 | /* |
| 78875 | 79164 | ** Compute a string that describes the P4 parameter for an opcode. |
| 78876 | 79165 | ** Use zTemp for any required temporary buffer space. |
| 78877 | 79166 | */ |
| 78878 | | -static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 78879 | | - char *zP4 = zTemp; |
| 79167 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){ |
| 79168 | + char *zP4 = 0; |
| 78880 | 79169 | StrAccum x; |
| 78881 | | - assert( nTemp>=20 ); |
| 78882 | | - sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); |
| 79170 | + |
| 79171 | + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); |
| 78883 | 79172 | switch( pOp->p4type ){ |
| 78884 | 79173 | case P4_KEYINFO: { |
| 78885 | 79174 | int j; |
| 78886 | 79175 | KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 78887 | 79176 | assert( pKeyInfo->aSortFlags!=0 ); |
| | @@ -78961,40 +79250,36 @@ |
| 78961 | 79250 | int i; |
| 78962 | 79251 | int *ai = pOp->p4.ai; |
| 78963 | 79252 | int n = ai[0]; /* The first element of an INTARRAY is always the |
| 78964 | 79253 | ** count of the number of elements to follow */ |
| 78965 | 79254 | for(i=1; i<=n; i++){ |
| 78966 | | - sqlite3_str_appendf(&x, ",%d", ai[i]); |
| 79255 | + sqlite3_str_appendf(&x, "%c%d", (i==1 ? '[' : ','), ai[i]); |
| 78967 | 79256 | } |
| 78968 | | - zTemp[0] = '['; |
| 78969 | 79257 | sqlite3_str_append(&x, "]", 1); |
| 78970 | 79258 | break; |
| 78971 | 79259 | } |
| 78972 | 79260 | case P4_SUBPROGRAM: { |
| 78973 | | - sqlite3_str_appendf(&x, "program"); |
| 79261 | + zP4 = "program"; |
| 78974 | 79262 | break; |
| 78975 | 79263 | } |
| 78976 | 79264 | case P4_DYNBLOB: |
| 78977 | 79265 | case P4_ADVANCE: { |
| 78978 | | - zTemp[0] = 0; |
| 78979 | 79266 | break; |
| 78980 | 79267 | } |
| 78981 | 79268 | case P4_TABLE: { |
| 78982 | | - sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName); |
| 79269 | + zP4 = pOp->p4.pTab->zName; |
| 78983 | 79270 | break; |
| 78984 | 79271 | } |
| 78985 | 79272 | default: { |
| 78986 | 79273 | zP4 = pOp->p4.z; |
| 78987 | | - if( zP4==0 ){ |
| 78988 | | - zP4 = zTemp; |
| 78989 | | - zTemp[0] = 0; |
| 78990 | | - } |
| 78991 | 79274 | } |
| 78992 | 79275 | } |
| 78993 | | - sqlite3StrAccumFinish(&x); |
| 78994 | | - assert( zP4!=0 ); |
| 78995 | | - return zP4; |
| 79276 | + if( zP4 ) sqlite3_str_appendall(&x, zP4); |
| 79277 | + if( (x.accError & SQLITE_NOMEM)!=0 ){ |
| 79278 | + sqlite3OomFault(db); |
| 79279 | + } |
| 79280 | + return sqlite3StrAccumFinish(&x); |
| 78996 | 79281 | } |
| 78997 | 79282 | #endif /* VDBE_DISPLAY_P4 */ |
| 78998 | 79283 | |
| 78999 | 79284 | /* |
| 79000 | 79285 | ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
| | @@ -79080,28 +79365,34 @@ |
| 79080 | 79365 | /* |
| 79081 | 79366 | ** Print a single opcode. This routine is used for debugging only. |
| 79082 | 79367 | */ |
| 79083 | 79368 | SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){ |
| 79084 | 79369 | char *zP4; |
| 79085 | | - char zPtr[50]; |
| 79086 | | - char zCom[100]; |
| 79370 | + char *zCom; |
| 79371 | + sqlite3 dummyDb; |
| 79087 | 79372 | static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; |
| 79088 | 79373 | if( pOut==0 ) pOut = stdout; |
| 79089 | | - zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
| 79374 | + sqlite3BeginBenignMalloc(); |
| 79375 | + dummyDb.mallocFailed = 1; |
| 79376 | + zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp); |
| 79090 | 79377 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 79091 | | - displayComment(pOp, zP4, zCom, sizeof(zCom)); |
| 79378 | + zCom = sqlite3VdbeDisplayComment(0, pOp, zP4); |
| 79092 | 79379 | #else |
| 79093 | | - zCom[0] = 0; |
| 79380 | + zCom = 0; |
| 79094 | 79381 | #endif |
| 79095 | 79382 | /* NB: The sqlite3OpcodeName() function is implemented by code created |
| 79096 | 79383 | ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the |
| 79097 | 79384 | ** information from the vdbe.c source text */ |
| 79098 | 79385 | fprintf(pOut, zFormat1, pc, |
| 79099 | | - sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
| 79100 | | - zCom |
| 79386 | + sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, |
| 79387 | + zP4 ? zP4 : "", pOp->p5, |
| 79388 | + zCom ? zCom : "" |
| 79101 | 79389 | ); |
| 79102 | 79390 | fflush(pOut); |
| 79391 | + sqlite3_free(zP4); |
| 79392 | + sqlite3_free(zCom); |
| 79393 | + sqlite3EndBenignMalloc(); |
| 79103 | 79394 | } |
| 79104 | 79395 | #endif |
| 79105 | 79396 | |
| 79106 | 79397 | /* |
| 79107 | 79398 | ** Initialize an array of N Mem element. |
| | @@ -79188,10 +79479,125 @@ |
| 79188 | 79479 | assert( sqlite3VdbeFrameIsValid(pFrame) ); |
| 79189 | 79480 | pFrame->pParent = pFrame->v->pDelFrame; |
| 79190 | 79481 | pFrame->v->pDelFrame = pFrame; |
| 79191 | 79482 | } |
| 79192 | 79483 | |
| 79484 | +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN) |
| 79485 | +/* |
| 79486 | +** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN |
| 79487 | +** QUERY PLAN output. |
| 79488 | +** |
| 79489 | +** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no |
| 79490 | +** more opcodes to be displayed. |
| 79491 | +*/ |
| 79492 | +SQLITE_PRIVATE int sqlite3VdbeNextOpcode( |
| 79493 | + Vdbe *p, /* The statement being explained */ |
| 79494 | + Mem *pSub, /* Storage for keeping track of subprogram nesting */ |
| 79495 | + int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */ |
| 79496 | + int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */ |
| 79497 | + int *piAddr, /* OUT: Write index into (*paOp)[] here */ |
| 79498 | + Op **paOp /* OUT: Write the opcode array here */ |
| 79499 | +){ |
| 79500 | + int nRow; /* Stop when row count reaches this */ |
| 79501 | + int nSub = 0; /* Number of sub-vdbes seen so far */ |
| 79502 | + SubProgram **apSub = 0; /* Array of sub-vdbes */ |
| 79503 | + int i; /* Next instruction address */ |
| 79504 | + int rc = SQLITE_OK; /* Result code */ |
| 79505 | + Op *aOp = 0; /* Opcode array */ |
| 79506 | + int iPc; /* Rowid. Copy of value in *piPc */ |
| 79507 | + |
| 79508 | + /* When the number of output rows reaches nRow, that means the |
| 79509 | + ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
| 79510 | + ** nRow is the sum of the number of rows in the main program, plus |
| 79511 | + ** the sum of the number of rows in all trigger subprograms encountered |
| 79512 | + ** so far. The nRow value will increase as new trigger subprograms are |
| 79513 | + ** encountered, but p->pc will eventually catch up to nRow. |
| 79514 | + */ |
| 79515 | + nRow = p->nOp; |
| 79516 | + if( pSub!=0 ){ |
| 79517 | + if( pSub->flags&MEM_Blob ){ |
| 79518 | + /* pSub is initiallly NULL. It is initialized to a BLOB by |
| 79519 | + ** the P4_SUBPROGRAM processing logic below */ |
| 79520 | + nSub = pSub->n/sizeof(Vdbe*); |
| 79521 | + apSub = (SubProgram **)pSub->z; |
| 79522 | + } |
| 79523 | + for(i=0; i<nSub; i++){ |
| 79524 | + nRow += apSub[i]->nOp; |
| 79525 | + } |
| 79526 | + } |
| 79527 | + iPc = *piPc; |
| 79528 | + while(1){ /* Loop exits via break */ |
| 79529 | + i = iPc++; |
| 79530 | + if( i>=nRow ){ |
| 79531 | + p->rc = SQLITE_OK; |
| 79532 | + rc = SQLITE_DONE; |
| 79533 | + break; |
| 79534 | + } |
| 79535 | + if( i<p->nOp ){ |
| 79536 | + /* The rowid is small enough that we are still in the |
| 79537 | + ** main program. */ |
| 79538 | + aOp = p->aOp; |
| 79539 | + }else{ |
| 79540 | + /* We are currently listing subprograms. Figure out which one and |
| 79541 | + ** pick up the appropriate opcode. */ |
| 79542 | + int j; |
| 79543 | + i -= p->nOp; |
| 79544 | + assert( apSub!=0 ); |
| 79545 | + assert( nSub>0 ); |
| 79546 | + for(j=0; i>=apSub[j]->nOp; j++){ |
| 79547 | + i -= apSub[j]->nOp; |
| 79548 | + assert( i<apSub[j]->nOp || j+1<nSub ); |
| 79549 | + } |
| 79550 | + aOp = apSub[j]->aOp; |
| 79551 | + } |
| 79552 | + |
| 79553 | + /* When an OP_Program opcode is encounter (the only opcode that has |
| 79554 | + ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
| 79555 | + ** kept in p->aMem[9].z to hold the new program - assuming this subprogram |
| 79556 | + ** has not already been seen. |
| 79557 | + */ |
| 79558 | + if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){ |
| 79559 | + int nByte = (nSub+1)*sizeof(SubProgram*); |
| 79560 | + int j; |
| 79561 | + for(j=0; j<nSub; j++){ |
| 79562 | + if( apSub[j]==aOp[i].p4.pProgram ) break; |
| 79563 | + } |
| 79564 | + if( j==nSub ){ |
| 79565 | + p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0); |
| 79566 | + if( p->rc!=SQLITE_OK ){ |
| 79567 | + rc = SQLITE_ERROR; |
| 79568 | + break; |
| 79569 | + } |
| 79570 | + apSub = (SubProgram **)pSub->z; |
| 79571 | + apSub[nSub++] = aOp[i].p4.pProgram; |
| 79572 | + MemSetTypeFlag(pSub, MEM_Blob); |
| 79573 | + pSub->n = nSub*sizeof(SubProgram*); |
| 79574 | + nRow += aOp[i].p4.pProgram->nOp; |
| 79575 | + } |
| 79576 | + } |
| 79577 | + if( eMode==0 ) break; |
| 79578 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 79579 | + if( eMode==2 ){ |
| 79580 | + Op *pOp = aOp + i; |
| 79581 | + if( pOp->opcode==OP_OpenRead ) break; |
| 79582 | + if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break; |
| 79583 | + if( pOp->opcode==OP_ReopenIdx ) break; |
| 79584 | + }else |
| 79585 | +#endif |
| 79586 | + { |
| 79587 | + assert( eMode==1 ); |
| 79588 | + if( aOp[i].opcode==OP_Explain ) break; |
| 79589 | + if( aOp[i].opcode==OP_Init && iPc>1 ) break; |
| 79590 | + } |
| 79591 | + } |
| 79592 | + *piPc = iPc; |
| 79593 | + *piAddr = i; |
| 79594 | + *paOp = aOp; |
| 79595 | + return rc; |
| 79596 | +} |
| 79597 | +#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */ |
| 79598 | + |
| 79193 | 79599 | |
| 79194 | 79600 | /* |
| 79195 | 79601 | ** Delete a VdbeFrame object and its contents. VdbeFrame objects are |
| 79196 | 79602 | ** allocated by the OP_Program opcode in sqlite3VdbeExec(). |
| 79197 | 79603 | */ |
| | @@ -79228,20 +79634,18 @@ |
| 79228 | 79634 | ** the trigger subprograms are listed one by one. |
| 79229 | 79635 | */ |
| 79230 | 79636 | SQLITE_PRIVATE int sqlite3VdbeList( |
| 79231 | 79637 | Vdbe *p /* The VDBE */ |
| 79232 | 79638 | ){ |
| 79233 | | - int nRow; /* Stop when row count reaches this */ |
| 79234 | | - int nSub = 0; /* Number of sub-vdbes seen so far */ |
| 79235 | | - SubProgram **apSub = 0; /* Array of sub-vdbes */ |
| 79236 | 79639 | Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
| 79237 | 79640 | sqlite3 *db = p->db; /* The database connection */ |
| 79238 | 79641 | int i; /* Loop counter */ |
| 79239 | 79642 | int rc = SQLITE_OK; /* Return code */ |
| 79240 | 79643 | Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
| 79241 | 79644 | int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0); |
| 79242 | | - Op *pOp = 0; |
| 79645 | + Op *aOp; /* Array of opcodes */ |
| 79646 | + Op *pOp; /* Current opcode */ |
| 79243 | 79647 | |
| 79244 | 79648 | assert( p->explain ); |
| 79245 | 79649 | assert( p->magic==VDBE_MAGIC_RUN ); |
| 79246 | 79650 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 79247 | 79651 | |
| | @@ -79257,166 +79661,66 @@ |
| 79257 | 79661 | ** sqlite3_column_text16() failed. */ |
| 79258 | 79662 | sqlite3OomFault(db); |
| 79259 | 79663 | return SQLITE_ERROR; |
| 79260 | 79664 | } |
| 79261 | 79665 | |
| 79262 | | - /* When the number of output rows reaches nRow, that means the |
| 79263 | | - ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
| 79264 | | - ** nRow is the sum of the number of rows in the main program, plus |
| 79265 | | - ** the sum of the number of rows in all trigger subprograms encountered |
| 79266 | | - ** so far. The nRow value will increase as new trigger subprograms are |
| 79267 | | - ** encountered, but p->pc will eventually catch up to nRow. |
| 79268 | | - */ |
| 79269 | | - nRow = p->nOp; |
| 79270 | 79666 | if( bListSubprogs ){ |
| 79271 | 79667 | /* The first 8 memory cells are used for the result set. So we will |
| 79272 | 79668 | ** commandeer the 9th cell to use as storage for an array of pointers |
| 79273 | 79669 | ** to trigger subprograms. The VDBE is guaranteed to have at least 9 |
| 79274 | 79670 | ** cells. */ |
| 79275 | 79671 | assert( p->nMem>9 ); |
| 79276 | 79672 | pSub = &p->aMem[9]; |
| 79277 | | - if( pSub->flags&MEM_Blob ){ |
| 79278 | | - /* On the first call to sqlite3_step(), pSub will hold a NULL. It is |
| 79279 | | - ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ |
| 79280 | | - nSub = pSub->n/sizeof(Vdbe*); |
| 79281 | | - apSub = (SubProgram **)pSub->z; |
| 79282 | | - } |
| 79283 | | - for(i=0; i<nSub; i++){ |
| 79284 | | - nRow += apSub[i]->nOp; |
| 79285 | | - } |
| 79286 | | - } |
| 79287 | | - |
| 79288 | | - while(1){ /* Loop exits via break */ |
| 79289 | | - i = p->pc++; |
| 79290 | | - if( i>=nRow ){ |
| 79291 | | - p->rc = SQLITE_OK; |
| 79292 | | - rc = SQLITE_DONE; |
| 79293 | | - break; |
| 79294 | | - } |
| 79295 | | - if( i<p->nOp ){ |
| 79296 | | - /* The output line number is small enough that we are still in the |
| 79297 | | - ** main program. */ |
| 79298 | | - pOp = &p->aOp[i]; |
| 79299 | | - }else{ |
| 79300 | | - /* We are currently listing subprograms. Figure out which one and |
| 79301 | | - ** pick up the appropriate opcode. */ |
| 79302 | | - int j; |
| 79303 | | - i -= p->nOp; |
| 79304 | | - assert( apSub!=0 ); |
| 79305 | | - assert( nSub>0 ); |
| 79306 | | - for(j=0; i>=apSub[j]->nOp; j++){ |
| 79307 | | - i -= apSub[j]->nOp; |
| 79308 | | - assert( i<apSub[j]->nOp || j+1<nSub ); |
| 79309 | | - } |
| 79310 | | - pOp = &apSub[j]->aOp[i]; |
| 79311 | | - } |
| 79312 | | - |
| 79313 | | - /* When an OP_Program opcode is encounter (the only opcode that has |
| 79314 | | - ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
| 79315 | | - ** kept in p->aMem[9].z to hold the new program - assuming this subprogram |
| 79316 | | - ** has not already been seen. |
| 79317 | | - */ |
| 79318 | | - if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){ |
| 79319 | | - int nByte = (nSub+1)*sizeof(SubProgram*); |
| 79320 | | - int j; |
| 79321 | | - for(j=0; j<nSub; j++){ |
| 79322 | | - if( apSub[j]==pOp->p4.pProgram ) break; |
| 79323 | | - } |
| 79324 | | - if( j==nSub ){ |
| 79325 | | - p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0); |
| 79326 | | - if( p->rc!=SQLITE_OK ){ |
| 79327 | | - rc = SQLITE_ERROR; |
| 79328 | | - break; |
| 79329 | | - } |
| 79330 | | - apSub = (SubProgram **)pSub->z; |
| 79331 | | - apSub[nSub++] = pOp->p4.pProgram; |
| 79332 | | - pSub->flags |= MEM_Blob; |
| 79333 | | - pSub->n = nSub*sizeof(SubProgram*); |
| 79334 | | - nRow += pOp->p4.pProgram->nOp; |
| 79335 | | - } |
| 79336 | | - } |
| 79337 | | - if( p->explain<2 ) break; |
| 79338 | | - if( pOp->opcode==OP_Explain ) break; |
| 79339 | | - if( pOp->opcode==OP_Init && p->pc>1 ) break; |
| 79340 | | - } |
| 79341 | | - |
| 79342 | | - if( rc==SQLITE_OK ){ |
| 79673 | + }else{ |
| 79674 | + pSub = 0; |
| 79675 | + } |
| 79676 | + |
| 79677 | + /* Figure out which opcode is next to display */ |
| 79678 | + rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp); |
| 79679 | + |
| 79680 | + if( rc==SQLITE_OK ){ |
| 79681 | + pOp = aOp + i; |
| 79343 | 79682 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
| 79344 | 79683 | p->rc = SQLITE_INTERRUPT; |
| 79345 | 79684 | rc = SQLITE_ERROR; |
| 79346 | 79685 | sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); |
| 79347 | 79686 | }else{ |
| 79348 | | - char *zP4; |
| 79349 | | - if( p->explain==1 ){ |
| 79350 | | - pMem->flags = MEM_Int; |
| 79351 | | - pMem->u.i = i; /* Program counter */ |
| 79352 | | - pMem++; |
| 79353 | | - |
| 79354 | | - pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
| 79355 | | - pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
| 79356 | | - assert( pMem->z!=0 ); |
| 79357 | | - pMem->n = sqlite3Strlen30(pMem->z); |
| 79358 | | - pMem->enc = SQLITE_UTF8; |
| 79359 | | - pMem++; |
| 79360 | | - } |
| 79361 | | - |
| 79362 | | - pMem->flags = MEM_Int; |
| 79363 | | - pMem->u.i = pOp->p1; /* P1 */ |
| 79364 | | - pMem++; |
| 79365 | | - |
| 79366 | | - pMem->flags = MEM_Int; |
| 79367 | | - pMem->u.i = pOp->p2; /* P2 */ |
| 79368 | | - pMem++; |
| 79369 | | - |
| 79370 | | - pMem->flags = MEM_Int; |
| 79371 | | - pMem->u.i = pOp->p3; /* P3 */ |
| 79372 | | - pMem++; |
| 79373 | | - |
| 79374 | | - if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ |
| 79375 | | - assert( p->db->mallocFailed ); |
| 79376 | | - return SQLITE_ERROR; |
| 79377 | | - } |
| 79378 | | - pMem->flags = MEM_Str|MEM_Term; |
| 79379 | | - zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); |
| 79380 | | - if( zP4!=pMem->z ){ |
| 79381 | | - pMem->n = 0; |
| 79382 | | - sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
| 79687 | + char *zP4 = sqlite3VdbeDisplayP4(db, pOp); |
| 79688 | + if( p->explain==2 ){ |
| 79689 | + sqlite3VdbeMemSetInt64(pMem, pOp->p1); |
| 79690 | + sqlite3VdbeMemSetInt64(pMem+1, pOp->p2); |
| 79691 | + sqlite3VdbeMemSetInt64(pMem+2, pOp->p3); |
| 79692 | + sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free); |
| 79693 | + p->nResColumn = 4; |
| 79383 | 79694 | }else{ |
| 79384 | | - assert( pMem->z!=0 ); |
| 79385 | | - pMem->n = sqlite3Strlen30(pMem->z); |
| 79386 | | - pMem->enc = SQLITE_UTF8; |
| 79387 | | - } |
| 79388 | | - pMem++; |
| 79389 | | - |
| 79390 | | - if( p->explain==1 ){ |
| 79391 | | - if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ |
| 79392 | | - assert( p->db->mallocFailed ); |
| 79393 | | - return SQLITE_ERROR; |
| 79394 | | - } |
| 79395 | | - pMem->flags = MEM_Str|MEM_Term; |
| 79396 | | - pMem->n = 2; |
| 79397 | | - sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
| 79398 | | - pMem->enc = SQLITE_UTF8; |
| 79399 | | - pMem++; |
| 79400 | | - |
| 79695 | + sqlite3VdbeMemSetInt64(pMem+0, i); |
| 79696 | + sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode), |
| 79697 | + -1, SQLITE_UTF8, SQLITE_STATIC); |
| 79698 | + sqlite3VdbeMemSetInt64(pMem+2, pOp->p1); |
| 79699 | + sqlite3VdbeMemSetInt64(pMem+3, pOp->p2); |
| 79700 | + sqlite3VdbeMemSetInt64(pMem+4, pOp->p3); |
| 79701 | + /* pMem+5 for p4 is done last */ |
| 79702 | + sqlite3VdbeMemSetInt64(pMem+6, pOp->p5); |
| 79401 | 79703 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 79402 | | - if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ |
| 79403 | | - assert( p->db->mallocFailed ); |
| 79404 | | - return SQLITE_ERROR; |
| 79405 | | - } |
| 79406 | | - pMem->flags = MEM_Str|MEM_Term; |
| 79407 | | - pMem->n = displayComment(pOp, zP4, pMem->z, 500); |
| 79408 | | - pMem->enc = SQLITE_UTF8; |
| 79409 | | -#else |
| 79410 | | - pMem->flags = MEM_Null; /* Comment */ |
| 79411 | | -#endif |
| 79412 | | - } |
| 79413 | | - |
| 79414 | | - p->nResColumn = 8 - 4*(p->explain-1); |
| 79415 | | - p->pResultSet = &p->aMem[1]; |
| 79416 | | - p->rc = SQLITE_OK; |
| 79417 | | - rc = SQLITE_ROW; |
| 79704 | + { |
| 79705 | + char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4); |
| 79706 | + sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free); |
| 79707 | + } |
| 79708 | +#else |
| 79709 | + sqlite3VdbeMemSetNull(pMem+7); |
| 79710 | +#endif |
| 79711 | + sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free); |
| 79712 | + p->nResColumn = 8; |
| 79713 | + } |
| 79714 | + p->pResultSet = pMem; |
| 79715 | + if( db->mallocFailed ){ |
| 79716 | + p->rc = SQLITE_NOMEM; |
| 79717 | + rc = SQLITE_ERROR; |
| 79718 | + }else{ |
| 79719 | + p->rc = SQLITE_OK; |
| 79720 | + rc = SQLITE_ROW; |
| 79721 | + } |
| 79418 | 79722 | } |
| 79419 | 79723 | } |
| 79420 | 79724 | return rc; |
| 79421 | 79725 | } |
| 79422 | 79726 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| | @@ -79982,12 +80286,13 @@ |
| 79982 | 80286 | int retryCount = 0; |
| 79983 | 80287 | int nMainFile; |
| 79984 | 80288 | |
| 79985 | 80289 | /* Select a master journal file name */ |
| 79986 | 80290 | nMainFile = sqlite3Strlen30(zMainFile); |
| 79987 | | - zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz%c%c", zMainFile, 0, 0); |
| 80291 | + zMaster = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0); |
| 79988 | 80292 | if( zMaster==0 ) return SQLITE_NOMEM_BKPT; |
| 80293 | + zMaster += 4; |
| 79989 | 80294 | do { |
| 79990 | 80295 | u32 iRandom; |
| 79991 | 80296 | if( retryCount ){ |
| 79992 | 80297 | if( retryCount>100 ){ |
| 79993 | 80298 | sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); |
| | @@ -80013,11 +80318,11 @@ |
| 80013 | 80318 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 80014 | 80319 | SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
| 80015 | 80320 | ); |
| 80016 | 80321 | } |
| 80017 | 80322 | if( rc!=SQLITE_OK ){ |
| 80018 | | - sqlite3DbFree(db, zMaster); |
| 80323 | + sqlite3DbFree(db, zMaster-4); |
| 80019 | 80324 | return rc; |
| 80020 | 80325 | } |
| 80021 | 80326 | |
| 80022 | 80327 | /* Write the name of each database file in the transaction into the new |
| 80023 | 80328 | ** master journal file. If an error occurs at this point close |
| | @@ -80036,11 +80341,11 @@ |
| 80036 | 80341 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 80037 | 80342 | offset += sqlite3Strlen30(zFile)+1; |
| 80038 | 80343 | if( rc!=SQLITE_OK ){ |
| 80039 | 80344 | sqlite3OsCloseFree(pMaster); |
| 80040 | 80345 | sqlite3OsDelete(pVfs, zMaster, 0); |
| 80041 | | - sqlite3DbFree(db, zMaster); |
| 80346 | + sqlite3DbFree(db, zMaster-4); |
| 80042 | 80347 | return rc; |
| 80043 | 80348 | } |
| 80044 | 80349 | } |
| 80045 | 80350 | } |
| 80046 | 80351 | |
| | @@ -80050,11 +80355,11 @@ |
| 80050 | 80355 | if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) |
| 80051 | 80356 | && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) |
| 80052 | 80357 | ){ |
| 80053 | 80358 | sqlite3OsCloseFree(pMaster); |
| 80054 | 80359 | sqlite3OsDelete(pVfs, zMaster, 0); |
| 80055 | | - sqlite3DbFree(db, zMaster); |
| 80360 | + sqlite3DbFree(db, zMaster-4); |
| 80056 | 80361 | return rc; |
| 80057 | 80362 | } |
| 80058 | 80363 | |
| 80059 | 80364 | /* Sync all the db files involved in the transaction. The same call |
| 80060 | 80365 | ** sets the master journal pointer in each individual journal. If |
| | @@ -80073,20 +80378,20 @@ |
| 80073 | 80378 | } |
| 80074 | 80379 | } |
| 80075 | 80380 | sqlite3OsCloseFree(pMaster); |
| 80076 | 80381 | assert( rc!=SQLITE_BUSY ); |
| 80077 | 80382 | if( rc!=SQLITE_OK ){ |
| 80078 | | - sqlite3DbFree(db, zMaster); |
| 80383 | + sqlite3DbFree(db, zMaster-4); |
| 80079 | 80384 | return rc; |
| 80080 | 80385 | } |
| 80081 | 80386 | |
| 80082 | 80387 | /* Delete the master journal file. This commits the transaction. After |
| 80083 | 80388 | ** doing this the directory is synced again before any individual |
| 80084 | 80389 | ** transaction files are deleted. |
| 80085 | 80390 | */ |
| 80086 | 80391 | rc = sqlite3OsDelete(pVfs, zMaster, 1); |
| 80087 | | - sqlite3DbFree(db, zMaster); |
| 80392 | + sqlite3DbFree(db, zMaster-4); |
| 80088 | 80393 | zMaster = 0; |
| 80089 | 80394 | if( rc ){ |
| 80090 | 80395 | return rc; |
| 80091 | 80396 | } |
| 80092 | 80397 | |
| | @@ -83825,11 +84130,11 @@ |
| 83825 | 84130 | p->db->errCode = SQLITE_OK; |
| 83826 | 84131 | |
| 83827 | 84132 | /* If the bit corresponding to this variable in Vdbe.expmask is set, then |
| 83828 | 84133 | ** binding a new value to this variable invalidates the current query plan. |
| 83829 | 84134 | ** |
| 83830 | | - ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host |
| 84135 | + ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host |
| 83831 | 84136 | ** parameter in the WHERE clause might influence the choice of query plan |
| 83832 | 84137 | ** for a statement, then the statement will be automatically recompiled, |
| 83833 | 84138 | ** as if there had been a schema change, on the first sqlite3_step() call |
| 83834 | 84139 | ** following any change to the bindings of that parameter. |
| 83835 | 84140 | */ |
| | @@ -87998,32 +88303,38 @@ |
| 87998 | 88303 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 87999 | 88304 | REGISTER_TRACE(pOp->p3, pOut); |
| 88000 | 88305 | break; |
| 88001 | 88306 | } |
| 88002 | 88307 | |
| 88003 | | -/* Opcode: Count P1 P2 * * * |
| 88308 | +/* Opcode: Count P1 P2 p3 * * |
| 88004 | 88309 | ** Synopsis: r[P2]=count() |
| 88005 | 88310 | ** |
| 88006 | 88311 | ** Store the number of entries (an integer value) in the table or index |
| 88007 | | -** opened by cursor P1 in register P2 |
| 88312 | +** opened by cursor P1 in register P2. |
| 88313 | +** |
| 88314 | +** If P3==0, then an exact count is obtained, which involves visiting |
| 88315 | +** every btree page of the table. But if P3 is non-zero, an estimate |
| 88316 | +** is returned based on the current cursor position. |
| 88008 | 88317 | */ |
| 88009 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 88010 | 88318 | case OP_Count: { /* out2 */ |
| 88011 | 88319 | i64 nEntry; |
| 88012 | 88320 | BtCursor *pCrsr; |
| 88013 | 88321 | |
| 88014 | 88322 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 88015 | 88323 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
| 88016 | 88324 | assert( pCrsr ); |
| 88017 | | - nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 88018 | | - rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 88019 | | - if( rc ) goto abort_due_to_error; |
| 88325 | + if( pOp->p3 ){ |
| 88326 | + nEntry = sqlite3BtreeRowCountEst(pCrsr); |
| 88327 | + }else{ |
| 88328 | + nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 88329 | + rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 88330 | + if( rc ) goto abort_due_to_error; |
| 88331 | + } |
| 88020 | 88332 | pOut = out2Prerelease(p, pOp); |
| 88021 | 88333 | pOut->u.i = nEntry; |
| 88022 | 88334 | goto check_for_interrupt; |
| 88023 | 88335 | } |
| 88024 | | -#endif |
| 88025 | 88336 | |
| 88026 | 88337 | /* Opcode: Savepoint P1 * * P4 * |
| 88027 | 88338 | ** |
| 88028 | 88339 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 88029 | 88340 | ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| | @@ -90457,16 +90768,23 @@ |
| 90457 | 90768 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 90458 | 90769 | if( rc) goto abort_due_to_error; |
| 90459 | 90770 | break; |
| 90460 | 90771 | } |
| 90461 | 90772 | |
| 90462 | | -/* Opcode: IdxDelete P1 P2 P3 * * |
| 90773 | +/* Opcode: IdxDelete P1 P2 P3 * P5 |
| 90463 | 90774 | ** Synopsis: key=r[P2@P3] |
| 90464 | 90775 | ** |
| 90465 | 90776 | ** The content of P3 registers starting at register P2 form |
| 90466 | 90777 | ** an unpacked index key. This opcode removes that entry from the |
| 90467 | 90778 | ** index opened by cursor P1. |
| 90779 | +** |
| 90780 | +** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 90781 | +** if no matching index entry is found. This happens when running |
| 90782 | +** an UPDATE or DELETE statement and the index entry to be updated |
| 90783 | +** or deleted is not found. For some uses of IdxDelete |
| 90784 | +** (example: the EXCEPT operator) it does not matter that no matching |
| 90785 | +** entry is found. For those cases, P5 is zero. |
| 90468 | 90786 | */ |
| 90469 | 90787 | case OP_IdxDelete: { |
| 90470 | 90788 | VdbeCursor *pC; |
| 90471 | 90789 | BtCursor *pCrsr; |
| 90472 | 90790 | int res; |
| | @@ -90479,20 +90797,22 @@ |
| 90479 | 90797 | assert( pC!=0 ); |
| 90480 | 90798 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 90481 | 90799 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 90482 | 90800 | pCrsr = pC->uc.pCursor; |
| 90483 | 90801 | assert( pCrsr!=0 ); |
| 90484 | | - assert( pOp->p5==0 ); |
| 90485 | 90802 | r.pKeyInfo = pC->pKeyInfo; |
| 90486 | 90803 | r.nField = (u16)pOp->p3; |
| 90487 | 90804 | r.default_rc = 0; |
| 90488 | 90805 | r.aMem = &aMem[pOp->p2]; |
| 90489 | 90806 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
| 90490 | 90807 | if( rc ) goto abort_due_to_error; |
| 90491 | 90808 | if( res==0 ){ |
| 90492 | 90809 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
| 90493 | 90810 | if( rc ) goto abort_due_to_error; |
| 90811 | + }else if( pOp->p5 ){ |
| 90812 | + rc = SQLITE_CORRUPT_INDEX; |
| 90813 | + goto abort_due_to_error; |
| 90494 | 90814 | } |
| 90495 | 90815 | assert( pC->deferredMoveto==0 ); |
| 90496 | 90816 | pC->cacheStatus = CACHE_STALE; |
| 90497 | 90817 | pC->seekResult = 0; |
| 90498 | 90818 | break; |
| | @@ -96093,10 +96413,437 @@ |
| 96093 | 96413 | *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2); |
| 96094 | 96414 | return SQLITE_OK; |
| 96095 | 96415 | } |
| 96096 | 96416 | |
| 96097 | 96417 | /************** End of vdbesort.c ********************************************/ |
| 96418 | +/************** Begin file vdbevtab.c ****************************************/ |
| 96419 | +/* |
| 96420 | +** 2020-03-23 |
| 96421 | +** |
| 96422 | +** The author disclaims copyright to this source code. In place of |
| 96423 | +** a legal notice, here is a blessing: |
| 96424 | +** |
| 96425 | +** May you do good and not evil. |
| 96426 | +** May you find forgiveness for yourself and forgive others. |
| 96427 | +** May you share freely, never taking more than you give. |
| 96428 | +** |
| 96429 | +************************************************************************* |
| 96430 | +** |
| 96431 | +** This file implements virtual-tables for examining the bytecode content |
| 96432 | +** of a prepared statement. |
| 96433 | +*/ |
| 96434 | +/* #include "sqliteInt.h" */ |
| 96435 | +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 96436 | +/* #include "vdbeInt.h" */ |
| 96437 | + |
| 96438 | +/* An instance of the bytecode() table-valued function. |
| 96439 | +*/ |
| 96440 | +typedef struct bytecodevtab bytecodevtab; |
| 96441 | +struct bytecodevtab { |
| 96442 | + sqlite3_vtab base; /* Base class - must be first */ |
| 96443 | + sqlite3 *db; /* Database connection */ |
| 96444 | + int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */ |
| 96445 | +}; |
| 96446 | + |
| 96447 | +/* A cursor for scanning through the bytecode |
| 96448 | +*/ |
| 96449 | +typedef struct bytecodevtab_cursor bytecodevtab_cursor; |
| 96450 | +struct bytecodevtab_cursor { |
| 96451 | + sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 96452 | + sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */ |
| 96453 | + int iRowid; /* The rowid of the output table */ |
| 96454 | + int iAddr; /* Address */ |
| 96455 | + int needFinalize; /* Cursors owns pStmt and must finalize it */ |
| 96456 | + int showSubprograms; /* Provide a listing of subprograms */ |
| 96457 | + Op *aOp; /* Operand array */ |
| 96458 | + char *zP4; /* Rendered P4 value */ |
| 96459 | + const char *zType; /* tables_used.type */ |
| 96460 | + const char *zSchema; /* tables_used.schema */ |
| 96461 | + const char *zName; /* tables_used.name */ |
| 96462 | + Mem sub; /* Subprograms */ |
| 96463 | +}; |
| 96464 | + |
| 96465 | +/* |
| 96466 | +** Create a new bytecode() table-valued function. |
| 96467 | +*/ |
| 96468 | +static int bytecodevtabConnect( |
| 96469 | + sqlite3 *db, |
| 96470 | + void *pAux, |
| 96471 | + int argc, const char *const*argv, |
| 96472 | + sqlite3_vtab **ppVtab, |
| 96473 | + char **pzErr |
| 96474 | +){ |
| 96475 | + bytecodevtab *pNew; |
| 96476 | + int rc; |
| 96477 | + int isTabUsed = pAux!=0; |
| 96478 | + const char *azSchema[2] = { |
| 96479 | + /* bytecode() schema */ |
| 96480 | + "CREATE TABLE x(" |
| 96481 | + "addr INT," |
| 96482 | + "opcode TEXT," |
| 96483 | + "p1 INT," |
| 96484 | + "p2 INT," |
| 96485 | + "p3 INT," |
| 96486 | + "p4 TEXT," |
| 96487 | + "p5 INT," |
| 96488 | + "comment TEXT," |
| 96489 | + "subprog TEXT," |
| 96490 | + "stmt HIDDEN" |
| 96491 | + ");", |
| 96492 | + |
| 96493 | + /* Tables_used() schema */ |
| 96494 | + "CREATE TABLE x(" |
| 96495 | + "type TEXT," |
| 96496 | + "schema TEXT," |
| 96497 | + "name TEXT," |
| 96498 | + "wr INT," |
| 96499 | + "subprog TEXT," |
| 96500 | + "stmt HIDDEN" |
| 96501 | + ");" |
| 96502 | + }; |
| 96503 | + |
| 96504 | + rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]); |
| 96505 | + if( rc==SQLITE_OK ){ |
| 96506 | + pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 96507 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 96508 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 96509 | + memset(pNew, 0, sizeof(*pNew)); |
| 96510 | + pNew->db = db; |
| 96511 | + pNew->bTablesUsed = isTabUsed*2; |
| 96512 | + } |
| 96513 | + return rc; |
| 96514 | +} |
| 96515 | + |
| 96516 | +/* |
| 96517 | +** This method is the destructor for bytecodevtab objects. |
| 96518 | +*/ |
| 96519 | +static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){ |
| 96520 | + bytecodevtab *p = (bytecodevtab*)pVtab; |
| 96521 | + sqlite3_free(p); |
| 96522 | + return SQLITE_OK; |
| 96523 | +} |
| 96524 | + |
| 96525 | +/* |
| 96526 | +** Constructor for a new bytecodevtab_cursor object. |
| 96527 | +*/ |
| 96528 | +static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 96529 | + bytecodevtab *pVTab = (bytecodevtab*)p; |
| 96530 | + bytecodevtab_cursor *pCur; |
| 96531 | + pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 96532 | + if( pCur==0 ) return SQLITE_NOMEM; |
| 96533 | + memset(pCur, 0, sizeof(*pCur)); |
| 96534 | + sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1); |
| 96535 | + *ppCursor = &pCur->base; |
| 96536 | + return SQLITE_OK; |
| 96537 | +} |
| 96538 | + |
| 96539 | +/* |
| 96540 | +** Clear all internal content from a bytecodevtab cursor. |
| 96541 | +*/ |
| 96542 | +static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){ |
| 96543 | + sqlite3_free(pCur->zP4); |
| 96544 | + pCur->zP4 = 0; |
| 96545 | + sqlite3VdbeMemRelease(&pCur->sub); |
| 96546 | + sqlite3VdbeMemSetNull(&pCur->sub); |
| 96547 | + if( pCur->needFinalize ){ |
| 96548 | + sqlite3_finalize(pCur->pStmt); |
| 96549 | + } |
| 96550 | + pCur->pStmt = 0; |
| 96551 | + pCur->needFinalize = 0; |
| 96552 | + pCur->zType = 0; |
| 96553 | + pCur->zSchema = 0; |
| 96554 | + pCur->zName = 0; |
| 96555 | +} |
| 96556 | + |
| 96557 | +/* |
| 96558 | +** Destructor for a bytecodevtab_cursor. |
| 96559 | +*/ |
| 96560 | +static int bytecodevtabClose(sqlite3_vtab_cursor *cur){ |
| 96561 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96562 | + bytecodevtabCursorClear(pCur); |
| 96563 | + sqlite3_free(pCur); |
| 96564 | + return SQLITE_OK; |
| 96565 | +} |
| 96566 | + |
| 96567 | + |
| 96568 | +/* |
| 96569 | +** Advance a bytecodevtab_cursor to its next row of output. |
| 96570 | +*/ |
| 96571 | +static int bytecodevtabNext(sqlite3_vtab_cursor *cur){ |
| 96572 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96573 | + bytecodevtab *pTab = (bytecodevtab*)cur->pVtab; |
| 96574 | + int rc; |
| 96575 | + if( pCur->zP4 ){ |
| 96576 | + sqlite3_free(pCur->zP4); |
| 96577 | + pCur->zP4 = 0; |
| 96578 | + } |
| 96579 | + if( pCur->zName ){ |
| 96580 | + pCur->zName = 0; |
| 96581 | + pCur->zType = 0; |
| 96582 | + pCur->zSchema = 0; |
| 96583 | + } |
| 96584 | + rc = sqlite3VdbeNextOpcode( |
| 96585 | + (Vdbe*)pCur->pStmt, |
| 96586 | + pCur->showSubprograms ? &pCur->sub : 0, |
| 96587 | + pTab->bTablesUsed, |
| 96588 | + &pCur->iRowid, |
| 96589 | + &pCur->iAddr, |
| 96590 | + &pCur->aOp); |
| 96591 | + if( rc!=SQLITE_OK ){ |
| 96592 | + sqlite3VdbeMemSetNull(&pCur->sub); |
| 96593 | + pCur->aOp = 0; |
| 96594 | + } |
| 96595 | + return SQLITE_OK; |
| 96596 | +} |
| 96597 | + |
| 96598 | +/* |
| 96599 | +** Return TRUE if the cursor has been moved off of the last |
| 96600 | +** row of output. |
| 96601 | +*/ |
| 96602 | +static int bytecodevtabEof(sqlite3_vtab_cursor *cur){ |
| 96603 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96604 | + return pCur->aOp==0; |
| 96605 | +} |
| 96606 | + |
| 96607 | +/* |
| 96608 | +** Return values of columns for the row at which the bytecodevtab_cursor |
| 96609 | +** is currently pointing. |
| 96610 | +*/ |
| 96611 | +static int bytecodevtabColumn( |
| 96612 | + sqlite3_vtab_cursor *cur, /* The cursor */ |
| 96613 | + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 96614 | + int i /* Which column to return */ |
| 96615 | +){ |
| 96616 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96617 | + bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab; |
| 96618 | + Op *pOp = pCur->aOp + pCur->iAddr; |
| 96619 | + if( pVTab->bTablesUsed ){ |
| 96620 | + if( i==4 ){ |
| 96621 | + i = 8; |
| 96622 | + }else{ |
| 96623 | + if( i<=2 && pCur->zType==0 ){ |
| 96624 | + Schema *pSchema; |
| 96625 | + HashElem *k; |
| 96626 | + int iDb = pOp->p3; |
| 96627 | + int iRoot = pOp->p2; |
| 96628 | + sqlite3 *db = pVTab->db; |
| 96629 | + pSchema = db->aDb[iDb].pSchema; |
| 96630 | + pCur->zSchema = db->aDb[iDb].zDbSName; |
| 96631 | + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 96632 | + Table *pTab = (Table*)sqliteHashData(k); |
| 96633 | + if( !IsVirtual(pTab) && pTab->tnum==iRoot ){ |
| 96634 | + pCur->zName = pTab->zName; |
| 96635 | + pCur->zType = "table"; |
| 96636 | + break; |
| 96637 | + } |
| 96638 | + } |
| 96639 | + if( pCur->zName==0 ){ |
| 96640 | + for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){ |
| 96641 | + Index *pIdx = (Index*)sqliteHashData(k); |
| 96642 | + if( pIdx->tnum==iRoot ){ |
| 96643 | + pCur->zName = pIdx->zName; |
| 96644 | + pCur->zType = "index"; |
| 96645 | + } |
| 96646 | + } |
| 96647 | + } |
| 96648 | + } |
| 96649 | + i += 10; |
| 96650 | + } |
| 96651 | + } |
| 96652 | + switch( i ){ |
| 96653 | + case 0: /* addr */ |
| 96654 | + sqlite3_result_int(ctx, pCur->iAddr); |
| 96655 | + break; |
| 96656 | + case 1: /* opcode */ |
| 96657 | + sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode), |
| 96658 | + -1, SQLITE_STATIC); |
| 96659 | + break; |
| 96660 | + case 2: /* p1 */ |
| 96661 | + sqlite3_result_int(ctx, pOp->p1); |
| 96662 | + break; |
| 96663 | + case 3: /* p2 */ |
| 96664 | + sqlite3_result_int(ctx, pOp->p2); |
| 96665 | + break; |
| 96666 | + case 4: /* p3 */ |
| 96667 | + sqlite3_result_int(ctx, pOp->p3); |
| 96668 | + break; |
| 96669 | + case 5: /* p4 */ |
| 96670 | + case 7: /* comment */ |
| 96671 | + if( pCur->zP4==0 ){ |
| 96672 | + pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp); |
| 96673 | + } |
| 96674 | + if( i==5 ){ |
| 96675 | + sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC); |
| 96676 | + }else{ |
| 96677 | +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 96678 | + char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4); |
| 96679 | + sqlite3_result_text(ctx, zCom, -1, sqlite3_free); |
| 96680 | +#endif |
| 96681 | + } |
| 96682 | + break; |
| 96683 | + case 6: /* p5 */ |
| 96684 | + sqlite3_result_int(ctx, pOp->p5); |
| 96685 | + break; |
| 96686 | + case 8: { /* subprog */ |
| 96687 | + Op *aOp = pCur->aOp; |
| 96688 | + assert( aOp[0].opcode==OP_Init ); |
| 96689 | + assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 ); |
| 96690 | + if( pCur->iRowid==pCur->iAddr+1 ){ |
| 96691 | + break; /* Result is NULL for the main program */ |
| 96692 | + }else if( aOp[0].p4.z!=0 ){ |
| 96693 | + sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC); |
| 96694 | + }else{ |
| 96695 | + sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC); |
| 96696 | + } |
| 96697 | + break; |
| 96698 | + } |
| 96699 | + case 10: /* tables_used.type */ |
| 96700 | + sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC); |
| 96701 | + break; |
| 96702 | + case 11: /* tables_used.schema */ |
| 96703 | + sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC); |
| 96704 | + break; |
| 96705 | + case 12: /* tables_used.name */ |
| 96706 | + sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC); |
| 96707 | + break; |
| 96708 | + case 13: /* tables_used.wr */ |
| 96709 | + sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite); |
| 96710 | + break; |
| 96711 | + } |
| 96712 | + return SQLITE_OK; |
| 96713 | +} |
| 96714 | + |
| 96715 | +/* |
| 96716 | +** Return the rowid for the current row. In this implementation, the |
| 96717 | +** rowid is the same as the output value. |
| 96718 | +*/ |
| 96719 | +static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 96720 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96721 | + *pRowid = pCur->iRowid; |
| 96722 | + return SQLITE_OK; |
| 96723 | +} |
| 96724 | + |
| 96725 | +/* |
| 96726 | +** Initialize a cursor. |
| 96727 | +** |
| 96728 | +** idxNum==0 means show all subprograms |
| 96729 | +** idxNum==1 means show only the main bytecode and omit subprograms. |
| 96730 | +*/ |
| 96731 | +static int bytecodevtabFilter( |
| 96732 | + sqlite3_vtab_cursor *pVtabCursor, |
| 96733 | + int idxNum, const char *idxStr, |
| 96734 | + int argc, sqlite3_value **argv |
| 96735 | +){ |
| 96736 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor; |
| 96737 | + bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab; |
| 96738 | + int rc = SQLITE_OK; |
| 96739 | + |
| 96740 | + bytecodevtabCursorClear(pCur); |
| 96741 | + pCur->iRowid = 0; |
| 96742 | + pCur->iAddr = 0; |
| 96743 | + pCur->showSubprograms = idxNum==0; |
| 96744 | + assert( argc==1 ); |
| 96745 | + if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){ |
| 96746 | + const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 96747 | + if( zSql==0 ){ |
| 96748 | + rc = SQLITE_NOMEM; |
| 96749 | + }else{ |
| 96750 | + rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0); |
| 96751 | + pCur->needFinalize = 1; |
| 96752 | + } |
| 96753 | + }else{ |
| 96754 | + pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer"); |
| 96755 | + } |
| 96756 | + if( pCur->pStmt==0 ){ |
| 96757 | + pVTab->base.zErrMsg = sqlite3_mprintf( |
| 96758 | + "argument to %s() is not a valid SQL statement", |
| 96759 | + pVTab->bTablesUsed ? "tables_used" : "bytecode" |
| 96760 | + ); |
| 96761 | + rc = SQLITE_ERROR; |
| 96762 | + }else{ |
| 96763 | + bytecodevtabNext(pVtabCursor); |
| 96764 | + } |
| 96765 | + return rc; |
| 96766 | +} |
| 96767 | + |
| 96768 | +/* |
| 96769 | +** We must have a single stmt=? constraint that will be passed through |
| 96770 | +** into the xFilter method. If there is no valid stmt=? constraint, |
| 96771 | +** then return an SQLITE_CONSTRAINT error. |
| 96772 | +*/ |
| 96773 | +static int bytecodevtabBestIndex( |
| 96774 | + sqlite3_vtab *tab, |
| 96775 | + sqlite3_index_info *pIdxInfo |
| 96776 | +){ |
| 96777 | + int i; |
| 96778 | + int rc = SQLITE_CONSTRAINT; |
| 96779 | + struct sqlite3_index_constraint *p; |
| 96780 | + bytecodevtab *pVTab = (bytecodevtab*)tab; |
| 96781 | + int iBaseCol = pVTab->bTablesUsed ? 4 : 8; |
| 96782 | + pIdxInfo->estimatedCost = (double)100; |
| 96783 | + pIdxInfo->estimatedRows = 100; |
| 96784 | + pIdxInfo->idxNum = 0; |
| 96785 | + for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){ |
| 96786 | + if( p->usable==0 ) continue; |
| 96787 | + if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){ |
| 96788 | + rc = SQLITE_OK; |
| 96789 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 96790 | + pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 96791 | + } |
| 96792 | + if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){ |
| 96793 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 96794 | + pIdxInfo->idxNum = 1; |
| 96795 | + } |
| 96796 | + } |
| 96797 | + return rc; |
| 96798 | +} |
| 96799 | + |
| 96800 | +/* |
| 96801 | +** This following structure defines all the methods for the |
| 96802 | +** virtual table. |
| 96803 | +*/ |
| 96804 | +static sqlite3_module bytecodevtabModule = { |
| 96805 | + /* iVersion */ 0, |
| 96806 | + /* xCreate */ 0, |
| 96807 | + /* xConnect */ bytecodevtabConnect, |
| 96808 | + /* xBestIndex */ bytecodevtabBestIndex, |
| 96809 | + /* xDisconnect */ bytecodevtabDisconnect, |
| 96810 | + /* xDestroy */ 0, |
| 96811 | + /* xOpen */ bytecodevtabOpen, |
| 96812 | + /* xClose */ bytecodevtabClose, |
| 96813 | + /* xFilter */ bytecodevtabFilter, |
| 96814 | + /* xNext */ bytecodevtabNext, |
| 96815 | + /* xEof */ bytecodevtabEof, |
| 96816 | + /* xColumn */ bytecodevtabColumn, |
| 96817 | + /* xRowid */ bytecodevtabRowid, |
| 96818 | + /* xUpdate */ 0, |
| 96819 | + /* xBegin */ 0, |
| 96820 | + /* xSync */ 0, |
| 96821 | + /* xCommit */ 0, |
| 96822 | + /* xRollback */ 0, |
| 96823 | + /* xFindMethod */ 0, |
| 96824 | + /* xRename */ 0, |
| 96825 | + /* xSavepoint */ 0, |
| 96826 | + /* xRelease */ 0, |
| 96827 | + /* xRollbackTo */ 0, |
| 96828 | + /* xShadowName */ 0 |
| 96829 | +}; |
| 96830 | + |
| 96831 | + |
| 96832 | +SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ |
| 96833 | + int rc; |
| 96834 | + rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0); |
| 96835 | + if( rc==SQLITE_OK ){ |
| 96836 | + rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db); |
| 96837 | + } |
| 96838 | + return rc; |
| 96839 | +} |
| 96840 | +#elif defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 96841 | +SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; } |
| 96842 | +#endif /* SQLITE_ENABLE_BYTECODE_VTAB */ |
| 96843 | + |
| 96844 | +/************** End of vdbevtab.c ********************************************/ |
| 96098 | 96845 | /************** Begin file memjournal.c **************************************/ |
| 96099 | 96846 | /* |
| 96100 | 96847 | ** 2008 October 7 |
| 96101 | 96848 | ** |
| 96102 | 96849 | ** The author disclaims copyright to this source code. In place of |
| | @@ -96735,10 +97482,47 @@ |
| 96735 | 97482 | } |
| 96736 | 97483 | p = p->pPrior; |
| 96737 | 97484 | }while( p!=0 ); |
| 96738 | 97485 | return WRC_Continue; |
| 96739 | 97486 | } |
| 97487 | + |
| 97488 | +/* Increase the walkerDepth when entering a subquery, and |
| 97489 | +** descrease when leaving the subquery. |
| 97490 | +*/ |
| 97491 | +SQLITE_PRIVATE int sqlite3WalkerDepthIncrease(Walker *pWalker, Select *pSelect){ |
| 97492 | + UNUSED_PARAMETER(pSelect); |
| 97493 | + pWalker->walkerDepth++; |
| 97494 | + return WRC_Continue; |
| 97495 | +} |
| 97496 | +SQLITE_PRIVATE void sqlite3WalkerDepthDecrease(Walker *pWalker, Select *pSelect){ |
| 97497 | + UNUSED_PARAMETER(pSelect); |
| 97498 | + pWalker->walkerDepth--; |
| 97499 | +} |
| 97500 | + |
| 97501 | + |
| 97502 | +/* |
| 97503 | +** No-op routine for the parse-tree walker. |
| 97504 | +** |
| 97505 | +** When this routine is the Walker.xExprCallback then expression trees |
| 97506 | +** are walked without any actions being taken at each node. Presumably, |
| 97507 | +** when this routine is used for Walker.xExprCallback then |
| 97508 | +** Walker.xSelectCallback is set to do something useful for every |
| 97509 | +** subquery in the parser tree. |
| 97510 | +*/ |
| 97511 | +SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 97512 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97513 | + return WRC_Continue; |
| 97514 | +} |
| 97515 | + |
| 97516 | +/* |
| 97517 | +** No-op routine for the parse-tree walker for SELECT statements. |
| 97518 | +** subquery in the parser tree. |
| 97519 | +*/ |
| 97520 | +SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 97521 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 97522 | + return WRC_Continue; |
| 97523 | +} |
| 96740 | 97524 | |
| 96741 | 97525 | /************** End of walker.c **********************************************/ |
| 96742 | 97526 | /************** Begin file resolve.c *****************************************/ |
| 96743 | 97527 | /* |
| 96744 | 97528 | ** 2008 August 18 |
| | @@ -96764,10 +97548,12 @@ |
| 96764 | 97548 | ** This needs to occur when copying a TK_AGG_FUNCTION node from an |
| 96765 | 97549 | ** outer query into an inner subquery. |
| 96766 | 97550 | ** |
| 96767 | 97551 | ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) |
| 96768 | 97552 | ** is a helper function - a callback for the tree walker. |
| 97553 | +** |
| 97554 | +** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c |
| 96769 | 97555 | */ |
| 96770 | 97556 | static int incrAggDepth(Walker *pWalker, Expr *pExpr){ |
| 96771 | 97557 | if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n; |
| 96772 | 97558 | return WRC_Continue; |
| 96773 | 97559 | } |
| | @@ -98739,11 +99525,11 @@ |
| 98739 | 99525 | ** SELECT * FROM t1 WHERE (select a from t1); |
| 98740 | 99526 | */ |
| 98741 | 99527 | SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){ |
| 98742 | 99528 | int op; |
| 98743 | 99529 | while( ExprHasProperty(pExpr, EP_Skip) ){ |
| 98744 | | - assert( pExpr->op==TK_COLLATE ); |
| 99530 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98745 | 99531 | pExpr = pExpr->pLeft; |
| 98746 | 99532 | assert( pExpr!=0 ); |
| 98747 | 99533 | } |
| 98748 | 99534 | op = pExpr->op; |
| 98749 | 99535 | if( op==TK_SELECT ){ |
| | @@ -98806,11 +99592,11 @@ |
| 98806 | 99592 | /* |
| 98807 | 99593 | ** Skip over any TK_COLLATE operators. |
| 98808 | 99594 | */ |
| 98809 | 99595 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){ |
| 98810 | 99596 | while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ |
| 98811 | | - assert( pExpr->op==TK_COLLATE ); |
| 99597 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98812 | 99598 | pExpr = pExpr->pLeft; |
| 98813 | 99599 | } |
| 98814 | 99600 | return pExpr; |
| 98815 | 99601 | } |
| 98816 | 99602 | |
| | @@ -98825,11 +99611,11 @@ |
| 98825 | 99611 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 98826 | 99612 | assert( pExpr->x.pList->nExpr>0 ); |
| 98827 | 99613 | assert( pExpr->op==TK_FUNCTION ); |
| 98828 | 99614 | pExpr = pExpr->x.pList->a[0].pExpr; |
| 98829 | 99615 | }else{ |
| 98830 | | - assert( pExpr->op==TK_COLLATE ); |
| 99616 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98831 | 99617 | pExpr = pExpr->pLeft; |
| 98832 | 99618 | } |
| 98833 | 99619 | } |
| 98834 | 99620 | return pExpr; |
| 98835 | 99621 | } |
| | @@ -102394,10 +103180,17 @@ |
| 102394 | 103180 | } |
| 102395 | 103181 | setDoNotMergeFlagOnCopy(v); |
| 102396 | 103182 | sqlite3VdbeResolveLabel(v, endCoalesce); |
| 102397 | 103183 | break; |
| 102398 | 103184 | } |
| 103185 | + case INLINEFUNC_iif: { |
| 103186 | + Expr caseExpr; |
| 103187 | + memset(&caseExpr, 0, sizeof(caseExpr)); |
| 103188 | + caseExpr.op = TK_CASE; |
| 103189 | + caseExpr.x.pList = pFarg; |
| 103190 | + return sqlite3ExprCodeTarget(pParse, &caseExpr, target); |
| 103191 | + } |
| 102399 | 103192 | |
| 102400 | 103193 | default: { |
| 102401 | 103194 | /* The UNLIKELY() function is a no-op. The result is the value |
| 102402 | 103195 | ** of the first argument. |
| 102403 | 103196 | */ |
| | @@ -102498,11 +103291,14 @@ |
| 102498 | 103291 | op = pExpr->op; |
| 102499 | 103292 | } |
| 102500 | 103293 | switch( op ){ |
| 102501 | 103294 | case TK_AGG_COLUMN: { |
| 102502 | 103295 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 102503 | | - struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 103296 | + struct AggInfo_col *pCol; |
| 103297 | + assert( pAggInfo!=0 ); |
| 103298 | + assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 103299 | + pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 102504 | 103300 | if( !pAggInfo->directMode ){ |
| 102505 | 103301 | assert( pCol->iMem>0 ); |
| 102506 | 103302 | return pCol->iMem; |
| 102507 | 103303 | }else if( pAggInfo->useSortingIdx ){ |
| 102508 | 103304 | Table *pTab = pCol->pTab; |
| | @@ -102798,11 +103594,14 @@ |
| 102798 | 103594 | sqlite3VdbeJumpHere(v, addr); |
| 102799 | 103595 | break; |
| 102800 | 103596 | } |
| 102801 | 103597 | case TK_AGG_FUNCTION: { |
| 102802 | 103598 | AggInfo *pInfo = pExpr->pAggInfo; |
| 102803 | | - if( pInfo==0 ){ |
| 103599 | + if( pInfo==0 |
| 103600 | + || NEVER(pExpr->iAgg<0) |
| 103601 | + || NEVER(pExpr->iAgg>=pInfo->nFunc) |
| 103602 | + ){ |
| 102804 | 103603 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 102805 | 103604 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
| 102806 | 103605 | }else{ |
| 102807 | 103606 | return pInfo->aFunc[pExpr->iAgg].iMem; |
| 102808 | 103607 | } |
| | @@ -103176,11 +103975,11 @@ |
| 103176 | 103975 | assert( pExpr->affExpr==OE_Rollback |
| 103177 | 103976 | || pExpr->affExpr==OE_Abort |
| 103178 | 103977 | || pExpr->affExpr==OE_Fail |
| 103179 | 103978 | || pExpr->affExpr==OE_Ignore |
| 103180 | 103979 | ); |
| 103181 | | - if( !pParse->pTriggerTab ){ |
| 103980 | + if( !pParse->pTriggerTab && !pParse->nested ){ |
| 103182 | 103981 | sqlite3ErrorMsg(pParse, |
| 103183 | 103982 | "RAISE() may only be used within a trigger-program"); |
| 103184 | 103983 | return 0; |
| 103185 | 103984 | } |
| 103186 | 103985 | if( pExpr->affExpr==OE_Abort ){ |
| | @@ -103190,12 +103989,13 @@ |
| 103190 | 103989 | if( pExpr->affExpr==OE_Ignore ){ |
| 103191 | 103990 | sqlite3VdbeAddOp4( |
| 103192 | 103991 | v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
| 103193 | 103992 | VdbeCoverage(v); |
| 103194 | 103993 | }else{ |
| 103195 | | - sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
| 103196 | | - pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103994 | + sqlite3HaltConstraint(pParse, |
| 103995 | + pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, |
| 103996 | + pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103197 | 103997 | } |
| 103198 | 103998 | |
| 103199 | 103999 | break; |
| 103200 | 104000 | } |
| 103201 | 104001 | #endif |
| | @@ -104553,19 +105353,10 @@ |
| 104553 | 105353 | } |
| 104554 | 105354 | } |
| 104555 | 105355 | } |
| 104556 | 105356 | return WRC_Continue; |
| 104557 | 105357 | } |
| 104558 | | -static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
| 104559 | | - UNUSED_PARAMETER(pSelect); |
| 104560 | | - pWalker->walkerDepth++; |
| 104561 | | - return WRC_Continue; |
| 104562 | | -} |
| 104563 | | -static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){ |
| 104564 | | - UNUSED_PARAMETER(pSelect); |
| 104565 | | - pWalker->walkerDepth--; |
| 104566 | | -} |
| 104567 | 105358 | |
| 104568 | 105359 | /* |
| 104569 | 105360 | ** Analyze the pExpr expression looking for aggregate functions and |
| 104570 | 105361 | ** for variables that need to be added to AggInfo object that pNC->pAggInfo |
| 104571 | 105362 | ** points to. Additional entries are made on the AggInfo object as |
| | @@ -104575,12 +105366,12 @@ |
| 104575 | 105366 | ** analyzed by sqlite3ResolveExprNames(). |
| 104576 | 105367 | */ |
| 104577 | 105368 | SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
| 104578 | 105369 | Walker w; |
| 104579 | 105370 | w.xExprCallback = analyzeAggregate; |
| 104580 | | - w.xSelectCallback = analyzeAggregatesInSelect; |
| 104581 | | - w.xSelectCallback2 = analyzeAggregatesInSelectEnd; |
| 105371 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 105372 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 104582 | 105373 | w.walkerDepth = 0; |
| 104583 | 105374 | w.u.pNC = pNC; |
| 104584 | 105375 | w.pParse = 0; |
| 104585 | 105376 | assert( pNC->pSrcList!=0 ); |
| 104586 | 105377 | sqlite3WalkExpr(&w, pExpr); |
| | @@ -104815,11 +105606,14 @@ |
| 104815 | 105606 | if( !zName ) goto exit_rename_table; |
| 104816 | 105607 | |
| 104817 | 105608 | /* Check that a table or index named 'zName' does not already exist |
| 104818 | 105609 | ** in database iDb. If so, this is an error. |
| 104819 | 105610 | */ |
| 104820 | | - if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
| 105611 | + if( sqlite3FindTable(db, zName, zDb) |
| 105612 | + || sqlite3FindIndex(db, zName, zDb) |
| 105613 | + || sqlite3IsShadowTableOf(db, pTab, zName) |
| 105614 | + ){ |
| 104821 | 105615 | sqlite3ErrorMsg(pParse, |
| 104822 | 105616 | "there is already another table or index with this name: %s", zName); |
| 104823 | 105617 | goto exit_rename_table; |
| 104824 | 105618 | } |
| 104825 | 105619 | |
| | @@ -104946,10 +105740,26 @@ |
| 104946 | 105740 | exit_rename_table: |
| 104947 | 105741 | sqlite3SrcListDelete(db, pSrc); |
| 104948 | 105742 | sqlite3DbFree(db, zName); |
| 104949 | 105743 | db->mDbFlags = savedDbFlags; |
| 104950 | 105744 | } |
| 105745 | + |
| 105746 | +/* |
| 105747 | +** Write code that will raise an error if the table described by |
| 105748 | +** zDb and zTab is not empty. |
| 105749 | +*/ |
| 105750 | +static void sqlite3ErrorIfNotEmpty( |
| 105751 | + Parse *pParse, /* Parsing context */ |
| 105752 | + const char *zDb, /* Schema holding the table */ |
| 105753 | + const char *zTab, /* Table to check for empty */ |
| 105754 | + const char *zErr /* Error message text */ |
| 105755 | +){ |
| 105756 | + sqlite3NestedParse(pParse, |
| 105757 | + "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", |
| 105758 | + zErr, zDb, zTab |
| 105759 | + ); |
| 105760 | +} |
| 104951 | 105761 | |
| 104952 | 105762 | /* |
| 104953 | 105763 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 104954 | 105764 | ** has been parsed. Argument pColDef contains the text of the new |
| 104955 | 105765 | ** column definition. |
| | @@ -104999,11 +105809,12 @@ |
| 104999 | 105809 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 105000 | 105810 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 105001 | 105811 | return; |
| 105002 | 105812 | } |
| 105003 | 105813 | if( pNew->pIndex ){ |
| 105004 | | - sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 105814 | + sqlite3ErrorMsg(pParse, |
| 105815 | + "Cannot add a UNIQUE column"); |
| 105005 | 105816 | return; |
| 105006 | 105817 | } |
| 105007 | 105818 | if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ |
| 105008 | 105819 | /* If the default value for the new column was specified with a |
| 105009 | 105820 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| | @@ -105012,19 +105823,18 @@ |
| 105012 | 105823 | assert( pDflt==0 || pDflt->op==TK_SPAN ); |
| 105013 | 105824 | if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
| 105014 | 105825 | pDflt = 0; |
| 105015 | 105826 | } |
| 105016 | 105827 | if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 105017 | | - sqlite3ErrorMsg(pParse, |
| 105828 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105018 | 105829 | "Cannot add a REFERENCES column with non-NULL default value"); |
| 105019 | | - return; |
| 105020 | 105830 | } |
| 105021 | 105831 | if( pCol->notNull && !pDflt ){ |
| 105022 | | - sqlite3ErrorMsg(pParse, |
| 105832 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105023 | 105833 | "Cannot add a NOT NULL column with default value NULL"); |
| 105024 | | - return; |
| 105025 | 105834 | } |
| 105835 | + |
| 105026 | 105836 | |
| 105027 | 105837 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 105028 | 105838 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 105029 | 105839 | */ |
| 105030 | 105840 | if( pDflt ){ |
| | @@ -105035,18 +105845,17 @@ |
| 105035 | 105845 | if( rc!=SQLITE_OK ){ |
| 105036 | 105846 | assert( db->mallocFailed == 1 ); |
| 105037 | 105847 | return; |
| 105038 | 105848 | } |
| 105039 | 105849 | if( !pVal ){ |
| 105040 | | - sqlite3ErrorMsg(pParse,"Cannot add a column with non-constant default"); |
| 105041 | | - return; |
| 105850 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105851 | + "Cannot add a column with non-constant default"); |
| 105042 | 105852 | } |
| 105043 | 105853 | sqlite3ValueFree(pVal); |
| 105044 | 105854 | } |
| 105045 | 105855 | }else if( pCol->colFlags & COLFLAG_STORED ){ |
| 105046 | | - sqlite3ErrorMsg(pParse, "cannot add a STORED column"); |
| 105047 | | - return; |
| 105856 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); |
| 105048 | 105857 | } |
| 105049 | 105858 | |
| 105050 | 105859 | |
| 105051 | 105860 | /* Modify the CREATE TABLE statement. */ |
| 105052 | 105861 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
| | @@ -106605,10 +107414,15 @@ |
| 106605 | 107414 | sqlite3 *db = pParse->db; |
| 106606 | 107415 | Db *pDb; |
| 106607 | 107416 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 106608 | 107417 | int aRoot[ArraySize(aTable)]; |
| 106609 | 107418 | u8 aCreateTbl[ArraySize(aTable)]; |
| 107419 | +#ifdef SQLITE_ENABLE_STAT4 |
| 107420 | + const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1; |
| 107421 | +#else |
| 107422 | + const int nToOpen = 1; |
| 107423 | +#endif |
| 106610 | 107424 | |
| 106611 | 107425 | if( v==0 ) return; |
| 106612 | 107426 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 106613 | 107427 | assert( sqlite3VdbeDb(v)==db ); |
| 106614 | 107428 | pDb = &db->aDb[iDb]; |
| | @@ -106617,12 +107431,13 @@ |
| 106617 | 107431 | ** if they do already exist. |
| 106618 | 107432 | */ |
| 106619 | 107433 | for(i=0; i<ArraySize(aTable); i++){ |
| 106620 | 107434 | const char *zTab = aTable[i].zName; |
| 106621 | 107435 | Table *pStat; |
| 107436 | + aCreateTbl[i] = 0; |
| 106622 | 107437 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){ |
| 106623 | | - if( aTable[i].zCols ){ |
| 107438 | + if( i<nToOpen ){ |
| 106624 | 107439 | /* The sqlite_statN table does not exist. Create it. Note that a |
| 106625 | 107440 | ** side-effect of the CREATE TABLE statement is to leave the rootpage |
| 106626 | 107441 | ** of the new table in register pParse->regRoot. This is important |
| 106627 | 107442 | ** because the OpenWrite opcode below will be needing it. */ |
| 106628 | 107443 | sqlite3NestedParse(pParse, |
| | @@ -106634,11 +107449,10 @@ |
| 106634 | 107449 | }else{ |
| 106635 | 107450 | /* The table already exists. If zWhere is not NULL, delete all entries |
| 106636 | 107451 | ** associated with the table zWhere. If zWhere is NULL, delete the |
| 106637 | 107452 | ** entire contents of the table. */ |
| 106638 | 107453 | aRoot[i] = pStat->tnum; |
| 106639 | | - aCreateTbl[i] = 0; |
| 106640 | 107454 | sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); |
| 106641 | 107455 | if( zWhere ){ |
| 106642 | 107456 | sqlite3NestedParse(pParse, |
| 106643 | 107457 | "DELETE FROM %Q.%s WHERE %s=%Q", |
| 106644 | 107458 | pDb->zDbSName, zTab, zWhereType, zWhere |
| | @@ -106653,11 +107467,11 @@ |
| 106653 | 107467 | } |
| 106654 | 107468 | } |
| 106655 | 107469 | } |
| 106656 | 107470 | |
| 106657 | 107471 | /* Open the sqlite_stat[134] tables for writing. */ |
| 106658 | | - for(i=0; aTable[i].zCols; i++){ |
| 107472 | + for(i=0; i<nToOpen; i++){ |
| 106659 | 107473 | assert( i<ArraySize(aTable) ); |
| 106660 | 107474 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3); |
| 106661 | 107475 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 106662 | 107476 | VdbeComment((v, aTable[i].zName)); |
| 106663 | 107477 | } |
| | @@ -106692,13 +107506,16 @@ |
| 106692 | 107506 | u32 iHash; /* Tiebreaker hash */ |
| 106693 | 107507 | #endif |
| 106694 | 107508 | }; |
| 106695 | 107509 | struct StatAccum { |
| 106696 | 107510 | sqlite3 *db; /* Database connection, for malloc() */ |
| 106697 | | - tRowcnt nRow; /* Number of rows in the entire table */ |
| 107511 | + tRowcnt nEst; /* Estimated number of rows */ |
| 107512 | + tRowcnt nRow; /* Number of rows visited so far */ |
| 107513 | + int nLimit; /* Analysis row-scan limit */ |
| 106698 | 107514 | int nCol; /* Number of columns in index + pk/rowid */ |
| 106699 | 107515 | int nKeyCol; /* Number of index columns w/o the pk/rowid */ |
| 107516 | + u8 nSkipAhead; /* Number of times of skip-ahead */ |
| 106700 | 107517 | StatSample current; /* Current row as a StatSample */ |
| 106701 | 107518 | #ifdef SQLITE_ENABLE_STAT4 |
| 106702 | 107519 | tRowcnt nPSample; /* How often to do a periodic sample */ |
| 106703 | 107520 | int mxSample; /* Maximum number of samples to accumulate */ |
| 106704 | 107521 | u32 iPrn; /* Pseudo-random number used for sampling */ |
| | @@ -106774,31 +107591,32 @@ |
| 106774 | 107591 | ** Reclaim all memory of a StatAccum structure. |
| 106775 | 107592 | */ |
| 106776 | 107593 | static void statAccumDestructor(void *pOld){ |
| 106777 | 107594 | StatAccum *p = (StatAccum*)pOld; |
| 106778 | 107595 | #ifdef SQLITE_ENABLE_STAT4 |
| 106779 | | - int i; |
| 106780 | | - for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i); |
| 106781 | | - for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i); |
| 106782 | | - sampleClear(p->db, &p->current); |
| 107596 | + if( p->mxSample ){ |
| 107597 | + int i; |
| 107598 | + for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i); |
| 107599 | + for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i); |
| 107600 | + sampleClear(p->db, &p->current); |
| 107601 | + } |
| 106783 | 107602 | #endif |
| 106784 | 107603 | sqlite3DbFree(p->db, p); |
| 106785 | 107604 | } |
| 106786 | 107605 | |
| 106787 | 107606 | /* |
| 106788 | | -** Implementation of the stat_init(N,K,C) SQL function. The three parameters |
| 107607 | +** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters |
| 106789 | 107608 | ** are: |
| 106790 | 107609 | ** N: The number of columns in the index including the rowid/pk (note 1) |
| 106791 | 107610 | ** K: The number of columns in the index excluding the rowid/pk. |
| 106792 | | -** C: The number of rows in the index (note 2) |
| 107611 | +** C: Estimated number of rows in the index |
| 107612 | +** L: A limit on the number of rows to scan, or 0 for no-limit |
| 106793 | 107613 | ** |
| 106794 | 107614 | ** Note 1: In the special case of the covering index that implements a |
| 106795 | 107615 | ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the |
| 106796 | 107616 | ** total number of columns in the table. |
| 106797 | 107617 | ** |
| 106798 | | -** Note 2: C is only used for STAT4. |
| 106799 | | -** |
| 106800 | 107618 | ** For indexes on ordinary rowid tables, N==K+1. But for indexes on |
| 106801 | 107619 | ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the |
| 106802 | 107620 | ** PRIMARY KEY of the table. The covering index that implements the |
| 106803 | 107621 | ** original WITHOUT ROWID table as N==K as a special case. |
| 106804 | 107622 | ** |
| | @@ -106815,13 +107633,14 @@ |
| 106815 | 107633 | StatAccum *p; |
| 106816 | 107634 | int nCol; /* Number of columns in index being sampled */ |
| 106817 | 107635 | int nKeyCol; /* Number of key columns */ |
| 106818 | 107636 | int nColUp; /* nCol rounded up for alignment */ |
| 106819 | 107637 | int n; /* Bytes of space to allocate */ |
| 106820 | | - sqlite3 *db; /* Database connection */ |
| 107638 | + sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */ |
| 106821 | 107639 | #ifdef SQLITE_ENABLE_STAT4 |
| 106822 | | - int mxSample = SQLITE_STAT4_SAMPLES; |
| 107640 | + /* Maximum number of samples. 0 if STAT4 data is not collected */ |
| 107641 | + int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0; |
| 106823 | 107642 | #endif |
| 106824 | 107643 | |
| 106825 | 107644 | /* Decode the three function arguments */ |
| 106826 | 107645 | UNUSED_PARAMETER(argc); |
| 106827 | 107646 | nCol = sqlite3_value_int(argv[0]); |
| | @@ -106832,39 +107651,43 @@ |
| 106832 | 107651 | assert( nKeyCol>0 ); |
| 106833 | 107652 | |
| 106834 | 107653 | /* Allocate the space required for the StatAccum object */ |
| 106835 | 107654 | n = sizeof(*p) |
| 106836 | 107655 | + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */ |
| 106837 | | - + sizeof(tRowcnt)*nColUp /* StatAccum.anDLt */ |
| 107656 | + + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */ |
| 106838 | 107657 | #ifdef SQLITE_ENABLE_STAT4 |
| 106839 | | - + sizeof(tRowcnt)*nColUp /* StatAccum.anLt */ |
| 106840 | | - + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */ |
| 106841 | | - + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample) |
| 107658 | + if( mxSample ){ |
| 107659 | + n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */ |
| 107660 | + + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */ |
| 107661 | + + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample); |
| 107662 | + } |
| 106842 | 107663 | #endif |
| 106843 | | - ; |
| 106844 | 107664 | db = sqlite3_context_db_handle(context); |
| 106845 | 107665 | p = sqlite3DbMallocZero(db, n); |
| 106846 | 107666 | if( p==0 ){ |
| 106847 | 107667 | sqlite3_result_error_nomem(context); |
| 106848 | 107668 | return; |
| 106849 | 107669 | } |
| 106850 | 107670 | |
| 106851 | 107671 | p->db = db; |
| 107672 | + p->nEst = sqlite3_value_int64(argv[2]); |
| 106852 | 107673 | p->nRow = 0; |
| 107674 | + p->nLimit = sqlite3_value_int64(argv[3]); |
| 106853 | 107675 | p->nCol = nCol; |
| 106854 | 107676 | p->nKeyCol = nKeyCol; |
| 107677 | + p->nSkipAhead = 0; |
| 106855 | 107678 | p->current.anDLt = (tRowcnt*)&p[1]; |
| 106856 | 107679 | p->current.anEq = &p->current.anDLt[nColUp]; |
| 106857 | 107680 | |
| 106858 | 107681 | #ifdef SQLITE_ENABLE_STAT4 |
| 106859 | | - { |
| 107682 | + p->mxSample = p->nLimit==0 ? mxSample : 0; |
| 107683 | + if( mxSample ){ |
| 106860 | 107684 | u8 *pSpace; /* Allocated space not yet assigned */ |
| 106861 | 107685 | int i; /* Used to iterate through p->aSample[] */ |
| 106862 | 107686 | |
| 106863 | 107687 | p->iGet = -1; |
| 106864 | | - p->mxSample = mxSample; |
| 106865 | | - p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); |
| 107688 | + p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1); |
| 106866 | 107689 | p->current.anLt = &p->current.anEq[nColUp]; |
| 106867 | 107690 | p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); |
| 106868 | 107691 | |
| 106869 | 107692 | /* Set up the StatAccum.a[] and aBest[] arrays */ |
| 106870 | 107693 | p->a = (struct StatSample*)&p->current.anLt[nColUp]; |
| | @@ -106888,11 +107711,11 @@ |
| 106888 | 107711 | ** (given by the 3rd parameter) is never used and can be any positive |
| 106889 | 107712 | ** value. */ |
| 106890 | 107713 | sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor); |
| 106891 | 107714 | } |
| 106892 | 107715 | static const FuncDef statInitFuncdef = { |
| 106893 | | - 2+IsStat4, /* nArg */ |
| 107716 | + 4, /* nArg */ |
| 106894 | 107717 | SQLITE_UTF8, /* funcFlags */ |
| 106895 | 107718 | 0, /* pUserData */ |
| 106896 | 107719 | 0, /* pNext */ |
| 106897 | 107720 | statInit, /* xSFunc */ |
| 106898 | 107721 | 0, /* xFinalize */ |
| | @@ -107092,14 +107915,17 @@ |
| 107092 | 107915 | ** P Pointer to the StatAccum object created by stat_init() |
| 107093 | 107916 | ** C Index of left-most column to differ from previous row |
| 107094 | 107917 | ** R Rowid for the current row. Might be a key record for |
| 107095 | 107918 | ** WITHOUT ROWID tables. |
| 107096 | 107919 | ** |
| 107097 | | -** This SQL function always returns NULL. It's purpose it to accumulate |
| 107098 | | -** statistical data and/or samples in the StatAccum object about the |
| 107099 | | -** index being analyzed. The stat_get() SQL function will later be used to |
| 107100 | | -** extract relevant information for constructing the sqlite_statN tables. |
| 107920 | +** The purpose of this routine is to collect statistical data and/or |
| 107921 | +** samples from the index being analyzed into the StatAccum object. |
| 107922 | +** The stat_get() SQL function will be used afterwards to |
| 107923 | +** retrieve the information gathered. |
| 107924 | +** |
| 107925 | +** This SQL function usually returns NULL, but might return an integer |
| 107926 | +** if it wants the byte-code to do special processing. |
| 107101 | 107927 | ** |
| 107102 | 107928 | ** The R parameter is only used for STAT4 |
| 107103 | 107929 | */ |
| 107104 | 107930 | static void statPush( |
| 107105 | 107931 | sqlite3_context *context, |
| | @@ -107121,11 +107947,11 @@ |
| 107121 | 107947 | /* This is the first call to this function. Do initialization. */ |
| 107122 | 107948 | for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1; |
| 107123 | 107949 | }else{ |
| 107124 | 107950 | /* Second and subsequent calls get processed here */ |
| 107125 | 107951 | #ifdef SQLITE_ENABLE_STAT4 |
| 107126 | | - samplePushPrevious(p, iChng); |
| 107952 | + if( p->mxSample ) samplePushPrevious(p, iChng); |
| 107127 | 107953 | #endif |
| 107128 | 107954 | |
| 107129 | 107955 | /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply |
| 107130 | 107956 | ** to the current row of the index. */ |
| 107131 | 107957 | for(i=0; i<iChng; i++){ |
| | @@ -107132,30 +107958,29 @@ |
| 107132 | 107958 | p->current.anEq[i]++; |
| 107133 | 107959 | } |
| 107134 | 107960 | for(i=iChng; i<p->nCol; i++){ |
| 107135 | 107961 | p->current.anDLt[i]++; |
| 107136 | 107962 | #ifdef SQLITE_ENABLE_STAT4 |
| 107137 | | - p->current.anLt[i] += p->current.anEq[i]; |
| 107963 | + if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i]; |
| 107138 | 107964 | #endif |
| 107139 | 107965 | p->current.anEq[i] = 1; |
| 107140 | 107966 | } |
| 107141 | 107967 | } |
| 107968 | + |
| 107142 | 107969 | p->nRow++; |
| 107143 | 107970 | #ifdef SQLITE_ENABLE_STAT4 |
| 107144 | | - if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ |
| 107145 | | - sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); |
| 107146 | | - }else{ |
| 107147 | | - sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), |
| 107148 | | - sqlite3_value_blob(argv[2])); |
| 107149 | | - } |
| 107150 | | - p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; |
| 107151 | | -#endif |
| 107152 | | - |
| 107153 | | -#ifdef SQLITE_ENABLE_STAT4 |
| 107154 | | - { |
| 107155 | | - tRowcnt nLt = p->current.anLt[p->nCol-1]; |
| 107156 | | - |
| 107971 | + if( p->mxSample ){ |
| 107972 | + tRowcnt nLt; |
| 107973 | + if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ |
| 107974 | + sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); |
| 107975 | + }else{ |
| 107976 | + sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), |
| 107977 | + sqlite3_value_blob(argv[2])); |
| 107978 | + } |
| 107979 | + p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; |
| 107980 | + |
| 107981 | + nLt = p->current.anLt[p->nCol-1]; |
| 107157 | 107982 | /* Check if this is to be a periodic sample. If so, add it. */ |
| 107158 | 107983 | if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ |
| 107159 | 107984 | p->current.isPSample = 1; |
| 107160 | 107985 | p->current.iCol = 0; |
| 107161 | 107986 | sampleInsert(p, &p->current, p->nCol-1); |
| | @@ -107167,13 +107992,18 @@ |
| 107167 | 107992 | p->current.iCol = i; |
| 107168 | 107993 | if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){ |
| 107169 | 107994 | sampleCopy(p, &p->aBest[i], &p->current); |
| 107170 | 107995 | } |
| 107171 | 107996 | } |
| 107997 | + }else |
| 107998 | +#endif |
| 107999 | + if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){ |
| 108000 | + p->nSkipAhead++; |
| 108001 | + sqlite3_result_int(context, p->current.anDLt[0]>0); |
| 107172 | 108002 | } |
| 107173 | | -#endif |
| 107174 | 108003 | } |
| 108004 | + |
| 107175 | 108005 | static const FuncDef statPushFuncdef = { |
| 107176 | 108006 | 2+IsStat4, /* nArg */ |
| 107177 | 108007 | SQLITE_UTF8, /* funcFlags */ |
| 107178 | 108008 | 0, /* pUserData */ |
| 107179 | 108009 | 0, /* pNext */ |
| | @@ -107221,10 +108051,11 @@ |
| 107221 | 108051 | assert( argc==2 ); |
| 107222 | 108052 | assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ |
| 107223 | 108053 | || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT |
| 107224 | 108054 | || eCall==STAT_GET_NDLT |
| 107225 | 108055 | ); |
| 108056 | + assert( eCall==STAT_GET_STAT1 || p->mxSample ); |
| 107226 | 108057 | if( eCall==STAT_GET_STAT1 ) |
| 107227 | 108058 | #else |
| 107228 | 108059 | assert( argc==1 ); |
| 107229 | 108060 | #endif |
| 107230 | 108061 | { |
| | @@ -107256,11 +108087,12 @@ |
| 107256 | 108087 | if( zRet==0 ){ |
| 107257 | 108088 | sqlite3_result_error_nomem(context); |
| 107258 | 108089 | return; |
| 107259 | 108090 | } |
| 107260 | 108091 | |
| 107261 | | - sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); |
| 108092 | + sqlite3_snprintf(24, zRet, "%llu", |
| 108093 | + p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow); |
| 107262 | 108094 | z = zRet + sqlite3Strlen30(zRet); |
| 107263 | 108095 | for(i=0; i<p->nKeyCol; i++){ |
| 107264 | 108096 | u64 nDistinct = p->current.anDLt[i] + 1; |
| 107265 | 108097 | u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; |
| 107266 | 108098 | sqlite3_snprintf(24, z, " %llu", iVal); |
| | @@ -107332,20 +108164,20 @@ |
| 107332 | 108164 | 0, 0, /* xValue, xInverse */ |
| 107333 | 108165 | "stat_get", /* zName */ |
| 107334 | 108166 | {0} |
| 107335 | 108167 | }; |
| 107336 | 108168 | |
| 107337 | | -static void callStatGet(Parse *pParse, int regStat4, int iParam, int regOut){ |
| 108169 | +static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){ |
| 107338 | 108170 | #ifdef SQLITE_ENABLE_STAT4 |
| 107339 | | - sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat4+1); |
| 108171 | + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1); |
| 107340 | 108172 | #elif SQLITE_DEBUG |
| 107341 | 108173 | assert( iParam==STAT_GET_STAT1 ); |
| 107342 | 108174 | #else |
| 107343 | 108175 | UNUSED_PARAMETER( iParam ); |
| 107344 | 108176 | #endif |
| 107345 | | - assert( regOut!=regStat4 && regOut!=regStat4+1 ); |
| 107346 | | - sqlite3VdbeAddFunctionCall(pParse, 0, regStat4, regOut, 1+IsStat4, |
| 108177 | + assert( regOut!=regStat && regOut!=regStat+1 ); |
| 108178 | + sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat4, |
| 107347 | 108179 | &statGetFuncdef, 0); |
| 107348 | 108180 | } |
| 107349 | 108181 | |
| 107350 | 108182 | /* |
| 107351 | 108183 | ** Generate code to do an analysis of all indices associated with |
| | @@ -107367,16 +108199,15 @@ |
| 107367 | 108199 | int i; /* Loop counter */ |
| 107368 | 108200 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 107369 | 108201 | int iDb; /* Index of database containing pTab */ |
| 107370 | 108202 | u8 needTableCnt = 1; /* True to count the table */ |
| 107371 | 108203 | int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 107372 | | - int regStat4 = iMem++; /* Register to hold StatAccum object */ |
| 108204 | + int regStat = iMem++; /* Register to hold StatAccum object */ |
| 107373 | 108205 | int regChng = iMem++; /* Index of changed index field */ |
| 107374 | | -#ifdef SQLITE_ENABLE_STAT4 |
| 107375 | 108206 | int regRowid = iMem++; /* Rowid argument passed to stat_push() */ |
| 107376 | | -#endif |
| 107377 | 108207 | int regTemp = iMem++; /* Temporary use register */ |
| 108208 | + int regTemp2 = iMem++; /* Second temporary use register */ |
| 107378 | 108209 | int regTabname = iMem++; /* Register containing table name */ |
| 107379 | 108210 | int regIdxname = iMem++; /* Register containing index name */ |
| 107380 | 108211 | int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ |
| 107381 | 108212 | int regPrev = iMem; /* MUST BE LAST (see below) */ |
| 107382 | 108213 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| | @@ -107500,21 +108331,30 @@ |
| 107500 | 108331 | /* Invoke the stat_init() function. The arguments are: |
| 107501 | 108332 | ** |
| 107502 | 108333 | ** (1) the number of columns in the index including the rowid |
| 107503 | 108334 | ** (or for a WITHOUT ROWID table, the number of PK columns), |
| 107504 | 108335 | ** (2) the number of columns in the key without the rowid/pk |
| 107505 | | - ** (3) the number of rows in the index, |
| 107506 | | - ** |
| 107507 | | - ** |
| 107508 | | - ** The third argument is only used for STAT4 |
| 108336 | + ** (3) estimated number of rows in the index, |
| 107509 | 108337 | */ |
| 108338 | + sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1); |
| 108339 | + assert( regRowid==regStat+2 ); |
| 108340 | + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid); |
| 107510 | 108341 | #ifdef SQLITE_ENABLE_STAT4 |
| 107511 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); |
| 108342 | + if( OptimizationEnabled(db, SQLITE_Stat4) ){ |
| 108343 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp); |
| 108344 | + addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 108345 | + VdbeCoverage(v); |
| 108346 | + }else |
| 107512 | 108347 | #endif |
| 107513 | | - sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1); |
| 107514 | | - sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2); |
| 107515 | | - sqlite3VdbeAddFunctionCall(pParse, 0, regStat4+1, regStat4, 2+IsStat4, |
| 108348 | + { |
| 108349 | + addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 108350 | + VdbeCoverage(v); |
| 108351 | + sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, 1); |
| 108352 | + } |
| 108353 | + assert( regTemp2==regStat+4 ); |
| 108354 | + sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2); |
| 108355 | + sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4, |
| 107516 | 108356 | &statInitFuncdef, 0); |
| 107517 | 108357 | |
| 107518 | 108358 | /* Implementation of the following: |
| 107519 | 108359 | ** |
| 107520 | 108360 | ** Rewind csr |
| | @@ -107521,12 +108361,10 @@ |
| 107521 | 108361 | ** if eof(csr) goto end_of_scan; |
| 107522 | 108362 | ** regChng = 0 |
| 107523 | 108363 | ** goto next_push_0; |
| 107524 | 108364 | ** |
| 107525 | 108365 | */ |
| 107526 | | - addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 107527 | | - VdbeCoverage(v); |
| 107528 | 108366 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); |
| 107529 | 108367 | addrNextRow = sqlite3VdbeCurrentAddr(v); |
| 107530 | 108368 | |
| 107531 | 108369 | if( nColTest>0 ){ |
| 107532 | 108370 | int endDistinctTest = sqlite3VdbeMakeLabel(pParse); |
| | @@ -107555,10 +108393,11 @@ |
| 107555 | 108393 | } |
| 107556 | 108394 | for(i=0; i<nColTest; i++){ |
| 107557 | 108395 | char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 107558 | 108396 | sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); |
| 107559 | 108397 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); |
| 108398 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 107560 | 108399 | aGotoChng[i] = |
| 107561 | 108400 | sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); |
| 107562 | 108401 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 107563 | 108402 | VdbeCoverage(v); |
| 107564 | 108403 | } |
| | @@ -107575,10 +108414,11 @@ |
| 107575 | 108414 | */ |
| 107576 | 108415 | sqlite3VdbeJumpHere(v, addrNextRow-1); |
| 107577 | 108416 | for(i=0; i<nColTest; i++){ |
| 107578 | 108417 | sqlite3VdbeJumpHere(v, aGotoChng[i]); |
| 107579 | 108418 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i); |
| 108419 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 107580 | 108420 | } |
| 107581 | 108421 | sqlite3VdbeResolveLabel(v, endDistinctTest); |
| 107582 | 108422 | sqlite3DbFree(db, aGotoChng); |
| 107583 | 108423 | } |
| 107584 | 108424 | |
| | @@ -107588,34 +108428,50 @@ |
| 107588 | 108428 | ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only |
| 107589 | 108429 | ** Next csr |
| 107590 | 108430 | ** if !eof(csr) goto next_row; |
| 107591 | 108431 | */ |
| 107592 | 108432 | #ifdef SQLITE_ENABLE_STAT4 |
| 107593 | | - assert( regRowid==(regStat4+2) ); |
| 107594 | | - if( HasRowid(pTab) ){ |
| 107595 | | - sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); |
| 107596 | | - }else{ |
| 107597 | | - Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 107598 | | - int j, k, regKey; |
| 107599 | | - regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 107600 | | - for(j=0; j<pPk->nKeyCol; j++){ |
| 107601 | | - k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); |
| 107602 | | - assert( k>=0 && k<pIdx->nColumn ); |
| 107603 | | - sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); |
| 107604 | | - VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName)); |
| 107605 | | - } |
| 107606 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
| 107607 | | - sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
| 108433 | + if( OptimizationEnabled(db, SQLITE_Stat4) ){ |
| 108434 | + assert( regRowid==(regStat+2) ); |
| 108435 | + if( HasRowid(pTab) ){ |
| 108436 | + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); |
| 108437 | + }else{ |
| 108438 | + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 108439 | + int j, k, regKey; |
| 108440 | + regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 108441 | + for(j=0; j<pPk->nKeyCol; j++){ |
| 108442 | + k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); |
| 108443 | + assert( k>=0 && k<pIdx->nColumn ); |
| 108444 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); |
| 108445 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 108446 | + } |
| 108447 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
| 108448 | + sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
| 108449 | + } |
| 107608 | 108450 | } |
| 107609 | 108451 | #endif |
| 107610 | | - assert( regChng==(regStat4+1) ); |
| 107611 | | - sqlite3VdbeAddFunctionCall(pParse, 1, regStat4, regTemp, 2+IsStat4, |
| 107612 | | - &statPushFuncdef, 0); |
| 107613 | | - sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 108452 | + assert( regChng==(regStat+1) ); |
| 108453 | + { |
| 108454 | + sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat4, |
| 108455 | + &statPushFuncdef, 0); |
| 108456 | + if( db->nAnalysisLimit ){ |
| 108457 | + int j1, j2, j3; |
| 108458 | + j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regTemp); VdbeCoverage(v); |
| 108459 | + j2 = sqlite3VdbeAddOp1(v, OP_If, regTemp); VdbeCoverage(v); |
| 108460 | + j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT, iIdxCur, 0, regPrev, 1); |
| 108461 | + VdbeCoverage(v); |
| 108462 | + sqlite3VdbeJumpHere(v, j1); |
| 108463 | + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 108464 | + sqlite3VdbeJumpHere(v, j2); |
| 108465 | + sqlite3VdbeJumpHere(v, j3); |
| 108466 | + }else{ |
| 108467 | + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 108468 | + } |
| 108469 | + } |
| 107614 | 108470 | |
| 107615 | 108471 | /* Add the entry to the stat1 table. */ |
| 107616 | | - callStatGet(pParse, regStat4, STAT_GET_STAT1, regStat1); |
| 108472 | + callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1); |
| 107617 | 108473 | assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
| 107618 | 108474 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
| 107619 | 108475 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 107620 | 108476 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
| 107621 | 108477 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| | @@ -107623,11 +108479,11 @@ |
| 107623 | 108479 | #endif |
| 107624 | 108480 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 107625 | 108481 | |
| 107626 | 108482 | /* Add the entries to the stat4 table. */ |
| 107627 | 108483 | #ifdef SQLITE_ENABLE_STAT4 |
| 107628 | | - { |
| 108484 | + if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){ |
| 107629 | 108485 | int regEq = regStat1; |
| 107630 | 108486 | int regLt = regStat1+1; |
| 107631 | 108487 | int regDLt = regStat1+2; |
| 107632 | 108488 | int regSample = regStat1+3; |
| 107633 | 108489 | int regCol = regStat1+4; |
| | @@ -107637,16 +108493,16 @@ |
| 107637 | 108493 | u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; |
| 107638 | 108494 | |
| 107639 | 108495 | pParse->nMem = MAX(pParse->nMem, regCol+nCol); |
| 107640 | 108496 | |
| 107641 | 108497 | addrNext = sqlite3VdbeCurrentAddr(v); |
| 107642 | | - callStatGet(pParse, regStat4, STAT_GET_ROWID, regSampleRowid); |
| 108498 | + callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid); |
| 107643 | 108499 | addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); |
| 107644 | 108500 | VdbeCoverage(v); |
| 107645 | | - callStatGet(pParse, regStat4, STAT_GET_NEQ, regEq); |
| 107646 | | - callStatGet(pParse, regStat4, STAT_GET_NLT, regLt); |
| 107647 | | - callStatGet(pParse, regStat4, STAT_GET_NDLT, regDLt); |
| 108501 | + callStatGet(pParse, regStat, STAT_GET_NEQ, regEq); |
| 108502 | + callStatGet(pParse, regStat, STAT_GET_NLT, regLt); |
| 108503 | + callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt); |
| 107648 | 108504 | sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); |
| 107649 | 108505 | VdbeCoverage(v); |
| 107650 | 108506 | for(i=0; i<nCol; i++){ |
| 107651 | 108507 | sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i); |
| 107652 | 108508 | } |
| | @@ -109500,26 +110356,43 @@ |
| 109500 | 110356 | ** exists */ |
| 109501 | 110357 | if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 109502 | 110358 | return 0; |
| 109503 | 110359 | } |
| 109504 | 110360 | #endif |
| 109505 | | - while(1){ |
| 109506 | | - for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 109507 | | - int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 109508 | | - if( zDatabase==0 || sqlite3DbIsNamed(db, j, zDatabase) ){ |
| 109509 | | - assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 109510 | | - p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); |
| 109511 | | - if( p ) return p; |
| 109512 | | - } |
| 109513 | | - } |
| 109514 | | - /* Not found. If the name we were looking for was temp.sqlite_master |
| 109515 | | - ** then change the name to sqlite_temp_master and try again. */ |
| 109516 | | - if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; |
| 109517 | | - if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; |
| 109518 | | - zName = TEMP_MASTER_NAME; |
| 109519 | | - } |
| 109520 | | - return 0; |
| 110361 | + if( zDatabase ){ |
| 110362 | + for(i=0; i<db->nDb; i++){ |
| 110363 | + if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 110364 | + } |
| 110365 | + if( i>=db->nDb ){ |
| 110366 | + /* No match against the official names. But always match "main" |
| 110367 | + ** to schema 0 as a legacy fallback. */ |
| 110368 | + if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 110369 | + i = 0; |
| 110370 | + }else{ |
| 110371 | + return 0; |
| 110372 | + } |
| 110373 | + } |
| 110374 | + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 110375 | + if( p==0 && i==1 && sqlite3StrICmp(zName, MASTER_NAME)==0 ){ |
| 110376 | + /* All temp.sqlite_master to be an alias for sqlite_temp_master */ |
| 110377 | + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, TEMP_MASTER_NAME); |
| 110378 | + } |
| 110379 | + }else{ |
| 110380 | + /* Match against TEMP first */ |
| 110381 | + p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 110382 | + if( p ) return p; |
| 110383 | + /* The main database is second */ |
| 110384 | + p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 110385 | + if( p ) return p; |
| 110386 | + /* Attached databases are in order of attachment */ |
| 110387 | + for(i=2; i<db->nDb; i++){ |
| 110388 | + assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 110389 | + p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 110390 | + if( p ) break; |
| 110391 | + } |
| 110392 | + } |
| 110393 | + return p; |
| 109521 | 110394 | } |
| 109522 | 110395 | |
| 109523 | 110396 | /* |
| 109524 | 110397 | ** Locate the in-memory structure that describes a particular database |
| 109525 | 110398 | ** table given the name of that table and (optionally) the name of the |
| | @@ -111319,10 +112192,32 @@ |
| 111319 | 112192 | assert( pPk->nColumn==j ); |
| 111320 | 112193 | assert( pTab->nNVCol<=j ); |
| 111321 | 112194 | recomputeColumnsNotIndexed(pPk); |
| 111322 | 112195 | } |
| 111323 | 112196 | |
| 112197 | + |
| 112198 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 112199 | +/* |
| 112200 | +** Return true if pTab is a virtual table and zName is a shadow table name |
| 112201 | +** for that virtual table. |
| 112202 | +*/ |
| 112203 | +SQLITE_PRIVATE int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 112204 | + int nName; /* Length of zName */ |
| 112205 | + Module *pMod; /* Module for the virtual table */ |
| 112206 | + |
| 112207 | + if( !IsVirtual(pTab) ) return 0; |
| 112208 | + nName = sqlite3Strlen30(pTab->zName); |
| 112209 | + if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 112210 | + if( zName[nName]!='_' ) return 0; |
| 112211 | + pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 112212 | + if( pMod==0 ) return 0; |
| 112213 | + if( pMod->pModule->iVersion<3 ) return 0; |
| 112214 | + if( pMod->pModule->xShadowName==0 ) return 0; |
| 112215 | + return pMod->pModule->xShadowName(zName+nName+1); |
| 112216 | +} |
| 112217 | +#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 112218 | + |
| 111324 | 112219 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 111325 | 112220 | /* |
| 111326 | 112221 | ** Return true if zName is a shadow table name in the current database |
| 111327 | 112222 | ** connection. |
| 111328 | 112223 | ** |
| | @@ -111330,26 +112225,21 @@ |
| 111330 | 112225 | ** restored to its original value prior to this routine returning. |
| 111331 | 112226 | */ |
| 111332 | 112227 | SQLITE_PRIVATE int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
| 111333 | 112228 | char *zTail; /* Pointer to the last "_" in zName */ |
| 111334 | 112229 | Table *pTab; /* Table that zName is a shadow of */ |
| 111335 | | - Module *pMod; /* Module for the virtual table */ |
| 111336 | | - |
| 111337 | 112230 | zTail = strrchr(zName, '_'); |
| 111338 | 112231 | if( zTail==0 ) return 0; |
| 111339 | 112232 | *zTail = 0; |
| 111340 | 112233 | pTab = sqlite3FindTable(db, zName, 0); |
| 111341 | 112234 | *zTail = '_'; |
| 111342 | 112235 | if( pTab==0 ) return 0; |
| 111343 | 112236 | if( !IsVirtual(pTab) ) return 0; |
| 111344 | | - pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 111345 | | - if( pMod==0 ) return 0; |
| 111346 | | - if( pMod->pModule->iVersion<3 ) return 0; |
| 111347 | | - if( pMod->pModule->xShadowName==0 ) return 0; |
| 111348 | | - return pMod->pModule->xShadowName(zTail+1); |
| 112237 | + return sqlite3IsShadowTableOf(db, pTab, zName); |
| 111349 | 112238 | } |
| 111350 | 112239 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 112240 | + |
| 111351 | 112241 | |
| 111352 | 112242 | #ifdef SQLITE_DEBUG |
| 111353 | 112243 | /* |
| 111354 | 112244 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 111355 | 112245 | ** they should not be changed. Expressions attached to a table or |
| | @@ -113818,11 +114708,11 @@ |
| 113818 | 114708 | pParse->rc = rc; |
| 113819 | 114709 | return 1; |
| 113820 | 114710 | } |
| 113821 | 114711 | db->aDb[1].pBt = pBt; |
| 113822 | 114712 | assert( db->aDb[1].pSchema ); |
| 113823 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
| 114713 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
| 113824 | 114714 | sqlite3OomFault(db); |
| 113825 | 114715 | return 1; |
| 113826 | 114716 | } |
| 113827 | 114717 | } |
| 113828 | 114718 | return 0; |
| | @@ -113929,11 +114819,11 @@ |
| 113929 | 114819 | char *p4, /* Error message */ |
| 113930 | 114820 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 113931 | 114821 | u8 p5Errmsg /* P5_ErrMsg type */ |
| 113932 | 114822 | ){ |
| 113933 | 114823 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 113934 | | - assert( (errCode&0xff)==SQLITE_CONSTRAINT ); |
| 114824 | + assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
| 113935 | 114825 | if( onError==OE_Abort ){ |
| 113936 | 114826 | sqlite3MayAbort(pParse); |
| 113937 | 114827 | } |
| 113938 | 114828 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
| 113939 | 114829 | sqlite3VdbeChangeP5(v, p5Errmsg); |
| | @@ -115642,10 +116532,11 @@ |
| 115642 | 116532 | VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName)); |
| 115643 | 116533 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, |
| 115644 | 116534 | &iPartIdxLabel, pPrior, r1); |
| 115645 | 116535 | sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1, |
| 115646 | 116536 | pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn); |
| 116537 | + sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */ |
| 115647 | 116538 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
| 115648 | 116539 | pPrior = pIdx; |
| 115649 | 116540 | } |
| 115650 | 116541 | } |
| 115651 | 116542 | |
| | @@ -117035,11 +117926,11 @@ |
| 117035 | 117926 | if( (cntExpand&(cntExpand-1))==0 ){ |
| 117036 | 117927 | /* Grow the size of the output buffer only on substitutions |
| 117037 | 117928 | ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */ |
| 117038 | 117929 | u8 *zOld; |
| 117039 | 117930 | zOld = zOut; |
| 117040 | | - zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1)); |
| 117931 | + zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1)); |
| 117041 | 117932 | if( zOut==0 ){ |
| 117042 | 117933 | sqlite3_result_error_nomem(context); |
| 117043 | 117934 | sqlite3_free(zOld); |
| 117044 | 117935 | return; |
| 117045 | 117936 | } |
| | @@ -117732,11 +118623,11 @@ |
| 117732 | 118623 | FUNCTION(round, 2, 0, 0, roundFunc ), |
| 117733 | 118624 | #endif |
| 117734 | 118625 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| 117735 | 118626 | FUNCTION(lower, 1, 0, 0, lowerFunc ), |
| 117736 | 118627 | FUNCTION(hex, 1, 0, 0, hexFunc ), |
| 117737 | | - INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, SQLITE_FUNC_COALESCE), |
| 118628 | + INLINE_FUNC(ifnull, 2, INLINEFUNC_coalesce, 0 ), |
| 117738 | 118629 | VFUNCTION(random, 0, 0, 0, randomFunc ), |
| 117739 | 118630 | VFUNCTION(randomblob, 1, 0, 0, randomBlob ), |
| 117740 | 118631 | FUNCTION(nullif, 2, 0, 1, nullifFunc ), |
| 117741 | 118632 | DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ), |
| 117742 | 118633 | DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), |
| | @@ -117772,11 +118663,12 @@ |
| 117772 | 118663 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 117773 | 118664 | FUNCTION(unknown, -1, 0, 0, unknownFunc ), |
| 117774 | 118665 | #endif |
| 117775 | 118666 | FUNCTION(coalesce, 1, 0, 0, 0 ), |
| 117776 | 118667 | FUNCTION(coalesce, 0, 0, 0, 0 ), |
| 117777 | | - INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, SQLITE_FUNC_COALESCE), |
| 118668 | + INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ), |
| 118669 | + INLINE_FUNC(iif, 3, INLINEFUNC_iif, 0 ), |
| 117778 | 118670 | }; |
| 117779 | 118671 | #ifndef SQLITE_OMIT_ALTERTABLE |
| 117780 | 118672 | sqlite3AlterFunctions(); |
| 117781 | 118673 | #endif |
| 117782 | 118674 | sqlite3WindowFunctions(); |
| | @@ -121197,11 +122089,11 @@ |
| 121197 | 122089 | } |
| 121198 | 122090 | if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){ |
| 121199 | 122091 | sqlite3TableAffinity(v, pTab, regNewData+1); |
| 121200 | 122092 | bAffinityDone = 1; |
| 121201 | 122093 | } |
| 121202 | | - VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName)); |
| 122094 | + VdbeNoopComment((v, "prep index %s", pIdx->zName)); |
| 121203 | 122095 | iThisCur = iIdxCur+ix; |
| 121204 | 122096 | |
| 121205 | 122097 | |
| 121206 | 122098 | /* Skip partial indices for which the WHERE clause is not true */ |
| 121207 | 122099 | if( pIdx->pPartIdxWhere ){ |
| | @@ -122629,10 +123521,11 @@ |
| 122629 | 123521 | const char *(*filename_wal)(const char*); |
| 122630 | 123522 | /* Version 3.32.0 and later */ |
| 122631 | 123523 | char *(*create_filename)(const char*,const char*,const char*, |
| 122632 | 123524 | int,const char**); |
| 122633 | 123525 | void (*free_filename)(char*); |
| 123526 | + sqlite3_file *(*database_file_object)(const char*); |
| 122634 | 123527 | }; |
| 122635 | 123528 | |
| 122636 | 123529 | /* |
| 122637 | 123530 | ** This is the function signature used for all extension entry points. It |
| 122638 | 123531 | ** is also defined in the file "loadext.c". |
| | @@ -122932,10 +123825,11 @@ |
| 122932 | 123825 | #define sqlite3_filename_journal sqlite3_api->filename_journal |
| 122933 | 123826 | #define sqlite3_filename_wal sqlite3_api->filename_wal |
| 122934 | 123827 | /* Version 3.32.0 and later */ |
| 122935 | 123828 | #define sqlite3_create_filename sqlite3_api->create_filename |
| 122936 | 123829 | #define sqlite3_free_filename sqlite3_api->free_filename |
| 123830 | +#define sqlite3_database_file_object sqlite3_api->database_file_object |
| 122937 | 123831 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 122938 | 123832 | |
| 122939 | 123833 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 122940 | 123834 | /* This case when the file really is being compiled as a loadable |
| 122941 | 123835 | ** extension */ |
| | @@ -123413,11 +124307,20 @@ |
| 123413 | 124307 | sqlite3_filename_journal, |
| 123414 | 124308 | sqlite3_filename_wal, |
| 123415 | 124309 | /* Version 3.32.0 and later */ |
| 123416 | 124310 | sqlite3_create_filename, |
| 123417 | 124311 | sqlite3_free_filename, |
| 124312 | + sqlite3_database_file_object, |
| 123418 | 124313 | }; |
| 124314 | + |
| 124315 | +/* True if x is the directory separator character |
| 124316 | +*/ |
| 124317 | +#if SQLITE_OS_WIN |
| 124318 | +# define DirSep(X) ((X)=='/'||(X)=='\\') |
| 124319 | +#else |
| 124320 | +# define DirSep(X) ((X)=='/') |
| 124321 | +#endif |
| 123419 | 124322 | |
| 123420 | 124323 | /* |
| 123421 | 124324 | ** Attempt to load an SQLite extension library contained in the file |
| 123422 | 124325 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| 123423 | 124326 | ** default entry point name (sqlite3_extension_init) is used. Use |
| | @@ -123516,11 +124419,11 @@ |
| 123516 | 124419 | if( zAltEntry==0 ){ |
| 123517 | 124420 | sqlite3OsDlClose(pVfs, handle); |
| 123518 | 124421 | return SQLITE_NOMEM_BKPT; |
| 123519 | 124422 | } |
| 123520 | 124423 | memcpy(zAltEntry, "sqlite3_", 8); |
| 123521 | | - for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} |
| 124424 | + for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){} |
| 123522 | 124425 | iFile++; |
| 123523 | 124426 | if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; |
| 123524 | 124427 | for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ |
| 123525 | 124428 | if( sqlite3Isalpha(c) ){ |
| 123526 | 124429 | zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; |
| | @@ -123820,53 +124723,54 @@ |
| 123820 | 124723 | ** that script and rerun it. |
| 123821 | 124724 | */ |
| 123822 | 124725 | |
| 123823 | 124726 | /* The various pragma types */ |
| 123824 | 124727 | #define PragTyp_ACTIVATE_EXTENSIONS 0 |
| 123825 | | -#define PragTyp_HEADER_VALUE 1 |
| 123826 | | -#define PragTyp_AUTO_VACUUM 2 |
| 123827 | | -#define PragTyp_FLAG 3 |
| 123828 | | -#define PragTyp_BUSY_TIMEOUT 4 |
| 123829 | | -#define PragTyp_CACHE_SIZE 5 |
| 123830 | | -#define PragTyp_CACHE_SPILL 6 |
| 123831 | | -#define PragTyp_CASE_SENSITIVE_LIKE 7 |
| 123832 | | -#define PragTyp_COLLATION_LIST 8 |
| 123833 | | -#define PragTyp_COMPILE_OPTIONS 9 |
| 123834 | | -#define PragTyp_DATA_STORE_DIRECTORY 10 |
| 123835 | | -#define PragTyp_DATABASE_LIST 11 |
| 123836 | | -#define PragTyp_DEFAULT_CACHE_SIZE 12 |
| 123837 | | -#define PragTyp_ENCODING 13 |
| 123838 | | -#define PragTyp_FOREIGN_KEY_CHECK 14 |
| 123839 | | -#define PragTyp_FOREIGN_KEY_LIST 15 |
| 123840 | | -#define PragTyp_FUNCTION_LIST 16 |
| 123841 | | -#define PragTyp_HARD_HEAP_LIMIT 17 |
| 123842 | | -#define PragTyp_INCREMENTAL_VACUUM 18 |
| 123843 | | -#define PragTyp_INDEX_INFO 19 |
| 123844 | | -#define PragTyp_INDEX_LIST 20 |
| 123845 | | -#define PragTyp_INTEGRITY_CHECK 21 |
| 123846 | | -#define PragTyp_JOURNAL_MODE 22 |
| 123847 | | -#define PragTyp_JOURNAL_SIZE_LIMIT 23 |
| 123848 | | -#define PragTyp_LOCK_PROXY_FILE 24 |
| 123849 | | -#define PragTyp_LOCKING_MODE 25 |
| 123850 | | -#define PragTyp_PAGE_COUNT 26 |
| 123851 | | -#define PragTyp_MMAP_SIZE 27 |
| 123852 | | -#define PragTyp_MODULE_LIST 28 |
| 123853 | | -#define PragTyp_OPTIMIZE 29 |
| 123854 | | -#define PragTyp_PAGE_SIZE 30 |
| 123855 | | -#define PragTyp_PRAGMA_LIST 31 |
| 123856 | | -#define PragTyp_SECURE_DELETE 32 |
| 123857 | | -#define PragTyp_SHRINK_MEMORY 33 |
| 123858 | | -#define PragTyp_SOFT_HEAP_LIMIT 34 |
| 123859 | | -#define PragTyp_SYNCHRONOUS 35 |
| 123860 | | -#define PragTyp_TABLE_INFO 36 |
| 123861 | | -#define PragTyp_TEMP_STORE 37 |
| 123862 | | -#define PragTyp_TEMP_STORE_DIRECTORY 38 |
| 123863 | | -#define PragTyp_THREADS 39 |
| 123864 | | -#define PragTyp_WAL_AUTOCHECKPOINT 40 |
| 123865 | | -#define PragTyp_WAL_CHECKPOINT 41 |
| 123866 | | -#define PragTyp_LOCK_STATUS 42 |
| 123867 | | -#define PragTyp_STATS 43 |
| 124728 | +#define PragTyp_ANALYSIS_LIMIT 1 |
| 124729 | +#define PragTyp_HEADER_VALUE 2 |
| 124730 | +#define PragTyp_AUTO_VACUUM 3 |
| 124731 | +#define PragTyp_FLAG 4 |
| 124732 | +#define PragTyp_BUSY_TIMEOUT 5 |
| 124733 | +#define PragTyp_CACHE_SIZE 6 |
| 124734 | +#define PragTyp_CACHE_SPILL 7 |
| 124735 | +#define PragTyp_CASE_SENSITIVE_LIKE 8 |
| 124736 | +#define PragTyp_COLLATION_LIST 9 |
| 124737 | +#define PragTyp_COMPILE_OPTIONS 10 |
| 124738 | +#define PragTyp_DATA_STORE_DIRECTORY 11 |
| 124739 | +#define PragTyp_DATABASE_LIST 12 |
| 124740 | +#define PragTyp_DEFAULT_CACHE_SIZE 13 |
| 124741 | +#define PragTyp_ENCODING 14 |
| 124742 | +#define PragTyp_FOREIGN_KEY_CHECK 15 |
| 124743 | +#define PragTyp_FOREIGN_KEY_LIST 16 |
| 124744 | +#define PragTyp_FUNCTION_LIST 17 |
| 124745 | +#define PragTyp_HARD_HEAP_LIMIT 18 |
| 124746 | +#define PragTyp_INCREMENTAL_VACUUM 19 |
| 124747 | +#define PragTyp_INDEX_INFO 20 |
| 124748 | +#define PragTyp_INDEX_LIST 21 |
| 124749 | +#define PragTyp_INTEGRITY_CHECK 22 |
| 124750 | +#define PragTyp_JOURNAL_MODE 23 |
| 124751 | +#define PragTyp_JOURNAL_SIZE_LIMIT 24 |
| 124752 | +#define PragTyp_LOCK_PROXY_FILE 25 |
| 124753 | +#define PragTyp_LOCKING_MODE 26 |
| 124754 | +#define PragTyp_PAGE_COUNT 27 |
| 124755 | +#define PragTyp_MMAP_SIZE 28 |
| 124756 | +#define PragTyp_MODULE_LIST 29 |
| 124757 | +#define PragTyp_OPTIMIZE 30 |
| 124758 | +#define PragTyp_PAGE_SIZE 31 |
| 124759 | +#define PragTyp_PRAGMA_LIST 32 |
| 124760 | +#define PragTyp_SECURE_DELETE 33 |
| 124761 | +#define PragTyp_SHRINK_MEMORY 34 |
| 124762 | +#define PragTyp_SOFT_HEAP_LIMIT 35 |
| 124763 | +#define PragTyp_SYNCHRONOUS 36 |
| 124764 | +#define PragTyp_TABLE_INFO 37 |
| 124765 | +#define PragTyp_TEMP_STORE 38 |
| 124766 | +#define PragTyp_TEMP_STORE_DIRECTORY 39 |
| 124767 | +#define PragTyp_THREADS 40 |
| 124768 | +#define PragTyp_WAL_AUTOCHECKPOINT 41 |
| 124769 | +#define PragTyp_WAL_CHECKPOINT 42 |
| 124770 | +#define PragTyp_LOCK_STATUS 43 |
| 124771 | +#define PragTyp_STATS 44 |
| 123868 | 124772 | |
| 123869 | 124773 | /* Property flags associated with various pragma. */ |
| 123870 | 124774 | #define PragFlg_NeedSchema 0x01 /* Force schema load before running */ |
| 123871 | 124775 | #define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */ |
| 123872 | 124776 | #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */ |
| | @@ -123953,10 +124857,15 @@ |
| 123953 | 124857 | /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS, |
| 123954 | 124858 | /* ePragFlg: */ 0, |
| 123955 | 124859 | /* ColNames: */ 0, 0, |
| 123956 | 124860 | /* iArg: */ 0 }, |
| 123957 | 124861 | #endif |
| 124862 | + {/* zName: */ "analysis_limit", |
| 124863 | + /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT, |
| 124864 | + /* ePragFlg: */ PragFlg_Result0, |
| 124865 | + /* ColNames: */ 0, 0, |
| 124866 | + /* iArg: */ 0 }, |
| 123958 | 124867 | #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) |
| 123959 | 124868 | {/* zName: */ "application_id", |
| 123960 | 124869 | /* ePragTyp: */ PragTyp_HEADER_VALUE, |
| 123961 | 124870 | /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0, |
| 123962 | 124871 | /* ColNames: */ 0, 0, |
| | @@ -124453,11 +125362,11 @@ |
| 124453 | 125362 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 124454 | 125363 | /* ColNames: */ 0, 0, |
| 124455 | 125364 | /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError }, |
| 124456 | 125365 | #endif |
| 124457 | 125366 | }; |
| 124458 | | -/* Number of pragmas: 66 on by default, 76 total. */ |
| 125367 | +/* Number of pragmas: 67 on by default, 77 total. */ |
| 124459 | 125368 | |
| 124460 | 125369 | /************** End of pragma.h **********************************************/ |
| 124461 | 125370 | /************** Continuing where we left off in pragma.c *********************/ |
| 124462 | 125371 | |
| 124463 | 125372 | /* |
| | @@ -124983,11 +125892,11 @@ |
| 124983 | 125892 | }else{ |
| 124984 | 125893 | /* Malloc may fail when setting the page-size, as there is an internal |
| 124985 | 125894 | ** buffer that the pager module resizes using sqlite3_realloc(). |
| 124986 | 125895 | */ |
| 124987 | 125896 | db->nextPagesize = sqlite3Atoi(zRight); |
| 124988 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ |
| 125897 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ |
| 124989 | 125898 | sqlite3OomFault(db); |
| 124990 | 125899 | } |
| 124991 | 125900 | } |
| 124992 | 125901 | break; |
| 124993 | 125902 | } |
| | @@ -126157,11 +127066,10 @@ |
| 126157 | 127066 | sqlite3ResolvePartIdxLabel(pParse, jmp3); |
| 126158 | 127067 | } |
| 126159 | 127068 | } |
| 126160 | 127069 | sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); |
| 126161 | 127070 | sqlite3VdbeJumpHere(v, loopTop-1); |
| 126162 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 126163 | 127071 | if( !isQuick ){ |
| 126164 | 127072 | sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); |
| 126165 | 127073 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 126166 | 127074 | if( pPk==pIdx ) continue; |
| 126167 | 127075 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); |
| | @@ -126171,11 +127079,10 @@ |
| 126171 | 127079 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); |
| 126172 | 127080 | integrityCheckResultRow(v); |
| 126173 | 127081 | sqlite3VdbeJumpHere(v, addr); |
| 126174 | 127082 | } |
| 126175 | 127083 | } |
| 126176 | | -#endif /* SQLITE_OMIT_BTREECOUNT */ |
| 126177 | 127084 | } |
| 126178 | 127085 | } |
| 126179 | 127086 | { |
| 126180 | 127087 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 126181 | 127088 | static const VdbeOpList endCode[] = { |
| | @@ -126605,10 +127512,29 @@ |
| 126605 | 127512 | sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); |
| 126606 | 127513 | } |
| 126607 | 127514 | returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); |
| 126608 | 127515 | break; |
| 126609 | 127516 | } |
| 127517 | + |
| 127518 | + /* |
| 127519 | + ** PRAGMA analysis_limit |
| 127520 | + ** PRAGMA analysis_limit = N |
| 127521 | + ** |
| 127522 | + ** Configure the maximum number of rows that ANALYZE will examine |
| 127523 | + ** in each index that it looks at. Return the new limit. |
| 127524 | + */ |
| 127525 | + case PragTyp_ANALYSIS_LIMIT: { |
| 127526 | + sqlite3_int64 N; |
| 127527 | + if( zRight |
| 127528 | + && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK |
| 127529 | + && N>=0 |
| 127530 | + ){ |
| 127531 | + db->nAnalysisLimit = (int)(N&0x7fffffff); |
| 127532 | + } |
| 127533 | + returnSingleInt(v, db->nAnalysisLimit); |
| 127534 | + break; |
| 127535 | + } |
| 126610 | 127536 | |
| 126611 | 127537 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 126612 | 127538 | /* |
| 126613 | 127539 | ** Report the current state of file logs for all databases |
| 126614 | 127540 | */ |
| | @@ -131404,10 +132330,11 @@ |
| 131404 | 132330 | if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){ |
| 131405 | 132331 | memset(&ifNullRow, 0, sizeof(ifNullRow)); |
| 131406 | 132332 | ifNullRow.op = TK_IF_NULL_ROW; |
| 131407 | 132333 | ifNullRow.pLeft = pCopy; |
| 131408 | 132334 | ifNullRow.iTable = pSubst->iNewTable; |
| 132335 | + ifNullRow.flags = EP_Skip; |
| 131409 | 132336 | pCopy = &ifNullRow; |
| 131410 | 132337 | } |
| 131411 | 132338 | testcase( ExprHasProperty(pCopy, EP_Subquery) ); |
| 131412 | 132339 | pNew = sqlite3ExprDup(db, pCopy, 0); |
| 131413 | 132340 | if( pNew && pSubst->isLeftJoin ){ |
| | @@ -133134,33 +134061,10 @@ |
| 133134 | 134061 | } |
| 133135 | 134062 | } |
| 133136 | 134063 | return WRC_Continue; |
| 133137 | 134064 | } |
| 133138 | 134065 | |
| 133139 | | -/* |
| 133140 | | -** No-op routine for the parse-tree walker. |
| 133141 | | -** |
| 133142 | | -** When this routine is the Walker.xExprCallback then expression trees |
| 133143 | | -** are walked without any actions being taken at each node. Presumably, |
| 133144 | | -** when this routine is used for Walker.xExprCallback then |
| 133145 | | -** Walker.xSelectCallback is set to do something useful for every |
| 133146 | | -** subquery in the parser tree. |
| 133147 | | -*/ |
| 133148 | | -SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ |
| 133149 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 133150 | | - return WRC_Continue; |
| 133151 | | -} |
| 133152 | | - |
| 133153 | | -/* |
| 133154 | | -** No-op routine for the parse-tree walker for SELECT statements. |
| 133155 | | -** subquery in the parser tree. |
| 133156 | | -*/ |
| 133157 | | -SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){ |
| 133158 | | - UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 133159 | | - return WRC_Continue; |
| 133160 | | -} |
| 133161 | | - |
| 133162 | 134066 | #if SQLITE_DEBUG |
| 133163 | 134067 | /* |
| 133164 | 134068 | ** Always assert. This xSelectCallback2 implementation proves that the |
| 133165 | 134069 | ** xSelectCallback2 is never invoked. |
| 133166 | 134070 | */ |
| | @@ -134327,11 +135231,11 @@ |
| 134327 | 135231 | sAggInfo.mxReg = pParse->nMem; |
| 134328 | 135232 | if( db->mallocFailed ) goto select_end; |
| 134329 | 135233 | #if SELECTTRACE_ENABLED |
| 134330 | 135234 | if( sqlite3SelectTrace & 0x400 ){ |
| 134331 | 135235 | int ii; |
| 134332 | | - SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n")); |
| 135236 | + SELECTTRACE(0x400,pParse,p,("After aggregate analysis %p:\n", &sAggInfo)); |
| 134333 | 135237 | sqlite3TreeViewSelect(0, p, 0); |
| 134334 | 135238 | for(ii=0; ii<sAggInfo.nColumn; ii++){ |
| 134335 | 135239 | sqlite3DebugPrintf("agg-column[%d] iMem=%d\n", |
| 134336 | 135240 | ii, sAggInfo.aCol[ii].iMem); |
| 134337 | 135241 | sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0); |
| | @@ -134568,11 +135472,10 @@ |
| 134568 | 135472 | VdbeComment((v, "indicate accumulator empty")); |
| 134569 | 135473 | sqlite3VdbeAddOp1(v, OP_Return, regReset); |
| 134570 | 135474 | |
| 134571 | 135475 | } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ |
| 134572 | 135476 | else { |
| 134573 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 134574 | 135477 | Table *pTab; |
| 134575 | 135478 | if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){ |
| 134576 | 135479 | /* If isSimpleCount() returns a pointer to a Table structure, then |
| 134577 | 135480 | ** the SQL statement is of the form: |
| 134578 | 135481 | ** |
| | @@ -134604,17 +135507,19 @@ |
| 134604 | 135507 | ** |
| 134605 | 135508 | ** In practice the KeyInfo structure will not be used. It is only |
| 134606 | 135509 | ** passed to keep OP_OpenRead happy. |
| 134607 | 135510 | */ |
| 134608 | 135511 | if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab); |
| 134609 | | - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 134610 | | - if( pIdx->bUnordered==0 |
| 134611 | | - && pIdx->szIdxRow<pTab->szTabRow |
| 134612 | | - && pIdx->pPartIdxWhere==0 |
| 134613 | | - && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 134614 | | - ){ |
| 134615 | | - pBest = pIdx; |
| 135512 | + if( !p->pSrc->a[0].fg.notIndexed ){ |
| 135513 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 135514 | + if( pIdx->bUnordered==0 |
| 135515 | + && pIdx->szIdxRow<pTab->szTabRow |
| 135516 | + && pIdx->pPartIdxWhere==0 |
| 135517 | + && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 135518 | + ){ |
| 135519 | + pBest = pIdx; |
| 135520 | + } |
| 134616 | 135521 | } |
| 134617 | 135522 | } |
| 134618 | 135523 | if( pBest ){ |
| 134619 | 135524 | iRoot = pBest->tnum; |
| 134620 | 135525 | pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest); |
| | @@ -134626,13 +135531,11 @@ |
| 134626 | 135531 | sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO); |
| 134627 | 135532 | } |
| 134628 | 135533 | sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); |
| 134629 | 135534 | sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
| 134630 | 135535 | explainSimpleCount(pParse, pTab, pBest); |
| 134631 | | - }else |
| 134632 | | -#endif /* SQLITE_OMIT_BTREECOUNT */ |
| 134633 | | - { |
| 135536 | + }else{ |
| 134634 | 135537 | int regAcc = 0; /* "populate accumulators" flag */ |
| 134635 | 135538 | |
| 134636 | 135539 | /* If there are accumulator registers but no min() or max() functions |
| 134637 | 135540 | ** without FILTER clauses, allocate register regAcc. Register regAcc |
| 134638 | 135541 | ** will contain 0 the first time the inner loop runs, and 1 thereafter. |
| | @@ -134791,11 +135694,11 @@ |
| 134791 | 135694 | need = nCol; |
| 134792 | 135695 | } |
| 134793 | 135696 | if( p->nData + need > p->nAlloc ){ |
| 134794 | 135697 | char **azNew; |
| 134795 | 135698 | p->nAlloc = p->nAlloc*2 + need; |
| 134796 | | - azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); |
| 135699 | + azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc ); |
| 134797 | 135700 | if( azNew==0 ) goto malloc_failed; |
| 134798 | 135701 | p->azResult = azNew; |
| 134799 | 135702 | } |
| 134800 | 135703 | |
| 134801 | 135704 | /* If this is the first row, then generate an extra row containing |
| | @@ -134900,11 +135803,11 @@ |
| 134900 | 135803 | sqlite3_free_table(&res.azResult[1]); |
| 134901 | 135804 | return rc; |
| 134902 | 135805 | } |
| 134903 | 135806 | if( res.nAlloc>res.nData ){ |
| 134904 | 135807 | char **azNew; |
| 134905 | | - azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); |
| 135808 | + azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData ); |
| 134906 | 135809 | if( azNew==0 ){ |
| 134907 | 135810 | sqlite3_free_table(&res.azResult[1]); |
| 134908 | 135811 | db->errCode = SQLITE_NOMEM; |
| 134909 | 135812 | return SQLITE_NOMEM_BKPT; |
| 134910 | 135813 | } |
| | @@ -136190,14 +137093,14 @@ |
| 136190 | 137093 | ** Therefore, the P4 parameter is only required if the default value for |
| 136191 | 137094 | ** the column is a literal number, string or null. The sqlite3ValueFromExpr() |
| 136192 | 137095 | ** function is capable of transforming these types of expressions into |
| 136193 | 137096 | ** sqlite3_value objects. |
| 136194 | 137097 | ** |
| 136195 | | -** If parameter iReg is not negative, code an OP_RealAffinity instruction |
| 136196 | | -** on register iReg. This is used when an equivalent integer value is |
| 136197 | | -** stored in place of an 8-byte floating point value in order to save |
| 136198 | | -** space. |
| 137098 | +** If column as REAL affinity and the table is an ordinary b-tree table |
| 137099 | +** (not a virtual table) then the value might have been stored as an |
| 137100 | +** integer. In that case, add an OP_RealAffinity opcode to make sure |
| 137101 | +** it has been converted into REAL. |
| 136199 | 137102 | */ |
| 136200 | 137103 | SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ |
| 136201 | 137104 | assert( pTab!=0 ); |
| 136202 | 137105 | if( !pTab->pSelect ){ |
| 136203 | 137106 | sqlite3_value *pValue = 0; |
| | @@ -136210,11 +137113,11 @@ |
| 136210 | 137113 | if( pValue ){ |
| 136211 | 137114 | sqlite3VdbeAppendP4(v, pValue, P4_MEM); |
| 136212 | 137115 | } |
| 136213 | 137116 | } |
| 136214 | 137117 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 136215 | | - if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ |
| 137118 | + if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){ |
| 136216 | 137119 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
| 136217 | 137120 | } |
| 136218 | 137121 | #endif |
| 136219 | 137122 | } |
| 136220 | 137123 | |
| | @@ -137852,11 +138755,11 @@ |
| 137852 | 138755 | db->mDbFlags = saved_mDbFlags; |
| 137853 | 138756 | db->flags = saved_flags; |
| 137854 | 138757 | db->nChange = saved_nChange; |
| 137855 | 138758 | db->nTotalChange = saved_nTotalChange; |
| 137856 | 138759 | db->mTrace = saved_mTrace; |
| 137857 | | - sqlite3BtreeSetPageSize(pMain, -1, -1, 1); |
| 138760 | + sqlite3BtreeSetPageSize(pMain, -1, 0, 1); |
| 137858 | 138761 | |
| 137859 | 138762 | /* Currently there is an SQL level transaction open on the vacuum |
| 137860 | 138763 | ** database. No locks are held on any other files (since the main file |
| 137861 | 138764 | ** was committed at the btree level). So it safe to end the transaction |
| 137862 | 138765 | ** by manually setting the autoCommit flag to true and detaching the |
| | @@ -139059,11 +139962,11 @@ |
| 139059 | 139962 | assert( IsVirtual(pTab) ); |
| 139060 | 139963 | for(i=0; i<pToplevel->nVtabLock; i++){ |
| 139061 | 139964 | if( pTab==pToplevel->apVtabLock[i] ) return; |
| 139062 | 139965 | } |
| 139063 | 139966 | n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
| 139064 | | - apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); |
| 139967 | + apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n); |
| 139065 | 139968 | if( apVtabLock ){ |
| 139066 | 139969 | pToplevel->apVtabLock = apVtabLock; |
| 139067 | 139970 | pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
| 139068 | 139971 | }else{ |
| 139069 | 139972 | sqlite3OomFault(pToplevel->db); |
| | @@ -150351,24 +151254,47 @@ |
| 150351 | 151254 | ){ |
| 150352 | 151255 | if( pAppend ){ |
| 150353 | 151256 | int i; |
| 150354 | 151257 | int nInit = pList ? pList->nExpr : 0; |
| 150355 | 151258 | for(i=0; i<pAppend->nExpr; i++){ |
| 150356 | | - int iDummy; |
| 150357 | 151259 | Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0); |
| 150358 | 151260 | assert( pDup==0 || !ExprHasProperty(pDup, EP_MemToken) ); |
| 150359 | | - if( bIntToNull && pDup && sqlite3ExprIsInteger(pDup, &iDummy) ){ |
| 150360 | | - pDup->op = TK_NULL; |
| 150361 | | - pDup->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 150362 | | - pDup->u.zToken = 0; |
| 151261 | + if( bIntToNull && pDup ){ |
| 151262 | + int iDummy; |
| 151263 | + Expr *pSub; |
| 151264 | + for(pSub=pDup; ExprHasProperty(pSub, EP_Skip); pSub=pSub->pLeft){ |
| 151265 | + assert( pSub ); |
| 151266 | + } |
| 151267 | + if( sqlite3ExprIsInteger(pSub, &iDummy) ){ |
| 151268 | + pSub->op = TK_NULL; |
| 151269 | + pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 151270 | + pSub->u.zToken = 0; |
| 151271 | + } |
| 150363 | 151272 | } |
| 150364 | 151273 | pList = sqlite3ExprListAppend(pParse, pList, pDup); |
| 150365 | 151274 | if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags; |
| 150366 | 151275 | } |
| 150367 | 151276 | } |
| 150368 | 151277 | return pList; |
| 150369 | 151278 | } |
| 151279 | + |
| 151280 | +/* |
| 151281 | +** When rewriting a query, if the new subquery in the FROM clause |
| 151282 | +** contains TK_AGG_FUNCTION nodes that refer to an outer query, |
| 151283 | +** then we have to increase the Expr->op2 values of those nodes |
| 151284 | +** due to the extra subquery layer that was added. |
| 151285 | +** |
| 151286 | +** See also the incrAggDepth() routine in resolve.c |
| 151287 | +*/ |
| 151288 | +static int sqlite3WindowExtraAggFuncDepth(Walker *pWalker, Expr *pExpr){ |
| 151289 | + if( pExpr->op==TK_AGG_FUNCTION |
| 151290 | + && pExpr->op2>=pWalker->walkerDepth |
| 151291 | + ){ |
| 151292 | + pExpr->op2++; |
| 151293 | + } |
| 151294 | + return WRC_Continue; |
| 151295 | +} |
| 150370 | 151296 | |
| 150371 | 151297 | /* |
| 150372 | 151298 | ** If the SELECT statement passed as the second argument does not invoke |
| 150373 | 151299 | ** any SQL window functions, this function is a no-op. Otherwise, it |
| 150374 | 151300 | ** rewrites the SELECT statement so that window function xStep functions |
| | @@ -150475,10 +151401,11 @@ |
| 150475 | 151401 | pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0 |
| 150476 | 151402 | ); |
| 150477 | 151403 | p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0); |
| 150478 | 151404 | if( p->pSrc ){ |
| 150479 | 151405 | Table *pTab2; |
| 151406 | + Walker w; |
| 150480 | 151407 | p->pSrc->a[0].pSelect = pSub; |
| 150481 | 151408 | sqlite3SrcListAssignCursors(pParse, p->pSrc); |
| 150482 | 151409 | pSub->selFlags |= SF_Expanded; |
| 150483 | 151410 | pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE); |
| 150484 | 151411 | pSub->selFlags |= (selFlags & SF_Aggregate); |
| | @@ -150490,10 +151417,15 @@ |
| 150490 | 151417 | }else{ |
| 150491 | 151418 | memcpy(pTab, pTab2, sizeof(Table)); |
| 150492 | 151419 | pTab->tabFlags |= TF_Ephemeral; |
| 150493 | 151420 | p->pSrc->a[0].pTab = pTab; |
| 150494 | 151421 | pTab = pTab2; |
| 151422 | + memset(&w, 0, sizeof(w)); |
| 151423 | + w.xExprCallback = sqlite3WindowExtraAggFuncDepth; |
| 151424 | + w.xSelectCallback = sqlite3WalkerDepthIncrease; |
| 151425 | + w.xSelectCallback2 = sqlite3WalkerDepthDecrease; |
| 151426 | + sqlite3WalkSelect(&w, pSub); |
| 150495 | 151427 | } |
| 150496 | 151428 | }else{ |
| 150497 | 151429 | sqlite3SelectDelete(db, pSub); |
| 150498 | 151430 | } |
| 150499 | 151431 | if( db->mallocFailed ) rc = SQLITE_NOMEM; |
| | @@ -159255,19 +160187,82 @@ |
| 159255 | 160187 | |
| 159256 | 160188 | |
| 159257 | 160189 | /************** End of sqliteicu.h *******************************************/ |
| 159258 | 160190 | /************** Continuing where we left off in main.c ***********************/ |
| 159259 | 160191 | #endif |
| 160192 | + |
| 160193 | +/* |
| 160194 | +** This is an extension initializer that is a no-op and always |
| 160195 | +** succeeds, except that it fails if the fault-simulation is set |
| 160196 | +** to 500. |
| 160197 | +*/ |
| 160198 | +static int sqlite3TestExtInit(sqlite3 *db){ |
| 160199 | + (void)db; |
| 160200 | + return sqlite3FaultSim(500); |
| 160201 | +} |
| 160202 | + |
| 160203 | + |
| 160204 | +/* |
| 160205 | +** Forward declarations of external module initializer functions |
| 160206 | +** for modules that need them. |
| 160207 | +*/ |
| 160208 | +#ifdef SQLITE_ENABLE_FTS1 |
| 160209 | +SQLITE_PRIVATE int sqlite3Fts1Init(sqlite3*); |
| 160210 | +#endif |
| 160211 | +#ifdef SQLITE_ENABLE_FTS2 |
| 160212 | +SQLITE_PRIVATE int sqlite3Fts2Init(sqlite3*); |
| 160213 | +#endif |
| 160214 | +#ifdef SQLITE_ENABLE_FTS5 |
| 160215 | +SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); |
| 160216 | +#endif |
| 159260 | 160217 | #ifdef SQLITE_ENABLE_JSON1 |
| 159261 | 160218 | SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*); |
| 159262 | 160219 | #endif |
| 159263 | 160220 | #ifdef SQLITE_ENABLE_STMTVTAB |
| 159264 | 160221 | SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*); |
| 159265 | 160222 | #endif |
| 160223 | + |
| 160224 | +/* |
| 160225 | +** An array of pointers to extension initializer functions for |
| 160226 | +** built-in extensions. |
| 160227 | +*/ |
| 160228 | +static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = { |
| 160229 | +#ifdef SQLITE_ENABLE_FTS1 |
| 160230 | + sqlite3Fts1Init, |
| 160231 | +#endif |
| 160232 | +#ifdef SQLITE_ENABLE_FTS2 |
| 160233 | + sqlite3Fts2Init, |
| 160234 | +#endif |
| 160235 | +#ifdef SQLITE_ENABLE_FTS3 |
| 160236 | + sqlite3Fts3Init, |
| 160237 | +#endif |
| 159266 | 160238 | #ifdef SQLITE_ENABLE_FTS5 |
| 159267 | | -SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); |
| 160239 | + sqlite3Fts5Init, |
| 159268 | 160240 | #endif |
| 160241 | +#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) |
| 160242 | + sqlite3IcuInit, |
| 160243 | +#endif |
| 160244 | +#ifdef SQLITE_ENABLE_RTREE |
| 160245 | + sqlite3RtreeInit, |
| 160246 | +#endif |
| 160247 | +#ifdef SQLITE_ENABLE_DBPAGE_VTAB |
| 160248 | + sqlite3DbpageRegister, |
| 160249 | +#endif |
| 160250 | +#ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 160251 | + sqlite3DbstatRegister, |
| 160252 | +#endif |
| 160253 | + sqlite3TestExtInit, |
| 160254 | +#ifdef SQLITE_ENABLE_JSON1 |
| 160255 | + sqlite3Json1Init, |
| 160256 | +#endif |
| 160257 | +#ifdef SQLITE_ENABLE_STMTVTAB |
| 160258 | + sqlite3StmtVtabInit, |
| 160259 | +#endif |
| 160260 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 160261 | + sqlite3VdbeBytecodeVtabInit, |
| 160262 | +#endif |
| 160263 | +}; |
| 159269 | 160264 | |
| 159270 | 160265 | #ifndef SQLITE_AMALGAMATION |
| 159271 | 160266 | /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant |
| 159272 | 160267 | ** contains the text of SQLITE_VERSION macro. |
| 159273 | 160268 | */ |
| | @@ -159480,10 +160475,11 @@ |
| 159480 | 160475 | } |
| 159481 | 160476 | #endif |
| 159482 | 160477 | if( rc==SQLITE_OK ){ |
| 159483 | 160478 | sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
| 159484 | 160479 | sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
| 160480 | + sqlite3MemoryBarrier(); |
| 159485 | 160481 | sqlite3GlobalConfig.isInit = 1; |
| 159486 | 160482 | #ifdef SQLITE_EXTRA_INIT |
| 159487 | 160483 | bRunExtraInit = 1; |
| 159488 | 160484 | #endif |
| 159489 | 160485 | } |
| | @@ -160781,12 +161777,11 @@ |
| 160781 | 161777 | ** Return non-zero to retry the lock. Return zero to stop trying |
| 160782 | 161778 | ** and cause SQLite to return SQLITE_BUSY. |
| 160783 | 161779 | */ |
| 160784 | 161780 | static int sqliteDefaultBusyCallback( |
| 160785 | 161781 | void *ptr, /* Database connection */ |
| 160786 | | - int count, /* Number of times table has been busy */ |
| 160787 | | - sqlite3_file *pFile /* The file on which the lock occurred */ |
| 161782 | + int count /* Number of times table has been busy */ |
| 160788 | 161783 | ){ |
| 160789 | 161784 | #if SQLITE_OS_WIN || HAVE_USLEEP |
| 160790 | 161785 | /* This case is for systems that have support for sleeping for fractions of |
| 160791 | 161786 | ** a second. Examples: All windows systems, unix systems with usleep() */ |
| 160792 | 161787 | static const u8 delays[] = |
| | @@ -160796,35 +161791,10 @@ |
| 160796 | 161791 | # define NDELAY ArraySize(delays) |
| 160797 | 161792 | sqlite3 *db = (sqlite3 *)ptr; |
| 160798 | 161793 | int tmout = db->busyTimeout; |
| 160799 | 161794 | int delay, prior; |
| 160800 | 161795 | |
| 160801 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 160802 | | - if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){ |
| 160803 | | - if( count ){ |
| 160804 | | - /* If this is the second or later invocation of the busy-handler, |
| 160805 | | - ** but tmout==0, then code in wal.c must have disabled the blocking |
| 160806 | | - ** lock before the SQLITE_BUSY error was hit. In this case, no delay |
| 160807 | | - ** occurred while waiting for the lock, so fall through to the xSleep() |
| 160808 | | - ** code below to delay a while before retrying the lock. |
| 160809 | | - ** |
| 160810 | | - ** Alternatively, if tmout!=0, then SQLite has already waited |
| 160811 | | - ** sqlite3.busyTimeout ms for a lock. In this case, return 0 to |
| 160812 | | - ** indicate that the lock should not be retried and the SQLITE_BUSY |
| 160813 | | - ** error returned to the application. */ |
| 160814 | | - if( tmout ){ |
| 160815 | | - tmout = 0; |
| 160816 | | - sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout); |
| 160817 | | - return 0; |
| 160818 | | - } |
| 160819 | | - }else{ |
| 160820 | | - return 1; |
| 160821 | | - } |
| 160822 | | - } |
| 160823 | | -#else |
| 160824 | | - UNUSED_PARAMETER(pFile); |
| 160825 | | -#endif |
| 160826 | 161796 | assert( count>=0 ); |
| 160827 | 161797 | if( count < NDELAY ){ |
| 160828 | 161798 | delay = delays[count]; |
| 160829 | 161799 | prior = totals[count]; |
| 160830 | 161800 | }else{ |
| | @@ -160840,11 +161810,10 @@ |
| 160840 | 161810 | #else |
| 160841 | 161811 | /* This case for unix systems that lack usleep() support. Sleeping |
| 160842 | 161812 | ** must be done in increments of whole seconds */ |
| 160843 | 161813 | sqlite3 *db = (sqlite3 *)ptr; |
| 160844 | 161814 | int tmout = ((sqlite3 *)ptr)->busyTimeout; |
| 160845 | | - UNUSED_PARAMETER(pFile); |
| 160846 | 161815 | if( (count+1)*1000 > tmout ){ |
| 160847 | 161816 | return 0; |
| 160848 | 161817 | } |
| 160849 | 161818 | sqlite3OsSleep(db->pVfs, 1000000); |
| 160850 | 161819 | return 1; |
| | @@ -160858,23 +161827,14 @@ |
| 160858 | 161827 | ** lock on VFS file pFile. |
| 160859 | 161828 | ** |
| 160860 | 161829 | ** If this routine returns non-zero, the lock is retried. If it |
| 160861 | 161830 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 160862 | 161831 | */ |
| 160863 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){ |
| 161832 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 160864 | 161833 | int rc; |
| 160865 | 161834 | if( p->xBusyHandler==0 || p->nBusy<0 ) return 0; |
| 160866 | | - if( p->bExtraFileArg ){ |
| 160867 | | - /* Add an extra parameter with the pFile pointer to the end of the |
| 160868 | | - ** callback argument list */ |
| 160869 | | - int (*xTra)(void*,int,sqlite3_file*); |
| 160870 | | - xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler; |
| 160871 | | - rc = xTra(p->pBusyArg, p->nBusy, pFile); |
| 160872 | | - }else{ |
| 160873 | | - /* Legacy style busy handler callback */ |
| 160874 | | - rc = p->xBusyHandler(p->pBusyArg, p->nBusy); |
| 160875 | | - } |
| 161835 | + rc = p->xBusyHandler(p->pBusyArg, p->nBusy); |
| 160876 | 161836 | if( rc==0 ){ |
| 160877 | 161837 | p->nBusy = -1; |
| 160878 | 161838 | }else{ |
| 160879 | 161839 | p->nBusy++; |
| 160880 | 161840 | } |
| | @@ -160895,11 +161855,10 @@ |
| 160895 | 161855 | #endif |
| 160896 | 161856 | sqlite3_mutex_enter(db->mutex); |
| 160897 | 161857 | db->busyHandler.xBusyHandler = xBusy; |
| 160898 | 161858 | db->busyHandler.pBusyArg = pArg; |
| 160899 | 161859 | db->busyHandler.nBusy = 0; |
| 160900 | | - db->busyHandler.bExtraFileArg = 0; |
| 160901 | 161860 | db->busyTimeout = 0; |
| 160902 | 161861 | sqlite3_mutex_leave(db->mutex); |
| 160903 | 161862 | return SQLITE_OK; |
| 160904 | 161863 | } |
| 160905 | 161864 | |
| | @@ -160946,11 +161905,10 @@ |
| 160946 | 161905 | #endif |
| 160947 | 161906 | if( ms>0 ){ |
| 160948 | 161907 | sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback, |
| 160949 | 161908 | (void*)db); |
| 160950 | 161909 | db->busyTimeout = ms; |
| 160951 | | - db->busyHandler.bExtraFileArg = 1; |
| 160952 | 161910 | }else{ |
| 160953 | 161911 | sqlite3_busy_handler(db, 0, 0); |
| 160954 | 161912 | } |
| 160955 | 161913 | return SQLITE_OK; |
| 160956 | 161914 | } |
| | @@ -162272,10 +163230,11 @@ |
| 162272 | 163230 | sqlite3 *db; /* Store allocated handle here */ |
| 162273 | 163231 | int rc; /* Return code */ |
| 162274 | 163232 | int isThreadsafe; /* True for threadsafe connections */ |
| 162275 | 163233 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 162276 | 163234 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 163235 | + int i; /* Loop counter */ |
| 162277 | 163236 | |
| 162278 | 163237 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 162279 | 163238 | if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 162280 | 163239 | #endif |
| 162281 | 163240 | *ppDb = 0; |
| | @@ -162420,10 +163379,13 @@ |
| 162420 | 163379 | | SQLITE_EnableQPSG |
| 162421 | 163380 | #endif |
| 162422 | 163381 | #if defined(SQLITE_DEFAULT_DEFENSIVE) |
| 162423 | 163382 | | SQLITE_Defensive |
| 162424 | 163383 | #endif |
| 163384 | +#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE) |
| 163385 | + | SQLITE_LegacyAlter |
| 163386 | +#endif |
| 162425 | 163387 | ; |
| 162426 | 163388 | sqlite3HashInit(&db->aCollSeq); |
| 162427 | 163389 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 162428 | 163390 | sqlite3HashInit(&db->aModule); |
| 162429 | 163391 | #endif |
| | @@ -162462,11 +163424,11 @@ |
| 162462 | 163424 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 162463 | 163425 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 162464 | 163426 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 162465 | 163427 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 162466 | 163428 | if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 162467 | | - rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 163429 | + rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */ |
| 162468 | 163430 | }else{ |
| 162469 | 163431 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 162470 | 163432 | } |
| 162471 | 163433 | if( rc!=SQLITE_OK ){ |
| 162472 | 163434 | if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
| | @@ -162512,18 +163474,15 @@ |
| 162512 | 163474 | */ |
| 162513 | 163475 | sqlite3Error(db, SQLITE_OK); |
| 162514 | 163476 | sqlite3RegisterPerConnectionBuiltinFunctions(db); |
| 162515 | 163477 | rc = sqlite3_errcode(db); |
| 162516 | 163478 | |
| 162517 | | -#ifdef SQLITE_ENABLE_FTS5 |
| 162518 | | - /* Register any built-in FTS5 module before loading the automatic |
| 162519 | | - ** extensions. This allows automatic extensions to register FTS5 |
| 162520 | | - ** tokenizers and auxiliary functions. */ |
| 162521 | | - if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 162522 | | - rc = sqlite3Fts5Init(db); |
| 162523 | | - } |
| 162524 | | -#endif |
| 163479 | + |
| 163480 | + /* Load compiled-in extensions */ |
| 163481 | + for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){ |
| 163482 | + rc = sqlite3BuiltinExtensions[i](db); |
| 163483 | + } |
| 162525 | 163484 | |
| 162526 | 163485 | /* Load automatic extensions - extensions that have been registered |
| 162527 | 163486 | ** using the sqlite3_automatic_extension() API. |
| 162528 | 163487 | */ |
| 162529 | 163488 | if( rc==SQLITE_OK ){ |
| | @@ -162532,66 +163491,10 @@ |
| 162532 | 163491 | if( rc!=SQLITE_OK ){ |
| 162533 | 163492 | goto opendb_out; |
| 162534 | 163493 | } |
| 162535 | 163494 | } |
| 162536 | 163495 | |
| 162537 | | -#ifdef SQLITE_ENABLE_FTS1 |
| 162538 | | - if( !db->mallocFailed ){ |
| 162539 | | - extern int sqlite3Fts1Init(sqlite3*); |
| 162540 | | - rc = sqlite3Fts1Init(db); |
| 162541 | | - } |
| 162542 | | -#endif |
| 162543 | | - |
| 162544 | | -#ifdef SQLITE_ENABLE_FTS2 |
| 162545 | | - if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 162546 | | - extern int sqlite3Fts2Init(sqlite3*); |
| 162547 | | - rc = sqlite3Fts2Init(db); |
| 162548 | | - } |
| 162549 | | -#endif |
| 162550 | | - |
| 162551 | | -#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */ |
| 162552 | | - if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 162553 | | - rc = sqlite3Fts3Init(db); |
| 162554 | | - } |
| 162555 | | -#endif |
| 162556 | | - |
| 162557 | | -#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) |
| 162558 | | - if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 162559 | | - rc = sqlite3IcuInit(db); |
| 162560 | | - } |
| 162561 | | -#endif |
| 162562 | | - |
| 162563 | | -#ifdef SQLITE_ENABLE_RTREE |
| 162564 | | - if( !db->mallocFailed && rc==SQLITE_OK){ |
| 162565 | | - rc = sqlite3RtreeInit(db); |
| 162566 | | - } |
| 162567 | | -#endif |
| 162568 | | - |
| 162569 | | -#ifdef SQLITE_ENABLE_DBPAGE_VTAB |
| 162570 | | - if( !db->mallocFailed && rc==SQLITE_OK){ |
| 162571 | | - rc = sqlite3DbpageRegister(db); |
| 162572 | | - } |
| 162573 | | -#endif |
| 162574 | | - |
| 162575 | | -#ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 162576 | | - if( !db->mallocFailed && rc==SQLITE_OK){ |
| 162577 | | - rc = sqlite3DbstatRegister(db); |
| 162578 | | - } |
| 162579 | | -#endif |
| 162580 | | - |
| 162581 | | -#ifdef SQLITE_ENABLE_JSON1 |
| 162582 | | - if( !db->mallocFailed && rc==SQLITE_OK){ |
| 162583 | | - rc = sqlite3Json1Init(db); |
| 162584 | | - } |
| 162585 | | -#endif |
| 162586 | | - |
| 162587 | | -#ifdef SQLITE_ENABLE_STMTVTAB |
| 162588 | | - if( !db->mallocFailed && rc==SQLITE_OK){ |
| 162589 | | - rc = sqlite3StmtVtabInit(db); |
| 162590 | | - } |
| 162591 | | -#endif |
| 162592 | | - |
| 162593 | 163496 | #ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS |
| 162594 | 163497 | /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time |
| 162595 | 163498 | ** option gives access to internal functions by default. |
| 162596 | 163499 | ** Testing use only!!! */ |
| 162597 | 163500 | db->mDbFlags |= DBFLAG_InternalFunc; |
| | @@ -163076,11 +163979,11 @@ |
| 163076 | 163979 | *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); |
| 163077 | 163980 | rc = SQLITE_OK; |
| 163078 | 163981 | }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){ |
| 163079 | 163982 | int iNew = *(int*)pArg; |
| 163080 | 163983 | *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree); |
| 163081 | | - if( iNew>=0 && iNew<=254 ){ |
| 163984 | + if( iNew>=0 && iNew<=255 ){ |
| 163082 | 163985 | sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0); |
| 163083 | 163986 | } |
| 163084 | 163987 | rc = SQLITE_OK; |
| 163085 | 163988 | }else{ |
| 163086 | 163989 | rc = sqlite3OsFileControl(fd, op, pArg); |
| | @@ -165357,10 +166260,11 @@ |
| 165357 | 166260 | SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*); |
| 165358 | 166261 | SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); |
| 165359 | 166262 | SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); |
| 165360 | 166263 | SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*); |
| 165361 | 166264 | SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); |
| 166265 | +SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut); |
| 165362 | 166266 | |
| 165363 | 166267 | /* fts3_tokenizer.c */ |
| 165364 | 166268 | SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); |
| 165365 | 166269 | SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); |
| 165366 | 166270 | SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, |
| | @@ -166088,10 +166992,26 @@ |
| 166088 | 166992 | fts3Appendf(pRc, &zRet, ", ?"); |
| 166089 | 166993 | } |
| 166090 | 166994 | sqlite3_free(zFree); |
| 166091 | 166995 | return zRet; |
| 166092 | 166996 | } |
| 166997 | + |
| 166998 | +/* |
| 166999 | +** Buffer z contains a positive integer value encoded as utf-8 text. |
| 167000 | +** Decode this value and store it in *pnOut, returning the number of bytes |
| 167001 | +** consumed. If an overflow error occurs return a negative value. |
| 167002 | +*/ |
| 167003 | +SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){ |
| 167004 | + u64 iVal = 0; |
| 167005 | + int i; |
| 167006 | + for(i=0; z[i]>='0' && z[i]<='9'; i++){ |
| 167007 | + iVal = iVal*10 + (z[i] - '0'); |
| 167008 | + if( iVal>0x7FFFFFFF ) return -1; |
| 167009 | + } |
| 167010 | + *pnOut = (int)iVal; |
| 167011 | + return i; |
| 167012 | +} |
| 166093 | 167013 | |
| 166094 | 167014 | /* |
| 166095 | 167015 | ** This function interprets the string at (*pp) as a non-negative integer |
| 166096 | 167016 | ** value. It reads the integer and sets *pnOut to the value read, then |
| 166097 | 167017 | ** sets *pp to point to the byte immediately following the last byte of |
| | @@ -166104,23 +167024,21 @@ |
| 166104 | 167024 | ** |
| 166105 | 167025 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 166106 | 167026 | */ |
| 166107 | 167027 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 166108 | 167028 | const int MAX_NPREFIX = 10000000; |
| 166109 | | - const char *p; /* Iterator pointer */ |
| 166110 | 167029 | int nInt = 0; /* Output value */ |
| 166111 | | - |
| 166112 | | - for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 166113 | | - nInt = nInt * 10 + (p[0] - '0'); |
| 166114 | | - if( nInt>MAX_NPREFIX ){ |
| 166115 | | - nInt = 0; |
| 166116 | | - break; |
| 166117 | | - } |
| 166118 | | - } |
| 166119 | | - if( p==*pp ) return SQLITE_ERROR; |
| 167030 | + int nByte; |
| 167031 | + nByte = sqlite3Fts3ReadInt(*pp, &nInt); |
| 167032 | + if( nInt>MAX_NPREFIX ){ |
| 167033 | + nInt = 0; |
| 167034 | + } |
| 167035 | + if( nByte==0 ){ |
| 167036 | + return SQLITE_ERROR; |
| 167037 | + } |
| 166120 | 167038 | *pnOut = nInt; |
| 166121 | | - *pp = p; |
| 167039 | + *pp += nByte; |
| 166122 | 167040 | return SQLITE_OK; |
| 166123 | 167041 | } |
| 166124 | 167042 | |
| 166125 | 167043 | /* |
| 166126 | 167044 | ** This function is called to allocate an array of Fts3Index structures |
| | @@ -167298,11 +168216,13 @@ |
| 167298 | 168216 | static void fts3ReadNextPos( |
| 167299 | 168217 | char **pp, /* IN/OUT: Pointer into position-list buffer */ |
| 167300 | 168218 | sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ |
| 167301 | 168219 | ){ |
| 167302 | 168220 | if( (**pp)&0xFE ){ |
| 167303 | | - fts3GetDeltaVarint(pp, pi); |
| 168221 | + int iVal; |
| 168222 | + *pp += fts3GetVarint32((*pp), &iVal); |
| 168223 | + *pi += iVal; |
| 167304 | 168224 | *pi -= 2; |
| 167305 | 168225 | }else{ |
| 167306 | 168226 | *pi = POSITION_LIST_END; |
| 167307 | 168227 | } |
| 167308 | 168228 | } |
| | @@ -170428,10 +171348,11 @@ |
| 170428 | 171348 | while( *pRc==SQLITE_OK && pLeft->bEof==0 ){ |
| 170429 | 171349 | memset(pDl->pList, 0, pDl->nList); |
| 170430 | 171350 | fts3EvalNextRow(pCsr, pLeft, pRc); |
| 170431 | 171351 | } |
| 170432 | 171352 | } |
| 171353 | + pRight->bEof = pLeft->bEof = 1; |
| 170433 | 171354 | } |
| 170434 | 171355 | } |
| 170435 | 171356 | break; |
| 170436 | 171357 | } |
| 170437 | 171358 | |
| | @@ -172198,14 +173119,11 @@ |
| 172198 | 173119 | |
| 172199 | 173120 | /* If this is a "NEAR" keyword, check for an explicit nearness. */ |
| 172200 | 173121 | if( pKey->eType==FTSQUERY_NEAR ){ |
| 172201 | 173122 | assert( nKey==4 ); |
| 172202 | 173123 | if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){ |
| 172203 | | - nNear = 0; |
| 172204 | | - for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){ |
| 172205 | | - nNear = nNear * 10 + (zInput[nKey] - '0'); |
| 172206 | | - } |
| 173124 | + nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear); |
| 172207 | 173125 | } |
| 172208 | 173126 | } |
| 172209 | 173127 | |
| 172210 | 173128 | /* At this point this is probably a keyword. But for that to be true, |
| 172211 | 173129 | ** the next byte must contain either whitespace, an open or close |
| | @@ -178384,25 +179302,25 @@ |
| 178384 | 179302 | ){ |
| 178385 | 179303 | const unsigned char *zText = sqlite3_column_text(pStmt, iCol); |
| 178386 | 179304 | if( zText ){ |
| 178387 | 179305 | int i; |
| 178388 | 179306 | int iMul = 1; |
| 178389 | | - i64 iVal = 0; |
| 179307 | + u64 iVal = 0; |
| 178390 | 179308 | for(i=0; zText[i]>='0' && zText[i]<='9'; i++){ |
| 178391 | 179309 | iVal = iVal*10 + (zText[i] - '0'); |
| 178392 | 179310 | } |
| 178393 | | - *piEndBlock = iVal; |
| 179311 | + *piEndBlock = (i64)iVal; |
| 178394 | 179312 | while( zText[i]==' ' ) i++; |
| 178395 | 179313 | iVal = 0; |
| 178396 | 179314 | if( zText[i]=='-' ){ |
| 178397 | 179315 | i++; |
| 178398 | 179316 | iMul = -1; |
| 178399 | 179317 | } |
| 178400 | 179318 | for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){ |
| 178401 | 179319 | iVal = iVal*10 + (zText[i] - '0'); |
| 178402 | 179320 | } |
| 178403 | | - *pnByte = (iVal * (i64)iMul); |
| 179321 | + *pnByte = ((i64)iVal * (i64)iMul); |
| 178404 | 179322 | } |
| 178405 | 179323 | } |
| 178406 | 179324 | |
| 178407 | 179325 | |
| 178408 | 179326 | /* |
| | @@ -181976,11 +182894,11 @@ |
| 181976 | 182894 | iStart = pExpr->iPhrase * p->nCol; |
| 181977 | 182895 | }else{ |
| 181978 | 182896 | iStart = pExpr->iPhrase * ((p->nCol + 31) / 32); |
| 181979 | 182897 | } |
| 181980 | 182898 | |
| 181981 | | - while( 1 ){ |
| 182899 | + if( pIter ) while( 1 ){ |
| 181982 | 182900 | int nHit = fts3ColumnlistCount(&pIter); |
| 181983 | 182901 | if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ |
| 181984 | 182902 | if( p->flag==FTS3_MATCHINFO_LHITS ){ |
| 181985 | 182903 | p->aMatchinfo[iStart + iCol] = (u32)nHit; |
| 181986 | 182904 | }else if( nHit ){ |
| | @@ -183890,10 +184808,11 @@ |
| 183890 | 184808 | } |
| 183891 | 184809 | |
| 183892 | 184810 | /* Append N bytes from zIn onto the end of the JsonString string. |
| 183893 | 184811 | */ |
| 183894 | 184812 | static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){ |
| 184813 | + if( N==0 ) return; |
| 183895 | 184814 | if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; |
| 183896 | 184815 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 183897 | 184816 | p->nUsed += N; |
| 183898 | 184817 | } |
| 183899 | 184818 | |
| | @@ -223901,11 +224820,11 @@ |
| 223901 | 224820 | int nArg, /* Number of args */ |
| 223902 | 224821 | sqlite3_value **apUnused /* Function arguments */ |
| 223903 | 224822 | ){ |
| 223904 | 224823 | assert( nArg==0 ); |
| 223905 | 224824 | UNUSED_PARAM2(nArg, apUnused); |
| 223906 | | - sqlite3_result_text(pCtx, "fts5: 2020-03-03 20:04:29 bd94d7d052734460904c687756231f8aa243a2252f07f742dd1e437aa940f536", -1, SQLITE_TRANSIENT); |
| 224825 | + sqlite3_result_text(pCtx, "fts5: 2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba83350", -1, SQLITE_TRANSIENT); |
| 223907 | 224826 | } |
| 223908 | 224827 | |
| 223909 | 224828 | /* |
| 223910 | 224829 | ** Return true if zName is the extension on one of the shadow tables used |
| 223911 | 224830 | ** by this module. |
| | @@ -228552,11 +229471,12 @@ |
| 228552 | 229471 | } |
| 228553 | 229472 | case STMT_COLUMN_BUSY: { |
| 228554 | 229473 | sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt)); |
| 228555 | 229474 | break; |
| 228556 | 229475 | } |
| 228557 | | - case STMT_COLUMN_MEM: { |
| 229476 | + default: { |
| 229477 | + assert( i==STMT_COLUMN_MEM ); |
| 228558 | 229478 | i = SQLITE_STMTSTATUS_MEMUSED + |
| 228559 | 229479 | STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 228560 | 229480 | /* Fall thru */ |
| 228561 | 229481 | } |
| 228562 | 229482 | case STMT_COLUMN_NSCAN: |
| | @@ -228683,12 +229603,12 @@ |
| 228683 | 229603 | } |
| 228684 | 229604 | #endif /* SQLITE_CORE */ |
| 228685 | 229605 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 228686 | 229606 | |
| 228687 | 229607 | /************** End of stmt.c ************************************************/ |
| 228688 | | -#if __LINE__!=228688 |
| 229608 | +#if __LINE__!=229608 |
| 228689 | 229609 | #undef SQLITE_SOURCE_ID |
| 228690 | | -#define SQLITE_SOURCE_ID "2020-04-20 17:35:32 2fc80ef16ce5878311ab88a0c64631813572ffbb71f75363b4619c9667e0alt2" |
| 229610 | +#define SQLITE_SOURCE_ID "2020-05-25 16:19:56 0c1fcf4711a2e66c813aed38cf41cd3e2123ee8eb6db98118086764c4ba8alt2" |
| 228691 | 229611 | #endif |
| 228692 | 229612 | /* Return the source-id for this library */ |
| 228693 | 229613 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 228694 | 229614 | /************************** End of sqlite3.c ******************************/ |
| 228695 | 229615 | |