| | @@ -216,10 +216,13 @@ |
| 216 | 216 | "ENABLE_ATOMIC_WRITE", |
| 217 | 217 | #endif |
| 218 | 218 | #if SQLITE_ENABLE_BATCH_ATOMIC_WRITE |
| 219 | 219 | "ENABLE_BATCH_ATOMIC_WRITE", |
| 220 | 220 | #endif |
| 221 | +#if SQLITE_ENABLE_BYTECODE_VTAB |
| 222 | + "ENABLE_BYTECODE_VTAB", |
| 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 |
| 225 | 228 | "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", |
| | @@ -1162,11 +1162,11 @@ |
| 1162 | 1162 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1163 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1164 | */ |
| 1165 | 1165 | #define SQLITE_VERSION "3.32.0" |
| 1166 | 1166 | #define SQLITE_VERSION_NUMBER 3032000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2020-04-20 17:35:32 2fc80ef16ce5878311ab88a0c64631813572ffbb71f75363b4619c9667e0926b" |
| 1167 | +#define SQLITE_SOURCE_ID "2020-05-08 19:02:21 3a16c0ce4d8851f79f670d94786032c8007619154ece44647dc9cc5b1f9654ff" |
| 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 |
| | @@ -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 */ |
| | @@ -19900,11 +19939,11 @@ |
| 19900 | 19939 | SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*); |
| 19901 | 19940 | SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*); |
| 19902 | 19941 | SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*); |
| 19903 | 19942 | SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*); |
| 19904 | 19943 | SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); |
| 19905 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*); |
| 19944 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); |
| 19906 | 19945 | SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); |
| 19907 | 19946 | SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); |
| 19908 | 19947 | SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); |
| 19909 | 19948 | SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); |
| 19910 | 19949 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); |
| | @@ -20636,11 +20675,12 @@ |
| 20636 | 20675 | /* |
| 20637 | 20676 | ** VDBE_DISPLAY_P4 is true or false depending on whether or not the |
| 20638 | 20677 | ** "explain" P4 display logic is enabled. |
| 20639 | 20678 | */ |
| 20640 | 20679 | #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
| 20641 | | - || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
| 20680 | + || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) \ |
| 20681 | + || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 20642 | 20682 | # define VDBE_DISPLAY_P4 1 |
| 20643 | 20683 | #else |
| 20644 | 20684 | # define VDBE_DISPLAY_P4 0 |
| 20645 | 20685 | #endif |
| 20646 | 20686 | |
| | @@ -21101,11 +21141,18 @@ |
| 21101 | 21141 | |
| 21102 | 21142 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 21103 | 21143 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*); |
| 21104 | 21144 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*); |
| 21105 | 21145 | SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 21106 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 21146 | +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 21147 | +SQLITE_PRIVATE int sqlite3VdbeNextOpcode(Vdbe*,Mem*,int,int*,int*,Op**); |
| 21148 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3*,Op*); |
| 21149 | +#endif |
| 21150 | +#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) |
| 21151 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayComment(sqlite3*,const Op*,const char*); |
| 21152 | +#endif |
| 21153 | +#if !defined(SQLITE_OMIT_EXPLAIN) |
| 21107 | 21154 | SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 21108 | 21155 | #endif |
| 21109 | 21156 | SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| 21110 | 21157 | SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); |
| 21111 | 21158 | SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); |
| | @@ -21143,11 +21190,11 @@ |
| 21143 | 21190 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 21144 | 21191 | SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 21145 | 21192 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 21146 | 21193 | SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*); |
| 21147 | 21194 | #endif |
| 21148 | | -#ifndef SQLITE_OMIT_EXPLAIN |
| 21195 | +#if !defined(SQLITE_OMIT_EXPLAIN) || defined(SQLITE_ENABLE_BYTECODE_VTAB) |
| 21149 | 21196 | SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 21150 | 21197 | #endif |
| 21151 | 21198 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 21152 | 21199 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n); |
| 21153 | 21200 | SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| | @@ -27316,11 +27363,11 @@ |
| 27316 | 27363 | if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){ |
| 27317 | 27364 | n = mem0.hardLimit; |
| 27318 | 27365 | } |
| 27319 | 27366 | mem0.alarmThreshold = n; |
| 27320 | 27367 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27321 | | - mem0.nearlyFull = (n>0 && n<=nUsed); |
| 27368 | + AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed); |
| 27322 | 27369 | sqlite3_mutex_leave(mem0.mutex); |
| 27323 | 27370 | excess = sqlite3_memory_used() - n; |
| 27324 | 27371 | if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); |
| 27325 | 27372 | return priorLimit; |
| 27326 | 27373 | } |
| | @@ -27384,11 +27431,11 @@ |
| 27384 | 27431 | ** Return true if the heap is currently under memory pressure - in other |
| 27385 | 27432 | ** words if the amount of heap used is close to the limit set by |
| 27386 | 27433 | ** sqlite3_soft_heap_limit(). |
| 27387 | 27434 | */ |
| 27388 | 27435 | SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ |
| 27389 | | - return mem0.nearlyFull; |
| 27436 | + return AtomicLoad(&mem0.nearlyFull); |
| 27390 | 27437 | } |
| 27391 | 27438 | |
| 27392 | 27439 | /* |
| 27393 | 27440 | ** Deinitialize the memory allocation subsystem. |
| 27394 | 27441 | */ |
| | @@ -27448,21 +27495,21 @@ |
| 27448 | 27495 | |
| 27449 | 27496 | sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); |
| 27450 | 27497 | if( mem0.alarmThreshold>0 ){ |
| 27451 | 27498 | sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27452 | 27499 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 27453 | | - mem0.nearlyFull = 1; |
| 27500 | + AtomicStore(&mem0.nearlyFull, 1); |
| 27454 | 27501 | sqlite3MallocAlarm(nFull); |
| 27455 | 27502 | if( mem0.hardLimit ){ |
| 27456 | 27503 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 27457 | 27504 | if( nUsed >= mem0.hardLimit - nFull ){ |
| 27458 | 27505 | *pp = 0; |
| 27459 | 27506 | return; |
| 27460 | 27507 | } |
| 27461 | 27508 | } |
| 27462 | 27509 | }else{ |
| 27463 | | - mem0.nearlyFull = 0; |
| 27510 | + AtomicStore(&mem0.nearlyFull, 0); |
| 27464 | 27511 | } |
| 27465 | 27512 | } |
| 27466 | 27513 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 27467 | 27514 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27468 | 27515 | if( p==0 && mem0.alarmThreshold>0 ){ |
| | @@ -27687,14 +27734,16 @@ |
| 27687 | 27734 | if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 27688 | 27735 | mem0.alarmThreshold-nDiff ){ |
| 27689 | 27736 | sqlite3MallocAlarm(nDiff); |
| 27690 | 27737 | } |
| 27691 | 27738 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27739 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27692 | 27740 | if( pNew==0 && mem0.alarmThreshold>0 ){ |
| 27693 | 27741 | sqlite3MallocAlarm((int)nBytes); |
| 27694 | 27742 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 27695 | 27743 | } |
| 27744 | +#endif |
| 27696 | 27745 | if( pNew ){ |
| 27697 | 27746 | nNew = sqlite3MallocSize(pNew); |
| 27698 | 27747 | sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 27699 | 27748 | } |
| 27700 | 27749 | sqlite3_mutex_leave(mem0.mutex); |
| | @@ -34928,20 +34977,21 @@ |
| 34928 | 34977 | static int osSetPosixAdvisoryLock( |
| 34929 | 34978 | int h, /* The file descriptor on which to take the lock */ |
| 34930 | 34979 | struct flock *pLock, /* The description of the lock */ |
| 34931 | 34980 | unixFile *pFile /* Structure holding timeout value */ |
| 34932 | 34981 | ){ |
| 34982 | + int tm = pFile->iBusyTimeout; |
| 34933 | 34983 | int rc = osFcntl(h,F_SETLK,pLock); |
| 34934 | | - while( rc<0 && pFile->iBusyTimeout>0 ){ |
| 34984 | + while( rc<0 && tm>0 ){ |
| 34935 | 34985 | /* On systems that support some kind of blocking file lock with a timeout, |
| 34936 | 34986 | ** make appropriate changes here to invoke that blocking file lock. On |
| 34937 | 34987 | ** generic posix, however, there is no such API. So we simply try the |
| 34938 | 34988 | ** lock once every millisecond until either the timeout expires, or until |
| 34939 | 34989 | ** the lock is obtained. */ |
| 34940 | 34990 | usleep(1000); |
| 34941 | 34991 | rc = osFcntl(h,F_SETLK,pLock); |
| 34942 | | - pFile->iBusyTimeout--; |
| 34992 | + tm--; |
| 34943 | 34993 | } |
| 34944 | 34994 | return rc; |
| 34945 | 34995 | } |
| 34946 | 34996 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 34947 | 34997 | |
| | @@ -37679,17 +37729,24 @@ |
| 37679 | 37729 | |
| 37680 | 37730 | /* Locks are within range */ |
| 37681 | 37731 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 37682 | 37732 | |
| 37683 | 37733 | if( pShmNode->hShm>=0 ){ |
| 37734 | + int res; |
| 37684 | 37735 | /* Initialize the locking parameters */ |
| 37685 | 37736 | f.l_type = lockType; |
| 37686 | 37737 | f.l_whence = SEEK_SET; |
| 37687 | 37738 | f.l_start = ofst; |
| 37688 | 37739 | f.l_len = n; |
| 37689 | | - rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37690 | | - rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 37740 | + res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 37741 | + if( res==-1 ){ |
| 37742 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 37743 | + rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); |
| 37744 | +#else |
| 37745 | + rc = SQLITE_BUSY; |
| 37746 | +#endif |
| 37747 | + } |
| 37691 | 37748 | } |
| 37692 | 37749 | |
| 37693 | 37750 | /* Update the global lock state and do debug tracing */ |
| 37694 | 37751 | #ifdef SQLITE_DEBUG |
| 37695 | 37752 | { u16 mask; |
| | @@ -38182,26 +38239,27 @@ |
| 38182 | 38239 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 38183 | 38240 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 38184 | 38241 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 38185 | 38242 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
| 38186 | 38243 | |
| 38187 | | - /* Check that, if this to be a blocking lock, that locks have been |
| 38188 | | - ** obtained in the following order. |
| 38244 | + /* Check that, if this to be a blocking lock, no locks that occur later |
| 38245 | + ** in the following list than the lock being obtained are already held: |
| 38189 | 38246 | ** |
| 38190 | 38247 | ** 1. Checkpointer lock (ofst==1). |
| 38191 | | - ** 2. Recover lock (ofst==2). |
| 38248 | + ** 2. Write lock (ofst==0). |
| 38192 | 38249 | ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK). |
| 38193 | | - ** 4. Write lock (ofst==0). |
| 38194 | 38250 | ** |
| 38195 | 38251 | ** In other words, if this is a blocking lock, none of the locks that |
| 38196 | 38252 | ** occur later in the above list than the lock being obtained may be |
| 38197 | 38253 | ** held. */ |
| 38198 | 38254 | #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 | | - ); |
| 38255 | + assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 38256 | + (ofst!=2) /* not RECOVER */ |
| 38257 | + && (ofst!=1 || (p->exclMask|p->sharedMask)==0) |
| 38258 | + && (ofst!=0 || (p->exclMask|p->sharedMask)<3) |
| 38259 | + && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst)) |
| 38260 | + )); |
| 38203 | 38261 | #endif |
| 38204 | 38262 | |
| 38205 | 38263 | mask = (1<<(ofst+n)) - (1<<ofst); |
| 38206 | 38264 | assert( n>1 || mask==(1<<ofst) ); |
| 38207 | 38265 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
| | @@ -51505,10 +51563,15 @@ |
| 51505 | 51563 | SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal); |
| 51506 | 51564 | #endif |
| 51507 | 51565 | |
| 51508 | 51566 | /* Return the sqlite3_file object for the WAL file */ |
| 51509 | 51567 | SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal); |
| 51568 | + |
| 51569 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 51570 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock); |
| 51571 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db); |
| 51572 | +#endif |
| 51510 | 51573 | |
| 51511 | 51574 | #endif /* ifndef SQLITE_OMIT_WAL */ |
| 51512 | 51575 | #endif /* SQLITE_WAL_H */ |
| 51513 | 51576 | |
| 51514 | 51577 | /************** End of wal.h *************************************************/ |
| | @@ -54026,13 +54089,16 @@ |
| 54026 | 54089 | } |
| 54027 | 54090 | if( exists ){ |
| 54028 | 54091 | /* One of the journals pointed to by the master journal exists. |
| 54029 | 54092 | ** Open it and check if it points at the master journal. If |
| 54030 | 54093 | ** so, return without deleting the master journal file. |
| 54094 | + ** NB: zJournal is really a MAIN_JOURNAL. But call it a |
| 54095 | + ** MASTER_JOURNAL here so that the VFS will not send the zJournal |
| 54096 | + ** name into sqlite3_database_file_object(). |
| 54031 | 54097 | */ |
| 54032 | 54098 | int c; |
| 54033 | | - int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 54099 | + int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 54034 | 54100 | rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
| 54035 | 54101 | if( rc!=SQLITE_OK ){ |
| 54036 | 54102 | goto delmaster_out; |
| 54037 | 54103 | } |
| 54038 | 54104 | |
| | @@ -56232,10 +56298,11 @@ |
| 56232 | 56298 | ** Pager object (sizeof(Pager) bytes) |
| 56233 | 56299 | ** PCache object (sqlite3PcacheSize() bytes) |
| 56234 | 56300 | ** Database file handle (pVfs->szOsFile bytes) |
| 56235 | 56301 | ** Sub-journal file handle (journalFileSize bytes) |
| 56236 | 56302 | ** Main journal file handle (journalFileSize bytes) |
| 56303 | + ** Ptr back to the Pager (sizeof(Pager*) bytes) |
| 56237 | 56304 | ** \0\0\0\0 database prefix (4 bytes) |
| 56238 | 56305 | ** Database file name (nPathname+1 bytes) |
| 56239 | 56306 | ** URI query parameters (nUriByte bytes) |
| 56240 | 56307 | ** Journal filename (nPathname+8+1 bytes) |
| 56241 | 56308 | ** WAL filename (nPathname+4+1 bytes) |
| | @@ -56271,10 +56338,11 @@ |
| 56271 | 56338 | pPtr = (u8 *)sqlite3MallocZero( |
| 56272 | 56339 | ROUND8(sizeof(*pPager)) + /* Pager structure */ |
| 56273 | 56340 | ROUND8(pcacheSize) + /* PCache object */ |
| 56274 | 56341 | ROUND8(pVfs->szOsFile) + /* The main db file */ |
| 56275 | 56342 | journalFileSize * 2 + /* The two journal files */ |
| 56343 | + sizeof(pPager) + /* Space to hold a pointer */ |
| 56276 | 56344 | 4 + /* Database prefix */ |
| 56277 | 56345 | nPathname + 1 + /* database filename */ |
| 56278 | 56346 | nUriByte + /* query parameters */ |
| 56279 | 56347 | nPathname + 8 + 1 + /* Journal filename */ |
| 56280 | 56348 | #ifndef SQLITE_OMIT_WAL |
| | @@ -56291,10 +56359,11 @@ |
| 56291 | 56359 | pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize); |
| 56292 | 56360 | pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile); |
| 56293 | 56361 | pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; |
| 56294 | 56362 | pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize; |
| 56295 | 56363 | assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 56364 | + memcpy(pPtr, &pPager, sizeof(pPager)); pPtr += sizeof(pPager); |
| 56296 | 56365 | |
| 56297 | 56366 | /* Fill in the Pager.zFilename and pPager.zQueryParam fields */ |
| 56298 | 56367 | pPtr += 4; /* Skip zero prefix */ |
| 56299 | 56368 | pPager->zFilename = (char*)pPtr; |
| 56300 | 56369 | if( nPathname>0 ){ |
| | @@ -56491,10 +56560,23 @@ |
| 56491 | 56560 | |
| 56492 | 56561 | *ppPager = pPager; |
| 56493 | 56562 | return SQLITE_OK; |
| 56494 | 56563 | } |
| 56495 | 56564 | |
| 56565 | +/* |
| 56566 | +** Return the sqlite3_file for the main database given the name |
| 56567 | +** of the corresonding WAL or Journal name as passed into |
| 56568 | +** xOpen. |
| 56569 | +*/ |
| 56570 | +SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){ |
| 56571 | + Pager *pPager; |
| 56572 | + while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){ |
| 56573 | + zName--; |
| 56574 | + } |
| 56575 | + pPager = *(Pager**)(zName - 4 - sizeof(Pager*)); |
| 56576 | + return pPager->fd; |
| 56577 | +} |
| 56496 | 56578 | |
| 56497 | 56579 | |
| 56498 | 56580 | /* |
| 56499 | 56581 | ** This function is called after transitioning from PAGER_UNLOCK to |
| 56500 | 56582 | ** PAGER_SHARED state. It tests if there is a hot journal present in |
| | @@ -57176,11 +57258,10 @@ |
| 57176 | 57258 | Pager *pPager; |
| 57177 | 57259 | assert( pPg!=0 ); |
| 57178 | 57260 | assert( pPg->pgno==1 ); |
| 57179 | 57261 | assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */ |
| 57180 | 57262 | pPager = pPg->pPager; |
| 57181 | | - sqlite3PagerResetLockTimeout(pPager); |
| 57182 | 57263 | sqlite3PcacheRelease(pPg); |
| 57183 | 57264 | pagerUnlockIfUnused(pPager); |
| 57184 | 57265 | } |
| 57185 | 57266 | |
| 57186 | 57267 | /* |
| | @@ -58469,20 +58550,10 @@ |
| 58469 | 58550 | */ |
| 58470 | 58551 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){ |
| 58471 | 58552 | return pPager->fd; |
| 58472 | 58553 | } |
| 58473 | 58554 | |
| 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 | 58555 | /* |
| 58485 | 58556 | ** Return the file handle for the journal file (if it exists). |
| 58486 | 58557 | ** This will be either the rollback journal or the WAL file. |
| 58487 | 58558 | */ |
| 58488 | 58559 | SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){ |
| | @@ -58892,11 +58963,10 @@ |
| 58892 | 58963 | (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 58893 | 58964 | pPager->pBusyHandlerArg, |
| 58894 | 58965 | pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| 58895 | 58966 | pnLog, pnCkpt |
| 58896 | 58967 | ); |
| 58897 | | - sqlite3PagerResetLockTimeout(pPager); |
| 58898 | 58968 | } |
| 58899 | 58969 | return rc; |
| 58900 | 58970 | } |
| 58901 | 58971 | |
| 58902 | 58972 | SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){ |
| | @@ -59057,11 +59127,35 @@ |
| 59057 | 59127 | } |
| 59058 | 59128 | } |
| 59059 | 59129 | return rc; |
| 59060 | 59130 | } |
| 59061 | 59131 | |
| 59132 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59133 | +/* |
| 59134 | +** If pager pPager is a wal-mode database not in exclusive locking mode, |
| 59135 | +** invoke the sqlite3WalWriteLock() function on the associated Wal object |
| 59136 | +** with the same db and bLock parameters as were passed to this function. |
| 59137 | +** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. |
| 59138 | +*/ |
| 59139 | +SQLITE_PRIVATE int sqlite3PagerWalWriteLock(Pager *pPager, int bLock){ |
| 59140 | + int rc = SQLITE_OK; |
| 59141 | + if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){ |
| 59142 | + rc = sqlite3WalWriteLock(pPager->pWal, bLock); |
| 59143 | + } |
| 59144 | + return rc; |
| 59145 | +} |
| 59062 | 59146 | |
| 59147 | +/* |
| 59148 | +** Set the database handle used by the wal layer to determine if |
| 59149 | +** blocking locks are required. |
| 59150 | +*/ |
| 59151 | +SQLITE_PRIVATE void sqlite3PagerWalDb(Pager *pPager, sqlite3 *db){ |
| 59152 | + if( pagerUseWal(pPager) ){ |
| 59153 | + sqlite3WalDb(pPager->pWal, db); |
| 59154 | + } |
| 59155 | +} |
| 59156 | +#endif |
| 59063 | 59157 | |
| 59064 | 59158 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59065 | 59159 | /* |
| 59066 | 59160 | ** If this is a WAL database, obtain a snapshot handle for the snapshot |
| 59067 | 59161 | ** currently open. Otherwise, return an error. |
| | @@ -59077,11 +59171,14 @@ |
| 59077 | 59171 | /* |
| 59078 | 59172 | ** If this is a WAL database, store a pointer to pSnapshot. Next time a |
| 59079 | 59173 | ** read transaction is opened, attempt to read from the snapshot it |
| 59080 | 59174 | ** identifies. If this is not a WAL database, return an error. |
| 59081 | 59175 | */ |
| 59082 | | -SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ |
| 59176 | +SQLITE_PRIVATE int sqlite3PagerSnapshotOpen( |
| 59177 | + Pager *pPager, |
| 59178 | + sqlite3_snapshot *pSnapshot |
| 59179 | +){ |
| 59083 | 59180 | int rc = SQLITE_OK; |
| 59084 | 59181 | if( pPager->pWal ){ |
| 59085 | 59182 | sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); |
| 59086 | 59183 | }else{ |
| 59087 | 59184 | rc = SQLITE_ERROR; |
| | @@ -59622,10 +59719,13 @@ |
| 59622 | 59719 | u8 lockError; /* True if a locking error has occurred */ |
| 59623 | 59720 | #endif |
| 59624 | 59721 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 59625 | 59722 | WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ |
| 59626 | 59723 | #endif |
| 59724 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 59725 | + sqlite3 *db; |
| 59726 | +#endif |
| 59627 | 59727 | }; |
| 59628 | 59728 | |
| 59629 | 59729 | /* |
| 59630 | 59730 | ** Candidate values for Wal.exclusiveMode. |
| 59631 | 59731 | */ |
| | @@ -59995,11 +60095,11 @@ |
| 59995 | 60095 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 59996 | 60096 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 59997 | 60097 | SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); |
| 59998 | 60098 | WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, |
| 59999 | 60099 | walLockName(lockIdx), rc ? "failed" : "ok")); |
| 60000 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60100 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60001 | 60101 | return rc; |
| 60002 | 60102 | } |
| 60003 | 60103 | static void walUnlockShared(Wal *pWal, int lockIdx){ |
| 60004 | 60104 | if( pWal->exclusiveMode ) return; |
| 60005 | 60105 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| | @@ -60011,11 +60111,11 @@ |
| 60011 | 60111 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 60012 | 60112 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 60013 | 60113 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 60014 | 60114 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 60015 | 60115 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
| 60016 | | - VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| 60116 | + VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); ) |
| 60017 | 60117 | return rc; |
| 60018 | 60118 | } |
| 60019 | 60119 | static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ |
| 60020 | 60120 | if( pWal->exclusiveMode ) return; |
| 60021 | 60121 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| | @@ -60283,15 +60383,10 @@ |
| 60283 | 60383 | int rc; /* Return Code */ |
| 60284 | 60384 | i64 nSize; /* Size of log file */ |
| 60285 | 60385 | u32 aFrameCksum[2] = {0, 0}; |
| 60286 | 60386 | int iLock; /* Lock offset to lock for checkpoint */ |
| 60287 | 60387 | |
| 60288 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 60289 | | - int tmout = 0; |
| 60290 | | - sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 60291 | | -#endif |
| 60292 | | - |
| 60293 | 60388 | /* Obtain an exclusive lock on all byte in the locking range not already |
| 60294 | 60389 | ** locked by the caller. The caller is guaranteed to have locked the |
| 60295 | 60390 | ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte. |
| 60296 | 60391 | ** If successful, the same bytes that are locked here are unlocked before |
| 60297 | 60392 | ** this function returns. |
| | @@ -60835,10 +60930,93 @@ |
| 60835 | 60930 | p = 0; |
| 60836 | 60931 | } |
| 60837 | 60932 | *pp = p; |
| 60838 | 60933 | return rc; |
| 60839 | 60934 | } |
| 60935 | + |
| 60936 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 60937 | +/* |
| 60938 | +** Attempt to enable blocking locks. Blocking locks are enabled only if (a) |
| 60939 | +** they are supported by the VFS, and (b) the database handle is configured |
| 60940 | +** with a busy-timeout. Return 1 if blocking locks are successfully enabled, |
| 60941 | +** or 0 otherwise. |
| 60942 | +*/ |
| 60943 | +static int walEnableBlocking(Wal *pWal){ |
| 60944 | + int res = 0; |
| 60945 | + if( pWal->db ){ |
| 60946 | + int tmout = pWal->db->busyTimeout; |
| 60947 | + if( tmout ){ |
| 60948 | + int rc; |
| 60949 | + rc = sqlite3OsFileControl( |
| 60950 | + pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout |
| 60951 | + ); |
| 60952 | + res = (rc==SQLITE_OK); |
| 60953 | + } |
| 60954 | + } |
| 60955 | + return res; |
| 60956 | +} |
| 60957 | + |
| 60958 | +/* |
| 60959 | +** Disable blocking locks. |
| 60960 | +*/ |
| 60961 | +static void walDisableBlocking(Wal *pWal){ |
| 60962 | + int tmout = 0; |
| 60963 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout); |
| 60964 | +} |
| 60965 | + |
| 60966 | +/* |
| 60967 | +** If parameter bLock is true, attempt to enable blocking locks, take |
| 60968 | +** the WRITER lock, and then disable blocking locks. If blocking locks |
| 60969 | +** cannot be enabled, no attempt to obtain the WRITER lock is made. Return |
| 60970 | +** an SQLite error code if an error occurs, or SQLITE_OK otherwise. It is not |
| 60971 | +** an error if blocking locks can not be enabled. |
| 60972 | +** |
| 60973 | +** If the bLock parameter is false and the WRITER lock is held, release it. |
| 60974 | +*/ |
| 60975 | +SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock){ |
| 60976 | + int rc = SQLITE_OK; |
| 60977 | + assert( pWal->readLock<0 || bLock==0 ); |
| 60978 | + if( bLock ){ |
| 60979 | + assert( pWal->db ); |
| 60980 | + if( walEnableBlocking(pWal) ){ |
| 60981 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 60982 | + if( rc==SQLITE_OK ){ |
| 60983 | + pWal->writeLock = 1; |
| 60984 | + } |
| 60985 | + walDisableBlocking(pWal); |
| 60986 | + } |
| 60987 | + }else if( pWal->writeLock ){ |
| 60988 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 60989 | + pWal->writeLock = 0; |
| 60990 | + } |
| 60991 | + return rc; |
| 60992 | +} |
| 60993 | + |
| 60994 | +/* |
| 60995 | +** Set the database handle used to determine if blocking locks are required. |
| 60996 | +*/ |
| 60997 | +SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db){ |
| 60998 | + pWal->db = db; |
| 60999 | +} |
| 61000 | + |
| 61001 | +/* |
| 61002 | +** Take an exclusive WRITE lock. Blocking if so configured. |
| 61003 | +*/ |
| 61004 | +static int walLockWriter(Wal *pWal){ |
| 61005 | + int rc; |
| 61006 | + walEnableBlocking(pWal); |
| 61007 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61008 | + walDisableBlocking(pWal); |
| 61009 | + return rc; |
| 61010 | +} |
| 61011 | +#else |
| 61012 | +# define walEnableBlocking(x) 0 |
| 61013 | +# define walDisableBlocking(x) |
| 61014 | +# define walLockWriter(pWal) walLockExclusive((pWal), WAL_WRITE_LOCK, 1) |
| 61015 | +# define sqlite3WalDb(pWal, db) |
| 61016 | +#endif /* ifdef SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 61017 | + |
| 60840 | 61018 | |
| 60841 | 61019 | /* |
| 60842 | 61020 | ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and |
| 60843 | 61021 | ** n. If the attempt fails and parameter xBusy is not NULL, then it is a |
| 60844 | 61022 | ** busy-handler function. Invoke it and retry the lock until either the |
| | @@ -60853,10 +61031,16 @@ |
| 60853 | 61031 | ){ |
| 60854 | 61032 | int rc; |
| 60855 | 61033 | do { |
| 60856 | 61034 | rc = walLockExclusive(pWal, lockIdx, n); |
| 60857 | 61035 | }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); |
| 61036 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61037 | + if( rc==SQLITE_BUSY_TIMEOUT ){ |
| 61038 | + walDisableBlocking(pWal); |
| 61039 | + rc = SQLITE_BUSY; |
| 61040 | + } |
| 61041 | +#endif |
| 60858 | 61042 | return rc; |
| 60859 | 61043 | } |
| 60860 | 61044 | |
| 60861 | 61045 | /* |
| 60862 | 61046 | ** The cache of the wal-index header must be valid to call this function. |
| | @@ -61023,10 +61207,11 @@ |
| 61023 | 61207 | ** about the eventual size of the db file to the VFS layer. |
| 61024 | 61208 | */ |
| 61025 | 61209 | if( rc==SQLITE_OK ){ |
| 61026 | 61210 | i64 nReq = ((i64)mxPage * szPage); |
| 61027 | 61211 | i64 nSize; /* Current size of database file */ |
| 61212 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0); |
| 61028 | 61213 | rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); |
| 61029 | 61214 | if( rc==SQLITE_OK && nSize<nReq ){ |
| 61030 | 61215 | sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); |
| 61031 | 61216 | } |
| 61032 | 61217 | } |
| | @@ -61050,10 +61235,11 @@ |
| 61050 | 61235 | iOffset = (iDbpage-1)*(i64)szPage; |
| 61051 | 61236 | testcase( IS_BIG_INT(iOffset) ); |
| 61052 | 61237 | rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset); |
| 61053 | 61238 | if( rc!=SQLITE_OK ) break; |
| 61054 | 61239 | } |
| 61240 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0); |
| 61055 | 61241 | |
| 61056 | 61242 | /* If work was actually accomplished... */ |
| 61057 | 61243 | if( rc==SQLITE_OK ){ |
| 61058 | 61244 | if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ |
| 61059 | 61245 | i64 szDb = pWal->hdr.nPage*(i64)szPage; |
| | @@ -61061,14 +61247,10 @@ |
| 61061 | 61247 | rc = sqlite3OsTruncate(pWal->pDbFd, szDb); |
| 61062 | 61248 | if( rc==SQLITE_OK ){ |
| 61063 | 61249 | rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); |
| 61064 | 61250 | } |
| 61065 | 61251 | } |
| 61066 | | - if( rc==SQLITE_OK ){ |
| 61067 | | - rc = sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0); |
| 61068 | | - if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 61069 | | - } |
| 61070 | 61252 | if( rc==SQLITE_OK ){ |
| 61071 | 61253 | pInfo->nBackfill = mxSafeFrame; |
| 61072 | 61254 | } |
| 61073 | 61255 | } |
| 61074 | 61256 | |
| | @@ -61334,32 +61516,36 @@ |
| 61334 | 61516 | badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); |
| 61335 | 61517 | |
| 61336 | 61518 | /* If the first attempt failed, it might have been due to a race |
| 61337 | 61519 | ** with a writer. So get a WRITE lock and try again. |
| 61338 | 61520 | */ |
| 61339 | | - assert( badHdr==0 || pWal->writeLock==0 ); |
| 61340 | 61521 | if( badHdr ){ |
| 61341 | 61522 | if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){ |
| 61342 | 61523 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 61343 | 61524 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 61344 | 61525 | rc = SQLITE_READONLY_RECOVERY; |
| 61345 | 61526 | } |
| 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); |
| 61527 | + }else{ |
| 61528 | + int bWriteLock = pWal->writeLock; |
| 61529 | + if( bWriteLock || SQLITE_OK==(rc = walLockWriter(pWal)) ){ |
| 61530 | + pWal->writeLock = 1; |
| 61531 | + if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 61532 | + badHdr = walIndexTryHdr(pWal, pChanged); |
| 61533 | + if( badHdr ){ |
| 61534 | + /* If the wal-index header is still malformed even while holding |
| 61535 | + ** a WRITE lock, it can only mean that the header is corrupted and |
| 61536 | + ** needs to be reconstructed. So run recovery to do exactly that. |
| 61537 | + */ |
| 61538 | + rc = walIndexRecover(pWal); |
| 61539 | + *pChanged = 1; |
| 61540 | + } |
| 61541 | + } |
| 61542 | + if( bWriteLock==0 ){ |
| 61543 | + pWal->writeLock = 0; |
| 61544 | + walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 61545 | + } |
| 61546 | + } |
| 61361 | 61547 | } |
| 61362 | 61548 | } |
| 61363 | 61549 | |
| 61364 | 61550 | /* If the header is read successfully, check the version number to make |
| 61365 | 61551 | ** sure the wal-index was not constructed with some future format that |
| | @@ -61907,26 +62093,38 @@ |
| 61907 | 62093 | ** needs to be flushed. |
| 61908 | 62094 | */ |
| 61909 | 62095 | SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 61910 | 62096 | int rc; /* Return code */ |
| 61911 | 62097 | int cnt = 0; /* Number of TryBeginRead attempts */ |
| 61912 | | -#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 61913 | | - int tmout = 0; |
| 61914 | | -#endif |
| 62098 | + |
| 62099 | + assert( pWal->ckptLock==0 ); |
| 61915 | 62100 | |
| 61916 | 62101 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61917 | 62102 | int bChanged = 0; |
| 61918 | 62103 | WalIndexHdr *pSnapshot = pWal->pSnapshot; |
| 61919 | | - if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 61920 | | - bChanged = 1; |
| 61921 | | - } |
| 61922 | | -#endif |
| 61923 | | - |
| 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); |
| 62104 | + if( pSnapshot ){ |
| 62105 | + if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 62106 | + bChanged = 1; |
| 62107 | + } |
| 62108 | + |
| 62109 | + /* It is possible that there is a checkpointer thread running |
| 62110 | + ** concurrent with this code. If this is the case, it may be that the |
| 62111 | + ** checkpointer has already determined that it will checkpoint |
| 62112 | + ** snapshot X, where X is later in the wal file than pSnapshot, but |
| 62113 | + ** has not yet set the pInfo->nBackfillAttempted variable to indicate |
| 62114 | + ** its intent. To avoid the race condition this leads to, ensure that |
| 62115 | + ** there is no checkpointer process by taking a shared CKPT lock |
| 62116 | + ** before checking pInfo->nBackfillAttempted. */ |
| 62117 | + (void)walEnableBlocking(pWal); |
| 62118 | + rc = walLockShared(pWal, WAL_CKPT_LOCK); |
| 62119 | + walDisableBlocking(pWal); |
| 62120 | + |
| 62121 | + if( rc!=SQLITE_OK ){ |
| 62122 | + return rc; |
| 62123 | + } |
| 62124 | + pWal->ckptLock = 1; |
| 62125 | + } |
| 61928 | 62126 | #endif |
| 61929 | 62127 | |
| 61930 | 62128 | do{ |
| 61931 | 62129 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
| 61932 | 62130 | }while( rc==WAL_RETRY ); |
| | @@ -61933,20 +62131,10 @@ |
| 61933 | 62131 | testcase( (rc&0xff)==SQLITE_BUSY ); |
| 61934 | 62132 | testcase( (rc&0xff)==SQLITE_IOERR ); |
| 61935 | 62133 | testcase( rc==SQLITE_PROTOCOL ); |
| 61936 | 62134 | testcase( rc==SQLITE_OK ); |
| 61937 | 62135 | |
| 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 | 62136 | #ifdef SQLITE_ENABLE_SNAPSHOT |
| 61949 | 62137 | if( rc==SQLITE_OK ){ |
| 61950 | 62138 | if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ |
| 61951 | 62139 | /* At this point the client has a lock on an aReadMark[] slot holding |
| 61952 | 62140 | ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr |
| | @@ -61964,52 +62152,46 @@ |
| 61964 | 62152 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 61965 | 62153 | |
| 61966 | 62154 | assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 ); |
| 61967 | 62155 | assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame ); |
| 61968 | 62156 | |
| 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 | | - |
| 62157 | + /* Check that the wal file has not been wrapped. Assuming that it has |
| 62158 | + ** not, also check that no checkpointer has attempted to checkpoint any |
| 62159 | + ** frames beyond pSnapshot->mxFrame. If either of these conditions are |
| 62160 | + ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr |
| 62161 | + ** with *pSnapshot and set *pChanged as appropriate for opening the |
| 62162 | + ** snapshot. */ |
| 62163 | + if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt)) |
| 62164 | + && pSnapshot->mxFrame>=pInfo->nBackfillAttempted |
| 62165 | + ){ |
| 62166 | + assert( pWal->readLock>0 ); |
| 62167 | + memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr)); |
| 62168 | + *pChanged = bChanged; |
| 62169 | + }else{ |
| 62170 | + rc = SQLITE_ERROR_SNAPSHOT; |
| 62171 | + } |
| 62172 | + |
| 62173 | + /* A client using a non-current snapshot may not ignore any frames |
| 62174 | + ** from the start of the wal file. This is because, for a system |
| 62175 | + ** where (minFrame < iSnapshot < maxFrame), a checkpointer may |
| 62176 | + ** have omitted to checkpoint a frame earlier than minFrame in |
| 62177 | + ** the file because there exists a frame after iSnapshot that |
| 62178 | + ** is the same database page. */ |
| 62179 | + pWal->minFrame = 1; |
| 62005 | 62180 | |
| 62006 | 62181 | if( rc!=SQLITE_OK ){ |
| 62007 | 62182 | sqlite3WalEndReadTransaction(pWal); |
| 62008 | 62183 | } |
| 62009 | 62184 | } |
| 62010 | 62185 | } |
| 62186 | + |
| 62187 | + /* Release the shared CKPT lock obtained above. */ |
| 62188 | + if( pWal->ckptLock ){ |
| 62189 | + assert( pSnapshot ); |
| 62190 | + walUnlockShared(pWal, WAL_CKPT_LOCK); |
| 62191 | + pWal->ckptLock = 0; |
| 62192 | + } |
| 62011 | 62193 | #endif |
| 62012 | 62194 | return rc; |
| 62013 | 62195 | } |
| 62014 | 62196 | |
| 62015 | 62197 | /* |
| | @@ -62175,10 +62357,20 @@ |
| 62175 | 62357 | ** |
| 62176 | 62358 | ** There can only be a single writer active at a time. |
| 62177 | 62359 | */ |
| 62178 | 62360 | SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){ |
| 62179 | 62361 | int rc; |
| 62362 | + |
| 62363 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 62364 | + /* If the write-lock is already held, then it was obtained before the |
| 62365 | + ** read-transaction was even opened, making this call a no-op. |
| 62366 | + ** Return early. */ |
| 62367 | + if( pWal->writeLock ){ |
| 62368 | + assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) ); |
| 62369 | + return SQLITE_OK; |
| 62370 | + } |
| 62371 | +#endif |
| 62180 | 62372 | |
| 62181 | 62373 | /* Cannot start a write transaction without first holding a read |
| 62182 | 62374 | ** transaction. */ |
| 62183 | 62375 | assert( pWal->readLock>=0 ); |
| 62184 | 62376 | assert( pWal->writeLock==0 && pWal->iReCksum==0 ); |
| | @@ -62751,50 +62943,57 @@ |
| 62751 | 62943 | ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ |
| 62752 | 62944 | assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); |
| 62753 | 62945 | |
| 62754 | 62946 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 62755 | 62947 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 62948 | + |
| 62949 | + /* Enable blocking locks, if possible. If blocking locks are successfully |
| 62950 | + ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */ |
| 62951 | + sqlite3WalDb(pWal, db); |
| 62952 | + (void)walEnableBlocking(pWal); |
| 62756 | 62953 | |
| 62757 | 62954 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 62758 | | - ** "checkpoint" lock on the database file. */ |
| 62955 | + ** "checkpoint" lock on the database file. |
| 62956 | + ** EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 62957 | + ** checkpoint operation at the same time, the lock cannot be obtained and |
| 62958 | + ** SQLITE_BUSY is returned. |
| 62959 | + ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| 62960 | + ** it will not be invoked in this case. |
| 62961 | + */ |
| 62759 | 62962 | 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 | | - } |
| 62963 | + testcase( rc==SQLITE_BUSY ); |
| 62964 | + testcase( rc!=SQLITE_OK && xBusy2!=0 ); |
| 62965 | + if( rc==SQLITE_OK ){ |
| 62966 | + pWal->ckptLock = 1; |
| 62967 | + |
| 62968 | + /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and |
| 62969 | + ** TRUNCATE modes also obtain the exclusive "writer" lock on the database |
| 62970 | + ** file. |
| 62971 | + ** |
| 62972 | + ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained |
| 62973 | + ** immediately, and a busy-handler is configured, it is invoked and the |
| 62974 | + ** writer lock retried until either the busy-handler returns 0 or the |
| 62975 | + ** lock is successfully obtained. |
| 62976 | + */ |
| 62977 | + if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ |
| 62978 | + rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1); |
| 62979 | + if( rc==SQLITE_OK ){ |
| 62980 | + pWal->writeLock = 1; |
| 62981 | + }else if( rc==SQLITE_BUSY ){ |
| 62982 | + eMode2 = SQLITE_CHECKPOINT_PASSIVE; |
| 62983 | + xBusy2 = 0; |
| 62984 | + rc = SQLITE_OK; |
| 62985 | + } |
| 62986 | + } |
| 62987 | + } |
| 62988 | + |
| 62792 | 62989 | |
| 62793 | 62990 | /* Read the wal-index header. */ |
| 62794 | 62991 | if( rc==SQLITE_OK ){ |
| 62992 | + walDisableBlocking(pWal); |
| 62795 | 62993 | rc = walIndexReadHdr(pWal, &isChanged); |
| 62994 | + (void)walEnableBlocking(pWal); |
| 62796 | 62995 | if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){ |
| 62797 | 62996 | sqlite3OsUnfetch(pWal->pDbFd, 0, 0); |
| 62798 | 62997 | } |
| 62799 | 62998 | } |
| 62800 | 62999 | |
| | @@ -62821,16 +63020,24 @@ |
| 62821 | 63020 | ** next time the pager opens a snapshot on this database it knows that |
| 62822 | 63021 | ** the cache needs to be reset. |
| 62823 | 63022 | */ |
| 62824 | 63023 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
| 62825 | 63024 | } |
| 63025 | + |
| 63026 | + walDisableBlocking(pWal); |
| 63027 | + sqlite3WalDb(pWal, 0); |
| 62826 | 63028 | |
| 62827 | 63029 | /* Release the locks. */ |
| 62828 | 63030 | sqlite3WalEndWriteTransaction(pWal); |
| 62829 | | - walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 62830 | | - pWal->ckptLock = 0; |
| 63031 | + if( pWal->ckptLock ){ |
| 63032 | + walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 63033 | + pWal->ckptLock = 0; |
| 63034 | + } |
| 62831 | 63035 | WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); |
| 63036 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 63037 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 63038 | +#endif |
| 62832 | 63039 | return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc); |
| 62833 | 63040 | } |
| 62834 | 63041 | |
| 62835 | 63042 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 62836 | 63043 | ** number of frames in the WAL at the point of the last commit since |
| | @@ -62943,11 +63150,14 @@ |
| 62943 | 63150 | return rc; |
| 62944 | 63151 | } |
| 62945 | 63152 | |
| 62946 | 63153 | /* Try to open on pSnapshot when the next read-transaction starts |
| 62947 | 63154 | */ |
| 62948 | | -SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){ |
| 63155 | +SQLITE_PRIVATE void sqlite3WalSnapshotOpen( |
| 63156 | + Wal *pWal, |
| 63157 | + sqlite3_snapshot *pSnapshot |
| 63158 | +){ |
| 62949 | 63159 | pWal->pSnapshot = (WalIndexHdr*)pSnapshot; |
| 62950 | 63160 | } |
| 62951 | 63161 | |
| 62952 | 63162 | /* |
| 62953 | 63163 | ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if |
| | @@ -63462,11 +63672,11 @@ |
| 63462 | 63672 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 63463 | 63673 | u8 bDoTruncate; /* True to truncate db on commit */ |
| 63464 | 63674 | #endif |
| 63465 | 63675 | u8 inTransaction; /* Transaction state */ |
| 63466 | 63676 | 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 */ |
| 63677 | + u8 nReserveWanted; /* Desired number of extra bytes per page */ |
| 63468 | 63678 | u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
| 63469 | 63679 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 63470 | 63680 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 63471 | 63681 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 63472 | 63682 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| | @@ -66356,12 +66566,11 @@ |
| 66356 | 66566 | */ |
| 66357 | 66567 | static int btreeInvokeBusyHandler(void *pArg){ |
| 66358 | 66568 | BtShared *pBt = (BtShared*)pArg; |
| 66359 | 66569 | assert( pBt->db ); |
| 66360 | 66570 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 66361 | | - return sqlite3InvokeBusyHandler(&pBt->db->busyHandler, |
| 66362 | | - sqlite3PagerFile(pBt->pPager)); |
| 66571 | + return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 66363 | 66572 | } |
| 66364 | 66573 | |
| 66365 | 66574 | /* |
| 66366 | 66575 | ** Open a database file. |
| 66367 | 66576 | ** |
| | @@ -66908,23 +67117,21 @@ |
| 66908 | 67117 | ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size |
| 66909 | 67118 | ** and autovacuum mode can no longer be changed. |
| 66910 | 67119 | */ |
| 66911 | 67120 | SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
| 66912 | 67121 | int rc = SQLITE_OK; |
| 67122 | + int x; |
| 66913 | 67123 | BtShared *pBt = p->pBt; |
| 66914 | | - assert( nReserve>=-1 && nReserve<=254 ); |
| 67124 | + assert( nReserve>=0 && nReserve<=255 ); |
| 66915 | 67125 | sqlite3BtreeEnter(p); |
| 66916 | | - if( nReserve>=0 ){ |
| 66917 | | - pBt->nReserveWanted = nReserve + 1; |
| 66918 | | - } |
| 67126 | + pBt->nReserveWanted = nReserve; |
| 67127 | + x = pBt->pageSize - pBt->usableSize; |
| 67128 | + if( nReserve<x ) nReserve = x; |
| 66919 | 67129 | if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
| 66920 | 67130 | sqlite3BtreeLeave(p); |
| 66921 | 67131 | return SQLITE_READONLY; |
| 66922 | 67132 | } |
| 66923 | | - if( nReserve<0 ){ |
| 66924 | | - nReserve = pBt->pageSize - pBt->usableSize; |
| 66925 | | - } |
| 66926 | 67133 | assert( nReserve>=0 && nReserve<=255 ); |
| 66927 | 67134 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 66928 | 67135 | ((pageSize-1)&pageSize)==0 ){ |
| 66929 | 67136 | assert( (pageSize & 7)==0 ); |
| 66930 | 67137 | assert( !pBt->pCursor ); |
| | @@ -66965,18 +67172,22 @@ |
| 66965 | 67172 | |
| 66966 | 67173 | /* |
| 66967 | 67174 | ** Return the number of bytes of space at the end of every page that |
| 66968 | 67175 | ** are intentually left unused. This is the "reserved" space that is |
| 66969 | 67176 | ** sometimes used by extensions. |
| 67177 | +** |
| 67178 | +** The value returned is the larger of the current reserve size and |
| 67179 | +** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. |
| 67180 | +** The amount of reserve can only grow - never shrink. |
| 66970 | 67181 | */ |
| 66971 | 67182 | SQLITE_PRIVATE int sqlite3BtreeGetRequestedReserve(Btree *p){ |
| 66972 | | - int n; |
| 67183 | + int n1, n2; |
| 66973 | 67184 | sqlite3BtreeEnter(p); |
| 66974 | | - n = ((int)p->pBt->nReserveWanted) - 1; |
| 66975 | | - if( n<0 ) n = sqlite3BtreeGetReserveNoMutex(p); |
| 67185 | + n1 = (int)p->pBt->nReserveWanted; |
| 67186 | + n2 = sqlite3BtreeGetReserveNoMutex(p); |
| 66976 | 67187 | sqlite3BtreeLeave(p); |
| 66977 | | - return n; |
| 67188 | + return n1>n2 ? n1 : n2; |
| 66978 | 67189 | } |
| 66979 | 67190 | |
| 66980 | 67191 | |
| 66981 | 67192 | /* |
| 66982 | 67193 | ** Set the maximum page count for a database if mxPage is positive. |
| | @@ -67422,10 +67633,11 @@ |
| 67422 | 67633 | ** when A already has a read lock, we encourage A to give up and let B |
| 67423 | 67634 | ** proceed. |
| 67424 | 67635 | */ |
| 67425 | 67636 | SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ |
| 67426 | 67637 | BtShared *pBt = p->pBt; |
| 67638 | + Pager *pPager = pBt->pPager; |
| 67427 | 67639 | int rc = SQLITE_OK; |
| 67428 | 67640 | |
| 67429 | 67641 | sqlite3BtreeEnter(p); |
| 67430 | 67642 | btreeIntegrity(p); |
| 67431 | 67643 | |
| | @@ -67437,11 +67649,11 @@ |
| 67437 | 67649 | goto trans_begun; |
| 67438 | 67650 | } |
| 67439 | 67651 | assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); |
| 67440 | 67652 | |
| 67441 | 67653 | if( (p->db->flags & SQLITE_ResetDatabase) |
| 67442 | | - && sqlite3PagerIsreadonly(pBt->pPager)==0 |
| 67654 | + && sqlite3PagerIsreadonly(pPager)==0 |
| 67443 | 67655 | ){ |
| 67444 | 67656 | pBt->btsFlags &= ~BTS_READ_ONLY; |
| 67445 | 67657 | } |
| 67446 | 67658 | |
| 67447 | 67659 | /* Write transactions are not possible on a read-only database */ |
| | @@ -67485,10 +67697,22 @@ |
| 67485 | 67697 | if( SQLITE_OK!=rc ) goto trans_begun; |
| 67486 | 67698 | |
| 67487 | 67699 | pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; |
| 67488 | 67700 | if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; |
| 67489 | 67701 | do { |
| 67702 | + sqlite3PagerWalDb(pPager, p->db); |
| 67703 | + |
| 67704 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67705 | + /* If transitioning from no transaction directly to a write transaction, |
| 67706 | + ** block for the WRITER lock first if possible. */ |
| 67707 | + if( pBt->pPage1==0 && wrflag ){ |
| 67708 | + assert( pBt->inTransaction==TRANS_NONE ); |
| 67709 | + rc = sqlite3PagerWalWriteLock(pPager, 1); |
| 67710 | + if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break; |
| 67711 | + } |
| 67712 | +#endif |
| 67713 | + |
| 67490 | 67714 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 67491 | 67715 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 67492 | 67716 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 67493 | 67717 | ** reading page 1 it discovers that the page-size of the database |
| 67494 | 67718 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| | @@ -67498,11 +67722,11 @@ |
| 67498 | 67722 | |
| 67499 | 67723 | if( rc==SQLITE_OK && wrflag ){ |
| 67500 | 67724 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ |
| 67501 | 67725 | rc = SQLITE_READONLY; |
| 67502 | 67726 | }else{ |
| 67503 | | - rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
| 67727 | + rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db)); |
| 67504 | 67728 | if( rc==SQLITE_OK ){ |
| 67505 | 67729 | rc = newDatabase(pBt); |
| 67506 | 67730 | }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ |
| 67507 | 67731 | /* if there was no transaction opened when this function was |
| 67508 | 67732 | ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error |
| | @@ -67511,15 +67735,19 @@ |
| 67511 | 67735 | } |
| 67512 | 67736 | } |
| 67513 | 67737 | } |
| 67514 | 67738 | |
| 67515 | 67739 | if( rc!=SQLITE_OK ){ |
| 67740 | + (void)sqlite3PagerWalWriteLock(pPager, 0); |
| 67516 | 67741 | unlockBtreeIfUnused(pBt); |
| 67517 | 67742 | } |
| 67518 | 67743 | }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
| 67519 | 67744 | btreeInvokeBusyHandler(pBt) ); |
| 67520 | | - sqlite3PagerResetLockTimeout(pBt->pPager); |
| 67745 | + sqlite3PagerWalDb(pPager, 0); |
| 67746 | +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 67747 | + if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; |
| 67748 | +#endif |
| 67521 | 67749 | |
| 67522 | 67750 | if( rc==SQLITE_OK ){ |
| 67523 | 67751 | if( p->inTrans==TRANS_NONE ){ |
| 67524 | 67752 | pBt->nTransaction++; |
| 67525 | 67753 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -67567,11 +67795,11 @@ |
| 67567 | 67795 | if( wrflag ){ |
| 67568 | 67796 | /* This call makes sure that the pager has the correct number of |
| 67569 | 67797 | ** open savepoints. If the second parameter is greater than 0 and |
| 67570 | 67798 | ** the sub-journal is not already open, then it will be opened here. |
| 67571 | 67799 | */ |
| 67572 | | - rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 67800 | + rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint); |
| 67573 | 67801 | } |
| 67574 | 67802 | } |
| 67575 | 67803 | |
| 67576 | 67804 | btreeIntegrity(p); |
| 67577 | 67805 | sqlite3BtreeLeave(p); |
| | @@ -71203,11 +71431,11 @@ |
| 71203 | 71431 | |
| 71204 | 71432 | /* Remove cells from the start and end of the page */ |
| 71205 | 71433 | assert( nCell>=0 ); |
| 71206 | 71434 | if( iOld<iNew ){ |
| 71207 | 71435 | int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); |
| 71208 | | - if( nShift>nCell ) return SQLITE_CORRUPT_BKPT; |
| 71436 | + if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT; |
| 71209 | 71437 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 71210 | 71438 | nCell -= nShift; |
| 71211 | 71439 | } |
| 71212 | 71440 | if( iNewEnd < iOldEnd ){ |
| 71213 | 71441 | int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); |
| | @@ -73560,11 +73788,10 @@ |
| 73560 | 73788 | } |
| 73561 | 73789 | sqlite3BtreeLeave(p); |
| 73562 | 73790 | return rc; |
| 73563 | 73791 | } |
| 73564 | 73792 | |
| 73565 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 73566 | 73793 | /* |
| 73567 | 73794 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 73568 | 73795 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 73569 | 73796 | ** |
| 73570 | 73797 | ** SQLITE_OK is returned if the operation is successfully executed. |
| | @@ -73633,11 +73860,10 @@ |
| 73633 | 73860 | } |
| 73634 | 73861 | |
| 73635 | 73862 | /* An error has occurred. Return an error code. */ |
| 73636 | 73863 | return rc; |
| 73637 | 73864 | } |
| 73638 | | -#endif |
| 73639 | 73865 | |
| 73640 | 73866 | /* |
| 73641 | 73867 | ** Return the pager associated with a BTree. This routine is used for |
| 73642 | 73868 | ** testing and debugging only. |
| 73643 | 73869 | */ |
| | @@ -74684,11 +74910,11 @@ |
| 74684 | 74910 | ** Attempt to set the page size of the destination to match the page size |
| 74685 | 74911 | ** of the source. |
| 74686 | 74912 | */ |
| 74687 | 74913 | static int setDestPgsz(sqlite3_backup *p){ |
| 74688 | 74914 | int rc; |
| 74689 | | - rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0); |
| 74915 | + rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0); |
| 74690 | 74916 | return rc; |
| 74691 | 74917 | } |
| 74692 | 74918 | |
| 74693 | 74919 | /* |
| 74694 | 74920 | ** Check that there is no open read-transaction on the b-tree passed as the |
| | @@ -78709,24 +78935,23 @@ |
| 78709 | 78935 | ** "PX" -> "r[X]" |
| 78710 | 78936 | ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 |
| 78711 | 78937 | ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 |
| 78712 | 78938 | ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x |
| 78713 | 78939 | */ |
| 78714 | | -static int displayComment( |
| 78940 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayComment( |
| 78941 | + sqlite3 *db, /* Optional - Oom error reporting only */ |
| 78715 | 78942 | 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[] */ |
| 78943 | + const char *zP4 /* Previously obtained value for P4 */ |
| 78719 | 78944 | ){ |
| 78720 | 78945 | const char *zOpName; |
| 78721 | 78946 | const char *zSynopsis; |
| 78722 | 78947 | int nOpName; |
| 78723 | 78948 | int ii; |
| 78724 | 78949 | char zAlt[50]; |
| 78725 | 78950 | StrAccum x; |
| 78726 | | - sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); |
| 78727 | 78951 | |
| 78952 | + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); |
| 78728 | 78953 | zOpName = sqlite3OpcodeName(pOp->opcode); |
| 78729 | 78954 | nOpName = sqlite3Strlen30(zOpName); |
| 78730 | 78955 | if( zOpName[nOpName+1] ){ |
| 78731 | 78956 | int seenCom = 0; |
| 78732 | 78957 | char c; |
| | @@ -78789,14 +79014,16 @@ |
| 78789 | 79014 | sqlite3_str_appendf(&x, "; %s", pOp->zComment); |
| 78790 | 79015 | } |
| 78791 | 79016 | }else if( pOp->zComment ){ |
| 78792 | 79017 | sqlite3_str_appendall(&x, pOp->zComment); |
| 78793 | 79018 | } |
| 78794 | | - sqlite3StrAccumFinish(&x); |
| 78795 | | - return x.nChar; |
| 79019 | + if( (x.accError & SQLITE_NOMEM)!=0 && db!=0 ){ |
| 79020 | + sqlite3OomFault(db); |
| 79021 | + } |
| 79022 | + return sqlite3StrAccumFinish(&x); |
| 78796 | 79023 | } |
| 78797 | | -#endif /* SQLITE_DEBUG */ |
| 79024 | +#endif /* SQLITE_ENABLE_EXPLAIN_COMMENTS */ |
| 78798 | 79025 | |
| 78799 | 79026 | #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) |
| 78800 | 79027 | /* |
| 78801 | 79028 | ** Translate the P4.pExpr value for an OP_CursorHint opcode into text |
| 78802 | 79029 | ** that can be displayed in the P4 column of EXPLAIN output. |
| | @@ -78873,15 +79100,15 @@ |
| 78873 | 79100 | #if VDBE_DISPLAY_P4 |
| 78874 | 79101 | /* |
| 78875 | 79102 | ** Compute a string that describes the P4 parameter for an opcode. |
| 78876 | 79103 | ** Use zTemp for any required temporary buffer space. |
| 78877 | 79104 | */ |
| 78878 | | -static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 78879 | | - char *zP4 = zTemp; |
| 79105 | +SQLITE_PRIVATE char *sqlite3VdbeDisplayP4(sqlite3 *db, Op *pOp){ |
| 79106 | + char *zP4 = 0; |
| 78880 | 79107 | StrAccum x; |
| 78881 | | - assert( nTemp>=20 ); |
| 78882 | | - sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); |
| 79108 | + |
| 79109 | + sqlite3StrAccumInit(&x, 0, 0, 0, SQLITE_MAX_LENGTH); |
| 78883 | 79110 | switch( pOp->p4type ){ |
| 78884 | 79111 | case P4_KEYINFO: { |
| 78885 | 79112 | int j; |
| 78886 | 79113 | KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 78887 | 79114 | assert( pKeyInfo->aSortFlags!=0 ); |
| | @@ -78961,40 +79188,36 @@ |
| 78961 | 79188 | int i; |
| 78962 | 79189 | int *ai = pOp->p4.ai; |
| 78963 | 79190 | int n = ai[0]; /* The first element of an INTARRAY is always the |
| 78964 | 79191 | ** count of the number of elements to follow */ |
| 78965 | 79192 | for(i=1; i<=n; i++){ |
| 78966 | | - sqlite3_str_appendf(&x, ",%d", ai[i]); |
| 79193 | + sqlite3_str_appendf(&x, "%c%d", (i==1 ? '[' : ','), ai[i]); |
| 78967 | 79194 | } |
| 78968 | | - zTemp[0] = '['; |
| 78969 | 79195 | sqlite3_str_append(&x, "]", 1); |
| 78970 | 79196 | break; |
| 78971 | 79197 | } |
| 78972 | 79198 | case P4_SUBPROGRAM: { |
| 78973 | | - sqlite3_str_appendf(&x, "program"); |
| 79199 | + zP4 = "program"; |
| 78974 | 79200 | break; |
| 78975 | 79201 | } |
| 78976 | 79202 | case P4_DYNBLOB: |
| 78977 | 79203 | case P4_ADVANCE: { |
| 78978 | | - zTemp[0] = 0; |
| 78979 | 79204 | break; |
| 78980 | 79205 | } |
| 78981 | 79206 | case P4_TABLE: { |
| 78982 | | - sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName); |
| 79207 | + zP4 = pOp->p4.pTab->zName; |
| 78983 | 79208 | break; |
| 78984 | 79209 | } |
| 78985 | 79210 | default: { |
| 78986 | 79211 | zP4 = pOp->p4.z; |
| 78987 | | - if( zP4==0 ){ |
| 78988 | | - zP4 = zTemp; |
| 78989 | | - zTemp[0] = 0; |
| 78990 | | - } |
| 78991 | 79212 | } |
| 78992 | 79213 | } |
| 78993 | | - sqlite3StrAccumFinish(&x); |
| 78994 | | - assert( zP4!=0 ); |
| 78995 | | - return zP4; |
| 79214 | + if( zP4 ) sqlite3_str_appendall(&x, zP4); |
| 79215 | + if( (x.accError & SQLITE_NOMEM)!=0 ){ |
| 79216 | + sqlite3OomFault(db); |
| 79217 | + } |
| 79218 | + return sqlite3StrAccumFinish(&x); |
| 78996 | 79219 | } |
| 78997 | 79220 | #endif /* VDBE_DISPLAY_P4 */ |
| 78998 | 79221 | |
| 78999 | 79222 | /* |
| 79000 | 79223 | ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
| | @@ -79080,28 +79303,32 @@ |
| 79080 | 79303 | /* |
| 79081 | 79304 | ** Print a single opcode. This routine is used for debugging only. |
| 79082 | 79305 | */ |
| 79083 | 79306 | SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){ |
| 79084 | 79307 | char *zP4; |
| 79085 | | - char zPtr[50]; |
| 79086 | | - char zCom[100]; |
| 79308 | + char *zCom; |
| 79309 | + sqlite3 dummyDb; |
| 79087 | 79310 | static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; |
| 79088 | 79311 | if( pOut==0 ) pOut = stdout; |
| 79089 | | - zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
| 79312 | + dummyDb.mallocFailed = 1; |
| 79313 | + zP4 = sqlite3VdbeDisplayP4(&dummyDb, pOp); |
| 79090 | 79314 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 79091 | | - displayComment(pOp, zP4, zCom, sizeof(zCom)); |
| 79315 | + zCom = sqlite3VdbeDisplayComment(0, pOp, zP4); |
| 79092 | 79316 | #else |
| 79093 | | - zCom[0] = 0; |
| 79317 | + zCom = 0; |
| 79094 | 79318 | #endif |
| 79095 | 79319 | /* NB: The sqlite3OpcodeName() function is implemented by code created |
| 79096 | 79320 | ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the |
| 79097 | 79321 | ** information from the vdbe.c source text */ |
| 79098 | 79322 | fprintf(pOut, zFormat1, pc, |
| 79099 | | - sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
| 79100 | | - zCom |
| 79323 | + sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, |
| 79324 | + zP4 ? zP4 : "", pOp->p5, |
| 79325 | + zCom ? zCom : "" |
| 79101 | 79326 | ); |
| 79102 | 79327 | fflush(pOut); |
| 79328 | + sqlite3_free(zP4); |
| 79329 | + sqlite3_free(zCom); |
| 79103 | 79330 | } |
| 79104 | 79331 | #endif |
| 79105 | 79332 | |
| 79106 | 79333 | /* |
| 79107 | 79334 | ** Initialize an array of N Mem element. |
| | @@ -79188,10 +79415,125 @@ |
| 79188 | 79415 | assert( sqlite3VdbeFrameIsValid(pFrame) ); |
| 79189 | 79416 | pFrame->pParent = pFrame->v->pDelFrame; |
| 79190 | 79417 | pFrame->v->pDelFrame = pFrame; |
| 79191 | 79418 | } |
| 79192 | 79419 | |
| 79420 | +#if defined(SQLITE_ENABLE_BYTECODE_VTAB) || !defined(SQLITE_OMIT_EXPLAIN) |
| 79421 | +/* |
| 79422 | +** Locate the next opcode to be displayed in EXPLAIN or EXPLAIN |
| 79423 | +** QUERY PLAN output. |
| 79424 | +** |
| 79425 | +** Return SQLITE_ROW on success. Return SQLITE_DONE if there are no |
| 79426 | +** more opcodes to be displayed. |
| 79427 | +*/ |
| 79428 | +SQLITE_PRIVATE int sqlite3VdbeNextOpcode( |
| 79429 | + Vdbe *p, /* The statement being explained */ |
| 79430 | + Mem *pSub, /* Storage for keeping track of subprogram nesting */ |
| 79431 | + int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */ |
| 79432 | + int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */ |
| 79433 | + int *piAddr, /* OUT: Write index into (*paOp)[] here */ |
| 79434 | + Op **paOp /* OUT: Write the opcode array here */ |
| 79435 | +){ |
| 79436 | + int nRow; /* Stop when row count reaches this */ |
| 79437 | + int nSub = 0; /* Number of sub-vdbes seen so far */ |
| 79438 | + SubProgram **apSub = 0; /* Array of sub-vdbes */ |
| 79439 | + int i; /* Next instruction address */ |
| 79440 | + int rc = SQLITE_OK; /* Result code */ |
| 79441 | + Op *aOp = 0; /* Opcode array */ |
| 79442 | + int iPc; /* Rowid. Copy of value in *piPc */ |
| 79443 | + |
| 79444 | + /* When the number of output rows reaches nRow, that means the |
| 79445 | + ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
| 79446 | + ** nRow is the sum of the number of rows in the main program, plus |
| 79447 | + ** the sum of the number of rows in all trigger subprograms encountered |
| 79448 | + ** so far. The nRow value will increase as new trigger subprograms are |
| 79449 | + ** encountered, but p->pc will eventually catch up to nRow. |
| 79450 | + */ |
| 79451 | + nRow = p->nOp; |
| 79452 | + if( pSub!=0 ){ |
| 79453 | + if( pSub->flags&MEM_Blob ){ |
| 79454 | + /* pSub is initiallly NULL. It is initialized to a BLOB by |
| 79455 | + ** the P4_SUBPROGRAM processing logic below */ |
| 79456 | + nSub = pSub->n/sizeof(Vdbe*); |
| 79457 | + apSub = (SubProgram **)pSub->z; |
| 79458 | + } |
| 79459 | + for(i=0; i<nSub; i++){ |
| 79460 | + nRow += apSub[i]->nOp; |
| 79461 | + } |
| 79462 | + } |
| 79463 | + iPc = *piPc; |
| 79464 | + while(1){ /* Loop exits via break */ |
| 79465 | + i = iPc++; |
| 79466 | + if( i>=nRow ){ |
| 79467 | + p->rc = SQLITE_OK; |
| 79468 | + rc = SQLITE_DONE; |
| 79469 | + break; |
| 79470 | + } |
| 79471 | + if( i<p->nOp ){ |
| 79472 | + /* The rowid is small enough that we are still in the |
| 79473 | + ** main program. */ |
| 79474 | + aOp = p->aOp; |
| 79475 | + }else{ |
| 79476 | + /* We are currently listing subprograms. Figure out which one and |
| 79477 | + ** pick up the appropriate opcode. */ |
| 79478 | + int j; |
| 79479 | + i -= p->nOp; |
| 79480 | + assert( apSub!=0 ); |
| 79481 | + assert( nSub>0 ); |
| 79482 | + for(j=0; i>=apSub[j]->nOp; j++){ |
| 79483 | + i -= apSub[j]->nOp; |
| 79484 | + assert( i<apSub[j]->nOp || j+1<nSub ); |
| 79485 | + } |
| 79486 | + aOp = apSub[j]->aOp; |
| 79487 | + } |
| 79488 | + |
| 79489 | + /* When an OP_Program opcode is encounter (the only opcode that has |
| 79490 | + ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms |
| 79491 | + ** kept in p->aMem[9].z to hold the new program - assuming this subprogram |
| 79492 | + ** has not already been seen. |
| 79493 | + */ |
| 79494 | + if( pSub!=0 && aOp[i].p4type==P4_SUBPROGRAM ){ |
| 79495 | + int nByte = (nSub+1)*sizeof(SubProgram*); |
| 79496 | + int j; |
| 79497 | + for(j=0; j<nSub; j++){ |
| 79498 | + if( apSub[j]==aOp[i].p4.pProgram ) break; |
| 79499 | + } |
| 79500 | + if( j==nSub ){ |
| 79501 | + p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0); |
| 79502 | + if( p->rc!=SQLITE_OK ){ |
| 79503 | + rc = SQLITE_ERROR; |
| 79504 | + break; |
| 79505 | + } |
| 79506 | + apSub = (SubProgram **)pSub->z; |
| 79507 | + apSub[nSub++] = aOp[i].p4.pProgram; |
| 79508 | + MemSetTypeFlag(pSub, MEM_Blob); |
| 79509 | + pSub->n = nSub*sizeof(SubProgram*); |
| 79510 | + nRow += aOp[i].p4.pProgram->nOp; |
| 79511 | + } |
| 79512 | + } |
| 79513 | + if( eMode==0 ) break; |
| 79514 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 79515 | + if( eMode==2 ){ |
| 79516 | + Op *pOp = aOp + i; |
| 79517 | + if( pOp->opcode==OP_OpenRead ) break; |
| 79518 | + if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break; |
| 79519 | + if( pOp->opcode==OP_ReopenIdx ) break; |
| 79520 | + }else |
| 79521 | +#endif |
| 79522 | + { |
| 79523 | + assert( eMode==1 ); |
| 79524 | + if( aOp[i].opcode==OP_Explain ) break; |
| 79525 | + if( aOp[i].opcode==OP_Init && iPc>1 ) break; |
| 79526 | + } |
| 79527 | + } |
| 79528 | + *piPc = iPc; |
| 79529 | + *piAddr = i; |
| 79530 | + *paOp = aOp; |
| 79531 | + return rc; |
| 79532 | +} |
| 79533 | +#endif /* SQLITE_ENABLE_BYTECODE_VTAB || !SQLITE_OMIT_EXPLAIN */ |
| 79534 | + |
| 79193 | 79535 | |
| 79194 | 79536 | /* |
| 79195 | 79537 | ** Delete a VdbeFrame object and its contents. VdbeFrame objects are |
| 79196 | 79538 | ** allocated by the OP_Program opcode in sqlite3VdbeExec(). |
| 79197 | 79539 | */ |
| | @@ -79228,20 +79570,18 @@ |
| 79228 | 79570 | ** the trigger subprograms are listed one by one. |
| 79229 | 79571 | */ |
| 79230 | 79572 | SQLITE_PRIVATE int sqlite3VdbeList( |
| 79231 | 79573 | Vdbe *p /* The VDBE */ |
| 79232 | 79574 | ){ |
| 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 | 79575 | Mem *pSub = 0; /* Memory cell hold array of subprogs */ |
| 79237 | 79576 | sqlite3 *db = p->db; /* The database connection */ |
| 79238 | 79577 | int i; /* Loop counter */ |
| 79239 | 79578 | int rc = SQLITE_OK; /* Return code */ |
| 79240 | 79579 | Mem *pMem = &p->aMem[1]; /* First Mem of result set */ |
| 79241 | 79580 | int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0); |
| 79242 | | - Op *pOp = 0; |
| 79581 | + Op *aOp; /* Array of opcodes */ |
| 79582 | + Op *pOp; /* Current opcode */ |
| 79243 | 79583 | |
| 79244 | 79584 | assert( p->explain ); |
| 79245 | 79585 | assert( p->magic==VDBE_MAGIC_RUN ); |
| 79246 | 79586 | assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 79247 | 79587 | |
| | @@ -79257,166 +79597,66 @@ |
| 79257 | 79597 | ** sqlite3_column_text16() failed. */ |
| 79258 | 79598 | sqlite3OomFault(db); |
| 79259 | 79599 | return SQLITE_ERROR; |
| 79260 | 79600 | } |
| 79261 | 79601 | |
| 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 | 79602 | if( bListSubprogs ){ |
| 79271 | 79603 | /* The first 8 memory cells are used for the result set. So we will |
| 79272 | 79604 | ** commandeer the 9th cell to use as storage for an array of pointers |
| 79273 | 79605 | ** to trigger subprograms. The VDBE is guaranteed to have at least 9 |
| 79274 | 79606 | ** cells. */ |
| 79275 | 79607 | assert( p->nMem>9 ); |
| 79276 | 79608 | 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 ){ |
| 79609 | + }else{ |
| 79610 | + pSub = 0; |
| 79611 | + } |
| 79612 | + |
| 79613 | + /* Figure out which opcode is next to display */ |
| 79614 | + rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp); |
| 79615 | + |
| 79616 | + if( rc==SQLITE_OK ){ |
| 79617 | + pOp = aOp + i; |
| 79343 | 79618 | if( AtomicLoad(&db->u1.isInterrupted) ){ |
| 79344 | 79619 | p->rc = SQLITE_INTERRUPT; |
| 79345 | 79620 | rc = SQLITE_ERROR; |
| 79346 | 79621 | sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); |
| 79347 | 79622 | }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); |
| 79623 | + char *zP4 = sqlite3VdbeDisplayP4(db, pOp); |
| 79624 | + if( p->explain==2 ){ |
| 79625 | + sqlite3VdbeMemSetInt64(pMem, pOp->p1); |
| 79626 | + sqlite3VdbeMemSetInt64(pMem+1, pOp->p2); |
| 79627 | + sqlite3VdbeMemSetInt64(pMem+2, pOp->p3); |
| 79628 | + sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free); |
| 79629 | + p->nResColumn = 4; |
| 79383 | 79630 | }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 | | - |
| 79631 | + sqlite3VdbeMemSetInt64(pMem+0, i); |
| 79632 | + sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode), |
| 79633 | + -1, SQLITE_UTF8, SQLITE_STATIC); |
| 79634 | + sqlite3VdbeMemSetInt64(pMem+2, pOp->p1); |
| 79635 | + sqlite3VdbeMemSetInt64(pMem+3, pOp->p2); |
| 79636 | + sqlite3VdbeMemSetInt64(pMem+4, pOp->p3); |
| 79637 | + /* pMem+5 for p4 is done last */ |
| 79638 | + sqlite3VdbeMemSetInt64(pMem+6, pOp->p5); |
| 79401 | 79639 | #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; |
| 79640 | + { |
| 79641 | + char *zCom = sqlite3VdbeDisplayComment(db, pOp, zP4); |
| 79642 | + sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free); |
| 79643 | + } |
| 79644 | +#else |
| 79645 | + sqlite3VdbeMemSetNull(pMem+7); |
| 79646 | +#endif |
| 79647 | + sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free); |
| 79648 | + p->nResColumn = 8; |
| 79649 | + } |
| 79650 | + p->pResultSet = pMem; |
| 79651 | + if( db->mallocFailed ){ |
| 79652 | + p->rc = SQLITE_NOMEM; |
| 79653 | + rc = SQLITE_ERROR; |
| 79654 | + }else{ |
| 79655 | + p->rc = SQLITE_OK; |
| 79656 | + rc = SQLITE_ROW; |
| 79657 | + } |
| 79418 | 79658 | } |
| 79419 | 79659 | } |
| 79420 | 79660 | return rc; |
| 79421 | 79661 | } |
| 79422 | 79662 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| | @@ -79982,12 +80222,13 @@ |
| 79982 | 80222 | int retryCount = 0; |
| 79983 | 80223 | int nMainFile; |
| 79984 | 80224 | |
| 79985 | 80225 | /* Select a master journal file name */ |
| 79986 | 80226 | nMainFile = sqlite3Strlen30(zMainFile); |
| 79987 | | - zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz%c%c", zMainFile, 0, 0); |
| 80227 | + zMaster = sqlite3MPrintf(db, "%.4c%s%.16c", 0,zMainFile,0); |
| 79988 | 80228 | if( zMaster==0 ) return SQLITE_NOMEM_BKPT; |
| 80229 | + zMaster += 4; |
| 79989 | 80230 | do { |
| 79990 | 80231 | u32 iRandom; |
| 79991 | 80232 | if( retryCount ){ |
| 79992 | 80233 | if( retryCount>100 ){ |
| 79993 | 80234 | sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); |
| | @@ -80013,11 +80254,11 @@ |
| 80013 | 80254 | SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 80014 | 80255 | SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
| 80015 | 80256 | ); |
| 80016 | 80257 | } |
| 80017 | 80258 | if( rc!=SQLITE_OK ){ |
| 80018 | | - sqlite3DbFree(db, zMaster); |
| 80259 | + sqlite3DbFree(db, zMaster-4); |
| 80019 | 80260 | return rc; |
| 80020 | 80261 | } |
| 80021 | 80262 | |
| 80022 | 80263 | /* Write the name of each database file in the transaction into the new |
| 80023 | 80264 | ** master journal file. If an error occurs at this point close |
| | @@ -80036,11 +80277,11 @@ |
| 80036 | 80277 | rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 80037 | 80278 | offset += sqlite3Strlen30(zFile)+1; |
| 80038 | 80279 | if( rc!=SQLITE_OK ){ |
| 80039 | 80280 | sqlite3OsCloseFree(pMaster); |
| 80040 | 80281 | sqlite3OsDelete(pVfs, zMaster, 0); |
| 80041 | | - sqlite3DbFree(db, zMaster); |
| 80282 | + sqlite3DbFree(db, zMaster-4); |
| 80042 | 80283 | return rc; |
| 80043 | 80284 | } |
| 80044 | 80285 | } |
| 80045 | 80286 | } |
| 80046 | 80287 | |
| | @@ -80050,11 +80291,11 @@ |
| 80050 | 80291 | if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) |
| 80051 | 80292 | && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) |
| 80052 | 80293 | ){ |
| 80053 | 80294 | sqlite3OsCloseFree(pMaster); |
| 80054 | 80295 | sqlite3OsDelete(pVfs, zMaster, 0); |
| 80055 | | - sqlite3DbFree(db, zMaster); |
| 80296 | + sqlite3DbFree(db, zMaster-4); |
| 80056 | 80297 | return rc; |
| 80057 | 80298 | } |
| 80058 | 80299 | |
| 80059 | 80300 | /* Sync all the db files involved in the transaction. The same call |
| 80060 | 80301 | ** sets the master journal pointer in each individual journal. If |
| | @@ -80073,20 +80314,20 @@ |
| 80073 | 80314 | } |
| 80074 | 80315 | } |
| 80075 | 80316 | sqlite3OsCloseFree(pMaster); |
| 80076 | 80317 | assert( rc!=SQLITE_BUSY ); |
| 80077 | 80318 | if( rc!=SQLITE_OK ){ |
| 80078 | | - sqlite3DbFree(db, zMaster); |
| 80319 | + sqlite3DbFree(db, zMaster-4); |
| 80079 | 80320 | return rc; |
| 80080 | 80321 | } |
| 80081 | 80322 | |
| 80082 | 80323 | /* Delete the master journal file. This commits the transaction. After |
| 80083 | 80324 | ** doing this the directory is synced again before any individual |
| 80084 | 80325 | ** transaction files are deleted. |
| 80085 | 80326 | */ |
| 80086 | 80327 | rc = sqlite3OsDelete(pVfs, zMaster, 1); |
| 80087 | | - sqlite3DbFree(db, zMaster); |
| 80328 | + sqlite3DbFree(db, zMaster-4); |
| 80088 | 80329 | zMaster = 0; |
| 80089 | 80330 | if( rc ){ |
| 80090 | 80331 | return rc; |
| 80091 | 80332 | } |
| 80092 | 80333 | |
| | @@ -87998,32 +88239,38 @@ |
| 87998 | 88239 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 87999 | 88240 | REGISTER_TRACE(pOp->p3, pOut); |
| 88000 | 88241 | break; |
| 88001 | 88242 | } |
| 88002 | 88243 | |
| 88003 | | -/* Opcode: Count P1 P2 * * * |
| 88244 | +/* Opcode: Count P1 P2 p3 * * |
| 88004 | 88245 | ** Synopsis: r[P2]=count() |
| 88005 | 88246 | ** |
| 88006 | 88247 | ** Store the number of entries (an integer value) in the table or index |
| 88007 | | -** opened by cursor P1 in register P2 |
| 88248 | +** opened by cursor P1 in register P2. |
| 88249 | +** |
| 88250 | +** If P3==0, then an exact count is obtained, which involves visiting |
| 88251 | +** every btree page of the table. But if P3 is non-zero, an estimate |
| 88252 | +** is returned based on the current cursor position. |
| 88008 | 88253 | */ |
| 88009 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 88010 | 88254 | case OP_Count: { /* out2 */ |
| 88011 | 88255 | i64 nEntry; |
| 88012 | 88256 | BtCursor *pCrsr; |
| 88013 | 88257 | |
| 88014 | 88258 | assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE ); |
| 88015 | 88259 | pCrsr = p->apCsr[pOp->p1]->uc.pCursor; |
| 88016 | 88260 | 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; |
| 88261 | + if( pOp->p3 ){ |
| 88262 | + nEntry = sqlite3BtreeRowCountEst(pCrsr); |
| 88263 | + }else{ |
| 88264 | + nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 88265 | + rc = sqlite3BtreeCount(db, pCrsr, &nEntry); |
| 88266 | + if( rc ) goto abort_due_to_error; |
| 88267 | + } |
| 88020 | 88268 | pOut = out2Prerelease(p, pOp); |
| 88021 | 88269 | pOut->u.i = nEntry; |
| 88022 | 88270 | goto check_for_interrupt; |
| 88023 | 88271 | } |
| 88024 | | -#endif |
| 88025 | 88272 | |
| 88026 | 88273 | /* Opcode: Savepoint P1 * * P4 * |
| 88027 | 88274 | ** |
| 88028 | 88275 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 88029 | 88276 | ** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| | @@ -90457,16 +90704,23 @@ |
| 90457 | 90704 | rc = sqlite3VdbeSorterWrite(pC, pIn2); |
| 90458 | 90705 | if( rc) goto abort_due_to_error; |
| 90459 | 90706 | break; |
| 90460 | 90707 | } |
| 90461 | 90708 | |
| 90462 | | -/* Opcode: IdxDelete P1 P2 P3 * * |
| 90709 | +/* Opcode: IdxDelete P1 P2 P3 * P5 |
| 90463 | 90710 | ** Synopsis: key=r[P2@P3] |
| 90464 | 90711 | ** |
| 90465 | 90712 | ** The content of P3 registers starting at register P2 form |
| 90466 | 90713 | ** an unpacked index key. This opcode removes that entry from the |
| 90467 | 90714 | ** index opened by cursor P1. |
| 90715 | +** |
| 90716 | +** If P5 is not zero, then raise an SQLITE_CORRUPT_INDEX error |
| 90717 | +** if no matching index entry is found. This happens when running |
| 90718 | +** an UPDATE or DELETE statement and the index entry to be updated |
| 90719 | +** or deleted is not found. For some uses of IdxDelete |
| 90720 | +** (example: the EXCEPT operator) it does not matter that no matching |
| 90721 | +** entry is found. For those cases, P5 is zero. |
| 90468 | 90722 | */ |
| 90469 | 90723 | case OP_IdxDelete: { |
| 90470 | 90724 | VdbeCursor *pC; |
| 90471 | 90725 | BtCursor *pCrsr; |
| 90472 | 90726 | int res; |
| | @@ -90479,20 +90733,22 @@ |
| 90479 | 90733 | assert( pC!=0 ); |
| 90480 | 90734 | assert( pC->eCurType==CURTYPE_BTREE ); |
| 90481 | 90735 | sqlite3VdbeIncrWriteCounter(p, pC); |
| 90482 | 90736 | pCrsr = pC->uc.pCursor; |
| 90483 | 90737 | assert( pCrsr!=0 ); |
| 90484 | | - assert( pOp->p5==0 ); |
| 90485 | 90738 | r.pKeyInfo = pC->pKeyInfo; |
| 90486 | 90739 | r.nField = (u16)pOp->p3; |
| 90487 | 90740 | r.default_rc = 0; |
| 90488 | 90741 | r.aMem = &aMem[pOp->p2]; |
| 90489 | 90742 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
| 90490 | 90743 | if( rc ) goto abort_due_to_error; |
| 90491 | 90744 | if( res==0 ){ |
| 90492 | 90745 | rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE); |
| 90493 | 90746 | if( rc ) goto abort_due_to_error; |
| 90747 | + }else if( pOp->p5 ){ |
| 90748 | + rc = SQLITE_CORRUPT_INDEX; |
| 90749 | + goto abort_due_to_error; |
| 90494 | 90750 | } |
| 90495 | 90751 | assert( pC->deferredMoveto==0 ); |
| 90496 | 90752 | pC->cacheStatus = CACHE_STALE; |
| 90497 | 90753 | pC->seekResult = 0; |
| 90498 | 90754 | break; |
| | @@ -96093,10 +96349,435 @@ |
| 96093 | 96349 | *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2); |
| 96094 | 96350 | return SQLITE_OK; |
| 96095 | 96351 | } |
| 96096 | 96352 | |
| 96097 | 96353 | /************** End of vdbesort.c ********************************************/ |
| 96354 | +/************** Begin file vdbevtab.c ****************************************/ |
| 96355 | +/* |
| 96356 | +** 2020-03-23 |
| 96357 | +** |
| 96358 | +** The author disclaims copyright to this source code. In place of |
| 96359 | +** a legal notice, here is a blessing: |
| 96360 | +** |
| 96361 | +** May you do good and not evil. |
| 96362 | +** May you find forgiveness for yourself and forgive others. |
| 96363 | +** May you share freely, never taking more than you give. |
| 96364 | +** |
| 96365 | +************************************************************************* |
| 96366 | +** |
| 96367 | +** This file implements virtual-tables for examining the bytecode content |
| 96368 | +** of a prepared statement. |
| 96369 | +*/ |
| 96370 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 96371 | +/* #include "sqliteInt.h" */ |
| 96372 | +/* #include "vdbeInt.h" */ |
| 96373 | + |
| 96374 | +/* An instance of the bytecode() table-valued function. |
| 96375 | +*/ |
| 96376 | +typedef struct bytecodevtab bytecodevtab; |
| 96377 | +struct bytecodevtab { |
| 96378 | + sqlite3_vtab base; /* Base class - must be first */ |
| 96379 | + sqlite3 *db; /* Database connection */ |
| 96380 | + int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */ |
| 96381 | +}; |
| 96382 | + |
| 96383 | +/* A cursor for scanning through the bytecode |
| 96384 | +*/ |
| 96385 | +typedef struct bytecodevtab_cursor bytecodevtab_cursor; |
| 96386 | +struct bytecodevtab_cursor { |
| 96387 | + sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 96388 | + sqlite3_stmt *pStmt; /* The statement whose bytecode is displayed */ |
| 96389 | + int iRowid; /* The rowid of the output table */ |
| 96390 | + int iAddr; /* Address */ |
| 96391 | + int needFinalize; /* Cursors owns pStmt and must finalize it */ |
| 96392 | + int showSubprograms; /* Provide a listing of subprograms */ |
| 96393 | + Op *aOp; /* Operand array */ |
| 96394 | + char *zP4; /* Rendered P4 value */ |
| 96395 | + const char *zType; /* tables_used.type */ |
| 96396 | + const char *zSchema; /* tables_used.schema */ |
| 96397 | + const char *zName; /* tables_used.name */ |
| 96398 | + Mem sub; /* Subprograms */ |
| 96399 | +}; |
| 96400 | + |
| 96401 | +/* |
| 96402 | +** Create a new bytecode() table-valued function. |
| 96403 | +*/ |
| 96404 | +static int bytecodevtabConnect( |
| 96405 | + sqlite3 *db, |
| 96406 | + void *pAux, |
| 96407 | + int argc, const char *const*argv, |
| 96408 | + sqlite3_vtab **ppVtab, |
| 96409 | + char **pzErr |
| 96410 | +){ |
| 96411 | + bytecodevtab *pNew; |
| 96412 | + int rc; |
| 96413 | + int isTabUsed = pAux!=0; |
| 96414 | + const char *azSchema[2] = { |
| 96415 | + /* bytecode() schema */ |
| 96416 | + "CREATE TABLE x(" |
| 96417 | + "addr INT," |
| 96418 | + "opcode TEXT," |
| 96419 | + "p1 INT," |
| 96420 | + "p2 INT," |
| 96421 | + "p3 INT," |
| 96422 | + "p4 TEXT," |
| 96423 | + "p5 INT," |
| 96424 | + "comment TEXT," |
| 96425 | + "subprog TEXT," |
| 96426 | + "stmt HIDDEN" |
| 96427 | + ");", |
| 96428 | + |
| 96429 | + /* Tables_used() schema */ |
| 96430 | + "CREATE TABLE x(" |
| 96431 | + "type TEXT," |
| 96432 | + "schema TEXT," |
| 96433 | + "name TEXT," |
| 96434 | + "wr INT," |
| 96435 | + "subprog TEXT," |
| 96436 | + "stmt HIDDEN" |
| 96437 | + ");" |
| 96438 | + }; |
| 96439 | + |
| 96440 | + rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]); |
| 96441 | + if( rc==SQLITE_OK ){ |
| 96442 | + pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 96443 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 96444 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 96445 | + memset(pNew, 0, sizeof(*pNew)); |
| 96446 | + pNew->db = db; |
| 96447 | + pNew->bTablesUsed = isTabUsed*2; |
| 96448 | + } |
| 96449 | + return rc; |
| 96450 | +} |
| 96451 | + |
| 96452 | +/* |
| 96453 | +** This method is the destructor for bytecodevtab objects. |
| 96454 | +*/ |
| 96455 | +static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){ |
| 96456 | + bytecodevtab *p = (bytecodevtab*)pVtab; |
| 96457 | + sqlite3_free(p); |
| 96458 | + return SQLITE_OK; |
| 96459 | +} |
| 96460 | + |
| 96461 | +/* |
| 96462 | +** Constructor for a new bytecodevtab_cursor object. |
| 96463 | +*/ |
| 96464 | +static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 96465 | + bytecodevtab *pVTab = (bytecodevtab*)p; |
| 96466 | + bytecodevtab_cursor *pCur; |
| 96467 | + pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 96468 | + if( pCur==0 ) return SQLITE_NOMEM; |
| 96469 | + memset(pCur, 0, sizeof(*pCur)); |
| 96470 | + sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1); |
| 96471 | + *ppCursor = &pCur->base; |
| 96472 | + return SQLITE_OK; |
| 96473 | +} |
| 96474 | + |
| 96475 | +/* |
| 96476 | +** Clear all internal content from a bytecodevtab cursor. |
| 96477 | +*/ |
| 96478 | +static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){ |
| 96479 | + sqlite3_free(pCur->zP4); |
| 96480 | + pCur->zP4 = 0; |
| 96481 | + sqlite3VdbeMemRelease(&pCur->sub); |
| 96482 | + sqlite3VdbeMemSetNull(&pCur->sub); |
| 96483 | + if( pCur->needFinalize ){ |
| 96484 | + sqlite3_finalize(pCur->pStmt); |
| 96485 | + } |
| 96486 | + pCur->pStmt = 0; |
| 96487 | + pCur->needFinalize = 0; |
| 96488 | + pCur->zType = 0; |
| 96489 | + pCur->zSchema = 0; |
| 96490 | + pCur->zName = 0; |
| 96491 | +} |
| 96492 | + |
| 96493 | +/* |
| 96494 | +** Destructor for a bytecodevtab_cursor. |
| 96495 | +*/ |
| 96496 | +static int bytecodevtabClose(sqlite3_vtab_cursor *cur){ |
| 96497 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96498 | + bytecodevtabCursorClear(pCur); |
| 96499 | + sqlite3_free(pCur); |
| 96500 | + return SQLITE_OK; |
| 96501 | +} |
| 96502 | + |
| 96503 | + |
| 96504 | +/* |
| 96505 | +** Advance a bytecodevtab_cursor to its next row of output. |
| 96506 | +*/ |
| 96507 | +static int bytecodevtabNext(sqlite3_vtab_cursor *cur){ |
| 96508 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96509 | + bytecodevtab *pTab = (bytecodevtab*)cur->pVtab; |
| 96510 | + int rc; |
| 96511 | + if( pCur->zP4 ){ |
| 96512 | + sqlite3_free(pCur->zP4); |
| 96513 | + pCur->zP4 = 0; |
| 96514 | + } |
| 96515 | + if( pCur->zName ){ |
| 96516 | + pCur->zName = 0; |
| 96517 | + pCur->zType = 0; |
| 96518 | + pCur->zSchema = 0; |
| 96519 | + } |
| 96520 | + rc = sqlite3VdbeNextOpcode( |
| 96521 | + (Vdbe*)pCur->pStmt, |
| 96522 | + pCur->showSubprograms ? &pCur->sub : 0, |
| 96523 | + pTab->bTablesUsed, |
| 96524 | + &pCur->iRowid, |
| 96525 | + &pCur->iAddr, |
| 96526 | + &pCur->aOp); |
| 96527 | + if( rc!=SQLITE_OK ){ |
| 96528 | + sqlite3VdbeMemSetNull(&pCur->sub); |
| 96529 | + pCur->aOp = 0; |
| 96530 | + } |
| 96531 | + return SQLITE_OK; |
| 96532 | +} |
| 96533 | + |
| 96534 | +/* |
| 96535 | +** Return TRUE if the cursor has been moved off of the last |
| 96536 | +** row of output. |
| 96537 | +*/ |
| 96538 | +static int bytecodevtabEof(sqlite3_vtab_cursor *cur){ |
| 96539 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96540 | + return pCur->aOp==0; |
| 96541 | +} |
| 96542 | + |
| 96543 | +/* |
| 96544 | +** Return values of columns for the row at which the bytecodevtab_cursor |
| 96545 | +** is currently pointing. |
| 96546 | +*/ |
| 96547 | +static int bytecodevtabColumn( |
| 96548 | + sqlite3_vtab_cursor *cur, /* The cursor */ |
| 96549 | + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 96550 | + int i /* Which column to return */ |
| 96551 | +){ |
| 96552 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96553 | + bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab; |
| 96554 | + Op *pOp = pCur->aOp + pCur->iAddr; |
| 96555 | + if( pVTab->bTablesUsed ){ |
| 96556 | + if( i==4 ){ |
| 96557 | + i = 8; |
| 96558 | + }else{ |
| 96559 | + if( i<=2 && pCur->zType==0 ){ |
| 96560 | + Schema *pSchema; |
| 96561 | + HashElem *k; |
| 96562 | + int iDb = pOp->p3; |
| 96563 | + int iRoot = pOp->p2; |
| 96564 | + sqlite3 *db = pVTab->db; |
| 96565 | + pSchema = db->aDb[iDb].pSchema; |
| 96566 | + pCur->zSchema = db->aDb[iDb].zDbSName; |
| 96567 | + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 96568 | + Table *pTab = (Table*)sqliteHashData(k); |
| 96569 | + if( !IsVirtual(pTab) && pTab->tnum==iRoot ){ |
| 96570 | + pCur->zName = pTab->zName; |
| 96571 | + pCur->zType = "table"; |
| 96572 | + break; |
| 96573 | + } |
| 96574 | + } |
| 96575 | + if( pCur->zName==0 ){ |
| 96576 | + for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){ |
| 96577 | + Index *pIdx = (Index*)sqliteHashData(k); |
| 96578 | + if( pIdx->tnum==iRoot ){ |
| 96579 | + pCur->zName = pIdx->zName; |
| 96580 | + pCur->zType = "index"; |
| 96581 | + } |
| 96582 | + } |
| 96583 | + } |
| 96584 | + } |
| 96585 | + i += 10; |
| 96586 | + } |
| 96587 | + } |
| 96588 | + switch( i ){ |
| 96589 | + case 0: /* addr */ |
| 96590 | + sqlite3_result_int(ctx, pCur->iAddr); |
| 96591 | + break; |
| 96592 | + case 1: /* opcode */ |
| 96593 | + sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode), |
| 96594 | + -1, SQLITE_STATIC); |
| 96595 | + break; |
| 96596 | + case 2: /* p1 */ |
| 96597 | + sqlite3_result_int(ctx, pOp->p1); |
| 96598 | + break; |
| 96599 | + case 3: /* p2 */ |
| 96600 | + sqlite3_result_int(ctx, pOp->p2); |
| 96601 | + break; |
| 96602 | + case 4: /* p3 */ |
| 96603 | + sqlite3_result_int(ctx, pOp->p3); |
| 96604 | + break; |
| 96605 | + case 5: /* p4 */ |
| 96606 | + case 7: /* comment */ |
| 96607 | + if( pCur->zP4==0 ){ |
| 96608 | + pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp); |
| 96609 | + } |
| 96610 | + if( i==5 ){ |
| 96611 | + sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC); |
| 96612 | + }else{ |
| 96613 | +#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 96614 | + char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4); |
| 96615 | + sqlite3_result_text(ctx, zCom, -1, sqlite3_free); |
| 96616 | +#endif |
| 96617 | + } |
| 96618 | + break; |
| 96619 | + case 6: /* p5 */ |
| 96620 | + sqlite3_result_int(ctx, pOp->p5); |
| 96621 | + break; |
| 96622 | + case 8: { /* subprog */ |
| 96623 | + Op *aOp = pCur->aOp; |
| 96624 | + assert( aOp[0].opcode==OP_Init ); |
| 96625 | + assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 ); |
| 96626 | + if( pCur->iRowid==pCur->iAddr+1 ){ |
| 96627 | + break; /* Result is NULL for the main program */ |
| 96628 | + }else if( aOp[0].p4.z!=0 ){ |
| 96629 | + sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC); |
| 96630 | + }else{ |
| 96631 | + sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC); |
| 96632 | + } |
| 96633 | + break; |
| 96634 | + } |
| 96635 | + case 10: /* tables_used.type */ |
| 96636 | + sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC); |
| 96637 | + break; |
| 96638 | + case 11: /* tables_used.schema */ |
| 96639 | + sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC); |
| 96640 | + break; |
| 96641 | + case 12: /* tables_used.name */ |
| 96642 | + sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC); |
| 96643 | + break; |
| 96644 | + case 13: /* tables_used.wr */ |
| 96645 | + sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite); |
| 96646 | + break; |
| 96647 | + } |
| 96648 | + return SQLITE_OK; |
| 96649 | +} |
| 96650 | + |
| 96651 | +/* |
| 96652 | +** Return the rowid for the current row. In this implementation, the |
| 96653 | +** rowid is the same as the output value. |
| 96654 | +*/ |
| 96655 | +static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 96656 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur; |
| 96657 | + *pRowid = pCur->iRowid; |
| 96658 | + return SQLITE_OK; |
| 96659 | +} |
| 96660 | + |
| 96661 | +/* |
| 96662 | +** Initialize a cursor. |
| 96663 | +** |
| 96664 | +** idxNum==0 means show all subprograms |
| 96665 | +** idxNum==1 means show only the main bytecode and omit subprograms. |
| 96666 | +*/ |
| 96667 | +static int bytecodevtabFilter( |
| 96668 | + sqlite3_vtab_cursor *pVtabCursor, |
| 96669 | + int idxNum, const char *idxStr, |
| 96670 | + int argc, sqlite3_value **argv |
| 96671 | +){ |
| 96672 | + bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor; |
| 96673 | + bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab; |
| 96674 | + int rc = SQLITE_OK; |
| 96675 | + |
| 96676 | + bytecodevtabCursorClear(pCur); |
| 96677 | + pCur->iRowid = 0; |
| 96678 | + pCur->iAddr = 0; |
| 96679 | + pCur->showSubprograms = idxNum==0; |
| 96680 | + assert( argc==1 ); |
| 96681 | + if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){ |
| 96682 | + const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 96683 | + if( zSql==0 ){ |
| 96684 | + rc = SQLITE_NOMEM; |
| 96685 | + }else{ |
| 96686 | + rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0); |
| 96687 | + pCur->needFinalize = 1; |
| 96688 | + } |
| 96689 | + }else{ |
| 96690 | + pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer"); |
| 96691 | + } |
| 96692 | + if( pCur->pStmt==0 ){ |
| 96693 | + pVTab->base.zErrMsg = sqlite3_mprintf( |
| 96694 | + "argument to %s() is not a valid SQL statement", |
| 96695 | + pVTab->bTablesUsed ? "tables_used" : "bytecode" |
| 96696 | + ); |
| 96697 | + rc = SQLITE_ERROR; |
| 96698 | + }else{ |
| 96699 | + bytecodevtabNext(pVtabCursor); |
| 96700 | + } |
| 96701 | + return rc; |
| 96702 | +} |
| 96703 | + |
| 96704 | +/* |
| 96705 | +** We must have a single stmt=? constraint that will be passed through |
| 96706 | +** into the xFilter method. If there is no valid stmt=? constraint, |
| 96707 | +** then return an SQLITE_CONSTRAINT error. |
| 96708 | +*/ |
| 96709 | +static int bytecodevtabBestIndex( |
| 96710 | + sqlite3_vtab *tab, |
| 96711 | + sqlite3_index_info *pIdxInfo |
| 96712 | +){ |
| 96713 | + int i; |
| 96714 | + int rc = SQLITE_CONSTRAINT; |
| 96715 | + struct sqlite3_index_constraint *p; |
| 96716 | + bytecodevtab *pVTab = (bytecodevtab*)tab; |
| 96717 | + int iBaseCol = pVTab->bTablesUsed ? 4 : 8; |
| 96718 | + pIdxInfo->estimatedCost = (double)100; |
| 96719 | + pIdxInfo->estimatedRows = 100; |
| 96720 | + pIdxInfo->idxNum = 0; |
| 96721 | + for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){ |
| 96722 | + if( p->usable==0 ) continue; |
| 96723 | + if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){ |
| 96724 | + rc = SQLITE_OK; |
| 96725 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 96726 | + pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 96727 | + } |
| 96728 | + if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){ |
| 96729 | + pIdxInfo->aConstraintUsage[i].omit = 1; |
| 96730 | + pIdxInfo->idxNum = 1; |
| 96731 | + } |
| 96732 | + } |
| 96733 | + return rc; |
| 96734 | +} |
| 96735 | + |
| 96736 | +/* |
| 96737 | +** This following structure defines all the methods for the |
| 96738 | +** virtual table. |
| 96739 | +*/ |
| 96740 | +static sqlite3_module bytecodevtabModule = { |
| 96741 | + /* iVersion */ 0, |
| 96742 | + /* xCreate */ 0, |
| 96743 | + /* xConnect */ bytecodevtabConnect, |
| 96744 | + /* xBestIndex */ bytecodevtabBestIndex, |
| 96745 | + /* xDisconnect */ bytecodevtabDisconnect, |
| 96746 | + /* xDestroy */ 0, |
| 96747 | + /* xOpen */ bytecodevtabOpen, |
| 96748 | + /* xClose */ bytecodevtabClose, |
| 96749 | + /* xFilter */ bytecodevtabFilter, |
| 96750 | + /* xNext */ bytecodevtabNext, |
| 96751 | + /* xEof */ bytecodevtabEof, |
| 96752 | + /* xColumn */ bytecodevtabColumn, |
| 96753 | + /* xRowid */ bytecodevtabRowid, |
| 96754 | + /* xUpdate */ 0, |
| 96755 | + /* xBegin */ 0, |
| 96756 | + /* xSync */ 0, |
| 96757 | + /* xCommit */ 0, |
| 96758 | + /* xRollback */ 0, |
| 96759 | + /* xFindMethod */ 0, |
| 96760 | + /* xRename */ 0, |
| 96761 | + /* xSavepoint */ 0, |
| 96762 | + /* xRelease */ 0, |
| 96763 | + /* xRollbackTo */ 0, |
| 96764 | + /* xShadowName */ 0 |
| 96765 | +}; |
| 96766 | + |
| 96767 | + |
| 96768 | +SQLITE_PRIVATE int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ |
| 96769 | + int rc; |
| 96770 | + rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0); |
| 96771 | + if( rc==SQLITE_OK ){ |
| 96772 | + rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db); |
| 96773 | + } |
| 96774 | + return rc; |
| 96775 | +} |
| 96776 | +#endif /* SQLITE_ENABLE_BYTECODE_VTAB */ |
| 96777 | + |
| 96778 | +/************** End of vdbevtab.c ********************************************/ |
| 96098 | 96779 | /************** Begin file memjournal.c **************************************/ |
| 96099 | 96780 | /* |
| 96100 | 96781 | ** 2008 October 7 |
| 96101 | 96782 | ** |
| 96102 | 96783 | ** The author disclaims copyright to this source code. In place of |
| | @@ -98739,11 +99420,11 @@ |
| 98739 | 99420 | ** SELECT * FROM t1 WHERE (select a from t1); |
| 98740 | 99421 | */ |
| 98741 | 99422 | SQLITE_PRIVATE char sqlite3ExprAffinity(const Expr *pExpr){ |
| 98742 | 99423 | int op; |
| 98743 | 99424 | while( ExprHasProperty(pExpr, EP_Skip) ){ |
| 98744 | | - assert( pExpr->op==TK_COLLATE ); |
| 99425 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98745 | 99426 | pExpr = pExpr->pLeft; |
| 98746 | 99427 | assert( pExpr!=0 ); |
| 98747 | 99428 | } |
| 98748 | 99429 | op = pExpr->op; |
| 98749 | 99430 | if( op==TK_SELECT ){ |
| | @@ -98806,11 +99487,11 @@ |
| 98806 | 99487 | /* |
| 98807 | 99488 | ** Skip over any TK_COLLATE operators. |
| 98808 | 99489 | */ |
| 98809 | 99490 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){ |
| 98810 | 99491 | while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ |
| 98811 | | - assert( pExpr->op==TK_COLLATE ); |
| 99492 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98812 | 99493 | pExpr = pExpr->pLeft; |
| 98813 | 99494 | } |
| 98814 | 99495 | return pExpr; |
| 98815 | 99496 | } |
| 98816 | 99497 | |
| | @@ -98825,11 +99506,11 @@ |
| 98825 | 99506 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 98826 | 99507 | assert( pExpr->x.pList->nExpr>0 ); |
| 98827 | 99508 | assert( pExpr->op==TK_FUNCTION ); |
| 98828 | 99509 | pExpr = pExpr->x.pList->a[0].pExpr; |
| 98829 | 99510 | }else{ |
| 98830 | | - assert( pExpr->op==TK_COLLATE ); |
| 99511 | + assert( pExpr->op==TK_COLLATE || pExpr->op==TK_IF_NULL_ROW ); |
| 98831 | 99512 | pExpr = pExpr->pLeft; |
| 98832 | 99513 | } |
| 98833 | 99514 | } |
| 98834 | 99515 | return pExpr; |
| 98835 | 99516 | } |
| | @@ -103176,11 +103857,11 @@ |
| 103176 | 103857 | assert( pExpr->affExpr==OE_Rollback |
| 103177 | 103858 | || pExpr->affExpr==OE_Abort |
| 103178 | 103859 | || pExpr->affExpr==OE_Fail |
| 103179 | 103860 | || pExpr->affExpr==OE_Ignore |
| 103180 | 103861 | ); |
| 103181 | | - if( !pParse->pTriggerTab ){ |
| 103862 | + if( !pParse->pTriggerTab && !pParse->nested ){ |
| 103182 | 103863 | sqlite3ErrorMsg(pParse, |
| 103183 | 103864 | "RAISE() may only be used within a trigger-program"); |
| 103184 | 103865 | return 0; |
| 103185 | 103866 | } |
| 103186 | 103867 | if( pExpr->affExpr==OE_Abort ){ |
| | @@ -103190,12 +103871,13 @@ |
| 103190 | 103871 | if( pExpr->affExpr==OE_Ignore ){ |
| 103191 | 103872 | sqlite3VdbeAddOp4( |
| 103192 | 103873 | v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
| 103193 | 103874 | VdbeCoverage(v); |
| 103194 | 103875 | }else{ |
| 103195 | | - sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
| 103196 | | - pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103876 | + sqlite3HaltConstraint(pParse, |
| 103877 | + pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, |
| 103878 | + pExpr->affExpr, pExpr->u.zToken, 0, 0); |
| 103197 | 103879 | } |
| 103198 | 103880 | |
| 103199 | 103881 | break; |
| 103200 | 103882 | } |
| 103201 | 103883 | #endif |
| | @@ -104946,10 +105628,26 @@ |
| 104946 | 105628 | exit_rename_table: |
| 104947 | 105629 | sqlite3SrcListDelete(db, pSrc); |
| 104948 | 105630 | sqlite3DbFree(db, zName); |
| 104949 | 105631 | db->mDbFlags = savedDbFlags; |
| 104950 | 105632 | } |
| 105633 | + |
| 105634 | +/* |
| 105635 | +** Write code that will raise an error if the table described by |
| 105636 | +** zDb and zTab is not empty. |
| 105637 | +*/ |
| 105638 | +static void sqlite3ErrorIfNotEmpty( |
| 105639 | + Parse *pParse, /* Parsing context */ |
| 105640 | + const char *zDb, /* Schema holding the table */ |
| 105641 | + const char *zTab, /* Table to check for empty */ |
| 105642 | + const char *zErr /* Error message text */ |
| 105643 | +){ |
| 105644 | + sqlite3NestedParse(pParse, |
| 105645 | + "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"", |
| 105646 | + zErr, zDb, zTab |
| 105647 | + ); |
| 105648 | +} |
| 104951 | 105649 | |
| 104952 | 105650 | /* |
| 104953 | 105651 | ** This function is called after an "ALTER TABLE ... ADD" statement |
| 104954 | 105652 | ** has been parsed. Argument pColDef contains the text of the new |
| 104955 | 105653 | ** column definition. |
| | @@ -104999,11 +105697,12 @@ |
| 104999 | 105697 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 105000 | 105698 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 105001 | 105699 | return; |
| 105002 | 105700 | } |
| 105003 | 105701 | if( pNew->pIndex ){ |
| 105004 | | - sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 105702 | + sqlite3ErrorMsg(pParse, |
| 105703 | + "Cannot add a UNIQUE column"); |
| 105005 | 105704 | return; |
| 105006 | 105705 | } |
| 105007 | 105706 | if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){ |
| 105008 | 105707 | /* If the default value for the new column was specified with a |
| 105009 | 105708 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| | @@ -105012,19 +105711,18 @@ |
| 105012 | 105711 | assert( pDflt==0 || pDflt->op==TK_SPAN ); |
| 105013 | 105712 | if( pDflt && pDflt->pLeft->op==TK_NULL ){ |
| 105014 | 105713 | pDflt = 0; |
| 105015 | 105714 | } |
| 105016 | 105715 | if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ |
| 105017 | | - sqlite3ErrorMsg(pParse, |
| 105716 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105018 | 105717 | "Cannot add a REFERENCES column with non-NULL default value"); |
| 105019 | | - return; |
| 105020 | 105718 | } |
| 105021 | 105719 | if( pCol->notNull && !pDflt ){ |
| 105022 | | - sqlite3ErrorMsg(pParse, |
| 105720 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105023 | 105721 | "Cannot add a NOT NULL column with default value NULL"); |
| 105024 | | - return; |
| 105025 | 105722 | } |
| 105723 | + |
| 105026 | 105724 | |
| 105027 | 105725 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 105028 | 105726 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 105029 | 105727 | */ |
| 105030 | 105728 | if( pDflt ){ |
| | @@ -105035,18 +105733,17 @@ |
| 105035 | 105733 | if( rc!=SQLITE_OK ){ |
| 105036 | 105734 | assert( db->mallocFailed == 1 ); |
| 105037 | 105735 | return; |
| 105038 | 105736 | } |
| 105039 | 105737 | if( !pVal ){ |
| 105040 | | - sqlite3ErrorMsg(pParse,"Cannot add a column with non-constant default"); |
| 105041 | | - return; |
| 105738 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, |
| 105739 | + "Cannot add a column with non-constant default"); |
| 105042 | 105740 | } |
| 105043 | 105741 | sqlite3ValueFree(pVal); |
| 105044 | 105742 | } |
| 105045 | 105743 | }else if( pCol->colFlags & COLFLAG_STORED ){ |
| 105046 | | - sqlite3ErrorMsg(pParse, "cannot add a STORED column"); |
| 105047 | | - return; |
| 105744 | + sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column"); |
| 105048 | 105745 | } |
| 105049 | 105746 | |
| 105050 | 105747 | |
| 105051 | 105748 | /* Modify the CREATE TABLE statement. */ |
| 105052 | 105749 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
| | @@ -106605,10 +107302,15 @@ |
| 106605 | 107302 | sqlite3 *db = pParse->db; |
| 106606 | 107303 | Db *pDb; |
| 106607 | 107304 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 106608 | 107305 | int aRoot[ArraySize(aTable)]; |
| 106609 | 107306 | u8 aCreateTbl[ArraySize(aTable)]; |
| 107307 | +#ifdef SQLITE_ENABLE_STAT4 |
| 107308 | + const int nToOpen = OptimizationEnabled(db,SQLITE_Stat4) ? 2 : 1; |
| 107309 | +#else |
| 107310 | + const int nToOpen = 1; |
| 107311 | +#endif |
| 106610 | 107312 | |
| 106611 | 107313 | if( v==0 ) return; |
| 106612 | 107314 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 106613 | 107315 | assert( sqlite3VdbeDb(v)==db ); |
| 106614 | 107316 | pDb = &db->aDb[iDb]; |
| | @@ -106617,12 +107319,13 @@ |
| 106617 | 107319 | ** if they do already exist. |
| 106618 | 107320 | */ |
| 106619 | 107321 | for(i=0; i<ArraySize(aTable); i++){ |
| 106620 | 107322 | const char *zTab = aTable[i].zName; |
| 106621 | 107323 | Table *pStat; |
| 107324 | + aCreateTbl[i] = 0; |
| 106622 | 107325 | if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){ |
| 106623 | | - if( aTable[i].zCols ){ |
| 107326 | + if( i<nToOpen ){ |
| 106624 | 107327 | /* The sqlite_statN table does not exist. Create it. Note that a |
| 106625 | 107328 | ** side-effect of the CREATE TABLE statement is to leave the rootpage |
| 106626 | 107329 | ** of the new table in register pParse->regRoot. This is important |
| 106627 | 107330 | ** because the OpenWrite opcode below will be needing it. */ |
| 106628 | 107331 | sqlite3NestedParse(pParse, |
| | @@ -106634,11 +107337,10 @@ |
| 106634 | 107337 | }else{ |
| 106635 | 107338 | /* The table already exists. If zWhere is not NULL, delete all entries |
| 106636 | 107339 | ** associated with the table zWhere. If zWhere is NULL, delete the |
| 106637 | 107340 | ** entire contents of the table. */ |
| 106638 | 107341 | aRoot[i] = pStat->tnum; |
| 106639 | | - aCreateTbl[i] = 0; |
| 106640 | 107342 | sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); |
| 106641 | 107343 | if( zWhere ){ |
| 106642 | 107344 | sqlite3NestedParse(pParse, |
| 106643 | 107345 | "DELETE FROM %Q.%s WHERE %s=%Q", |
| 106644 | 107346 | pDb->zDbSName, zTab, zWhereType, zWhere |
| | @@ -106653,11 +107355,11 @@ |
| 106653 | 107355 | } |
| 106654 | 107356 | } |
| 106655 | 107357 | } |
| 106656 | 107358 | |
| 106657 | 107359 | /* Open the sqlite_stat[134] tables for writing. */ |
| 106658 | | - for(i=0; aTable[i].zCols; i++){ |
| 107360 | + for(i=0; i<nToOpen; i++){ |
| 106659 | 107361 | assert( i<ArraySize(aTable) ); |
| 106660 | 107362 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3); |
| 106661 | 107363 | sqlite3VdbeChangeP5(v, aCreateTbl[i]); |
| 106662 | 107364 | VdbeComment((v, aTable[i].zName)); |
| 106663 | 107365 | } |
| | @@ -106692,13 +107394,16 @@ |
| 106692 | 107394 | u32 iHash; /* Tiebreaker hash */ |
| 106693 | 107395 | #endif |
| 106694 | 107396 | }; |
| 106695 | 107397 | struct StatAccum { |
| 106696 | 107398 | sqlite3 *db; /* Database connection, for malloc() */ |
| 106697 | | - tRowcnt nRow; /* Number of rows in the entire table */ |
| 107399 | + tRowcnt nEst; /* Estimated number of rows */ |
| 107400 | + tRowcnt nRow; /* Number of rows visited so far */ |
| 107401 | + int nLimit; /* Analysis row-scan limit */ |
| 106698 | 107402 | int nCol; /* Number of columns in index + pk/rowid */ |
| 106699 | 107403 | int nKeyCol; /* Number of index columns w/o the pk/rowid */ |
| 107404 | + u8 nSkipAhead; /* Number of times of skip-ahead */ |
| 106700 | 107405 | StatSample current; /* Current row as a StatSample */ |
| 106701 | 107406 | #ifdef SQLITE_ENABLE_STAT4 |
| 106702 | 107407 | tRowcnt nPSample; /* How often to do a periodic sample */ |
| 106703 | 107408 | int mxSample; /* Maximum number of samples to accumulate */ |
| 106704 | 107409 | u32 iPrn; /* Pseudo-random number used for sampling */ |
| | @@ -106774,31 +107479,32 @@ |
| 106774 | 107479 | ** Reclaim all memory of a StatAccum structure. |
| 106775 | 107480 | */ |
| 106776 | 107481 | static void statAccumDestructor(void *pOld){ |
| 106777 | 107482 | StatAccum *p = (StatAccum*)pOld; |
| 106778 | 107483 | #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); |
| 107484 | + if( p->mxSample ){ |
| 107485 | + int i; |
| 107486 | + for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i); |
| 107487 | + for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i); |
| 107488 | + sampleClear(p->db, &p->current); |
| 107489 | + } |
| 106783 | 107490 | #endif |
| 106784 | 107491 | sqlite3DbFree(p->db, p); |
| 106785 | 107492 | } |
| 106786 | 107493 | |
| 106787 | 107494 | /* |
| 106788 | | -** Implementation of the stat_init(N,K,C) SQL function. The three parameters |
| 107495 | +** Implementation of the stat_init(N,K,C,L) SQL function. The four parameters |
| 106789 | 107496 | ** are: |
| 106790 | 107497 | ** N: The number of columns in the index including the rowid/pk (note 1) |
| 106791 | 107498 | ** K: The number of columns in the index excluding the rowid/pk. |
| 106792 | | -** C: The number of rows in the index (note 2) |
| 107499 | +** C: Estimated number of rows in the index |
| 107500 | +** L: A limit on the number of rows to scan, or 0 for no-limit |
| 106793 | 107501 | ** |
| 106794 | 107502 | ** Note 1: In the special case of the covering index that implements a |
| 106795 | 107503 | ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the |
| 106796 | 107504 | ** total number of columns in the table. |
| 106797 | 107505 | ** |
| 106798 | | -** Note 2: C is only used for STAT4. |
| 106799 | | -** |
| 106800 | 107506 | ** For indexes on ordinary rowid tables, N==K+1. But for indexes on |
| 106801 | 107507 | ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the |
| 106802 | 107508 | ** PRIMARY KEY of the table. The covering index that implements the |
| 106803 | 107509 | ** original WITHOUT ROWID table as N==K as a special case. |
| 106804 | 107510 | ** |
| | @@ -106815,13 +107521,14 @@ |
| 106815 | 107521 | StatAccum *p; |
| 106816 | 107522 | int nCol; /* Number of columns in index being sampled */ |
| 106817 | 107523 | int nKeyCol; /* Number of key columns */ |
| 106818 | 107524 | int nColUp; /* nCol rounded up for alignment */ |
| 106819 | 107525 | int n; /* Bytes of space to allocate */ |
| 106820 | | - sqlite3 *db; /* Database connection */ |
| 107526 | + sqlite3 *db = sqlite3_context_db_handle(context); /* Database connection */ |
| 106821 | 107527 | #ifdef SQLITE_ENABLE_STAT4 |
| 106822 | | - int mxSample = SQLITE_STAT4_SAMPLES; |
| 107528 | + /* Maximum number of samples. 0 if STAT4 data is not collected */ |
| 107529 | + int mxSample = OptimizationEnabled(db,SQLITE_Stat4) ?SQLITE_STAT4_SAMPLES :0; |
| 106823 | 107530 | #endif |
| 106824 | 107531 | |
| 106825 | 107532 | /* Decode the three function arguments */ |
| 106826 | 107533 | UNUSED_PARAMETER(argc); |
| 106827 | 107534 | nCol = sqlite3_value_int(argv[0]); |
| | @@ -106832,39 +107539,43 @@ |
| 106832 | 107539 | assert( nKeyCol>0 ); |
| 106833 | 107540 | |
| 106834 | 107541 | /* Allocate the space required for the StatAccum object */ |
| 106835 | 107542 | n = sizeof(*p) |
| 106836 | 107543 | + sizeof(tRowcnt)*nColUp /* StatAccum.anEq */ |
| 106837 | | - + sizeof(tRowcnt)*nColUp /* StatAccum.anDLt */ |
| 107544 | + + sizeof(tRowcnt)*nColUp; /* StatAccum.anDLt */ |
| 106838 | 107545 | #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) |
| 107546 | + if( mxSample ){ |
| 107547 | + n += sizeof(tRowcnt)*nColUp /* StatAccum.anLt */ |
| 107548 | + + sizeof(StatSample)*(nCol+mxSample) /* StatAccum.aBest[], a[] */ |
| 107549 | + + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample); |
| 107550 | + } |
| 106842 | 107551 | #endif |
| 106843 | | - ; |
| 106844 | 107552 | db = sqlite3_context_db_handle(context); |
| 106845 | 107553 | p = sqlite3DbMallocZero(db, n); |
| 106846 | 107554 | if( p==0 ){ |
| 106847 | 107555 | sqlite3_result_error_nomem(context); |
| 106848 | 107556 | return; |
| 106849 | 107557 | } |
| 106850 | 107558 | |
| 106851 | 107559 | p->db = db; |
| 107560 | + p->nEst = sqlite3_value_int64(argv[2]); |
| 106852 | 107561 | p->nRow = 0; |
| 107562 | + p->nLimit = sqlite3_value_int64(argv[3]); |
| 106853 | 107563 | p->nCol = nCol; |
| 106854 | 107564 | p->nKeyCol = nKeyCol; |
| 107565 | + p->nSkipAhead = 0; |
| 106855 | 107566 | p->current.anDLt = (tRowcnt*)&p[1]; |
| 106856 | 107567 | p->current.anEq = &p->current.anDLt[nColUp]; |
| 106857 | 107568 | |
| 106858 | 107569 | #ifdef SQLITE_ENABLE_STAT4 |
| 106859 | | - { |
| 107570 | + p->mxSample = p->nLimit==0 ? mxSample : 0; |
| 107571 | + if( mxSample ){ |
| 106860 | 107572 | u8 *pSpace; /* Allocated space not yet assigned */ |
| 106861 | 107573 | int i; /* Used to iterate through p->aSample[] */ |
| 106862 | 107574 | |
| 106863 | 107575 | p->iGet = -1; |
| 106864 | | - p->mxSample = mxSample; |
| 106865 | | - p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1); |
| 107576 | + p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1); |
| 106866 | 107577 | p->current.anLt = &p->current.anEq[nColUp]; |
| 106867 | 107578 | p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]); |
| 106868 | 107579 | |
| 106869 | 107580 | /* Set up the StatAccum.a[] and aBest[] arrays */ |
| 106870 | 107581 | p->a = (struct StatSample*)&p->current.anLt[nColUp]; |
| | @@ -106888,11 +107599,11 @@ |
| 106888 | 107599 | ** (given by the 3rd parameter) is never used and can be any positive |
| 106889 | 107600 | ** value. */ |
| 106890 | 107601 | sqlite3_result_blob(context, p, sizeof(*p), statAccumDestructor); |
| 106891 | 107602 | } |
| 106892 | 107603 | static const FuncDef statInitFuncdef = { |
| 106893 | | - 2+IsStat4, /* nArg */ |
| 107604 | + 4, /* nArg */ |
| 106894 | 107605 | SQLITE_UTF8, /* funcFlags */ |
| 106895 | 107606 | 0, /* pUserData */ |
| 106896 | 107607 | 0, /* pNext */ |
| 106897 | 107608 | statInit, /* xSFunc */ |
| 106898 | 107609 | 0, /* xFinalize */ |
| | @@ -107092,14 +107803,17 @@ |
| 107092 | 107803 | ** P Pointer to the StatAccum object created by stat_init() |
| 107093 | 107804 | ** C Index of left-most column to differ from previous row |
| 107094 | 107805 | ** R Rowid for the current row. Might be a key record for |
| 107095 | 107806 | ** WITHOUT ROWID tables. |
| 107096 | 107807 | ** |
| 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. |
| 107808 | +** The purpose of this routine is to collect statistical data and/or |
| 107809 | +** samples from the index being analyzed into the StatAccum object. |
| 107810 | +** The stat_get() SQL function will be used afterwards to |
| 107811 | +** retrieve the information gathered. |
| 107812 | +** |
| 107813 | +** This SQL function usually returns NULL, but might return an integer |
| 107814 | +** if it wants the byte-code to do special processing. |
| 107101 | 107815 | ** |
| 107102 | 107816 | ** The R parameter is only used for STAT4 |
| 107103 | 107817 | */ |
| 107104 | 107818 | static void statPush( |
| 107105 | 107819 | sqlite3_context *context, |
| | @@ -107121,11 +107835,11 @@ |
| 107121 | 107835 | /* This is the first call to this function. Do initialization. */ |
| 107122 | 107836 | for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1; |
| 107123 | 107837 | }else{ |
| 107124 | 107838 | /* Second and subsequent calls get processed here */ |
| 107125 | 107839 | #ifdef SQLITE_ENABLE_STAT4 |
| 107126 | | - samplePushPrevious(p, iChng); |
| 107840 | + if( p->mxSample ) samplePushPrevious(p, iChng); |
| 107127 | 107841 | #endif |
| 107128 | 107842 | |
| 107129 | 107843 | /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply |
| 107130 | 107844 | ** to the current row of the index. */ |
| 107131 | 107845 | for(i=0; i<iChng; i++){ |
| | @@ -107132,30 +107846,29 @@ |
| 107132 | 107846 | p->current.anEq[i]++; |
| 107133 | 107847 | } |
| 107134 | 107848 | for(i=iChng; i<p->nCol; i++){ |
| 107135 | 107849 | p->current.anDLt[i]++; |
| 107136 | 107850 | #ifdef SQLITE_ENABLE_STAT4 |
| 107137 | | - p->current.anLt[i] += p->current.anEq[i]; |
| 107851 | + if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i]; |
| 107138 | 107852 | #endif |
| 107139 | 107853 | p->current.anEq[i] = 1; |
| 107140 | 107854 | } |
| 107141 | 107855 | } |
| 107856 | + |
| 107142 | 107857 | p->nRow++; |
| 107143 | 107858 | #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 | | - |
| 107859 | + if( p->mxSample ){ |
| 107860 | + tRowcnt nLt; |
| 107861 | + if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){ |
| 107862 | + sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2])); |
| 107863 | + }else{ |
| 107864 | + sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]), |
| 107865 | + sqlite3_value_blob(argv[2])); |
| 107866 | + } |
| 107867 | + p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345; |
| 107868 | + |
| 107869 | + nLt = p->current.anLt[p->nCol-1]; |
| 107157 | 107870 | /* Check if this is to be a periodic sample. If so, add it. */ |
| 107158 | 107871 | if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){ |
| 107159 | 107872 | p->current.isPSample = 1; |
| 107160 | 107873 | p->current.iCol = 0; |
| 107161 | 107874 | sampleInsert(p, &p->current, p->nCol-1); |
| | @@ -107167,13 +107880,18 @@ |
| 107167 | 107880 | p->current.iCol = i; |
| 107168 | 107881 | if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){ |
| 107169 | 107882 | sampleCopy(p, &p->aBest[i], &p->current); |
| 107170 | 107883 | } |
| 107171 | 107884 | } |
| 107885 | + }else |
| 107886 | +#endif |
| 107887 | + if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){ |
| 107888 | + p->nSkipAhead++; |
| 107889 | + sqlite3_result_int(context, p->current.anDLt[0]>0); |
| 107172 | 107890 | } |
| 107173 | | -#endif |
| 107174 | 107891 | } |
| 107892 | + |
| 107175 | 107893 | static const FuncDef statPushFuncdef = { |
| 107176 | 107894 | 2+IsStat4, /* nArg */ |
| 107177 | 107895 | SQLITE_UTF8, /* funcFlags */ |
| 107178 | 107896 | 0, /* pUserData */ |
| 107179 | 107897 | 0, /* pNext */ |
| | @@ -107221,10 +107939,11 @@ |
| 107221 | 107939 | assert( argc==2 ); |
| 107222 | 107940 | assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ |
| 107223 | 107941 | || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT |
| 107224 | 107942 | || eCall==STAT_GET_NDLT |
| 107225 | 107943 | ); |
| 107944 | + assert( eCall==STAT_GET_STAT1 || p->mxSample ); |
| 107226 | 107945 | if( eCall==STAT_GET_STAT1 ) |
| 107227 | 107946 | #else |
| 107228 | 107947 | assert( argc==1 ); |
| 107229 | 107948 | #endif |
| 107230 | 107949 | { |
| | @@ -107256,11 +107975,12 @@ |
| 107256 | 107975 | if( zRet==0 ){ |
| 107257 | 107976 | sqlite3_result_error_nomem(context); |
| 107258 | 107977 | return; |
| 107259 | 107978 | } |
| 107260 | 107979 | |
| 107261 | | - sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow); |
| 107980 | + sqlite3_snprintf(24, zRet, "%llu", |
| 107981 | + p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow); |
| 107262 | 107982 | z = zRet + sqlite3Strlen30(zRet); |
| 107263 | 107983 | for(i=0; i<p->nKeyCol; i++){ |
| 107264 | 107984 | u64 nDistinct = p->current.anDLt[i] + 1; |
| 107265 | 107985 | u64 iVal = (p->nRow + nDistinct - 1) / nDistinct; |
| 107266 | 107986 | sqlite3_snprintf(24, z, " %llu", iVal); |
| | @@ -107332,20 +108052,20 @@ |
| 107332 | 108052 | 0, 0, /* xValue, xInverse */ |
| 107333 | 108053 | "stat_get", /* zName */ |
| 107334 | 108054 | {0} |
| 107335 | 108055 | }; |
| 107336 | 108056 | |
| 107337 | | -static void callStatGet(Parse *pParse, int regStat4, int iParam, int regOut){ |
| 108057 | +static void callStatGet(Parse *pParse, int regStat, int iParam, int regOut){ |
| 107338 | 108058 | #ifdef SQLITE_ENABLE_STAT4 |
| 107339 | | - sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat4+1); |
| 108059 | + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1); |
| 107340 | 108060 | #elif SQLITE_DEBUG |
| 107341 | 108061 | assert( iParam==STAT_GET_STAT1 ); |
| 107342 | 108062 | #else |
| 107343 | 108063 | UNUSED_PARAMETER( iParam ); |
| 107344 | 108064 | #endif |
| 107345 | | - assert( regOut!=regStat4 && regOut!=regStat4+1 ); |
| 107346 | | - sqlite3VdbeAddFunctionCall(pParse, 0, regStat4, regOut, 1+IsStat4, |
| 108065 | + assert( regOut!=regStat && regOut!=regStat+1 ); |
| 108066 | + sqlite3VdbeAddFunctionCall(pParse, 0, regStat, regOut, 1+IsStat4, |
| 107347 | 108067 | &statGetFuncdef, 0); |
| 107348 | 108068 | } |
| 107349 | 108069 | |
| 107350 | 108070 | /* |
| 107351 | 108071 | ** Generate code to do an analysis of all indices associated with |
| | @@ -107367,16 +108087,15 @@ |
| 107367 | 108087 | int i; /* Loop counter */ |
| 107368 | 108088 | int jZeroRows = -1; /* Jump from here if number of rows is zero */ |
| 107369 | 108089 | int iDb; /* Index of database containing pTab */ |
| 107370 | 108090 | u8 needTableCnt = 1; /* True to count the table */ |
| 107371 | 108091 | int regNewRowid = iMem++; /* Rowid for the inserted record */ |
| 107372 | | - int regStat4 = iMem++; /* Register to hold StatAccum object */ |
| 108092 | + int regStat = iMem++; /* Register to hold StatAccum object */ |
| 107373 | 108093 | int regChng = iMem++; /* Index of changed index field */ |
| 107374 | | -#ifdef SQLITE_ENABLE_STAT4 |
| 107375 | 108094 | int regRowid = iMem++; /* Rowid argument passed to stat_push() */ |
| 107376 | | -#endif |
| 107377 | 108095 | int regTemp = iMem++; /* Temporary use register */ |
| 108096 | + int regTemp2 = iMem++; /* Second temporary use register */ |
| 107378 | 108097 | int regTabname = iMem++; /* Register containing table name */ |
| 107379 | 108098 | int regIdxname = iMem++; /* Register containing index name */ |
| 107380 | 108099 | int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */ |
| 107381 | 108100 | int regPrev = iMem; /* MUST BE LAST (see below) */ |
| 107382 | 108101 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| | @@ -107500,21 +108219,30 @@ |
| 107500 | 108219 | /* Invoke the stat_init() function. The arguments are: |
| 107501 | 108220 | ** |
| 107502 | 108221 | ** (1) the number of columns in the index including the rowid |
| 107503 | 108222 | ** (or for a WITHOUT ROWID table, the number of PK columns), |
| 107504 | 108223 | ** (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 |
| 108224 | + ** (3) estimated number of rows in the index, |
| 107509 | 108225 | */ |
| 108226 | + sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat+1); |
| 108227 | + assert( regRowid==regStat+2 ); |
| 108228 | + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid); |
| 107510 | 108229 | #ifdef SQLITE_ENABLE_STAT4 |
| 107511 | | - sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3); |
| 108230 | + if( OptimizationEnabled(db, SQLITE_Stat4) ){ |
| 108231 | + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regTemp); |
| 108232 | + addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 108233 | + VdbeCoverage(v); |
| 108234 | + }else |
| 107512 | 108235 | #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, |
| 108236 | + { |
| 108237 | + addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 108238 | + VdbeCoverage(v); |
| 108239 | + sqlite3VdbeAddOp3(v, OP_Count, iIdxCur, regTemp, 1); |
| 108240 | + } |
| 108241 | + assert( regTemp2==regStat+4 ); |
| 108242 | + sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2); |
| 108243 | + sqlite3VdbeAddFunctionCall(pParse, 0, regStat+1, regStat, 4, |
| 107516 | 108244 | &statInitFuncdef, 0); |
| 107517 | 108245 | |
| 107518 | 108246 | /* Implementation of the following: |
| 107519 | 108247 | ** |
| 107520 | 108248 | ** Rewind csr |
| | @@ -107521,12 +108249,10 @@ |
| 107521 | 108249 | ** if eof(csr) goto end_of_scan; |
| 107522 | 108250 | ** regChng = 0 |
| 107523 | 108251 | ** goto next_push_0; |
| 107524 | 108252 | ** |
| 107525 | 108253 | */ |
| 107526 | | - addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur); |
| 107527 | | - VdbeCoverage(v); |
| 107528 | 108254 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng); |
| 107529 | 108255 | addrNextRow = sqlite3VdbeCurrentAddr(v); |
| 107530 | 108256 | |
| 107531 | 108257 | if( nColTest>0 ){ |
| 107532 | 108258 | int endDistinctTest = sqlite3VdbeMakeLabel(pParse); |
| | @@ -107555,10 +108281,11 @@ |
| 107555 | 108281 | } |
| 107556 | 108282 | for(i=0; i<nColTest; i++){ |
| 107557 | 108283 | char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); |
| 107558 | 108284 | sqlite3VdbeAddOp2(v, OP_Integer, i, regChng); |
| 107559 | 108285 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp); |
| 108286 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 107560 | 108287 | aGotoChng[i] = |
| 107561 | 108288 | sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ); |
| 107562 | 108289 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 107563 | 108290 | VdbeCoverage(v); |
| 107564 | 108291 | } |
| | @@ -107575,10 +108302,11 @@ |
| 107575 | 108302 | */ |
| 107576 | 108303 | sqlite3VdbeJumpHere(v, addrNextRow-1); |
| 107577 | 108304 | for(i=0; i<nColTest; i++){ |
| 107578 | 108305 | sqlite3VdbeJumpHere(v, aGotoChng[i]); |
| 107579 | 108306 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i); |
| 108307 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 107580 | 108308 | } |
| 107581 | 108309 | sqlite3VdbeResolveLabel(v, endDistinctTest); |
| 107582 | 108310 | sqlite3DbFree(db, aGotoChng); |
| 107583 | 108311 | } |
| 107584 | 108312 | |
| | @@ -107588,34 +108316,50 @@ |
| 107588 | 108316 | ** stat_push(P, regChng, regRowid) // 3rd parameter STAT4 only |
| 107589 | 108317 | ** Next csr |
| 107590 | 108318 | ** if !eof(csr) goto next_row; |
| 107591 | 108319 | */ |
| 107592 | 108320 | #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); |
| 108321 | + if( OptimizationEnabled(db, SQLITE_Stat4) ){ |
| 108322 | + assert( regRowid==(regStat+2) ); |
| 108323 | + if( HasRowid(pTab) ){ |
| 108324 | + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid); |
| 108325 | + }else{ |
| 108326 | + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 108327 | + int j, k, regKey; |
| 108328 | + regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 108329 | + for(j=0; j<pPk->nKeyCol; j++){ |
| 108330 | + k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); |
| 108331 | + assert( k>=0 && k<pIdx->nColumn ); |
| 108332 | + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j); |
| 108333 | + VdbeComment((v, "%s.column(%d)", pIdx->zName, i)); |
| 108334 | + } |
| 108335 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid); |
| 108336 | + sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol); |
| 108337 | + } |
| 107608 | 108338 | } |
| 107609 | 108339 | #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); |
| 108340 | + assert( regChng==(regStat+1) ); |
| 108341 | + { |
| 108342 | + sqlite3VdbeAddFunctionCall(pParse, 1, regStat, regTemp, 2+IsStat4, |
| 108343 | + &statPushFuncdef, 0); |
| 108344 | + if( db->nAnalysisLimit ){ |
| 108345 | + int j1, j2, j3; |
| 108346 | + j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regTemp); VdbeCoverage(v); |
| 108347 | + j2 = sqlite3VdbeAddOp1(v, OP_If, regTemp); VdbeCoverage(v); |
| 108348 | + j3 = sqlite3VdbeAddOp4Int(v, OP_SeekGT, iIdxCur, 0, regPrev, 1); |
| 108349 | + VdbeCoverage(v); |
| 108350 | + sqlite3VdbeJumpHere(v, j1); |
| 108351 | + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 108352 | + sqlite3VdbeJumpHere(v, j2); |
| 108353 | + sqlite3VdbeJumpHere(v, j3); |
| 108354 | + }else{ |
| 108355 | + sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v); |
| 108356 | + } |
| 108357 | + } |
| 107614 | 108358 | |
| 107615 | 108359 | /* Add the entry to the stat1 table. */ |
| 107616 | | - callStatGet(pParse, regStat4, STAT_GET_STAT1, regStat1); |
| 108360 | + callStatGet(pParse, regStat, STAT_GET_STAT1, regStat1); |
| 107617 | 108361 | assert( "BBB"[0]==SQLITE_AFF_TEXT ); |
| 107618 | 108362 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0); |
| 107619 | 108363 | sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid); |
| 107620 | 108364 | sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid); |
| 107621 | 108365 | #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| | @@ -107623,11 +108367,11 @@ |
| 107623 | 108367 | #endif |
| 107624 | 108368 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 107625 | 108369 | |
| 107626 | 108370 | /* Add the entries to the stat4 table. */ |
| 107627 | 108371 | #ifdef SQLITE_ENABLE_STAT4 |
| 107628 | | - { |
| 108372 | + if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){ |
| 107629 | 108373 | int regEq = regStat1; |
| 107630 | 108374 | int regLt = regStat1+1; |
| 107631 | 108375 | int regDLt = regStat1+2; |
| 107632 | 108376 | int regSample = regStat1+3; |
| 107633 | 108377 | int regCol = regStat1+4; |
| | @@ -107637,16 +108381,16 @@ |
| 107637 | 108381 | u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound; |
| 107638 | 108382 | |
| 107639 | 108383 | pParse->nMem = MAX(pParse->nMem, regCol+nCol); |
| 107640 | 108384 | |
| 107641 | 108385 | addrNext = sqlite3VdbeCurrentAddr(v); |
| 107642 | | - callStatGet(pParse, regStat4, STAT_GET_ROWID, regSampleRowid); |
| 108386 | + callStatGet(pParse, regStat, STAT_GET_ROWID, regSampleRowid); |
| 107643 | 108387 | addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid); |
| 107644 | 108388 | 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); |
| 108389 | + callStatGet(pParse, regStat, STAT_GET_NEQ, regEq); |
| 108390 | + callStatGet(pParse, regStat, STAT_GET_NLT, regLt); |
| 108391 | + callStatGet(pParse, regStat, STAT_GET_NDLT, regDLt); |
| 107648 | 108392 | sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0); |
| 107649 | 108393 | VdbeCoverage(v); |
| 107650 | 108394 | for(i=0; i<nCol; i++){ |
| 107651 | 108395 | sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i); |
| 107652 | 108396 | } |
| | @@ -113818,11 +114562,11 @@ |
| 113818 | 114562 | pParse->rc = rc; |
| 113819 | 114563 | return 1; |
| 113820 | 114564 | } |
| 113821 | 114565 | db->aDb[1].pBt = pBt; |
| 113822 | 114566 | assert( db->aDb[1].pSchema ); |
| 113823 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
| 114567 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
| 113824 | 114568 | sqlite3OomFault(db); |
| 113825 | 114569 | return 1; |
| 113826 | 114570 | } |
| 113827 | 114571 | } |
| 113828 | 114572 | return 0; |
| | @@ -113929,11 +114673,11 @@ |
| 113929 | 114673 | char *p4, /* Error message */ |
| 113930 | 114674 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 113931 | 114675 | u8 p5Errmsg /* P5_ErrMsg type */ |
| 113932 | 114676 | ){ |
| 113933 | 114677 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 113934 | | - assert( (errCode&0xff)==SQLITE_CONSTRAINT ); |
| 114678 | + assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
| 113935 | 114679 | if( onError==OE_Abort ){ |
| 113936 | 114680 | sqlite3MayAbort(pParse); |
| 113937 | 114681 | } |
| 113938 | 114682 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
| 113939 | 114683 | sqlite3VdbeChangeP5(v, p5Errmsg); |
| | @@ -115642,10 +116386,11 @@ |
| 115642 | 116386 | VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName)); |
| 115643 | 116387 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1, |
| 115644 | 116388 | &iPartIdxLabel, pPrior, r1); |
| 115645 | 116389 | sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1, |
| 115646 | 116390 | pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn); |
| 116391 | + sqlite3VdbeChangeP5(v, 1); /* Cause IdxDelete to error if no entry found */ |
| 115647 | 116392 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
| 115648 | 116393 | pPrior = pIdx; |
| 115649 | 116394 | } |
| 115650 | 116395 | } |
| 115651 | 116396 | |
| | @@ -122629,10 +123374,11 @@ |
| 122629 | 123374 | const char *(*filename_wal)(const char*); |
| 122630 | 123375 | /* Version 3.32.0 and later */ |
| 122631 | 123376 | char *(*create_filename)(const char*,const char*,const char*, |
| 122632 | 123377 | int,const char**); |
| 122633 | 123378 | void (*free_filename)(char*); |
| 123379 | + sqlite3_file *(*database_file_object)(const char*); |
| 122634 | 123380 | }; |
| 122635 | 123381 | |
| 122636 | 123382 | /* |
| 122637 | 123383 | ** This is the function signature used for all extension entry points. It |
| 122638 | 123384 | ** is also defined in the file "loadext.c". |
| | @@ -122932,10 +123678,11 @@ |
| 122932 | 123678 | #define sqlite3_filename_journal sqlite3_api->filename_journal |
| 122933 | 123679 | #define sqlite3_filename_wal sqlite3_api->filename_wal |
| 122934 | 123680 | /* Version 3.32.0 and later */ |
| 122935 | 123681 | #define sqlite3_create_filename sqlite3_api->create_filename |
| 122936 | 123682 | #define sqlite3_free_filename sqlite3_api->free_filename |
| 123683 | +#define sqlite3_database_file_object sqlite3_api->database_file_object |
| 122937 | 123684 | #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 122938 | 123685 | |
| 122939 | 123686 | #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 122940 | 123687 | /* This case when the file really is being compiled as a loadable |
| 122941 | 123688 | ** extension */ |
| | @@ -123413,11 +124160,20 @@ |
| 123413 | 124160 | sqlite3_filename_journal, |
| 123414 | 124161 | sqlite3_filename_wal, |
| 123415 | 124162 | /* Version 3.32.0 and later */ |
| 123416 | 124163 | sqlite3_create_filename, |
| 123417 | 124164 | sqlite3_free_filename, |
| 124165 | + sqlite3_database_file_object, |
| 123418 | 124166 | }; |
| 124167 | + |
| 124168 | +/* True if x is the directory separator character |
| 124169 | +*/ |
| 124170 | +#if SQLITE_OS_WIN |
| 124171 | +# define DirSep(X) ((X)=='/'||(X)=='\\') |
| 124172 | +#else |
| 124173 | +# define DirSep(X) ((X)=='/') |
| 124174 | +#endif |
| 123419 | 124175 | |
| 123420 | 124176 | /* |
| 123421 | 124177 | ** Attempt to load an SQLite extension library contained in the file |
| 123422 | 124178 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| 123423 | 124179 | ** default entry point name (sqlite3_extension_init) is used. Use |
| | @@ -123516,11 +124272,11 @@ |
| 123516 | 124272 | if( zAltEntry==0 ){ |
| 123517 | 124273 | sqlite3OsDlClose(pVfs, handle); |
| 123518 | 124274 | return SQLITE_NOMEM_BKPT; |
| 123519 | 124275 | } |
| 123520 | 124276 | memcpy(zAltEntry, "sqlite3_", 8); |
| 123521 | | - for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} |
| 124277 | + for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){} |
| 123522 | 124278 | iFile++; |
| 123523 | 124279 | if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; |
| 123524 | 124280 | for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ |
| 123525 | 124281 | if( sqlite3Isalpha(c) ){ |
| 123526 | 124282 | zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; |
| | @@ -123820,53 +124576,54 @@ |
| 123820 | 124576 | ** that script and rerun it. |
| 123821 | 124577 | */ |
| 123822 | 124578 | |
| 123823 | 124579 | /* The various pragma types */ |
| 123824 | 124580 | #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 |
| 124581 | +#define PragTyp_ANALYSIS_LIMIT 1 |
| 124582 | +#define PragTyp_HEADER_VALUE 2 |
| 124583 | +#define PragTyp_AUTO_VACUUM 3 |
| 124584 | +#define PragTyp_FLAG 4 |
| 124585 | +#define PragTyp_BUSY_TIMEOUT 5 |
| 124586 | +#define PragTyp_CACHE_SIZE 6 |
| 124587 | +#define PragTyp_CACHE_SPILL 7 |
| 124588 | +#define PragTyp_CASE_SENSITIVE_LIKE 8 |
| 124589 | +#define PragTyp_COLLATION_LIST 9 |
| 124590 | +#define PragTyp_COMPILE_OPTIONS 10 |
| 124591 | +#define PragTyp_DATA_STORE_DIRECTORY 11 |
| 124592 | +#define PragTyp_DATABASE_LIST 12 |
| 124593 | +#define PragTyp_DEFAULT_CACHE_SIZE 13 |
| 124594 | +#define PragTyp_ENCODING 14 |
| 124595 | +#define PragTyp_FOREIGN_KEY_CHECK 15 |
| 124596 | +#define PragTyp_FOREIGN_KEY_LIST 16 |
| 124597 | +#define PragTyp_FUNCTION_LIST 17 |
| 124598 | +#define PragTyp_HARD_HEAP_LIMIT 18 |
| 124599 | +#define PragTyp_INCREMENTAL_VACUUM 19 |
| 124600 | +#define PragTyp_INDEX_INFO 20 |
| 124601 | +#define PragTyp_INDEX_LIST 21 |
| 124602 | +#define PragTyp_INTEGRITY_CHECK 22 |
| 124603 | +#define PragTyp_JOURNAL_MODE 23 |
| 124604 | +#define PragTyp_JOURNAL_SIZE_LIMIT 24 |
| 124605 | +#define PragTyp_LOCK_PROXY_FILE 25 |
| 124606 | +#define PragTyp_LOCKING_MODE 26 |
| 124607 | +#define PragTyp_PAGE_COUNT 27 |
| 124608 | +#define PragTyp_MMAP_SIZE 28 |
| 124609 | +#define PragTyp_MODULE_LIST 29 |
| 124610 | +#define PragTyp_OPTIMIZE 30 |
| 124611 | +#define PragTyp_PAGE_SIZE 31 |
| 124612 | +#define PragTyp_PRAGMA_LIST 32 |
| 124613 | +#define PragTyp_SECURE_DELETE 33 |
| 124614 | +#define PragTyp_SHRINK_MEMORY 34 |
| 124615 | +#define PragTyp_SOFT_HEAP_LIMIT 35 |
| 124616 | +#define PragTyp_SYNCHRONOUS 36 |
| 124617 | +#define PragTyp_TABLE_INFO 37 |
| 124618 | +#define PragTyp_TEMP_STORE 38 |
| 124619 | +#define PragTyp_TEMP_STORE_DIRECTORY 39 |
| 124620 | +#define PragTyp_THREADS 40 |
| 124621 | +#define PragTyp_WAL_AUTOCHECKPOINT 41 |
| 124622 | +#define PragTyp_WAL_CHECKPOINT 42 |
| 124623 | +#define PragTyp_LOCK_STATUS 43 |
| 124624 | +#define PragTyp_STATS 44 |
| 123868 | 124625 | |
| 123869 | 124626 | /* Property flags associated with various pragma. */ |
| 123870 | 124627 | #define PragFlg_NeedSchema 0x01 /* Force schema load before running */ |
| 123871 | 124628 | #define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */ |
| 123872 | 124629 | #define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */ |
| | @@ -123953,10 +124710,15 @@ |
| 123953 | 124710 | /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS, |
| 123954 | 124711 | /* ePragFlg: */ 0, |
| 123955 | 124712 | /* ColNames: */ 0, 0, |
| 123956 | 124713 | /* iArg: */ 0 }, |
| 123957 | 124714 | #endif |
| 124715 | + {/* zName: */ "analysis_limit", |
| 124716 | + /* ePragTyp: */ PragTyp_ANALYSIS_LIMIT, |
| 124717 | + /* ePragFlg: */ PragFlg_Result0, |
| 124718 | + /* ColNames: */ 0, 0, |
| 124719 | + /* iArg: */ 0 }, |
| 123958 | 124720 | #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS) |
| 123959 | 124721 | {/* zName: */ "application_id", |
| 123960 | 124722 | /* ePragTyp: */ PragTyp_HEADER_VALUE, |
| 123961 | 124723 | /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0, |
| 123962 | 124724 | /* ColNames: */ 0, 0, |
| | @@ -124453,11 +125215,11 @@ |
| 124453 | 125215 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 124454 | 125216 | /* ColNames: */ 0, 0, |
| 124455 | 125217 | /* iArg: */ SQLITE_WriteSchema|SQLITE_NoSchemaError }, |
| 124456 | 125218 | #endif |
| 124457 | 125219 | }; |
| 124458 | | -/* Number of pragmas: 66 on by default, 76 total. */ |
| 125220 | +/* Number of pragmas: 67 on by default, 77 total. */ |
| 124459 | 125221 | |
| 124460 | 125222 | /************** End of pragma.h **********************************************/ |
| 124461 | 125223 | /************** Continuing where we left off in pragma.c *********************/ |
| 124462 | 125224 | |
| 124463 | 125225 | /* |
| | @@ -124983,11 +125745,11 @@ |
| 124983 | 125745 | }else{ |
| 124984 | 125746 | /* Malloc may fail when setting the page-size, as there is an internal |
| 124985 | 125747 | ** buffer that the pager module resizes using sqlite3_realloc(). |
| 124986 | 125748 | */ |
| 124987 | 125749 | db->nextPagesize = sqlite3Atoi(zRight); |
| 124988 | | - if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ |
| 125750 | + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ |
| 124989 | 125751 | sqlite3OomFault(db); |
| 124990 | 125752 | } |
| 124991 | 125753 | } |
| 124992 | 125754 | break; |
| 124993 | 125755 | } |
| | @@ -126157,11 +126919,10 @@ |
| 126157 | 126919 | sqlite3ResolvePartIdxLabel(pParse, jmp3); |
| 126158 | 126920 | } |
| 126159 | 126921 | } |
| 126160 | 126922 | sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); |
| 126161 | 126923 | sqlite3VdbeJumpHere(v, loopTop-1); |
| 126162 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 126163 | 126924 | if( !isQuick ){ |
| 126164 | 126925 | sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); |
| 126165 | 126926 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 126166 | 126927 | if( pPk==pIdx ) continue; |
| 126167 | 126928 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); |
| | @@ -126171,11 +126932,10 @@ |
| 126171 | 126932 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); |
| 126172 | 126933 | integrityCheckResultRow(v); |
| 126173 | 126934 | sqlite3VdbeJumpHere(v, addr); |
| 126174 | 126935 | } |
| 126175 | 126936 | } |
| 126176 | | -#endif /* SQLITE_OMIT_BTREECOUNT */ |
| 126177 | 126937 | } |
| 126178 | 126938 | } |
| 126179 | 126939 | { |
| 126180 | 126940 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 126181 | 126941 | static const VdbeOpList endCode[] = { |
| | @@ -126605,10 +127365,29 @@ |
| 126605 | 127365 | sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); |
| 126606 | 127366 | } |
| 126607 | 127367 | returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); |
| 126608 | 127368 | break; |
| 126609 | 127369 | } |
| 127370 | + |
| 127371 | + /* |
| 127372 | + ** PRAGMA analysis_limit |
| 127373 | + ** PRAGMA analysis_limit = N |
| 127374 | + ** |
| 127375 | + ** Configure the maximum number of rows that ANALYZE will examine |
| 127376 | + ** in each index that it looks at. Return the new limit. |
| 127377 | + */ |
| 127378 | + case PragTyp_ANALYSIS_LIMIT: { |
| 127379 | + sqlite3_int64 N; |
| 127380 | + if( zRight |
| 127381 | + && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK |
| 127382 | + && N>=0 |
| 127383 | + ){ |
| 127384 | + db->nAnalysisLimit = (int)(N&0x7fffffff); |
| 127385 | + } |
| 127386 | + returnSingleInt(v, db->nAnalysisLimit); |
| 127387 | + break; |
| 127388 | + } |
| 126610 | 127389 | |
| 126611 | 127390 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 126612 | 127391 | /* |
| 126613 | 127392 | ** Report the current state of file logs for all databases |
| 126614 | 127393 | */ |
| | @@ -131404,10 +132183,11 @@ |
| 131404 | 132183 | if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){ |
| 131405 | 132184 | memset(&ifNullRow, 0, sizeof(ifNullRow)); |
| 131406 | 132185 | ifNullRow.op = TK_IF_NULL_ROW; |
| 131407 | 132186 | ifNullRow.pLeft = pCopy; |
| 131408 | 132187 | ifNullRow.iTable = pSubst->iNewTable; |
| 132188 | + ifNullRow.flags = EP_Skip; |
| 131409 | 132189 | pCopy = &ifNullRow; |
| 131410 | 132190 | } |
| 131411 | 132191 | testcase( ExprHasProperty(pCopy, EP_Subquery) ); |
| 131412 | 132192 | pNew = sqlite3ExprDup(db, pCopy, 0); |
| 131413 | 132193 | if( pNew && pSubst->isLeftJoin ){ |
| | @@ -134568,11 +135348,10 @@ |
| 134568 | 135348 | VdbeComment((v, "indicate accumulator empty")); |
| 134569 | 135349 | sqlite3VdbeAddOp1(v, OP_Return, regReset); |
| 134570 | 135350 | |
| 134571 | 135351 | } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ |
| 134572 | 135352 | else { |
| 134573 | | -#ifndef SQLITE_OMIT_BTREECOUNT |
| 134574 | 135353 | Table *pTab; |
| 134575 | 135354 | if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){ |
| 134576 | 135355 | /* If isSimpleCount() returns a pointer to a Table structure, then |
| 134577 | 135356 | ** the SQL statement is of the form: |
| 134578 | 135357 | ** |
| | @@ -134604,17 +135383,19 @@ |
| 134604 | 135383 | ** |
| 134605 | 135384 | ** In practice the KeyInfo structure will not be used. It is only |
| 134606 | 135385 | ** passed to keep OP_OpenRead happy. |
| 134607 | 135386 | */ |
| 134608 | 135387 | 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; |
| 135388 | + if( !p->pSrc->a[0].fg.notIndexed ){ |
| 135389 | + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 135390 | + if( pIdx->bUnordered==0 |
| 135391 | + && pIdx->szIdxRow<pTab->szTabRow |
| 135392 | + && pIdx->pPartIdxWhere==0 |
| 135393 | + && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 135394 | + ){ |
| 135395 | + pBest = pIdx; |
| 135396 | + } |
| 134616 | 135397 | } |
| 134617 | 135398 | } |
| 134618 | 135399 | if( pBest ){ |
| 134619 | 135400 | iRoot = pBest->tnum; |
| 134620 | 135401 | pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest); |
| | @@ -134626,13 +135407,11 @@ |
| 134626 | 135407 | sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO); |
| 134627 | 135408 | } |
| 134628 | 135409 | sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); |
| 134629 | 135410 | sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
| 134630 | 135411 | explainSimpleCount(pParse, pTab, pBest); |
| 134631 | | - }else |
| 134632 | | -#endif /* SQLITE_OMIT_BTREECOUNT */ |
| 134633 | | - { |
| 135412 | + }else{ |
| 134634 | 135413 | int regAcc = 0; /* "populate accumulators" flag */ |
| 134635 | 135414 | |
| 134636 | 135415 | /* If there are accumulator registers but no min() or max() functions |
| 134637 | 135416 | ** without FILTER clauses, allocate register regAcc. Register regAcc |
| 134638 | 135417 | ** will contain 0 the first time the inner loop runs, and 1 thereafter. |
| | @@ -137852,11 +138631,11 @@ |
| 137852 | 138631 | db->mDbFlags = saved_mDbFlags; |
| 137853 | 138632 | db->flags = saved_flags; |
| 137854 | 138633 | db->nChange = saved_nChange; |
| 137855 | 138634 | db->nTotalChange = saved_nTotalChange; |
| 137856 | 138635 | db->mTrace = saved_mTrace; |
| 137857 | | - sqlite3BtreeSetPageSize(pMain, -1, -1, 1); |
| 138636 | + sqlite3BtreeSetPageSize(pMain, -1, 0, 1); |
| 137858 | 138637 | |
| 137859 | 138638 | /* Currently there is an SQL level transaction open on the vacuum |
| 137860 | 138639 | ** database. No locks are held on any other files (since the main file |
| 137861 | 138640 | ** was committed at the btree level). So it safe to end the transaction |
| 137862 | 138641 | ** by manually setting the autoCommit flag to true and detaching the |
| | @@ -159255,19 +160034,82 @@ |
| 159255 | 160034 | |
| 159256 | 160035 | |
| 159257 | 160036 | /************** End of sqliteicu.h *******************************************/ |
| 159258 | 160037 | /************** Continuing where we left off in main.c ***********************/ |
| 159259 | 160038 | #endif |
| 160039 | + |
| 160040 | +/* |
| 160041 | +** This is an extension initializer that is a no-op and always |
| 160042 | +** succeeds, except that it fails if the fault-simulation is set |
| 160043 | +** to 500. |
| 160044 | +*/ |
| 160045 | +static int sqlite3TestExtInit(sqlite3 *db){ |
| 160046 | + (void)db; |
| 160047 | + return sqlite3FaultSim(500); |
| 160048 | +} |
| 160049 | + |
| 160050 | + |
| 160051 | +/* |
| 160052 | +** Forward declarations of external module initializer functions |
| 160053 | +** for modules that need them. |
| 160054 | +*/ |
| 160055 | +#ifdef SQLITE_ENABLE_FTS1 |
| 160056 | +SQLITE_PRIVATE int sqlite3Fts1Init(sqlite3*); |
| 160057 | +#endif |
| 160058 | +#ifdef SQLITE_ENABLE_FTS2 |
| 160059 | +SQLITE_PRIVATE int sqlite3Fts2Init(sqlite3*); |
| 160060 | +#endif |
| 160061 | +#ifdef SQLITE_ENABLE_FTS5 |
| 160062 | +SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); |
| 160063 | +#endif |
| 159260 | 160064 | #ifdef SQLITE_ENABLE_JSON1 |
| 159261 | 160065 | SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*); |
| 159262 | 160066 | #endif |
| 159263 | 160067 | #ifdef SQLITE_ENABLE_STMTVTAB |
| 159264 | 160068 | SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*); |
| 159265 | 160069 | #endif |
| 160070 | + |
| 160071 | +/* |
| 160072 | +** An array of pointers to extension initializer functions for |
| 160073 | +** built-in extensions. |
| 160074 | +*/ |
| 160075 | +static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = { |
| 160076 | +#ifdef SQLITE_ENABLE_FTS1 |
| 160077 | + sqlite3Fts1Init, |
| 160078 | +#endif |
| 160079 | +#ifdef SQLITE_ENABLE_FTS2 |
| 160080 | + sqlite3Fts2Init, |
| 160081 | +#endif |
| 160082 | +#ifdef SQLITE_ENABLE_FTS3 |
| 160083 | + sqlite3Fts3Init, |
| 160084 | +#endif |
| 159266 | 160085 | #ifdef SQLITE_ENABLE_FTS5 |
| 159267 | | -SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); |
| 160086 | + sqlite3Fts5Init, |
| 159268 | 160087 | #endif |
| 160088 | +#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) |
| 160089 | + sqlite3IcuInit, |
| 160090 | +#endif |
| 160091 | +#ifdef SQLITE_ENABLE_RTREE |
| 160092 | + sqlite3RtreeInit, |
| 160093 | +#endif |
| 160094 | +#ifdef SQLITE_ENABLE_DBPAGE_VTAB |
| 160095 | + sqlite3DbpageRegister, |
| 160096 | +#endif |
| 160097 | +#ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 160098 | + sqlite3DbstatRegister, |
| 160099 | +#endif |
| 160100 | + sqlite3TestExtInit, |
| 160101 | +#ifdef SQLITE_ENABLE_JSON1 |
| 160102 | + sqlite3Json1Init, |
| 160103 | +#endif |
| 160104 | +#ifdef SQLITE_ENABLE_STMTVTAB |
| 160105 | + sqlite3StmtVtabInit, |
| 160106 | +#endif |
| 160107 | +#ifdef SQLITE_ENABLE_BYTECODE_VTAB |
| 160108 | + sqlite3VdbeBytecodeVtabInit, |
| 160109 | +#endif |
| 160110 | +}; |
| 159269 | 160111 | |
| 159270 | 160112 | #ifndef SQLITE_AMALGAMATION |
| 159271 | 160113 | /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant |
| 159272 | 160114 | ** contains the text of SQLITE_VERSION macro. |
| 159273 | 160115 | */ |
| | @@ -160781,12 +161623,11 @@ |
| 160781 | 161623 | ** Return non-zero to retry the lock. Return zero to stop trying |
| 160782 | 161624 | ** and cause SQLite to return SQLITE_BUSY. |
| 160783 | 161625 | */ |
| 160784 | 161626 | static int sqliteDefaultBusyCallback( |
| 160785 | 161627 | 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 */ |
| 161628 | + int count /* Number of times table has been busy */ |
| 160788 | 161629 | ){ |
| 160789 | 161630 | #if SQLITE_OS_WIN || HAVE_USLEEP |
| 160790 | 161631 | /* This case is for systems that have support for sleeping for fractions of |
| 160791 | 161632 | ** a second. Examples: All windows systems, unix systems with usleep() */ |
| 160792 | 161633 | static const u8 delays[] = |
| | @@ -160796,35 +161637,10 @@ |
| 160796 | 161637 | # define NDELAY ArraySize(delays) |
| 160797 | 161638 | sqlite3 *db = (sqlite3 *)ptr; |
| 160798 | 161639 | int tmout = db->busyTimeout; |
| 160799 | 161640 | int delay, prior; |
| 160800 | 161641 | |
| 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 | 161642 | assert( count>=0 ); |
| 160827 | 161643 | if( count < NDELAY ){ |
| 160828 | 161644 | delay = delays[count]; |
| 160829 | 161645 | prior = totals[count]; |
| 160830 | 161646 | }else{ |
| | @@ -160840,11 +161656,10 @@ |
| 160840 | 161656 | #else |
| 160841 | 161657 | /* This case for unix systems that lack usleep() support. Sleeping |
| 160842 | 161658 | ** must be done in increments of whole seconds */ |
| 160843 | 161659 | sqlite3 *db = (sqlite3 *)ptr; |
| 160844 | 161660 | int tmout = ((sqlite3 *)ptr)->busyTimeout; |
| 160845 | | - UNUSED_PARAMETER(pFile); |
| 160846 | 161661 | if( (count+1)*1000 > tmout ){ |
| 160847 | 161662 | return 0; |
| 160848 | 161663 | } |
| 160849 | 161664 | sqlite3OsSleep(db->pVfs, 1000000); |
| 160850 | 161665 | return 1; |
| | @@ -160858,23 +161673,14 @@ |
| 160858 | 161673 | ** lock on VFS file pFile. |
| 160859 | 161674 | ** |
| 160860 | 161675 | ** If this routine returns non-zero, the lock is retried. If it |
| 160861 | 161676 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 160862 | 161677 | */ |
| 160863 | | -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){ |
| 161678 | +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 160864 | 161679 | int rc; |
| 160865 | 161680 | 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 | | - } |
| 161681 | + rc = p->xBusyHandler(p->pBusyArg, p->nBusy); |
| 160876 | 161682 | if( rc==0 ){ |
| 160877 | 161683 | p->nBusy = -1; |
| 160878 | 161684 | }else{ |
| 160879 | 161685 | p->nBusy++; |
| 160880 | 161686 | } |
| | @@ -160895,11 +161701,10 @@ |
| 160895 | 161701 | #endif |
| 160896 | 161702 | sqlite3_mutex_enter(db->mutex); |
| 160897 | 161703 | db->busyHandler.xBusyHandler = xBusy; |
| 160898 | 161704 | db->busyHandler.pBusyArg = pArg; |
| 160899 | 161705 | db->busyHandler.nBusy = 0; |
| 160900 | | - db->busyHandler.bExtraFileArg = 0; |
| 160901 | 161706 | db->busyTimeout = 0; |
| 160902 | 161707 | sqlite3_mutex_leave(db->mutex); |
| 160903 | 161708 | return SQLITE_OK; |
| 160904 | 161709 | } |
| 160905 | 161710 | |
| | @@ -160946,11 +161751,10 @@ |
| 160946 | 161751 | #endif |
| 160947 | 161752 | if( ms>0 ){ |
| 160948 | 161753 | sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback, |
| 160949 | 161754 | (void*)db); |
| 160950 | 161755 | db->busyTimeout = ms; |
| 160951 | | - db->busyHandler.bExtraFileArg = 1; |
| 160952 | 161756 | }else{ |
| 160953 | 161757 | sqlite3_busy_handler(db, 0, 0); |
| 160954 | 161758 | } |
| 160955 | 161759 | return SQLITE_OK; |
| 160956 | 161760 | } |
| | @@ -162272,10 +163076,11 @@ |
| 162272 | 163076 | sqlite3 *db; /* Store allocated handle here */ |
| 162273 | 163077 | int rc; /* Return code */ |
| 162274 | 163078 | int isThreadsafe; /* True for threadsafe connections */ |
| 162275 | 163079 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 162276 | 163080 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 163081 | + int i; /* Loop counter */ |
| 162277 | 163082 | |
| 162278 | 163083 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 162279 | 163084 | if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 162280 | 163085 | #endif |
| 162281 | 163086 | *ppDb = 0; |
| | @@ -162420,10 +163225,13 @@ |
| 162420 | 163225 | | SQLITE_EnableQPSG |
| 162421 | 163226 | #endif |
| 162422 | 163227 | #if defined(SQLITE_DEFAULT_DEFENSIVE) |
| 162423 | 163228 | | SQLITE_Defensive |
| 162424 | 163229 | #endif |
| 163230 | +#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE) |
| 163231 | + | SQLITE_LegacyAlter |
| 163232 | +#endif |
| 162425 | 163233 | ; |
| 162426 | 163234 | sqlite3HashInit(&db->aCollSeq); |
| 162427 | 163235 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 162428 | 163236 | sqlite3HashInit(&db->aModule); |
| 162429 | 163237 | #endif |
| | @@ -162512,18 +163320,15 @@ |
| 162512 | 163320 | */ |
| 162513 | 163321 | sqlite3Error(db, SQLITE_OK); |
| 162514 | 163322 | sqlite3RegisterPerConnectionBuiltinFunctions(db); |
| 162515 | 163323 | rc = sqlite3_errcode(db); |
| 162516 | 163324 | |
| 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 |
| 163325 | + |
| 163326 | + /* Load compiled-in extensions */ |
| 163327 | + for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){ |
| 163328 | + rc = sqlite3BuiltinExtensions[i](db); |
| 163329 | + } |
| 162525 | 163330 | |
| 162526 | 163331 | /* Load automatic extensions - extensions that have been registered |
| 162527 | 163332 | ** using the sqlite3_automatic_extension() API. |
| 162528 | 163333 | */ |
| 162529 | 163334 | if( rc==SQLITE_OK ){ |
| | @@ -162532,66 +163337,10 @@ |
| 162532 | 163337 | if( rc!=SQLITE_OK ){ |
| 162533 | 163338 | goto opendb_out; |
| 162534 | 163339 | } |
| 162535 | 163340 | } |
| 162536 | 163341 | |
| 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 | 163342 | #ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS |
| 162594 | 163343 | /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time |
| 162595 | 163344 | ** option gives access to internal functions by default. |
| 162596 | 163345 | ** Testing use only!!! */ |
| 162597 | 163346 | db->mDbFlags |= DBFLAG_InternalFunc; |
| | @@ -163076,11 +163825,11 @@ |
| 163076 | 163825 | *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); |
| 163077 | 163826 | rc = SQLITE_OK; |
| 163078 | 163827 | }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){ |
| 163079 | 163828 | int iNew = *(int*)pArg; |
| 163080 | 163829 | *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree); |
| 163081 | | - if( iNew>=0 && iNew<=254 ){ |
| 163830 | + if( iNew>=0 && iNew<=255 ){ |
| 163082 | 163831 | sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0); |
| 163083 | 163832 | } |
| 163084 | 163833 | rc = SQLITE_OK; |
| 163085 | 163834 | }else{ |
| 163086 | 163835 | rc = sqlite3OsFileControl(fd, op, pArg); |
| | @@ -165357,10 +166106,11 @@ |
| 165357 | 166106 | SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*); |
| 165358 | 166107 | SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *); |
| 165359 | 166108 | SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *); |
| 165360 | 166109 | SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*); |
| 165361 | 166110 | SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc); |
| 166111 | +SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut); |
| 165362 | 166112 | |
| 165363 | 166113 | /* fts3_tokenizer.c */ |
| 165364 | 166114 | SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); |
| 165365 | 166115 | SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); |
| 165366 | 166116 | SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, |
| | @@ -166088,10 +166838,26 @@ |
| 166088 | 166838 | fts3Appendf(pRc, &zRet, ", ?"); |
| 166089 | 166839 | } |
| 166090 | 166840 | sqlite3_free(zFree); |
| 166091 | 166841 | return zRet; |
| 166092 | 166842 | } |
| 166843 | + |
| 166844 | +/* |
| 166845 | +** Buffer z contains a positive integer value encoded as utf-8 text. |
| 166846 | +** Decode this value and store it in *pnOut, returning the number of bytes |
| 166847 | +** consumed. If an overflow error occurs return a negative value. |
| 166848 | +*/ |
| 166849 | +SQLITE_PRIVATE int sqlite3Fts3ReadInt(const char *z, int *pnOut){ |
| 166850 | + u64 iVal = 0; |
| 166851 | + int i; |
| 166852 | + for(i=0; z[i]>='0' && z[i]<='9'; i++){ |
| 166853 | + iVal = iVal*10 + (z[i] - '0'); |
| 166854 | + if( iVal>0x7FFFFFFF ) return -1; |
| 166855 | + } |
| 166856 | + *pnOut = (int)iVal; |
| 166857 | + return i; |
| 166858 | +} |
| 166093 | 166859 | |
| 166094 | 166860 | /* |
| 166095 | 166861 | ** This function interprets the string at (*pp) as a non-negative integer |
| 166096 | 166862 | ** value. It reads the integer and sets *pnOut to the value read, then |
| 166097 | 166863 | ** sets *pp to point to the byte immediately following the last byte of |
| | @@ -166104,23 +166870,21 @@ |
| 166104 | 166870 | ** |
| 166105 | 166871 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 166106 | 166872 | */ |
| 166107 | 166873 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 166108 | 166874 | const int MAX_NPREFIX = 10000000; |
| 166109 | | - const char *p; /* Iterator pointer */ |
| 166110 | 166875 | 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; |
| 166876 | + int nByte; |
| 166877 | + nByte = sqlite3Fts3ReadInt(*pp, &nInt); |
| 166878 | + if( nInt>MAX_NPREFIX ){ |
| 166879 | + nInt = 0; |
| 166880 | + } |
| 166881 | + if( nByte==0 ){ |
| 166882 | + return SQLITE_ERROR; |
| 166883 | + } |
| 166120 | 166884 | *pnOut = nInt; |
| 166121 | | - *pp = p; |
| 166885 | + *pp += nByte; |
| 166122 | 166886 | return SQLITE_OK; |
| 166123 | 166887 | } |
| 166124 | 166888 | |
| 166125 | 166889 | /* |
| 166126 | 166890 | ** This function is called to allocate an array of Fts3Index structures |
| | @@ -167298,11 +168062,13 @@ |
| 167298 | 168062 | static void fts3ReadNextPos( |
| 167299 | 168063 | char **pp, /* IN/OUT: Pointer into position-list buffer */ |
| 167300 | 168064 | sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ |
| 167301 | 168065 | ){ |
| 167302 | 168066 | if( (**pp)&0xFE ){ |
| 167303 | | - fts3GetDeltaVarint(pp, pi); |
| 168067 | + int iVal; |
| 168068 | + *pp += fts3GetVarint32((*pp), &iVal); |
| 168069 | + *pi += iVal; |
| 167304 | 168070 | *pi -= 2; |
| 167305 | 168071 | }else{ |
| 167306 | 168072 | *pi = POSITION_LIST_END; |
| 167307 | 168073 | } |
| 167308 | 168074 | } |
| | @@ -172198,14 +172964,11 @@ |
| 172198 | 172964 | |
| 172199 | 172965 | /* If this is a "NEAR" keyword, check for an explicit nearness. */ |
| 172200 | 172966 | if( pKey->eType==FTSQUERY_NEAR ){ |
| 172201 | 172967 | assert( nKey==4 ); |
| 172202 | 172968 | 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 | | - } |
| 172969 | + nKey += 1+sqlite3Fts3ReadInt(&zInput[nKey+1], &nNear); |
| 172207 | 172970 | } |
| 172208 | 172971 | } |
| 172209 | 172972 | |
| 172210 | 172973 | /* At this point this is probably a keyword. But for that to be true, |
| 172211 | 172974 | ** the next byte must contain either whitespace, an open or close |
| | @@ -178384,25 +179147,25 @@ |
| 178384 | 179147 | ){ |
| 178385 | 179148 | const unsigned char *zText = sqlite3_column_text(pStmt, iCol); |
| 178386 | 179149 | if( zText ){ |
| 178387 | 179150 | int i; |
| 178388 | 179151 | int iMul = 1; |
| 178389 | | - i64 iVal = 0; |
| 179152 | + u64 iVal = 0; |
| 178390 | 179153 | for(i=0; zText[i]>='0' && zText[i]<='9'; i++){ |
| 178391 | 179154 | iVal = iVal*10 + (zText[i] - '0'); |
| 178392 | 179155 | } |
| 178393 | | - *piEndBlock = iVal; |
| 179156 | + *piEndBlock = (i64)iVal; |
| 178394 | 179157 | while( zText[i]==' ' ) i++; |
| 178395 | 179158 | iVal = 0; |
| 178396 | 179159 | if( zText[i]=='-' ){ |
| 178397 | 179160 | i++; |
| 178398 | 179161 | iMul = -1; |
| 178399 | 179162 | } |
| 178400 | 179163 | for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){ |
| 178401 | 179164 | iVal = iVal*10 + (zText[i] - '0'); |
| 178402 | 179165 | } |
| 178403 | | - *pnByte = (iVal * (i64)iMul); |
| 179166 | + *pnByte = ((i64)iVal * (i64)iMul); |
| 178404 | 179167 | } |
| 178405 | 179168 | } |
| 178406 | 179169 | |
| 178407 | 179170 | |
| 178408 | 179171 | /* |
| | @@ -223901,11 +224664,11 @@ |
| 223901 | 224664 | int nArg, /* Number of args */ |
| 223902 | 224665 | sqlite3_value **apUnused /* Function arguments */ |
| 223903 | 224666 | ){ |
| 223904 | 224667 | assert( nArg==0 ); |
| 223905 | 224668 | UNUSED_PARAM2(nArg, apUnused); |
| 223906 | | - sqlite3_result_text(pCtx, "fts5: 2020-03-03 20:04:29 bd94d7d052734460904c687756231f8aa243a2252f07f742dd1e437aa940f536", -1, SQLITE_TRANSIENT); |
| 224669 | + sqlite3_result_text(pCtx, "fts5: 2020-05-08 19:02:21 3a16c0ce4d8851f79f670d94786032c8007619154ece44647dc9cc5b1f9654ff", -1, SQLITE_TRANSIENT); |
| 223907 | 224670 | } |
| 223908 | 224671 | |
| 223909 | 224672 | /* |
| 223910 | 224673 | ** Return true if zName is the extension on one of the shadow tables used |
| 223911 | 224674 | ** by this module. |
| | @@ -228552,11 +229315,12 @@ |
| 228552 | 229315 | } |
| 228553 | 229316 | case STMT_COLUMN_BUSY: { |
| 228554 | 229317 | sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt)); |
| 228555 | 229318 | break; |
| 228556 | 229319 | } |
| 228557 | | - case STMT_COLUMN_MEM: { |
| 229320 | + default: { |
| 229321 | + assert( i==STMT_COLUMN_MEM ); |
| 228558 | 229322 | i = SQLITE_STMTSTATUS_MEMUSED + |
| 228559 | 229323 | STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 228560 | 229324 | /* Fall thru */ |
| 228561 | 229325 | } |
| 228562 | 229326 | case STMT_COLUMN_NSCAN: |
| | @@ -228683,12 +229447,12 @@ |
| 228683 | 229447 | } |
| 228684 | 229448 | #endif /* SQLITE_CORE */ |
| 228685 | 229449 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 228686 | 229450 | |
| 228687 | 229451 | /************** End of stmt.c ************************************************/ |
| 228688 | | -#if __LINE__!=228688 |
| 229452 | +#if __LINE__!=229452 |
| 228689 | 229453 | #undef SQLITE_SOURCE_ID |
| 228690 | | -#define SQLITE_SOURCE_ID "2020-04-20 17:35:32 2fc80ef16ce5878311ab88a0c64631813572ffbb71f75363b4619c9667e0alt2" |
| 229454 | +#define SQLITE_SOURCE_ID "2020-05-08 19:02:21 3a16c0ce4d8851f79f670d94786032c8007619154ece44647dc9cc5b1f96alt2" |
| 228691 | 229455 | #endif |
| 228692 | 229456 | /* Return the source-id for this library */ |
| 228693 | 229457 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 228694 | 229458 | /************************** End of sqlite3.c ******************************/ |
| 228695 | 229459 | |