| | @@ -261,10 +261,17 @@ |
| 261 | 261 | #ifndef SQLITE_API |
| 262 | 262 | # define SQLITE_API |
| 263 | 263 | #endif |
| 264 | 264 | |
| 265 | 265 | |
| 266 | +/* |
| 267 | +** Add the ability to override 'cdecl' |
| 268 | +*/ |
| 269 | +#ifndef SQLITE_CDECL |
| 270 | +# define SQLITE_CDECL |
| 271 | +#endif |
| 272 | + |
| 266 | 273 | /* |
| 267 | 274 | ** These no-op macros are used in front of interfaces to mark those |
| 268 | 275 | ** interfaces as either deprecated or experimental. New applications |
| 269 | 276 | ** should not use deprecated interfaces - they are supported for backwards |
| 270 | 277 | ** compatibility only. Application writers should be aware that |
| | @@ -316,11 +323,11 @@ |
| 316 | 323 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 317 | 324 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 318 | 325 | */ |
| 319 | 326 | #define SQLITE_VERSION "3.8.9" |
| 320 | 327 | #define SQLITE_VERSION_NUMBER 3008009 |
| 321 | | -#define SQLITE_SOURCE_ID "2015-03-09 10:40:48 e5da5e7d5dc5a3438ced23f1ee83e695abc29c45" |
| 328 | +#define SQLITE_SOURCE_ID "2015-03-24 18:19:39 436314b5728c9413f9ac2d837e1c19364f31be72" |
| 322 | 329 | |
| 323 | 330 | /* |
| 324 | 331 | ** CAPI3REF: Run-Time Library Version Numbers |
| 325 | 332 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 326 | 333 | ** |
| | @@ -1157,10 +1164,17 @@ |
| 1157 | 1164 | ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This |
| 1158 | 1165 | ** opcode causes the xFileControl method to swap the file handle with the one |
| 1159 | 1166 | ** pointed to by the pArg argument. This capability is used during testing |
| 1160 | 1167 | ** and only needs to be supported when SQLITE_TEST is defined. |
| 1161 | 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 | +** |
| 1162 | 1176 | ** </ul> |
| 1163 | 1177 | */ |
| 1164 | 1178 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1165 | 1179 | #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 |
| 1166 | 1180 | #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 |
| | @@ -1181,10 +1195,11 @@ |
| 1181 | 1195 | #define SQLITE_FCNTL_TRACE 19 |
| 1182 | 1196 | #define SQLITE_FCNTL_HAS_MOVED 20 |
| 1183 | 1197 | #define SQLITE_FCNTL_SYNC 21 |
| 1184 | 1198 | #define SQLITE_FCNTL_COMMIT_PHASETWO 22 |
| 1185 | 1199 | #define SQLITE_FCNTL_WIN32_SET_HANDLE 23 |
| 1200 | +#define SQLITE_FCNTL_WAL_BLOCK 24 |
| 1186 | 1201 | |
| 1187 | 1202 | /* deprecated names */ |
| 1188 | 1203 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 1189 | 1204 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 1190 | 1205 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -1747,11 +1762,11 @@ |
| 1747 | 1762 | ** disabled, the following SQLite interfaces become non-operational: |
| 1748 | 1763 | ** <ul> |
| 1749 | 1764 | ** <li> [sqlite3_memory_used()] |
| 1750 | 1765 | ** <li> [sqlite3_memory_highwater()] |
| 1751 | 1766 | ** <li> [sqlite3_soft_heap_limit64()] |
| 1752 | | -** <li> [sqlite3_status()] |
| 1767 | +** <li> [sqlite3_status64()] |
| 1753 | 1768 | ** </ul>)^ |
| 1754 | 1769 | ** ^Memory allocation statistics are enabled by default unless SQLite is |
| 1755 | 1770 | ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory |
| 1756 | 1771 | ** allocation statistics are disabled by default. |
| 1757 | 1772 | ** </dd> |
| | @@ -3204,15 +3219,17 @@ |
| 3204 | 3219 | |
| 3205 | 3220 | |
| 3206 | 3221 | /* |
| 3207 | 3222 | ** CAPI3REF: Error Codes And Messages |
| 3208 | 3223 | ** |
| 3209 | | -** ^The sqlite3_errcode() interface returns the numeric [result code] or |
| 3210 | | -** [extended result code] for the most recent failed sqlite3_* API call |
| 3211 | | -** associated with a [database connection]. If a prior API call failed |
| 3212 | | -** but the most recent API call succeeded, the return value from |
| 3213 | | -** 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() |
| 3214 | 3231 | ** interface is the same except that it always returns the |
| 3215 | 3232 | ** [extended result code] even when extended result codes are |
| 3216 | 3233 | ** disabled. |
| 3217 | 3234 | ** |
| 3218 | 3235 | ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language |
| | @@ -5822,11 +5839,11 @@ |
| 5822 | 5839 | ** is delivered up to the client application, the string will be automatically |
| 5823 | 5840 | ** freed by sqlite3_free() and the zErrMsg field will be zeroed. |
| 5824 | 5841 | */ |
| 5825 | 5842 | struct sqlite3_vtab { |
| 5826 | 5843 | const sqlite3_module *pModule; /* The module for this virtual table */ |
| 5827 | | - int nRef; /* NO LONGER USED */ |
| 5844 | + int nRef; /* Number of open cursors */ |
| 5828 | 5845 | char *zErrMsg; /* Error message from sqlite3_mprintf() */ |
| 5829 | 5846 | /* Virtual table implementations will typically add additional fields */ |
| 5830 | 5847 | }; |
| 5831 | 5848 | |
| 5832 | 5849 | /* |
| | @@ -6500,11 +6517,11 @@ |
| 6500 | 6517 | #define SQLITE_TESTCTRL_LAST 25 |
| 6501 | 6518 | |
| 6502 | 6519 | /* |
| 6503 | 6520 | ** CAPI3REF: SQLite Runtime Status |
| 6504 | 6521 | ** |
| 6505 | | -** ^This interface is used to retrieve runtime status information |
| 6522 | +** ^These interfaces are used to retrieve runtime status information |
| 6506 | 6523 | ** about the performance of SQLite, and optionally to reset various |
| 6507 | 6524 | ** highwater marks. ^The first argument is an integer code for |
| 6508 | 6525 | ** the specific parameter to measure. ^(Recognized integer codes |
| 6509 | 6526 | ** are of the form [status parameters | SQLITE_STATUS_...].)^ |
| 6510 | 6527 | ** ^The current value of the parameter is returned into *pCurrent. |
| | @@ -6514,23 +6531,26 @@ |
| 6514 | 6531 | ** value. For those parameters |
| 6515 | 6532 | ** nothing is written into *pHighwater and the resetFlag is ignored.)^ |
| 6516 | 6533 | ** ^(Other parameters record only the highwater mark and not the current |
| 6517 | 6534 | ** value. For these latter parameters nothing is written into *pCurrent.)^ |
| 6518 | 6535 | ** |
| 6519 | | -** ^The sqlite3_status() routine returns SQLITE_OK on success and a |
| 6520 | | -** 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. |
| 6521 | 6538 | ** |
| 6522 | | -** This routine is threadsafe but is not atomic. This routine can be |
| 6523 | | -** called while other threads are running the same or different SQLite |
| 6524 | | -** interfaces. However the values returned in *pCurrent and |
| 6525 | | -** *pHighwater reflect the status of SQLite at different points in time |
| 6526 | | -** and it is possible that another thread might change the parameter |
| 6527 | | -** 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. |
| 6528 | 6542 | ** |
| 6529 | 6543 | ** See also: [sqlite3_db_status()] |
| 6530 | 6544 | */ |
| 6531 | 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 | +); |
| 6532 | 6552 | |
| 6533 | 6553 | |
| 6534 | 6554 | /* |
| 6535 | 6555 | ** CAPI3REF: Status Parameters |
| 6536 | 6556 | ** KEYWORDS: {status parameters} |
| | @@ -8911,10 +8931,24 @@ |
| 8911 | 8931 | ** |
| 8912 | 8932 | ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40 |
| 8913 | 8933 | */ |
| 8914 | 8934 | typedef INT16_TYPE LogEst; |
| 8915 | 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 | + |
| 8916 | 8950 | /* |
| 8917 | 8951 | ** Macros to determine whether the machine is big or little endian, |
| 8918 | 8952 | ** and whether or not that determination is run-time or compile-time. |
| 8919 | 8953 | ** |
| 8920 | 8954 | ** For best performance, an attempt is made to guess at the byte-order |
| | @@ -9361,12 +9395,22 @@ |
| 9361 | 9395 | #define BTREE_DATA_VERSION 15 /* A virtual meta-value */ |
| 9362 | 9396 | |
| 9363 | 9397 | /* |
| 9364 | 9398 | ** Values that may be OR'd together to form the second argument of an |
| 9365 | 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 | +** |
| 9366 | 9409 | */ |
| 9367 | | -#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 */ |
| 9368 | 9412 | |
| 9369 | 9413 | SQLITE_PRIVATE int sqlite3BtreeCursor( |
| 9370 | 9414 | Btree*, /* BTree containing table to open */ |
| 9371 | 9415 | int iTable, /* Index of root page */ |
| 9372 | 9416 | int wrFlag, /* 1 for writing. 0 for read-only */ |
| | @@ -9408,10 +9452,13 @@ |
| 9408 | 9452 | SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); |
| 9409 | 9453 | SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 9410 | 9454 | SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); |
| 9411 | 9455 | SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 9412 | 9456 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask); |
| 9457 | +#ifdef SQLITE_DEBUG |
| 9458 | +SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); |
| 9459 | +#endif |
| 9413 | 9460 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt); |
| 9414 | 9461 | SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void); |
| 9415 | 9462 | |
| 9416 | 9463 | #ifndef NDEBUG |
| 9417 | 9464 | SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); |
| | @@ -10908,10 +10955,11 @@ |
| 10908 | 10955 | } init; |
| 10909 | 10956 | int nVdbeActive; /* Number of VDBEs currently running */ |
| 10910 | 10957 | int nVdbeRead; /* Number of active VDBEs that read or write */ |
| 10911 | 10958 | int nVdbeWrite; /* Number of active VDBEs that read and write */ |
| 10912 | 10959 | int nVdbeExec; /* Number of nested calls to VdbeExec() */ |
| 10960 | + int nVDestroy; /* Number of active OP_VDestroy operations */ |
| 10913 | 10961 | int nExtension; /* Number of loaded extensions */ |
| 10914 | 10962 | void **aExtension; /* Array of shared library handles */ |
| 10915 | 10963 | void (*xTrace)(void*,const char*); /* Trace function */ |
| 10916 | 10964 | void *pTraceArg; /* Argument to the trace function */ |
| 10917 | 10965 | void (*xProfile)(void*,const char*,u64); /* Profiling function */ |
| | @@ -12502,11 +12550,12 @@ |
| 12502 | 12550 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 12503 | 12551 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 12504 | 12552 | #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ |
| 12505 | 12553 | #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ |
| 12506 | 12554 | #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ |
| 12507 | | -#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 */ |
| 12508 | 12557 | #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ |
| 12509 | 12558 | |
| 12510 | 12559 | /* |
| 12511 | 12560 | * Each trigger present in the database schema is stored as an instance of |
| 12512 | 12561 | * struct Trigger. |
| | @@ -12906,14 +12955,19 @@ |
| 12906 | 12955 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int); |
| 12907 | 12956 | SQLITE_PRIVATE int sqlite3MutexInit(void); |
| 12908 | 12957 | SQLITE_PRIVATE int sqlite3MutexEnd(void); |
| 12909 | 12958 | #endif |
| 12910 | 12959 | |
| 12911 | | -SQLITE_PRIVATE int sqlite3StatusValue(int); |
| 12912 | | -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); |
| 12913 | 12963 | SQLITE_PRIVATE void sqlite3StatusSet(int, int); |
| 12914 | 12964 | |
| 12965 | +/* Access to mutexes used by sqlite3_status() */ |
| 12966 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void); |
| 12967 | +SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void); |
| 12968 | + |
| 12915 | 12969 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 12916 | 12970 | SQLITE_PRIVATE int sqlite3IsNaN(double); |
| 12917 | 12971 | #else |
| 12918 | 12972 | # define sqlite3IsNaN(X) 0 |
| 12919 | 12973 | #endif |
| | @@ -13289,11 +13343,11 @@ |
| 13289 | 13343 | SQLITE_PRIVATE const char *sqlite3ErrStr(int); |
| 13290 | 13344 | SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); |
| 13291 | 13345 | SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); |
| 13292 | 13346 | SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); |
| 13293 | 13347 | SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); |
| 13294 | | -SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*); |
| 13348 | +SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int); |
| 13295 | 13349 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); |
| 13296 | 13350 | SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*); |
| 13297 | 13351 | SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); |
| 13298 | 13352 | SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); |
| 13299 | 13353 | SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); |
| | @@ -14676,18 +14730,10 @@ |
| 14676 | 14730 | ** An instance of the virtual machine. This structure contains the complete |
| 14677 | 14731 | ** state of the virtual machine. |
| 14678 | 14732 | ** |
| 14679 | 14733 | ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare() |
| 14680 | 14734 | ** is really a pointer to an instance of this structure. |
| 14681 | | -** |
| 14682 | | -** The Vdbe.inVtabMethod variable is set to non-zero for the duration of |
| 14683 | | -** any virtual table method invocations made by the vdbe program. It is |
| 14684 | | -** set to 2 for xDestroy method calls and 1 for all other methods. This |
| 14685 | | -** variable is used for two purposes: to allow xDestroy methods to execute |
| 14686 | | -** "DROP TABLE" statements and to prevent some nasty side effects of |
| 14687 | | -** malloc failure when SQLite is invoked recursively by a virtual table |
| 14688 | | -** method function. |
| 14689 | 14735 | */ |
| 14690 | 14736 | struct Vdbe { |
| 14691 | 14737 | sqlite3 *db; /* The database connection that owns this statement */ |
| 14692 | 14738 | Op *aOp; /* Space to hold the virtual machine's program */ |
| 14693 | 14739 | Mem *aMem; /* The memory locations */ |
| | @@ -14714,11 +14760,10 @@ |
| 14714 | 14760 | #endif |
| 14715 | 14761 | u16 nResColumn; /* Number of columns in one row of the result set */ |
| 14716 | 14762 | u8 errorAction; /* Recovery action to do in case of an error */ |
| 14717 | 14763 | u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 14718 | 14764 | bft explain:2; /* True if EXPLAIN present on SQL command */ |
| 14719 | | - bft inVtabMethod:2; /* See comments above */ |
| 14720 | 14765 | bft changeCntOn:1; /* True to update the change-counter */ |
| 14721 | 14766 | bft expired:1; /* True if the VM needs to be recompiled */ |
| 14722 | 14767 | bft runOnlyOnce:1; /* Automatically expire on reset */ |
| 14723 | 14768 | bft usesStmtJournal:1; /* True if uses a statement journal */ |
| 14724 | 14769 | bft readOnly:1; /* True for statements that do not write */ |
| | @@ -14874,13 +14919,35 @@ |
| 14874 | 14919 | /* |
| 14875 | 14920 | ** Variables in which to record status information. |
| 14876 | 14921 | */ |
| 14877 | 14922 | typedef struct sqlite3StatType sqlite3StatType; |
| 14878 | 14923 | static SQLITE_WSD struct sqlite3StatType { |
| 14879 | | - int nowValue[10]; /* Current value */ |
| 14880 | | - 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 |
| 14881 | 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 | +}; |
| 14882 | 14949 | |
| 14883 | 14950 | |
| 14884 | 14951 | /* The "wsdStat" macro will resolve to the status information |
| 14885 | 14952 | ** state vector. If writable static data is unsupported on the target, |
| 14886 | 14953 | ** we have to locate the state vector at run-time. In the more common |
| | @@ -14894,64 +14961,110 @@ |
| 14894 | 14961 | # define wsdStatInit |
| 14895 | 14962 | # define wsdStat sqlite3Stat |
| 14896 | 14963 | #endif |
| 14897 | 14964 | |
| 14898 | 14965 | /* |
| 14899 | | -** 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. |
| 14900 | 14968 | */ |
| 14901 | | -SQLITE_PRIVATE int sqlite3StatusValue(int op){ |
| 14969 | +SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){ |
| 14902 | 14970 | wsdStatInit; |
| 14903 | 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()) ); |
| 14904 | 14975 | return wsdStat.nowValue[op]; |
| 14905 | 14976 | } |
| 14906 | 14977 | |
| 14907 | 14978 | /* |
| 14908 | | -** Add N to the value of a status record. It is assumed that the |
| 14909 | | -** 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(). |
| 14910 | 14988 | */ |
| 14911 | | -SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){ |
| 14989 | +SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){ |
| 14912 | 14990 | wsdStatInit; |
| 14913 | 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()) ); |
| 14914 | 14995 | wsdStat.nowValue[op] += N; |
| 14915 | 14996 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 14916 | 14997 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14917 | 14998 | } |
| 14918 | 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 | +} |
| 14919 | 15009 | |
| 14920 | 15010 | /* |
| 14921 | | -** 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. |
| 14922 | 15013 | */ |
| 14923 | 15014 | SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){ |
| 14924 | 15015 | wsdStatInit; |
| 14925 | 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()) ); |
| 14926 | 15020 | wsdStat.nowValue[op] = X; |
| 14927 | 15021 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 14928 | 15022 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14929 | 15023 | } |
| 14930 | 15024 | } |
| 14931 | 15025 | |
| 14932 | 15026 | /* |
| 14933 | 15027 | ** Query status information. |
| 14934 | | -** |
| 14935 | | -** This implementation assumes that reading or writing an aligned |
| 14936 | | -** 32-bit integer is an atomic operation. If that assumption is not true, |
| 14937 | | -** then this routine is not threadsafe. |
| 14938 | 15028 | */ |
| 14939 | | -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; |
| 14940 | 15036 | wsdStatInit; |
| 14941 | 15037 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 14942 | 15038 | return SQLITE_MISUSE_BKPT; |
| 14943 | 15039 | } |
| 14944 | 15040 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 14945 | 15041 | if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 14946 | 15042 | #endif |
| 15043 | + pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex(); |
| 15044 | + sqlite3_mutex_enter(pMutex); |
| 14947 | 15045 | *pCurrent = wsdStat.nowValue[op]; |
| 14948 | 15046 | *pHighwater = wsdStat.mxValue[op]; |
| 14949 | 15047 | if( resetFlag ){ |
| 14950 | 15048 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 14951 | 15049 | } |
| 15050 | + sqlite3_mutex_leave(pMutex); |
| 15051 | + (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */ |
| 14952 | 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; |
| 14953 | 15066 | } |
| 14954 | 15067 | |
| 14955 | 15068 | /* |
| 14956 | 15069 | ** Query status information for a single database connection |
| 14957 | 15070 | */ |
| | @@ -20401,10 +20514,17 @@ |
| 20401 | 20514 | */ |
| 20402 | 20515 | int nearlyFull; |
| 20403 | 20516 | } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 20404 | 20517 | |
| 20405 | 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 | +} |
| 20406 | 20526 | |
| 20407 | 20527 | /* |
| 20408 | 20528 | ** This routine runs when the memory allocator sees that the |
| 20409 | 20529 | ** total memory allocation is about to exceed the soft heap |
| 20410 | 20530 | ** limit. |
| | @@ -20424,11 +20544,11 @@ |
| 20424 | 20544 | static int sqlite3MemoryAlarm( |
| 20425 | 20545 | void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 20426 | 20546 | void *pArg, |
| 20427 | 20547 | sqlite3_int64 iThreshold |
| 20428 | 20548 | ){ |
| 20429 | | - int nUsed; |
| 20549 | + sqlite3_int64 nUsed; |
| 20430 | 20550 | sqlite3_mutex_enter(mem0.mutex); |
| 20431 | 20551 | mem0.alarmCallback = xCallback; |
| 20432 | 20552 | mem0.alarmArg = pArg; |
| 20433 | 20553 | mem0.alarmThreshold = iThreshold; |
| 20434 | 20554 | nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| | @@ -20593,11 +20713,11 @@ |
| 20593 | 20713 | void *p; |
| 20594 | 20714 | assert( sqlite3_mutex_held(mem0.mutex) ); |
| 20595 | 20715 | nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 20596 | 20716 | sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); |
| 20597 | 20717 | if( mem0.alarmCallback!=0 ){ |
| 20598 | | - int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 20718 | + sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 20599 | 20719 | if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 20600 | 20720 | mem0.nearlyFull = 1; |
| 20601 | 20721 | sqlite3MallocAlarm(nFull); |
| 20602 | 20722 | }else{ |
| 20603 | 20723 | mem0.nearlyFull = 0; |
| | @@ -20610,12 +20730,12 @@ |
| 20610 | 20730 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 20611 | 20731 | } |
| 20612 | 20732 | #endif |
| 20613 | 20733 | if( p ){ |
| 20614 | 20734 | nFull = sqlite3MallocSize(p); |
| 20615 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 20616 | | - sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20735 | + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); |
| 20736 | + sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20617 | 20737 | } |
| 20618 | 20738 | *pp = p; |
| 20619 | 20739 | return nFull; |
| 20620 | 20740 | } |
| 20621 | 20741 | |
| | @@ -20688,18 +20808,18 @@ |
| 20688 | 20808 | sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 20689 | 20809 | if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ |
| 20690 | 20810 | p = mem0.pScratchFree; |
| 20691 | 20811 | mem0.pScratchFree = mem0.pScratchFree->pNext; |
| 20692 | 20812 | mem0.nScratchFree--; |
| 20693 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20813 | + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20694 | 20814 | sqlite3_mutex_leave(mem0.mutex); |
| 20695 | 20815 | }else{ |
| 20696 | 20816 | sqlite3_mutex_leave(mem0.mutex); |
| 20697 | 20817 | p = sqlite3Malloc(n); |
| 20698 | 20818 | if( sqlite3GlobalConfig.bMemstat && p ){ |
| 20699 | 20819 | sqlite3_mutex_enter(mem0.mutex); |
| 20700 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
| 20820 | + sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
| 20701 | 20821 | sqlite3_mutex_leave(mem0.mutex); |
| 20702 | 20822 | } |
| 20703 | 20823 | sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 20704 | 20824 | } |
| 20705 | 20825 | assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| | @@ -20736,23 +20856,23 @@ |
| 20736 | 20856 | sqlite3_mutex_enter(mem0.mutex); |
| 20737 | 20857 | pSlot->pNext = mem0.pScratchFree; |
| 20738 | 20858 | mem0.pScratchFree = pSlot; |
| 20739 | 20859 | mem0.nScratchFree++; |
| 20740 | 20860 | assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); |
| 20741 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 20861 | + sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); |
| 20742 | 20862 | sqlite3_mutex_leave(mem0.mutex); |
| 20743 | 20863 | }else{ |
| 20744 | 20864 | /* Release memory back to the heap */ |
| 20745 | 20865 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 20746 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 20866 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); |
| 20747 | 20867 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20748 | 20868 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20749 | 20869 | int iSize = sqlite3MallocSize(p); |
| 20750 | 20870 | sqlite3_mutex_enter(mem0.mutex); |
| 20751 | | - sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 20752 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 20753 | | - 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); |
| 20754 | 20874 | sqlite3GlobalConfig.m.xFree(p); |
| 20755 | 20875 | sqlite3_mutex_leave(mem0.mutex); |
| 20756 | 20876 | }else{ |
| 20757 | 20877 | sqlite3GlobalConfig.m.xFree(p); |
| 20758 | 20878 | } |
| | @@ -20779,26 +20899,26 @@ |
| 20779 | 20899 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20780 | 20900 | return sqlite3GlobalConfig.m.xSize(p); |
| 20781 | 20901 | } |
| 20782 | 20902 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 20783 | 20903 | if( db==0 ){ |
| 20784 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20904 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20785 | 20905 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20786 | 20906 | return sqlite3MallocSize(p); |
| 20787 | 20907 | }else{ |
| 20788 | 20908 | assert( sqlite3_mutex_held(db->mutex) ); |
| 20789 | 20909 | if( isLookaside(db, p) ){ |
| 20790 | 20910 | return db->lookaside.sz; |
| 20791 | 20911 | }else{ |
| 20792 | 20912 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20793 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20913 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20794 | 20914 | return sqlite3GlobalConfig.m.xSize(p); |
| 20795 | 20915 | } |
| 20796 | 20916 | } |
| 20797 | 20917 | } |
| 20798 | 20918 | SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){ |
| 20799 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20919 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20800 | 20920 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20801 | 20921 | return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); |
| 20802 | 20922 | } |
| 20803 | 20923 | |
| 20804 | 20924 | /* |
| | @@ -20805,15 +20925,15 @@ |
| 20805 | 20925 | ** Free memory previously obtained from sqlite3Malloc(). |
| 20806 | 20926 | */ |
| 20807 | 20927 | SQLITE_API void sqlite3_free(void *p){ |
| 20808 | 20928 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 20809 | 20929 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20810 | | - assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20930 | + assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 20811 | 20931 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20812 | 20932 | sqlite3_mutex_enter(mem0.mutex); |
| 20813 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 20814 | | - sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 20933 | + sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); |
| 20934 | + sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 20815 | 20935 | sqlite3GlobalConfig.m.xFree(p); |
| 20816 | 20936 | sqlite3_mutex_leave(mem0.mutex); |
| 20817 | 20937 | }else{ |
| 20818 | 20938 | sqlite3GlobalConfig.m.xFree(p); |
| 20819 | 20939 | } |
| | @@ -20850,11 +20970,11 @@ |
| 20850 | 20970 | db->lookaside.nOut--; |
| 20851 | 20971 | return; |
| 20852 | 20972 | } |
| 20853 | 20973 | } |
| 20854 | 20974 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20855 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20975 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20856 | 20976 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20857 | 20977 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20858 | 20978 | sqlite3_free(p); |
| 20859 | 20979 | } |
| 20860 | 20980 | |
| | @@ -20863,11 +20983,11 @@ |
| 20863 | 20983 | */ |
| 20864 | 20984 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 20865 | 20985 | int nOld, nNew, nDiff; |
| 20866 | 20986 | void *pNew; |
| 20867 | 20987 | assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20868 | | - assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20988 | + assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); |
| 20869 | 20989 | if( pOld==0 ){ |
| 20870 | 20990 | return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 20871 | 20991 | } |
| 20872 | 20992 | if( nBytes==0 ){ |
| 20873 | 20993 | sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| | @@ -20897,11 +21017,11 @@ |
| 20897 | 21017 | sqlite3MallocAlarm((int)nBytes); |
| 20898 | 21018 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20899 | 21019 | } |
| 20900 | 21020 | if( pNew ){ |
| 20901 | 21021 | nNew = sqlite3MallocSize(pNew); |
| 20902 | | - sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 21022 | + sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 20903 | 21023 | } |
| 20904 | 21024 | sqlite3_mutex_leave(mem0.mutex); |
| 20905 | 21025 | }else{ |
| 20906 | 21026 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20907 | 21027 | } |
| | @@ -21030,11 +21150,11 @@ |
| 21030 | 21150 | memcpy(pNew, p, db->lookaside.sz); |
| 21031 | 21151 | sqlite3DbFree(db, p); |
| 21032 | 21152 | } |
| 21033 | 21153 | }else{ |
| 21034 | 21154 | assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 21035 | | - assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 21155 | + assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 21036 | 21156 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 21037 | 21157 | pNew = sqlite3_realloc64(p, n); |
| 21038 | 21158 | if( !pNew ){ |
| 21039 | 21159 | db->mallocFailed = 1; |
| 21040 | 21160 | } |
| | @@ -25287,10 +25407,11 @@ |
| 25287 | 25407 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| 25288 | 25408 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 25289 | 25409 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 25290 | 25410 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
| 25291 | 25411 | #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings issued */ |
| 25412 | +#define UNIXFILE_BLOCK 0x0200 /* Next SHM lock might block */ |
| 25292 | 25413 | |
| 25293 | 25414 | /* |
| 25294 | 25415 | ** Include code that is common to all os_*.c files |
| 25295 | 25416 | */ |
| 25296 | 25417 | /************** Include os_common.h in the middle of os_unix.c ***************/ |
| | @@ -26782,11 +26903,11 @@ |
| 26782 | 26903 | |
| 26783 | 26904 | assert( pFile ); |
| 26784 | 26905 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 26785 | 26906 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 26786 | 26907 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
| 26787 | | - osGetpid())); |
| 26908 | + osGetpid(0))); |
| 26788 | 26909 | |
| 26789 | 26910 | /* If there is already a lock of this type or more restrictive on the |
| 26790 | 26911 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
| 26791 | 26912 | ** unixEnterMutex() hasn't been called yet. |
| 26792 | 26913 | */ |
| | @@ -26990,11 +27111,11 @@ |
| 26990 | 27111 | int rc = SQLITE_OK; |
| 26991 | 27112 | |
| 26992 | 27113 | assert( pFile ); |
| 26993 | 27114 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
| 26994 | 27115 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 26995 | | - osGetpid())); |
| 27116 | + osGetpid(0))); |
| 26996 | 27117 | |
| 26997 | 27118 | assert( eFileLock<=SHARED_LOCK ); |
| 26998 | 27119 | if( pFile->eFileLock<=eFileLock ){ |
| 26999 | 27120 | return SQLITE_OK; |
| 27000 | 27121 | } |
| | @@ -27417,11 +27538,11 @@ |
| 27417 | 27538 | char *zLockFile = (char *)pFile->lockingContext; |
| 27418 | 27539 | int rc; |
| 27419 | 27540 | |
| 27420 | 27541 | assert( pFile ); |
| 27421 | 27542 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 27422 | | - pFile->eFileLock, osGetpid())); |
| 27543 | + pFile->eFileLock, osGetpid(0))); |
| 27423 | 27544 | assert( eFileLock<=SHARED_LOCK ); |
| 27424 | 27545 | |
| 27425 | 27546 | /* no-op if possible */ |
| 27426 | 27547 | if( pFile->eFileLock==eFileLock ){ |
| 27427 | 27548 | return SQLITE_OK; |
| | @@ -27635,11 +27756,11 @@ |
| 27635 | 27756 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
| 27636 | 27757 | unixFile *pFile = (unixFile*)id; |
| 27637 | 27758 | |
| 27638 | 27759 | assert( pFile ); |
| 27639 | 27760 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 27640 | | - pFile->eFileLock, osGetpid())); |
| 27761 | + pFile->eFileLock, osGetpid(0))); |
| 27641 | 27762 | assert( eFileLock<=SHARED_LOCK ); |
| 27642 | 27763 | |
| 27643 | 27764 | /* no-op if possible */ |
| 27644 | 27765 | if( pFile->eFileLock==eFileLock ){ |
| 27645 | 27766 | return SQLITE_OK; |
| | @@ -27803,11 +27924,11 @@ |
| 27803 | 27924 | sem_t *pSem = pFile->pInode->pSem; |
| 27804 | 27925 | |
| 27805 | 27926 | assert( pFile ); |
| 27806 | 27927 | assert( pSem ); |
| 27807 | 27928 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 27808 | | - pFile->eFileLock, osGetpid())); |
| 27929 | + pFile->eFileLock, osGetpid(0))); |
| 27809 | 27930 | assert( eFileLock<=SHARED_LOCK ); |
| 27810 | 27931 | |
| 27811 | 27932 | /* no-op if possible */ |
| 27812 | 27933 | if( pFile->eFileLock==eFileLock ){ |
| 27813 | 27934 | return SQLITE_OK; |
| | @@ -28017,11 +28138,11 @@ |
| 28017 | 28138 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 28018 | 28139 | |
| 28019 | 28140 | assert( pFile ); |
| 28020 | 28141 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 28021 | 28142 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 28022 | | - azFileLock(pInode->eFileLock), pInode->nShared , osGetpid())); |
| 28143 | + azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
| 28023 | 28144 | |
| 28024 | 28145 | /* If there is already a lock of this type or more restrictive on the |
| 28025 | 28146 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
| 28026 | 28147 | ** unixEnterMutex() hasn't been called yet. |
| 28027 | 28148 | */ |
| | @@ -28203,11 +28324,11 @@ |
| 28203 | 28324 | #endif |
| 28204 | 28325 | |
| 28205 | 28326 | assert( pFile ); |
| 28206 | 28327 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
| 28207 | 28328 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 28208 | | - osGetpid())); |
| 28329 | + osGetpid(0))); |
| 28209 | 28330 | |
| 28210 | 28331 | assert( eFileLock<=SHARED_LOCK ); |
| 28211 | 28332 | if( pFile->eFileLock<=eFileLock ){ |
| 28212 | 28333 | return SQLITE_OK; |
| 28213 | 28334 | } |
| | @@ -29028,10 +29149,14 @@ |
| 29028 | 29149 | ** Information and control of an open file handle. |
| 29029 | 29150 | */ |
| 29030 | 29151 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| 29031 | 29152 | unixFile *pFile = (unixFile*)id; |
| 29032 | 29153 | switch( op ){ |
| 29154 | + case SQLITE_FCNTL_WAL_BLOCK: { |
| 29155 | + pFile->ctrlFlags |= UNIXFILE_BLOCK; |
| 29156 | + return SQLITE_OK; |
| 29157 | + } |
| 29033 | 29158 | case SQLITE_FCNTL_LOCKSTATE: { |
| 29034 | 29159 | *(int*)pArg = pFile->eFileLock; |
| 29035 | 29160 | return SQLITE_OK; |
| 29036 | 29161 | } |
| 29037 | 29162 | case SQLITE_FCNTL_LAST_ERRNO: { |
| | @@ -29337,37 +29462,42 @@ |
| 29337 | 29462 | ** |
| 29338 | 29463 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 29339 | 29464 | ** otherwise. |
| 29340 | 29465 | */ |
| 29341 | 29466 | static int unixShmSystemLock( |
| 29342 | | - unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 29467 | + unixFile *pFile, /* Open connection to the WAL file */ |
| 29343 | 29468 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
| 29344 | 29469 | int ofst, /* First byte of the locking range */ |
| 29345 | 29470 | int n /* Number of bytes to lock */ |
| 29346 | 29471 | ){ |
| 29347 | | - struct flock f; /* The posix advisory locking structure */ |
| 29348 | | - 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() */ |
| 29349 | 29475 | |
| 29350 | 29476 | /* Access to the unixShmNode object is serialized by the caller */ |
| 29477 | + pShmNode = pFile->pInode->pShmNode; |
| 29351 | 29478 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
| 29352 | 29479 | |
| 29353 | 29480 | /* Shared locks never span more than one byte */ |
| 29354 | 29481 | assert( n==1 || lockType!=F_RDLCK ); |
| 29355 | 29482 | |
| 29356 | 29483 | /* Locks are within range */ |
| 29357 | 29484 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
| 29358 | 29485 | |
| 29359 | 29486 | if( pShmNode->h>=0 ){ |
| 29487 | + int lkType; |
| 29360 | 29488 | /* Initialize the locking parameters */ |
| 29361 | 29489 | memset(&f, 0, sizeof(f)); |
| 29362 | 29490 | f.l_type = lockType; |
| 29363 | 29491 | f.l_whence = SEEK_SET; |
| 29364 | 29492 | f.l_start = ofst; |
| 29365 | 29493 | f.l_len = n; |
| 29366 | 29494 | |
| 29367 | | - 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); |
| 29368 | 29497 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 29498 | + pFile->ctrlFlags &= ~UNIXFILE_BLOCK; |
| 29369 | 29499 | } |
| 29370 | 29500 | |
| 29371 | 29501 | /* Update the global lock state and do debug tracing */ |
| 29372 | 29502 | #ifdef SQLITE_DEBUG |
| 29373 | 29503 | { u16 mask; |
| | @@ -29573,17 +29703,17 @@ |
| 29573 | 29703 | |
| 29574 | 29704 | /* Check to see if another process is holding the dead-man switch. |
| 29575 | 29705 | ** If not, truncate the file to zero length. |
| 29576 | 29706 | */ |
| 29577 | 29707 | rc = SQLITE_OK; |
| 29578 | | - if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 29708 | + if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 29579 | 29709 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 29580 | 29710 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
| 29581 | 29711 | } |
| 29582 | 29712 | } |
| 29583 | 29713 | if( rc==SQLITE_OK ){ |
| 29584 | | - rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 29714 | + rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 29585 | 29715 | } |
| 29586 | 29716 | if( rc ) goto shm_open_err; |
| 29587 | 29717 | } |
| 29588 | 29718 | } |
| 29589 | 29719 | |
| | @@ -29811,11 +29941,11 @@ |
| 29811 | 29941 | allMask |= pX->sharedMask; |
| 29812 | 29942 | } |
| 29813 | 29943 | |
| 29814 | 29944 | /* Unlock the system-level locks */ |
| 29815 | 29945 | if( (mask & allMask)==0 ){ |
| 29816 | | - rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 29946 | + rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 29817 | 29947 | }else{ |
| 29818 | 29948 | rc = SQLITE_OK; |
| 29819 | 29949 | } |
| 29820 | 29950 | |
| 29821 | 29951 | /* Undo the local locks */ |
| | @@ -29839,11 +29969,11 @@ |
| 29839 | 29969 | } |
| 29840 | 29970 | |
| 29841 | 29971 | /* Get shared locks at the system level, if necessary */ |
| 29842 | 29972 | if( rc==SQLITE_OK ){ |
| 29843 | 29973 | if( (allShared & mask)==0 ){ |
| 29844 | | - rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 29974 | + rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 29845 | 29975 | }else{ |
| 29846 | 29976 | rc = SQLITE_OK; |
| 29847 | 29977 | } |
| 29848 | 29978 | } |
| 29849 | 29979 | |
| | @@ -29864,20 +29994,20 @@ |
| 29864 | 29994 | |
| 29865 | 29995 | /* Get the exclusive locks at the system level. Then if successful |
| 29866 | 29996 | ** also mark the local connection as being locked. |
| 29867 | 29997 | */ |
| 29868 | 29998 | if( rc==SQLITE_OK ){ |
| 29869 | | - rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 29999 | + rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 29870 | 30000 | if( rc==SQLITE_OK ){ |
| 29871 | 30001 | assert( (p->sharedMask & mask)==0 ); |
| 29872 | 30002 | p->exclMask |= mask; |
| 29873 | 30003 | } |
| 29874 | 30004 | } |
| 29875 | 30005 | } |
| 29876 | 30006 | sqlite3_mutex_leave(pShmNode->mutex); |
| 29877 | 30007 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 29878 | | - p->id, osGetpid(), p->sharedMask, p->exclMask)); |
| 30008 | + p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
| 29879 | 30009 | return rc; |
| 29880 | 30010 | } |
| 29881 | 30011 | |
| 29882 | 30012 | /* |
| 29883 | 30013 | ** Implement a memory barrier or memory fence on shared memory. |
| | @@ -30968,12 +31098,12 @@ |
| 30968 | 31098 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 30969 | 31099 | ** here such that two or more threads all trying to open databases at |
| 30970 | 31100 | ** the same instant might all reset the PRNG. But multiple resets |
| 30971 | 31101 | ** are harmless. |
| 30972 | 31102 | */ |
| 30973 | | - if( randomnessPid!=osGetpid() ){ |
| 30974 | | - randomnessPid = osGetpid(); |
| 31103 | + if( randomnessPid!=osGetpid(0) ){ |
| 31104 | + randomnessPid = osGetpid(0); |
| 30975 | 31105 | sqlite3_randomness(0,0); |
| 30976 | 31106 | } |
| 30977 | 31107 | |
| 30978 | 31108 | memset(p, 0, sizeof(unixFile)); |
| 30979 | 31109 | |
| | @@ -31360,11 +31490,11 @@ |
| 31360 | 31490 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 31361 | 31491 | ** that we always use the same random number sequence. This makes the |
| 31362 | 31492 | ** tests repeatable. |
| 31363 | 31493 | */ |
| 31364 | 31494 | memset(zBuf, 0, nBuf); |
| 31365 | | - randomnessPid = osGetpid(); |
| 31495 | + randomnessPid = osGetpid(0); |
| 31366 | 31496 | #if !defined(SQLITE_TEST) |
| 31367 | 31497 | { |
| 31368 | 31498 | int fd, got; |
| 31369 | 31499 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
| 31370 | 31500 | if( fd<0 ){ |
| | @@ -31681,11 +31811,11 @@ |
| 31681 | 31811 | #else |
| 31682 | 31812 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 31683 | 31813 | { |
| 31684 | 31814 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
| 31685 | 31815 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 31686 | | - lPath, errno, osGetpid())); |
| 31816 | + lPath, errno, osGetpid(0))); |
| 31687 | 31817 | return SQLITE_IOERR_LOCK; |
| 31688 | 31818 | } |
| 31689 | 31819 | len = strlcat(lPath, "sqliteplocks", maxLen); |
| 31690 | 31820 | } |
| 31691 | 31821 | # else |
| | @@ -31703,11 +31833,11 @@ |
| 31703 | 31833 | char c = dbPath[i]; |
| 31704 | 31834 | lPath[i+len] = (c=='/')?'_':c; |
| 31705 | 31835 | } |
| 31706 | 31836 | lPath[i+len]='\0'; |
| 31707 | 31837 | strlcat(lPath, ":auto:", maxLen); |
| 31708 | | - OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid())); |
| 31838 | + OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
| 31709 | 31839 | return SQLITE_OK; |
| 31710 | 31840 | } |
| 31711 | 31841 | |
| 31712 | 31842 | /* |
| 31713 | 31843 | ** Creates the lock file and any missing directories in lockPath |
| | @@ -31730,20 +31860,20 @@ |
| 31730 | 31860 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 31731 | 31861 | int err=errno; |
| 31732 | 31862 | if( err!=EEXIST ) { |
| 31733 | 31863 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
| 31734 | 31864 | "'%s' proxy lock path=%s pid=%d\n", |
| 31735 | | - buf, strerror(err), lockPath, osGetpid())); |
| 31865 | + buf, strerror(err), lockPath, osGetpid(0))); |
| 31736 | 31866 | return err; |
| 31737 | 31867 | } |
| 31738 | 31868 | } |
| 31739 | 31869 | } |
| 31740 | 31870 | start=i+1; |
| 31741 | 31871 | } |
| 31742 | 31872 | buf[i] = lockPath[i]; |
| 31743 | 31873 | } |
| 31744 | | - OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid())); |
| 31874 | + OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, osGetpid(0))); |
| 31745 | 31875 | return 0; |
| 31746 | 31876 | } |
| 31747 | 31877 | |
| 31748 | 31878 | /* |
| 31749 | 31879 | ** Create a new VFS file descriptor (stored in memory obtained from |
| | @@ -32045,11 +32175,11 @@ |
| 32045 | 32175 | int tryOldLockPath = 0; |
| 32046 | 32176 | int forceNewLockPath = 0; |
| 32047 | 32177 | |
| 32048 | 32178 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 32049 | 32179 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 32050 | | - osGetpid())); |
| 32180 | + osGetpid(0))); |
| 32051 | 32181 | |
| 32052 | 32182 | rc = proxyGetHostID(myHostID, &pError); |
| 32053 | 32183 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 32054 | 32184 | storeLastErrno(pFile, pError); |
| 32055 | 32185 | goto end_takeconch; |
| | @@ -32255,11 +32385,11 @@ |
| 32255 | 32385 | |
| 32256 | 32386 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 32257 | 32387 | conchFile = pCtx->conchFile; |
| 32258 | 32388 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
| 32259 | 32389 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 32260 | | - osGetpid())); |
| 32390 | + osGetpid(0))); |
| 32261 | 32391 | if( pCtx->conchHeld>0 ){ |
| 32262 | 32392 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 32263 | 32393 | } |
| 32264 | 32394 | pCtx->conchHeld = 0; |
| 32265 | 32395 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| | @@ -32397,11 +32527,11 @@ |
| 32397 | 32527 | }else{ |
| 32398 | 32528 | lockPath=(char *)path; |
| 32399 | 32529 | } |
| 32400 | 32530 | |
| 32401 | 32531 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 32402 | | - (lockPath ? lockPath : ":auto:"), osGetpid())); |
| 32532 | + (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
| 32403 | 32533 | |
| 32404 | 32534 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 32405 | 32535 | if( pCtx==0 ){ |
| 32406 | 32536 | return SQLITE_NOMEM; |
| 32407 | 32537 | } |
| | @@ -39866,20 +39996,20 @@ |
| 39866 | 39996 | ** in pcache1 need to be protected via mutex. |
| 39867 | 39997 | */ |
| 39868 | 39998 | static void *pcache1Alloc(int nByte){ |
| 39869 | 39999 | void *p = 0; |
| 39870 | 40000 | assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 39871 | | - sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 39872 | 40001 | if( nByte<=pcache1.szSlot ){ |
| 39873 | 40002 | sqlite3_mutex_enter(pcache1.mutex); |
| 39874 | 40003 | p = (PgHdr1 *)pcache1.pFree; |
| 39875 | 40004 | if( p ){ |
| 39876 | 40005 | pcache1.pFree = pcache1.pFree->pNext; |
| 39877 | 40006 | pcache1.nFreeSlot--; |
| 39878 | 40007 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 39879 | 40008 | assert( pcache1.nFreeSlot>=0 ); |
| 39880 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 40009 | + sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 40010 | + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 39881 | 40011 | } |
| 39882 | 40012 | sqlite3_mutex_leave(pcache1.mutex); |
| 39883 | 40013 | } |
| 39884 | 40014 | if( p==0 ){ |
| 39885 | 40015 | /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get |
| | @@ -39888,11 +40018,12 @@ |
| 39888 | 40018 | p = sqlite3Malloc(nByte); |
| 39889 | 40019 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 39890 | 40020 | if( p ){ |
| 39891 | 40021 | int sz = sqlite3MallocSize(p); |
| 39892 | 40022 | sqlite3_mutex_enter(pcache1.mutex); |
| 39893 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 40023 | + sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 40024 | + sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 39894 | 40025 | sqlite3_mutex_leave(pcache1.mutex); |
| 39895 | 40026 | } |
| 39896 | 40027 | #endif |
| 39897 | 40028 | sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 39898 | 40029 | } |
| | @@ -39906,11 +40037,11 @@ |
| 39906 | 40037 | int nFreed = 0; |
| 39907 | 40038 | if( p==0 ) return 0; |
| 39908 | 40039 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 39909 | 40040 | PgFreeslot *pSlot; |
| 39910 | 40041 | sqlite3_mutex_enter(pcache1.mutex); |
| 39911 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 40042 | + sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 39912 | 40043 | pSlot = (PgFreeslot*)p; |
| 39913 | 40044 | pSlot->pNext = pcache1.pFree; |
| 39914 | 40045 | pcache1.pFree = pSlot; |
| 39915 | 40046 | pcache1.nFreeSlot++; |
| 39916 | 40047 | pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| | @@ -39920,11 +40051,11 @@ |
| 39920 | 40051 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 39921 | 40052 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 39922 | 40053 | nFreed = sqlite3MallocSize(p); |
| 39923 | 40054 | #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 39924 | 40055 | sqlite3_mutex_enter(pcache1.mutex); |
| 39925 | | - sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed); |
| 40056 | + sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed); |
| 39926 | 40057 | sqlite3_mutex_leave(pcache1.mutex); |
| 39927 | 40058 | #endif |
| 39928 | 40059 | sqlite3_free(p); |
| 39929 | 40060 | } |
| 39930 | 40061 | return nFreed; |
| | @@ -40656,10 +40787,18 @@ |
| 40656 | 40787 | |
| 40657 | 40788 | /* |
| 40658 | 40789 | ** Return the size of the header on each page of this PCACHE implementation. |
| 40659 | 40790 | */ |
| 40660 | 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 | +} |
| 40661 | 40800 | |
| 40662 | 40801 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 40663 | 40802 | /* |
| 40664 | 40803 | ** This function is called to free superfluous dynamically allocated memory |
| 40665 | 40804 | ** held by the pager system. Memory in use by any SQLite pager allocated |
| | @@ -49429,13 +49568,14 @@ |
| 49429 | 49568 | if( pWal->exclusiveMode ) return; |
| 49430 | 49569 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 49431 | 49570 | SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); |
| 49432 | 49571 | WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); |
| 49433 | 49572 | } |
| 49434 | | -static int walLockExclusive(Wal *pWal, int lockIdx, int n){ |
| 49573 | +static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){ |
| 49435 | 49574 | int rc; |
| 49436 | 49575 | if( pWal->exclusiveMode ) return SQLITE_OK; |
| 49576 | + if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0); |
| 49437 | 49577 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 49438 | 49578 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 49439 | 49579 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 49440 | 49580 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
| 49441 | 49581 | VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
| | @@ -49717,11 +49857,11 @@ |
| 49717 | 49857 | assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 ); |
| 49718 | 49858 | assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE ); |
| 49719 | 49859 | assert( pWal->writeLock ); |
| 49720 | 49860 | iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; |
| 49721 | 49861 | nLock = SQLITE_SHM_NLOCK - iLock; |
| 49722 | | - rc = walLockExclusive(pWal, iLock, nLock); |
| 49862 | + rc = walLockExclusive(pWal, iLock, nLock, 0); |
| 49723 | 49863 | if( rc ){ |
| 49724 | 49864 | return rc; |
| 49725 | 49865 | } |
| 49726 | 49866 | WALTRACE(("WAL%p: recovery begin...\n", pWal)); |
| 49727 | 49867 | |
| | @@ -50251,11 +50391,11 @@ |
| 50251 | 50391 | int lockIdx, /* Offset of first byte to lock */ |
| 50252 | 50392 | int n /* Number of bytes to lock */ |
| 50253 | 50393 | ){ |
| 50254 | 50394 | int rc; |
| 50255 | 50395 | do { |
| 50256 | | - rc = walLockExclusive(pWal, lockIdx, n); |
| 50396 | + rc = walLockExclusive(pWal, lockIdx, n, 0); |
| 50257 | 50397 | }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) ); |
| 50258 | 50398 | return rc; |
| 50259 | 50399 | } |
| 50260 | 50400 | |
| 50261 | 50401 | /* |
| | @@ -50684,11 +50824,11 @@ |
| 50684 | 50824 | if( pWal->readOnly & WAL_SHM_RDONLY ){ |
| 50685 | 50825 | if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){ |
| 50686 | 50826 | walUnlockShared(pWal, WAL_WRITE_LOCK); |
| 50687 | 50827 | rc = SQLITE_READONLY_RECOVERY; |
| 50688 | 50828 | } |
| 50689 | | - }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ |
| 50829 | + }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){ |
| 50690 | 50830 | pWal->writeLock = 1; |
| 50691 | 50831 | if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
| 50692 | 50832 | badHdr = walIndexTryHdr(pWal, pChanged); |
| 50693 | 50833 | if( badHdr ){ |
| 50694 | 50834 | /* If the wal-index header is still malformed even while holding |
| | @@ -50890,11 +51030,11 @@ |
| 50890 | 51030 | { |
| 50891 | 51031 | if( (pWal->readOnly & WAL_SHM_RDONLY)==0 |
| 50892 | 51032 | && (mxReadMark<pWal->hdr.mxFrame || mxI==0) |
| 50893 | 51033 | ){ |
| 50894 | 51034 | for(i=1; i<WAL_NREADER; i++){ |
| 50895 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 51035 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0); |
| 50896 | 51036 | if( rc==SQLITE_OK ){ |
| 50897 | 51037 | mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame; |
| 50898 | 51038 | mxI = i; |
| 50899 | 51039 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 50900 | 51040 | break; |
| | @@ -51146,11 +51286,11 @@ |
| 51146 | 51286 | } |
| 51147 | 51287 | |
| 51148 | 51288 | /* Only one writer allowed at a time. Get the write lock. Return |
| 51149 | 51289 | ** SQLITE_BUSY if unable. |
| 51150 | 51290 | */ |
| 51151 | | - rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 51291 | + rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0); |
| 51152 | 51292 | if( rc ){ |
| 51153 | 51293 | return rc; |
| 51154 | 51294 | } |
| 51155 | 51295 | pWal->writeLock = 1; |
| 51156 | 51296 | |
| | @@ -51291,11 +51431,11 @@ |
| 51291 | 51431 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 51292 | 51432 | assert( pInfo->nBackfill==pWal->hdr.mxFrame ); |
| 51293 | 51433 | if( pInfo->nBackfill>0 ){ |
| 51294 | 51434 | u32 salt1; |
| 51295 | 51435 | sqlite3_randomness(4, &salt1); |
| 51296 | | - rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 51436 | + rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0); |
| 51297 | 51437 | if( rc==SQLITE_OK ){ |
| 51298 | 51438 | /* If all readers are using WAL_READ_LOCK(0) (in other words if no |
| 51299 | 51439 | ** readers are currently using the WAL), then the transactions |
| 51300 | 51440 | ** frames will overwrite the start of the existing log. Update the |
| 51301 | 51441 | ** wal-index header to reflect this. |
| | @@ -51616,11 +51756,11 @@ |
| 51616 | 51756 | if( pWal->readOnly ) return SQLITE_READONLY; |
| 51617 | 51757 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
| 51618 | 51758 | |
| 51619 | 51759 | /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive |
| 51620 | 51760 | ** "checkpoint" lock on the database file. */ |
| 51621 | | - rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 51761 | + rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0); |
| 51622 | 51762 | if( rc ){ |
| 51623 | 51763 | /* EVIDENCE-OF: R-10421-19736 If any other process is running a |
| 51624 | 51764 | ** checkpoint operation at the same time, the lock cannot be obtained and |
| 51625 | 51765 | ** SQLITE_BUSY is returned. |
| 51626 | 51766 | ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured, |
| | @@ -52618,10 +52758,11 @@ |
| 52618 | 52758 | |
| 52619 | 52759 | /* |
| 52620 | 52760 | ** Exit the recursive mutex on a Btree. |
| 52621 | 52761 | */ |
| 52622 | 52762 | SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){ |
| 52763 | + assert( sqlite3_mutex_held(p->db->mutex) ); |
| 52623 | 52764 | if( p->sharable ){ |
| 52624 | 52765 | assert( p->wantToLock>0 ); |
| 52625 | 52766 | p->wantToLock--; |
| 52626 | 52767 | if( p->wantToLock==0 ){ |
| 52627 | 52768 | unlockBtreeMutex(p); |
| | @@ -54796,12 +54937,12 @@ |
| 54796 | 54937 | /* |
| 54797 | 54938 | ** The following asserts make sure that structures used by the btree are |
| 54798 | 54939 | ** the right size. This is to guard against size changes that result |
| 54799 | 54940 | ** when compiling on a different architecture. |
| 54800 | 54941 | */ |
| 54801 | | - assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 54802 | | - assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 54942 | + assert( sizeof(i64)==8 ); |
| 54943 | + assert( sizeof(u64)==8 ); |
| 54803 | 54944 | assert( sizeof(u32)==4 ); |
| 54804 | 54945 | assert( sizeof(u16)==2 ); |
| 54805 | 54946 | assert( sizeof(Pgno)==4 ); |
| 54806 | 54947 | |
| 54807 | 54948 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| | @@ -60259,11 +60400,12 @@ |
| 60259 | 60400 | ** the previous call, as the overflow cell data will have been |
| 60260 | 60401 | ** copied either into the body of a database page or into the new |
| 60261 | 60402 | ** pSpace buffer passed to the latter call to balance_nonroot(). |
| 60262 | 60403 | */ |
| 60263 | 60404 | u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
| 60264 | | - rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints); |
| 60405 | + rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, |
| 60406 | + pCur->hints&BTREE_BULKLOAD); |
| 60265 | 60407 | if( pFree ){ |
| 60266 | 60408 | /* If pFree is not NULL, it points to the pSpace buffer used |
| 60267 | 60409 | ** by a previous call to balance_nonroot(). Its contents are |
| 60268 | 60410 | ** now stored either on real database pages or within the |
| 60269 | 60411 | ** new pSpace buffer, so it may be safely freed here. */ |
| | @@ -61922,17 +62064,26 @@ |
| 61922 | 62064 | pBt->btsFlags &= ~BTS_NO_WAL; |
| 61923 | 62065 | return rc; |
| 61924 | 62066 | } |
| 61925 | 62067 | |
| 61926 | 62068 | /* |
| 61927 | | -** set the mask of hint flags for cursor pCsr. Currently the only valid |
| 61928 | | -** values are 0 and BTREE_BULKLOAD. |
| 62069 | +** set the mask of hint flags for cursor pCsr. |
| 61929 | 62070 | */ |
| 61930 | 62071 | SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
| 61931 | | - assert( mask==BTREE_BULKLOAD || mask==0 ); |
| 62072 | + assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 ); |
| 61932 | 62073 | pCsr->hints = mask; |
| 61933 | 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 |
| 61934 | 62085 | |
| 61935 | 62086 | /* |
| 61936 | 62087 | ** Return true if the given Btree is read-only. |
| 61937 | 62088 | */ |
| 61938 | 62089 | SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){ |
| | @@ -63822,11 +63973,11 @@ |
| 63822 | 63973 | ** by calling sqlite3ValueNew(). |
| 63823 | 63974 | ** |
| 63824 | 63975 | ** Otherwise, if the second argument is non-zero, then this function is |
| 63825 | 63976 | ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not |
| 63826 | 63977 | ** already been allocated, allocate the UnpackedRecord structure that |
| 63827 | | -** 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 |
| 63828 | 63979 | ** an sqlite3_value within the UnpackedRecord.a[] array. |
| 63829 | 63980 | */ |
| 63830 | 63981 | static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ |
| 63831 | 63982 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 63832 | 63983 | if( p ){ |
| | @@ -63866,10 +64017,117 @@ |
| 63866 | 64017 | UNUSED_PARAMETER(p); |
| 63867 | 64018 | #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ |
| 63868 | 64019 | return sqlite3ValueNew(db); |
| 63869 | 64020 | } |
| 63870 | 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 | + |
| 63871 | 64129 | /* |
| 63872 | 64130 | ** Extract a value from the supplied expression in the manner described |
| 63873 | 64131 | ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object |
| 63874 | 64132 | ** using valueNew(). |
| 63875 | 64133 | ** |
| | @@ -63897,10 +64155,16 @@ |
| 63897 | 64155 | *ppVal = 0; |
| 63898 | 64156 | return SQLITE_OK; |
| 63899 | 64157 | } |
| 63900 | 64158 | while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft; |
| 63901 | 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 ); |
| 63902 | 64166 | |
| 63903 | 64167 | if( op==TK_CAST ){ |
| 63904 | 64168 | u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); |
| 63905 | 64169 | rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); |
| 63906 | 64170 | testcase( rc!=SQLITE_OK ); |
| | @@ -63973,10 +64237,16 @@ |
| 63973 | 64237 | assert( zVal[nVal]=='\'' ); |
| 63974 | 64238 | sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, |
| 63975 | 64239 | 0, SQLITE_DYNAMIC); |
| 63976 | 64240 | } |
| 63977 | 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 |
| 63978 | 64248 | |
| 63979 | 64249 | *ppVal = pVal; |
| 63980 | 64250 | return rc; |
| 63981 | 64251 | |
| 63982 | 64252 | no_mem: |
| | @@ -65426,11 +65696,11 @@ |
| 65426 | 65696 | break; |
| 65427 | 65697 | } |
| 65428 | 65698 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 65429 | 65699 | case P4_VTAB: { |
| 65430 | 65700 | sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
| 65431 | | - sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
| 65701 | + sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab); |
| 65432 | 65702 | break; |
| 65433 | 65703 | } |
| 65434 | 65704 | #endif |
| 65435 | 65705 | case P4_INTARRAY: { |
| 65436 | 65706 | sqlite3_snprintf(nTemp, zTemp, "intarray"); |
| | @@ -66090,13 +66360,13 @@ |
| 66090 | 66360 | } |
| 66091 | 66361 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 66092 | 66362 | else if( pCx->pVtabCursor ){ |
| 66093 | 66363 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
| 66094 | 66364 | const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
| 66095 | | - p->inVtabMethod = 1; |
| 66365 | + assert( pVtabCursor->pVtab->nRef>0 ); |
| 66366 | + pVtabCursor->pVtab->nRef--; |
| 66096 | 66367 | pModule->xClose(pVtabCursor); |
| 66097 | | - p->inVtabMethod = 0; |
| 66098 | 66368 | } |
| 66099 | 66369 | #endif |
| 66100 | 66370 | } |
| 66101 | 66371 | |
| 66102 | 66372 | /* |
| | @@ -66451,11 +66721,11 @@ |
| 66451 | 66721 | |
| 66452 | 66722 | /* Delete the master journal file. This commits the transaction. After |
| 66453 | 66723 | ** doing this the directory is synced again before any individual |
| 66454 | 66724 | ** transaction files are deleted. |
| 66455 | 66725 | */ |
| 66456 | | - rc = sqlite3OsDelete(pVfs, zMaster, 1); |
| 66726 | + rc = sqlite3OsDelete(pVfs, zMaster, needSync); |
| 66457 | 66727 | sqlite3DbFree(db, zMaster); |
| 66458 | 66728 | zMaster = 0; |
| 66459 | 66729 | if( rc ){ |
| 66460 | 66730 | return rc; |
| 66461 | 66731 | } |
| | @@ -68864,11 +69134,11 @@ |
| 68864 | 69134 | } |
| 68865 | 69135 | SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ |
| 68866 | 69136 | pCtx->isError = errCode; |
| 68867 | 69137 | pCtx->fErrorOrAux = 1; |
| 68868 | 69138 | #ifdef SQLITE_DEBUG |
| 68869 | | - pCtx->pVdbe->rcApp = errCode; |
| 69139 | + if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; |
| 68870 | 69140 | #endif |
| 68871 | 69141 | if( pCtx->pOut->flags & MEM_Null ){ |
| 68872 | 69142 | sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, |
| 68873 | 69143 | SQLITE_UTF8, SQLITE_STATIC); |
| 68874 | 69144 | } |
| | @@ -69127,20 +69397,30 @@ |
| 69127 | 69397 | assert( p && p->pFunc ); |
| 69128 | 69398 | return p->pOut->db; |
| 69129 | 69399 | } |
| 69130 | 69400 | |
| 69131 | 69401 | /* |
| 69132 | | -** 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. |
| 69133 | 69407 | */ |
| 69134 | 69408 | SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ |
| 69135 | | - Vdbe *v = p->pVdbe; |
| 69136 | 69409 | int rc; |
| 69137 | | - if( v->iCurrentTime==0 ){ |
| 69138 | | - rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime); |
| 69139 | | - 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; |
| 69140 | 69420 | } |
| 69141 | | - return v->iCurrentTime; |
| 69421 | + return *piTime; |
| 69142 | 69422 | } |
| 69143 | 69423 | |
| 69144 | 69424 | /* |
| 69145 | 69425 | ** The following is the implementation of an SQL function that always |
| 69146 | 69426 | ** fails with an error message stating that the function is used in the |
| | @@ -69206,10 +69486,15 @@ |
| 69206 | 69486 | */ |
| 69207 | 69487 | SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ |
| 69208 | 69488 | AuxData *pAuxData; |
| 69209 | 69489 | |
| 69210 | 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 |
| 69211 | 69496 | for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 69212 | 69497 | if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 69213 | 69498 | } |
| 69214 | 69499 | |
| 69215 | 69500 | return (pAuxData ? pAuxData->pAux : 0); |
| | @@ -69229,10 +69514,15 @@ |
| 69229 | 69514 | AuxData *pAuxData; |
| 69230 | 69515 | Vdbe *pVdbe = pCtx->pVdbe; |
| 69231 | 69516 | |
| 69232 | 69517 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 69233 | 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 |
| 69234 | 69524 | |
| 69235 | 69525 | for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ |
| 69236 | 69526 | if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; |
| 69237 | 69527 | } |
| 69238 | 69528 | if( pAuxData==0 ){ |
| | @@ -71845,11 +72135,11 @@ |
| 71845 | 72135 | ** max() aggregate will set to 1 if the current row is not the minimum or |
| 71846 | 72136 | ** maximum. The P1 register is initialized to 0 by this instruction. |
| 71847 | 72137 | ** |
| 71848 | 72138 | ** The interface used by the implementation of the aforementioned functions |
| 71849 | 72139 | ** to retrieve the collation sequence set by this opcode is not available |
| 71850 | | -** publicly, only to user functions defined in func.c. |
| 72140 | +** publicly. Only built-in functions have access to this feature. |
| 71851 | 72141 | */ |
| 71852 | 72142 | case OP_CollSeq: { |
| 71853 | 72143 | assert( pOp->p4type==P4_COLLSEQ ); |
| 71854 | 72144 | if( pOp->p1 ){ |
| 71855 | 72145 | sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); |
| | @@ -73563,35 +73853,33 @@ |
| 73563 | 73853 | ** cursors or a single read/write cursor but not both. |
| 73564 | 73854 | ** |
| 73565 | 73855 | ** See also OpenRead. |
| 73566 | 73856 | */ |
| 73567 | 73857 | case OP_ReopenIdx: { |
| 73568 | | - VdbeCursor *pCur; |
| 73569 | | - |
| 73570 | | - assert( pOp->p5==0 ); |
| 73571 | | - assert( pOp->p4type==P4_KEYINFO ); |
| 73572 | | - pCur = p->apCsr[pOp->p1]; |
| 73573 | | - if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ |
| 73574 | | - assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ |
| 73575 | | - break; |
| 73576 | | - } |
| 73577 | | - /* If the cursor is not currently open or is open on a different |
| 73578 | | - ** index, then fall through into OP_OpenRead to force a reopen */ |
| 73579 | | -} |
| 73580 | | -case OP_OpenRead: |
| 73581 | | -case OP_OpenWrite: { |
| 73582 | 73858 | int nField; |
| 73583 | 73859 | KeyInfo *pKeyInfo; |
| 73584 | 73860 | int p2; |
| 73585 | 73861 | int iDb; |
| 73586 | 73862 | int wrFlag; |
| 73587 | 73863 | Btree *pX; |
| 73588 | 73864 | VdbeCursor *pCur; |
| 73589 | 73865 | Db *pDb; |
| 73590 | 73866 | |
| 73591 | | - assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 ); |
| 73592 | | - 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 ); |
| 73593 | 73881 | assert( p->bIsReader ); |
| 73594 | 73882 | assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx |
| 73595 | 73883 | || p->readOnly==0 ); |
| 73596 | 73884 | |
| 73597 | 73885 | if( p->expired ){ |
| | @@ -73650,18 +73938,21 @@ |
| 73650 | 73938 | pCur->nullRow = 1; |
| 73651 | 73939 | pCur->isOrdered = 1; |
| 73652 | 73940 | pCur->pgnoRoot = p2; |
| 73653 | 73941 | rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); |
| 73654 | 73942 | pCur->pKeyInfo = pKeyInfo; |
| 73655 | | - assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); |
| 73656 | | - sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR)); |
| 73657 | | - |
| 73658 | 73943 | /* Set the VdbeCursor.isTable variable. Previous versions of |
| 73659 | 73944 | ** SQLite used to check if the root-page flags were sane at this point |
| 73660 | 73945 | ** and report database corruption if they were not, but this check has |
| 73661 | 73946 | ** since moved into the btree layer. */ |
| 73662 | 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))); |
| 73663 | 73954 | break; |
| 73664 | 73955 | } |
| 73665 | 73956 | |
| 73666 | 73957 | /* Opcode: OpenEphemeral P1 P2 * P4 P5 |
| 73667 | 73958 | ** Synopsis: nColumn=P2 |
| | @@ -73918,10 +74209,26 @@ |
| 73918 | 74209 | oc = pOp->opcode; |
| 73919 | 74210 | pC->nullRow = 0; |
| 73920 | 74211 | #ifdef SQLITE_DEBUG |
| 73921 | 74212 | pC->seekOp = pOp->opcode; |
| 73922 | 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 | + |
| 73923 | 74230 | if( pC->isTable ){ |
| 73924 | 74231 | /* The input value in P3 might be of any type: integer, real, string, |
| 73925 | 74232 | ** blob, or NULL. But it needs to be an integer before we can do |
| 73926 | 74233 | ** the seek, so convert it. */ |
| 73927 | 74234 | pIn3 = &aMem[pOp->p3]; |
| | @@ -75257,34 +75564,19 @@ |
| 75257 | 75564 | ** |
| 75258 | 75565 | ** See also: Clear |
| 75259 | 75566 | */ |
| 75260 | 75567 | case OP_Destroy: { /* out2-prerelease */ |
| 75261 | 75568 | int iMoved; |
| 75262 | | - int iCnt; |
| 75263 | | - Vdbe *pVdbe; |
| 75264 | 75569 | int iDb; |
| 75265 | 75570 | |
| 75266 | 75571 | assert( p->readOnly==0 ); |
| 75267 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 75268 | | - iCnt = 0; |
| 75269 | | - for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ |
| 75270 | | - if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader |
| 75271 | | - && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 |
| 75272 | | - ){ |
| 75273 | | - iCnt++; |
| 75274 | | - } |
| 75275 | | - } |
| 75276 | | -#else |
| 75277 | | - iCnt = db->nVdbeRead; |
| 75278 | | -#endif |
| 75279 | 75572 | pOut->flags = MEM_Null; |
| 75280 | | - if( iCnt>1 ){ |
| 75573 | + if( db->nVdbeRead > db->nVDestroy+1 ){ |
| 75281 | 75574 | rc = SQLITE_LOCKED; |
| 75282 | 75575 | p->errorAction = OE_Abort; |
| 75283 | 75576 | }else{ |
| 75284 | 75577 | iDb = pOp->p3; |
| 75285 | | - assert( iCnt==1 ); |
| 75286 | 75578 | assert( DbMaskTest(p->btreeMask, iDb) ); |
| 75287 | 75579 | iMoved = 0; /* Not needed. Only to silence a warning. */ |
| 75288 | 75580 | rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); |
| 75289 | 75581 | pOut->flags = MEM_Int; |
| 75290 | 75582 | pOut->u.i = iMoved; |
| | @@ -76337,17 +76629,33 @@ |
| 76337 | 76629 | break; |
| 76338 | 76630 | } |
| 76339 | 76631 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76340 | 76632 | |
| 76341 | 76633 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 76342 | | -/* Opcode: VCreate P1 * * P4 * |
| 76634 | +/* Opcode: VCreate P1 P2 * * * |
| 76343 | 76635 | ** |
| 76344 | | -** P4 is the name of a virtual table in database P1. Call the xCreate method |
| 76345 | | -** 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. |
| 76346 | 76638 | */ |
| 76347 | 76639 | case OP_VCreate: { |
| 76348 | | - 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); |
| 76349 | 76657 | break; |
| 76350 | 76658 | } |
| 76351 | 76659 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76352 | 76660 | |
| 76353 | 76661 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -76355,13 +76663,13 @@ |
| 76355 | 76663 | ** |
| 76356 | 76664 | ** P4 is the name of a virtual table in database P1. Call the xDestroy method |
| 76357 | 76665 | ** of that table. |
| 76358 | 76666 | */ |
| 76359 | 76667 | case OP_VDestroy: { |
| 76360 | | - p->inVtabMethod = 2; |
| 76668 | + db->nVDestroy++; |
| 76361 | 76669 | rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); |
| 76362 | | - p->inVtabMethod = 0; |
| 76670 | + db->nVDestroy--; |
| 76363 | 76671 | break; |
| 76364 | 76672 | } |
| 76365 | 76673 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76366 | 76674 | |
| 76367 | 76675 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -76373,18 +76681,21 @@ |
| 76373 | 76681 | */ |
| 76374 | 76682 | case OP_VOpen: { |
| 76375 | 76683 | VdbeCursor *pCur; |
| 76376 | 76684 | sqlite3_vtab_cursor *pVtabCursor; |
| 76377 | 76685 | sqlite3_vtab *pVtab; |
| 76378 | | - sqlite3_module *pModule; |
| 76686 | + const sqlite3_module *pModule; |
| 76379 | 76687 | |
| 76380 | 76688 | assert( p->bIsReader ); |
| 76381 | 76689 | pCur = 0; |
| 76382 | 76690 | pVtabCursor = 0; |
| 76383 | 76691 | pVtab = pOp->p4.pVtab->pVtab; |
| 76384 | | - pModule = (sqlite3_module *)pVtab->pModule; |
| 76385 | | - assert(pVtab && pModule); |
| 76692 | + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 76693 | + rc = SQLITE_LOCKED; |
| 76694 | + break; |
| 76695 | + } |
| 76696 | + pModule = pVtab->pModule; |
| 76386 | 76697 | rc = pModule->xOpen(pVtab, &pVtabCursor); |
| 76387 | 76698 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76388 | 76699 | if( SQLITE_OK==rc ){ |
| 76389 | 76700 | /* Initialize sqlite3_vtab_cursor base class */ |
| 76390 | 76701 | pVtabCursor->pVtab = pVtab; |
| | @@ -76391,10 +76702,11 @@ |
| 76391 | 76702 | |
| 76392 | 76703 | /* Initialize vdbe cursor object */ |
| 76393 | 76704 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
| 76394 | 76705 | if( pCur ){ |
| 76395 | 76706 | pCur->pVtabCursor = pVtabCursor; |
| 76707 | + pVtab->nRef++; |
| 76396 | 76708 | }else{ |
| 76397 | 76709 | db->mallocFailed = 1; |
| 76398 | 76710 | pModule->xClose(pVtabCursor); |
| 76399 | 76711 | } |
| 76400 | 76712 | } |
| | @@ -76456,13 +76768,11 @@ |
| 76456 | 76768 | apArg = p->apArg; |
| 76457 | 76769 | for(i = 0; i<nArg; i++){ |
| 76458 | 76770 | apArg[i] = &pArgc[i+1]; |
| 76459 | 76771 | } |
| 76460 | 76772 | |
| 76461 | | - p->inVtabMethod = 1; |
| 76462 | 76773 | rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
| 76463 | | - p->inVtabMethod = 0; |
| 76464 | 76774 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76465 | 76775 | if( rc==SQLITE_OK ){ |
| 76466 | 76776 | res = pModule->xEof(pVtabCursor); |
| 76467 | 76777 | } |
| 76468 | 76778 | VdbeBranchTaken(res!=0,2); |
| | @@ -76548,13 +76858,11 @@ |
| 76548 | 76858 | ** underlying implementation to return an error if one occurs during |
| 76549 | 76859 | ** xNext(). Instead, if an error occurs, true is returned (indicating that |
| 76550 | 76860 | ** data is available) and the error code returned when xColumn or |
| 76551 | 76861 | ** some other method is next invoked on the save virtual table cursor. |
| 76552 | 76862 | */ |
| 76553 | | - p->inVtabMethod = 1; |
| 76554 | 76863 | rc = pModule->xNext(pCur->pVtabCursor); |
| 76555 | | - p->inVtabMethod = 0; |
| 76556 | 76864 | sqlite3VtabImportErrmsg(p, pVtab); |
| 76557 | 76865 | if( rc==SQLITE_OK ){ |
| 76558 | 76866 | res = pModule->xEof(pCur->pVtabCursor); |
| 76559 | 76867 | } |
| 76560 | 76868 | VdbeBranchTaken(!res,2); |
| | @@ -76625,11 +76933,11 @@ |
| 76625 | 76933 | ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to |
| 76626 | 76934 | ** apply in the case of a constraint failure on an insert or update. |
| 76627 | 76935 | */ |
| 76628 | 76936 | case OP_VUpdate: { |
| 76629 | 76937 | sqlite3_vtab *pVtab; |
| 76630 | | - sqlite3_module *pModule; |
| 76938 | + const sqlite3_module *pModule; |
| 76631 | 76939 | int nArg; |
| 76632 | 76940 | int i; |
| 76633 | 76941 | sqlite_int64 rowid; |
| 76634 | 76942 | Mem **apArg; |
| 76635 | 76943 | Mem *pX; |
| | @@ -76637,11 +76945,15 @@ |
| 76637 | 76945 | assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback |
| 76638 | 76946 | || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace |
| 76639 | 76947 | ); |
| 76640 | 76948 | assert( p->readOnly==0 ); |
| 76641 | 76949 | pVtab = pOp->p4.pVtab->pVtab; |
| 76642 | | - pModule = (sqlite3_module *)pVtab->pModule; |
| 76950 | + if( pVtab==0 || NEVER(pVtab->pModule==0) ){ |
| 76951 | + rc = SQLITE_LOCKED; |
| 76952 | + break; |
| 76953 | + } |
| 76954 | + pModule = pVtab->pModule; |
| 76643 | 76955 | nArg = pOp->p2; |
| 76644 | 76956 | assert( pOp->p4type==P4_VTAB ); |
| 76645 | 76957 | if( ALWAYS(pModule->xUpdate) ){ |
| 76646 | 76958 | u8 vtabOnConflict = db->vtabOnConflict; |
| 76647 | 76959 | apArg = p->apArg; |
| | @@ -78507,10 +78819,11 @@ |
| 78507 | 78819 | sqlite3 *db, /* Database handle doing sort */ |
| 78508 | 78820 | i64 nExtend, /* Attempt to extend file to this size */ |
| 78509 | 78821 | sqlite3_file **ppFd |
| 78510 | 78822 | ){ |
| 78511 | 78823 | int rc; |
| 78824 | + if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS; |
| 78512 | 78825 | rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd, |
| 78513 | 78826 | SQLITE_OPEN_TEMP_JOURNAL | |
| 78514 | 78827 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 78515 | 78828 | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc |
| 78516 | 78829 | ); |
| | @@ -82098,14 +82411,15 @@ |
| 82098 | 82411 | ** and the pExpr parameter is returned unchanged. |
| 82099 | 82412 | */ |
| 82100 | 82413 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken( |
| 82101 | 82414 | Parse *pParse, /* Parsing context */ |
| 82102 | 82415 | Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
| 82103 | | - const Token *pCollName /* Name of collating sequence */ |
| 82416 | + const Token *pCollName, /* Name of collating sequence */ |
| 82417 | + int dequote /* True to dequote pCollName */ |
| 82104 | 82418 | ){ |
| 82105 | 82419 | if( pCollName->n>0 ){ |
| 82106 | | - Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); |
| 82420 | + Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); |
| 82107 | 82421 | if( pNew ){ |
| 82108 | 82422 | pNew->pLeft = pExpr; |
| 82109 | 82423 | pNew->flags |= EP_Collate|EP_Skip; |
| 82110 | 82424 | pExpr = pNew; |
| 82111 | 82425 | } |
| | @@ -82115,11 +82429,11 @@ |
| 82115 | 82429 | SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ |
| 82116 | 82430 | Token s; |
| 82117 | 82431 | assert( zC!=0 ); |
| 82118 | 82432 | s.z = zC; |
| 82119 | 82433 | s.n = sqlite3Strlen30(s.z); |
| 82120 | | - return sqlite3ExprAddCollateToken(pParse, pExpr, &s); |
| 82434 | + return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); |
| 82121 | 82435 | } |
| 82122 | 82436 | |
| 82123 | 82437 | /* |
| 82124 | 82438 | ** Skip over any TK_COLLATE or TK_AS operators and any unlikely() |
| 82125 | 82439 | ** or likelihood() function at the root of an expression. |
| | @@ -82425,10 +82739,11 @@ |
| 82425 | 82739 | ** |
| 82426 | 82740 | ** Also propagate all EP_Propagate flags from the Expr.x.pList into |
| 82427 | 82741 | ** Expr.flags. |
| 82428 | 82742 | */ |
| 82429 | 82743 | SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
| 82744 | + if( pParse->nErr ) return; |
| 82430 | 82745 | exprSetHeight(p); |
| 82431 | 82746 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
| 82432 | 82747 | } |
| 82433 | 82748 | |
| 82434 | 82749 | /* |
| | @@ -87139,11 +87454,14 @@ |
| 87139 | 87454 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 87140 | 87455 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 87141 | 87456 | */ |
| 87142 | 87457 | if( pDflt ){ |
| 87143 | 87458 | sqlite3_value *pVal = 0; |
| 87144 | | - 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 ){ |
| 87145 | 87463 | db->mallocFailed = 1; |
| 87146 | 87464 | return; |
| 87147 | 87465 | } |
| 87148 | 87466 | if( !pVal ){ |
| 87149 | 87467 | sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
| | @@ -93202,10 +93520,11 @@ |
| 93202 | 93520 | } |
| 93203 | 93521 | if( pIdx->onError==OE_Default ){ |
| 93204 | 93522 | pIdx->onError = pIndex->onError; |
| 93205 | 93523 | } |
| 93206 | 93524 | } |
| 93525 | + pRet = pIdx; |
| 93207 | 93526 | goto exit_create_index; |
| 93208 | 93527 | } |
| 93209 | 93528 | } |
| 93210 | 93529 | } |
| 93211 | 93530 | |
| | @@ -95661,11 +95980,13 @@ |
| 95661 | 95980 | |
| 95662 | 95981 | /* |
| 95663 | 95982 | ** Return the collating function associated with a function. |
| 95664 | 95983 | */ |
| 95665 | 95984 | static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ |
| 95666 | | - VdbeOp *pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 95985 | + VdbeOp *pOp; |
| 95986 | + assert( context->pVdbe!=0 ); |
| 95987 | + pOp = &context->pVdbe->aOp[context->iOp-1]; |
| 95667 | 95988 | assert( pOp->opcode==OP_CollSeq ); |
| 95668 | 95989 | assert( pOp->p4type==P4_COLLSEQ ); |
| 95669 | 95990 | return pOp->p4.pColl; |
| 95670 | 95991 | } |
| 95671 | 95992 | |
| | @@ -109678,11 +109999,11 @@ |
| 109678 | 109999 | sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); |
| 109679 | 110000 | |
| 109680 | 110001 | for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); |
| 109681 | 110002 | pEList = pLeft->pEList; |
| 109682 | 110003 | if( pCte->pCols ){ |
| 109683 | | - if( pEList->nExpr!=pCte->pCols->nExpr ){ |
| 110004 | + if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ |
| 109684 | 110005 | sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", |
| 109685 | 110006 | pCte->zName, pEList->nExpr, pCte->pCols->nExpr |
| 109686 | 110007 | ); |
| 109687 | 110008 | pParse->pWith = pSavedWith; |
| 109688 | 110009 | return SQLITE_ERROR; |
| | @@ -114104,10 +114425,11 @@ |
| 114104 | 114425 | */ |
| 114105 | 114426 | if( !db->init.busy ){ |
| 114106 | 114427 | char *zStmt; |
| 114107 | 114428 | char *zWhere; |
| 114108 | 114429 | int iDb; |
| 114430 | + int iReg; |
| 114109 | 114431 | Vdbe *v; |
| 114110 | 114432 | |
| 114111 | 114433 | /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
| 114112 | 114434 | if( pEnd ){ |
| 114113 | 114435 | pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; |
| | @@ -114138,12 +114460,14 @@ |
| 114138 | 114460 | sqlite3ChangeCookie(pParse, iDb); |
| 114139 | 114461 | |
| 114140 | 114462 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 114141 | 114463 | zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
| 114142 | 114464 | sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
| 114143 | | - sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 114144 | | - 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); |
| 114145 | 114469 | } |
| 114146 | 114470 | |
| 114147 | 114471 | /* If we are rereading the sqlite_master table create the in-memory |
| 114148 | 114472 | ** record of the table. The xConnect() method is not called until |
| 114149 | 114473 | ** the first time the virtual table is used in an SQL statement. This |
| | @@ -114492,15 +114816,19 @@ |
| 114492 | 114816 | int rc = SQLITE_OK; |
| 114493 | 114817 | Table *pTab; |
| 114494 | 114818 | |
| 114495 | 114819 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
| 114496 | 114820 | if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ |
| 114497 | | - VTable *p = vtabDisconnectAll(db, pTab); |
| 114498 | | - |
| 114499 | | - 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); |
| 114500 | 114829 | rc = p->pMod->pModule->xDestroy(p->pVtab); |
| 114501 | | - |
| 114502 | 114830 | /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ |
| 114503 | 114831 | if( rc==SQLITE_OK ){ |
| 114504 | 114832 | assert( pTab->pVTable==p && p->pNext==0 ); |
| 114505 | 114833 | p->pVtab = 0; |
| 114506 | 114834 | pTab->pVTable = 0; |
| | @@ -116078,10 +116406,83 @@ |
| 116078 | 116406 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 116079 | 116407 | pWC->a[iChild].iParent = iParent; |
| 116080 | 116408 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 116081 | 116409 | pWC->a[iParent].nChild++; |
| 116082 | 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 | +} |
| 116083 | 116484 | |
| 116084 | 116485 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 116085 | 116486 | /* |
| 116086 | 116487 | ** Analyze a term that consists of two or more OR-connected |
| 116087 | 116488 | ** subterms. So in: |
| | @@ -116103,10 +116504,11 @@ |
| 116103 | 116504 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 116104 | 116505 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 116105 | 116506 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 116106 | 116507 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 116107 | 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) |
| 116108 | 116510 | ** |
| 116109 | 116511 | ** CASE 1: |
| 116110 | 116512 | ** |
| 116111 | 116513 | ** If all subterms are of the form T.C=expr for some single column of C and |
| 116112 | 116514 | ** a single table T (as shown in example B above) then create a new virtual |
| | @@ -116118,10 +116520,20 @@ |
| 116118 | 116520 | ** then create a new virtual term like this: |
| 116119 | 116521 | ** |
| 116120 | 116522 | ** x IN (expr1,expr2,expr3) |
| 116121 | 116523 | ** |
| 116122 | 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: |
| 116123 | 116535 | ** |
| 116124 | 116536 | ** If all subterms are indexable by a single table T, then set |
| 116125 | 116537 | ** |
| 116126 | 116538 | ** WhereTerm.eOperator = WO_OR |
| 116127 | 116539 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| | @@ -116245,15 +116657,29 @@ |
| 116245 | 116657 | } |
| 116246 | 116658 | } |
| 116247 | 116659 | } |
| 116248 | 116660 | |
| 116249 | 116661 | /* |
| 116250 | | - ** 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 |
| 116251 | 116663 | ** empty. |
| 116252 | 116664 | */ |
| 116253 | 116665 | pOrInfo->indexable = indexable; |
| 116254 | 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 | + } |
| 116255 | 116681 | |
| 116256 | 116682 | /* |
| 116257 | 116683 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 116258 | 116684 | ** we have to do some additional checking to see if case 1 really |
| 116259 | 116685 | ** is satisfied. |
| | @@ -116380,11 +116806,11 @@ |
| 116380 | 116806 | pTerm = &pWC->a[idxTerm]; |
| 116381 | 116807 | markTermAsChild(pWC, idxNew, idxTerm); |
| 116382 | 116808 | }else{ |
| 116383 | 116809 | sqlite3ExprListDelete(db, pList); |
| 116384 | 116810 | } |
| 116385 | | - pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
| 116811 | + pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
| 116386 | 116812 | } |
| 116387 | 116813 | } |
| 116388 | 116814 | } |
| 116389 | 116815 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
| 116390 | 116816 | |
| | @@ -116575,11 +117001,11 @@ |
| 116575 | 117001 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 116576 | 117002 | Expr *pNewExpr1; |
| 116577 | 117003 | Expr *pNewExpr2; |
| 116578 | 117004 | int idxNew1; |
| 116579 | 117005 | int idxNew2; |
| 116580 | | - Token sCollSeqName; /* Name of collating sequence */ |
| 117006 | + const char *zCollSeqName; /* Name of collating sequence */ |
| 116581 | 117007 | const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
| 116582 | 117008 | |
| 116583 | 117009 | pLeft = pExpr->x.pList->a[1].pExpr; |
| 116584 | 117010 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
| 116585 | 117011 | |
| | @@ -116611,23 +117037,22 @@ |
| 116611 | 117037 | if( c=='A'-1 ) isComplete = 0; |
| 116612 | 117038 | c = sqlite3UpperToLower[c]; |
| 116613 | 117039 | } |
| 116614 | 117040 | *pC = c + 1; |
| 116615 | 117041 | } |
| 116616 | | - sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 116617 | | - sCollSeqName.n = 6; |
| 117042 | + zCollSeqName = noCase ? "NOCASE" : "BINARY"; |
| 116618 | 117043 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
| 116619 | 117044 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 116620 | | - sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
| 117045 | + sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
| 116621 | 117046 | pStr1, 0); |
| 116622 | 117047 | transferJoinMarkings(pNewExpr1, pExpr); |
| 116623 | 117048 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
| 116624 | 117049 | testcase( idxNew1==0 ); |
| 116625 | 117050 | exprAnalyze(pSrc, pWC, idxNew1); |
| 116626 | 117051 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
| 116627 | 117052 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
| 116628 | | - sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
| 117053 | + sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
| 116629 | 117054 | pStr2, 0); |
| 116630 | 117055 | transferJoinMarkings(pNewExpr2, pExpr); |
| 116631 | 117056 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
| 116632 | 117057 | testcase( idxNew2==0 ); |
| 116633 | 117058 | exprAnalyze(pSrc, pWC, idxNew2); |
| | @@ -117240,15 +117665,18 @@ |
| 117240 | 117665 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 117241 | 117666 | /* |
| 117242 | 117667 | ** Estimate the location of a particular key among all keys in an |
| 117243 | 117668 | ** index. Store the results in aStat as follows: |
| 117244 | 117669 | ** |
| 117245 | | -** aStat[0] Est. number of rows less than pVal |
| 117246 | | -** 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 |
| 117247 | 117672 | ** |
| 117248 | 117673 | ** Return the index of the sample that is the smallest sample that |
| 117249 | | -** 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. |
| 117250 | 117678 | */ |
| 117251 | 117679 | static int whereKeyStats( |
| 117252 | 117680 | Parse *pParse, /* Database connection */ |
| 117253 | 117681 | Index *pIdx, /* Index to consider domain of */ |
| 117254 | 117682 | UnpackedRecord *pRec, /* Vector of values to consider */ |
| | @@ -117255,71 +117683,162 @@ |
| 117255 | 117683 | int roundUp, /* Round up if true. Round down if false */ |
| 117256 | 117684 | tRowcnt *aStat /* OUT: stats written here */ |
| 117257 | 117685 | ){ |
| 117258 | 117686 | IndexSample *aSample = pIdx->aSample; |
| 117259 | 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 */ |
| 117260 | 117690 | int iMin = 0; /* Smallest sample not yet tested */ |
| 117261 | | - int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */ |
| 117262 | 117691 | int iTest; /* Next sample to test */ |
| 117263 | 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 > */ |
| 117264 | 117695 | |
| 117265 | 117696 | #ifndef SQLITE_DEBUG |
| 117266 | 117697 | UNUSED_PARAMETER( pParse ); |
| 117267 | 117698 | #endif |
| 117268 | 117699 | assert( pRec!=0 ); |
| 117269 | | - iCol = pRec->nField - 1; |
| 117270 | 117700 | assert( pIdx->nSample>0 ); |
| 117271 | | - 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; |
| 117272 | 117750 | do{ |
| 117273 | | - iTest = (iMin+i)/2; |
| 117274 | | - 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); |
| 117275 | 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]; |
| 117276 | 117774 | iMin = iTest+1; |
| 117775 | + res = -1; |
| 117277 | 117776 | }else{ |
| 117278 | | - i = iTest; |
| 117777 | + iSample = iTest; |
| 117778 | + iCol = n-1; |
| 117279 | 117779 | } |
| 117280 | | - }while( res && iMin<i ); |
| 117780 | + }while( res && iMin<iSample ); |
| 117781 | + i = iSample / nField; |
| 117281 | 117782 | |
| 117282 | 117783 | #ifdef SQLITE_DEBUG |
| 117283 | 117784 | /* The following assert statements check that the binary search code |
| 117284 | 117785 | ** above found the right answer. This block serves no purpose other |
| 117285 | 117786 | ** than to invoke the asserts. */ |
| 117286 | | - if( res==0 ){ |
| 117287 | | - /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 117288 | | - assert( i<pIdx->nSample ); |
| 117289 | | - assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 117290 | | - || pParse->db->mallocFailed ); |
| 117291 | | - }else{ |
| 117292 | | - /* Otherwise, pRec must be smaller than sample $i and larger than |
| 117293 | | - ** sample ($i-1). */ |
| 117294 | | - assert( i==pIdx->nSample |
| 117295 | | - || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 117296 | | - || pParse->db->mallocFailed ); |
| 117297 | | - assert( i==0 |
| 117298 | | - || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 117299 | | - || 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 | + } |
| 117300 | 117821 | } |
| 117301 | 117822 | #endif /* ifdef SQLITE_DEBUG */ |
| 117302 | 117823 | |
| 117303 | | - /* At this point, aSample[i] is the first sample that is greater than |
| 117304 | | - ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 117305 | | - ** than pVal. If aSample[i]==pVal, then res==0. |
| 117306 | | - */ |
| 117307 | 117824 | if( res==0 ){ |
| 117825 | + /* Record pRec is equal to sample i */ |
| 117826 | + assert( iCol==nField-1 ); |
| 117308 | 117827 | aStat[0] = aSample[i].anLt[iCol]; |
| 117309 | 117828 | aStat[1] = aSample[i].anEq[iCol]; |
| 117310 | 117829 | }else{ |
| 117311 | | - tRowcnt iLower, iUpper, iGap; |
| 117312 | | - if( i==0 ){ |
| 117313 | | - iLower = 0; |
| 117314 | | - 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]); |
| 117315 | 117836 | }else{ |
| 117316 | | - i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 117317 | | - iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol]; |
| 117318 | | - iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol]; |
| 117837 | + iUpper = aSample[i].anLt[iCol]; |
| 117319 | 117838 | } |
| 117320 | | - aStat[1] = pIdx->aAvgEq[iCol]; |
| 117839 | + |
| 117321 | 117840 | if( iLower>=iUpper ){ |
| 117322 | 117841 | iGap = 0; |
| 117323 | 117842 | }else{ |
| 117324 | 117843 | iGap = iUpper - iLower; |
| 117325 | 117844 | } |
| | @@ -117327,11 +117846,15 @@ |
| 117327 | 117846 | iGap = (iGap*2)/3; |
| 117328 | 117847 | }else{ |
| 117329 | 117848 | iGap = iGap/3; |
| 117330 | 117849 | } |
| 117331 | 117850 | aStat[0] = iLower + iGap; |
| 117851 | + aStat[1] = pIdx->aAvgEq[iCol]; |
| 117332 | 117852 | } |
| 117853 | + |
| 117854 | + /* Restore the pRec->nField value before returning. */ |
| 117855 | + pRec->nField = nField; |
| 117333 | 117856 | return i; |
| 117334 | 117857 | } |
| 117335 | 117858 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 117336 | 117859 | |
| 117337 | 117860 | /* |
| | @@ -118322,25 +118845,33 @@ |
| 118322 | 118845 | #else |
| 118323 | 118846 | # define addScanStatus(a, b, c, d) ((void)d) |
| 118324 | 118847 | #endif |
| 118325 | 118848 | |
| 118326 | 118849 | /* |
| 118327 | | -** Look at the last instruction coded. If that instruction is OP_String8 |
| 118328 | | -** and if pLoop->iLikeRepCntr is non-zero, then change the P3 to be |
| 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 |
| 118329 | 118852 | ** pLoop->iLikeRepCntr and set P5. |
| 118330 | 118853 | ** |
| 118331 | 118854 | ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 118332 | 118855 | ** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 118333 | 118856 | ** scan loop run twice, once for strings and a second time for BLOBs. |
| 118334 | 118857 | ** The OP_String opcodes on the second pass convert the upper and lower |
| 118335 | 118858 | ** bound string contants to blobs. This routine makes the necessary changes |
| 118336 | 118859 | ** to the OP_String opcodes for that to happen. |
| 118337 | 118860 | */ |
| 118338 | | -static void whereLikeOptimizationStringFixup(Vdbe *v, WhereLevel *pLevel){ |
| 118339 | | - VdbeOp *pOp; |
| 118340 | | - pOp = sqlite3VdbeGetOp(v, -1); |
| 118341 | | - if( pLevel->iLikeRepCntr && pOp->opcode==OP_String8 ){ |
| 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 ); |
| 118342 | 118873 | pOp->p3 = pLevel->iLikeRepCntr; |
| 118343 | 118874 | pOp->p5 = 1; |
| 118344 | 118875 | } |
| 118345 | 118876 | } |
| 118346 | 118877 | |
| | @@ -118670,18 +119201,20 @@ |
| 118670 | 119201 | */ |
| 118671 | 119202 | j = nEq; |
| 118672 | 119203 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 118673 | 119204 | pRangeStart = pLoop->aLTerm[j++]; |
| 118674 | 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 ); |
| 118675 | 119209 | } |
| 118676 | 119210 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 118677 | 119211 | pRangeEnd = pLoop->aLTerm[j++]; |
| 118678 | 119212 | nExtraReg = 1; |
| 118679 | | - if( pRangeStart |
| 118680 | | - && (pRangeStart->wtFlags & TERM_LIKEOPT)!=0 |
| 118681 | | - && (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 |
| 118682 | | - ){ |
| 119213 | + if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 119214 | + assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 119215 | + assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
| 118683 | 119216 | pLevel->iLikeRepCntr = ++pParse->nMem; |
| 118684 | 119217 | testcase( bRev ); |
| 118685 | 119218 | testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 118686 | 119219 | sqlite3VdbeAddOp2(v, OP_Integer, |
| 118687 | 119220 | bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), |
| | @@ -118729,11 +119262,11 @@ |
| 118729 | 119262 | /* Seek the index cursor to the start of the range. */ |
| 118730 | 119263 | nConstraint = nEq; |
| 118731 | 119264 | if( pRangeStart ){ |
| 118732 | 119265 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 118733 | 119266 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 118734 | | - whereLikeOptimizationStringFixup(v, pLevel); |
| 119267 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
| 118735 | 119268 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 118736 | 119269 | && sqlite3ExprCanBeNull(pRight) |
| 118737 | 119270 | ){ |
| 118738 | 119271 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118739 | 119272 | VdbeCoverage(v); |
| | @@ -118775,11 +119308,11 @@ |
| 118775 | 119308 | nConstraint = nEq; |
| 118776 | 119309 | if( pRangeEnd ){ |
| 118777 | 119310 | Expr *pRight = pRangeEnd->pExpr->pRight; |
| 118778 | 119311 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 118779 | 119312 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
| 118780 | | - whereLikeOptimizationStringFixup(v, pLevel); |
| 119313 | + whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 118781 | 119314 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 118782 | 119315 | && sqlite3ExprCanBeNull(pRight) |
| 118783 | 119316 | ){ |
| 118784 | 119317 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 118785 | 119318 | VdbeCoverage(v); |
| | @@ -119852,10 +120385,14 @@ |
| 119852 | 120385 | ){ |
| 119853 | 120386 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 119854 | 120387 | } |
| 119855 | 120388 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 119856 | 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 | + |
| 119857 | 120394 | pNew->wsFlags = saved_wsFlags; |
| 119858 | 120395 | pNew->u.btree.nEq = saved_nEq; |
| 119859 | 120396 | pNew->nLTerm = saved_nLTerm; |
| 119860 | 120397 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 119861 | 120398 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| | @@ -119895,10 +120432,21 @@ |
| 119895 | 120432 | testcase( eOp & WO_GT ); |
| 119896 | 120433 | testcase( eOp & WO_GE ); |
| 119897 | 120434 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 119898 | 120435 | pBtm = pTerm; |
| 119899 | 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 | + } |
| 119900 | 120448 | }else{ |
| 119901 | 120449 | assert( eOp & (WO_LT|WO_LE) ); |
| 119902 | 120450 | testcase( eOp & WO_LT ); |
| 119903 | 120451 | testcase( eOp & WO_LE ); |
| 119904 | 120452 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| | @@ -121089,14 +121637,14 @@ |
| 121089 | 121637 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 121090 | 121638 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
| 121091 | 121639 | |
| 121092 | 121640 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 121093 | 121641 | ** |
| 121094 | | - ** TUNING: Do not let the number of iterations go above 25. If the cost |
| 121095 | | - ** 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 |
| 121096 | 121644 | ** rows, then do not use the automatic index. */ |
| 121097 | | - aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) ); |
| 121645 | + aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); |
| 121098 | 121646 | nFrom = 1; |
| 121099 | 121647 | assert( aFrom[0].isOrdered==0 ); |
| 121100 | 121648 | if( nOrderBy ){ |
| 121101 | 121649 | /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 121102 | 121650 | ** in this case the query may return a maximum of one row, the results |
| | @@ -121890,10 +122438,16 @@ |
| 121890 | 122438 | assert( pIx->pSchema==pTab->pSchema ); |
| 121891 | 122439 | assert( iIndexCur>=0 ); |
| 121892 | 122440 | if( op ){ |
| 121893 | 122441 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 121894 | 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 | + } |
| 121895 | 122449 | VdbeComment((v, "%s", pIx->zName)); |
| 121896 | 122450 | } |
| 121897 | 122451 | } |
| 121898 | 122452 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
| 121899 | 122453 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
| | @@ -124891,11 +125445,11 @@ |
| 124891 | 125445 | spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); |
| 124892 | 125446 | } |
| 124893 | 125447 | break; |
| 124894 | 125448 | case 193: /* expr ::= expr COLLATE ID|STRING */ |
| 124895 | 125449 | { |
| 124896 | | - 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); |
| 124897 | 125451 | yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart; |
| 124898 | 125452 | yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 124899 | 125453 | } |
| 124900 | 125454 | break; |
| 124901 | 125455 | case 194: /* expr ::= CAST LP expr AS typetoken RP */ |
| | @@ -125171,20 +125725,20 @@ |
| 125171 | 125725 | case 241: /* uniqueflag ::= */ |
| 125172 | 125726 | {yygotominor.yy328 = OE_None;} |
| 125173 | 125727 | break; |
| 125174 | 125728 | case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */ |
| 125175 | 125729 | { |
| 125176 | | - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0); |
| 125730 | + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1); |
| 125177 | 125731 | yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p); |
| 125178 | 125732 | sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1); |
| 125179 | 125733 | sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index"); |
| 125180 | 125734 | if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328; |
| 125181 | 125735 | } |
| 125182 | 125736 | break; |
| 125183 | 125737 | case 245: /* idxlist ::= nm collate sortorder */ |
| 125184 | 125738 | { |
| 125185 | | - Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0); |
| 125739 | + Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1); |
| 125186 | 125740 | yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p); |
| 125187 | 125741 | sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1); |
| 125188 | 125742 | sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index"); |
| 125189 | 125743 | if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328; |
| 125190 | 125744 | } |
| | @@ -126439,13 +126993,15 @@ |
| 126439 | 126993 | pParse->zTail = &zSql[i]; |
| 126440 | 126994 | } |
| 126441 | 126995 | sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
| 126442 | 126996 | } |
| 126443 | 126997 | #ifdef YYTRACKMAXSTACKDEPTH |
| 126998 | + sqlite3_mutex_enter(sqlite3MallocMutex()); |
| 126444 | 126999 | sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, |
| 126445 | 127000 | sqlite3ParserStackPeak(pEngine) |
| 126446 | 127001 | ); |
| 127002 | + sqlite3_mutex_leave(sqlite3MallocMutex()); |
| 126447 | 127003 | #endif /* YYDEBUG */ |
| 126448 | 127004 | sqlite3ParserFree(pEngine, sqlite3_free); |
| 126449 | 127005 | db->lookaside.bEnabled = enableLookaside; |
| 126450 | 127006 | if( db->mallocFailed ){ |
| 126451 | 127007 | pParse->rc = SQLITE_NOMEM; |
| | @@ -127011,10 +127567,15 @@ |
| 127011 | 127567 | rc = sqlite3_wsd_init(4096, 24); |
| 127012 | 127568 | if( rc!=SQLITE_OK ){ |
| 127013 | 127569 | return rc; |
| 127014 | 127570 | } |
| 127015 | 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*) ); |
| 127016 | 127577 | |
| 127017 | 127578 | /* If SQLite is already completely initialized, then this call |
| 127018 | 127579 | ** to sqlite3_initialize() should be a no-op. But the initialization |
| 127019 | 127580 | ** must be complete. So isInit must not be set until the very end |
| 127020 | 127581 | ** of this routine. |
| | @@ -132766,15 +133327,20 @@ |
| 132766 | 133327 | ** the output value undefined. Otherwise SQLITE_OK is returned. |
| 132767 | 133328 | ** |
| 132768 | 133329 | ** This function is used when parsing the "prefix=" FTS4 parameter. |
| 132769 | 133330 | */ |
| 132770 | 133331 | static int fts3GobbleInt(const char **pp, int *pnOut){ |
| 133332 | + const int MAX_NPREFIX = 10000000; |
| 132771 | 133333 | const char *p; /* Iterator pointer */ |
| 132772 | 133334 | int nInt = 0; /* Output value */ |
| 132773 | 133335 | |
| 132774 | 133336 | for(p=*pp; p[0]>='0' && p[0]<='9'; p++){ |
| 132775 | 133337 | nInt = nInt * 10 + (p[0] - '0'); |
| 133338 | + if( nInt>MAX_NPREFIX ){ |
| 133339 | + nInt = 0; |
| 133340 | + break; |
| 133341 | + } |
| 132776 | 133342 | } |
| 132777 | 133343 | if( p==*pp ) return SQLITE_ERROR; |
| 132778 | 133344 | *pnOut = nInt; |
| 132779 | 133345 | *pp = p; |
| 132780 | 133346 | return SQLITE_OK; |
| | @@ -132813,27 +133379,33 @@ |
| 132813 | 133379 | } |
| 132814 | 133380 | } |
| 132815 | 133381 | |
| 132816 | 133382 | aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex); |
| 132817 | 133383 | *apIndex = aIndex; |
| 132818 | | - *pnIndex = nIndex; |
| 132819 | 133384 | if( !aIndex ){ |
| 132820 | 133385 | return SQLITE_NOMEM; |
| 132821 | 133386 | } |
| 132822 | 133387 | |
| 132823 | 133388 | memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex); |
| 132824 | 133389 | if( zParam ){ |
| 132825 | 133390 | const char *p = zParam; |
| 132826 | 133391 | int i; |
| 132827 | 133392 | for(i=1; i<nIndex; i++){ |
| 132828 | | - int nPrefix; |
| 133393 | + int nPrefix = 0; |
| 132829 | 133394 | if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR; |
| 132830 | | - aIndex[i].nPrefix = nPrefix; |
| 133395 | + assert( nPrefix>=0 ); |
| 133396 | + if( nPrefix==0 ){ |
| 133397 | + nIndex--; |
| 133398 | + i--; |
| 133399 | + }else{ |
| 133400 | + aIndex[i].nPrefix = nPrefix; |
| 133401 | + } |
| 132831 | 133402 | p++; |
| 132832 | 133403 | } |
| 132833 | 133404 | } |
| 132834 | 133405 | |
| 133406 | + *pnIndex = nIndex; |
| 132835 | 133407 | return SQLITE_OK; |
| 132836 | 133408 | } |
| 132837 | 133409 | |
| 132838 | 133410 | /* |
| 132839 | 133411 | ** This function is called when initializing an FTS4 table that uses the |
| | @@ -140609,11 +141181,11 @@ |
| 140609 | 141181 | nName = sqlite3_value_bytes(argv[0])+1; |
| 140610 | 141182 | |
| 140611 | 141183 | if( argc==2 ){ |
| 140612 | 141184 | void *pOld; |
| 140613 | 141185 | int n = sqlite3_value_bytes(argv[1]); |
| 140614 | | - if( n!=sizeof(pPtr) ){ |
| 141186 | + if( zName==0 || n!=sizeof(pPtr) ){ |
| 140615 | 141187 | sqlite3_result_error(context, "argument type mismatch", -1); |
| 140616 | 141188 | return; |
| 140617 | 141189 | } |
| 140618 | 141190 | pPtr = *(void **)sqlite3_value_blob(argv[1]); |
| 140619 | 141191 | pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); |
| | @@ -140620,11 +141192,13 @@ |
| 140620 | 141192 | if( pOld==pPtr ){ |
| 140621 | 141193 | sqlite3_result_error(context, "out of memory", -1); |
| 140622 | 141194 | return; |
| 140623 | 141195 | } |
| 140624 | 141196 | }else{ |
| 140625 | | - pPtr = sqlite3Fts3HashFind(pHash, zName, nName); |
| 141197 | + if( zName ){ |
| 141198 | + pPtr = sqlite3Fts3HashFind(pHash, zName, nName); |
| 141199 | + } |
| 140626 | 141200 | if( !pPtr ){ |
| 140627 | 141201 | char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
| 140628 | 141202 | sqlite3_result_error(context, zErr, -1); |
| 140629 | 141203 | sqlite3_free(zErr); |
| 140630 | 141204 | return; |
| | @@ -140701,10 +141275,14 @@ |
| 140701 | 141275 | zCopy = sqlite3_mprintf("%s", zArg); |
| 140702 | 141276 | if( !zCopy ) return SQLITE_NOMEM; |
| 140703 | 141277 | zEnd = &zCopy[strlen(zCopy)]; |
| 140704 | 141278 | |
| 140705 | 141279 | z = (char *)sqlite3Fts3NextToken(zCopy, &n); |
| 141280 | + if( z==0 ){ |
| 141281 | + assert( n==0 ); |
| 141282 | + z = zCopy; |
| 141283 | + } |
| 140706 | 141284 | z[n] = '\0'; |
| 140707 | 141285 | sqlite3Fts3Dequote(z); |
| 140708 | 141286 | |
| 140709 | 141287 | m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1); |
| 140710 | 141288 | if( !m ){ |
| | @@ -143346,11 +143924,14 @@ |
| 143346 | 143924 | /* |
| 143347 | 143925 | ** This is a comparison function used as a qsort() callback when sorting |
| 143348 | 143926 | ** an array of pending terms by term. This occurs as part of flushing |
| 143349 | 143927 | ** the contents of the pending-terms hash table to the database. |
| 143350 | 143928 | */ |
| 143351 | | -static int fts3CompareElemByTerm(const void *lhs, const void *rhs){ |
| 143929 | +static int SQLITE_CDECL fts3CompareElemByTerm( |
| 143930 | + const void *lhs, |
| 143931 | + const void *rhs |
| 143932 | +){ |
| 143352 | 143933 | char *z1 = fts3HashKey(*(Fts3HashElem **)lhs); |
| 143353 | 143934 | char *z2 = fts3HashKey(*(Fts3HashElem **)rhs); |
| 143354 | 143935 | int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs); |
| 143355 | 143936 | int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs); |
| 143356 | 143937 | |
| 143357 | 143938 | |