| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.8.8. By combining all the individual C code files into this |
| 3 | +** version 3.8.9. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -88,10 +88,48 @@ |
| 88 | 88 | #endif /* _MSVC_H_ */ |
| 89 | 89 | |
| 90 | 90 | /************** End of msvc.h ************************************************/ |
| 91 | 91 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 92 | 92 | |
| 93 | +/* |
| 94 | +** Special setup for VxWorks |
| 95 | +*/ |
| 96 | +/************** Include vxworks.h in the middle of sqliteInt.h ***************/ |
| 97 | +/************** Begin file vxworks.h *****************************************/ |
| 98 | +/* |
| 99 | +** 2015-03-02 |
| 100 | +** |
| 101 | +** The author disclaims copyright to this source code. In place of |
| 102 | +** a legal notice, here is a blessing: |
| 103 | +** |
| 104 | +** May you do good and not evil. |
| 105 | +** May you find forgiveness for yourself and forgive others. |
| 106 | +** May you share freely, never taking more than you give. |
| 107 | +** |
| 108 | +****************************************************************************** |
| 109 | +** |
| 110 | +** This file contains code that is specific to Wind River's VxWorks |
| 111 | +*/ |
| 112 | +#if defined(__RTP__) || defined(_WRS_KERNEL) |
| 113 | +/* This is VxWorks. Set up things specially for that OS |
| 114 | +*/ |
| 115 | +#include <vxWorks.h> |
| 116 | +#include <pthread.h> /* amalgamator: dontcache */ |
| 117 | +#define OS_VXWORKS 1 |
| 118 | +#define SQLITE_OS_OTHER 0 |
| 119 | +#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1 |
| 120 | +#define SQLITE_OMIT_LOAD_EXTENSION 1 |
| 121 | +#define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 122 | +#define HAVE_UTIME 1 |
| 123 | +#else |
| 124 | +/* This is not VxWorks. */ |
| 125 | +#define OS_VXWORKS 0 |
| 126 | +#endif /* defined(_WRS_KERNEL) */ |
| 127 | + |
| 128 | +/************** End of vxworks.h *********************************************/ |
| 129 | +/************** Continuing where we left off in sqliteInt.h ******************/ |
| 130 | + |
| 93 | 131 | /* |
| 94 | 132 | ** These #defines should enable >2GB file support on POSIX if the |
| 95 | 133 | ** underlying operating system supports it. If the OS lacks |
| 96 | 134 | ** large file support, or if the OS is windows, these should be no-ops. |
| 97 | 135 | ** |
| | @@ -223,10 +261,17 @@ |
| 223 | 261 | #ifndef SQLITE_API |
| 224 | 262 | # define SQLITE_API |
| 225 | 263 | #endif |
| 226 | 264 | |
| 227 | 265 | |
| 266 | +/* |
| 267 | +** Add the ability to override 'cdecl' |
| 268 | +*/ |
| 269 | +#ifndef SQLITE_CDECL |
| 270 | +# define SQLITE_CDECL |
| 271 | +#endif |
| 272 | + |
| 228 | 273 | /* |
| 229 | 274 | ** These no-op macros are used in front of interfaces to mark those |
| 230 | 275 | ** interfaces as either deprecated or experimental. New applications |
| 231 | 276 | ** should not use deprecated interfaces - they are supported for backwards |
| 232 | 277 | ** compatibility only. Application writers should be aware that |
| | @@ -276,13 +321,13 @@ |
| 276 | 321 | ** |
| 277 | 322 | ** See also: [sqlite3_libversion()], |
| 278 | 323 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 279 | 324 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 280 | 325 | */ |
| 281 | | -#define SQLITE_VERSION "3.8.8" |
| 282 | | -#define SQLITE_VERSION_NUMBER 3008008 |
| 283 | | -#define SQLITE_SOURCE_ID "2015-02-25 14:25:31 6d132e7a224ee68b5cefe9222944aac5760ffc20" |
| 326 | +#define SQLITE_VERSION "3.8.9" |
| 327 | +#define SQLITE_VERSION_NUMBER 3008009 |
| 328 | +#define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72" |
| 284 | 329 | |
| 285 | 330 | /* |
| 286 | 331 | ** CAPI3REF: Run-Time Library Version Numbers |
| 287 | 332 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 288 | 333 | ** |
| | @@ -925,18 +970,20 @@ |
| 925 | 970 | ** |
| 926 | 971 | ** These integer constants are opcodes for the xFileControl method |
| 927 | 972 | ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()] |
| 928 | 973 | ** interface. |
| 929 | 974 | ** |
| 975 | +** <ul> |
| 976 | +** <li>[[SQLITE_FCNTL_LOCKSTATE]] |
| 930 | 977 | ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This |
| 931 | 978 | ** opcode causes the xFileControl method to write the current state of |
| 932 | 979 | ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], |
| 933 | 980 | ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) |
| 934 | 981 | ** into an integer that the pArg argument points to. This capability |
| 935 | | -** is used during testing and only needs to be supported when SQLITE_TEST |
| 936 | | -** is defined. |
| 937 | | -** <ul> |
| 982 | +** is used during testing and is only available when the SQLITE_TEST |
| 983 | +** compile-time option is used. |
| 984 | +** |
| 938 | 985 | ** <li>[[SQLITE_FCNTL_SIZE_HINT]] |
| 939 | 986 | ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS |
| 940 | 987 | ** layer a hint of how large the database file will grow to be during the |
| 941 | 988 | ** current transaction. This hint is not guaranteed to be accurate but it |
| 942 | 989 | ** is often close. The underlying VFS might choose to preallocate database |
| | @@ -1057,11 +1104,13 @@ |
| 1057 | 1104 | ** the error message if the pragma fails. ^If the |
| 1058 | 1105 | ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal |
| 1059 | 1106 | ** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA] |
| 1060 | 1107 | ** file control returns [SQLITE_OK], then the parser assumes that the |
| 1061 | 1108 | ** VFS has handled the PRAGMA itself and the parser generates a no-op |
| 1062 | | -** prepared statement. ^If the [SQLITE_FCNTL_PRAGMA] file control returns |
| 1109 | +** prepared statement if result string is NULL, or that returns a copy |
| 1110 | +** of the result string if the string is non-NULL. |
| 1111 | +** ^If the [SQLITE_FCNTL_PRAGMA] file control returns |
| 1063 | 1112 | ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means |
| 1064 | 1113 | ** that the VFS encountered an error while handling the [PRAGMA] and the |
| 1065 | 1114 | ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA] |
| 1066 | 1115 | ** file control occurs at the beginning of pragma statement analysis and so |
| 1067 | 1116 | ** it is able to override built-in [PRAGMA] statements. |
| | @@ -1115,10 +1164,17 @@ |
| 1115 | 1164 | ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This |
| 1116 | 1165 | ** opcode causes the xFileControl method to swap the file handle with the one |
| 1117 | 1166 | ** pointed to by the pArg argument. This capability is used during testing |
| 1118 | 1167 | ** and only needs to be supported when SQLITE_TEST is defined. |
| 1119 | 1168 | ** |
| 1169 | +** <li>[[SQLITE_FCNTL_WAL_BLOCK]] |
| 1170 | +** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might |
| 1171 | +** be advantageous to block on the next WAL lock if the lock is not immediately |
| 1172 | +** available. The WAL subsystem issues this signal during rare |
| 1173 | +** circumstances in order to fix a problem with priority inversion. |
| 1174 | +** Applications should <em>not</em> use this file-control. |
| 1175 | +** |
| 1120 | 1176 | ** </ul> |
| 1121 | 1177 | */ |
| 1122 | 1178 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1123 | 1179 | #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 |
| 1124 | 1180 | #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 |
| | @@ -1139,10 +1195,11 @@ |
| 1139 | 1195 | #define SQLITE_FCNTL_TRACE 19 |
| 1140 | 1196 | #define SQLITE_FCNTL_HAS_MOVED 20 |
| 1141 | 1197 | #define SQLITE_FCNTL_SYNC 21 |
| 1142 | 1198 | #define SQLITE_FCNTL_COMMIT_PHASETWO 22 |
| 1143 | 1199 | #define SQLITE_FCNTL_WIN32_SET_HANDLE 23 |
| 1200 | +#define SQLITE_FCNTL_WAL_BLOCK 24 |
| 1144 | 1201 | |
| 1145 | 1202 | /* deprecated names */ |
| 1146 | 1203 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 1147 | 1204 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 1148 | 1205 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -1705,11 +1762,11 @@ |
| 1705 | 1762 | ** disabled, the following SQLite interfaces become non-operational: |
| 1706 | 1763 | ** <ul> |
| 1707 | 1764 | ** <li> [sqlite3_memory_used()] |
| 1708 | 1765 | ** <li> [sqlite3_memory_highwater()] |
| 1709 | 1766 | ** <li> [sqlite3_soft_heap_limit64()] |
| 1710 | | -** <li> [sqlite3_status()] |
| 1767 | +** <li> [sqlite3_status64()] |
| 1711 | 1768 | ** </ul>)^ |
| 1712 | 1769 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1713 | 1770 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1714 | 1771 | ** allocation statistics are disabled by default. |
| 1715 | 1772 | ** </dd> |
| | @@ -1916,11 +1973,10 @@ |
| 1916 | 1973 | ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE |
| 1917 | 1974 | ** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is |
| 1918 | 1975 | ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro |
| 1919 | 1976 | ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value |
| 1920 | 1977 | ** that specifies the maximum size of the created heap. |
| 1921 | | -** </dl> |
| 1922 | 1978 | ** |
| 1923 | 1979 | ** [[SQLITE_CONFIG_PCACHE_HDRSZ]] |
| 1924 | 1980 | ** <dt>SQLITE_CONFIG_PCACHE_HDRSZ |
| 1925 | 1981 | ** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which |
| 1926 | 1982 | ** is a pointer to an integer and writes into that integer the number of extra |
| | @@ -3163,15 +3219,17 @@ |
| 3163 | 3219 | |
| 3164 | 3220 | |
| 3165 | 3221 | /* |
| 3166 | 3222 | ** CAPI3REF: Error Codes And Messages |
| 3167 | 3223 | ** |
| 3168 | | -** ^The sqlite3_errcode() interface returns the numeric [result code] or |
| 3169 | | -** [extended result code] for the most recent failed sqlite3_* API call |
| 3170 | | -** associated with a [database connection]. If a prior API call failed |
| 3171 | | -** but the most recent API call succeeded, the return value from |
| 3172 | | -** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode() |
| 3224 | +** ^If the most recent sqlite3_* API call associated with |
| 3225 | +** [database connection] D failed, then the sqlite3_errcode(D) interface |
| 3226 | +** returns the numeric [result code] or [extended result code] for that |
| 3227 | +** API call. |
| 3228 | +** If the most recent API call was successful, |
| 3229 | +** then the return value from sqlite3_errcode() is undefined. |
| 3230 | +** ^The sqlite3_extended_errcode() |
| 3173 | 3231 | ** interface is the same except that it always returns the |
| 3174 | 3232 | ** [extended result code] even when extended result codes are |
| 3175 | 3233 | ** disabled. |
| 3176 | 3234 | ** |
| 3177 | 3235 | ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language |
| | @@ -3356,20 +3414,18 @@ |
| 3356 | 3414 | ** The second argument, "zSql", is the statement to be compiled, encoded |
| 3357 | 3415 | ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() |
| 3358 | 3416 | ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2() |
| 3359 | 3417 | ** use UTF-16. |
| 3360 | 3418 | ** |
| 3361 | | -** ^If the nByte argument is less than zero, then zSql is read up to the |
| 3362 | | -** first zero terminator. ^If nByte is non-negative, then it is the maximum |
| 3363 | | -** number of bytes read from zSql. ^When nByte is non-negative, the |
| 3364 | | -** zSql string ends at either the first '\000' or '\u0000' character or |
| 3365 | | -** the nByte-th byte, whichever comes first. If the caller knows |
| 3366 | | -** that the supplied string is nul-terminated, then there is a small |
| 3367 | | -** performance advantage to be gained by passing an nByte parameter that |
| 3368 | | -** is equal to the number of bytes in the input string <i>including</i> |
| 3369 | | -** the nul-terminator bytes as this saves SQLite from having to |
| 3370 | | -** make a copy of the input string. |
| 3419 | +** ^If the nByte argument is negative, then zSql is read up to the |
| 3420 | +** first zero terminator. ^If nByte is positive, then it is the |
| 3421 | +** number of bytes read from zSql. ^If nByte is zero, then no prepared |
| 3422 | +** statement is generated. |
| 3423 | +** If the caller knows that the supplied string is nul-terminated, then |
| 3424 | +** there is a small performance advantage to passing an nByte parameter that |
| 3425 | +** is the number of bytes in the input string <i>including</i> |
| 3426 | +** the nul-terminator. |
| 3371 | 3427 | ** |
| 3372 | 3428 | ** ^If pzTail is not NULL then *pzTail is made to point to the first byte |
| 3373 | 3429 | ** past the end of the first SQL statement in zSql. These routines only |
| 3374 | 3430 | ** compile the first statement in zSql, so *pzTail is left pointing to |
| 3375 | 3431 | ** what remains uncompiled. |
| | @@ -4394,12 +4450,12 @@ |
| 4394 | 4450 | ** DEPRECATED |
| 4395 | 4451 | ** |
| 4396 | 4452 | ** These functions are [deprecated]. In order to maintain |
| 4397 | 4453 | ** backwards compatibility with older code, these functions continue |
| 4398 | 4454 | ** to be supported. However, new applications should avoid |
| 4399 | | -** the use of these functions. To help encourage people to avoid |
| 4400 | | -** using these functions, we are not going to tell you what they do. |
| 4455 | +** the use of these functions. To encourage programmers to avoid |
| 4456 | +** these functions, we will not explain what they do. |
| 4401 | 4457 | */ |
| 4402 | 4458 | #ifndef SQLITE_OMIT_DEPRECATED |
| 4403 | 4459 | SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); |
| 4404 | 4460 | SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); |
| 4405 | 4461 | SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); |
| | @@ -5783,11 +5839,11 @@ |
| 5783 | 5839 | ** is delivered up to the client application, the string will be automatically |
| 5784 | 5840 | ** freed by sqlite3_free() and the zErrMsg field will be zeroed. |
| 5785 | 5841 | */ |
| 5786 | 5842 | struct sqlite3_vtab { |
| 5787 | 5843 | const sqlite3_module *pModule; /* The module for this virtual table */ |
| 5788 | | - int nRef; /* NO LONGER USED */ |
| 5844 | + int nRef; /* Number of open cursors */ |
| 5789 | 5845 | char *zErrMsg; /* Error message from sqlite3_mprintf() */ |
| 5790 | 5846 | /* Virtual table implementations will typically add additional fields */ |
| 5791 | 5847 | }; |
| 5792 | 5848 | |
| 5793 | 5849 | /* |
| | @@ -6461,11 +6517,11 @@ |
| 6461 | 6517 | #define SQLITE_TESTCTRL_LAST 25 |
| 6462 | 6518 | |
| 6463 | 6519 | /* |
| 6464 | 6520 | ** CAPI3REF: SQLite Runtime Status |
| 6465 | 6521 | ** |
| 6466 | | -** ^This interface is used to retrieve runtime status information |
| 6522 | +** ^These interfaces are used to retrieve runtime status information |
| 6467 | 6523 | ** about the performance of SQLite, and optionally to reset various |
| 6468 | 6524 | ** highwater marks. ^The first argument is an integer code for |
| 6469 | 6525 | ** the specific parameter to measure. ^(Recognized integer codes |
| 6470 | 6526 | ** are of the form [status parameters | SQLITE_STATUS_...].)^ |
| 6471 | 6527 | ** ^The current value of the parameter is returned into *pCurrent. |
| | @@ -6475,23 +6531,26 @@ |
| 6475 | 6531 | ** value. For those parameters |
| 6476 | 6532 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 6477 | 6533 | ** ^(Other parameters record only the highwater mark and not the current |
| 6478 | 6534 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 6479 | 6535 | ** |
| 6480 | | -** ^The sqlite3_status() routine returns SQLITE_OK on success and a |
| 6481 | | -** non-zero [error code] on failure. |
| 6536 | +** ^The sqlite3_status() and sqlite3_status64() routines return |
| 6537 | +** SQLITE_OK on success and a non-zero [error code] on failure. |
| 6482 | 6538 | ** |
| 6483 | | -** This routine is threadsafe but is not atomic. This routine can be |
| 6484 | | -** called while other threads are running the same or different SQLite |
| 6485 | | -** interfaces. However the values returned in *pCurrent and |
| 6486 | | -** *pHighwater reflect the status of SQLite at different points in time |
| 6487 | | -** and it is possible that another thread might change the parameter |
| 6488 | | -** in between the times when *pCurrent and *pHighwater are written. |
| 6539 | +** If either the current value or the highwater mark is too large to |
| 6540 | +** be represented by a 32-bit integer, then the values returned by |
| 6541 | +** sqlite3_status() are undefined. |
| 6489 | 6542 | ** |
| 6490 | 6543 | ** See also: [sqlite3_db_status()] |
| 6491 | 6544 | */ |
| 6492 | 6545 | SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); |
| 6546 | +SQLITE_API int sqlite3_status64( |
| 6547 | + int op, |
| 6548 | + sqlite3_int64 *pCurrent, |
| 6549 | + sqlite3_int64 *pHighwater, |
| 6550 | + int resetFlag |
| 6551 | +); |
| 6493 | 6552 | |
| 6494 | 6553 | |
| 6495 | 6554 | /* |
| 6496 | 6555 | ** CAPI3REF: Status Parameters |
| 6497 | 6556 | ** KEYWORDS: {status parameters} |
| | @@ -7157,24 +7216,24 @@ |
| 7157 | 7216 | ** |
| 7158 | 7217 | ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() |
| 7159 | 7218 | ** is not a permanent error and does not affect the return value of |
| 7160 | 7219 | ** sqlite3_backup_finish(). |
| 7161 | 7220 | ** |
| 7162 | | -** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]] |
| 7221 | +** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]] |
| 7163 | 7222 | ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b> |
| 7164 | 7223 | ** |
| 7165 | | -** ^Each call to sqlite3_backup_step() sets two values inside |
| 7166 | | -** the [sqlite3_backup] object: the number of pages still to be backed |
| 7167 | | -** up and the total number of pages in the source database file. |
| 7168 | | -** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces |
| 7169 | | -** retrieve these two values, respectively. |
| 7170 | | -** |
| 7171 | | -** ^The values returned by these functions are only updated by |
| 7172 | | -** sqlite3_backup_step(). ^If the source database is modified during a backup |
| 7173 | | -** operation, then the values are not updated to account for any extra |
| 7174 | | -** pages that need to be updated or the size of the source database file |
| 7175 | | -** changing. |
| 7224 | +** ^The sqlite3_backup_remaining() routine returns the number of pages still |
| 7225 | +** to be backed up at the conclusion of the most recent sqlite3_backup_step(). |
| 7226 | +** ^The sqlite3_backup_pagecount() routine returns the total number of pages |
| 7227 | +** in the source database at the conclusion of the most recent |
| 7228 | +** sqlite3_backup_step(). |
| 7229 | +** ^(The values returned by these functions are only updated by |
| 7230 | +** sqlite3_backup_step(). If the source database is modified in a way that |
| 7231 | +** changes the size of the source database or the number of pages remaining, |
| 7232 | +** those changes are not reflected in the output of sqlite3_backup_pagecount() |
| 7233 | +** and sqlite3_backup_remaining() until after the next |
| 7234 | +** sqlite3_backup_step().)^ |
| 7176 | 7235 | ** |
| 7177 | 7236 | ** <b>Concurrent Usage of Database Handles</b> |
| 7178 | 7237 | ** |
| 7179 | 7238 | ** ^The source [database connection] may be used by the application for other |
| 7180 | 7239 | ** purposes while a backup operation is underway or being initialized. |
| | @@ -8018,19 +8077,21 @@ |
| 8018 | 8077 | #ifndef SQLITE_MAX_FUNCTION_ARG |
| 8019 | 8078 | # define SQLITE_MAX_FUNCTION_ARG 127 |
| 8020 | 8079 | #endif |
| 8021 | 8080 | |
| 8022 | 8081 | /* |
| 8023 | | -** The maximum number of in-memory pages to use for the main database |
| 8024 | | -** table and for temporary tables. The SQLITE_DEFAULT_CACHE_SIZE |
| 8082 | +** The suggested maximum number of in-memory pages to use for |
| 8083 | +** the main database table and for temporary tables. |
| 8084 | +** |
| 8085 | +** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size |
| 8086 | +** is 2000 pages. |
| 8087 | +** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be |
| 8088 | +** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options. |
| 8025 | 8089 | */ |
| 8026 | 8090 | #ifndef SQLITE_DEFAULT_CACHE_SIZE |
| 8027 | 8091 | # define SQLITE_DEFAULT_CACHE_SIZE 2000 |
| 8028 | 8092 | #endif |
| 8029 | | -#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE |
| 8030 | | -# define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500 |
| 8031 | | -#endif |
| 8032 | 8093 | |
| 8033 | 8094 | /* |
| 8034 | 8095 | ** The default number of frames to accumulate in the log file before |
| 8035 | 8096 | ** checkpointing the database in WAL mode. |
| 8036 | 8097 | */ |
| | @@ -8870,10 +8931,24 @@ |
| 8870 | 8931 | ** |
| 8871 | 8932 | ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 |
| 8872 | 8933 | */ |
| 8873 | 8934 | typedef INT16_TYPE LogEst; |
| 8874 | 8935 | |
| 8936 | +/* |
| 8937 | +** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer |
| 8938 | +*/ |
| 8939 | +#ifndef SQLITE_PTRSIZE |
| 8940 | +# if defined(__SIZEOF_POINTER__) |
| 8941 | +# define SQLITE_PTRSIZE __SIZEOF_POINTER__ |
| 8942 | +# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 8943 | + defined(_M_ARM) || defined(__arm__) || defined(__x86) |
| 8944 | +# define SQLITE_PTRSIZE 4 |
| 8945 | +# else |
| 8946 | +# define SQLITE_PTRSIZE 8 |
| 8947 | +# endif |
| 8948 | +#endif |
| 8949 | + |
| 8875 | 8950 | /* |
| 8876 | 8951 | ** Macros to determine whether the machine is big or little endian, |
| 8877 | 8952 | ** and whether or not that determination is run-time or compile-time. |
| 8878 | 8953 | ** |
| 8879 | 8954 | ** For best performance, an attempt is made to guess at the byte-order |
| | @@ -9320,12 +9395,22 @@ |
| 9320 | 9395 | #define BTREE_DATA_VERSION 15 /* A virtual meta-value */ |
| 9321 | 9396 | |
| 9322 | 9397 | /* |
| 9323 | 9398 | ** Values that may be OR'd together to form the second argument of an |
| 9324 | 9399 | ** sqlite3BtreeCursorHints() call. |
| 9400 | +** |
| 9401 | +** The BTREE_BULKLOAD flag is set on index cursors when the index is going |
| 9402 | +** to be filled with content that is already in sorted order. |
| 9403 | +** |
| 9404 | +** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or |
| 9405 | +** OP_SeekLE opcodes for a range search, but where the range of entries |
| 9406 | +** selected will all have the same key. In other words, the cursor will |
| 9407 | +** be used only for equality key searches. |
| 9408 | +** |
| 9325 | 9409 | */ |
| 9326 | | -#define BTREE_BULKLOAD 0x00000001 |
| 9410 | +#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */ |
| 9411 | +#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */ |
| 9327 | 9412 | |
| 9328 | 9413 | SQLITE_PRIVATE int sqlite3BtreeCursor( |
| 9329 | 9414 | Btree*, /* BTree containing table to open */ |
| 9330 | 9415 | int iTable, /* Index of root page */ |
| 9331 | 9416 | int wrFlag, /* 1 for writing. 0 for read-only */ |
| | @@ -9367,10 +9452,13 @@ |
| 9367 | 9452 | SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); |
| 9368 | 9453 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9369 | 9454 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9370 | 9455 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9371 | 9456 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9457 | +#ifdef SQLITE_DEBUG |
| 9458 | +SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); |
| 9459 | +#endif |
| 9372 | 9460 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9373 | 9461 | SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 9374 | 9462 | |
| 9375 | 9463 | #ifndef NDEBUG |
| 9376 | 9464 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| | @@ -9733,27 +9821,29 @@ |
| 9733 | 9821 | #define OP_FkCounter 134 /* synopsis: fkctr[P1]+=P2 */ |
| 9734 | 9822 | #define OP_FkIfZero 135 /* synopsis: if fkctr[P1]==0 goto P2 */ |
| 9735 | 9823 | #define OP_MemMax 136 /* synopsis: r[P1]=max(r[P1],r[P2]) */ |
| 9736 | 9824 | #define OP_IfPos 137 /* synopsis: if r[P1]>0 goto P2 */ |
| 9737 | 9825 | #define OP_IfNeg 138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2 */ |
| 9738 | | -#define OP_IfZero 139 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2 */ |
| 9739 | | -#define OP_AggFinal 140 /* synopsis: accum=r[P1] N=P2 */ |
| 9740 | | -#define OP_IncrVacuum 141 |
| 9741 | | -#define OP_Expire 142 |
| 9742 | | -#define OP_TableLock 143 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 9743 | | -#define OP_VBegin 144 |
| 9744 | | -#define OP_VCreate 145 |
| 9745 | | -#define OP_VDestroy 146 |
| 9746 | | -#define OP_VOpen 147 |
| 9747 | | -#define OP_VColumn 148 /* synopsis: r[P3]=vcolumn(P2) */ |
| 9748 | | -#define OP_VNext 149 |
| 9749 | | -#define OP_VRename 150 |
| 9750 | | -#define OP_Pagecount 151 |
| 9751 | | -#define OP_MaxPgcnt 152 |
| 9752 | | -#define OP_Init 153 /* synopsis: Start at P2 */ |
| 9753 | | -#define OP_Noop 154 |
| 9754 | | -#define OP_Explain 155 |
| 9826 | +#define OP_IfNotZero 139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */ |
| 9827 | +#define OP_DecrJumpZero 140 /* synopsis: if (--r[P1])==0 goto P2 */ |
| 9828 | +#define OP_JumpZeroIncr 141 /* synopsis: if (r[P1]++)==0 ) goto P2 */ |
| 9829 | +#define OP_AggFinal 142 /* synopsis: accum=r[P1] N=P2 */ |
| 9830 | +#define OP_IncrVacuum 143 |
| 9831 | +#define OP_Expire 144 |
| 9832 | +#define OP_TableLock 145 /* synopsis: iDb=P1 root=P2 write=P3 */ |
| 9833 | +#define OP_VBegin 146 |
| 9834 | +#define OP_VCreate 147 |
| 9835 | +#define OP_VDestroy 148 |
| 9836 | +#define OP_VOpen 149 |
| 9837 | +#define OP_VColumn 150 /* synopsis: r[P3]=vcolumn(P2) */ |
| 9838 | +#define OP_VNext 151 |
| 9839 | +#define OP_VRename 152 |
| 9840 | +#define OP_Pagecount 153 |
| 9841 | +#define OP_MaxPgcnt 154 |
| 9842 | +#define OP_Init 155 /* synopsis: Start at P2 */ |
| 9843 | +#define OP_Noop 156 |
| 9844 | +#define OP_Explain 157 |
| 9755 | 9845 | |
| 9756 | 9846 | |
| 9757 | 9847 | /* Properties such as "out2" or "jump" that are specified in |
| 9758 | 9848 | ** comments following the "case" for each opcode in the vdbe.c |
| 9759 | 9849 | ** are encoded into bitvectors as follows: |
| | @@ -9781,13 +9871,13 @@ |
| 9781 | 9871 | /* 96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,\ |
| 9782 | 9872 | /* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x08, 0x08, 0x00,\ |
| 9783 | 9873 | /* 112 */ 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,\ |
| 9784 | 9874 | /* 120 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 9785 | 9875 | /* 128 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x02, 0x00, 0x01,\ |
| 9786 | | -/* 136 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x01, 0x00, 0x00,\ |
| 9787 | | -/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,\ |
| 9788 | | -/* 152 */ 0x02, 0x01, 0x00, 0x00,} |
| 9876 | +/* 136 */ 0x08, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00, 0x01,\ |
| 9877 | +/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\ |
| 9878 | +/* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,} |
| 9789 | 9879 | |
| 9790 | 9880 | /************** End of opcodes.h *********************************************/ |
| 9791 | 9881 | /************** Continuing where we left off in vdbe.h ***********************/ |
| 9792 | 9882 | |
| 9793 | 9883 | /* |
| | @@ -10865,10 +10955,11 @@ |
| 10865 | 10955 | } init; |
| 10866 | 10956 | int nVdbeActive; /* Number of VDBEs currently running */ |
| 10867 | 10957 | int nVdbeRead; /* Number of active VDBEs that read or write */ |
| 10868 | 10958 | int nVdbeWrite; /* Number of active VDBEs that read and write */ |
| 10869 | 10959 | int nVdbeExec; /* Number of nested calls to VdbeExec() */ |
| 10960 | + int nVDestroy; /* Number of active OP_VDestroy operations */ |
| 10870 | 10961 | int nExtension; /* Number of loaded extensions */ |
| 10871 | 10962 | void **aExtension; /* Array of shared library handles */ |
| 10872 | 10963 | void (*xTrace)(void*,const char*); /* Trace function */ |
| 10873 | 10964 | void *pTraceArg; /* Argument to the trace function */ |
| 10874 | 10965 | void (*xProfile)(void*,const char*,u64); /* Profiling function */ |
| | @@ -12022,11 +12113,11 @@ |
| 12022 | 12113 | #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */ |
| 12023 | 12114 | #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */ |
| 12024 | 12115 | #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */ |
| 12025 | 12116 | #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */ |
| 12026 | 12117 | #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */ |
| 12027 | | - /* 0x0080 // not currently used */ |
| 12118 | +#define WHERE_NO_AUTOINDEX 0x0080 /* Disallow automatic indexes */ |
| 12028 | 12119 | #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */ |
| 12029 | 12120 | #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */ |
| 12030 | 12121 | #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ |
| 12031 | 12122 | #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ |
| 12032 | 12123 | #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ |
| | @@ -12459,11 +12550,12 @@ |
| 12459 | 12550 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 12460 | 12551 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 12461 | 12552 | #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ |
| 12462 | 12553 | #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ |
| 12463 | 12554 | #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ |
| 12464 | | -#define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */ |
| 12555 | +#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */ |
| 12556 | +#define OPFLAG_P2ISREG 0x04 /* P2 to OP_Open** is a register number */ |
| 12465 | 12557 | #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ |
| 12466 | 12558 | |
| 12467 | 12559 | /* |
| 12468 | 12560 | * Each trigger present in the database schema is stored as an instance of |
| 12469 | 12561 | * struct Trigger. |
| | @@ -12863,14 +12955,19 @@ |
| 12863 | 12955 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int); |
| 12864 | 12956 | SQLITE_PRIVATE int sqlite3MutexInit(void); |
| 12865 | 12957 | SQLITE_PRIVATE int sqlite3MutexEnd(void); |
| 12866 | 12958 | #endif |
| 12867 | 12959 | |
| 12868 | | -SQLITE_PRIVATE int sqlite3StatusValue(int); |
| 12869 | | -SQLITE_PRIVATE void sqlite3StatusAdd(int, int); |
| 12960 | +SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int); |
| 12961 | +SQLITE_PRIVATE void sqlite3StatusUp(int, int); |
| 12962 | +SQLITE_PRIVATE void sqlite3StatusDown(int, int); |
| 12870 | 12963 | SQLITE_PRIVATE void sqlite3StatusSet(int, int); |
| 12871 | 12964 | |
| 12965 | +/* Access to mutexes used by sqlite3_status() */ |
| 12966 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void); |
| 12967 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void); |
| 12968 | + |
| 12872 | 12969 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 12873 | 12970 | SQLITE_PRIVATE int sqlite3IsNaN(double); |
| 12874 | 12971 | #else |
| 12875 | 12972 | # define sqlite3IsNaN(X) 0 |
| 12876 | 12973 | #endif |
| | @@ -13246,11 +13343,11 @@ |
| 13246 | 13343 | SQLITE_PRIVATE const char *sqlite3ErrStr(int); |
| 13247 | 13344 | SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); |
| 13248 | 13345 | SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); |
| 13249 | 13346 | SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); |
| 13250 | 13347 | SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); |
| 13251 | | -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*); |
| 13348 | +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); |
| 13252 | 13349 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); |
| 13253 | 13350 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); |
| 13254 | 13351 | SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); |
| 13255 | 13352 | SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); |
| 13256 | 13353 | SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); |
| | @@ -14633,18 +14730,10 @@ |
| 14633 | 14730 | ** An instance of the virtual machine. This structure contains the complete |
| 14634 | 14731 | ** state of the virtual machine. |
| 14635 | 14732 | ** |
| 14636 | 14733 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| 14637 | 14734 | ** is really a pointer to an instance of this structure. |
| 14638 | | -** |
| 14639 | | -** The Vdbe.inVtabMethod variable is set to non-zero for the duration of |
| 14640 | | -** any virtual table method invocations made by the vdbe program. It is |
| 14641 | | -** set to 2 for xDestroy method calls and 1 for all other methods. This |
| 14642 | | -** variable is used for two purposes: to allow xDestroy methods to execute |
| 14643 | | -** "DROP TABLE" statements and to prevent some nasty side effects of |
| 14644 | | -** malloc failure when SQLite is invoked recursively by a virtual table |
| 14645 | | -** method function. |
| 14646 | 14735 | */ |
| 14647 | 14736 | struct Vdbe { |
| 14648 | 14737 | sqlite3 *db; /* The database connection that owns this statement */ |
| 14649 | 14738 | Op *aOp; /* Space to hold the virtual machine's program */ |
| 14650 | 14739 | Mem *aMem; /* The memory locations */ |
| | @@ -14671,11 +14760,10 @@ |
| 14671 | 14760 | #endif |
| 14672 | 14761 | u16 nResColumn; /* Number of columns in one row of the result set */ |
| 14673 | 14762 | u8 errorAction; /* Recovery action to do in case of an error */ |
| 14674 | 14763 | u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 14675 | 14764 | bft explain:2; /* True if EXPLAIN present on SQL command */ |
| 14676 | | - bft inVtabMethod:2; /* See comments above */ |
| 14677 | 14765 | bft changeCntOn:1; /* True to update the change-counter */ |
| 14678 | 14766 | bft expired:1; /* True if the VM needs to be recompiled */ |
| 14679 | 14767 | bft runOnlyOnce:1; /* Automatically expire on reset */ |
| 14680 | 14768 | bft usesStmtJournal:1; /* True if uses a statement journal */ |
| 14681 | 14769 | bft readOnly:1; /* True for statements that do not write */ |
| | @@ -14831,13 +14919,35 @@ |
| 14831 | 14919 | /* |
| 14832 | 14920 | ** Variables in which to record status information. |
| 14833 | 14921 | */ |
| 14834 | 14922 | typedef struct sqlite3StatType sqlite3StatType; |
| 14835 | 14923 | static SQLITE_WSD struct sqlite3StatType { |
| 14836 | | - int nowValue[10]; /* Current value */ |
| 14837 | | - int mxValue[10]; /* Maximum value */ |
| 14924 | +#if SQLITE_PTRSIZE>4 |
| 14925 | + sqlite3_int64 nowValue[10]; /* Current value */ |
| 14926 | + sqlite3_int64 mxValue[10]; /* Maximum value */ |
| 14927 | +#else |
| 14928 | + u32 nowValue[10]; /* Current value */ |
| 14929 | + u32 mxValue[10]; /* Maximum value */ |
| 14930 | +#endif |
| 14838 | 14931 | } sqlite3Stat = { {0,}, {0,} }; |
| 14932 | + |
| 14933 | +/* |
| 14934 | +** Elements of sqlite3Stat[] are protected by either the memory allocator |
| 14935 | +** mutex, or by the pcache1 mutex. The following array determines which. |
| 14936 | +*/ |
| 14937 | +static const char statMutex[] = { |
| 14938 | + 0, /* SQLITE_STATUS_MEMORY_USED */ |
| 14939 | + 1, /* SQLITE_STATUS_PAGECACHE_USED */ |
| 14940 | + 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */ |
| 14941 | + 0, /* SQLITE_STATUS_SCRATCH_USED */ |
| 14942 | + 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */ |
| 14943 | + 0, /* SQLITE_STATUS_MALLOC_SIZE */ |
| 14944 | + 0, /* SQLITE_STATUS_PARSER_STACK */ |
| 14945 | + 1, /* SQLITE_STATUS_PAGECACHE_SIZE */ |
| 14946 | + 0, /* SQLITE_STATUS_SCRATCH_SIZE */ |
| 14947 | + 0, /* SQLITE_STATUS_MALLOC_COUNT */ |
| 14948 | +}; |
| 14839 | 14949 | |
| 14840 | 14950 | |
| 14841 | 14951 | /* The "wsdStat" macro will resolve to the status information |
| 14842 | 14952 | ** state vector. If writable static data is unsupported on the target, |
| 14843 | 14953 | ** we have to locate the state vector at run-time. In the more common |
| | @@ -14851,64 +14961,110 @@ |
| 14851 | 14961 | # define wsdStatInit |
| 14852 | 14962 | # define wsdStat sqlite3Stat |
| 14853 | 14963 | #endif |
| 14854 | 14964 | |
| 14855 | 14965 | /* |
| 14856 | | -** Return the current value of a status parameter. |
| 14966 | +** Return the current value of a status parameter. The caller must |
| 14967 | +** be holding the appropriate mutex. |
| 14857 | 14968 | */ |
| 14858 | | -SQLITE_PRIVATE int sqlite3StatusValue(int op){ |
| 14969 | +SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){ |
| 14859 | 14970 | wsdStatInit; |
| 14860 | 14971 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 14972 | + assert( op>=0 && op<ArraySize(statMutex) ); |
| 14973 | + assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() |
| 14974 | + : sqlite3MallocMutex()) ); |
| 14861 | 14975 | return wsdStat.nowValue[op]; |
| 14862 | 14976 | } |
| 14863 | 14977 | |
| 14864 | 14978 | /* |
| 14865 | | -** Add N to the value of a status record. It is assumed that the |
| 14866 | | -** caller holds appropriate locks. |
| 14979 | +** Add N to the value of a status record. The caller must hold the |
| 14980 | +** appropriate mutex. (Locking is checked by assert()). |
| 14981 | +** |
| 14982 | +** The StatusUp() routine can accept positive or negative values for N. |
| 14983 | +** The value of N is added to the current status value and the high-water |
| 14984 | +** mark is adjusted if necessary. |
| 14985 | +** |
| 14986 | +** The StatusDown() routine lowers the current value by N. The highwater |
| 14987 | +** mark is unchanged. N must be non-negative for StatusDown(). |
| 14867 | 14988 | */ |
| 14868 | | -SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){ |
| 14989 | +SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){ |
| 14869 | 14990 | wsdStatInit; |
| 14870 | 14991 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 14992 | + assert( op>=0 && op<ArraySize(statMutex) ); |
| 14993 | + assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() |
| 14994 | + : sqlite3MallocMutex()) ); |
| 14871 | 14995 | wsdStat.nowValue[op] += N; |
| 14872 | 14996 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 14873 | 14997 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14874 | 14998 | } |
| 14875 | 14999 | } |
| 15000 | +SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){ |
| 15001 | + wsdStatInit; |
| 15002 | + assert( N>=0 ); |
| 15003 | + assert( op>=0 && op<ArraySize(statMutex) ); |
| 15004 | + assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() |
| 15005 | + : sqlite3MallocMutex()) ); |
| 15006 | + assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 15007 | + wsdStat.nowValue[op] -= N; |
| 15008 | +} |
| 14876 | 15009 | |
| 14877 | 15010 | /* |
| 14878 | | -** Set the value of a status to X. |
| 15011 | +** Set the value of a status to X. The highwater mark is adjusted if |
| 15012 | +** necessary. The caller must hold the appropriate mutex. |
| 14879 | 15013 | */ |
| 14880 | 15014 | SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){ |
| 14881 | 15015 | wsdStatInit; |
| 14882 | 15016 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 15017 | + assert( op>=0 && op<ArraySize(statMutex) ); |
| 15018 | + assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() |
| 15019 | + : sqlite3MallocMutex()) ); |
| 14883 | 15020 | wsdStat.nowValue[op] = X; |
| 14884 | 15021 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 14885 | 15022 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14886 | 15023 | } |
| 14887 | 15024 | } |
| 14888 | 15025 | |
| 14889 | 15026 | /* |
| 14890 | 15027 | ** Query status information. |
| 14891 | | -** |
| 14892 | | -** This implementation assumes that reading or writing an aligned |
| 14893 | | -** 32-bit integer is an atomic operation. If that assumption is not true, |
| 14894 | | -** then this routine is not threadsafe. |
| 14895 | 15028 | */ |
| 14896 | | -SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 15029 | +SQLITE_API int sqlite3_status64( |
| 15030 | + int op, |
| 15031 | + sqlite3_int64 *pCurrent, |
| 15032 | + sqlite3_int64 *pHighwater, |
| 15033 | + int resetFlag |
| 15034 | +){ |
| 15035 | + sqlite3_mutex *pMutex; |
| 14897 | 15036 | wsdStatInit; |
| 14898 | 15037 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14899 | 15038 | return SQLITE_MISUSE_BKPT; |
| 14900 | 15039 | } |
| 14901 | 15040 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 14902 | 15041 | if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 14903 | 15042 | #endif |
| 15043 | + pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex(); |
| 15044 | + sqlite3_mutex_enter(pMutex); |
| 14904 | 15045 | *pCurrent = wsdStat.nowValue[op]; |
| 14905 | 15046 | *pHighwater = wsdStat.mxValue[op]; |
| 14906 | 15047 | if( resetFlag ){ |
| 14907 | 15048 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14908 | 15049 | } |
| 15050 | + sqlite3_mutex_leave(pMutex); |
| 15051 | + (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */ |
| 14909 | 15052 | return SQLITE_OK; |
| 15053 | +} |
| 15054 | +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 15055 | + sqlite3_int64 iCur, iHwtr; |
| 15056 | + int rc; |
| 15057 | +#ifdef SQLITE_ENABLE_API_ARMOR |
| 15058 | + if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 15059 | +#endif |
| 15060 | + rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag); |
| 15061 | + if( rc==0 ){ |
| 15062 | + *pCurrent = (int)iCur; |
| 15063 | + *pHighwater = (int)iHwtr; |
| 15064 | + } |
| 15065 | + return rc; |
| 14910 | 15066 | } |
| 14911 | 15067 | |
| 14912 | 15068 | /* |
| 14913 | 15069 | ** Query status information for a single database connection |
| 14914 | 15070 | */ |
| | @@ -20358,10 +20514,17 @@ |
| 20358 | 20514 | */ |
| 20359 | 20515 | int nearlyFull; |
| 20360 | 20516 | } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 20361 | 20517 | |
| 20362 | 20518 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
| 20519 | + |
| 20520 | +/* |
| 20521 | +** Return the memory allocator mutex. sqlite3_status() needs it. |
| 20522 | +*/ |
| 20523 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){ |
| 20524 | + return mem0.mutex; |
| 20525 | +} |
| 20363 | 20526 | |
| 20364 | 20527 | /* |
| 20365 | 20528 | ** This routine runs when the memory allocator sees that the |
| 20366 | 20529 | ** total memory allocation is about to exceed the soft heap |
| 20367 | 20530 | ** limit. |
| | @@ -20381,11 +20544,11 @@ |
| 20381 | 20544 | static int sqlite3MemoryAlarm( |
| 20382 | 20545 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 20383 | 20546 | void *pArg, |
| 20384 | 20547 | sqlite3_int64 iThreshold |
| 20385 | 20548 | ){ |
| 20386 | | - int nUsed; |
| 20549 | + sqlite3_int64 nUsed; |
| 20387 | 20550 | sqlite3_mutex_enter(mem0.mutex); |
| 20388 | 20551 | mem0.alarmCallback = xCallback; |
| 20389 | 20552 | mem0.alarmArg = pArg; |
| 20390 | 20553 | mem0.alarmThreshold = iThreshold; |
| 20391 | 20554 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| | @@ -20550,11 +20713,11 @@ |
| 20550 | 20713 | void *p; |
| 20551 | 20714 | assert( sqlite3_mutex_held(mem0.mutex) ); |
| 20552 | 20715 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 20553 | 20716 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 20554 | 20717 | if( mem0.alarmCallback!=0 ){ |
| 20555 | | - int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 20718 | + sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 20556 | 20719 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 20557 | 20720 | mem0.nearlyFull = 1; |
| 20558 | 20721 | sqlite3MallocAlarm(nFull); |
| 20559 | 20722 | }else{ |
| 20560 | 20723 | mem0.nearlyFull = 0; |
| | @@ -20567,12 +20730,12 @@ |
| 20567 | 20730 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 20568 | 20731 | } |
| 20569 | 20732 | #endif |
| 20570 | 20733 | if( p ){ |
| 20571 | 20734 | nFull = sqlite3MallocSize(p); |
| 20572 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 20573 | | - sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20735 | + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); |
| 20736 | + sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20574 | 20737 | } |
| 20575 | 20738 | *pp = p; |
| 20576 | 20739 | return nFull; |
| 20577 | 20740 | } |
| 20578 | 20741 | |
| | @@ -20645,18 +20808,18 @@ |
| 20645 | 20808 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 20646 | 20809 | if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ |
| 20647 | 20810 | p = mem0.pScratchFree; |
| 20648 | 20811 | mem0.pScratchFree = mem0.pScratchFree->pNext; |
| 20649 | 20812 | mem0.nScratchFree--; |
| 20650 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20813 | + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20651 | 20814 | sqlite3_mutex_leave(mem0.mutex); |
| 20652 | 20815 | }else{ |
| 20653 | 20816 | sqlite3_mutex_leave(mem0.mutex); |
| 20654 | 20817 | p = sqlite3Malloc(n); |
| 20655 | 20818 | if( sqlite3GlobalConfig.bMemstat && p ){ |
| 20656 | 20819 | sqlite3_mutex_enter(mem0.mutex); |
| 20657 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
| 20820 | + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
| 20658 | 20821 | sqlite3_mutex_leave(mem0.mutex); |
| 20659 | 20822 | } |
| 20660 | 20823 | sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 20661 | 20824 | } |
| 20662 | 20825 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| | @@ -20693,23 +20856,23 @@ |
| 20693 | 20856 | sqlite3_mutex_enter(mem0.mutex); |
| 20694 | 20857 | pSlot->pNext = mem0.pScratchFree; |
| 20695 | 20858 | mem0.pScratchFree = pSlot; |
| 20696 | 20859 | mem0.nScratchFree++; |
| 20697 | 20860 | assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); |
| 20698 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 20861 | + sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20699 | 20862 | sqlite3_mutex_leave(mem0.mutex); |
| 20700 | 20863 | }else{ |
| 20701 | 20864 | /* Release memory back to the heap */ |
| 20702 | 20865 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 20703 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 20866 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); |
| 20704 | 20867 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20705 | 20868 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20706 | 20869 | int iSize = sqlite3MallocSize(p); |
| 20707 | 20870 | sqlite3_mutex_enter(mem0.mutex); |
| 20708 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 20709 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 20710 | | - sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 20871 | + sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); |
| 20872 | + sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); |
| 20873 | + sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20711 | 20874 | sqlite3GlobalConfig.m.xFree(p); |
| 20712 | 20875 | sqlite3_mutex_leave(mem0.mutex); |
| 20713 | 20876 | }else{ |
| 20714 | 20877 | sqlite3GlobalConfig.m.xFree(p); |
| 20715 | 20878 | } |
| | @@ -20736,26 +20899,26 @@ |
| 20736 | 20899 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20737 | 20900 | return sqlite3GlobalConfig.m.xSize(p); |
| 20738 | 20901 | } |
| 20739 | 20902 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 20740 | 20903 | if( db==0 ){ |
| 20741 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20904 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20742 | 20905 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20743 | 20906 | return sqlite3MallocSize(p); |
| 20744 | 20907 | }else{ |
| 20745 | 20908 | assert( sqlite3_mutex_held(db->mutex) ); |
| 20746 | 20909 | if( isLookaside(db, p) ){ |
| 20747 | 20910 | return db->lookaside.sz; |
| 20748 | 20911 | }else{ |
| 20749 | 20912 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20750 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20913 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20751 | 20914 | return sqlite3GlobalConfig.m.xSize(p); |
| 20752 | 20915 | } |
| 20753 | 20916 | } |
| 20754 | 20917 | } |
| 20755 | 20918 | SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){ |
| 20756 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20919 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20757 | 20920 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20758 | 20921 | return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); |
| 20759 | 20922 | } |
| 20760 | 20923 | |
| 20761 | 20924 | /* |
| | @@ -20762,15 +20925,15 @@ |
| 20762 | 20925 | ** Free memory previously obtained from sqlite3Malloc(). |
| 20763 | 20926 | */ |
| 20764 | 20927 | SQLITE_API void sqlite3_free(void *p){ |
| 20765 | 20928 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 20766 | 20929 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20767 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20930 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20768 | 20931 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20769 | 20932 | sqlite3_mutex_enter(mem0.mutex); |
| 20770 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 20771 | | - sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 20933 | + sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); |
| 20934 | + sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20772 | 20935 | sqlite3GlobalConfig.m.xFree(p); |
| 20773 | 20936 | sqlite3_mutex_leave(mem0.mutex); |
| 20774 | 20937 | }else{ |
| 20775 | 20938 | sqlite3GlobalConfig.m.xFree(p); |
| 20776 | 20939 | } |
| | @@ -20807,11 +20970,11 @@ |
| 20807 | 20970 | db->lookaside.nOut--; |
| 20808 | 20971 | return; |
| 20809 | 20972 | } |
| 20810 | 20973 | } |
| 20811 | 20974 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20812 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20975 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20813 | 20976 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20814 | 20977 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20815 | 20978 | sqlite3_free(p); |
| 20816 | 20979 | } |
| 20817 | 20980 | |
| | @@ -20820,11 +20983,11 @@ |
| 20820 | 20983 | */ |
| 20821 | 20984 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 20822 | 20985 | int nOld, nNew, nDiff; |
| 20823 | 20986 | void *pNew; |
| 20824 | 20987 | assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20825 | | - assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20988 | + assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); |
| 20826 | 20989 | if( pOld==0 ){ |
| 20827 | 20990 | return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 20828 | 20991 | } |
| 20829 | 20992 | if( nBytes==0 ){ |
| 20830 | 20993 | sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| | @@ -20854,11 +21017,11 @@ |
| 20854 | 21017 | sqlite3MallocAlarm((int)nBytes); |
| 20855 | 21018 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20856 | 21019 | } |
| 20857 | 21020 | if( pNew ){ |
| 20858 | 21021 | nNew = sqlite3MallocSize(pNew); |
| 20859 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 21022 | + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 20860 | 21023 | } |
| 20861 | 21024 | sqlite3_mutex_leave(mem0.mutex); |
| 20862 | 21025 | }else{ |
| 20863 | 21026 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20864 | 21027 | } |
| | @@ -20987,11 +21150,11 @@ |
| 20987 | 21150 | memcpy(pNew, p, db->lookaside.sz); |
| 20988 | 21151 | sqlite3DbFree(db, p); |
| 20989 | 21152 | } |
| 20990 | 21153 | }else{ |
| 20991 | 21154 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20992 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 21155 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20993 | 21156 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20994 | 21157 | pNew = sqlite3_realloc64(p, n); |
| 20995 | 21158 | if( !pNew ){ |
| 20996 | 21159 | db->mallocFailed = 1; |
| 20997 | 21160 | } |
| | @@ -24969,27 +25132,29 @@ |
| 24969 | 25132 | /* 134 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 24970 | 25133 | /* 135 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 24971 | 25134 | /* 136 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 24972 | 25135 | /* 137 */ "IfPos" OpHelp("if r[P1]>0 goto P2"), |
| 24973 | 25136 | /* 138 */ "IfNeg" OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"), |
| 24974 | | - /* 139 */ "IfZero" OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"), |
| 24975 | | - /* 140 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 24976 | | - /* 141 */ "IncrVacuum" OpHelp(""), |
| 24977 | | - /* 142 */ "Expire" OpHelp(""), |
| 24978 | | - /* 143 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 24979 | | - /* 144 */ "VBegin" OpHelp(""), |
| 24980 | | - /* 145 */ "VCreate" OpHelp(""), |
| 24981 | | - /* 146 */ "VDestroy" OpHelp(""), |
| 24982 | | - /* 147 */ "VOpen" OpHelp(""), |
| 24983 | | - /* 148 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 24984 | | - /* 149 */ "VNext" OpHelp(""), |
| 24985 | | - /* 150 */ "VRename" OpHelp(""), |
| 24986 | | - /* 151 */ "Pagecount" OpHelp(""), |
| 24987 | | - /* 152 */ "MaxPgcnt" OpHelp(""), |
| 24988 | | - /* 153 */ "Init" OpHelp("Start at P2"), |
| 24989 | | - /* 154 */ "Noop" OpHelp(""), |
| 24990 | | - /* 155 */ "Explain" OpHelp(""), |
| 25137 | + /* 139 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"), |
| 25138 | + /* 140 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 25139 | + /* 141 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"), |
| 25140 | + /* 142 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 25141 | + /* 143 */ "IncrVacuum" OpHelp(""), |
| 25142 | + /* 144 */ "Expire" OpHelp(""), |
| 25143 | + /* 145 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 25144 | + /* 146 */ "VBegin" OpHelp(""), |
| 25145 | + /* 147 */ "VCreate" OpHelp(""), |
| 25146 | + /* 148 */ "VDestroy" OpHelp(""), |
| 25147 | + /* 149 */ "VOpen" OpHelp(""), |
| 25148 | + /* 150 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 25149 | + /* 151 */ "VNext" OpHelp(""), |
| 25150 | + /* 152 */ "VRename" OpHelp(""), |
| 25151 | + /* 153 */ "Pagecount" OpHelp(""), |
| 25152 | + /* 154 */ "MaxPgcnt" OpHelp(""), |
| 25153 | + /* 155 */ "Init" OpHelp("Start at P2"), |
| 25154 | + /* 156 */ "Noop" OpHelp(""), |
| 25155 | + /* 157 */ "Explain" OpHelp(""), |
| 24991 | 25156 | }; |
| 24992 | 25157 | return azName[i]; |
| 24993 | 25158 | } |
| 24994 | 25159 | #endif |
| 24995 | 25160 | |
| | @@ -25065,22 +25230,10 @@ |
| 25065 | 25230 | # else |
| 25066 | 25231 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 25067 | 25232 | # endif |
| 25068 | 25233 | #endif |
| 25069 | 25234 | |
| 25070 | | -/* |
| 25071 | | -** Define the OS_VXWORKS pre-processor macro to 1 if building on |
| 25072 | | -** vxworks, or 0 otherwise. |
| 25073 | | -*/ |
| 25074 | | -#ifndef OS_VXWORKS |
| 25075 | | -# if defined(__RTP__) || defined(_WRS_KERNEL) |
| 25076 | | -# define OS_VXWORKS 1 |
| 25077 | | -# else |
| 25078 | | -# define OS_VXWORKS 0 |
| 25079 | | -# endif |
| 25080 | | -#endif |
| 25081 | | - |
| 25082 | 25235 | /* |
| 25083 | 25236 | ** standard include files. |
| 25084 | 25237 | */ |
| 25085 | 25238 | #include <sys/types.h> |
| 25086 | 25239 | #include <sys/stat.h> |
| | @@ -25091,22 +25244,23 @@ |
| 25091 | 25244 | #include <errno.h> |
| 25092 | 25245 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 25093 | 25246 | # include <sys/mman.h> |
| 25094 | 25247 | #endif |
| 25095 | 25248 | |
| 25096 | | -#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
| 25249 | +#if SQLITE_ENABLE_LOCKING_STYLE |
| 25097 | 25250 | # include <sys/ioctl.h> |
| 25098 | | -# if OS_VXWORKS |
| 25099 | | -# include <semaphore.h> |
| 25100 | | -# include <limits.h> |
| 25101 | | -# else |
| 25102 | | -# include <sys/file.h> |
| 25103 | | -# include <sys/param.h> |
| 25104 | | -# endif |
| 25251 | +# include <sys/file.h> |
| 25252 | +# include <sys/param.h> |
| 25105 | 25253 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 25106 | 25254 | |
| 25107 | | -#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
| 25255 | +#if OS_VXWORKS |
| 25256 | +/* # include <sys/ioctl.h> */ |
| 25257 | +# include <semaphore.h> |
| 25258 | +# include <limits.h> |
| 25259 | +#endif /* OS_VXWORKS */ |
| 25260 | + |
| 25261 | +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 25108 | 25262 | # include <sys/mount.h> |
| 25109 | 25263 | #endif |
| 25110 | 25264 | |
| 25111 | 25265 | #ifdef HAVE_UTIME |
| 25112 | 25266 | # include <utime.h> |
| | @@ -25142,10 +25296,14 @@ |
| 25142 | 25296 | |
| 25143 | 25297 | /* |
| 25144 | 25298 | ** Maximum supported path-length. |
| 25145 | 25299 | */ |
| 25146 | 25300 | #define MAX_PATHNAME 512 |
| 25301 | + |
| 25302 | +/* Always cast the getpid() return type for compatibility with |
| 25303 | +** kernel modules in VxWorks. */ |
| 25304 | +#define osGetpid(X) (pid_t)getpid() |
| 25147 | 25305 | |
| 25148 | 25306 | /* |
| 25149 | 25307 | ** Only set the lastErrno if the error code is a real error and not |
| 25150 | 25308 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 25151 | 25309 | */ |
| | @@ -25231,11 +25389,11 @@ |
| 25231 | 25389 | |
| 25232 | 25390 | /* This variable holds the process id (pid) from when the xRandomness() |
| 25233 | 25391 | ** method was called. If xOpen() is called from a different process id, |
| 25234 | 25392 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 25235 | 25393 | */ |
| 25236 | | -static int randomnessPid = 0; |
| 25394 | +static pid_t randomnessPid = 0; |
| 25237 | 25395 | |
| 25238 | 25396 | /* |
| 25239 | 25397 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 25240 | 25398 | */ |
| 25241 | 25399 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| | @@ -25249,10 +25407,11 @@ |
| 25249 | 25407 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| 25250 | 25408 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 25251 | 25409 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 25252 | 25410 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
| 25253 | 25411 | #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */ |
| 25412 | +#define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */ |
| 25254 | 25413 | |
| 25255 | 25414 | /* |
| 25256 | 25415 | ** Include code that is common to all os_*.c files |
| 25257 | 25416 | */ |
| 25258 | 25417 | /************** Include os_common.h in the middle of os_unix.c ***************/ |
| | @@ -25587,11 +25746,11 @@ |
| 25587 | 25746 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
| 25588 | 25747 | |
| 25589 | 25748 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
| 25590 | 25749 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 25591 | 25750 | |
| 25592 | | -#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
| 25751 | +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
| 25593 | 25752 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
| 25594 | 25753 | #else |
| 25595 | 25754 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
| 25596 | 25755 | #endif |
| 25597 | 25756 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| | @@ -25604,11 +25763,11 @@ |
| 25604 | 25763 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 25605 | 25764 | |
| 25606 | 25765 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
| 25607 | 25766 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 25608 | 25767 | |
| 25609 | | -#if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
| 25768 | +#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
| 25610 | 25769 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
| 25611 | 25770 | #else |
| 25612 | 25771 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
| 25613 | 25772 | #endif |
| 25614 | 25773 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| | @@ -26743,11 +26902,12 @@ |
| 26743 | 26902 | int tErrno = 0; |
| 26744 | 26903 | |
| 26745 | 26904 | assert( pFile ); |
| 26746 | 26905 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 26747 | 26906 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 26748 | | - azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
| 26907 | + azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
| 26908 | + osGetpid(0))); |
| 26749 | 26909 | |
| 26750 | 26910 | /* If there is already a lock of this type or more restrictive on the |
| 26751 | 26911 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
| 26752 | 26912 | ** unixEnterMutex() hasn't been called yet. |
| 26753 | 26913 | */ |
| | @@ -26951,11 +27111,11 @@ |
| 26951 | 27111 | int rc = SQLITE_OK; |
| 26952 | 27112 | |
| 26953 | 27113 | assert( pFile ); |
| 26954 | 27114 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
| 26955 | 27115 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 26956 | | - getpid())); |
| 27116 | + osGetpid(0))); |
| 26957 | 27117 | |
| 26958 | 27118 | assert( eFileLock<=SHARED_LOCK ); |
| 26959 | 27119 | if( pFile->eFileLock<=eFileLock ){ |
| 26960 | 27120 | return SQLITE_OK; |
| 26961 | 27121 | } |
| | @@ -27378,11 +27538,11 @@ |
| 27378 | 27538 | char *zLockFile = (char *)pFile->lockingContext; |
| 27379 | 27539 | int rc; |
| 27380 | 27540 | |
| 27381 | 27541 | assert( pFile ); |
| 27382 | 27542 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 27383 | | - pFile->eFileLock, getpid())); |
| 27543 | + pFile->eFileLock, osGetpid(0))); |
| 27384 | 27544 | assert( eFileLock<=SHARED_LOCK ); |
| 27385 | 27545 | |
| 27386 | 27546 | /* no-op if possible */ |
| 27387 | 27547 | if( pFile->eFileLock==eFileLock ){ |
| 27388 | 27548 | return SQLITE_OK; |
| | @@ -27441,14 +27601,13 @@ |
| 27441 | 27601 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 27442 | 27602 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 27443 | 27603 | ** still works when you do this, but concurrency is reduced since |
| 27444 | 27604 | ** only a single process can be reading the database at a time. |
| 27445 | 27605 | ** |
| 27446 | | -** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 27447 | | -** compiling for VXWORKS. |
| 27606 | +** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
| 27448 | 27607 | */ |
| 27449 | | -#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
| 27608 | +#if SQLITE_ENABLE_LOCKING_STYLE |
| 27450 | 27609 | |
| 27451 | 27610 | /* |
| 27452 | 27611 | ** Retry flock() calls that fail with EINTR |
| 27453 | 27612 | */ |
| 27454 | 27613 | #ifdef EINTR |
| | @@ -27597,11 +27756,11 @@ |
| 27597 | 27756 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
| 27598 | 27757 | unixFile *pFile = (unixFile*)id; |
| 27599 | 27758 | |
| 27600 | 27759 | assert( pFile ); |
| 27601 | 27760 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 27602 | | - pFile->eFileLock, getpid())); |
| 27761 | + pFile->eFileLock, osGetpid(0))); |
| 27603 | 27762 | assert( eFileLock<=SHARED_LOCK ); |
| 27604 | 27763 | |
| 27605 | 27764 | /* no-op if possible */ |
| 27606 | 27765 | if( pFile->eFileLock==eFileLock ){ |
| 27607 | 27766 | return SQLITE_OK; |
| | @@ -27658,11 +27817,11 @@ |
| 27658 | 27817 | ** This routine checks if there is a RESERVED lock held on the specified |
| 27659 | 27818 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 27660 | 27819 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 27661 | 27820 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 27662 | 27821 | */ |
| 27663 | | -static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 27822 | +static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 27664 | 27823 | int rc = SQLITE_OK; |
| 27665 | 27824 | int reserved = 0; |
| 27666 | 27825 | unixFile *pFile = (unixFile*)id; |
| 27667 | 27826 | |
| 27668 | 27827 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| | @@ -27725,11 +27884,11 @@ |
| 27725 | 27884 | ** access the file. |
| 27726 | 27885 | ** |
| 27727 | 27886 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 27728 | 27887 | ** routine to lower a locking level. |
| 27729 | 27888 | */ |
| 27730 | | -static int semLock(sqlite3_file *id, int eFileLock) { |
| 27889 | +static int semXLock(sqlite3_file *id, int eFileLock) { |
| 27731 | 27890 | unixFile *pFile = (unixFile*)id; |
| 27732 | 27891 | sem_t *pSem = pFile->pInode->pSem; |
| 27733 | 27892 | int rc = SQLITE_OK; |
| 27734 | 27893 | |
| 27735 | 27894 | /* if we already have a lock, it is exclusive. |
| | @@ -27758,18 +27917,18 @@ |
| 27758 | 27917 | ** must be either NO_LOCK or SHARED_LOCK. |
| 27759 | 27918 | ** |
| 27760 | 27919 | ** If the locking level of the file descriptor is already at or below |
| 27761 | 27920 | ** the requested locking level, this routine is a no-op. |
| 27762 | 27921 | */ |
| 27763 | | -static int semUnlock(sqlite3_file *id, int eFileLock) { |
| 27922 | +static int semXUnlock(sqlite3_file *id, int eFileLock) { |
| 27764 | 27923 | unixFile *pFile = (unixFile*)id; |
| 27765 | 27924 | sem_t *pSem = pFile->pInode->pSem; |
| 27766 | 27925 | |
| 27767 | 27926 | assert( pFile ); |
| 27768 | 27927 | assert( pSem ); |
| 27769 | 27928 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 27770 | | - pFile->eFileLock, getpid())); |
| 27929 | + pFile->eFileLock, osGetpid(0))); |
| 27771 | 27930 | assert( eFileLock<=SHARED_LOCK ); |
| 27772 | 27931 | |
| 27773 | 27932 | /* no-op if possible */ |
| 27774 | 27933 | if( pFile->eFileLock==eFileLock ){ |
| 27775 | 27934 | return SQLITE_OK; |
| | @@ -27795,14 +27954,14 @@ |
| 27795 | 27954 | } |
| 27796 | 27955 | |
| 27797 | 27956 | /* |
| 27798 | 27957 | ** Close a file. |
| 27799 | 27958 | */ |
| 27800 | | -static int semClose(sqlite3_file *id) { |
| 27959 | +static int semXClose(sqlite3_file *id) { |
| 27801 | 27960 | if( id ){ |
| 27802 | 27961 | unixFile *pFile = (unixFile*)id; |
| 27803 | | - semUnlock(id, NO_LOCK); |
| 27962 | + semXUnlock(id, NO_LOCK); |
| 27804 | 27963 | assert( pFile ); |
| 27805 | 27964 | unixEnterMutex(); |
| 27806 | 27965 | releaseInodeInfo(pFile); |
| 27807 | 27966 | unixLeaveMutex(); |
| 27808 | 27967 | closeUnixFile(id); |
| | @@ -27979,11 +28138,11 @@ |
| 27979 | 28138 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 27980 | 28139 | |
| 27981 | 28140 | assert( pFile ); |
| 27982 | 28141 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 27983 | 28142 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 27984 | | - azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
| 28143 | + azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
| 27985 | 28144 | |
| 27986 | 28145 | /* If there is already a lock of this type or more restrictive on the |
| 27987 | 28146 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
| 27988 | 28147 | ** unixEnterMutex() hasn't been called yet. |
| 27989 | 28148 | */ |
| | @@ -28165,11 +28324,11 @@ |
| 28165 | 28324 | #endif |
| 28166 | 28325 | |
| 28167 | 28326 | assert( pFile ); |
| 28168 | 28327 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
| 28169 | 28328 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 28170 | | - getpid())); |
| 28329 | + osGetpid(0))); |
| 28171 | 28330 | |
| 28172 | 28331 | assert( eFileLock<=SHARED_LOCK ); |
| 28173 | 28332 | if( pFile->eFileLock<=eFileLock ){ |
| 28174 | 28333 | return SQLITE_OK; |
| 28175 | 28334 | } |
| | @@ -28990,10 +29149,14 @@ |
| 28990 | 29149 | ** Information and control of an open file handle. |
| 28991 | 29150 | */ |
| 28992 | 29151 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| 28993 | 29152 | unixFile *pFile = (unixFile*)id; |
| 28994 | 29153 | switch( op ){ |
| 29154 | + case SQLITE_FCNTL_WAL_BLOCK: { |
| 29155 | + pFile->ctrlFlags |= UNIXFILE_BLOCK; |
| 29156 | + return SQLITE_OK; |
| 29157 | + } |
| 28995 | 29158 | case SQLITE_FCNTL_LOCKSTATE: { |
| 28996 | 29159 | *(int*)pArg = pFile->eFileLock; |
| 28997 | 29160 | return SQLITE_OK; |
| 28998 | 29161 | } |
| 28999 | 29162 | case SQLITE_FCNTL_LAST_ERRNO: { |
| | @@ -29204,11 +29367,13 @@ |
| 29204 | 29367 | ** |
| 29205 | 29368 | ** This function should not be called directly by other code in this file. |
| 29206 | 29369 | ** Instead, it should be called via macro osGetpagesize(). |
| 29207 | 29370 | */ |
| 29208 | 29371 | static int unixGetpagesize(void){ |
| 29209 | | -#if defined(_BSD_SOURCE) |
| 29372 | +#if OS_VXWORKS |
| 29373 | + return 1024; |
| 29374 | +#elif defined(_BSD_SOURCE) |
| 29210 | 29375 | return getpagesize(); |
| 29211 | 29376 | #else |
| 29212 | 29377 | return (int)sysconf(_SC_PAGESIZE); |
| 29213 | 29378 | #endif |
| 29214 | 29379 | } |
| | @@ -29297,37 +29462,42 @@ |
| 29297 | 29462 | ** |
| 29298 | 29463 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 29299 | 29464 | ** otherwise. |
| 29300 | 29465 | */ |
| 29301 | 29466 | static int unixShmSystemLock( |
| 29302 | | - unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 29467 | + unixFile *pFile, /* Open connection to the WAL file */ |
| 29303 | 29468 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
| 29304 | 29469 | int ofst, /* First byte of the locking range */ |
| 29305 | 29470 | int n /* Number of bytes to lock */ |
| 29306 | 29471 | ){ |
| 29307 | | - struct flock f; /* The posix advisory locking structure */ |
| 29308 | | - int rc = SQLITE_OK; /* Result code form fcntl() */ |
| 29472 | + unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 29473 | + struct flock f; /* The posix advisory locking structure */ |
| 29474 | + int rc = SQLITE_OK; /* Result code form fcntl() */ |
| 29309 | 29475 | |
| 29310 | 29476 | /* Access to the unixShmNode object is serialized by the caller */ |
| 29477 | + pShmNode = pFile->pInode->pShmNode; |
| 29311 | 29478 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
| 29312 | 29479 | |
| 29313 | 29480 | /* Shared locks never span more than one byte */ |
| 29314 | 29481 | assert( n==1 || lockType!=F_RDLCK ); |
| 29315 | 29482 | |
| 29316 | 29483 | /* Locks are within range */ |
| 29317 | 29484 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
| 29318 | 29485 | |
| 29319 | 29486 | if( pShmNode->h>=0 ){ |
| 29487 | + int lkType; |
| 29320 | 29488 | /* Initialize the locking parameters */ |
| 29321 | 29489 | memset(&f, 0, sizeof(f)); |
| 29322 | 29490 | f.l_type = lockType; |
| 29323 | 29491 | f.l_whence = SEEK_SET; |
| 29324 | 29492 | f.l_start = ofst; |
| 29325 | 29493 | f.l_len = n; |
| 29326 | 29494 | |
| 29327 | | - rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 29495 | + lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK; |
| 29496 | + rc = osFcntl(pShmNode->h, lkType, &f); |
| 29328 | 29497 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 29498 | + pFile->ctrlFlags &= ~UNIXFILE_BLOCK; |
| 29329 | 29499 | } |
| 29330 | 29500 | |
| 29331 | 29501 | /* Update the global lock state and do debug tracing */ |
| 29332 | 29502 | #ifdef SQLITE_DEBUG |
| 29333 | 29503 | { u16 mask; |
| | @@ -29533,17 +29703,17 @@ |
| 29533 | 29703 | |
| 29534 | 29704 | /* Check to see if another process is holding the dead-man switch. |
| 29535 | 29705 | ** If not, truncate the file to zero length. |
| 29536 | 29706 | */ |
| 29537 | 29707 | rc = SQLITE_OK; |
| 29538 | | - if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 29708 | + if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 29539 | 29709 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 29540 | 29710 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
| 29541 | 29711 | } |
| 29542 | 29712 | } |
| 29543 | 29713 | if( rc==SQLITE_OK ){ |
| 29544 | | - rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 29714 | + rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 29545 | 29715 | } |
| 29546 | 29716 | if( rc ) goto shm_open_err; |
| 29547 | 29717 | } |
| 29548 | 29718 | } |
| 29549 | 29719 | |
| | @@ -29771,11 +29941,11 @@ |
| 29771 | 29941 | allMask |= pX->sharedMask; |
| 29772 | 29942 | } |
| 29773 | 29943 | |
| 29774 | 29944 | /* Unlock the system-level locks */ |
| 29775 | 29945 | if( (mask & allMask)==0 ){ |
| 29776 | | - rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 29946 | + rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 29777 | 29947 | }else{ |
| 29778 | 29948 | rc = SQLITE_OK; |
| 29779 | 29949 | } |
| 29780 | 29950 | |
| 29781 | 29951 | /* Undo the local locks */ |
| | @@ -29799,11 +29969,11 @@ |
| 29799 | 29969 | } |
| 29800 | 29970 | |
| 29801 | 29971 | /* Get shared locks at the system level, if necessary */ |
| 29802 | 29972 | if( rc==SQLITE_OK ){ |
| 29803 | 29973 | if( (allShared & mask)==0 ){ |
| 29804 | | - rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 29974 | + rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 29805 | 29975 | }else{ |
| 29806 | 29976 | rc = SQLITE_OK; |
| 29807 | 29977 | } |
| 29808 | 29978 | } |
| 29809 | 29979 | |
| | @@ -29824,20 +29994,20 @@ |
| 29824 | 29994 | |
| 29825 | 29995 | /* Get the exclusive locks at the system level. Then if successful |
| 29826 | 29996 | ** also mark the local connection as being locked. |
| 29827 | 29997 | */ |
| 29828 | 29998 | if( rc==SQLITE_OK ){ |
| 29829 | | - rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 29999 | + rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 29830 | 30000 | if( rc==SQLITE_OK ){ |
| 29831 | 30001 | assert( (p->sharedMask & mask)==0 ); |
| 29832 | 30002 | p->exclMask |= mask; |
| 29833 | 30003 | } |
| 29834 | 30004 | } |
| 29835 | 30005 | } |
| 29836 | 30006 | sqlite3_mutex_leave(pShmNode->mutex); |
| 29837 | 30007 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 29838 | | - p->id, getpid(), p->sharedMask, p->exclMask)); |
| 30008 | + p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
| 29839 | 30009 | return rc; |
| 29840 | 30010 | } |
| 29841 | 30011 | |
| 29842 | 30012 | /* |
| 29843 | 30013 | ** Implement a memory barrier or memory fence on shared memory. |
| | @@ -30236,11 +30406,11 @@ |
| 30236 | 30406 | dotlockUnlock, /* xUnlock method */ |
| 30237 | 30407 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 30238 | 30408 | 0 /* xShmMap method */ |
| 30239 | 30409 | ) |
| 30240 | 30410 | |
| 30241 | | -#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
| 30411 | +#if SQLITE_ENABLE_LOCKING_STYLE |
| 30242 | 30412 | IOMETHODS( |
| 30243 | 30413 | flockIoFinder, /* Finder function name */ |
| 30244 | 30414 | flockIoMethods, /* sqlite3_io_methods object name */ |
| 30245 | 30415 | 1, /* shared memory is disabled */ |
| 30246 | 30416 | flockClose, /* xClose method */ |
| | @@ -30254,14 +30424,14 @@ |
| 30254 | 30424 | #if OS_VXWORKS |
| 30255 | 30425 | IOMETHODS( |
| 30256 | 30426 | semIoFinder, /* Finder function name */ |
| 30257 | 30427 | semIoMethods, /* sqlite3_io_methods object name */ |
| 30258 | 30428 | 1, /* shared memory is disabled */ |
| 30259 | | - semClose, /* xClose method */ |
| 30260 | | - semLock, /* xLock method */ |
| 30261 | | - semUnlock, /* xUnlock method */ |
| 30262 | | - semCheckReservedLock, /* xCheckReservedLock method */ |
| 30429 | + semXClose, /* xClose method */ |
| 30430 | + semXLock, /* xLock method */ |
| 30431 | + semXUnlock, /* xUnlock method */ |
| 30432 | + semXCheckReservedLock, /* xCheckReservedLock method */ |
| 30263 | 30433 | 0 /* xShmMap method */ |
| 30264 | 30434 | ) |
| 30265 | 30435 | #endif |
| 30266 | 30436 | |
| 30267 | 30437 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| | @@ -30381,19 +30551,17 @@ |
| 30381 | 30551 | static const sqlite3_io_methods |
| 30382 | 30552 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
| 30383 | 30553 | |
| 30384 | 30554 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 30385 | 30555 | |
| 30386 | | -#if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 30387 | | -/* |
| 30388 | | -** This "finder" function attempts to determine the best locking strategy |
| 30389 | | -** for the database file "filePath". It then returns the sqlite3_io_methods |
| 30390 | | -** object that implements that strategy. |
| 30391 | | -** |
| 30392 | | -** This is for VXWorks only. |
| 30556 | +#if OS_VXWORKS |
| 30557 | +/* |
| 30558 | +** This "finder" function for VxWorks checks to see if posix advisory |
| 30559 | +** locking works. If it does, then that is what is used. If it does not |
| 30560 | +** work, then fallback to named semaphore locking. |
| 30393 | 30561 | */ |
| 30394 | | -static const sqlite3_io_methods *autolockIoFinderImpl( |
| 30562 | +static const sqlite3_io_methods *vxworksIoFinderImpl( |
| 30395 | 30563 | const char *filePath, /* name of the database file */ |
| 30396 | 30564 | unixFile *pNew /* the open file object */ |
| 30397 | 30565 | ){ |
| 30398 | 30566 | struct flock lockInfo; |
| 30399 | 30567 | |
| | @@ -30415,13 +30583,13 @@ |
| 30415 | 30583 | }else{ |
| 30416 | 30584 | return &semIoMethods; |
| 30417 | 30585 | } |
| 30418 | 30586 | } |
| 30419 | 30587 | static const sqlite3_io_methods |
| 30420 | | - *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
| 30588 | + *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
| 30421 | 30589 | |
| 30422 | | -#endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 30590 | +#endif /* OS_VXWORKS */ |
| 30423 | 30591 | |
| 30424 | 30592 | /* |
| 30425 | 30593 | ** An abstract type for a pointer to an IO method finder function: |
| 30426 | 30594 | */ |
| 30427 | 30595 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
| | @@ -30930,12 +31098,12 @@ |
| 30930 | 31098 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 30931 | 31099 | ** here such that two or more threads all trying to open databases at |
| 30932 | 31100 | ** the same instant might all reset the PRNG. But multiple resets |
| 30933 | 31101 | ** are harmless. |
| 30934 | 31102 | */ |
| 30935 | | - if( randomnessPid!=getpid() ){ |
| 30936 | | - randomnessPid = getpid(); |
| 31103 | + if( randomnessPid!=osGetpid(0) ){ |
| 31104 | + randomnessPid = osGetpid(0); |
| 30937 | 31105 | sqlite3_randomness(0,0); |
| 30938 | 31106 | } |
| 30939 | 31107 | |
| 30940 | 31108 | memset(p, 0, sizeof(unixFile)); |
| 30941 | 31109 | |
| | @@ -31322,11 +31490,11 @@ |
| 31322 | 31490 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 31323 | 31491 | ** that we always use the same random number sequence. This makes the |
| 31324 | 31492 | ** tests repeatable. |
| 31325 | 31493 | */ |
| 31326 | 31494 | memset(zBuf, 0, nBuf); |
| 31327 | | - randomnessPid = getpid(); |
| 31495 | + randomnessPid = osGetpid(0); |
| 31328 | 31496 | #if !defined(SQLITE_TEST) |
| 31329 | 31497 | { |
| 31330 | 31498 | int fd, got; |
| 31331 | 31499 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
| 31332 | 31500 | if( fd<0 ){ |
| | @@ -31643,11 +31811,11 @@ |
| 31643 | 31811 | #else |
| 31644 | 31812 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 31645 | 31813 | { |
| 31646 | 31814 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
| 31647 | 31815 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 31648 | | - lPath, errno, getpid())); |
| 31816 | + lPath, errno, osGetpid(0))); |
| 31649 | 31817 | return SQLITE_IOERR_LOCK; |
| 31650 | 31818 | } |
| 31651 | 31819 | len = strlcat(lPath, "sqliteplocks", maxLen); |
| 31652 | 31820 | } |
| 31653 | 31821 | # else |
| | @@ -31665,11 +31833,11 @@ |
| 31665 | 31833 | char c = dbPath[i]; |
| 31666 | 31834 | lPath[i+len] = (c=='/')?'_':c; |
| 31667 | 31835 | } |
| 31668 | 31836 | lPath[i+len]='\0'; |
| 31669 | 31837 | strlcat(lPath, ":auto:", maxLen); |
| 31670 | | - OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
| 31838 | + OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
| 31671 | 31839 | return SQLITE_OK; |
| 31672 | 31840 | } |
| 31673 | 31841 | |
| 31674 | 31842 | /* |
| 31675 | 31843 | ** Creates the lock file and any missing directories in lockPath |
| | @@ -31692,20 +31860,20 @@ |
| 31692 | 31860 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 31693 | 31861 | int err=errno; |
| 31694 | 31862 | if( err!=EEXIST ) { |
| 31695 | 31863 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
| 31696 | 31864 | "'%s' proxy lock path=%s pid=%d\n", |
| 31697 | | - buf, strerror(err), lockPath, getpid())); |
| 31865 | + buf, strerror(err), lockPath, osGetpid(0))); |
| 31698 | 31866 | return err; |
| 31699 | 31867 | } |
| 31700 | 31868 | } |
| 31701 | 31869 | } |
| 31702 | 31870 | start=i+1; |
| 31703 | 31871 | } |
| 31704 | 31872 | buf[i] = lockPath[i]; |
| 31705 | 31873 | } |
| 31706 | | - OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
| 31874 | + OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0))); |
| 31707 | 31875 | return 0; |
| 31708 | 31876 | } |
| 31709 | 31877 | |
| 31710 | 31878 | /* |
| 31711 | 31879 | ** Create a new VFS file descriptor (stored in memory obtained from |
| | @@ -32006,11 +32174,12 @@ |
| 32006 | 32174 | int readLen = 0; |
| 32007 | 32175 | int tryOldLockPath = 0; |
| 32008 | 32176 | int forceNewLockPath = 0; |
| 32009 | 32177 | |
| 32010 | 32178 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 32011 | | - (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
| 32179 | + (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 32180 | + osGetpid(0))); |
| 32012 | 32181 | |
| 32013 | 32182 | rc = proxyGetHostID(myHostID, &pError); |
| 32014 | 32183 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 32015 | 32184 | storeLastErrno(pFile, pError); |
| 32016 | 32185 | goto end_takeconch; |
| | @@ -32216,11 +32385,11 @@ |
| 32216 | 32385 | |
| 32217 | 32386 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 32218 | 32387 | conchFile = pCtx->conchFile; |
| 32219 | 32388 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
| 32220 | 32389 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 32221 | | - getpid())); |
| 32390 | + osGetpid(0))); |
| 32222 | 32391 | if( pCtx->conchHeld>0 ){ |
| 32223 | 32392 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 32224 | 32393 | } |
| 32225 | 32394 | pCtx->conchHeld = 0; |
| 32226 | 32395 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| | @@ -32358,11 +32527,11 @@ |
| 32358 | 32527 | }else{ |
| 32359 | 32528 | lockPath=(char *)path; |
| 32360 | 32529 | } |
| 32361 | 32530 | |
| 32362 | 32531 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 32363 | | - (lockPath ? lockPath : ":auto:"), getpid())); |
| 32532 | + (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
| 32364 | 32533 | |
| 32365 | 32534 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 32366 | 32535 | if( pCtx==0 ){ |
| 32367 | 32536 | return SQLITE_NOMEM; |
| 32368 | 32537 | } |
| | @@ -32699,26 +32868,28 @@ |
| 32699 | 32868 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 32700 | 32869 | ** by the SQLite core when the VFS is registered. So the following |
| 32701 | 32870 | ** array cannot be const. |
| 32702 | 32871 | */ |
| 32703 | 32872 | static sqlite3_vfs aVfs[] = { |
| 32704 | | -#if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
| 32873 | +#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 32705 | 32874 | UNIXVFS("unix", autolockIoFinder ), |
| 32875 | +#elif OS_VXWORKS |
| 32876 | + UNIXVFS("unix", vxworksIoFinder ), |
| 32706 | 32877 | #else |
| 32707 | 32878 | UNIXVFS("unix", posixIoFinder ), |
| 32708 | 32879 | #endif |
| 32709 | 32880 | UNIXVFS("unix-none", nolockIoFinder ), |
| 32710 | 32881 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
| 32711 | 32882 | UNIXVFS("unix-excl", posixIoFinder ), |
| 32712 | 32883 | #if OS_VXWORKS |
| 32713 | 32884 | UNIXVFS("unix-namedsem", semIoFinder ), |
| 32714 | 32885 | #endif |
| 32715 | | -#if SQLITE_ENABLE_LOCKING_STYLE |
| 32886 | +#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
| 32716 | 32887 | UNIXVFS("unix-posix", posixIoFinder ), |
| 32717 | | -#if !OS_VXWORKS |
| 32888 | +#endif |
| 32889 | +#if SQLITE_ENABLE_LOCKING_STYLE |
| 32718 | 32890 | UNIXVFS("unix-flock", flockIoFinder ), |
| 32719 | | -#endif |
| 32720 | 32891 | #endif |
| 32721 | 32892 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 32722 | 32893 | UNIXVFS("unix-afp", afpIoFinder ), |
| 32723 | 32894 | UNIXVFS("unix-nfs", nfsIoFinder ), |
| 32724 | 32895 | UNIXVFS("unix-proxy", proxyIoFinder ), |
| | @@ -39072,16 +39243,24 @@ |
| 39072 | 39243 | sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); |
| 39073 | 39244 | } |
| 39074 | 39245 | } |
| 39075 | 39246 | |
| 39076 | 39247 | /* |
| 39077 | | -** Compute the number of pages of cache requested. |
| 39248 | +** Compute the number of pages of cache requested. p->szCache is the |
| 39249 | +** cache size requested by the "PRAGMA cache_size" statement. |
| 39250 | +** |
| 39251 | +** |
| 39078 | 39252 | */ |
| 39079 | 39253 | static int numberOfCachePages(PCache *p){ |
| 39080 | 39254 | if( p->szCache>=0 ){ |
| 39255 | + /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the |
| 39256 | + ** suggested cache size is set to N. */ |
| 39081 | 39257 | return p->szCache; |
| 39082 | 39258 | }else{ |
| 39259 | + /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then |
| 39260 | + ** the number of cache pages is adjusted to use approximately abs(N*1024) |
| 39261 | + ** bytes of memory. */ |
| 39083 | 39262 | return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); |
| 39084 | 39263 | } |
| 39085 | 39264 | } |
| 39086 | 39265 | |
| 39087 | 39266 | /*************************************************** General Interfaces ****** |
| | @@ -39817,20 +39996,20 @@ |
| 39817 | 39996 | ** in pcache1 need to be protected via mutex. |
| 39818 | 39997 | */ |
| 39819 | 39998 | static void *pcache1Alloc(int nByte){ |
| 39820 | 39999 | void *p = 0; |
| 39821 | 40000 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 39822 | | - sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 39823 | 40001 | if( nByte<=pcache1.szSlot ){ |
| 39824 | 40002 | sqlite3_mutex_enter(pcache1.mutex); |
| 39825 | 40003 | p = (PgHdr1 *)pcache1.pFree; |
| 39826 | 40004 | if( p ){ |
| 39827 | 40005 | pcache1.pFree = pcache1.pFree->pNext; |
| 39828 | 40006 | pcache1.nFreeSlot--; |
| 39829 | 40007 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 39830 | 40008 | assert( pcache1.nFreeSlot>=0 ); |
| 39831 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 40009 | + sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 40010 | + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 39832 | 40011 | } |
| 39833 | 40012 | sqlite3_mutex_leave(pcache1.mutex); |
| 39834 | 40013 | } |
| 39835 | 40014 | if( p==0 ){ |
| 39836 | 40015 | /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get |
| | @@ -39839,11 +40018,12 @@ |
| 39839 | 40018 | p = sqlite3Malloc(nByte); |
| 39840 | 40019 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 39841 | 40020 | if( p ){ |
| 39842 | 40021 | int sz = sqlite3MallocSize(p); |
| 39843 | 40022 | sqlite3_mutex_enter(pcache1.mutex); |
| 39844 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 40023 | + sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 40024 | + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 39845 | 40025 | sqlite3_mutex_leave(pcache1.mutex); |
| 39846 | 40026 | } |
| 39847 | 40027 | #endif |
| 39848 | 40028 | sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 39849 | 40029 | } |
| | @@ -39857,11 +40037,11 @@ |
| 39857 | 40037 | int nFreed = 0; |
| 39858 | 40038 | if( p==0 ) return 0; |
| 39859 | 40039 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 39860 | 40040 | PgFreeslot *pSlot; |
| 39861 | 40041 | sqlite3_mutex_enter(pcache1.mutex); |
| 39862 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 40042 | + sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 39863 | 40043 | pSlot = (PgFreeslot*)p; |
| 39864 | 40044 | pSlot->pNext = pcache1.pFree; |
| 39865 | 40045 | pcache1.pFree = pSlot; |
| 39866 | 40046 | pcache1.nFreeSlot++; |
| 39867 | 40047 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| | @@ -39871,11 +40051,11 @@ |
| 39871 | 40051 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 39872 | 40052 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 39873 | 40053 | nFreed = sqlite3MallocSize(p); |
| 39874 | 40054 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 39875 | 40055 | sqlite3_mutex_enter(pcache1.mutex); |
| 39876 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed); |
| 40056 | + sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed); |
| 39877 | 40057 | sqlite3_mutex_leave(pcache1.mutex); |
| 39878 | 40058 | #endif |
| 39879 | 40059 | sqlite3_free(p); |
| 39880 | 40060 | } |
| 39881 | 40061 | return nFreed; |
| | @@ -40607,10 +40787,18 @@ |
| 40607 | 40787 | |
| 40608 | 40788 | /* |
| 40609 | 40789 | ** Return the size of the header on each page of this PCACHE implementation. |
| 40610 | 40790 | */ |
| 40611 | 40791 | SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); } |
| 40792 | + |
| 40793 | +/* |
| 40794 | +** Return the global mutex used by this PCACHE implementation. The |
| 40795 | +** sqlite3_status() routine needs access to this mutex. |
| 40796 | +*/ |
| 40797 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){ |
| 40798 | + return pcache1.mutex; |
| 40799 | +} |
| 40612 | 40800 | |
| 40613 | 40801 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40614 | 40802 | /* |
| 40615 | 40803 | ** This function is called to free superfluous dynamically allocated memory |
| 40616 | 40804 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| | @@ -49380,13 +49568,14 @@ |
| 49380 | 49568 | if( pWal->exclusiveMode ) return; |
| 49381 | 49569 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 49382 | 49570 | SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); |
| 49383 | 49571 | WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); |
| 49384 | 49572 | } |
| 49385 | | -static int walLockExclusive(Wal *pWal, int lockIdx, int n){ |
| 49573 | +static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){ |
| 49386 | 49574 | int rc; |
| 49387 | 49575 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 49576 | + if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0); |
| 49388 | 49577 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 49389 | 49578 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 49390 | 49579 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 49391 | 49580 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
| 49392 | 49581 | VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| | @@ -49668,11 +49857,11 @@ |
| 49668 | 49857 | assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 ); |
| 49669 | 49858 | assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE ); |
| 49670 | 49859 | assert( pWal->writeLock ); |
| 49671 | 49860 | iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; |
| 49672 | 49861 | nLock = SQLITE_SHM_NLOCK - iLock; |
| 49673 | | - rc = walLockExclusive(pWal, iLock, nLock); |
| 49862 | + rc = walLockExclusive(pWal, iLock, nLock, 0); |
| 49674 | 49863 | if( rc ){ |
| 49675 | 49864 | return rc; |
| 49676 | 49865 | } |
| 49677 | 49866 | WALTRACE(("WAL%p: recovery begin...\n", pWal)); |
| 49678 | 49867 | |
| | @@ -50202,11 +50391,11 @@ |
| 50202 | 50391 | int lockIdx, /* Offset of first byte to lock */ |
| 50203 | 50392 | int n /* Number of bytes to lock */ |
| 50204 | 50393 | ){ |
| 50205 | 50394 | int rc; |
| 50206 | 50395 | do { |
| 50207 | | - rc = walLockExclusive(pWal, lockIdx, n); |
| 50396 | + rc = walLockExclusive(pWal, lockIdx, n, 0); |
| 50208 | 50397 | }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); |
| 50209 | 50398 | return rc; |
| 50210 | 50399 | } |
| 50211 | 50400 | |
| 50212 | 50401 | /* |
| | @@ -50635,11 +50824,11 @@ |
| 50635 | 50824 | if( pWal->readOnly & WAL_SHM_RDONLY ){ |
| 50636 | 50825 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 50637 | 50826 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 50638 | 50827 | rc = SQLITE_READONLY_RECOVERY; |
| 50639 | 50828 | } |
| 50640 | | - }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ |
| 50829 | + }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){ |
| 50641 | 50830 | pWal->writeLock = 1; |
| 50642 | 50831 | if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 50643 | 50832 | badHdr = walIndexTryHdr(pWal, pChanged); |
| 50644 | 50833 | if( badHdr ){ |
| 50645 | 50834 | /* If the wal-index header is still malformed even while holding |
| | @@ -50841,11 +51030,11 @@ |
| 50841 | 51030 | { |
| 50842 | 51031 | if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 50843 | 51032 | && (mxReadMark<pWal->hdr.mxFrame || mxI==0) |
| 50844 | 51033 | ){ |
| 50845 | 51034 | for(i=1; i<WAL_NREADER; i++){ |
| 50846 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 51035 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0); |
| 50847 | 51036 | if( rc==SQLITE_OK ){ |
| 50848 | 51037 | mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame; |
| 50849 | 51038 | mxI = i; |
| 50850 | 51039 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 50851 | 51040 | break; |
| | @@ -51097,11 +51286,11 @@ |
| 51097 | 51286 | } |
| 51098 | 51287 | |
| 51099 | 51288 | /* Only one writer allowed at a time. Get the write lock. Return |
| 51100 | 51289 | ** SQLITE_BUSY if unable. |
| 51101 | 51290 | */ |
| 51102 | | - rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 51291 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0); |
| 51103 | 51292 | if( rc ){ |
| 51104 | 51293 | return rc; |
| 51105 | 51294 | } |
| 51106 | 51295 | pWal->writeLock = 1; |
| 51107 | 51296 | |
| | @@ -51242,11 +51431,11 @@ |
| 51242 | 51431 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 51243 | 51432 | assert( pInfo->nBackfill==pWal->hdr.mxFrame ); |
| 51244 | 51433 | if( pInfo->nBackfill>0 ){ |
| 51245 | 51434 | u32 salt1; |
| 51246 | 51435 | sqlite3_randomness(4, &salt1); |
| 51247 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 51436 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0); |
| 51248 | 51437 | if( rc==SQLITE_OK ){ |
| 51249 | 51438 | /* If all readers are using WAL_READ_LOCK(0) (in other words if no |
| 51250 | 51439 | ** readers are currently using the WAL), then the transactions |
| 51251 | 51440 | ** frames will overwrite the start of the existing log. Update the |
| 51252 | 51441 | ** wal-index header to reflect this. |
| | @@ -51567,11 +51756,11 @@ |
| 51567 | 51756 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 51568 | 51757 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 51569 | 51758 | |
| 51570 | 51759 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 51571 | 51760 | ** "checkpoint" lock on the database file. */ |
| 51572 | | - rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 51761 | + rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0); |
| 51573 | 51762 | if( rc ){ |
| 51574 | 51763 | /* EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 51575 | 51764 | ** checkpoint operation at the same time, the lock cannot be obtained and |
| 51576 | 51765 | ** SQLITE_BUSY is returned. |
| 51577 | 51766 | ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| | @@ -52569,10 +52758,11 @@ |
| 52569 | 52758 | |
| 52570 | 52759 | /* |
| 52571 | 52760 | ** Exit the recursive mutex on a Btree. |
| 52572 | 52761 | */ |
| 52573 | 52762 | SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){ |
| 52763 | + assert( sqlite3_mutex_held(p->db->mutex) ); |
| 52574 | 52764 | if( p->sharable ){ |
| 52575 | 52765 | assert( p->wantToLock>0 ); |
| 52576 | 52766 | p->wantToLock--; |
| 52577 | 52767 | if( p->wantToLock==0 ){ |
| 52578 | 52768 | unlockBtreeMutex(p); |
| | @@ -54747,12 +54937,12 @@ |
| 54747 | 54937 | /* |
| 54748 | 54938 | ** The following asserts make sure that structures used by the btree are |
| 54749 | 54939 | ** the right size. This is to guard against size changes that result |
| 54750 | 54940 | ** when compiling on a different architecture. |
| 54751 | 54941 | */ |
| 54752 | | - assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 54753 | | - assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 54942 | + assert( sizeof(i64)==8 ); |
| 54943 | + assert( sizeof(u64)==8 ); |
| 54754 | 54944 | assert( sizeof(u32)==4 ); |
| 54755 | 54945 | assert( sizeof(u16)==2 ); |
| 54756 | 54946 | assert( sizeof(Pgno)==4 ); |
| 54757 | 54947 | |
| 54758 | 54948 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| | @@ -60210,11 +60400,12 @@ |
| 60210 | 60400 | ** the previous call, as the overflow cell data will have been |
| 60211 | 60401 | ** copied either into the body of a database page or into the new |
| 60212 | 60402 | ** pSpace buffer passed to the latter call to balance_nonroot(). |
| 60213 | 60403 | */ |
| 60214 | 60404 | u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
| 60215 | | - rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints); |
| 60405 | + rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, |
| 60406 | + pCur->hints&BTREE_BULKLOAD); |
| 60216 | 60407 | if( pFree ){ |
| 60217 | 60408 | /* If pFree is not NULL, it points to the pSpace buffer used |
| 60218 | 60409 | ** by a previous call to balance_nonroot(). Its contents are |
| 60219 | 60410 | ** now stored either on real database pages or within the |
| 60220 | 60411 | ** new pSpace buffer, so it may be safely freed here. */ |
| | @@ -61873,17 +62064,26 @@ |
| 61873 | 62064 | pBt->btsFlags &= ~BTS_NO_WAL; |
| 61874 | 62065 | return rc; |
| 61875 | 62066 | } |
| 61876 | 62067 | |
| 61877 | 62068 | /* |
| 61878 | | -** set the mask of hint flags for cursor pCsr. Currently the only valid |
| 61879 | | -** values are 0 and BTREE_BULKLOAD. |
| 62069 | +** set the mask of hint flags for cursor pCsr. |
| 61880 | 62070 | */ |
| 61881 | 62071 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
| 61882 | | - assert( mask==BTREE_BULKLOAD || mask==0 ); |
| 62072 | + assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 ); |
| 61883 | 62073 | pCsr->hints = mask; |
| 61884 | 62074 | } |
| 62075 | + |
| 62076 | +#ifdef SQLITE_DEBUG |
| 62077 | +/* |
| 62078 | +** Return true if the cursor has a hint specified. This routine is |
| 62079 | +** only used from within assert() statements |
| 62080 | +*/ |
| 62081 | +SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){ |
| 62082 | + return (pCsr->hints & mask)!=0; |
| 62083 | +} |
| 62084 | +#endif |
| 61885 | 62085 | |
| 61886 | 62086 | /* |
| 61887 | 62087 | ** Return true if the given Btree is read-only. |
| 61888 | 62088 | */ |
| 61889 | 62089 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| | @@ -63773,11 +63973,11 @@ |
| 63773 | 63973 | ** by calling sqlite3ValueNew(). |
| 63774 | 63974 | ** |
| 63775 | 63975 | ** Otherwise, if the second argument is non-zero, then this function is |
| 63776 | 63976 | ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not |
| 63777 | 63977 | ** already been allocated, allocate the UnpackedRecord structure that |
| 63778 | | -** that function will return to its caller here. Then return a pointer |
| 63978 | +** that function will return to its caller here. Then return a pointer to |
| 63779 | 63979 | ** an sqlite3_value within the UnpackedRecord.a[] array. |
| 63780 | 63980 | */ |
| 63781 | 63981 | static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ |
| 63782 | 63982 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 63783 | 63983 | if( p ){ |
| | @@ -63817,10 +64017,117 @@ |
| 63817 | 64017 | UNUSED_PARAMETER(p); |
| 63818 | 64018 | #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ |
| 63819 | 64019 | return sqlite3ValueNew(db); |
| 63820 | 64020 | } |
| 63821 | 64021 | |
| 64022 | +/* |
| 64023 | +** The expression object indicated by the second argument is guaranteed |
| 64024 | +** to be a scalar SQL function. If |
| 64025 | +** |
| 64026 | +** * all function arguments are SQL literals, |
| 64027 | +** * the SQLITE_FUNC_CONSTANT function flag is set, and |
| 64028 | +** * the SQLITE_FUNC_NEEDCOLL function flag is not set, |
| 64029 | +** |
| 64030 | +** then this routine attempts to invoke the SQL function. Assuming no |
| 64031 | +** error occurs, output parameter (*ppVal) is set to point to a value |
| 64032 | +** object containing the result before returning SQLITE_OK. |
| 64033 | +** |
| 64034 | +** Affinity aff is applied to the result of the function before returning. |
| 64035 | +** If the result is a text value, the sqlite3_value object uses encoding |
| 64036 | +** enc. |
| 64037 | +** |
| 64038 | +** If the conditions above are not met, this function returns SQLITE_OK |
| 64039 | +** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to |
| 64040 | +** NULL and an SQLite error code returned. |
| 64041 | +*/ |
| 64042 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 64043 | +static int valueFromFunction( |
| 64044 | + sqlite3 *db, /* The database connection */ |
| 64045 | + Expr *p, /* The expression to evaluate */ |
| 64046 | + u8 enc, /* Encoding to use */ |
| 64047 | + u8 aff, /* Affinity to use */ |
| 64048 | + sqlite3_value **ppVal, /* Write the new value here */ |
| 64049 | + struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ |
| 64050 | +){ |
| 64051 | + sqlite3_context ctx; /* Context object for function invocation */ |
| 64052 | + sqlite3_value **apVal = 0; /* Function arguments */ |
| 64053 | + int nVal = 0; /* Size of apVal[] array */ |
| 64054 | + FuncDef *pFunc = 0; /* Function definition */ |
| 64055 | + sqlite3_value *pVal = 0; /* New value */ |
| 64056 | + int rc = SQLITE_OK; /* Return code */ |
| 64057 | + int nName; /* Size of function name in bytes */ |
| 64058 | + ExprList *pList = 0; /* Function arguments */ |
| 64059 | + int i; /* Iterator variable */ |
| 64060 | + |
| 64061 | + assert( pCtx!=0 ); |
| 64062 | + assert( (p->flags & EP_TokenOnly)==0 ); |
| 64063 | + pList = p->x.pList; |
| 64064 | + if( pList ) nVal = pList->nExpr; |
| 64065 | + nName = sqlite3Strlen30(p->u.zToken); |
| 64066 | + pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0); |
| 64067 | + assert( pFunc ); |
| 64068 | + if( (pFunc->funcFlags & SQLITE_FUNC_CONSTANT)==0 |
| 64069 | + || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) |
| 64070 | + ){ |
| 64071 | + return SQLITE_OK; |
| 64072 | + } |
| 64073 | + |
| 64074 | + if( pList ){ |
| 64075 | + apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal); |
| 64076 | + if( apVal==0 ){ |
| 64077 | + rc = SQLITE_NOMEM; |
| 64078 | + goto value_from_function_out; |
| 64079 | + } |
| 64080 | + for(i=0; i<nVal; i++){ |
| 64081 | + rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]); |
| 64082 | + if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out; |
| 64083 | + } |
| 64084 | + } |
| 64085 | + |
| 64086 | + pVal = valueNew(db, pCtx); |
| 64087 | + if( pVal==0 ){ |
| 64088 | + rc = SQLITE_NOMEM; |
| 64089 | + goto value_from_function_out; |
| 64090 | + } |
| 64091 | + |
| 64092 | + assert( pCtx->pParse->rc==SQLITE_OK ); |
| 64093 | + memset(&ctx, 0, sizeof(ctx)); |
| 64094 | + ctx.pOut = pVal; |
| 64095 | + ctx.pFunc = pFunc; |
| 64096 | + pFunc->xFunc(&ctx, nVal, apVal); |
| 64097 | + if( ctx.isError ){ |
| 64098 | + rc = ctx.isError; |
| 64099 | + sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); |
| 64100 | + }else{ |
| 64101 | + sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); |
| 64102 | + assert( rc==SQLITE_OK ); |
| 64103 | + rc = sqlite3VdbeChangeEncoding(pVal, enc); |
| 64104 | + if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ |
| 64105 | + rc = SQLITE_TOOBIG; |
| 64106 | + pCtx->pParse->nErr++; |
| 64107 | + } |
| 64108 | + } |
| 64109 | + pCtx->pParse->rc = rc; |
| 64110 | + |
| 64111 | + value_from_function_out: |
| 64112 | + if( rc!=SQLITE_OK ){ |
| 64113 | + pVal = 0; |
| 64114 | + } |
| 64115 | + if( apVal ){ |
| 64116 | + for(i=0; i<nVal; i++){ |
| 64117 | + sqlite3ValueFree(apVal[i]); |
| 64118 | + } |
| 64119 | + sqlite3DbFree(db, apVal); |
| 64120 | + } |
| 64121 | + |
| 64122 | + *ppVal = pVal; |
| 64123 | + return rc; |
| 64124 | +} |
| 64125 | +#else |
| 64126 | +# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK |
| 64127 | +#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ |
| 64128 | + |
| 63822 | 64129 | /* |
| 63823 | 64130 | ** Extract a value from the supplied expression in the manner described |
| 63824 | 64131 | ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object |
| 63825 | 64132 | ** using valueNew(). |
| 63826 | 64133 | ** |
| | @@ -63848,10 +64155,16 @@ |
| 63848 | 64155 | *ppVal = 0; |
| 63849 | 64156 | return SQLITE_OK; |
| 63850 | 64157 | } |
| 63851 | 64158 | while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft; |
| 63852 | 64159 | if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 64160 | + |
| 64161 | + /* Compressed expressions only appear when parsing the DEFAULT clause |
| 64162 | + ** on a table column definition, and hence only when pCtx==0. This |
| 64163 | + ** check ensures that an EP_TokenOnly expression is never passed down |
| 64164 | + ** into valueFromFunction(). */ |
| 64165 | + assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 ); |
| 63853 | 64166 | |
| 63854 | 64167 | if( op==TK_CAST ){ |
| 63855 | 64168 | u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); |
| 63856 | 64169 | rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); |
| 63857 | 64170 | testcase( rc!=SQLITE_OK ); |
| | @@ -63924,10 +64237,16 @@ |
| 63924 | 64237 | assert( zVal[nVal]=='\'' ); |
| 63925 | 64238 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, |
| 63926 | 64239 | 0, SQLITE_DYNAMIC); |
| 63927 | 64240 | } |
| 63928 | 64241 | #endif |
| 64242 | + |
| 64243 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 64244 | + else if( op==TK_FUNCTION && pCtx!=0 ){ |
| 64245 | + rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); |
| 64246 | + } |
| 64247 | +#endif |
| 63929 | 64248 | |
| 63930 | 64249 | *ppVal = pVal; |
| 63931 | 64250 | return rc; |
| 63932 | 64251 | |
| 63933 | 64252 | no_mem: |
| | @@ -65377,11 +65696,11 @@ |
| 65377 | 65696 | break; |
| 65378 | 65697 | } |
| 65379 | 65698 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 65380 | 65699 | case P4_VTAB: { |
| 65381 | 65700 | sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
| 65382 | | - sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
| 65701 | + sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab); |
| 65383 | 65702 | break; |
| 65384 | 65703 | } |
| 65385 | 65704 | #endif |
| 65386 | 65705 | case P4_INTARRAY: { |
| 65387 | 65706 | sqlite3_snprintf(nTemp, zTemp, "intarray"); |
| | @@ -66041,13 +66360,13 @@ |
| 66041 | 66360 | } |
| 66042 | 66361 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 66043 | 66362 | else if( pCx->pVtabCursor ){ |
| 66044 | 66363 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
| 66045 | 66364 | const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
| 66046 | | - p->inVtabMethod = 1; |
| 66365 | + assert( pVtabCursor->pVtab->nRef>0 ); |
| 66366 | + pVtabCursor->pVtab->nRef--; |
| 66047 | 66367 | pModule->xClose(pVtabCursor); |
| 66048 | | - p->inVtabMethod = 0; |
| 66049 | 66368 | } |
| 66050 | 66369 | #endif |
| 66051 | 66370 | } |
| 66052 | 66371 | |
| 66053 | 66372 | /* |
| | @@ -66402,11 +66721,11 @@ |
| 66402 | 66721 | |
| 66403 | 66722 | /* Delete the master journal file. This commits the transaction. After |
| 66404 | 66723 | ** doing this the directory is synced again before any individual |
| 66405 | 66724 | ** transaction files are deleted. |
| 66406 | 66725 | */ |
| 66407 | | - rc = sqlite3OsDelete(pVfs, zMaster, 1); |
| 66726 | + rc = sqlite3OsDelete(pVfs, zMaster, needSync); |
| 66408 | 66727 | sqlite3DbFree(db, zMaster); |
| 66409 | 66728 | zMaster = 0; |
| 66410 | 66729 | if( rc ){ |
| 66411 | 66730 | return rc; |
| 66412 | 66731 | } |
| | @@ -68615,10 +68934,14 @@ |
| 68615 | 68934 | } |
| 68616 | 68935 | SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){ |
| 68617 | 68936 | return sqlite3ValueText(pVal, SQLITE_UTF16LE); |
| 68618 | 68937 | } |
| 68619 | 68938 | #endif /* SQLITE_OMIT_UTF16 */ |
| 68939 | +/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five |
| 68940 | +** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating |
| 68941 | +** point number string BLOB NULL |
| 68942 | +*/ |
| 68620 | 68943 | SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){ |
| 68621 | 68944 | static const u8 aType[] = { |
| 68622 | 68945 | SQLITE_BLOB, /* 0x00 */ |
| 68623 | 68946 | SQLITE_NULL, /* 0x01 */ |
| 68624 | 68947 | SQLITE_TEXT, /* 0x02 */ |
| | @@ -68811,11 +69134,11 @@ |
| 68811 | 69134 | } |
| 68812 | 69135 | SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 68813 | 69136 | pCtx->isError = errCode; |
| 68814 | 69137 | pCtx->fErrorOrAux = 1; |
| 68815 | 69138 | #ifdef SQLITE_DEBUG |
| 68816 | | - pCtx->pVdbe->rcApp = errCode; |
| 69139 | + if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
| 68817 | 69140 | #endif |
| 68818 | 69141 | if( pCtx->pOut->flags & MEM_Null ){ |
| 68819 | 69142 | sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, |
| 68820 | 69143 | SQLITE_UTF8, SQLITE_STATIC); |
| 68821 | 69144 | } |
| | @@ -69074,20 +69397,30 @@ |
| 69074 | 69397 | assert( p && p->pFunc ); |
| 69075 | 69398 | return p->pOut->db; |
| 69076 | 69399 | } |
| 69077 | 69400 | |
| 69078 | 69401 | /* |
| 69079 | | -** Return the current time for a statement |
| 69402 | +** Return the current time for a statement. If the current time |
| 69403 | +** is requested more than once within the same run of a single prepared |
| 69404 | +** statement, the exact same time is returned for each invocation regardless |
| 69405 | +** of the amount of time that elapses between invocations. In other words, |
| 69406 | +** the time returned is always the time of the first call. |
| 69080 | 69407 | */ |
| 69081 | 69408 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ |
| 69082 | | - Vdbe *v = p->pVdbe; |
| 69083 | 69409 | int rc; |
| 69084 | | - if( v->iCurrentTime==0 ){ |
| 69085 | | - rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime); |
| 69086 | | - if( rc ) v->iCurrentTime = 0; |
| 69410 | +#ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 69411 | + sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; |
| 69412 | + assert( p->pVdbe!=0 ); |
| 69413 | +#else |
| 69414 | + sqlite3_int64 iTime = 0; |
| 69415 | + sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; |
| 69416 | +#endif |
| 69417 | + if( *piTime==0 ){ |
| 69418 | + rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); |
| 69419 | + if( rc ) *piTime = 0; |
| 69087 | 69420 | } |
| 69088 | | - return v->iCurrentTime; |
| 69421 | + return *piTime; |
| 69089 | 69422 | } |
| 69090 | 69423 | |
| 69091 | 69424 | /* |
| 69092 | 69425 | ** The following is the implementation of an SQL function that always |
| 69093 | 69426 | ** fails with an error message stating that the function is used in the |
| | @@ -69153,10 +69486,15 @@ |
| 69153 | 69486 | */ |
| 69154 | 69487 | SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
| 69155 | 69488 | AuxData *pAuxData; |
| 69156 | 69489 | |
| 69157 | 69490 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 69491 | +#if SQLITE_ENABLE_STAT3_OR_STAT4 |
| 69492 | + if( pCtx->pVdbe==0 ) return 0; |
| 69493 | +#else |
| 69494 | + assert( pCtx->pVdbe!=0 ); |
| 69495 | +#endif |
| 69158 | 69496 | for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 69159 | 69497 | if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 69160 | 69498 | } |
| 69161 | 69499 | |
| 69162 | 69500 | return (pAuxData ? pAuxData->pAux : 0); |
| | @@ -69176,10 +69514,15 @@ |
| 69176 | 69514 | AuxData *pAuxData; |
| 69177 | 69515 | Vdbe *pVdbe = pCtx->pVdbe; |
| 69178 | 69516 | |
| 69179 | 69517 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 69180 | 69518 | if( iArg<0 ) goto failed; |
| 69519 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 69520 | + if( pVdbe==0 ) goto failed; |
| 69521 | +#else |
| 69522 | + assert( pVdbe!=0 ); |
| 69523 | +#endif |
| 69181 | 69524 | |
| 69182 | 69525 | for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 69183 | 69526 | if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 69184 | 69527 | } |
| 69185 | 69528 | if( pAuxData==0 ){ |
| | @@ -71290,11 +71633,11 @@ |
| 71290 | 71633 | |
| 71291 | 71634 | /* Opcode: String8 * P2 * P4 * |
| 71292 | 71635 | ** Synopsis: r[P2]='P4' |
| 71293 | 71636 | ** |
| 71294 | 71637 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
| 71295 | | -** into a String before it is executed for the first time. During |
| 71638 | +** into a String opcode before it is executed for the first time. During |
| 71296 | 71639 | ** this transformation, the length of string P4 is computed and stored |
| 71297 | 71640 | ** as the P1 parameter. |
| 71298 | 71641 | */ |
| 71299 | 71642 | case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
| 71300 | 71643 | assert( pOp->p4.z!=0 ); |
| | @@ -71322,22 +71665,34 @@ |
| 71322 | 71665 | goto too_big; |
| 71323 | 71666 | } |
| 71324 | 71667 | /* Fall through to the next case, OP_String */ |
| 71325 | 71668 | } |
| 71326 | 71669 | |
| 71327 | | -/* Opcode: String P1 P2 * P4 * |
| 71670 | +/* Opcode: String P1 P2 P3 P4 P5 |
| 71328 | 71671 | ** Synopsis: r[P2]='P4' (len=P1) |
| 71329 | 71672 | ** |
| 71330 | 71673 | ** The string value P4 of length P1 (bytes) is stored in register P2. |
| 71674 | +** |
| 71675 | +** If P5!=0 and the content of register P3 is greater than zero, then |
| 71676 | +** the datatype of the register P2 is converted to BLOB. The content is |
| 71677 | +** the same sequence of bytes, it is merely interpreted as a BLOB instead |
| 71678 | +** of a string, as if it had been CAST. |
| 71331 | 71679 | */ |
| 71332 | 71680 | case OP_String: { /* out2-prerelease */ |
| 71333 | 71681 | assert( pOp->p4.z!=0 ); |
| 71334 | 71682 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 71335 | 71683 | pOut->z = pOp->p4.z; |
| 71336 | 71684 | pOut->n = pOp->p1; |
| 71337 | 71685 | pOut->enc = encoding; |
| 71338 | 71686 | UPDATE_MAX_BLOBSIZE(pOut); |
| 71687 | + if( pOp->p5 ){ |
| 71688 | + assert( pOp->p3>0 ); |
| 71689 | + assert( pOp->p3<=(p->nMem-p->nCursor) ); |
| 71690 | + pIn3 = &aMem[pOp->p3]; |
| 71691 | + assert( pIn3->flags & MEM_Int ); |
| 71692 | + if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term; |
| 71693 | + } |
| 71339 | 71694 | break; |
| 71340 | 71695 | } |
| 71341 | 71696 | |
| 71342 | 71697 | /* Opcode: Null P1 P2 P3 * * |
| 71343 | 71698 | ** Synopsis: r[P2..P3]=NULL |
| | @@ -71780,11 +72135,11 @@ |
| 71780 | 72135 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 71781 | 72136 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 71782 | 72137 | ** |
| 71783 | 72138 | ** The interface used by the implementation of the aforementioned functions |
| 71784 | 72139 | ** to retrieve the collation sequence set by this opcode is not available |
| 71785 | | -** publicly, only to user functions defined in func.c. |
| 72140 | +** publicly. Only built-in functions have access to this feature. |
| 71786 | 72141 | */ |
| 71787 | 72142 | case OP_CollSeq: { |
| 71788 | 72143 | assert( pOp->p4type==P4_COLLSEQ ); |
| 71789 | 72144 | if( pOp->p1 ){ |
| 71790 | 72145 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| | @@ -73325,11 +73680,16 @@ |
| 73325 | 73680 | ** the value of this counter needs to be restored too. */ |
| 73326 | 73681 | p->nStmtDefCons = db->nDeferredCons; |
| 73327 | 73682 | p->nStmtDefImmCons = db->nDeferredImmCons; |
| 73328 | 73683 | } |
| 73329 | 73684 | |
| 73330 | | - /* Gather the schema version number for checking */ |
| 73685 | + /* Gather the schema version number for checking: |
| 73686 | + ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite |
| 73687 | + ** each time a query is executed to ensure that the internal cache of the |
| 73688 | + ** schema used when compiling the SQL query matches the schema of the |
| 73689 | + ** database against which the compiled query is actually executed. |
| 73690 | + */ |
| 73331 | 73691 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); |
| 73332 | 73692 | iGen = db->aDb[pOp->p1].pSchema->iGeneration; |
| 73333 | 73693 | }else{ |
| 73334 | 73694 | iGen = iMeta = 0; |
| 73335 | 73695 | } |
| | @@ -73493,35 +73853,33 @@ |
| 73493 | 73853 | ** cursors or a single read/write cursor but not both. |
| 73494 | 73854 | ** |
| 73495 | 73855 | ** See also OpenRead. |
| 73496 | 73856 | */ |
| 73497 | 73857 | case OP_ReopenIdx: { |
| 73498 | | - VdbeCursor *pCur; |
| 73499 | | - |
| 73500 | | - assert( pOp->p5==0 ); |
| 73501 | | - assert( pOp->p4type==P4_KEYINFO ); |
| 73502 | | - pCur = p->apCsr[pOp->p1]; |
| 73503 | | - if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
| 73504 | | - assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
| 73505 | | - break; |
| 73506 | | - } |
| 73507 | | - /* If the cursor is not currently open or is open on a different |
| 73508 | | - ** index, then fall through into OP_OpenRead to force a reopen */ |
| 73509 | | -} |
| 73510 | | -case OP_OpenRead: |
| 73511 | | -case OP_OpenWrite: { |
| 73512 | 73858 | int nField; |
| 73513 | 73859 | KeyInfo *pKeyInfo; |
| 73514 | 73860 | int p2; |
| 73515 | 73861 | int iDb; |
| 73516 | 73862 | int wrFlag; |
| 73517 | 73863 | Btree *pX; |
| 73518 | 73864 | VdbeCursor *pCur; |
| 73519 | 73865 | Db *pDb; |
| 73520 | 73866 | |
| 73521 | | - assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 ); |
| 73522 | | - assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 ); |
| 73867 | + assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
| 73868 | + assert( pOp->p4type==P4_KEYINFO ); |
| 73869 | + pCur = p->apCsr[pOp->p1]; |
| 73870 | + if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
| 73871 | + assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
| 73872 | + goto open_cursor_set_hints; |
| 73873 | + } |
| 73874 | + /* If the cursor is not currently open or is open on a different |
| 73875 | + ** index, then fall through into OP_OpenRead to force a reopen */ |
| 73876 | +case OP_OpenRead: |
| 73877 | +case OP_OpenWrite: |
| 73878 | + |
| 73879 | + assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR|OPFLAG_SEEKEQ))==pOp->p5 ); |
| 73880 | + assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ ); |
| 73523 | 73881 | assert( p->bIsReader ); |
| 73524 | 73882 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 73525 | 73883 | || p->readOnly==0 ); |
| 73526 | 73884 | |
| 73527 | 73885 | if( p->expired ){ |
| | @@ -73580,18 +73938,21 @@ |
| 73580 | 73938 | pCur->nullRow = 1; |
| 73581 | 73939 | pCur->isOrdered = 1; |
| 73582 | 73940 | pCur->pgnoRoot = p2; |
| 73583 | 73941 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 73584 | 73942 | pCur->pKeyInfo = pKeyInfo; |
| 73585 | | - assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 73586 | | - sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR)); |
| 73587 | | - |
| 73588 | 73943 | /* Set the VdbeCursor.isTable variable. Previous versions of |
| 73589 | 73944 | ** SQLite used to check if the root-page flags were sane at this point |
| 73590 | 73945 | ** and report database corruption if they were not, but this check has |
| 73591 | 73946 | ** since moved into the btree layer. */ |
| 73592 | 73947 | pCur->isTable = pOp->p4type!=P4_KEYINFO; |
| 73948 | + |
| 73949 | +open_cursor_set_hints: |
| 73950 | + assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 73951 | + assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ ); |
| 73952 | + sqlite3BtreeCursorHints(pCur->pCursor, |
| 73953 | + (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ))); |
| 73593 | 73954 | break; |
| 73594 | 73955 | } |
| 73595 | 73956 | |
| 73596 | 73957 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
| 73597 | 73958 | ** Synopsis: nColumn=P2 |
| | @@ -73848,10 +74209,26 @@ |
| 73848 | 74209 | oc = pOp->opcode; |
| 73849 | 74210 | pC->nullRow = 0; |
| 73850 | 74211 | #ifdef SQLITE_DEBUG |
| 73851 | 74212 | pC->seekOp = pOp->opcode; |
| 73852 | 74213 | #endif |
| 74214 | + |
| 74215 | + /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and |
| 74216 | + ** OP_SeekLE opcodes are allowed, and these must be immediately followed |
| 74217 | + ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key. |
| 74218 | + */ |
| 74219 | +#ifdef SQLITE_DEBUG |
| 74220 | + if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){ |
| 74221 | + assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE ); |
| 74222 | + assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT ); |
| 74223 | + assert( pOp[1].p1==pOp[0].p1 ); |
| 74224 | + assert( pOp[1].p2==pOp[0].p2 ); |
| 74225 | + assert( pOp[1].p3==pOp[0].p3 ); |
| 74226 | + assert( pOp[1].p4.i==pOp[0].p4.i ); |
| 74227 | + } |
| 74228 | +#endif |
| 74229 | + |
| 73853 | 74230 | if( pC->isTable ){ |
| 73854 | 74231 | /* The input value in P3 might be of any type: integer, real, string, |
| 73855 | 74232 | ** blob, or NULL. But it needs to be an integer before we can do |
| 73856 | 74233 | ** the seek, so convert it. */ |
| 73857 | 74234 | pIn3 = &aMem[pOp->p3]; |
| | @@ -75187,34 +75564,19 @@ |
| 75187 | 75564 | ** |
| 75188 | 75565 | ** See also: Clear |
| 75189 | 75566 | */ |
| 75190 | 75567 | case OP_Destroy: { /* out2-prerelease */ |
| 75191 | 75568 | int iMoved; |
| 75192 | | - int iCnt; |
| 75193 | | - Vdbe *pVdbe; |
| 75194 | 75569 | int iDb; |
| 75195 | 75570 | |
| 75196 | 75571 | assert( p->readOnly==0 ); |
| 75197 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 75198 | | - iCnt = 0; |
| 75199 | | - for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ |
| 75200 | | - if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader |
| 75201 | | - && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 |
| 75202 | | - ){ |
| 75203 | | - iCnt++; |
| 75204 | | - } |
| 75205 | | - } |
| 75206 | | -#else |
| 75207 | | - iCnt = db->nVdbeRead; |
| 75208 | | -#endif |
| 75209 | 75572 | pOut->flags = MEM_Null; |
| 75210 | | - if( iCnt>1 ){ |
| 75573 | + if( db->nVdbeRead > db->nVDestroy+1 ){ |
| 75211 | 75574 | rc = SQLITE_LOCKED; |
| 75212 | 75575 | p->errorAction = OE_Abort; |
| 75213 | 75576 | }else{ |
| 75214 | 75577 | iDb = pOp->p3; |
| 75215 | | - assert( iCnt==1 ); |
| 75216 | 75578 | assert( DbMaskTest(p->btreeMask, iDb) ); |
| 75217 | 75579 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
| 75218 | 75580 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
| 75219 | 75581 | pOut->flags = MEM_Int; |
| 75220 | 75582 | pOut->u.i = iMoved; |
| | @@ -75843,14 +76205,16 @@ |
| 75843 | 76205 | #endif /* SQLITE_OMIT_AUTOINCREMENT */ |
| 75844 | 76206 | |
| 75845 | 76207 | /* Opcode: IfPos P1 P2 * * * |
| 75846 | 76208 | ** Synopsis: if r[P1]>0 goto P2 |
| 75847 | 76209 | ** |
| 75848 | | -** If the value of register P1 is 1 or greater, jump to P2. |
| 76210 | +** Register P1 must contain an integer. |
| 76211 | +** If the value of register P1 is 1 or greater, jump to P2 and |
| 76212 | +** add the literal value P3 to register P1. |
| 75849 | 76213 | ** |
| 75850 | | -** It is illegal to use this instruction on a register that does |
| 75851 | | -** not contain an integer. An assertion fault will result if you try. |
| 76214 | +** If the initial value of register P1 is less than 1, then the |
| 76215 | +** value is unchanged and control passes through to the next instruction. |
| 75852 | 76216 | */ |
| 75853 | 76217 | case OP_IfPos: { /* jump, in1 */ |
| 75854 | 76218 | pIn1 = &aMem[pOp->p1]; |
| 75855 | 76219 | assert( pIn1->flags&MEM_Int ); |
| 75856 | 76220 | VdbeBranchTaken( pIn1->u.i>0, 2); |
| | @@ -75875,27 +76239,63 @@ |
| 75875 | 76239 | pc = pOp->p2 - 1; |
| 75876 | 76240 | } |
| 75877 | 76241 | break; |
| 75878 | 76242 | } |
| 75879 | 76243 | |
| 75880 | | -/* Opcode: IfZero P1 P2 P3 * * |
| 75881 | | -** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2 |
| 76244 | +/* Opcode: IfNotZero P1 P2 P3 * * |
| 76245 | +** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 |
| 75882 | 76246 | ** |
| 75883 | | -** The register P1 must contain an integer. Add literal P3 to the |
| 75884 | | -** value in register P1. If the result is exactly 0, jump to P2. |
| 76247 | +** Register P1 must contain an integer. If the content of register P1 is |
| 76248 | +** initially nonzero, then add P3 to P1 and jump to P2. If register P1 is |
| 76249 | +** initially zero, leave it unchanged and fall through. |
| 75885 | 76250 | */ |
| 75886 | | -case OP_IfZero: { /* jump, in1 */ |
| 76251 | +case OP_IfNotZero: { /* jump, in1 */ |
| 76252 | + pIn1 = &aMem[pOp->p1]; |
| 76253 | + assert( pIn1->flags&MEM_Int ); |
| 76254 | + VdbeBranchTaken(pIn1->u.i<0, 2); |
| 76255 | + if( pIn1->u.i ){ |
| 76256 | + pIn1->u.i += pOp->p3; |
| 76257 | + pc = pOp->p2 - 1; |
| 76258 | + } |
| 76259 | + break; |
| 76260 | +} |
| 76261 | + |
| 76262 | +/* Opcode: DecrJumpZero P1 P2 * * * |
| 76263 | +** Synopsis: if (--r[P1])==0 goto P2 |
| 76264 | +** |
| 76265 | +** Register P1 must hold an integer. Decrement the value in register P1 |
| 76266 | +** then jump to P2 if the new value is exactly zero. |
| 76267 | +*/ |
| 76268 | +case OP_DecrJumpZero: { /* jump, in1 */ |
| 75887 | 76269 | pIn1 = &aMem[pOp->p1]; |
| 75888 | 76270 | assert( pIn1->flags&MEM_Int ); |
| 75889 | | - pIn1->u.i += pOp->p3; |
| 76271 | + pIn1->u.i--; |
| 75890 | 76272 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 75891 | 76273 | if( pIn1->u.i==0 ){ |
| 75892 | 76274 | pc = pOp->p2 - 1; |
| 75893 | 76275 | } |
| 75894 | 76276 | break; |
| 75895 | 76277 | } |
| 75896 | 76278 | |
| 76279 | + |
| 76280 | +/* Opcode: JumpZeroIncr P1 P2 * * * |
| 76281 | +** Synopsis: if (r[P1]++)==0 ) goto P2 |
| 76282 | +** |
| 76283 | +** The register P1 must contain an integer. If register P1 is initially |
| 76284 | +** zero, then jump to P2. Increment register P1 regardless of whether or |
| 76285 | +** not the jump is taken. |
| 76286 | +*/ |
| 76287 | +case OP_JumpZeroIncr: { /* jump, in1 */ |
| 76288 | + pIn1 = &aMem[pOp->p1]; |
| 76289 | + assert( pIn1->flags&MEM_Int ); |
| 76290 | + VdbeBranchTaken(pIn1->u.i==0, 2); |
| 76291 | + if( (pIn1->u.i++)==0 ){ |
| 76292 | + pc = pOp->p2 - 1; |
| 76293 | + } |
| 76294 | + break; |
| 76295 | +} |
| 76296 | + |
| 75897 | 76297 | /* Opcode: AggStep * P2 P3 P4 P5 |
| 75898 | 76298 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| 75899 | 76299 | ** |
| 75900 | 76300 | ** Execute the step function for an aggregate. The |
| 75901 | 76301 | ** function has P5 arguments. P4 is a pointer to the FuncDef |
| | @@ -76229,17 +76629,33 @@ |
| 76229 | 76629 | break; |
| 76230 | 76630 | } |
| 76231 | 76631 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76232 | 76632 | |
| 76233 | 76633 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 76234 | | -/* Opcode: VCreate P1 * * P4 * |
| 76634 | +/* Opcode: VCreate P1 P2 * * * |
| 76235 | 76635 | ** |
| 76236 | | -** P4 is the name of a virtual table in database P1. Call the xCreate method |
| 76237 | | -** for that table. |
| 76636 | +** P2 is a register that holds the name of a virtual table in database |
| 76637 | +** P1. Call the xCreate method for that table. |
| 76238 | 76638 | */ |
| 76239 | 76639 | case OP_VCreate: { |
| 76240 | | - rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); |
| 76640 | + Mem sMem; /* For storing the record being decoded */ |
| 76641 | + const char *zTab; /* Name of the virtual table */ |
| 76642 | + |
| 76643 | + memset(&sMem, 0, sizeof(sMem)); |
| 76644 | + sMem.db = db; |
| 76645 | + /* Because P2 is always a static string, it is impossible for the |
| 76646 | + ** sqlite3VdbeMemCopy() to fail */ |
| 76647 | + assert( (aMem[pOp->p2].flags & MEM_Str)!=0 ); |
| 76648 | + assert( (aMem[pOp->p2].flags & MEM_Static)!=0 ); |
| 76649 | + rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]); |
| 76650 | + assert( rc==SQLITE_OK ); |
| 76651 | + zTab = (const char*)sqlite3_value_text(&sMem); |
| 76652 | + assert( zTab || db->mallocFailed ); |
| 76653 | + if( zTab ){ |
| 76654 | + rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg); |
| 76655 | + } |
| 76656 | + sqlite3VdbeMemRelease(&sMem); |
| 76241 | 76657 | break; |
| 76242 | 76658 | } |
| 76243 | 76659 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76244 | 76660 | |
| 76245 | 76661 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -76247,13 +76663,13 @@ |
| 76247 | 76663 | ** |
| 76248 | 76664 | ** P4 is the name of a virtual table in database P1. Call the xDestroy method |
| 76249 | 76665 | ** of that table. |
| 76250 | 76666 | */ |
| 76251 | 76667 | case OP_VDestroy: { |
| 76252 | | - p->inVtabMethod = 2; |
| 76668 | + db->nVDestroy++; |
| 76253 | 76669 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
| 76254 | | - p->inVtabMethod = 0; |
| 76670 | + db->nVDestroy--; |
| 76255 | 76671 | break; |
| 76256 | 76672 | } |
| 76257 | 76673 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76258 | 76674 | |
| 76259 | 76675 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -76265,18 +76681,21 @@ |
| 76265 | 76681 | */ |
| 76266 | 76682 | case OP_VOpen: { |
| 76267 | 76683 | VdbeCursor *pCur; |
| 76268 | 76684 | sqlite3_vtab_cursor *pVtabCursor; |
| 76269 | 76685 | sqlite3_vtab *pVtab; |
| 76270 | | - sqlite3_module *pModule; |
| 76686 | + const sqlite3_module *pModule; |
| 76271 | 76687 | |
| 76272 | 76688 | assert( p->bIsReader ); |
| 76273 | 76689 | pCur = 0; |
| 76274 | 76690 | pVtabCursor = 0; |
| 76275 | 76691 | pVtab = pOp->p4.pVtab->pVtab; |
| 76276 | | - pModule = (sqlite3_module *)pVtab->pModule; |
| 76277 | | - assert(pVtab && pModule); |
| 76692 | + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 76693 | + rc = SQLITE_LOCKED; |
| 76694 | + break; |
| 76695 | + } |
| 76696 | + pModule = pVtab->pModule; |
| 76278 | 76697 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
| 76279 | 76698 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76280 | 76699 | if( SQLITE_OK==rc ){ |
| 76281 | 76700 | /* Initialize sqlite3_vtab_cursor base class */ |
| 76282 | 76701 | pVtabCursor->pVtab = pVtab; |
| | @@ -76283,10 +76702,11 @@ |
| 76283 | 76702 | |
| 76284 | 76703 | /* Initialize vdbe cursor object */ |
| 76285 | 76704 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
| 76286 | 76705 | if( pCur ){ |
| 76287 | 76706 | pCur->pVtabCursor = pVtabCursor; |
| 76707 | + pVtab->nRef++; |
| 76288 | 76708 | }else{ |
| 76289 | 76709 | db->mallocFailed = 1; |
| 76290 | 76710 | pModule->xClose(pVtabCursor); |
| 76291 | 76711 | } |
| 76292 | 76712 | } |
| | @@ -76348,13 +76768,11 @@ |
| 76348 | 76768 | apArg = p->apArg; |
| 76349 | 76769 | for(i = 0; i<nArg; i++){ |
| 76350 | 76770 | apArg[i] = &pArgc[i+1]; |
| 76351 | 76771 | } |
| 76352 | 76772 | |
| 76353 | | - p->inVtabMethod = 1; |
| 76354 | 76773 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
| 76355 | | - p->inVtabMethod = 0; |
| 76356 | 76774 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76357 | 76775 | if( rc==SQLITE_OK ){ |
| 76358 | 76776 | res = pModule->xEof(pVtabCursor); |
| 76359 | 76777 | } |
| 76360 | 76778 | VdbeBranchTaken(res!=0,2); |
| | @@ -76440,13 +76858,11 @@ |
| 76440 | 76858 | ** underlying implementation to return an error if one occurs during |
| 76441 | 76859 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 76442 | 76860 | ** data is available) and the error code returned when xColumn or |
| 76443 | 76861 | ** some other method is next invoked on the save virtual table cursor. |
| 76444 | 76862 | */ |
| 76445 | | - p->inVtabMethod = 1; |
| 76446 | 76863 | rc = pModule->xNext(pCur->pVtabCursor); |
| 76447 | | - p->inVtabMethod = 0; |
| 76448 | 76864 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76449 | 76865 | if( rc==SQLITE_OK ){ |
| 76450 | 76866 | res = pModule->xEof(pCur->pVtabCursor); |
| 76451 | 76867 | } |
| 76452 | 76868 | VdbeBranchTaken(!res,2); |
| | @@ -76517,11 +76933,11 @@ |
| 76517 | 76933 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 76518 | 76934 | ** apply in the case of a constraint failure on an insert or update. |
| 76519 | 76935 | */ |
| 76520 | 76936 | case OP_VUpdate: { |
| 76521 | 76937 | sqlite3_vtab *pVtab; |
| 76522 | | - sqlite3_module *pModule; |
| 76938 | + const sqlite3_module *pModule; |
| 76523 | 76939 | int nArg; |
| 76524 | 76940 | int i; |
| 76525 | 76941 | sqlite_int64 rowid; |
| 76526 | 76942 | Mem **apArg; |
| 76527 | 76943 | Mem *pX; |
| | @@ -76529,11 +76945,15 @@ |
| 76529 | 76945 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 76530 | 76946 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 76531 | 76947 | ); |
| 76532 | 76948 | assert( p->readOnly==0 ); |
| 76533 | 76949 | pVtab = pOp->p4.pVtab->pVtab; |
| 76534 | | - pModule = (sqlite3_module *)pVtab->pModule; |
| 76950 | + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 76951 | + rc = SQLITE_LOCKED; |
| 76952 | + break; |
| 76953 | + } |
| 76954 | + pModule = pVtab->pModule; |
| 76535 | 76955 | nArg = pOp->p2; |
| 76536 | 76956 | assert( pOp->p4type==P4_VTAB ); |
| 76537 | 76957 | if( ALWAYS(pModule->xUpdate) ){ |
| 76538 | 76958 | u8 vtabOnConflict = db->vtabOnConflict; |
| 76539 | 76959 | apArg = p->apArg; |
| | @@ -78399,10 +78819,11 @@ |
| 78399 | 78819 | sqlite3 *db, /* Database handle doing sort */ |
| 78400 | 78820 | i64 nExtend, /* Attempt to extend file to this size */ |
| 78401 | 78821 | sqlite3_file **ppFd |
| 78402 | 78822 | ){ |
| 78403 | 78823 | int rc; |
| 78824 | + if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS; |
| 78404 | 78825 | rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd, |
| 78405 | 78826 | SQLITE_OPEN_TEMP_JOURNAL | |
| 78406 | 78827 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 78407 | 78828 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc |
| 78408 | 78829 | ); |
| | @@ -81990,14 +82411,15 @@ |
| 81990 | 82411 | ** and the pExpr parameter is returned unchanged. |
| 81991 | 82412 | */ |
| 81992 | 82413 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken( |
| 81993 | 82414 | Parse *pParse, /* Parsing context */ |
| 81994 | 82415 | Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
| 81995 | | - const Token *pCollName /* Name of collating sequence */ |
| 82416 | + const Token *pCollName, /* Name of collating sequence */ |
| 82417 | + int dequote /* True to dequote pCollName */ |
| 81996 | 82418 | ){ |
| 81997 | 82419 | if( pCollName->n>0 ){ |
| 81998 | | - Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); |
| 82420 | + Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); |
| 81999 | 82421 | if( pNew ){ |
| 82000 | 82422 | pNew->pLeft = pExpr; |
| 82001 | 82423 | pNew->flags |= EP_Collate|EP_Skip; |
| 82002 | 82424 | pExpr = pNew; |
| 82003 | 82425 | } |
| | @@ -82007,11 +82429,11 @@ |
| 82007 | 82429 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ |
| 82008 | 82430 | Token s; |
| 82009 | 82431 | assert( zC!=0 ); |
| 82010 | 82432 | s.z = zC; |
| 82011 | 82433 | s.n = sqlite3Strlen30(s.z); |
| 82012 | | - return sqlite3ExprAddCollateToken(pParse, pExpr, &s); |
| 82434 | + return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); |
| 82013 | 82435 | } |
| 82014 | 82436 | |
| 82015 | 82437 | /* |
| 82016 | 82438 | ** Skip over any TK_COLLATE or TK_AS operators and any unlikely() |
| 82017 | 82439 | ** or likelihood() function at the root of an expression. |
| | @@ -82317,10 +82739,11 @@ |
| 82317 | 82739 | ** |
| 82318 | 82740 | ** Also propagate all EP_Propagate flags from the Expr.x.pList into |
| 82319 | 82741 | ** Expr.flags. |
| 82320 | 82742 | */ |
| 82321 | 82743 | SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
| 82744 | + if( pParse->nErr ) return; |
| 82322 | 82745 | exprSetHeight(p); |
| 82323 | 82746 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
| 82324 | 82747 | } |
| 82325 | 82748 | |
| 82326 | 82749 | /* |
| | @@ -87031,11 +87454,14 @@ |
| 87031 | 87454 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 87032 | 87455 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 87033 | 87456 | */ |
| 87034 | 87457 | if( pDflt ){ |
| 87035 | 87458 | sqlite3_value *pVal = 0; |
| 87036 | | - if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ |
| 87459 | + int rc; |
| 87460 | + rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal); |
| 87461 | + assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| 87462 | + if( rc!=SQLITE_OK ){ |
| 87037 | 87463 | db->mallocFailed = 1; |
| 87038 | 87464 | return; |
| 87039 | 87465 | } |
| 87040 | 87466 | if( !pVal ){ |
| 87041 | 87467 | sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
| | @@ -93094,10 +93520,11 @@ |
| 93094 | 93520 | } |
| 93095 | 93521 | if( pIdx->onError==OE_Default ){ |
| 93096 | 93522 | pIdx->onError = pIndex->onError; |
| 93097 | 93523 | } |
| 93098 | 93524 | } |
| 93525 | + pRet = pIdx; |
| 93099 | 93526 | goto exit_create_index; |
| 93100 | 93527 | } |
| 93101 | 93528 | } |
| 93102 | 93529 | } |
| 93103 | 93530 | |
| | @@ -95553,11 +95980,13 @@ |
| 95553 | 95980 | |
| 95554 | 95981 | /* |
| 95555 | 95982 | ** Return the collating function associated with a function. |
| 95556 | 95983 | */ |
| 95557 | 95984 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 95558 | | - VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 95985 | + VdbeOp *pOp; |
| 95986 | + assert( context->pVdbe!=0 ); |
| 95987 | + pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 95559 | 95988 | assert( pOp->opcode==OP_CollSeq ); |
| 95560 | 95989 | assert( pOp->p4type==P4_COLLSEQ ); |
| 95561 | 95990 | return pOp->p4.pColl; |
| 95562 | 95991 | } |
| 95563 | 95992 | |
| | @@ -97181,10 +97610,15 @@ |
| 97181 | 97610 | ** pExpr points to an expression which implements a function. If |
| 97182 | 97611 | ** it is appropriate to apply the LIKE optimization to that function |
| 97183 | 97612 | ** then set aWc[0] through aWc[2] to the wildcard characters and |
| 97184 | 97613 | ** return TRUE. If the function is not a LIKE-style function then |
| 97185 | 97614 | ** return FALSE. |
| 97615 | +** |
| 97616 | +** *pIsNocase is set to true if uppercase and lowercase are equivalent for |
| 97617 | +** the function (default for LIKE). If the function makes the distinction |
| 97618 | +** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to |
| 97619 | +** false. |
| 97186 | 97620 | */ |
| 97187 | 97621 | SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ |
| 97188 | 97622 | FuncDef *pDef; |
| 97189 | 97623 | if( pExpr->op!=TK_FUNCTION |
| 97190 | 97624 | || !pExpr->x.pList |
| | @@ -102972,10 +103406,21 @@ |
| 102972 | 103406 | } |
| 102973 | 103407 | |
| 102974 | 103408 | /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS |
| 102975 | 103409 | ** connection. If it returns SQLITE_OK, then assume that the VFS |
| 102976 | 103410 | ** handled the pragma and generate a no-op prepared statement. |
| 103411 | + ** |
| 103412 | + ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, |
| 103413 | + ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file |
| 103414 | + ** object corresponding to the database file to which the pragma |
| 103415 | + ** statement refers. |
| 103416 | + ** |
| 103417 | + ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA |
| 103418 | + ** file control is an array of pointers to strings (char**) in which the |
| 103419 | + ** second element of the array is the name of the pragma and the third |
| 103420 | + ** element is the argument to the pragma or NULL if the pragma has no |
| 103421 | + ** argument. |
| 102977 | 103422 | */ |
| 102978 | 103423 | aFcntl[0] = 0; |
| 102979 | 103424 | aFcntl[1] = zLeft; |
| 102980 | 103425 | aFcntl[2] = zRight; |
| 102981 | 103426 | aFcntl[3] = 0; |
| | @@ -103732,34 +104177,46 @@ |
| 103732 | 104177 | Index *pIdx; |
| 103733 | 104178 | Table *pTab; |
| 103734 | 104179 | pIdx = sqlite3FindIndex(db, zRight, zDb); |
| 103735 | 104180 | if( pIdx ){ |
| 103736 | 104181 | int i; |
| 103737 | | - int mx = pPragma->iArg ? pIdx->nColumn : pIdx->nKeyCol; |
| 104182 | + int mx; |
| 104183 | + if( pPragma->iArg ){ |
| 104184 | + /* PRAGMA index_xinfo (newer version with more rows and columns) */ |
| 104185 | + mx = pIdx->nColumn; |
| 104186 | + pParse->nMem = 6; |
| 104187 | + }else{ |
| 104188 | + /* PRAGMA index_info (legacy version) */ |
| 104189 | + mx = pIdx->nKeyCol; |
| 104190 | + pParse->nMem = 3; |
| 104191 | + } |
| 103738 | 104192 | pTab = pIdx->pTable; |
| 103739 | | - sqlite3VdbeSetNumCols(v, 6); |
| 103740 | | - pParse->nMem = 6; |
| 104193 | + sqlite3VdbeSetNumCols(v, pParse->nMem); |
| 103741 | 104194 | sqlite3CodeVerifySchema(pParse, iDb); |
| 103742 | 104195 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC); |
| 103743 | 104196 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 103744 | 104197 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC); |
| 103745 | | - sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC); |
| 103746 | | - sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC); |
| 103747 | | - sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC); |
| 104198 | + if( pPragma->iArg ){ |
| 104199 | + sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC); |
| 104200 | + sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC); |
| 104201 | + sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC); |
| 104202 | + } |
| 103748 | 104203 | for(i=0; i<mx; i++){ |
| 103749 | 104204 | i16 cnum = pIdx->aiColumn[i]; |
| 103750 | 104205 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| 103751 | 104206 | sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2); |
| 103752 | 104207 | if( cnum<0 ){ |
| 103753 | 104208 | sqlite3VdbeAddOp2(v, OP_Null, 0, 3); |
| 103754 | 104209 | }else{ |
| 103755 | 104210 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0); |
| 103756 | 104211 | } |
| 103757 | | - sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4); |
| 103758 | | - sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0); |
| 103759 | | - sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6); |
| 103760 | | - sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); |
| 104212 | + if( pPragma->iArg ){ |
| 104213 | + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4); |
| 104214 | + sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0); |
| 104215 | + sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6); |
| 104216 | + } |
| 104217 | + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); |
| 103761 | 104218 | } |
| 103762 | 104219 | } |
| 103763 | 104220 | } |
| 103764 | 104221 | break; |
| 103765 | 104222 | |
| | @@ -104457,12 +104914,13 @@ |
| 104457 | 104914 | #endif |
| 104458 | 104915 | |
| 104459 | 104916 | /* |
| 104460 | 104917 | ** PRAGMA shrink_memory |
| 104461 | 104918 | ** |
| 104462 | | - ** This pragma attempts to free as much memory as possible from the |
| 104463 | | - ** current database connection. |
| 104919 | + ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database |
| 104920 | + ** connection on which it is invoked to free up as much memory as it |
| 104921 | + ** can, by calling sqlite3_db_release_memory(). |
| 104464 | 104922 | */ |
| 104465 | 104923 | case PragTyp_SHRINK_MEMORY: { |
| 104466 | 104924 | sqlite3_db_release_memory(db); |
| 104467 | 104925 | break; |
| 104468 | 104926 | } |
| | @@ -104487,12 +104945,16 @@ |
| 104487 | 104945 | |
| 104488 | 104946 | /* |
| 104489 | 104947 | ** PRAGMA soft_heap_limit |
| 104490 | 104948 | ** PRAGMA soft_heap_limit = N |
| 104491 | 104949 | ** |
| 104492 | | - ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted, |
| 104493 | | - ** use -1. |
| 104950 | + ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the |
| 104951 | + ** sqlite3_soft_heap_limit64() interface with the argument N, if N is |
| 104952 | + ** specified and is a non-negative integer. |
| 104953 | + ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always |
| 104954 | + ** returns the same integer that would be returned by the |
| 104955 | + ** sqlite3_soft_heap_limit64(-1) C-language function. |
| 104494 | 104956 | */ |
| 104495 | 104957 | case PragTyp_SOFT_HEAP_LIMIT: { |
| 104496 | 104958 | sqlite3_int64 N; |
| 104497 | 104959 | if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ |
| 104498 | 104960 | sqlite3_soft_heap_limit64(N); |
| | @@ -106065,24 +106527,21 @@ |
| 106065 | 106527 | }else{ |
| 106066 | 106528 | op = OP_IdxInsert; |
| 106067 | 106529 | } |
| 106068 | 106530 | sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord); |
| 106069 | 106531 | if( pSelect->iLimit ){ |
| 106070 | | - int addr1, addr2; |
| 106532 | + int addr; |
| 106071 | 106533 | int iLimit; |
| 106072 | 106534 | if( pSelect->iOffset ){ |
| 106073 | 106535 | iLimit = pSelect->iOffset+1; |
| 106074 | 106536 | }else{ |
| 106075 | 106537 | iLimit = pSelect->iLimit; |
| 106076 | 106538 | } |
| 106077 | | - addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v); |
| 106078 | | - sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1); |
| 106079 | | - addr2 = sqlite3VdbeAddOp0(v, OP_Goto); |
| 106080 | | - sqlite3VdbeJumpHere(v, addr1); |
| 106539 | + addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, -1); VdbeCoverage(v); |
| 106081 | 106540 | sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor); |
| 106082 | 106541 | sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor); |
| 106083 | | - sqlite3VdbeJumpHere(v, addr2); |
| 106542 | + sqlite3VdbeJumpHere(v, addr); |
| 106084 | 106543 | } |
| 106085 | 106544 | } |
| 106086 | 106545 | |
| 106087 | 106546 | /* |
| 106088 | 106547 | ** Add code to implement the OFFSET |
| | @@ -106475,11 +106934,11 @@ |
| 106475 | 106934 | /* Jump to the end of the loop if the LIMIT is reached. Except, if |
| 106476 | 106935 | ** there is a sorter, in which case the sorter has already limited |
| 106477 | 106936 | ** the output for us. |
| 106478 | 106937 | */ |
| 106479 | 106938 | if( pSort==0 && p->iLimit ){ |
| 106480 | | - sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v); |
| 106939 | + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); |
| 106481 | 106940 | } |
| 106482 | 106941 | } |
| 106483 | 106942 | |
| 106484 | 106943 | /* |
| 106485 | 106944 | ** Allocate a KeyInfo object sufficient for an index of N key columns and |
| | @@ -107328,11 +107787,11 @@ |
| 107328 | 107787 | } |
| 107329 | 107788 | }else{ |
| 107330 | 107789 | sqlite3ExprCode(pParse, p->pLimit, iLimit); |
| 107331 | 107790 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); |
| 107332 | 107791 | VdbeComment((v, "LIMIT counter")); |
| 107333 | | - sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); VdbeCoverage(v); |
| 107792 | + sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); |
| 107334 | 107793 | } |
| 107335 | 107794 | if( p->pOffset ){ |
| 107336 | 107795 | p->iOffset = iOffset = ++pParse->nMem; |
| 107337 | 107796 | pParse->nMem++; /* Allocate an extra register for limit+offset */ |
| 107338 | 107797 | sqlite3ExprCode(pParse, p->pOffset, iOffset); |
| | @@ -107547,11 +108006,11 @@ |
| 107547 | 108006 | addrCont = sqlite3VdbeMakeLabel(v); |
| 107548 | 108007 | codeOffset(v, regOffset, addrCont); |
| 107549 | 108008 | selectInnerLoop(pParse, p, p->pEList, iCurrent, |
| 107550 | 108009 | 0, 0, pDest, addrCont, addrBreak); |
| 107551 | 108010 | if( regLimit ){ |
| 107552 | | - sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1); |
| 108011 | + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak); |
| 107553 | 108012 | VdbeCoverage(v); |
| 107554 | 108013 | } |
| 107555 | 108014 | sqlite3VdbeResolveLabel(v, addrCont); |
| 107556 | 108015 | |
| 107557 | 108016 | /* Execute the recursive SELECT taking the single row in Current as |
| | @@ -107772,11 +108231,11 @@ |
| 107772 | 108231 | } |
| 107773 | 108232 | p->pPrior = 0; |
| 107774 | 108233 | p->iLimit = pPrior->iLimit; |
| 107775 | 108234 | p->iOffset = pPrior->iOffset; |
| 107776 | 108235 | if( p->iLimit ){ |
| 107777 | | - addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); VdbeCoverage(v); |
| 108236 | + addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); |
| 107778 | 108237 | VdbeComment((v, "Jump ahead if LIMIT reached")); |
| 107779 | 108238 | } |
| 107780 | 108239 | explainSetInteger(iSub2, pParse->iNextSelectId); |
| 107781 | 108240 | rc = sqlite3Select(pParse, p, &dest); |
| 107782 | 108241 | testcase( rc!=SQLITE_OK ); |
| | @@ -108173,11 +108632,11 @@ |
| 108173 | 108632 | } |
| 108174 | 108633 | |
| 108175 | 108634 | /* Jump to the end of the loop if the LIMIT is reached. |
| 108176 | 108635 | */ |
| 108177 | 108636 | if( p->iLimit ){ |
| 108178 | | - sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v); |
| 108637 | + sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); |
| 108179 | 108638 | } |
| 108180 | 108639 | |
| 108181 | 108640 | /* Generate the subroutine return |
| 108182 | 108641 | */ |
| 108183 | 108642 | sqlite3VdbeResolveLabel(v, iContinue); |
| | @@ -109540,11 +109999,11 @@ |
| 109540 | 109999 | sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); |
| 109541 | 110000 | |
| 109542 | 110001 | for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); |
| 109543 | 110002 | pEList = pLeft->pEList; |
| 109544 | 110003 | if( pCte->pCols ){ |
| 109545 | | - if( pEList->nExpr!=pCte->pCols->nExpr ){ |
| 110004 | + if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ |
| 109546 | 110005 | sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", |
| 109547 | 110006 | pCte->zName, pEList->nExpr, pCte->pCols->nExpr |
| 109548 | 110007 | ); |
| 109549 | 110008 | pParse->pWith = pSavedWith; |
| 109550 | 110009 | return SQLITE_ERROR; |
| | @@ -113966,10 +114425,11 @@ |
| 113966 | 114425 | */ |
| 113967 | 114426 | if( !db->init.busy ){ |
| 113968 | 114427 | char *zStmt; |
| 113969 | 114428 | char *zWhere; |
| 113970 | 114429 | int iDb; |
| 114430 | + int iReg; |
| 113971 | 114431 | Vdbe *v; |
| 113972 | 114432 | |
| 113973 | 114433 | /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
| 113974 | 114434 | if( pEnd ){ |
| 113975 | 114435 | pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; |
| | @@ -114000,12 +114460,14 @@ |
| 114000 | 114460 | sqlite3ChangeCookie(pParse, iDb); |
| 114001 | 114461 | |
| 114002 | 114462 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 114003 | 114463 | zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
| 114004 | 114464 | sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
| 114005 | | - sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 114006 | | - pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
| 114465 | + |
| 114466 | + iReg = ++pParse->nMem; |
| 114467 | + sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0); |
| 114468 | + sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); |
| 114007 | 114469 | } |
| 114008 | 114470 | |
| 114009 | 114471 | /* If we are rereading the sqlite_master table create the in-memory |
| 114010 | 114472 | ** record of the table. The xConnect() method is not called until |
| 114011 | 114473 | ** the first time the virtual table is used in an SQL statement. This |
| | @@ -114354,15 +114816,19 @@ |
| 114354 | 114816 | int rc = SQLITE_OK; |
| 114355 | 114817 | Table *pTab; |
| 114356 | 114818 | |
| 114357 | 114819 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
| 114358 | 114820 | if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ |
| 114359 | | - VTable *p = vtabDisconnectAll(db, pTab); |
| 114360 | | - |
| 114361 | | - assert( rc==SQLITE_OK ); |
| 114821 | + VTable *p; |
| 114822 | + for(p=pTab->pVTable; p; p=p->pNext){ |
| 114823 | + assert( p->pVtab ); |
| 114824 | + if( p->pVtab->nRef>0 ){ |
| 114825 | + return SQLITE_LOCKED; |
| 114826 | + } |
| 114827 | + } |
| 114828 | + p = vtabDisconnectAll(db, pTab); |
| 114362 | 114829 | rc = p->pMod->pModule->xDestroy(p->pVtab); |
| 114363 | | - |
| 114364 | 114830 | /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ |
| 114365 | 114831 | if( rc==SQLITE_OK ){ |
| 114366 | 114832 | assert( pTab->pVTable==p && p->pNext==0 ); |
| 114367 | 114833 | p->pVtab = 0; |
| 114368 | 114834 | pTab->pVTable = 0; |
| | @@ -114787,10 +115253,12 @@ |
| 114787 | 115253 | int addrNxt; /* Jump here to start the next IN combination */ |
| 114788 | 115254 | int addrSkip; /* Jump here for next iteration of skip-scan */ |
| 114789 | 115255 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 114790 | 115256 | int addrFirst; /* First instruction of interior of the loop */ |
| 114791 | 115257 | int addrBody; /* Beginning of the body of this loop */ |
| 115258 | + int iLikeRepCntr; /* LIKE range processing counter register */ |
| 115259 | + int addrLikeRep; /* LIKE range processing address */ |
| 114792 | 115260 | u8 iFrom; /* Which entry in the FROM clause */ |
| 114793 | 115261 | u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ |
| 114794 | 115262 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 114795 | 115263 | union { /* Information that depends on pWLoop->wsFlags */ |
| 114796 | 115264 | struct { |
| | @@ -114971,11 +115439,11 @@ |
| 114971 | 115439 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 114972 | 115440 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
| 114973 | 115441 | } u; |
| 114974 | 115442 | LogEst truthProb; /* Probability of truth for this expression */ |
| 114975 | 115443 | u16 eOperator; /* A WO_xx value describing <op> */ |
| 114976 | | - u8 wtFlags; /* TERM_xxx bit flags. See below */ |
| 115444 | + u16 wtFlags; /* TERM_xxx bit flags. See below */ |
| 114977 | 115445 | u8 nChild; /* Number of children that must disable us */ |
| 114978 | 115446 | WhereClause *pWC; /* The clause this term is part of */ |
| 114979 | 115447 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 114980 | 115448 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
| 114981 | 115449 | }; |
| | @@ -114993,10 +115461,13 @@ |
| 114993 | 115461 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 114994 | 115462 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 114995 | 115463 | #else |
| 114996 | 115464 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 114997 | 115465 | #endif |
| 115466 | +#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ |
| 115467 | +#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ |
| 115468 | +#define TERM_LIKE 0x400 /* The original LIKE operator */ |
| 114998 | 115469 | |
| 114999 | 115470 | /* |
| 115000 | 115471 | ** An instance of the WhereScan object is used as an iterator for locating |
| 115001 | 115472 | ** terms in the WHERE clause that are useful to the query planner. |
| 115002 | 115473 | */ |
| | @@ -115368,11 +115839,11 @@ |
| 115368 | 115839 | ** WARNING: This routine might reallocate the space used to store |
| 115369 | 115840 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
| 115370 | 115841 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 115371 | 115842 | ** the pWC->a[] array. |
| 115372 | 115843 | */ |
| 115373 | | -static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
| 115844 | +static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ |
| 115374 | 115845 | WhereTerm *pTerm; |
| 115375 | 115846 | int idx; |
| 115376 | 115847 | testcase( wtFlags & TERM_VIRTUAL ); |
| 115377 | 115848 | if( pWC->nTerm>=pWC->nSlot ){ |
| 115378 | 115849 | WhereTerm *pOld = pWC->a; |
| | @@ -115793,11 +116264,15 @@ |
| 115793 | 116264 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 115794 | 116265 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 115795 | 116266 | ** so and false if not. |
| 115796 | 116267 | ** |
| 115797 | 116268 | ** In order for the operator to be optimizible, the RHS must be a string |
| 115798 | | -** literal that does not begin with a wildcard. |
| 116269 | +** literal that does not begin with a wildcard. The LHS must be a column |
| 116270 | +** that may only be NULL, a string, or a BLOB, never a number. (This means |
| 116271 | +** that virtual tables cannot participate in the LIKE optimization.) If the |
| 116272 | +** collating sequence for the column on the LHS must be appropriate for |
| 116273 | +** the operator. |
| 115799 | 116274 | */ |
| 115800 | 116275 | static int isLikeOrGlob( |
| 115801 | 116276 | Parse *pParse, /* Parsing and code generating context */ |
| 115802 | 116277 | Expr *pExpr, /* Test this expression */ |
| 115803 | 116278 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
| | @@ -115822,11 +116297,11 @@ |
| 115822 | 116297 | #endif |
| 115823 | 116298 | pList = pExpr->x.pList; |
| 115824 | 116299 | pLeft = pList->a[1].pExpr; |
| 115825 | 116300 | if( pLeft->op!=TK_COLUMN |
| 115826 | 116301 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 115827 | | - || IsVirtual(pLeft->pTab) |
| 116302 | + || IsVirtual(pLeft->pTab) /* Value might be numeric */ |
| 115828 | 116303 | ){ |
| 115829 | 116304 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 115830 | 116305 | ** be the name of an indexed column with TEXT affinity. */ |
| 115831 | 116306 | return 0; |
| 115832 | 116307 | } |
| | @@ -115931,10 +116406,83 @@ |
| 115931 | 116406 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 115932 | 116407 | pWC->a[iChild].iParent = iParent; |
| 115933 | 116408 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 115934 | 116409 | pWC->a[iParent].nChild++; |
| 115935 | 116410 | } |
| 116411 | + |
| 116412 | +/* |
| 116413 | +** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 116414 | +** a conjunction, then return just pTerm when N==0. If N is exceeds |
| 116415 | +** the number of available subterms, return NULL. |
| 116416 | +*/ |
| 116417 | +static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ |
| 116418 | + if( pTerm->eOperator!=WO_AND ){ |
| 116419 | + return N==0 ? pTerm : 0; |
| 116420 | + } |
| 116421 | + if( N<pTerm->u.pAndInfo->wc.nTerm ){ |
| 116422 | + return &pTerm->u.pAndInfo->wc.a[N]; |
| 116423 | + } |
| 116424 | + return 0; |
| 116425 | +} |
| 116426 | + |
| 116427 | +/* |
| 116428 | +** Subterms pOne and pTwo are contained within WHERE clause pWC. The |
| 116429 | +** two subterms are in disjunction - they are OR-ed together. |
| 116430 | +** |
| 116431 | +** If these two terms are both of the form: "A op B" with the same |
| 116432 | +** A and B values but different operators and if the operators are |
| 116433 | +** compatible (if one is = and the other is <, for example) then |
| 116434 | +** add a new virtual AND term to pWC that is the combination of the |
| 116435 | +** two. |
| 116436 | +** |
| 116437 | +** Some examples: |
| 116438 | +** |
| 116439 | +** x<y OR x=y --> x<=y |
| 116440 | +** x=y OR x=y --> x=y |
| 116441 | +** x<=y OR x<y --> x<=y |
| 116442 | +** |
| 116443 | +** The following is NOT generated: |
| 116444 | +** |
| 116445 | +** x<y OR x>y --> x!=y |
| 116446 | +*/ |
| 116447 | +static void whereCombineDisjuncts( |
| 116448 | + SrcList *pSrc, /* the FROM clause */ |
| 116449 | + WhereClause *pWC, /* The complete WHERE clause */ |
| 116450 | + WhereTerm *pOne, /* First disjunct */ |
| 116451 | + WhereTerm *pTwo /* Second disjunct */ |
| 116452 | +){ |
| 116453 | + u16 eOp = pOne->eOperator | pTwo->eOperator; |
| 116454 | + sqlite3 *db; /* Database connection (for malloc) */ |
| 116455 | + Expr *pNew; /* New virtual expression */ |
| 116456 | + int op; /* Operator for the combined expression */ |
| 116457 | + int idxNew; /* Index in pWC of the next virtual term */ |
| 116458 | + |
| 116459 | + if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 116460 | + if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 116461 | + if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 116462 | + && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 116463 | + assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); |
| 116464 | + assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); |
| 116465 | + if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; |
| 116466 | + if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; |
| 116467 | + /* If we reach this point, it means the two subterms can be combined */ |
| 116468 | + if( (eOp & (eOp-1))!=0 ){ |
| 116469 | + if( eOp & (WO_LT|WO_LE) ){ |
| 116470 | + eOp = WO_LE; |
| 116471 | + }else{ |
| 116472 | + assert( eOp & (WO_GT|WO_GE) ); |
| 116473 | + eOp = WO_GE; |
| 116474 | + } |
| 116475 | + } |
| 116476 | + db = pWC->pWInfo->pParse->db; |
| 116477 | + pNew = sqlite3ExprDup(db, pOne->pExpr, 0); |
| 116478 | + if( pNew==0 ) return; |
| 116479 | + for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } |
| 116480 | + pNew->op = op; |
| 116481 | + idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 116482 | + exprAnalyze(pSrc, pWC, idxNew); |
| 116483 | +} |
| 115936 | 116484 | |
| 115937 | 116485 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 115938 | 116486 | /* |
| 115939 | 116487 | ** Analyze a term that consists of two or more OR-connected |
| 115940 | 116488 | ** subterms. So in: |
| | @@ -115956,10 +116504,11 @@ |
| 115956 | 116504 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 115957 | 116505 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 115958 | 116506 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 115959 | 116507 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 115960 | 116508 | ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) |
| 116509 | +** (F) x>A OR (x=A AND y>=B) |
| 115961 | 116510 | ** |
| 115962 | 116511 | ** CASE 1: |
| 115963 | 116512 | ** |
| 115964 | 116513 | ** If all subterms are of the form T.C=expr for some single column of C and |
| 115965 | 116514 | ** a single table T (as shown in example B above) then create a new virtual |
| | @@ -115971,10 +116520,20 @@ |
| 115971 | 116520 | ** then create a new virtual term like this: |
| 115972 | 116521 | ** |
| 115973 | 116522 | ** x IN (expr1,expr2,expr3) |
| 115974 | 116523 | ** |
| 115975 | 116524 | ** CASE 2: |
| 116525 | +** |
| 116526 | +** If there are exactly two disjuncts one side has x>A and the other side |
| 116527 | +** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 116528 | +** WHERE clause of the form "x>=A". Example: |
| 116529 | +** |
| 116530 | +** x>A OR (x=A AND y>B) adds: x>=A |
| 116531 | +** |
| 116532 | +** The added conjunct can sometimes be helpful in query planning. |
| 116533 | +** |
| 116534 | +** CASE 3: |
| 115976 | 116535 | ** |
| 115977 | 116536 | ** If all subterms are indexable by a single table T, then set |
| 115978 | 116537 | ** |
| 115979 | 116538 | ** WhereTerm.eOperator = WO_OR |
| 115980 | 116539 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| | @@ -116098,15 +116657,29 @@ |
| 116098 | 116657 | } |
| 116099 | 116658 | } |
| 116100 | 116659 | } |
| 116101 | 116660 | |
| 116102 | 116661 | /* |
| 116103 | | - ** Record the set of tables that satisfy case 2. The set might be |
| 116662 | + ** Record the set of tables that satisfy case 3. The set might be |
| 116104 | 116663 | ** empty. |
| 116105 | 116664 | */ |
| 116106 | 116665 | pOrInfo->indexable = indexable; |
| 116107 | 116666 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
| 116667 | + |
| 116668 | + /* For a two-way OR, attempt to implementation case 2. |
| 116669 | + */ |
| 116670 | + if( indexable && pOrWc->nTerm==2 ){ |
| 116671 | + int iOne = 0; |
| 116672 | + WhereTerm *pOne; |
| 116673 | + while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ |
| 116674 | + int iTwo = 0; |
| 116675 | + WhereTerm *pTwo; |
| 116676 | + while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ |
| 116677 | + whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); |
| 116678 | + } |
| 116679 | + } |
| 116680 | + } |
| 116108 | 116681 | |
| 116109 | 116682 | /* |
| 116110 | 116683 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 116111 | 116684 | ** we have to do some additional checking to see if case 1 really |
| 116112 | 116685 | ** is satisfied. |
| | @@ -116233,11 +116806,11 @@ |
| 116233 | 116806 | pTerm = &pWC->a[idxTerm]; |
| 116234 | 116807 | markTermAsChild(pWC, idxNew, idxTerm); |
| 116235 | 116808 | }else{ |
| 116236 | 116809 | sqlite3ExprListDelete(db, pList); |
| 116237 | 116810 | } |
| 116238 | | - pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 116811 | + pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
| 116239 | 116812 | } |
| 116240 | 116813 | } |
| 116241 | 116814 | } |
| 116242 | 116815 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
| 116243 | 116816 | |
| | @@ -116271,11 +116844,11 @@ |
| 116271 | 116844 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 116272 | 116845 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
| 116273 | 116846 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 116274 | 116847 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 116275 | 116848 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 116276 | | - int noCase = 0; /* LIKE/GLOB distinguishes case */ |
| 116849 | + int noCase = 0; /* uppercase equivalent to lowercase */ |
| 116277 | 116850 | int op; /* Top-level operator. pExpr->op */ |
| 116278 | 116851 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 116279 | 116852 | sqlite3 *db = pParse->db; /* Database connection */ |
| 116280 | 116853 | |
| 116281 | 116854 | if( db->mallocFailed ){ |
| | @@ -116409,16 +116982,19 @@ |
| 116409 | 116982 | |
| 116410 | 116983 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 116411 | 116984 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 116412 | 116985 | ** operator. |
| 116413 | 116986 | ** |
| 116414 | | - ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 116987 | + ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints |
| 116415 | 116988 | ** |
| 116416 | | - ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 116989 | + ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' |
| 116417 | 116990 | ** |
| 116418 | 116991 | ** The last character of the prefix "abc" is incremented to form the |
| 116419 | | - ** termination condition "abd". |
| 116992 | + ** termination condition "abd". If case is not significant (the default |
| 116993 | + ** for LIKE) then the lower-bound is made all uppercase and the upper- |
| 116994 | + ** bound is made all lowercase so that the bounds also work when comparing |
| 116995 | + ** BLOBs. |
| 116420 | 116996 | */ |
| 116421 | 116997 | if( pWC->op==TK_AND |
| 116422 | 116998 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 116423 | 116999 | ){ |
| 116424 | 117000 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| | @@ -116425,14 +117001,30 @@ |
| 116425 | 117001 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 116426 | 117002 | Expr *pNewExpr1; |
| 116427 | 117003 | Expr *pNewExpr2; |
| 116428 | 117004 | int idxNew1; |
| 116429 | 117005 | int idxNew2; |
| 116430 | | - Token sCollSeqName; /* Name of collating sequence */ |
| 117006 | + const char *zCollSeqName; /* Name of collating sequence */ |
| 117007 | + const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
| 116431 | 117008 | |
| 116432 | 117009 | pLeft = pExpr->x.pList->a[1].pExpr; |
| 116433 | 117010 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
| 117011 | + |
| 117012 | + /* Convert the lower bound to upper-case and the upper bound to |
| 117013 | + ** lower-case (upper-case is less than lower-case in ASCII) so that |
| 117014 | + ** the range constraints also work for BLOBs |
| 117015 | + */ |
| 117016 | + if( noCase && !pParse->db->mallocFailed ){ |
| 117017 | + int i; |
| 117018 | + char c; |
| 117019 | + pTerm->wtFlags |= TERM_LIKE; |
| 117020 | + for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ |
| 117021 | + pStr1->u.zToken[i] = sqlite3Toupper(c); |
| 117022 | + pStr2->u.zToken[i] = sqlite3Tolower(c); |
| 117023 | + } |
| 117024 | + } |
| 117025 | + |
| 116434 | 117026 | if( !db->mallocFailed ){ |
| 116435 | 117027 | u8 c, *pC; /* Last character before the first wildcard */ |
| 116436 | 117028 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
| 116437 | 117029 | c = *pC; |
| 116438 | 117030 | if( noCase ){ |
| | @@ -116445,26 +117037,25 @@ |
| 116445 | 117037 | if( c=='A'-1 ) isComplete = 0; |
| 116446 | 117038 | c = sqlite3UpperToLower[c]; |
| 116447 | 117039 | } |
| 116448 | 117040 | *pC = c + 1; |
| 116449 | 117041 | } |
| 116450 | | - sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 116451 | | - sCollSeqName.n = 6; |
| 117042 | + zCollSeqName = noCase ? "NOCASE" : "BINARY"; |
| 116452 | 117043 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
| 116453 | | - pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 116454 | | - sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
| 117044 | + pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 117045 | + sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
| 116455 | 117046 | pStr1, 0); |
| 116456 | 117047 | transferJoinMarkings(pNewExpr1, pExpr); |
| 116457 | | - idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
| 117048 | + idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
| 116458 | 117049 | testcase( idxNew1==0 ); |
| 116459 | 117050 | exprAnalyze(pSrc, pWC, idxNew1); |
| 116460 | 117051 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
| 116461 | 117052 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
| 116462 | | - sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
| 117053 | + sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
| 116463 | 117054 | pStr2, 0); |
| 116464 | 117055 | transferJoinMarkings(pNewExpr2, pExpr); |
| 116465 | | - idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 117056 | + idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
| 116466 | 117057 | testcase( idxNew2==0 ); |
| 116467 | 117058 | exprAnalyze(pSrc, pWC, idxNew2); |
| 116468 | 117059 | pTerm = &pWC->a[idxTerm]; |
| 116469 | 117060 | if( isComplete ){ |
| 116470 | 117061 | markTermAsChild(pWC, idxNew1, idxTerm); |
| | @@ -117074,15 +117665,18 @@ |
| 117074 | 117665 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 117075 | 117666 | /* |
| 117076 | 117667 | ** Estimate the location of a particular key among all keys in an |
| 117077 | 117668 | ** index. Store the results in aStat as follows: |
| 117078 | 117669 | ** |
| 117079 | | -** aStat[0] Est. number of rows less than pVal |
| 117080 | | -** aStat[1] Est. number of rows equal to pVal |
| 117670 | +** aStat[0] Est. number of rows less than pRec |
| 117671 | +** aStat[1] Est. number of rows equal to pRec |
| 117081 | 117672 | ** |
| 117082 | 117673 | ** Return the index of the sample that is the smallest sample that |
| 117083 | | -** is greater than or equal to pRec. |
| 117674 | +** is greater than or equal to pRec. Note that this index is not an index |
| 117675 | +** into the aSample[] array - it is an index into a virtual set of samples |
| 117676 | +** based on the contents of aSample[] and the number of fields in record |
| 117677 | +** pRec. |
| 117084 | 117678 | */ |
| 117085 | 117679 | static int whereKeyStats( |
| 117086 | 117680 | Parse *pParse, /* Database connection */ |
| 117087 | 117681 | Index *pIdx, /* Index to consider domain of */ |
| 117088 | 117682 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| | @@ -117089,71 +117683,162 @@ |
| 117089 | 117683 | int roundUp, /* Round up if true. Round down if false */ |
| 117090 | 117684 | tRowcnt *aStat /* OUT: stats written here */ |
| 117091 | 117685 | ){ |
| 117092 | 117686 | IndexSample *aSample = pIdx->aSample; |
| 117093 | 117687 | int iCol; /* Index of required stats in anEq[] etc. */ |
| 117688 | + int i; /* Index of first sample >= pRec */ |
| 117689 | + int iSample; /* Smallest sample larger than or equal to pRec */ |
| 117094 | 117690 | int iMin = 0; /* Smallest sample not yet tested */ |
| 117095 | | - int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 117096 | 117691 | int iTest; /* Next sample to test */ |
| 117097 | 117692 | int res; /* Result of comparison operation */ |
| 117693 | + int nField; /* Number of fields in pRec */ |
| 117694 | + tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ |
| 117098 | 117695 | |
| 117099 | 117696 | #ifndef SQLITE_DEBUG |
| 117100 | 117697 | UNUSED_PARAMETER( pParse ); |
| 117101 | 117698 | #endif |
| 117102 | 117699 | assert( pRec!=0 ); |
| 117103 | | - iCol = pRec->nField - 1; |
| 117104 | 117700 | assert( pIdx->nSample>0 ); |
| 117105 | | - assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
| 117701 | + assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); |
| 117702 | + |
| 117703 | + /* Do a binary search to find the first sample greater than or equal |
| 117704 | + ** to pRec. If pRec contains a single field, the set of samples to search |
| 117705 | + ** is simply the aSample[] array. If the samples in aSample[] contain more |
| 117706 | + ** than one fields, all fields following the first are ignored. |
| 117707 | + ** |
| 117708 | + ** If pRec contains N fields, where N is more than one, then as well as the |
| 117709 | + ** samples in aSample[] (truncated to N fields), the search also has to |
| 117710 | + ** consider prefixes of those samples. For example, if the set of samples |
| 117711 | + ** in aSample is: |
| 117712 | + ** |
| 117713 | + ** aSample[0] = (a, 5) |
| 117714 | + ** aSample[1] = (a, 10) |
| 117715 | + ** aSample[2] = (b, 5) |
| 117716 | + ** aSample[3] = (c, 100) |
| 117717 | + ** aSample[4] = (c, 105) |
| 117718 | + ** |
| 117719 | + ** Then the search space should ideally be the samples above and the |
| 117720 | + ** unique prefixes [a], [b] and [c]. But since that is hard to organize, |
| 117721 | + ** the code actually searches this set: |
| 117722 | + ** |
| 117723 | + ** 0: (a) |
| 117724 | + ** 1: (a, 5) |
| 117725 | + ** 2: (a, 10) |
| 117726 | + ** 3: (a, 10) |
| 117727 | + ** 4: (b) |
| 117728 | + ** 5: (b, 5) |
| 117729 | + ** 6: (c) |
| 117730 | + ** 7: (c, 100) |
| 117731 | + ** 8: (c, 105) |
| 117732 | + ** 9: (c, 105) |
| 117733 | + ** |
| 117734 | + ** For each sample in the aSample[] array, N samples are present in the |
| 117735 | + ** effective sample array. In the above, samples 0 and 1 are based on |
| 117736 | + ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. |
| 117737 | + ** |
| 117738 | + ** Often, sample i of each block of N effective samples has (i+1) fields. |
| 117739 | + ** Except, each sample may be extended to ensure that it is greater than or |
| 117740 | + ** equal to the previous sample in the array. For example, in the above, |
| 117741 | + ** sample 2 is the first sample of a block of N samples, so at first it |
| 117742 | + ** appears that it should be 1 field in size. However, that would make it |
| 117743 | + ** smaller than sample 1, so the binary search would not work. As a result, |
| 117744 | + ** it is extended to two fields. The duplicates that this creates do not |
| 117745 | + ** cause any problems. |
| 117746 | + */ |
| 117747 | + nField = pRec->nField; |
| 117748 | + iCol = 0; |
| 117749 | + iSample = pIdx->nSample * nField; |
| 117106 | 117750 | do{ |
| 117107 | | - iTest = (iMin+i)/2; |
| 117108 | | - res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
| 117751 | + int iSamp; /* Index in aSample[] of test sample */ |
| 117752 | + int n; /* Number of fields in test sample */ |
| 117753 | + |
| 117754 | + iTest = (iMin+iSample)/2; |
| 117755 | + iSamp = iTest / nField; |
| 117756 | + if( iSamp>0 ){ |
| 117757 | + /* The proposed effective sample is a prefix of sample aSample[iSamp]. |
| 117758 | + ** Specifically, the shortest prefix of at least (1 + iTest%nField) |
| 117759 | + ** fields that is greater than the previous effective sample. */ |
| 117760 | + for(n=(iTest % nField) + 1; n<nField; n++){ |
| 117761 | + if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; |
| 117762 | + } |
| 117763 | + }else{ |
| 117764 | + n = iTest + 1; |
| 117765 | + } |
| 117766 | + |
| 117767 | + pRec->nField = n; |
| 117768 | + res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); |
| 117109 | 117769 | if( res<0 ){ |
| 117770 | + iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; |
| 117771 | + iMin = iTest+1; |
| 117772 | + }else if( res==0 && n<nField ){ |
| 117773 | + iLower = aSample[iSamp].anLt[n-1]; |
| 117110 | 117774 | iMin = iTest+1; |
| 117775 | + res = -1; |
| 117111 | 117776 | }else{ |
| 117112 | | - i = iTest; |
| 117777 | + iSample = iTest; |
| 117778 | + iCol = n-1; |
| 117113 | 117779 | } |
| 117114 | | - }while( res && iMin<i ); |
| 117780 | + }while( res && iMin<iSample ); |
| 117781 | + i = iSample / nField; |
| 117115 | 117782 | |
| 117116 | 117783 | #ifdef SQLITE_DEBUG |
| 117117 | 117784 | /* The following assert statements check that the binary search code |
| 117118 | 117785 | ** above found the right answer. This block serves no purpose other |
| 117119 | 117786 | ** than to invoke the asserts. */ |
| 117120 | | - if( res==0 ){ |
| 117121 | | - /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 117122 | | - assert( i<pIdx->nSample ); |
| 117123 | | - assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 117124 | | - || pParse->db->mallocFailed ); |
| 117125 | | - }else{ |
| 117126 | | - /* Otherwise, pRec must be smaller than sample $i and larger than |
| 117127 | | - ** sample ($i-1). */ |
| 117128 | | - assert( i==pIdx->nSample |
| 117129 | | - || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 117130 | | - || pParse->db->mallocFailed ); |
| 117131 | | - assert( i==0 |
| 117132 | | - || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 117133 | | - || pParse->db->mallocFailed ); |
| 117787 | + if( pParse->db->mallocFailed==0 ){ |
| 117788 | + if( res==0 ){ |
| 117789 | + /* If (res==0) is true, then pRec must be equal to sample i. */ |
| 117790 | + assert( i<pIdx->nSample ); |
| 117791 | + assert( iCol==nField-1 ); |
| 117792 | + pRec->nField = nField; |
| 117793 | + assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 117794 | + || pParse->db->mallocFailed |
| 117795 | + ); |
| 117796 | + }else{ |
| 117797 | + /* Unless i==pIdx->nSample, indicating that pRec is larger than |
| 117798 | + ** all samples in the aSample[] array, pRec must be smaller than the |
| 117799 | + ** (iCol+1) field prefix of sample i. */ |
| 117800 | + assert( i<=pIdx->nSample && i>=0 ); |
| 117801 | + pRec->nField = iCol+1; |
| 117802 | + assert( i==pIdx->nSample |
| 117803 | + || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 117804 | + || pParse->db->mallocFailed ); |
| 117805 | + |
| 117806 | + /* if i==0 and iCol==0, then record pRec is smaller than all samples |
| 117807 | + ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must |
| 117808 | + ** be greater than or equal to the (iCol) field prefix of sample i. |
| 117809 | + ** If (i>0), then pRec must also be greater than sample (i-1). */ |
| 117810 | + if( iCol>0 ){ |
| 117811 | + pRec->nField = iCol; |
| 117812 | + assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 |
| 117813 | + || pParse->db->mallocFailed ); |
| 117814 | + } |
| 117815 | + if( i>0 ){ |
| 117816 | + pRec->nField = nField; |
| 117817 | + assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 117818 | + || pParse->db->mallocFailed ); |
| 117819 | + } |
| 117820 | + } |
| 117134 | 117821 | } |
| 117135 | 117822 | #endif /* ifdef SQLITE_DEBUG */ |
| 117136 | 117823 | |
| 117137 | | - /* At this point, aSample[i] is the first sample that is greater than |
| 117138 | | - ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 117139 | | - ** than pVal. If aSample[i]==pVal, then res==0. |
| 117140 | | - */ |
| 117141 | 117824 | if( res==0 ){ |
| 117825 | + /* Record pRec is equal to sample i */ |
| 117826 | + assert( iCol==nField-1 ); |
| 117142 | 117827 | aStat[0] = aSample[i].anLt[iCol]; |
| 117143 | 117828 | aStat[1] = aSample[i].anEq[iCol]; |
| 117144 | 117829 | }else{ |
| 117145 | | - tRowcnt iLower, iUpper, iGap; |
| 117146 | | - if( i==0 ){ |
| 117147 | | - iLower = 0; |
| 117148 | | - iUpper = aSample[0].anLt[iCol]; |
| 117830 | + /* At this point, the (iCol+1) field prefix of aSample[i] is the first |
| 117831 | + ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec |
| 117832 | + ** is larger than all samples in the array. */ |
| 117833 | + tRowcnt iUpper, iGap; |
| 117834 | + if( i>=pIdx->nSample ){ |
| 117835 | + iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 117149 | 117836 | }else{ |
| 117150 | | - i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 117151 | | - iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol]; |
| 117152 | | - iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
| 117837 | + iUpper = aSample[i].anLt[iCol]; |
| 117153 | 117838 | } |
| 117154 | | - aStat[1] = pIdx->aAvgEq[iCol]; |
| 117839 | + |
| 117155 | 117840 | if( iLower>=iUpper ){ |
| 117156 | 117841 | iGap = 0; |
| 117157 | 117842 | }else{ |
| 117158 | 117843 | iGap = iUpper - iLower; |
| 117159 | 117844 | } |
| | @@ -117161,11 +117846,15 @@ |
| 117161 | 117846 | iGap = (iGap*2)/3; |
| 117162 | 117847 | }else{ |
| 117163 | 117848 | iGap = iGap/3; |
| 117164 | 117849 | } |
| 117165 | 117850 | aStat[0] = iLower + iGap; |
| 117851 | + aStat[1] = pIdx->aAvgEq[iCol]; |
| 117166 | 117852 | } |
| 117853 | + |
| 117854 | + /* Restore the pRec->nField value before returning. */ |
| 117855 | + pRec->nField = nField; |
| 117167 | 117856 | return i; |
| 117168 | 117857 | } |
| 117169 | 117858 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 117170 | 117859 | |
| 117171 | 117860 | /* |
| | @@ -117635,24 +118324,47 @@ |
| 117635 | 118324 | ** by indices, we disable them to prevent redundant tests in the inner |
| 117636 | 118325 | ** loop. We would get the correct results if nothing were ever disabled, |
| 117637 | 118326 | ** but joins might run a little slower. The trick is to disable as much |
| 117638 | 118327 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 117639 | 118328 | ** the wrong answer. See ticket #813. |
| 118329 | +** |
| 118330 | +** If all the children of a term are disabled, then that term is also |
| 118331 | +** automatically disabled. In this way, terms get disabled if derived |
| 118332 | +** virtual terms are tested first. For example: |
| 118333 | +** |
| 118334 | +** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 118335 | +** \___________/ \______/ \_____/ |
| 118336 | +** parent child1 child2 |
| 118337 | +** |
| 118338 | +** Only the parent term was in the original WHERE clause. The child1 |
| 118339 | +** and child2 terms were added by the LIKE optimization. If both of |
| 118340 | +** the virtual child terms are valid, then testing of the parent can be |
| 118341 | +** skipped. |
| 118342 | +** |
| 118343 | +** Usually the parent term is marked as TERM_CODED. But if the parent |
| 118344 | +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 118345 | +** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 118346 | +** a conditional such that is only evaluated on the second pass of a |
| 118347 | +** LIKE-optimization loop, when scanning BLOBs instead of strings. |
| 117640 | 118348 | */ |
| 117641 | 118349 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 117642 | | - if( pTerm |
| 118350 | + int nLoop = 0; |
| 118351 | + while( pTerm |
| 117643 | 118352 | && (pTerm->wtFlags & TERM_CODED)==0 |
| 117644 | 118353 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 117645 | 118354 | && (pLevel->notReady & pTerm->prereqAll)==0 |
| 117646 | 118355 | ){ |
| 117647 | | - pTerm->wtFlags |= TERM_CODED; |
| 117648 | | - if( pTerm->iParent>=0 ){ |
| 117649 | | - WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 117650 | | - if( (--pOther->nChild)==0 ){ |
| 117651 | | - disableTerm(pLevel, pOther); |
| 117652 | | - } |
| 118356 | + if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 118357 | + pTerm->wtFlags |= TERM_LIKECOND; |
| 118358 | + }else{ |
| 118359 | + pTerm->wtFlags |= TERM_CODED; |
| 117653 | 118360 | } |
| 118361 | + if( pTerm->iParent<0 ) break; |
| 118362 | + pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 118363 | + pTerm->nChild--; |
| 118364 | + if( pTerm->nChild!=0 ) break; |
| 118365 | + nLoop++; |
| 117654 | 118366 | } |
| 117655 | 118367 | } |
| 117656 | 118368 | |
| 117657 | 118369 | /* |
| 117658 | 118370 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| | @@ -118132,11 +118844,38 @@ |
| 118132 | 118844 | } |
| 118133 | 118845 | #else |
| 118134 | 118846 | # define addScanStatus(a, b, c, d) ((void)d) |
| 118135 | 118847 | #endif |
| 118136 | 118848 | |
| 118137 | | - |
| 118849 | +/* |
| 118850 | +** If the most recently coded instruction is a constant range contraint |
| 118851 | +** that originated from the LIKE optimization, then change the P3 to be |
| 118852 | +** pLoop->iLikeRepCntr and set P5. |
| 118853 | +** |
| 118854 | +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 118855 | +** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 118856 | +** scan loop run twice, once for strings and a second time for BLOBs. |
| 118857 | +** The OP_String opcodes on the second pass convert the upper and lower |
| 118858 | +** bound string contants to blobs. This routine makes the necessary changes |
| 118859 | +** to the OP_String opcodes for that to happen. |
| 118860 | +*/ |
| 118861 | +static void whereLikeOptimizationStringFixup( |
| 118862 | + Vdbe *v, /* prepared statement under construction */ |
| 118863 | + WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 118864 | + WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 118865 | +){ |
| 118866 | + if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 118867 | + VdbeOp *pOp; |
| 118868 | + assert( pLevel->iLikeRepCntr>0 ); |
| 118869 | + pOp = sqlite3VdbeGetOp(v, -1); |
| 118870 | + assert( pOp!=0 ); |
| 118871 | + assert( pOp->opcode==OP_String8 |
| 118872 | + || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
| 118873 | + pOp->p3 = pLevel->iLikeRepCntr; |
| 118874 | + pOp->p5 = 1; |
| 118875 | + } |
| 118876 | +} |
| 118138 | 118877 | |
| 118139 | 118878 | /* |
| 118140 | 118879 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 118141 | 118880 | ** implementation described by pWInfo. |
| 118142 | 118881 | */ |
| | @@ -118462,14 +119201,29 @@ |
| 118462 | 119201 | */ |
| 118463 | 119202 | j = nEq; |
| 118464 | 119203 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 118465 | 119204 | pRangeStart = pLoop->aLTerm[j++]; |
| 118466 | 119205 | nExtraReg = 1; |
| 119206 | + /* Like optimization range constraints always occur in pairs */ |
| 119207 | + assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
| 119208 | + (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
| 118467 | 119209 | } |
| 118468 | 119210 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 118469 | 119211 | pRangeEnd = pLoop->aLTerm[j++]; |
| 118470 | 119212 | nExtraReg = 1; |
| 119213 | + if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 119214 | + assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 119215 | + assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
| 119216 | + pLevel->iLikeRepCntr = ++pParse->nMem; |
| 119217 | + testcase( bRev ); |
| 119218 | + testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 119219 | + sqlite3VdbeAddOp2(v, OP_Integer, |
| 119220 | + bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), |
| 119221 | + pLevel->iLikeRepCntr); |
| 119222 | + VdbeComment((v, "LIKE loop counter")); |
| 119223 | + pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
| 119224 | + } |
| 118471 | 119225 | if( pRangeStart==0 |
| 118472 | 119226 | && (j = pIdx->aiColumn[nEq])>=0 |
| 118473 | 119227 | && pIdx->pTable->aCol[j].notNull==0 |
| 118474 | 119228 | ){ |
| 118475 | 119229 | bSeekPastNull = 1; |
| | @@ -118508,10 +119262,11 @@ |
| 118508 | 119262 | /* Seek the index cursor to the start of the range. */ |
| 118509 | 119263 | nConstraint = nEq; |
| 118510 | 119264 | if( pRangeStart ){ |
| 118511 | 119265 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 118512 | 119266 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 119267 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
| 118513 | 119268 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 118514 | 119269 | && sqlite3ExprCanBeNull(pRight) |
| 118515 | 119270 | ){ |
| 118516 | 119271 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118517 | 119272 | VdbeCoverage(v); |
| | @@ -118553,10 +119308,11 @@ |
| 118553 | 119308 | nConstraint = nEq; |
| 118554 | 119309 | if( pRangeEnd ){ |
| 118555 | 119310 | Expr *pRight = pRangeEnd->pExpr->pRight; |
| 118556 | 119311 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 118557 | 119312 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 119313 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 118558 | 119314 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 118559 | 119315 | && sqlite3ExprCanBeNull(pRight) |
| 118560 | 119316 | ){ |
| 118561 | 119317 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118562 | 119318 | VdbeCoverage(v); |
| | @@ -118780,11 +119536,12 @@ |
| 118780 | 119536 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 118781 | 119537 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 118782 | 119538 | */ |
| 118783 | 119539 | wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 118784 | 119540 | | WHERE_FORCE_TABLE |
| 118785 | | - | WHERE_ONETABLE_ONLY; |
| 119541 | + | WHERE_ONETABLE_ONLY |
| 119542 | + | WHERE_NO_AUTOINDEX; |
| 118786 | 119543 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 118787 | 119544 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 118788 | 119545 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 118789 | 119546 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 118790 | 119547 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| | @@ -118942,10 +119699,11 @@ |
| 118942 | 119699 | /* Insert code to test every subexpression that can be completely |
| 118943 | 119700 | ** computed using the current set of tables. |
| 118944 | 119701 | */ |
| 118945 | 119702 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 118946 | 119703 | Expr *pE; |
| 119704 | + int skipLikeAddr = 0; |
| 118947 | 119705 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 118948 | 119706 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 118949 | 119707 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 118950 | 119708 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 118951 | 119709 | testcase( pWInfo->untestedTerms==0 |
| | @@ -118955,12 +119713,18 @@ |
| 118955 | 119713 | } |
| 118956 | 119714 | pE = pTerm->pExpr; |
| 118957 | 119715 | assert( pE!=0 ); |
| 118958 | 119716 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 118959 | 119717 | continue; |
| 119718 | + } |
| 119719 | + if( pTerm->wtFlags & TERM_LIKECOND ){ |
| 119720 | + assert( pLevel->iLikeRepCntr>0 ); |
| 119721 | + skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); |
| 119722 | + VdbeCoverage(v); |
| 118960 | 119723 | } |
| 118961 | 119724 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
| 119725 | + if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
| 118962 | 119726 | pTerm->wtFlags |= TERM_CODED; |
| 118963 | 119727 | } |
| 118964 | 119728 | |
| 118965 | 119729 | /* Insert code to test for implied constraints based on transitivity |
| 118966 | 119730 | ** of the "==" operator. |
| | @@ -119621,10 +120385,14 @@ |
| 119621 | 120385 | ){ |
| 119622 | 120386 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 119623 | 120387 | } |
| 119624 | 120388 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 119625 | 120389 | |
| 120390 | + /* Do not allow the upper bound of a LIKE optimization range constraint |
| 120391 | + ** to mix with a lower range bound from some other source */ |
| 120392 | + if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; |
| 120393 | + |
| 119626 | 120394 | pNew->wsFlags = saved_wsFlags; |
| 119627 | 120395 | pNew->u.btree.nEq = saved_nEq; |
| 119628 | 120396 | pNew->nLTerm = saved_nLTerm; |
| 119629 | 120397 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 119630 | 120398 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| | @@ -119664,10 +120432,21 @@ |
| 119664 | 120432 | testcase( eOp & WO_GT ); |
| 119665 | 120433 | testcase( eOp & WO_GE ); |
| 119666 | 120434 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 119667 | 120435 | pBtm = pTerm; |
| 119668 | 120436 | pTop = 0; |
| 120437 | + if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 120438 | + /* Range contraints that come from the LIKE optimization are |
| 120439 | + ** always used in pairs. */ |
| 120440 | + pTop = &pTerm[1]; |
| 120441 | + assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 120442 | + assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 120443 | + assert( pTop->eOperator==WO_LT ); |
| 120444 | + if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 120445 | + pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 120446 | + pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 120447 | + } |
| 119669 | 120448 | }else{ |
| 119670 | 120449 | assert( eOp & (WO_LT|WO_LE) ); |
| 119671 | 120450 | testcase( eOp & WO_LT ); |
| 119672 | 120451 | testcase( eOp & WO_LE ); |
| 119673 | 120452 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| | @@ -119974,10 +120753,11 @@ |
| 119974 | 120753 | rLogSize = estLog(rSize); |
| 119975 | 120754 | |
| 119976 | 120755 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 119977 | 120756 | /* Automatic indexes */ |
| 119978 | 120757 | if( !pBuilder->pOrSet |
| 120758 | + && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 |
| 119979 | 120759 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 119980 | 120760 | && pSrc->pIndex==0 |
| 119981 | 120761 | && !pSrc->viaCoroutine |
| 119982 | 120762 | && !pSrc->notIndexed |
| 119983 | 120763 | && HasRowid(pTab) |
| | @@ -120857,14 +121637,14 @@ |
| 120857 | 121637 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 120858 | 121638 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
| 120859 | 121639 | |
| 120860 | 121640 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 120861 | 121641 | ** |
| 120862 | | - ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 120863 | | - ** of computing an automatic index is not paid back within the first 25 |
| 121642 | + ** TUNING: Do not let the number of iterations go above 28. If the cost |
| 121643 | + ** of computing an automatic index is not paid back within the first 28 |
| 120864 | 121644 | ** rows, then do not use the automatic index. */ |
| 120865 | | - aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) ); |
| 121645 | + aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); |
| 120866 | 121646 | nFrom = 1; |
| 120867 | 121647 | assert( aFrom[0].isOrdered==0 ); |
| 120868 | 121648 | if( nOrderBy ){ |
| 120869 | 121649 | /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 120870 | 121650 | ** in this case the query may return a maximum of one row, the results |
| | @@ -121658,10 +122438,16 @@ |
| 121658 | 122438 | assert( pIx->pSchema==pTab->pSchema ); |
| 121659 | 122439 | assert( iIndexCur>=0 ); |
| 121660 | 122440 | if( op ){ |
| 121661 | 122441 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 121662 | 122442 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
| 122443 | + if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 |
| 122444 | + && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 |
| 122445 | + && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 |
| 122446 | + ){ |
| 122447 | + sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ |
| 122448 | + } |
| 121663 | 122449 | VdbeComment((v, "%s", pIx->zName)); |
| 121664 | 122450 | } |
| 121665 | 122451 | } |
| 121666 | 122452 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
| 121667 | 122453 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
| | @@ -121758,10 +122544,20 @@ |
| 121758 | 122544 | if( pLevel->addrSkip ){ |
| 121759 | 122545 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
| 121760 | 122546 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
| 121761 | 122547 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 121762 | 122548 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
| 122549 | + } |
| 122550 | + if( pLevel->addrLikeRep ){ |
| 122551 | + int op; |
| 122552 | + if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){ |
| 122553 | + op = OP_DecrJumpZero; |
| 122554 | + }else{ |
| 122555 | + op = OP_JumpZeroIncr; |
| 122556 | + } |
| 122557 | + sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep); |
| 122558 | + VdbeCoverage(v); |
| 121763 | 122559 | } |
| 121764 | 122560 | if( pLevel->iLeftJoin ){ |
| 121765 | 122561 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
| 121766 | 122562 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 121767 | 122563 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| | @@ -124649,11 +125445,11 @@ |
| 124649 | 125445 | spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); |
| 124650 | 125446 | } |
| 124651 | 125447 | break; |
| 124652 | 125448 | case 193: /* expr ::= expr COLLATE ID|STRING */ |
| 124653 | 125449 | { |
| 124654 | | - yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0); |
| 125450 | + yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0, 1); |
| 124655 | 125451 | yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart; |
| 124656 | 125452 | yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 124657 | 125453 | } |
| 124658 | 125454 | break; |
| 124659 | 125455 | case 194: /* expr ::= CAST LP expr AS typetoken RP */ |
| | @@ -124929,20 +125725,20 @@ |
| 124929 | 125725 | case 241: /* uniqueflag ::= */ |
| 124930 | 125726 | {yygotominor.yy328 = OE_None;} |
| 124931 | 125727 | break; |
| 124932 | 125728 | case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */ |
| 124933 | 125729 | { |
| 124934 | | - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0); |
| 125730 | + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1); |
| 124935 | 125731 | yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p); |
| 124936 | 125732 | sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1); |
| 124937 | 125733 | sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index"); |
| 124938 | 125734 | if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328; |
| 124939 | 125735 | } |
| 124940 | 125736 | break; |
| 124941 | 125737 | case 245: /* idxlist ::= nm collate sortorder */ |
| 124942 | 125738 | { |
| 124943 | | - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0); |
| 125739 | + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1); |
| 124944 | 125740 | yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p); |
| 124945 | 125741 | sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1); |
| 124946 | 125742 | sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index"); |
| 124947 | 125743 | if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328; |
| 124948 | 125744 | } |
| | @@ -126197,13 +126993,15 @@ |
| 126197 | 126993 | pParse->zTail = &zSql[i]; |
| 126198 | 126994 | } |
| 126199 | 126995 | sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
| 126200 | 126996 | } |
| 126201 | 126997 | #ifdef YYTRACKMAXSTACKDEPTH |
| 126998 | + sqlite3_mutex_enter(sqlite3MallocMutex()); |
| 126202 | 126999 | sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, |
| 126203 | 127000 | sqlite3ParserStackPeak(pEngine) |
| 126204 | 127001 | ); |
| 127002 | + sqlite3_mutex_leave(sqlite3MallocMutex()); |
| 126205 | 127003 | #endif /* YYDEBUG */ |
| 126206 | 127004 | sqlite3ParserFree(pEngine, sqlite3_free); |
| 126207 | 127005 | db->lookaside.bEnabled = enableLookaside; |
| 126208 | 127006 | if( db->mallocFailed ){ |
| 126209 | 127007 | pParse->rc = SQLITE_NOMEM; |
| | @@ -126769,10 +127567,15 @@ |
| 126769 | 127567 | rc = sqlite3_wsd_init(4096, 24); |
| 126770 | 127568 | if( rc!=SQLITE_OK ){ |
| 126771 | 127569 | return rc; |
| 126772 | 127570 | } |
| 126773 | 127571 | #endif |
| 127572 | + |
| 127573 | + /* If the following assert() fails on some obscure processor/compiler |
| 127574 | + ** combination, the work-around is to set the correct pointer |
| 127575 | + ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ |
| 127576 | + assert( SQLITE_PTRSIZE==sizeof(char*) ); |
| 126774 | 127577 | |
| 126775 | 127578 | /* If SQLite is already completely initialized, then this call |
| 126776 | 127579 | ** to sqlite3_initialize() should be a no-op. But the initialization |
| 126777 | 127580 | ** must be complete. So isInit must not be set until the very end |
| 126778 | 127581 | ** of this routine. |
| | @@ -126982,30 +127785,32 @@ |
| 126982 | 127785 | /* Mutex configuration options are only available in a threadsafe |
| 126983 | 127786 | ** compile. |
| 126984 | 127787 | */ |
| 126985 | 127788 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ |
| 126986 | 127789 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 126987 | | - /* Disable all mutexing */ |
| 126988 | | - sqlite3GlobalConfig.bCoreMutex = 0; |
| 126989 | | - sqlite3GlobalConfig.bFullMutex = 0; |
| 127790 | + /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to |
| 127791 | + ** Single-thread. */ |
| 127792 | + sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */ |
| 127793 | + sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ |
| 126990 | 127794 | break; |
| 126991 | 127795 | } |
| 126992 | 127796 | #endif |
| 126993 | 127797 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ |
| 126994 | 127798 | case SQLITE_CONFIG_MULTITHREAD: { |
| 126995 | | - /* Disable mutexing of database connections */ |
| 126996 | | - /* Enable mutexing of core data structures */ |
| 126997 | | - sqlite3GlobalConfig.bCoreMutex = 1; |
| 126998 | | - sqlite3GlobalConfig.bFullMutex = 0; |
| 127799 | + /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to |
| 127800 | + ** Multi-thread. */ |
| 127801 | + sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ |
| 127802 | + sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ |
| 126999 | 127803 | break; |
| 127000 | 127804 | } |
| 127001 | 127805 | #endif |
| 127002 | 127806 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ |
| 127003 | 127807 | case SQLITE_CONFIG_SERIALIZED: { |
| 127004 | | - /* Enable all mutexing */ |
| 127005 | | - sqlite3GlobalConfig.bCoreMutex = 1; |
| 127006 | | - sqlite3GlobalConfig.bFullMutex = 1; |
| 127808 | + /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to |
| 127809 | + ** Serialized. */ |
| 127810 | + sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ |
| 127811 | + sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */ |
| 127007 | 127812 | break; |
| 127008 | 127813 | } |
| 127009 | 127814 | #endif |
| 127010 | 127815 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ |
| 127011 | 127816 | case SQLITE_CONFIG_MUTEX: { |
| | @@ -127113,11 +127918,12 @@ |
| 127113 | 127918 | ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ |
| 127114 | 127919 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 127115 | 127920 | case SQLITE_CONFIG_HEAP: { |
| 127116 | 127921 | /* EVIDENCE-OF: R-19854-42126 There are three arguments to |
| 127117 | 127922 | ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the |
| 127118 | | - ** number of bytes in the memory buffer, and the minimum allocation size. */ |
| 127923 | + ** number of bytes in the memory buffer, and the minimum allocation size. |
| 127924 | + */ |
| 127119 | 127925 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 127120 | 127926 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 127121 | 127927 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 127122 | 127928 | |
| 127123 | 127929 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| | @@ -127218,11 +128024,13 @@ |
| 127218 | 128024 | ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be |
| 127219 | 128025 | ** silently truncated if necessary so that it does not exceed the |
| 127220 | 128026 | ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE |
| 127221 | 128027 | ** compile-time option. |
| 127222 | 128028 | */ |
| 127223 | | - if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ) mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 128029 | + if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ |
| 128030 | + mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 128031 | + } |
| 127224 | 128032 | if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 127225 | 128033 | if( szMmap>mxMmap) szMmap = mxMmap; |
| 127226 | 128034 | sqlite3GlobalConfig.mxMmap = mxMmap; |
| 127227 | 128035 | sqlite3GlobalConfig.szMmap = szMmap; |
| 127228 | 128036 | break; |
| | @@ -129062,11 +129870,23 @@ |
| 129062 | 129870 | for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); |
| 129063 | 129871 | zFile = sqlite3_malloc(nByte); |
| 129064 | 129872 | if( !zFile ) return SQLITE_NOMEM; |
| 129065 | 129873 | |
| 129066 | 129874 | iIn = 5; |
| 129067 | | -#ifndef SQLITE_ALLOW_URI_AUTHORITY |
| 129875 | +#ifdef SQLITE_ALLOW_URI_AUTHORITY |
| 129876 | + if( strncmp(zUri+5, "///", 3)==0 ){ |
| 129877 | + iIn = 7; |
| 129878 | + /* The following condition causes URIs with five leading / characters |
| 129879 | + ** like file://///host/path to be converted into UNCs like //host/path. |
| 129880 | + ** The correct URI for that UNC has only two or four leading / characters |
| 129881 | + ** file://host/path or file:////host/path. But 5 leading slashes is a |
| 129882 | + ** common error, we are told, so we handle it as a special case. */ |
| 129883 | + if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; } |
| 129884 | + }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){ |
| 129885 | + iIn = 16; |
| 129886 | + } |
| 129887 | +#else |
| 129068 | 129888 | /* Discard the scheme and authority segments of the URI. */ |
| 129069 | 129889 | if( zUri[5]=='/' && zUri[6]=='/' ){ |
| 129070 | 129890 | iIn = 7; |
| 129071 | 129891 | while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; |
| 129072 | 129892 | if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ |
| | @@ -129505,11 +130325,12 @@ |
| 129505 | 130325 | sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); |
| 129506 | 130326 | |
| 129507 | 130327 | opendb_out: |
| 129508 | 130328 | sqlite3_free(zOpen); |
| 129509 | 130329 | if( db ){ |
| 129510 | | - assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
| 130330 | + assert( db->mutex!=0 || isThreadsafe==0 |
| 130331 | + || sqlite3GlobalConfig.bFullMutex==0 ); |
| 129511 | 130332 | sqlite3_mutex_leave(db->mutex); |
| 129512 | 130333 | } |
| 129513 | 130334 | rc = sqlite3_errcode(db); |
| 129514 | 130335 | assert( db!=0 || rc==SQLITE_NOMEM ); |
| 129515 | 130336 | if( rc==SQLITE_NOMEM ){ |
| | @@ -130250,21 +131071,21 @@ |
| 130250 | 131071 | case SQLITE_TESTCTRL_ISINIT: { |
| 130251 | 131072 | if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; |
| 130252 | 131073 | break; |
| 130253 | 131074 | } |
| 130254 | 131075 | |
| 130255 | | - /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); |
| 131076 | + /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); |
| 130256 | 131077 | ** |
| 130257 | 131078 | ** This test control is used to create imposter tables. "db" is a pointer |
| 130258 | 131079 | ** to the database connection. dbName is the database name (ex: "main" or |
| 130259 | 131080 | ** "temp") which will receive the imposter. "onOff" turns imposter mode on |
| 130260 | 131081 | ** or off. "tnum" is the root page of the b-tree to which the imposter |
| 130261 | 131082 | ** table should connect. |
| 130262 | 131083 | ** |
| 130263 | 131084 | ** Enable imposter mode only when the schema has already been parsed. Then |
| 130264 | | - ** run a single CREATE TABLE statement to construct the imposter table in the |
| 130265 | | - ** parsed schema. Then turn imposter mode back off again. |
| 131085 | + ** run a single CREATE TABLE statement to construct the imposter table in |
| 131086 | + ** the parsed schema. Then turn imposter mode back off again. |
| 130266 | 131087 | ** |
| 130267 | 131088 | ** If onOff==0 and tnum>0 then reset the schema for all databases, causing |
| 130268 | 131089 | ** the schema to be reparsed the next time it is needed. This has the |
| 130269 | 131090 | ** effect of erasing all imposter tables. |
| 130270 | 131091 | */ |
| | @@ -132506,15 +133327,20 @@ |
| 132506 | 133327 | ** the output value undefined. Otherwise SQLITE_OK is returned. |
| 132507 | 133328 | ** |
| 132508 | 133329 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 132509 | 133330 | */ |
| 132510 | 133331 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 133332 | + const int MAX_NPREFIX = 10000000; |
| 132511 | 133333 | const char *p; /* Iterator pointer */ |
| 132512 | 133334 | int nInt = 0; /* Output value */ |
| 132513 | 133335 | |
| 132514 | 133336 | for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 132515 | 133337 | nInt = nInt * 10 + (p[0] - '0'); |
| 133338 | + if( nInt>MAX_NPREFIX ){ |
| 133339 | + nInt = 0; |
| 133340 | + break; |
| 133341 | + } |
| 132516 | 133342 | } |
| 132517 | 133343 | if( p==*pp ) return SQLITE_ERROR; |
| 132518 | 133344 | *pnOut = nInt; |
| 132519 | 133345 | *pp = p; |
| 132520 | 133346 | return SQLITE_OK; |
| | @@ -132553,27 +133379,33 @@ |
| 132553 | 133379 | } |
| 132554 | 133380 | } |
| 132555 | 133381 | |
| 132556 | 133382 | aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex); |
| 132557 | 133383 | *apIndex = aIndex; |
| 132558 | | - *pnIndex = nIndex; |
| 132559 | 133384 | if( !aIndex ){ |
| 132560 | 133385 | return SQLITE_NOMEM; |
| 132561 | 133386 | } |
| 132562 | 133387 | |
| 132563 | 133388 | memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex); |
| 132564 | 133389 | if( zParam ){ |
| 132565 | 133390 | const char *p = zParam; |
| 132566 | 133391 | int i; |
| 132567 | 133392 | for(i=1; i<nIndex; i++){ |
| 132568 | | - int nPrefix; |
| 133393 | + int nPrefix = 0; |
| 132569 | 133394 | if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR; |
| 132570 | | - aIndex[i].nPrefix = nPrefix; |
| 133395 | + assert( nPrefix>=0 ); |
| 133396 | + if( nPrefix==0 ){ |
| 133397 | + nIndex--; |
| 133398 | + i--; |
| 133399 | + }else{ |
| 133400 | + aIndex[i].nPrefix = nPrefix; |
| 133401 | + } |
| 132571 | 133402 | p++; |
| 132572 | 133403 | } |
| 132573 | 133404 | } |
| 132574 | 133405 | |
| 133406 | + *pnIndex = nIndex; |
| 132575 | 133407 | return SQLITE_OK; |
| 132576 | 133408 | } |
| 132577 | 133409 | |
| 132578 | 133410 | /* |
| 132579 | 133411 | ** This function is called when initializing an FTS4 table that uses the |
| | @@ -140349,11 +141181,11 @@ |
| 140349 | 141181 | nName = sqlite3_value_bytes(argv[0])+1; |
| 140350 | 141182 | |
| 140351 | 141183 | if( argc==2 ){ |
| 140352 | 141184 | void *pOld; |
| 140353 | 141185 | int n = sqlite3_value_bytes(argv[1]); |
| 140354 | | - if( n!=sizeof(pPtr) ){ |
| 141186 | + if( zName==0 || n!=sizeof(pPtr) ){ |
| 140355 | 141187 | sqlite3_result_error(context, "argument type mismatch", -1); |
| 140356 | 141188 | return; |
| 140357 | 141189 | } |
| 140358 | 141190 | pPtr = *(void **)sqlite3_value_blob(argv[1]); |
| 140359 | 141191 | pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); |
| | @@ -140360,11 +141192,13 @@ |
| 140360 | 141192 | if( pOld==pPtr ){ |
| 140361 | 141193 | sqlite3_result_error(context, "out of memory", -1); |
| 140362 | 141194 | return; |
| 140363 | 141195 | } |
| 140364 | 141196 | }else{ |
| 140365 | | - pPtr = sqlite3Fts3HashFind(pHash, zName, nName); |
| 141197 | + if( zName ){ |
| 141198 | + pPtr = sqlite3Fts3HashFind(pHash, zName, nName); |
| 141199 | + } |
| 140366 | 141200 | if( !pPtr ){ |
| 140367 | 141201 | char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
| 140368 | 141202 | sqlite3_result_error(context, zErr, -1); |
| 140369 | 141203 | sqlite3_free(zErr); |
| 140370 | 141204 | return; |
| | @@ -140441,10 +141275,14 @@ |
| 140441 | 141275 | zCopy = sqlite3_mprintf("%s", zArg); |
| 140442 | 141276 | if( !zCopy ) return SQLITE_NOMEM; |
| 140443 | 141277 | zEnd = &zCopy[strlen(zCopy)]; |
| 140444 | 141278 | |
| 140445 | 141279 | z = (char *)sqlite3Fts3NextToken(zCopy, &n); |
| 141280 | + if( z==0 ){ |
| 141281 | + assert( n==0 ); |
| 141282 | + z = zCopy; |
| 141283 | + } |
| 140446 | 141284 | z[n] = '\0'; |
| 140447 | 141285 | sqlite3Fts3Dequote(z); |
| 140448 | 141286 | |
| 140449 | 141287 | m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1); |
| 140450 | 141288 | if( !m ){ |
| | @@ -143086,11 +143924,14 @@ |
| 143086 | 143924 | /* |
| 143087 | 143925 | ** This is a comparison function used as a qsort() callback when sorting |
| 143088 | 143926 | ** an array of pending terms by term. This occurs as part of flushing |
| 143089 | 143927 | ** the contents of the pending-terms hash table to the database. |
| 143090 | 143928 | */ |
| 143091 | | -static int fts3CompareElemByTerm(const void *lhs, const void *rhs){ |
| 143929 | +static int SQLITE_CDECL fts3CompareElemByTerm( |
| 143930 | + const void *lhs, |
| 143931 | + const void *rhs |
| 143932 | +){ |
| 143092 | 143933 | char *z1 = fts3HashKey(*(Fts3HashElem **)lhs); |
| 143093 | 143934 | char *z2 = fts3HashKey(*(Fts3HashElem **)rhs); |
| 143094 | 143935 | int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs); |
| 143095 | 143936 | int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs); |
| 143096 | 143937 | |
| 143097 | 143938 | |