| | @@ -1186,11 +1186,11 @@ |
| 1186 | 1186 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1187 | 1187 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1188 | 1188 | */ |
| 1189 | 1189 | #define SQLITE_VERSION "3.36.0" |
| 1190 | 1190 | #define SQLITE_VERSION_NUMBER 3036000 |
| 1191 | | -#define SQLITE_SOURCE_ID "2021-04-28 17:37:26 65ec39f0f092fe29e1d4e9e96cf07a73d2ef7ce2c41b6f1cd3ab23546ada0e67" |
| 1191 | +#define SQLITE_SOURCE_ID "2021-05-14 15:37:00 cf63abbe559d04f993f99a37d41ba4a97c0261094f1d4cc05cfa23b1e11731f5" |
| 1192 | 1192 | |
| 1193 | 1193 | /* |
| 1194 | 1194 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1195 | 1195 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1196 | 1196 | ** |
| | @@ -2202,10 +2202,14 @@ |
| 2202 | 2202 | ** currently has an SQL transaction open on the database. It is set to 0 if |
| 2203 | 2203 | ** the database is not a wal-mode db, or if there is no such connection in any |
| 2204 | 2204 | ** other process. This opcode cannot be used to detect transactions opened |
| 2205 | 2205 | ** by clients within the current process, only within other processes. |
| 2206 | 2206 | ** </ul> |
| 2207 | +** |
| 2208 | +** <li>[[SQLITE_FCNTL_CKSM_FILE]] |
| 2209 | +** Used by the cksmvfs VFS module only. |
| 2210 | +** </ul> |
| 2207 | 2211 | */ |
| 2208 | 2212 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 2209 | 2213 | #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 |
| 2210 | 2214 | #define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 |
| 2211 | 2215 | #define SQLITE_FCNTL_LAST_ERRNO 4 |
| | @@ -2241,12 +2245,12 @@ |
| 2241 | 2245 | #define SQLITE_FCNTL_DATA_VERSION 35 |
| 2242 | 2246 | #define SQLITE_FCNTL_SIZE_LIMIT 36 |
| 2243 | 2247 | #define SQLITE_FCNTL_CKPT_DONE 37 |
| 2244 | 2248 | #define SQLITE_FCNTL_RESERVE_BYTES 38 |
| 2245 | 2249 | #define SQLITE_FCNTL_CKPT_START 39 |
| 2246 | | - |
| 2247 | 2250 | #define SQLITE_FCNTL_EXTERNAL_READER 40 |
| 2251 | +#define SQLITE_FCNTL_CKSM_FILE 41 |
| 2248 | 2252 | |
| 2249 | 2253 | /* deprecated names */ |
| 2250 | 2254 | #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE |
| 2251 | 2255 | #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE |
| 2252 | 2256 | #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO |
| | @@ -5255,10 +5259,19 @@ |
| 5255 | 5259 | ** changes to the content of the database files on disk. |
| 5256 | 5260 | ** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since |
| 5257 | 5261 | ** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and |
| 5258 | 5262 | ** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so |
| 5259 | 5263 | ** sqlite3_stmt_readonly() returns false for those commands. |
| 5264 | +** |
| 5265 | +** ^This routine returns false if there is any possibility that the |
| 5266 | +** statement might change the database file. ^A false return does |
| 5267 | +** not guarantee that the statement will change the database file. |
| 5268 | +** ^For example, an UPDATE statement might have a WHERE clause that |
| 5269 | +** makes it a no-op, but the sqlite3_stmt_readonly() result would still |
| 5270 | +** be false. ^Similarly, a CREATE TABLE IF NOT EXISTS statement is a |
| 5271 | +** read-only no-op if the table already exists, but |
| 5272 | +** sqlite3_stmt_readonly() still returns false for such a statement. |
| 5260 | 5273 | */ |
| 5261 | 5274 | SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); |
| 5262 | 5275 | |
| 5263 | 5276 | /* |
| 5264 | 5277 | ** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement |
| | @@ -5424,22 +5437,26 @@ |
| 5424 | 5437 | ** terminated. If any NUL characters occurs at byte offsets less than |
| 5425 | 5438 | ** the value of the fourth parameter then the resulting string value will |
| 5426 | 5439 | ** contain embedded NULs. The result of expressions involving strings |
| 5427 | 5440 | ** with embedded NULs is undefined. |
| 5428 | 5441 | ** |
| 5429 | | -** ^The fifth argument to the BLOB and string binding interfaces |
| 5430 | | -** is a destructor used to dispose of the BLOB or |
| 5431 | | -** string after SQLite has finished with it. ^The destructor is called |
| 5432 | | -** to dispose of the BLOB or string even if the call to the bind API fails, |
| 5433 | | -** except the destructor is not called if the third parameter is a NULL |
| 5434 | | -** pointer or the fourth parameter is negative. |
| 5435 | | -** ^If the fifth argument is |
| 5436 | | -** the special value [SQLITE_STATIC], then SQLite assumes that the |
| 5437 | | -** information is in static, unmanaged space and does not need to be freed. |
| 5438 | | -** ^If the fifth argument has the value [SQLITE_TRANSIENT], then |
| 5439 | | -** SQLite makes its own private copy of the data immediately, before |
| 5440 | | -** the sqlite3_bind_*() routine returns. |
| 5442 | +** ^The fifth argument to the BLOB and string binding interfaces controls |
| 5443 | +** or indicates the lifetime of the object referenced by the third parameter. |
| 5444 | +** ^These three options exist: |
| 5445 | +** ^(1) A destructor to dispose of the BLOB or string after SQLite has finished |
| 5446 | +** with it may be passed. ^It is called to dispose of the BLOB or string even |
| 5447 | +** if the call to the bind API fails, except the destructor is not called if |
| 5448 | +** the third parameter is a NULL pointer or the fourth parameter is negative. |
| 5449 | +** ^(2) The special constant, [SQLITE_STATIC], may be passsed to indicate that |
| 5450 | +** the application remains responsible for disposing of the object. ^In this |
| 5451 | +** case, the object and the provided pointer to it must remain valid until |
| 5452 | +** either the prepared statement is finalized or the same SQL parameter is |
| 5453 | +** bound to something else, whichever occurs sooner. |
| 5454 | +** ^(3) The constant, [SQLITE_TRANSIENT], may be passed to indicate that the |
| 5455 | +** object is to be copied prior to the return from sqlite3_bind_*(). ^The |
| 5456 | +** object and pointer to it must remain valid until then. ^SQLite will then |
| 5457 | +** manage the lifetime of its private copy. |
| 5441 | 5458 | ** |
| 5442 | 5459 | ** ^The sixth argument to sqlite3_bind_text64() must be one of |
| 5443 | 5460 | ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] |
| 5444 | 5461 | ** to specify the encoding of the text in the third parameter. If |
| 5445 | 5462 | ** the sixth argument to sqlite3_bind_text64() is not one of the |
| | @@ -10875,12 +10892,12 @@ |
| 10875 | 10892 | ** |
| 10876 | 10893 | ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the |
| 10877 | 10894 | ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory |
| 10878 | 10895 | ** allocation error occurs. |
| 10879 | 10896 | ** |
| 10880 | | -** This interface is only available if SQLite is compiled with the |
| 10881 | | -** [SQLITE_ENABLE_DESERIALIZE] option. |
| 10897 | +** This interface is omitted if SQLite is compiled with the |
| 10898 | +** [SQLITE_OMIT_DESERIALIZE] option. |
| 10882 | 10899 | */ |
| 10883 | 10900 | SQLITE_API unsigned char *sqlite3_serialize( |
| 10884 | 10901 | sqlite3 *db, /* The database connection */ |
| 10885 | 10902 | const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */ |
| 10886 | 10903 | sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */ |
| | @@ -10927,12 +10944,12 @@ |
| 10927 | 10944 | ** |
| 10928 | 10945 | ** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the |
| 10929 | 10946 | ** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then |
| 10930 | 10947 | ** [sqlite3_free()] is invoked on argument P prior to returning. |
| 10931 | 10948 | ** |
| 10932 | | -** This interface is only available if SQLite is compiled with the |
| 10933 | | -** [SQLITE_ENABLE_DESERIALIZE] option. |
| 10949 | +** This interface is omitted if SQLite is compiled with the |
| 10950 | +** [SQLITE_OMIT_DESERIALIZE] option. |
| 10934 | 10951 | */ |
| 10935 | 10952 | SQLITE_API int sqlite3_deserialize( |
| 10936 | 10953 | sqlite3 *db, /* The database connection */ |
| 10937 | 10954 | const char *zSchema, /* Which DB to reopen with the deserialization */ |
| 10938 | 10955 | unsigned char *pData, /* The serialized database content */ |
| | @@ -11184,18 +11201,15 @@ |
| 11184 | 11201 | ** METHOD: sqlite3_session |
| 11185 | 11202 | ** |
| 11186 | 11203 | ** This method is used to configure a session object after it has been |
| 11187 | 11204 | ** created. At present the only valid value for the second parameter is |
| 11188 | 11205 | ** [SQLITE_SESSION_OBJCONFIG_SIZE]. |
| 11189 | | -*/ |
| 11190 | | -SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg); |
| 11191 | | - |
| 11192 | | -/* |
| 11193 | | -** CAPI3REF: Arguments for sqlite3session_object_config() |
| 11206 | +** |
| 11207 | +** Arguments for sqlite3session_object_config() |
| 11194 | 11208 | ** |
| 11195 | 11209 | ** The following values may passed as the the 4th parameter to |
| 11196 | | -** [sqlite3session_object_config]. |
| 11210 | +** sqlite3session_object_config(). |
| 11197 | 11211 | ** |
| 11198 | 11212 | ** <dt>SQLITE_SESSION_OBJCONFIG_SIZE <dd> |
| 11199 | 11213 | ** This option is used to set, clear or query the flag that enables |
| 11200 | 11214 | ** the [sqlite3session_changeset_size()] API. Because it imposes some |
| 11201 | 11215 | ** computational overhead, this API is disabled by default. Argument |
| | @@ -11206,10 +11220,14 @@ |
| 11206 | 11220 | ** variable is set to 1 if the sqlite3session_changeset_size() API is |
| 11207 | 11221 | ** enabled following the current call, or 0 otherwise. |
| 11208 | 11222 | ** |
| 11209 | 11223 | ** It is an error (SQLITE_MISUSE) to attempt to modify this setting after |
| 11210 | 11224 | ** the first table has been attached to the session object. |
| 11225 | +*/ |
| 11226 | +SQLITE_API int sqlite3session_object_config(sqlite3_session*, int op, void *pArg); |
| 11227 | + |
| 11228 | +/* |
| 11211 | 11229 | */ |
| 11212 | 11230 | #define SQLITE_SESSION_OBJCONFIG_SIZE 1 |
| 11213 | 11231 | |
| 11214 | 11232 | /* |
| 11215 | 11233 | ** CAPI3REF: Enable Or Disable A Session Object |
| | @@ -11454,15 +11472,15 @@ |
| 11454 | 11472 | void **ppChangeset /* OUT: Buffer containing changeset */ |
| 11455 | 11473 | ); |
| 11456 | 11474 | |
| 11457 | 11475 | /* |
| 11458 | 11476 | ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset |
| 11459 | | -** METHOD: sqlite3session_changeset_size() |
| 11477 | +** METHOD: sqlite3_session |
| 11460 | 11478 | ** |
| 11461 | 11479 | ** By default, this function always returns 0. For it to return |
| 11462 | 11480 | ** a useful result, the sqlite3_session object must have been configured |
| 11463 | | -** to enable this API using [sqlite3session_object_config()] with the |
| 11481 | +** to enable this API using sqlite3session_object_config() with the |
| 11464 | 11482 | ** SQLITE_SESSION_OBJCONFIG_SIZE verb. |
| 11465 | 11483 | ** |
| 11466 | 11484 | ** When enabled, this function returns an upper limit, in bytes, for the size |
| 11467 | 11485 | ** of the changeset that might be produced if sqlite3session_changeset() were |
| 11468 | 11486 | ** called. The final changeset size might be equal to or smaller than the |
| | @@ -17276,11 +17294,10 @@ |
| 17276 | 17294 | #define SQLITE_PushDown 0x00001000 /* The push-down optimization */ |
| 17277 | 17295 | #define SQLITE_SimplifyJoin 0x00002000 /* Convert LEFT JOIN to JOIN */ |
| 17278 | 17296 | #define SQLITE_SkipScan 0x00004000 /* Skip-scans */ |
| 17279 | 17297 | #define SQLITE_PropagateConst 0x00008000 /* The constant propagation opt */ |
| 17280 | 17298 | #define SQLITE_MinMaxOpt 0x00010000 /* The min/max optimization */ |
| 17281 | | -#define SQLITE_ExistsToIN 0x00020000 /* The EXISTS-to-IN optimization */ |
| 17282 | 17299 | #define SQLITE_AllOpts 0xffffffff /* All optimizations */ |
| 17283 | 17300 | |
| 17284 | 17301 | /* |
| 17285 | 17302 | ** Macros for testing whether or not optimizations are enabled or disabled. |
| 17286 | 17303 | */ |
| | @@ -19339,11 +19356,11 @@ |
| 19339 | 19356 | ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE. |
| 19340 | 19357 | */ |
| 19341 | 19358 | void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */ |
| 19342 | 19359 | void *pVdbeBranchArg; /* 1st argument */ |
| 19343 | 19360 | #endif |
| 19344 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 19361 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 19345 | 19362 | sqlite3_int64 mxMemdbSize; /* Default max memdb size */ |
| 19346 | 19363 | #endif |
| 19347 | 19364 | #ifndef SQLITE_UNTESTABLE |
| 19348 | 19365 | int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */ |
| 19349 | 19366 | #endif |
| | @@ -20235,11 +20252,11 @@ |
| 20235 | 20252 | |
| 20236 | 20253 | #if defined(SQLITE_NEED_ERR_NAME) |
| 20237 | 20254 | SQLITE_PRIVATE const char *sqlite3ErrName(int); |
| 20238 | 20255 | #endif |
| 20239 | 20256 | |
| 20240 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 20257 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 20241 | 20258 | SQLITE_PRIVATE int sqlite3MemdbInit(void); |
| 20242 | 20259 | #endif |
| 20243 | 20260 | |
| 20244 | 20261 | SQLITE_PRIVATE const char *sqlite3ErrStr(int); |
| 20245 | 20262 | SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); |
| | @@ -20997,11 +21014,11 @@ |
| 20997 | 21014 | #endif |
| 20998 | 21015 | #ifdef SQLITE_VDBE_COVERAGE |
| 20999 | 21016 | 0, /* xVdbeBranch */ |
| 21000 | 21017 | 0, /* pVbeBranchArg */ |
| 21001 | 21018 | #endif |
| 21002 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 21019 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 21003 | 21020 | SQLITE_MEMDB_DEFAULT_MAXSIZE, /* mxMemdbSize */ |
| 21004 | 21021 | #endif |
| 21005 | 21022 | #ifndef SQLITE_UNTESTABLE |
| 21006 | 21023 | 0, /* xTestCallback */ |
| 21007 | 21024 | #endif |
| | @@ -48428,35 +48445,92 @@ |
| 48428 | 48445 | ** |
| 48429 | 48446 | ** This file also implements interface sqlite3_serialize() and |
| 48430 | 48447 | ** sqlite3_deserialize(). |
| 48431 | 48448 | */ |
| 48432 | 48449 | /* #include "sqliteInt.h" */ |
| 48433 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 48450 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 48434 | 48451 | |
| 48435 | 48452 | /* |
| 48436 | 48453 | ** Forward declaration of objects used by this utility |
| 48437 | 48454 | */ |
| 48438 | 48455 | typedef struct sqlite3_vfs MemVfs; |
| 48439 | 48456 | typedef struct MemFile MemFile; |
| 48457 | +typedef struct MemStore MemStore; |
| 48440 | 48458 | |
| 48441 | 48459 | /* Access to a lower-level VFS that (might) implement dynamic loading, |
| 48442 | 48460 | ** access to randomness, etc. |
| 48443 | 48461 | */ |
| 48444 | 48462 | #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
| 48445 | 48463 | |
| 48446 | | -/* An open file */ |
| 48447 | | -struct MemFile { |
| 48448 | | - sqlite3_file base; /* IO methods */ |
| 48464 | +/* Storage for a memdb file. |
| 48465 | +** |
| 48466 | +** An memdb object can be shared or separate. Shared memdb objects can be |
| 48467 | +** used by more than one database connection. Mutexes are used by shared |
| 48468 | +** memdb objects to coordinate access. Separate memdb objects are only |
| 48469 | +** connected to a single database connection and do not require additional |
| 48470 | +** mutexes. |
| 48471 | +** |
| 48472 | +** Shared memdb objects have .zFName!=0 and .pMutex!=0. They are created |
| 48473 | +** using "file:/name?vfs=memdb". The first character of the name must be |
| 48474 | +** "/" or else the object will be a separate memdb object. All shared |
| 48475 | +** memdb objects are stored in memdb_g.apMemStore[] in an arbitrary order. |
| 48476 | +** |
| 48477 | +** Separate memdb objects are created using a name that does not begin |
| 48478 | +** with "/" or using sqlite3_deserialize(). |
| 48479 | +** |
| 48480 | +** Access rules for shared MemStore objects: |
| 48481 | +** |
| 48482 | +** * .zFName is initialized when the object is created and afterwards |
| 48483 | +** is unchanged until the object is destroyed. So it can be accessed |
| 48484 | +** at any time as long as we know the object is not being destroyed, |
| 48485 | +** which means while either the SQLITE_MUTEX_STATIC_VFS1 or |
| 48486 | +** .pMutex is held or the object is not part of memdb_g.apMemStore[]. |
| 48487 | +** |
| 48488 | +** * Can .pMutex can only be changed while holding the |
| 48489 | +** SQLITE_MUTEX_STATIC_VFS1 mutex or while the object is not part |
| 48490 | +** of memdb_g.apMemStore[]. |
| 48491 | +** |
| 48492 | +** * Other fields can only be changed while holding the .pMutex mutex |
| 48493 | +** or when the .nRef is less than zero and the object is not part of |
| 48494 | +** memdb_g.apMemStore[]. |
| 48495 | +** |
| 48496 | +** * The .aData pointer has the added requirement that it can can only |
| 48497 | +** be changed (for resizing) when nMmap is zero. |
| 48498 | +** |
| 48499 | +*/ |
| 48500 | +struct MemStore { |
| 48449 | 48501 | sqlite3_int64 sz; /* Size of the file */ |
| 48450 | 48502 | sqlite3_int64 szAlloc; /* Space allocated to aData */ |
| 48451 | 48503 | sqlite3_int64 szMax; /* Maximum allowed size of the file */ |
| 48452 | 48504 | unsigned char *aData; /* content of the file */ |
| 48505 | + sqlite3_mutex *pMutex; /* Used by shared stores only */ |
| 48453 | 48506 | int nMmap; /* Number of memory mapped pages */ |
| 48454 | 48507 | unsigned mFlags; /* Flags */ |
| 48508 | + int nRdLock; /* Number of readers */ |
| 48509 | + int nWrLock; /* Number of writers. (Always 0 or 1) */ |
| 48510 | + int nRef; /* Number of users of this MemStore */ |
| 48511 | + char *zFName; /* The filename for shared stores */ |
| 48512 | +}; |
| 48513 | + |
| 48514 | +/* An open file */ |
| 48515 | +struct MemFile { |
| 48516 | + sqlite3_file base; /* IO methods */ |
| 48517 | + MemStore *pStore; /* The storage */ |
| 48455 | 48518 | int eLock; /* Most recent lock against this file */ |
| 48456 | 48519 | }; |
| 48457 | 48520 | |
| 48521 | +/* |
| 48522 | +** Global variables for holding the memdb files that are accessible |
| 48523 | +** to multiple database connections in separate threads. |
| 48524 | +** |
| 48525 | +** Must hold SQLITE_MUTEX_STATIC_VFS1 to access any part of this object. |
| 48526 | +*/ |
| 48527 | +struct MemFS { |
| 48528 | + int nMemStore; /* Number of shared MemStore objects */ |
| 48529 | + MemStore **apMemStore; /* Array of all shared MemStore objects */ |
| 48530 | +} memdb_g; |
| 48531 | + |
| 48458 | 48532 | /* |
| 48459 | 48533 | ** Methods for MemFile |
| 48460 | 48534 | */ |
| 48461 | 48535 | static int memdbClose(sqlite3_file*); |
| 48462 | 48536 | static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| | @@ -48531,23 +48605,62 @@ |
| 48531 | 48605 | 0, /* xShmUnmap */ |
| 48532 | 48606 | memdbFetch, /* xFetch */ |
| 48533 | 48607 | memdbUnfetch /* xUnfetch */ |
| 48534 | 48608 | }; |
| 48535 | 48609 | |
| 48610 | +/* |
| 48611 | +** Enter/leave the mutex on a MemStore |
| 48612 | +*/ |
| 48613 | +static void memdbEnter(MemStore *p){ |
| 48614 | + sqlite3_mutex_enter(p->pMutex); |
| 48615 | +} |
| 48616 | +static void memdbLeave(MemStore *p){ |
| 48617 | + sqlite3_mutex_leave(p->pMutex); |
| 48618 | +} |
| 48619 | + |
| 48536 | 48620 | |
| 48537 | 48621 | |
| 48538 | 48622 | /* |
| 48539 | 48623 | ** Close an memdb-file. |
| 48540 | | -** |
| 48541 | | -** The pData pointer is owned by the application, so there is nothing |
| 48542 | | -** to free. Unless the SQLITE_DESERIALIZE_FREEONCLOSE flag is set, |
| 48543 | | -** in which case we own the pData pointer and need to free it. |
| 48624 | +** Free the underlying MemStore object when its refcount drops to zero |
| 48625 | +** or less. |
| 48544 | 48626 | */ |
| 48545 | 48627 | static int memdbClose(sqlite3_file *pFile){ |
| 48546 | | - MemFile *p = (MemFile *)pFile; |
| 48547 | | - if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){ |
| 48548 | | - sqlite3_free(p->aData); |
| 48628 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48629 | + if( p->zFName ){ |
| 48630 | + int i; |
| 48631 | +#ifndef SQLITE_MUTEX_OMIT |
| 48632 | + sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
| 48633 | +#endif |
| 48634 | + sqlite3_mutex_enter(pVfsMutex); |
| 48635 | + for(i=0; ALWAYS(i<memdb_g.nMemStore); i++){ |
| 48636 | + if( memdb_g.apMemStore[i]==p ){ |
| 48637 | + memdbEnter(p); |
| 48638 | + if( p->nRef==1 ){ |
| 48639 | + memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore]; |
| 48640 | + if( memdb_g.nMemStore==0 ){ |
| 48641 | + sqlite3_free(memdb_g.apMemStore); |
| 48642 | + memdb_g.apMemStore = 0; |
| 48643 | + } |
| 48644 | + } |
| 48645 | + break; |
| 48646 | + } |
| 48647 | + } |
| 48648 | + sqlite3_mutex_leave(pVfsMutex); |
| 48649 | + }else{ |
| 48650 | + memdbEnter(p); |
| 48651 | + } |
| 48652 | + p->nRef--; |
| 48653 | + if( p->nRef<=0 ){ |
| 48654 | + if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){ |
| 48655 | + sqlite3_free(p->aData); |
| 48656 | + } |
| 48657 | + memdbLeave(p); |
| 48658 | + sqlite3_mutex_free(p->pMutex); |
| 48659 | + sqlite3_free(p); |
| 48660 | + }else{ |
| 48661 | + memdbLeave(p); |
| 48549 | 48662 | } |
| 48550 | 48663 | return SQLITE_OK; |
| 48551 | 48664 | } |
| 48552 | 48665 | |
| 48553 | 48666 | /* |
| | @@ -48557,24 +48670,27 @@ |
| 48557 | 48670 | sqlite3_file *pFile, |
| 48558 | 48671 | void *zBuf, |
| 48559 | 48672 | int iAmt, |
| 48560 | 48673 | sqlite_int64 iOfst |
| 48561 | 48674 | ){ |
| 48562 | | - MemFile *p = (MemFile *)pFile; |
| 48675 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48676 | + memdbEnter(p); |
| 48563 | 48677 | if( iOfst+iAmt>p->sz ){ |
| 48564 | 48678 | memset(zBuf, 0, iAmt); |
| 48565 | 48679 | if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst); |
| 48680 | + memdbLeave(p); |
| 48566 | 48681 | return SQLITE_IOERR_SHORT_READ; |
| 48567 | 48682 | } |
| 48568 | 48683 | memcpy(zBuf, p->aData+iOfst, iAmt); |
| 48684 | + memdbLeave(p); |
| 48569 | 48685 | return SQLITE_OK; |
| 48570 | 48686 | } |
| 48571 | 48687 | |
| 48572 | 48688 | /* |
| 48573 | 48689 | ** Try to enlarge the memory allocation to hold at least sz bytes |
| 48574 | 48690 | */ |
| 48575 | | -static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){ |
| 48691 | +static int memdbEnlarge(MemStore *p, sqlite3_int64 newSz){ |
| 48576 | 48692 | unsigned char *pNew; |
| 48577 | 48693 | if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){ |
| 48578 | 48694 | return SQLITE_FULL; |
| 48579 | 48695 | } |
| 48580 | 48696 | if( newSz>p->szMax ){ |
| | @@ -48596,23 +48712,31 @@ |
| 48596 | 48712 | sqlite3_file *pFile, |
| 48597 | 48713 | const void *z, |
| 48598 | 48714 | int iAmt, |
| 48599 | 48715 | sqlite_int64 iOfst |
| 48600 | 48716 | ){ |
| 48601 | | - MemFile *p = (MemFile *)pFile; |
| 48602 | | - if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY; |
| 48717 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48718 | + memdbEnter(p); |
| 48719 | + if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){ |
| 48720 | + /* Can't happen: memdbLock() will return SQLITE_READONLY before |
| 48721 | + ** reaching this point */ |
| 48722 | + memdbLeave(p); |
| 48723 | + return SQLITE_IOERR_WRITE; |
| 48724 | + } |
| 48603 | 48725 | if( iOfst+iAmt>p->sz ){ |
| 48604 | 48726 | int rc; |
| 48605 | 48727 | if( iOfst+iAmt>p->szAlloc |
| 48606 | 48728 | && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK |
| 48607 | 48729 | ){ |
| 48730 | + memdbLeave(p); |
| 48608 | 48731 | return rc; |
| 48609 | 48732 | } |
| 48610 | 48733 | if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz); |
| 48611 | 48734 | p->sz = iOfst+iAmt; |
| 48612 | 48735 | } |
| 48613 | 48736 | memcpy(p->aData+iOfst, z, iAmt); |
| 48737 | + memdbLeave(p); |
| 48614 | 48738 | return SQLITE_OK; |
| 48615 | 48739 | } |
| 48616 | 48740 | |
| 48617 | 48741 | /* |
| 48618 | 48742 | ** Truncate an memdb-file. |
| | @@ -48620,14 +48744,20 @@ |
| 48620 | 48744 | ** In rollback mode (which is always the case for memdb, as it does not |
| 48621 | 48745 | ** support WAL mode) the truncate() method is only used to reduce |
| 48622 | 48746 | ** the size of a file, never to increase the size. |
| 48623 | 48747 | */ |
| 48624 | 48748 | static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 48625 | | - MemFile *p = (MemFile *)pFile; |
| 48626 | | - if( NEVER(size>p->sz) ) return SQLITE_FULL; |
| 48627 | | - p->sz = size; |
| 48628 | | - return SQLITE_OK; |
| 48749 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48750 | + int rc = SQLITE_OK; |
| 48751 | + memdbEnter(p); |
| 48752 | + if( NEVER(size>p->sz) ){ |
| 48753 | + rc = SQLITE_FULL; |
| 48754 | + }else{ |
| 48755 | + p->sz = size; |
| 48756 | + } |
| 48757 | + memdbLeave(p); |
| 48758 | + return rc; |
| 48629 | 48759 | } |
| 48630 | 48760 | |
| 48631 | 48761 | /* |
| 48632 | 48762 | ** Sync an memdb-file. |
| 48633 | 48763 | */ |
| | @@ -48637,45 +48767,78 @@ |
| 48637 | 48767 | |
| 48638 | 48768 | /* |
| 48639 | 48769 | ** Return the current file-size of an memdb-file. |
| 48640 | 48770 | */ |
| 48641 | 48771 | static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 48642 | | - MemFile *p = (MemFile *)pFile; |
| 48772 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48773 | + memdbEnter(p); |
| 48643 | 48774 | *pSize = p->sz; |
| 48775 | + memdbLeave(p); |
| 48644 | 48776 | return SQLITE_OK; |
| 48645 | 48777 | } |
| 48646 | 48778 | |
| 48647 | 48779 | /* |
| 48648 | 48780 | ** Lock an memdb-file. |
| 48649 | 48781 | */ |
| 48650 | 48782 | static int memdbLock(sqlite3_file *pFile, int eLock){ |
| 48651 | | - MemFile *p = (MemFile *)pFile; |
| 48652 | | - if( eLock>SQLITE_LOCK_SHARED |
| 48653 | | - && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0 |
| 48654 | | - ){ |
| 48655 | | - return SQLITE_READONLY; |
| 48656 | | - } |
| 48657 | | - p->eLock = eLock; |
| 48658 | | - return SQLITE_OK; |
| 48783 | + MemFile *pThis = (MemFile*)pFile; |
| 48784 | + MemStore *p = pThis->pStore; |
| 48785 | + int rc = SQLITE_OK; |
| 48786 | + if( eLock==pThis->eLock ) return SQLITE_OK; |
| 48787 | + memdbEnter(p); |
| 48788 | + if( eLock>SQLITE_LOCK_SHARED ){ |
| 48789 | + if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){ |
| 48790 | + rc = SQLITE_READONLY; |
| 48791 | + }else if( pThis->eLock<=SQLITE_LOCK_SHARED ){ |
| 48792 | + if( p->nWrLock ){ |
| 48793 | + rc = SQLITE_BUSY; |
| 48794 | + }else{ |
| 48795 | + p->nWrLock = 1; |
| 48796 | + } |
| 48797 | + } |
| 48798 | + }else if( eLock==SQLITE_LOCK_SHARED ){ |
| 48799 | + if( pThis->eLock > SQLITE_LOCK_SHARED ){ |
| 48800 | + assert( p->nWrLock==1 ); |
| 48801 | + p->nWrLock = 0; |
| 48802 | + }else if( p->nWrLock ){ |
| 48803 | + rc = SQLITE_BUSY; |
| 48804 | + }else{ |
| 48805 | + p->nRdLock++; |
| 48806 | + } |
| 48807 | + }else{ |
| 48808 | + assert( eLock==SQLITE_LOCK_NONE ); |
| 48809 | + if( pThis->eLock>SQLITE_LOCK_SHARED ){ |
| 48810 | + assert( p->nWrLock==1 ); |
| 48811 | + p->nWrLock = 0; |
| 48812 | + } |
| 48813 | + assert( p->nRdLock>0 ); |
| 48814 | + p->nRdLock--; |
| 48815 | + } |
| 48816 | + if( rc==SQLITE_OK ) pThis->eLock = eLock; |
| 48817 | + memdbLeave(p); |
| 48818 | + return rc; |
| 48659 | 48819 | } |
| 48660 | 48820 | |
| 48661 | | -#if 0 /* Never used because memdbAccess() always returns false */ |
| 48821 | +#if 0 |
| 48662 | 48822 | /* |
| 48663 | | -** Check if another file-handle holds a RESERVED lock on an memdb-file. |
| 48823 | +** This interface is only used for crash recovery, which does not |
| 48824 | +** occur on an in-memory database. |
| 48664 | 48825 | */ |
| 48665 | 48826 | static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 48666 | 48827 | *pResOut = 0; |
| 48667 | 48828 | return SQLITE_OK; |
| 48668 | 48829 | } |
| 48669 | 48830 | #endif |
| 48831 | + |
| 48670 | 48832 | |
| 48671 | 48833 | /* |
| 48672 | 48834 | ** File control method. For custom operations on an memdb-file. |
| 48673 | 48835 | */ |
| 48674 | 48836 | static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 48675 | | - MemFile *p = (MemFile *)pFile; |
| 48837 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48676 | 48838 | int rc = SQLITE_NOTFOUND; |
| 48839 | + memdbEnter(p); |
| 48677 | 48840 | if( op==SQLITE_FCNTL_VFSNAME ){ |
| 48678 | 48841 | *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz); |
| 48679 | 48842 | rc = SQLITE_OK; |
| 48680 | 48843 | } |
| 48681 | 48844 | if( op==SQLITE_FCNTL_SIZE_LIMIT ){ |
| | @@ -48689,10 +48852,11 @@ |
| 48689 | 48852 | } |
| 48690 | 48853 | p->szMax = iLimit; |
| 48691 | 48854 | *(sqlite3_int64*)pArg = iLimit; |
| 48692 | 48855 | rc = SQLITE_OK; |
| 48693 | 48856 | } |
| 48857 | + memdbLeave(p); |
| 48694 | 48858 | return rc; |
| 48695 | 48859 | } |
| 48696 | 48860 | |
| 48697 | 48861 | #if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| 48698 | 48862 | /* |
| | @@ -48718,47 +48882,110 @@ |
| 48718 | 48882 | sqlite3_file *pFile, |
| 48719 | 48883 | sqlite3_int64 iOfst, |
| 48720 | 48884 | int iAmt, |
| 48721 | 48885 | void **pp |
| 48722 | 48886 | ){ |
| 48723 | | - MemFile *p = (MemFile *)pFile; |
| 48887 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48888 | + memdbEnter(p); |
| 48724 | 48889 | if( iOfst+iAmt>p->sz ){ |
| 48725 | 48890 | *pp = 0; |
| 48726 | 48891 | }else{ |
| 48727 | 48892 | p->nMmap++; |
| 48728 | 48893 | *pp = (void*)(p->aData + iOfst); |
| 48729 | 48894 | } |
| 48895 | + memdbLeave(p); |
| 48730 | 48896 | return SQLITE_OK; |
| 48731 | 48897 | } |
| 48732 | 48898 | |
| 48733 | 48899 | /* Release a memory-mapped page */ |
| 48734 | 48900 | static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
| 48735 | | - MemFile *p = (MemFile *)pFile; |
| 48901 | + MemStore *p = ((MemFile*)pFile)->pStore; |
| 48902 | + memdbEnter(p); |
| 48736 | 48903 | p->nMmap--; |
| 48904 | + memdbLeave(p); |
| 48737 | 48905 | return SQLITE_OK; |
| 48738 | 48906 | } |
| 48739 | 48907 | |
| 48740 | 48908 | /* |
| 48741 | 48909 | ** Open an mem file handle. |
| 48742 | 48910 | */ |
| 48743 | 48911 | static int memdbOpen( |
| 48744 | 48912 | sqlite3_vfs *pVfs, |
| 48745 | 48913 | const char *zName, |
| 48746 | | - sqlite3_file *pFile, |
| 48914 | + sqlite3_file *pFd, |
| 48747 | 48915 | int flags, |
| 48748 | 48916 | int *pOutFlags |
| 48749 | 48917 | ){ |
| 48750 | | - MemFile *p = (MemFile*)pFile; |
| 48918 | + MemFile *pFile = (MemFile*)pFd; |
| 48919 | + MemStore *p = 0; |
| 48920 | + int szName; |
| 48751 | 48921 | if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
| 48752 | | - return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags); |
| 48922 | + return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFd, flags, pOutFlags); |
| 48753 | 48923 | } |
| 48754 | | - memset(p, 0, sizeof(*p)); |
| 48755 | | - p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 48924 | + memset(pFile, 0, sizeof(*p)); |
| 48925 | + szName = sqlite3Strlen30(zName); |
| 48926 | + if( szName>1 && zName[0]=='/' ){ |
| 48927 | + int i; |
| 48928 | +#ifndef SQLITE_MUTEX_OMIT |
| 48929 | + sqlite3_mutex *pVfsMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
| 48930 | +#endif |
| 48931 | + sqlite3_mutex_enter(pVfsMutex); |
| 48932 | + for(i=0; i<memdb_g.nMemStore; i++){ |
| 48933 | + if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){ |
| 48934 | + p = memdb_g.apMemStore[i]; |
| 48935 | + break; |
| 48936 | + } |
| 48937 | + } |
| 48938 | + if( p==0 ){ |
| 48939 | + MemStore **apNew; |
| 48940 | + p = sqlite3Malloc( sizeof(*p) + szName + 3 ); |
| 48941 | + if( p==0 ){ |
| 48942 | + sqlite3_mutex_leave(pVfsMutex); |
| 48943 | + return SQLITE_NOMEM; |
| 48944 | + } |
| 48945 | + apNew = sqlite3Realloc(memdb_g.apMemStore, |
| 48946 | + sizeof(apNew[0])*(memdb_g.nMemStore+1) ); |
| 48947 | + if( apNew==0 ){ |
| 48948 | + sqlite3_free(p); |
| 48949 | + sqlite3_mutex_leave(pVfsMutex); |
| 48950 | + return SQLITE_NOMEM; |
| 48951 | + } |
| 48952 | + apNew[memdb_g.nMemStore++] = p; |
| 48953 | + memdb_g.apMemStore = apNew; |
| 48954 | + memset(p, 0, sizeof(*p)); |
| 48955 | + p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE; |
| 48956 | + p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 48957 | + p->zFName = (char*)&p[1]; |
| 48958 | + memcpy(p->zFName, zName, szName+1); |
| 48959 | + p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 48960 | + if( p->pMutex==0 ){ |
| 48961 | + memdb_g.nMemStore--; |
| 48962 | + sqlite3_free(p); |
| 48963 | + sqlite3_mutex_leave(pVfsMutex); |
| 48964 | + return SQLITE_NOMEM; |
| 48965 | + } |
| 48966 | + p->nRef = 1; |
| 48967 | + memdbEnter(p); |
| 48968 | + }else{ |
| 48969 | + memdbEnter(p); |
| 48970 | + p->nRef++; |
| 48971 | + } |
| 48972 | + sqlite3_mutex_leave(pVfsMutex); |
| 48973 | + }else{ |
| 48974 | + p = sqlite3Malloc( sizeof(*p) ); |
| 48975 | + if( p==0 ){ |
| 48976 | + return SQLITE_NOMEM; |
| 48977 | + } |
| 48978 | + memset(p, 0, sizeof(*p)); |
| 48979 | + p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE; |
| 48980 | + p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 48981 | + } |
| 48982 | + pFile->pStore = p; |
| 48756 | 48983 | assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */ |
| 48757 | 48984 | *pOutFlags = flags | SQLITE_OPEN_MEMORY; |
| 48758 | | - pFile->pMethods = &memdb_io_methods; |
| 48759 | | - p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 48985 | + pFd->pMethods = &memdb_io_methods; |
| 48986 | + memdbLeave(p); |
| 48760 | 48987 | return SQLITE_OK; |
| 48761 | 48988 | } |
| 48762 | 48989 | |
| 48763 | 48990 | #if 0 /* Only used to delete rollback journals, super-journals, and WAL |
| 48764 | 48991 | ** files, none of which exist in memdb. So this routine is never used */ |
| | @@ -48869,13 +49096,18 @@ |
| 48869 | 49096 | ** Translate a database connection pointer and schema name into a |
| 48870 | 49097 | ** MemFile pointer. |
| 48871 | 49098 | */ |
| 48872 | 49099 | static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){ |
| 48873 | 49100 | MemFile *p = 0; |
| 49101 | + MemStore *pStore; |
| 48874 | 49102 | int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p); |
| 48875 | 49103 | if( rc ) return 0; |
| 48876 | 49104 | if( p->base.pMethods!=&memdb_io_methods ) return 0; |
| 49105 | + pStore = p->pStore; |
| 49106 | + memdbEnter(pStore); |
| 49107 | + if( pStore->zFName!=0 ) p = 0; |
| 49108 | + memdbLeave(pStore); |
| 48877 | 49109 | return p; |
| 48878 | 49110 | } |
| 48879 | 49111 | |
| 48880 | 49112 | /* |
| 48881 | 49113 | ** Return the serialization of a database |
| | @@ -48907,16 +49139,18 @@ |
| 48907 | 49139 | p = memdbFromDbSchema(db, zSchema); |
| 48908 | 49140 | iDb = sqlite3FindDbName(db, zSchema); |
| 48909 | 49141 | if( piSize ) *piSize = -1; |
| 48910 | 49142 | if( iDb<0 ) return 0; |
| 48911 | 49143 | if( p ){ |
| 48912 | | - if( piSize ) *piSize = p->sz; |
| 49144 | + MemStore *pStore = p->pStore; |
| 49145 | + assert( pStore->pMutex==0 ); |
| 49146 | + if( piSize ) *piSize = pStore->sz; |
| 48913 | 49147 | if( mFlags & SQLITE_SERIALIZE_NOCOPY ){ |
| 48914 | | - pOut = p->aData; |
| 49148 | + pOut = pStore->aData; |
| 48915 | 49149 | }else{ |
| 48916 | | - pOut = sqlite3_malloc64( p->sz ); |
| 48917 | | - if( pOut ) memcpy(pOut, p->aData, p->sz); |
| 49150 | + pOut = sqlite3_malloc64( pStore->sz ); |
| 49151 | + if( pOut ) memcpy(pOut, pStore->aData, pStore->sz); |
| 48918 | 49152 | } |
| 48919 | 49153 | return pOut; |
| 48920 | 49154 | } |
| 48921 | 49155 | pBt = db->aDb[iDb].pBt; |
| 48922 | 49156 | if( pBt==0 ) return 0; |
| | @@ -49006,19 +49240,20 @@ |
| 49006 | 49240 | } |
| 49007 | 49241 | p = memdbFromDbSchema(db, zSchema); |
| 49008 | 49242 | if( p==0 ){ |
| 49009 | 49243 | rc = SQLITE_ERROR; |
| 49010 | 49244 | }else{ |
| 49011 | | - p->aData = pData; |
| 49245 | + MemStore *pStore = p->pStore; |
| 49246 | + pStore->aData = pData; |
| 49012 | 49247 | pData = 0; |
| 49013 | | - p->sz = szDb; |
| 49014 | | - p->szAlloc = szBuf; |
| 49015 | | - p->szMax = szBuf; |
| 49016 | | - if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){ |
| 49017 | | - p->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 49248 | + pStore->sz = szDb; |
| 49249 | + pStore->szAlloc = szBuf; |
| 49250 | + pStore->szMax = szBuf; |
| 49251 | + if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){ |
| 49252 | + pStore->szMax = sqlite3GlobalConfig.mxMemdbSize; |
| 49018 | 49253 | } |
| 49019 | | - p->mFlags = mFlags; |
| 49254 | + pStore->mFlags = mFlags; |
| 49020 | 49255 | rc = SQLITE_OK; |
| 49021 | 49256 | } |
| 49022 | 49257 | |
| 49023 | 49258 | end_deserialize: |
| 49024 | 49259 | sqlite3_finalize(pStmt); |
| | @@ -49043,11 +49278,11 @@ |
| 49043 | 49278 | ** is no way to reach it under most builds. */ |
| 49044 | 49279 | if( sz<sizeof(MemFile) ) sz = sizeof(MemFile); /*NO_TEST*/ |
| 49045 | 49280 | memdb_vfs.szOsFile = sz; |
| 49046 | 49281 | return sqlite3_vfs_register(&memdb_vfs, 0); |
| 49047 | 49282 | } |
| 49048 | | -#endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 49283 | +#endif /* SQLITE_OMIT_DESERIALIZE */ |
| 49049 | 49284 | |
| 49050 | 49285 | /************** End of memdb.c ***********************************************/ |
| 49051 | 49286 | /************** Begin file bitvec.c ******************************************/ |
| 49052 | 49287 | /* |
| 49053 | 49288 | ** 2008 February 16 |
| | @@ -56958,11 +57193,11 @@ |
| 56958 | 57193 | u8 *pPtr; |
| 56959 | 57194 | Pager *pPager = 0; /* Pager object to allocate and return */ |
| 56960 | 57195 | int rc = SQLITE_OK; /* Return code */ |
| 56961 | 57196 | int tempFile = 0; /* True for temp files (incl. in-memory files) */ |
| 56962 | 57197 | int memDb = 0; /* True if this is an in-memory file */ |
| 56963 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 57198 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 56964 | 57199 | int memJM = 0; /* Memory journal mode */ |
| 56965 | 57200 | #else |
| 56966 | 57201 | # define memJM 0 |
| 56967 | 57202 | #endif |
| 56968 | 57203 | int readOnly = 0; /* True if this is a read-only file */ |
| | @@ -57162,11 +57397,11 @@ |
| 57162 | 57397 | */ |
| 57163 | 57398 | if( zFilename && zFilename[0] ){ |
| 57164 | 57399 | int fout = 0; /* VFS flags returned by xOpen() */ |
| 57165 | 57400 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout); |
| 57166 | 57401 | assert( !memDb ); |
| 57167 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 57402 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 57168 | 57403 | memJM = (fout&SQLITE_OPEN_MEMORY)!=0; |
| 57169 | 57404 | #endif |
| 57170 | 57405 | readOnly = (fout&SQLITE_OPEN_READONLY)!=0; |
| 57171 | 57406 | |
| 57172 | 57407 | /* If the file was successfully opened for read/write access, |
| | @@ -61004,11 +61239,10 @@ |
| 61004 | 61239 | static void walCleanupHash(Wal *pWal){ |
| 61005 | 61240 | WalHashLoc sLoc; /* Hash table location */ |
| 61006 | 61241 | int iLimit = 0; /* Zero values greater than this */ |
| 61007 | 61242 | int nByte; /* Number of bytes to zero in aPgno[] */ |
| 61008 | 61243 | int i; /* Used to iterate through aHash[] */ |
| 61009 | | - int rc; /* Return code form walHashGet() */ |
| 61010 | 61244 | |
| 61011 | 61245 | assert( pWal->writeLock ); |
| 61012 | 61246 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 ); |
| 61013 | 61247 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE ); |
| 61014 | 61248 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 ); |
| | @@ -61019,12 +61253,12 @@ |
| 61019 | 61253 | ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed |
| 61020 | 61254 | ** that the page said hash-table and array reside on is already mapped.(1) |
| 61021 | 61255 | */ |
| 61022 | 61256 | assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); |
| 61023 | 61257 | assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); |
| 61024 | | - rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc); |
| 61025 | | - if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */ |
| 61258 | + i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc); |
| 61259 | + if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */ |
| 61026 | 61260 | |
| 61027 | 61261 | /* Zero all hash-table entries that correspond to frame numbers greater |
| 61028 | 61262 | ** than pWal->hdr.mxFrame. |
| 61029 | 61263 | */ |
| 61030 | 61264 | iLimit = pWal->hdr.mxFrame - sLoc.iZero; |
| | @@ -72271,11 +72505,13 @@ |
| 72271 | 72505 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 72272 | 72506 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 72273 | 72507 | } |
| 72274 | 72508 | pFree = pCell; |
| 72275 | 72509 | szFree = sz; |
| 72276 | | - if( pFree+sz>pEnd ) return 0; |
| 72510 | + if( pFree+sz>pEnd ){ |
| 72511 | + return 0; |
| 72512 | + } |
| 72277 | 72513 | }else{ |
| 72278 | 72514 | pFree = pCell; |
| 72279 | 72515 | szFree += sz; |
| 72280 | 72516 | } |
| 72281 | 72517 | nRet++; |
| | @@ -73247,10 +73483,11 @@ |
| 73247 | 73483 | /* Insert new divider cells into pParent. */ |
| 73248 | 73484 | for(i=0; i<nNew-1; i++){ |
| 73249 | 73485 | u8 *pCell; |
| 73250 | 73486 | u8 *pTemp; |
| 73251 | 73487 | int sz; |
| 73488 | + u8 *pSrcEnd; |
| 73252 | 73489 | MemPage *pNew = apNew[i]; |
| 73253 | 73490 | j = cntNew[i]; |
| 73254 | 73491 | |
| 73255 | 73492 | assert( j<nMaxCells ); |
| 73256 | 73493 | assert( b.apCell[j]!=0 ); |
| | @@ -73290,10 +73527,16 @@ |
| 73290 | 73527 | } |
| 73291 | 73528 | } |
| 73292 | 73529 | iOvflSpace += sz; |
| 73293 | 73530 | assert( sz<=pBt->maxLocal+23 ); |
| 73294 | 73531 | assert( iOvflSpace <= (int)pBt->pageSize ); |
| 73532 | + for(k=0; b.ixNx[k]<=i && ALWAYS(k<NB*2); k++){} |
| 73533 | + pSrcEnd = b.apEnd[k]; |
| 73534 | + if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){ |
| 73535 | + rc = SQLITE_CORRUPT_BKPT; |
| 73536 | + goto balance_cleanup; |
| 73537 | + } |
| 73295 | 73538 | insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
| 73296 | 73539 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 73297 | 73540 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 73298 | 73541 | } |
| 73299 | 73542 | |
| | @@ -86617,10 +86860,15 @@ |
| 86617 | 86860 | printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg])); |
| 86618 | 86861 | } |
| 86619 | 86862 | printf("\n"); |
| 86620 | 86863 | sqlite3VdbeCheckMemInvariants(p); |
| 86621 | 86864 | } |
| 86865 | +SQLITE_PRIVATE void sqlite3PrintMem(Mem *pMem){ |
| 86866 | + memTracePrint(pMem); |
| 86867 | + printf("\n"); |
| 86868 | + fflush(stdout); |
| 86869 | +} |
| 86622 | 86870 | #endif |
| 86623 | 86871 | |
| 86624 | 86872 | #ifdef SQLITE_DEBUG |
| 86625 | 86873 | /* |
| 86626 | 86874 | ** Show the values of all registers in the virtual machine. Used for |
| | @@ -90667,12 +90915,22 @@ |
| 90667 | 90915 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 90668 | 90916 | pC = p->apCsr[pOp->p1]; |
| 90669 | 90917 | assert( pC!=0 ); |
| 90670 | 90918 | assert( pOp->p3>=pOp->p2 ); |
| 90671 | 90919 | if( pC->seekHit<pOp->p2 ){ |
| 90920 | +#ifdef SQLITE_DEBUG |
| 90921 | + if( db->flags&SQLITE_VdbeTrace ){ |
| 90922 | + printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2); |
| 90923 | + } |
| 90924 | +#endif |
| 90672 | 90925 | pC->seekHit = pOp->p2; |
| 90673 | 90926 | }else if( pC->seekHit>pOp->p3 ){ |
| 90927 | +#ifdef SQLITE_DEBUG |
| 90928 | + if( db->flags&SQLITE_VdbeTrace ){ |
| 90929 | + printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3); |
| 90930 | + } |
| 90931 | +#endif |
| 90674 | 90932 | pC->seekHit = pOp->p3; |
| 90675 | 90933 | } |
| 90676 | 90934 | break; |
| 90677 | 90935 | } |
| 90678 | 90936 | |
| | @@ -90783,10 +91041,15 @@ |
| 90783 | 91041 | case OP_IfNoHope: { /* jump, in3 */ |
| 90784 | 91042 | VdbeCursor *pC; |
| 90785 | 91043 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 90786 | 91044 | pC = p->apCsr[pOp->p1]; |
| 90787 | 91045 | assert( pC!=0 ); |
| 91046 | +#ifdef SQLITE_DEBUG |
| 91047 | + if( db->flags&SQLITE_VdbeTrace ){ |
| 91048 | + printf("seekHit is %d\n", pC->seekHit); |
| 91049 | + } |
| 91050 | +#endif |
| 90788 | 91051 | if( pC->seekHit>=pOp->p4.i ) break; |
| 90789 | 91052 | /* Fall through into OP_NotFound */ |
| 90790 | 91053 | /* no break */ deliberate_fall_through |
| 90791 | 91054 | } |
| 90792 | 91055 | case OP_NoConflict: /* jump, in3 */ |
| | @@ -100897,22 +101160,11 @@ |
| 100897 | 101160 | Parse *pParse, /* Parsing context */ |
| 100898 | 101161 | Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
| 100899 | 101162 | const Token *pCollName, /* Name of collating sequence */ |
| 100900 | 101163 | int dequote /* True to dequote pCollName */ |
| 100901 | 101164 | ){ |
| 100902 | | - assert( pExpr!=0 || pParse->db->mallocFailed ); |
| 100903 | | - if( pExpr==0 ) return 0; |
| 100904 | | - if( pExpr->op==TK_VECTOR ){ |
| 100905 | | - ExprList *pList = pExpr->x.pList; |
| 100906 | | - if( ALWAYS(pList!=0) ){ |
| 100907 | | - int i; |
| 100908 | | - for(i=0; i<pList->nExpr; i++){ |
| 100909 | | - pList->a[i].pExpr = sqlite3ExprAddCollateToken(pParse,pList->a[i].pExpr, |
| 100910 | | - pCollName, dequote); |
| 100911 | | - } |
| 100912 | | - } |
| 100913 | | - }else if( pCollName->n>0 ){ |
| 101165 | + if( pCollName->n>0 ){ |
| 100914 | 101166 | Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); |
| 100915 | 101167 | if( pNew ){ |
| 100916 | 101168 | pNew->pLeft = pExpr; |
| 100917 | 101169 | pNew->flags |= EP_Collate|EP_Skip; |
| 100918 | 101170 | pExpr = pNew; |
| | @@ -107808,11 +108060,11 @@ |
| 107808 | 108060 | */ |
| 107809 | 108061 | static int renameUnmapSelectCb(Walker *pWalker, Select *p){ |
| 107810 | 108062 | Parse *pParse = pWalker->pParse; |
| 107811 | 108063 | int i; |
| 107812 | 108064 | if( pParse->nErr ) return WRC_Abort; |
| 107813 | | - if( NEVER(p->selFlags & SF_View) ) return WRC_Prune; |
| 108065 | + if( p->selFlags & SF_View ) return WRC_Prune; |
| 107814 | 108066 | if( ALWAYS(p->pEList) ){ |
| 107815 | 108067 | ExprList *pList = p->pEList; |
| 107816 | 108068 | for(i=0; i<pList->nExpr; i++){ |
| 107817 | 108069 | if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){ |
| 107818 | 108070 | sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName); |
| | @@ -111180,11 +111432,11 @@ |
| 111180 | 111432 | zFile = (const char *)sqlite3_value_text(argv[0]); |
| 111181 | 111433 | zName = (const char *)sqlite3_value_text(argv[1]); |
| 111182 | 111434 | if( zFile==0 ) zFile = ""; |
| 111183 | 111435 | if( zName==0 ) zName = ""; |
| 111184 | 111436 | |
| 111185 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 111437 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 111186 | 111438 | # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb) |
| 111187 | 111439 | #else |
| 111188 | 111440 | # define REOPEN_AS_MEMDB(db) (0) |
| 111189 | 111441 | #endif |
| 111190 | 111442 | |
| | @@ -113000,10 +113252,26 @@ |
| 113000 | 113252 | /* iCol is a normal or stored column */ |
| 113001 | 113253 | return n; |
| 113002 | 113254 | } |
| 113003 | 113255 | } |
| 113004 | 113256 | #endif |
| 113257 | + |
| 113258 | +/* |
| 113259 | +** Insert a single OP_JournalMode query opcode in order to force the |
| 113260 | +** prepared statement to return false for sqlite3_stmt_readonly(). This |
| 113261 | +** is used by CREATE TABLE IF NOT EXISTS and similar if the table already |
| 113262 | +** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS |
| 113263 | +** will return false for sqlite3_stmt_readonly() even if that statement |
| 113264 | +** is a read-only no-op. |
| 113265 | +*/ |
| 113266 | +static void sqlite3ForceNotReadOnly(Parse *pParse){ |
| 113267 | + int iReg = ++pParse->nMem; |
| 113268 | + Vdbe *v = sqlite3GetVdbe(pParse); |
| 113269 | + if( v ){ |
| 113270 | + sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); |
| 113271 | + } |
| 113272 | +} |
| 113005 | 113273 | |
| 113006 | 113274 | /* |
| 113007 | 113275 | ** Begin constructing a new table representation in memory. This is |
| 113008 | 113276 | ** the first of several action routines that get called in response |
| 113009 | 113277 | ** to a CREATE TABLE statement. In particular, this routine is called |
| | @@ -113100,10 +113368,11 @@ |
| 113100 | 113368 | if( !noErr ){ |
| 113101 | 113369 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 113102 | 113370 | }else{ |
| 113103 | 113371 | assert( !db->init.busy || CORRUPT_DB ); |
| 113104 | 113372 | sqlite3CodeVerifySchema(pParse, iDb); |
| 113373 | + sqlite3ForceNotReadOnly(pParse); |
| 113105 | 113374 | } |
| 113106 | 113375 | goto begin_table_error; |
| 113107 | 113376 | } |
| 113108 | 113377 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
| 113109 | 113378 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| | @@ -115150,11 +115419,14 @@ |
| 115150 | 115419 | assert( isView==0 || isView==LOCATE_VIEW ); |
| 115151 | 115420 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
| 115152 | 115421 | if( noErr ) db->suppressErr--; |
| 115153 | 115422 | |
| 115154 | 115423 | if( pTab==0 ){ |
| 115155 | | - if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 115424 | + if( noErr ){ |
| 115425 | + sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 115426 | + sqlite3ForceNotReadOnly(pParse); |
| 115427 | + } |
| 115156 | 115428 | goto exit_drop_table; |
| 115157 | 115429 | } |
| 115158 | 115430 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 115159 | 115431 | assert( iDb>=0 && iDb<db->nDb ); |
| 115160 | 115432 | |
| | @@ -115720,10 +115992,11 @@ |
| 115720 | 115992 | if( !ifNotExist ){ |
| 115721 | 115993 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 115722 | 115994 | }else{ |
| 115723 | 115995 | assert( !db->init.busy ); |
| 115724 | 115996 | sqlite3CodeVerifySchema(pParse, iDb); |
| 115997 | + sqlite3ForceNotReadOnly(pParse); |
| 115725 | 115998 | } |
| 115726 | 115999 | goto exit_create_index; |
| 115727 | 116000 | } |
| 115728 | 116001 | } |
| 115729 | 116002 | }else{ |
| | @@ -116200,11 +116473,11 @@ |
| 116200 | 116473 | x = pIdx->pTable->nRowLogEst; |
| 116201 | 116474 | assert( 99==sqlite3LogEst(1000) ); |
| 116202 | 116475 | if( x<99 ){ |
| 116203 | 116476 | pIdx->pTable->nRowLogEst = x = 99; |
| 116204 | 116477 | } |
| 116205 | | - if( pIdx->pPartIdxWhere!=0 ) x -= 10; assert( 10==sqlite3LogEst(2) ); |
| 116478 | + if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } |
| 116206 | 116479 | a[0] = x; |
| 116207 | 116480 | |
| 116208 | 116481 | /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 116209 | 116482 | ** 6 and each subsequent value (if any) is 5. */ |
| 116210 | 116483 | memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
| | @@ -116238,10 +116511,11 @@ |
| 116238 | 116511 | if( pIndex==0 ){ |
| 116239 | 116512 | if( !ifExists ){ |
| 116240 | 116513 | sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); |
| 116241 | 116514 | }else{ |
| 116242 | 116515 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 116516 | + sqlite3ForceNotReadOnly(pParse); |
| 116243 | 116517 | } |
| 116244 | 116518 | pParse->checkSchema = 1; |
| 116245 | 116519 | goto exit_drop_index; |
| 116246 | 116520 | } |
| 116247 | 116521 | if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
| | @@ -136029,11 +136303,11 @@ |
| 136029 | 136303 | ** is part of the AND-connected terms of the expression. For each term |
| 136030 | 136304 | ** found, add it to the pConst structure. |
| 136031 | 136305 | */ |
| 136032 | 136306 | static void findConstInWhere(WhereConst *pConst, Expr *pExpr){ |
| 136033 | 136307 | Expr *pRight, *pLeft; |
| 136034 | | - if( pExpr==0 ) return; |
| 136308 | + if( NEVER(pExpr==0) ) return; |
| 136035 | 136309 | if( ExprHasProperty(pExpr, EP_FromJoin) ) return; |
| 136036 | 136310 | if( pExpr->op==TK_AND ){ |
| 136037 | 136311 | findConstInWhere(pConst, pExpr->pRight); |
| 136038 | 136312 | findConstInWhere(pConst, pExpr->pLeft); |
| 136039 | 136313 | return; |
| | @@ -137888,11 +138162,12 @@ |
| 137888 | 138162 | /* Do the WHERE-clause constant propagation optimization if this is |
| 137889 | 138163 | ** a join. No need to speed time on this operation for non-join queries |
| 137890 | 138164 | ** as the equivalent optimization will be handled by query planner in |
| 137891 | 138165 | ** sqlite3WhereBegin(). |
| 137892 | 138166 | */ |
| 137893 | | - if( pTabList->nSrc>1 |
| 138167 | + if( p->pWhere!=0 |
| 138168 | + && p->pWhere->op==TK_AND |
| 137894 | 138169 | && OptimizationEnabled(db, SQLITE_PropagateConst) |
| 137895 | 138170 | && propagateConstants(pParse, p) |
| 137896 | 138171 | ){ |
| 137897 | 138172 | #if SELECTTRACE_ENABLED |
| 137898 | 138173 | if( sqlite3SelectTrace & 0x100 ){ |
| | @@ -144173,12 +144448,12 @@ |
| 144173 | 144448 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 144174 | 144449 | WhereClause *pWC; /* WhereClause currently being scanned */ |
| 144175 | 144450 | const char *zCollName; /* Required collating sequence, if not NULL */ |
| 144176 | 144451 | Expr *pIdxExpr; /* Search for this index expression */ |
| 144177 | 144452 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 144178 | | - unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 144179 | | - unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 144453 | + unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */ |
| 144454 | + unsigned char iEquiv; /* Next unused slot in aiCur[] and aiColumn[] */ |
| 144180 | 144455 | u32 opMask; /* Acceptable operators */ |
| 144181 | 144456 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 144182 | 144457 | int aiCur[11]; /* Cursors in the equivalence class */ |
| 144183 | 144458 | i16 aiColumn[11]; /* Corresponding column number in the eq-class */ |
| 144184 | 144459 | }; |
| | @@ -144483,10 +144758,11 @@ |
| 144483 | 144758 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 144484 | 144759 | #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 144485 | 144760 | #define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ |
| 144486 | 144761 | #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ |
| 144487 | 144762 | #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ |
| 144763 | +#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ |
| 144488 | 144764 | |
| 144489 | 144765 | #endif /* !defined(SQLITE_WHEREINT_H) */ |
| 144490 | 144766 | |
| 144491 | 144767 | /************** End of whereInt.h ********************************************/ |
| 144492 | 144768 | /************** Continuing where we left off in wherecode.c ******************/ |
| | @@ -144766,10 +145042,16 @@ |
| 144766 | 145042 | if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 144767 | 145043 | pTerm->wtFlags |= TERM_LIKECOND; |
| 144768 | 145044 | }else{ |
| 144769 | 145045 | pTerm->wtFlags |= TERM_CODED; |
| 144770 | 145046 | } |
| 145047 | +#ifdef WHERETRACE_ENABLED |
| 145048 | + if( sqlite3WhereTrace & 0x20000 ){ |
| 145049 | + sqlite3DebugPrintf("DISABLE-"); |
| 145050 | + sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a))); |
| 145051 | + } |
| 145052 | +#endif |
| 144771 | 145053 | if( pTerm->iParent<0 ) break; |
| 144772 | 145054 | pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 144773 | 145055 | assert( pTerm!=0 ); |
| 144774 | 145056 | pTerm->nChild--; |
| 144775 | 145057 | if( pTerm->nChild!=0 ) break; |
| | @@ -145083,11 +145365,26 @@ |
| 145083 | 145365 | pLevel->u.in.nIn = 0; |
| 145084 | 145366 | } |
| 145085 | 145367 | sqlite3DbFree(pParse->db, aiMap); |
| 145086 | 145368 | #endif |
| 145087 | 145369 | } |
| 145088 | | - disableTerm(pLevel, pTerm); |
| 145370 | + |
| 145371 | + /* As an optimization, try to disable the WHERE clause term that is |
| 145372 | + ** driving the index as it will always be true. The correct answer is |
| 145373 | + ** obtained regardless, but we might get the answer with fewer CPU cycles |
| 145374 | + ** by omitting the term. |
| 145375 | + ** |
| 145376 | + ** But do not disable the term unless we are certain that the term is |
| 145377 | + ** not a transitive constraint. For an example of where that does not |
| 145378 | + ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04) |
| 145379 | + */ |
| 145380 | + if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0 |
| 145381 | + || (pTerm->eOperator & WO_EQUIV)==0 |
| 145382 | + ){ |
| 145383 | + disableTerm(pLevel, pTerm); |
| 145384 | + } |
| 145385 | + |
| 145089 | 145386 | return iReg; |
| 145090 | 145387 | } |
| 145091 | 145388 | |
| 145092 | 145389 | /* |
| 145093 | 145390 | ** Generate code that will evaluate all == and IN constraints for an |
| | @@ -146197,13 +146494,11 @@ |
| 146197 | 146494 | |
| 146198 | 146495 | /* If we are doing a reverse order scan on an ascending index, or |
| 146199 | 146496 | ** a forward order scan on a descending index, interchange the |
| 146200 | 146497 | ** start and end terms (pRangeStart and pRangeEnd). |
| 146201 | 146498 | */ |
| 146202 | | - if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 146203 | | - || (bRev && pIdx->nKeyCol==nEq) |
| 146204 | | - ){ |
| 146499 | + if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){ |
| 146205 | 146500 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 146206 | 146501 | SWAP(u8, bSeekPastNull, bStopAtNull); |
| 146207 | 146502 | SWAP(u8, nBtm, nTop); |
| 146208 | 146503 | } |
| 146209 | 146504 | |
| | @@ -147832,11 +148127,11 @@ |
| 147832 | 148127 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 147833 | 148128 | pNew->x.pList = pList; |
| 147834 | 148129 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 147835 | 148130 | testcase( idxNew==0 ); |
| 147836 | 148131 | exprAnalyze(pSrc, pWC, idxNew); |
| 147837 | | - /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */ |
| 148132 | + /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */ |
| 147838 | 148133 | markTermAsChild(pWC, idxNew, idxTerm); |
| 147839 | 148134 | }else{ |
| 147840 | 148135 | sqlite3ExprListDelete(db, pList); |
| 147841 | 148136 | } |
| 147842 | 148137 | } |
| | @@ -147956,10 +148251,11 @@ |
| 147956 | 148251 | assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE ); |
| 147957 | 148252 | assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE ); |
| 147958 | 148253 | assert( op<=TK_GE ); |
| 147959 | 148254 | if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){ |
| 147960 | 148255 | pExpr = pExpr->x.pList->a[0].pExpr; |
| 148256 | + |
| 147961 | 148257 | } |
| 147962 | 148258 | |
| 147963 | 148259 | if( pExpr->op==TK_COLUMN ){ |
| 147964 | 148260 | aiCurCol[0] = pExpr->iTable; |
| 147965 | 148261 | aiCurCol[1] = pExpr->iColumn; |
| | @@ -147968,280 +148264,10 @@ |
| 147968 | 148264 | if( mPrereq==0 ) return 0; /* No table references */ |
| 147969 | 148265 | if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */ |
| 147970 | 148266 | return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr); |
| 147971 | 148267 | } |
| 147972 | 148268 | |
| 147973 | | -/* |
| 147974 | | -** Expression callback for exprUsesSrclist(). |
| 147975 | | -*/ |
| 147976 | | -static int exprUsesSrclistCb(Walker *p, Expr *pExpr){ |
| 147977 | | - if( pExpr->op==TK_COLUMN ){ |
| 147978 | | - SrcList *pSrc = p->u.pSrcList; |
| 147979 | | - int iCsr = pExpr->iTable; |
| 147980 | | - int ii; |
| 147981 | | - for(ii=0; ii<pSrc->nSrc; ii++){ |
| 147982 | | - if( pSrc->a[ii].iCursor==iCsr ){ |
| 147983 | | - return p->eCode ? WRC_Abort : WRC_Continue; |
| 147984 | | - } |
| 147985 | | - } |
| 147986 | | - return p->eCode ? WRC_Continue : WRC_Abort; |
| 147987 | | - } |
| 147988 | | - return WRC_Continue; |
| 147989 | | -} |
| 147990 | | - |
| 147991 | | -/* |
| 147992 | | -** Select callback for exprUsesSrclist(). |
| 147993 | | -*/ |
| 147994 | | -static int exprUsesSrclistSelectCb(Walker *NotUsed1, Select *NotUsed2){ |
| 147995 | | - UNUSED_PARAMETER(NotUsed1); |
| 147996 | | - UNUSED_PARAMETER(NotUsed2); |
| 147997 | | - return WRC_Abort; |
| 147998 | | -} |
| 147999 | | - |
| 148000 | | -/* |
| 148001 | | -** This function always returns true if expression pExpr contains |
| 148002 | | -** a sub-select. |
| 148003 | | -** |
| 148004 | | -** If there is no sub-select in pExpr, then return true if pExpr |
| 148005 | | -** contains a TK_COLUMN node for a table that is (bUses==1) |
| 148006 | | -** or is not (bUses==0) in pSrc. |
| 148007 | | -** |
| 148008 | | -** Said another way: |
| 148009 | | -** |
| 148010 | | -** bUses Return Meaning |
| 148011 | | -** -------- ------ ------------------------------------------------ |
| 148012 | | -** |
| 148013 | | -** bUses==1 true pExpr contains either a sub-select or a |
| 148014 | | -** TK_COLUMN referencing pSrc. |
| 148015 | | -** |
| 148016 | | -** bUses==1 false pExpr contains no sub-selects and all TK_COLUMN |
| 148017 | | -** nodes reference tables not found in pSrc |
| 148018 | | -** |
| 148019 | | -** bUses==0 true pExpr contains either a sub-select or a TK_COLUMN |
| 148020 | | -** that references a table not in pSrc. |
| 148021 | | -** |
| 148022 | | -** bUses==0 false pExpr contains no sub-selects and all TK_COLUMN |
| 148023 | | -** nodes reference pSrc |
| 148024 | | -*/ |
| 148025 | | -static int exprUsesSrclist(SrcList *pSrc, Expr *pExpr, int bUses){ |
| 148026 | | - Walker sWalker; |
| 148027 | | - memset(&sWalker, 0, sizeof(Walker)); |
| 148028 | | - sWalker.eCode = bUses; |
| 148029 | | - sWalker.u.pSrcList = pSrc; |
| 148030 | | - sWalker.xExprCallback = exprUsesSrclistCb; |
| 148031 | | - sWalker.xSelectCallback = exprUsesSrclistSelectCb; |
| 148032 | | - return (sqlite3WalkExpr(&sWalker, pExpr)==WRC_Abort); |
| 148033 | | -} |
| 148034 | | - |
| 148035 | | -/* |
| 148036 | | -** Context object used by exprExistsToInIter() as it iterates through an |
| 148037 | | -** expression tree. |
| 148038 | | -*/ |
| 148039 | | -struct ExistsToInCtx { |
| 148040 | | - SrcList *pSrc; /* The tables in an EXISTS(SELECT ... FROM <here> ...) */ |
| 148041 | | - Expr *pInLhs; /* OUT: Use this as the LHS of the IN operator */ |
| 148042 | | - Expr *pEq; /* OUT: The == term that include pInLhs */ |
| 148043 | | - Expr **ppAnd; /* OUT: The AND operator that includes pEq as a child */ |
| 148044 | | - Expr **ppParent; /* The AND operator currently being examined */ |
| 148045 | | -}; |
| 148046 | | - |
| 148047 | | -/* |
| 148048 | | -** Iterate through all AND connected nodes in the expression tree |
| 148049 | | -** headed by (*ppExpr), populating the structure passed as the first |
| 148050 | | -** argument with the values required by exprAnalyzeExistsFindEq(). |
| 148051 | | -** |
| 148052 | | -** This function returns non-zero if the expression tree does not meet |
| 148053 | | -** the two conditions described by the header comment for |
| 148054 | | -** exprAnalyzeExistsFindEq(), or zero if it does. |
| 148055 | | -*/ |
| 148056 | | -static int exprExistsToInIter(struct ExistsToInCtx *p, Expr **ppExpr){ |
| 148057 | | - Expr *pExpr = *ppExpr; |
| 148058 | | - switch( pExpr->op ){ |
| 148059 | | - case TK_AND: |
| 148060 | | - p->ppParent = ppExpr; |
| 148061 | | - if( exprExistsToInIter(p, &pExpr->pLeft) ) return 1; |
| 148062 | | - p->ppParent = ppExpr; |
| 148063 | | - if( exprExistsToInIter(p, &pExpr->pRight) ) return 1; |
| 148064 | | - break; |
| 148065 | | - case TK_EQ: { |
| 148066 | | - int bLeft = exprUsesSrclist(p->pSrc, pExpr->pLeft, 0); |
| 148067 | | - int bRight = exprUsesSrclist(p->pSrc, pExpr->pRight, 0); |
| 148068 | | - if( bLeft || bRight ){ |
| 148069 | | - if( (bLeft && bRight) || p->pInLhs ) return 1; |
| 148070 | | - p->pInLhs = bLeft ? pExpr->pLeft : pExpr->pRight; |
| 148071 | | - if( exprUsesSrclist(p->pSrc, p->pInLhs, 1) ) return 1; |
| 148072 | | - p->pEq = pExpr; |
| 148073 | | - p->ppAnd = p->ppParent; |
| 148074 | | - } |
| 148075 | | - break; |
| 148076 | | - } |
| 148077 | | - default: |
| 148078 | | - if( exprUsesSrclist(p->pSrc, pExpr, 0) ){ |
| 148079 | | - return 1; |
| 148080 | | - } |
| 148081 | | - break; |
| 148082 | | - } |
| 148083 | | - |
| 148084 | | - return 0; |
| 148085 | | -} |
| 148086 | | - |
| 148087 | | -/* |
| 148088 | | -** This function is used by exprAnalyzeExists() when creating virtual IN(...) |
| 148089 | | -** terms equivalent to user-supplied EXIST(...) clauses. It splits the WHERE |
| 148090 | | -** clause of the Select object passed as the first argument into one or more |
| 148091 | | -** expressions joined by AND operators, and then tests if the following are |
| 148092 | | -** true: |
| 148093 | | -** |
| 148094 | | -** 1. Exactly one of the AND separated terms refers to the outer |
| 148095 | | -** query, and it is an == (TK_EQ) expression. |
| 148096 | | -** |
| 148097 | | -** 2. Only one side of the == expression refers to the outer query, and |
| 148098 | | -** it does not refer to any columns from the inner query. |
| 148099 | | -** |
| 148100 | | -** If both these conditions are true, then a pointer to the side of the == |
| 148101 | | -** expression that refers to the outer query is returned. The caller will |
| 148102 | | -** use this expression as the LHS of the IN(...) virtual term. Or, if one |
| 148103 | | -** or both of the above conditions are not true, NULL is returned. |
| 148104 | | -** |
| 148105 | | -** If non-NULL is returned and ppEq is non-NULL, *ppEq is set to point |
| 148106 | | -** to the == expression node before returning. If pppAnd is non-NULL and |
| 148107 | | -** the == node is not the root of the WHERE clause, then *pppAnd is set |
| 148108 | | -** to point to the pointer to the AND node that is the parent of the == |
| 148109 | | -** node within the WHERE expression tree. |
| 148110 | | -*/ |
| 148111 | | -static Expr *exprAnalyzeExistsFindEq( |
| 148112 | | - Select *pSel, /* The SELECT of the EXISTS */ |
| 148113 | | - Expr **ppEq, /* OUT: == node from WHERE clause */ |
| 148114 | | - Expr ***pppAnd /* OUT: Pointer to parent of ==, if any */ |
| 148115 | | -){ |
| 148116 | | - struct ExistsToInCtx ctx; |
| 148117 | | - memset(&ctx, 0, sizeof(ctx)); |
| 148118 | | - ctx.pSrc = pSel->pSrc; |
| 148119 | | - if( exprExistsToInIter(&ctx, &pSel->pWhere) ){ |
| 148120 | | - return 0; |
| 148121 | | - } |
| 148122 | | - if( ppEq ) *ppEq = ctx.pEq; |
| 148123 | | - if( pppAnd ) *pppAnd = ctx.ppAnd; |
| 148124 | | - return ctx.pInLhs; |
| 148125 | | -} |
| 148126 | | - |
| 148127 | | -/* |
| 148128 | | -** Term idxTerm of the WHERE clause passed as the second argument is an |
| 148129 | | -** EXISTS expression with a correlated SELECT statement on the RHS. |
| 148130 | | -** This function analyzes the SELECT statement, and if possible adds an |
| 148131 | | -** equivalent "? IN(SELECT...)" virtual term to the WHERE clause. |
| 148132 | | -** |
| 148133 | | -** For an EXISTS term such as the following: |
| 148134 | | -** |
| 148135 | | -** EXISTS (SELECT ... FROM <srclist> WHERE <e1> = <e2> AND <e3>) |
| 148136 | | -** |
| 148137 | | -** The virtual IN() term added is: |
| 148138 | | -** |
| 148139 | | -** <e1> IN (SELECT <e2> FROM <srclist> WHERE <e3>) |
| 148140 | | -** |
| 148141 | | -** The virtual term is only added if the following conditions are met: |
| 148142 | | -** |
| 148143 | | -** 1. The sub-select must not be an aggregate or use window functions, |
| 148144 | | -** |
| 148145 | | -** 2. The sub-select must not be a compound SELECT, |
| 148146 | | -** |
| 148147 | | -** 3. Expression <e1> must refer to at least one column from the outer |
| 148148 | | -** query, and must not refer to any column from the inner query |
| 148149 | | -** (i.e. from <srclist>). |
| 148150 | | -** |
| 148151 | | -** 4. <e2> and <e3> must not refer to any values from the outer query. |
| 148152 | | -** In other words, once <e1> has been removed, the inner query |
| 148153 | | -** must not be correlated. |
| 148154 | | -** |
| 148155 | | -*/ |
| 148156 | | -static void exprAnalyzeExists( |
| 148157 | | - SrcList *pSrc, /* the FROM clause */ |
| 148158 | | - WhereClause *pWC, /* the WHERE clause */ |
| 148159 | | - int idxTerm /* Index of the term to be analyzed */ |
| 148160 | | -){ |
| 148161 | | - Parse *pParse = pWC->pWInfo->pParse; |
| 148162 | | - WhereTerm *pTerm = &pWC->a[idxTerm]; |
| 148163 | | - Expr *pExpr = pTerm->pExpr; |
| 148164 | | - Select *pSel = pExpr->x.pSelect; |
| 148165 | | - Expr *pDup = 0; |
| 148166 | | - Expr *pEq = 0; |
| 148167 | | - Expr *pRet = 0; |
| 148168 | | - Expr *pInLhs = 0; |
| 148169 | | - Expr **ppAnd = 0; |
| 148170 | | - int idxNew; |
| 148171 | | - sqlite3 *db = pParse->db; |
| 148172 | | - |
| 148173 | | - assert( pExpr->op==TK_EXISTS ); |
| 148174 | | - assert( (pExpr->flags & EP_VarSelect) && (pExpr->flags & EP_xIsSelect) ); |
| 148175 | | - |
| 148176 | | - if( pSel->selFlags & SF_Aggregate ) return; |
| 148177 | | -#ifndef SQLITE_OMIT_WINDOWFUNC |
| 148178 | | - if( pSel->pWin ) return; |
| 148179 | | -#endif |
| 148180 | | - if( pSel->pPrior ) return; |
| 148181 | | - if( pSel->pWhere==0 ) return; |
| 148182 | | - if( pSel->pLimit ) return; |
| 148183 | | - if( 0==exprAnalyzeExistsFindEq(pSel, 0, 0) ) return; |
| 148184 | | - |
| 148185 | | - pDup = sqlite3ExprDup(db, pExpr, 0); |
| 148186 | | - if( db->mallocFailed ){ |
| 148187 | | - sqlite3ExprDelete(db, pDup); |
| 148188 | | - return; |
| 148189 | | - } |
| 148190 | | - pSel = pDup->x.pSelect; |
| 148191 | | - sqlite3ExprListDelete(db, pSel->pEList); |
| 148192 | | - pSel->pEList = 0; |
| 148193 | | - |
| 148194 | | - pInLhs = exprAnalyzeExistsFindEq(pSel, &pEq, &ppAnd); |
| 148195 | | - assert( pInLhs && pEq ); |
| 148196 | | - assert( pEq==pSel->pWhere || ppAnd ); |
| 148197 | | - if( pInLhs==pEq->pLeft ){ |
| 148198 | | - pRet = pEq->pRight; |
| 148199 | | - }else{ |
| 148200 | | - CollSeq *p = sqlite3ExprCompareCollSeq(pParse, pEq); |
| 148201 | | - pInLhs = sqlite3ExprAddCollateString(pParse, pInLhs, p?p->zName:"BINARY"); |
| 148202 | | - pRet = pEq->pLeft; |
| 148203 | | - } |
| 148204 | | - |
| 148205 | | - assert( pDup->pLeft==0 ); |
| 148206 | | - pDup->op = TK_IN; |
| 148207 | | - pDup->pLeft = pInLhs; |
| 148208 | | - pDup->flags &= ~EP_VarSelect; |
| 148209 | | - if( pRet->op==TK_VECTOR ){ |
| 148210 | | - pSel->pEList = pRet->x.pList; |
| 148211 | | - pRet->x.pList = 0; |
| 148212 | | - sqlite3ExprDelete(db, pRet); |
| 148213 | | - }else{ |
| 148214 | | - pSel->pEList = sqlite3ExprListAppend(pParse, 0, pRet); |
| 148215 | | - } |
| 148216 | | - pEq->pLeft = 0; |
| 148217 | | - pEq->pRight = 0; |
| 148218 | | - if( ppAnd ){ |
| 148219 | | - Expr *pAnd = *ppAnd; |
| 148220 | | - Expr *pOther = (pAnd->pLeft==pEq) ? pAnd->pRight : pAnd->pLeft; |
| 148221 | | - pAnd->pLeft = pAnd->pRight = 0; |
| 148222 | | - sqlite3ExprDelete(db, pAnd); |
| 148223 | | - *ppAnd = pOther; |
| 148224 | | - }else{ |
| 148225 | | - assert( pSel->pWhere==pEq ); |
| 148226 | | - pSel->pWhere = 0; |
| 148227 | | - } |
| 148228 | | - sqlite3ExprDelete(db, pEq); |
| 148229 | | - |
| 148230 | | -#ifdef WHERETRACE_ENABLED /* 0x20 */ |
| 148231 | | - if( sqlite3WhereTrace & 0x20 ){ |
| 148232 | | - sqlite3DebugPrintf("Convert EXISTS:\n"); |
| 148233 | | - sqlite3TreeViewExpr(0, pExpr, 0); |
| 148234 | | - sqlite3DebugPrintf("into IN:\n"); |
| 148235 | | - sqlite3TreeViewExpr(0, pDup, 0); |
| 148236 | | - } |
| 148237 | | -#endif |
| 148238 | | - idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 148239 | | - exprAnalyze(pSrc, pWC, idxNew); |
| 148240 | | - markTermAsChild(pWC, idxNew, idxTerm); |
| 148241 | | - pWC->a[idxTerm].wtFlags |= TERM_COPIED; |
| 148242 | | -} |
| 148243 | 148269 | |
| 148244 | 148270 | /* |
| 148245 | 148271 | ** The input to this routine is an WhereTerm structure with only the |
| 148246 | 148272 | ** "pExpr" field filled in. The job of this routine is to analyze the |
| 148247 | 148273 | ** subexpression and populate all the other fields of the WhereTerm |
| | @@ -148337,10 +148363,11 @@ |
| 148337 | 148363 | pTerm->eOperator = operatorMask(op) & opMask; |
| 148338 | 148364 | } |
| 148339 | 148365 | if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
| 148340 | 148366 | if( pRight |
| 148341 | 148367 | && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op) |
| 148368 | + && !ExprHasProperty(pRight, EP_FixedCol) |
| 148342 | 148369 | ){ |
| 148343 | 148370 | WhereTerm *pNew; |
| 148344 | 148371 | Expr *pDup; |
| 148345 | 148372 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
| 148346 | 148373 | assert( pTerm->u.x.iField==0 ); |
| | @@ -148429,20 +148456,10 @@ |
| 148429 | 148456 | assert( pWC->op==TK_AND ); |
| 148430 | 148457 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
| 148431 | 148458 | pTerm = &pWC->a[idxTerm]; |
| 148432 | 148459 | } |
| 148433 | 148460 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 148434 | | - |
| 148435 | | - else if( pExpr->op==TK_EXISTS ){ |
| 148436 | | - /* Perhaps treat an EXISTS operator as an IN operator */ |
| 148437 | | - if( (pExpr->flags & EP_VarSelect)!=0 |
| 148438 | | - && OptimizationEnabled(db, SQLITE_ExistsToIN) |
| 148439 | | - ){ |
| 148440 | | - exprAnalyzeExists(pSrc, pWC, idxTerm); |
| 148441 | | - } |
| 148442 | | - } |
| 148443 | | - |
| 148444 | 148461 | /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 148445 | 148462 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 148446 | 148463 | ** virtual term of that form. |
| 148447 | 148464 | ** |
| 148448 | 148465 | ** The virtual term must be tagged with TERM_VNULL. |
| | @@ -149202,10 +149219,22 @@ |
| 149202 | 149219 | testcase( pTerm->eOperator & WO_IS ); |
| 149203 | 149220 | continue; |
| 149204 | 149221 | } |
| 149205 | 149222 | pScan->pWC = pWC; |
| 149206 | 149223 | pScan->k = k+1; |
| 149224 | +#ifdef WHERETRACE_ENABLED |
| 149225 | + if( sqlite3WhereTrace & 0x20000 ){ |
| 149226 | + int ii; |
| 149227 | + sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d", |
| 149228 | + pTerm, pScan->nEquiv); |
| 149229 | + for(ii=0; ii<pScan->nEquiv; ii++){ |
| 149230 | + sqlite3DebugPrintf(" {%d:%d}", |
| 149231 | + pScan->aiCur[ii], pScan->aiColumn[ii]); |
| 149232 | + } |
| 149233 | + sqlite3DebugPrintf("\n"); |
| 149234 | + } |
| 149235 | +#endif |
| 149207 | 149236 | return pTerm; |
| 149208 | 149237 | } |
| 149209 | 149238 | } |
| 149210 | 149239 | } |
| 149211 | 149240 | pWC = pWC->pOuter; |
| | @@ -151365,10 +151394,12 @@ |
| 151365 | 151394 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; |
| 151366 | 151395 | } |
| 151367 | 151396 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 151368 | 151397 | |
| 151369 | 151398 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 151399 | + assert( pNew->u.btree.nEq<pProbe->nKeyCol |
| 151400 | + || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY ); |
| 151370 | 151401 | |
| 151371 | 151402 | saved_nEq = pNew->u.btree.nEq; |
| 151372 | 151403 | saved_nBtm = pNew->u.btree.nBtm; |
| 151373 | 151404 | saved_nTop = pNew->u.btree.nTop; |
| 151374 | 151405 | saved_nSkip = pNew->nSkip; |
| | @@ -151498,10 +151529,11 @@ |
| 151498 | 151529 | pNew->wsFlags |= WHERE_ONEROW; |
| 151499 | 151530 | }else{ |
| 151500 | 151531 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 151501 | 151532 | } |
| 151502 | 151533 | } |
| 151534 | + if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS; |
| 151503 | 151535 | }else if( eOp & WO_ISNULL ){ |
| 151504 | 151536 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 151505 | 151537 | }else if( eOp & (WO_GT|WO_GE) ){ |
| 151506 | 151538 | testcase( eOp & WO_GT ); |
| 151507 | 151539 | testcase( eOp & WO_GE ); |
| | @@ -151641,10 +151673,12 @@ |
| 151641 | 151673 | pNew->nOut = nOutUnadjusted; |
| 151642 | 151674 | } |
| 151643 | 151675 | |
| 151644 | 151676 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
| 151645 | 151677 | && pNew->u.btree.nEq<pProbe->nColumn |
| 151678 | + && (pNew->u.btree.nEq<pProbe->nKeyCol || |
| 151679 | + pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY) |
| 151646 | 151680 | ){ |
| 151647 | 151681 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
| 151648 | 151682 | } |
| 151649 | 151683 | pNew->nOut = saved_nOut; |
| 151650 | 151684 | #ifdef SQLITE_ENABLE_STAT4 |
| | @@ -153009,11 +153043,11 @@ |
| 153009 | 153043 | if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){ |
| 153010 | 153044 | nRow = pWInfo->iLimit; |
| 153011 | 153045 | }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){ |
| 153012 | 153046 | /* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT |
| 153013 | 153047 | ** reduces the number of output rows by a factor of 2 */ |
| 153014 | | - if( nRow>10 ) nRow -= 10; assert( 10==sqlite3LogEst(2) ); |
| 153048 | + if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); } |
| 153015 | 153049 | } |
| 153016 | 153050 | rSortCost += estLog(nRow); |
| 153017 | 153051 | return rSortCost; |
| 153018 | 153052 | } |
| 153019 | 153053 | |
| | @@ -154299,10 +154333,12 @@ |
| 154299 | 154333 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 154300 | 154334 | struct InLoop *pIn; |
| 154301 | 154335 | int j; |
| 154302 | 154336 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 154303 | 154337 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 154338 | + assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull |
| 154339 | + || pParse->db->mallocFailed ); |
| 154304 | 154340 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 154305 | 154341 | if( pIn->eEndLoopOp!=OP_Noop ){ |
| 154306 | 154342 | if( pIn->nPrefix ){ |
| 154307 | 154343 | int bEarlyOut = |
| 154308 | 154344 | (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| | @@ -154323,10 +154359,15 @@ |
| 154323 | 154359 | if( bEarlyOut ){ |
| 154324 | 154360 | sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur, |
| 154325 | 154361 | sqlite3VdbeCurrentAddr(v)+2, |
| 154326 | 154362 | pIn->iBase, pIn->nPrefix); |
| 154327 | 154363 | VdbeCoverage(v); |
| 154364 | + /* Retarget the OP_IsNull against the left operand of IN so |
| 154365 | + ** it jumps past the OP_IfNoHope. This is because the |
| 154366 | + ** OP_IsNull also bypasses the OP_Affinity opcode that is |
| 154367 | + ** required by OP_IfNoHope. */ |
| 154368 | + sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 154328 | 154369 | } |
| 154329 | 154370 | } |
| 154330 | 154371 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
| 154331 | 154372 | VdbeCoverage(v); |
| 154332 | 154373 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev); |
| | @@ -155454,10 +155495,18 @@ |
| 155454 | 155495 | ){ |
| 155455 | 155496 | pExpr->op2++; |
| 155456 | 155497 | } |
| 155457 | 155498 | return WRC_Continue; |
| 155458 | 155499 | } |
| 155500 | + |
| 155501 | +static int disallowAggregatesInOrderByCb(Walker *pWalker, Expr *pExpr){ |
| 155502 | + if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){ |
| 155503 | + sqlite3ErrorMsg(pWalker->pParse, |
| 155504 | + "misuse of aggregate: %s()", pExpr->u.zToken); |
| 155505 | + } |
| 155506 | + return WRC_Continue; |
| 155507 | +} |
| 155459 | 155508 | |
| 155460 | 155509 | /* |
| 155461 | 155510 | ** If the SELECT statement passed as the second argument does not invoke |
| 155462 | 155511 | ** any SQL window functions, this function is a no-op. Otherwise, it |
| 155463 | 155512 | ** rewrites the SELECT statement so that window function xStep functions |
| | @@ -155488,10 +155537,14 @@ |
| 155488 | 155537 | if( pTab==0 ){ |
| 155489 | 155538 | return sqlite3ErrorToParser(db, SQLITE_NOMEM); |
| 155490 | 155539 | } |
| 155491 | 155540 | sqlite3AggInfoPersistWalkerInit(&w, pParse); |
| 155492 | 155541 | sqlite3WalkSelect(&w, p); |
| 155542 | + if( (p->selFlags & SF_Aggregate)==0 ){ |
| 155543 | + w.xExprCallback = disallowAggregatesInOrderByCb; |
| 155544 | + sqlite3WalkExprList(&w, p->pOrderBy); |
| 155545 | + } |
| 155493 | 155546 | |
| 155494 | 155547 | p->pSrc = 0; |
| 155495 | 155548 | p->pWhere = 0; |
| 155496 | 155549 | p->pGroupBy = 0; |
| 155497 | 155550 | p->pHaving = 0; |
| | @@ -164970,11 +165023,11 @@ |
| 164970 | 165023 | } |
| 164971 | 165024 | if( rc==SQLITE_OK ){ |
| 164972 | 165025 | sqlite3GlobalConfig.isPCacheInit = 1; |
| 164973 | 165026 | rc = sqlite3OsInit(); |
| 164974 | 165027 | } |
| 164975 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 165028 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 164976 | 165029 | if( rc==SQLITE_OK ){ |
| 164977 | 165030 | rc = sqlite3MemdbInit(); |
| 164978 | 165031 | } |
| 164979 | 165032 | #endif |
| 164980 | 165033 | if( rc==SQLITE_OK ){ |
| | @@ -165385,16 +165438,16 @@ |
| 165385 | 165438 | sqlite3GlobalConfig.szSorterRef = (u32)iVal; |
| 165386 | 165439 | break; |
| 165387 | 165440 | } |
| 165388 | 165441 | #endif /* SQLITE_ENABLE_SORTER_REFERENCES */ |
| 165389 | 165442 | |
| 165390 | | -#ifdef SQLITE_ENABLE_DESERIALIZE |
| 165443 | +#ifndef SQLITE_OMIT_DESERIALIZE |
| 165391 | 165444 | case SQLITE_CONFIG_MEMDB_MAXSIZE: { |
| 165392 | 165445 | sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64); |
| 165393 | 165446 | break; |
| 165394 | 165447 | } |
| 165395 | | -#endif /* SQLITE_ENABLE_DESERIALIZE */ |
| 165448 | +#endif /* SQLITE_OMIT_DESERIALIZE */ |
| 165396 | 165449 | |
| 165397 | 165450 | default: { |
| 165398 | 165451 | rc = SQLITE_ERROR; |
| 165399 | 165452 | break; |
| 165400 | 165453 | } |
| | @@ -172515,11 +172568,11 @@ |
| 172515 | 172568 | const char *zCsr = zNode; /* Cursor to iterate through node */ |
| 172516 | 172569 | const char *zEnd = &zCsr[nNode];/* End of interior node buffer */ |
| 172517 | 172570 | char *zBuffer = 0; /* Buffer to load terms into */ |
| 172518 | 172571 | i64 nAlloc = 0; /* Size of allocated buffer */ |
| 172519 | 172572 | int isFirstTerm = 1; /* True when processing first term on page */ |
| 172520 | | - sqlite3_int64 iChild; /* Block id of child node to descend to */ |
| 172573 | + u64 iChild; /* Block id of child node to descend to */ |
| 172521 | 172574 | int nBuffer = 0; /* Total term size */ |
| 172522 | 172575 | |
| 172523 | 172576 | /* Skip over the 'height' varint that occurs at the start of every |
| 172524 | 172577 | ** interior node. Then load the blockid of the left-child of the b-tree |
| 172525 | 172578 | ** node into variable iChild. |
| | @@ -172531,12 +172584,12 @@ |
| 172531 | 172584 | ** either more than 20 bytes of allocated space following the nNode bytes of |
| 172532 | 172585 | ** contents, or two zero bytes. Or, if the node is read from the %_segments |
| 172533 | 172586 | ** table, then there are always 20 bytes of zeroed padding following the |
| 172534 | 172587 | ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). |
| 172535 | 172588 | */ |
| 172536 | | - zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 172537 | | - zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); |
| 172589 | + zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild); |
| 172590 | + zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild); |
| 172538 | 172591 | if( zCsr>zEnd ){ |
| 172539 | 172592 | return FTS_CORRUPT_VTAB; |
| 172540 | 172593 | } |
| 172541 | 172594 | |
| 172542 | 172595 | while( zCsr<zEnd && (piFirst || piLast) ){ |
| | @@ -172585,24 +172638,24 @@ |
| 172585 | 172638 | ** If the interior node term is larger than the specified term, then |
| 172586 | 172639 | ** the tree headed by iChild may contain the specified term. |
| 172587 | 172640 | */ |
| 172588 | 172641 | cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer)); |
| 172589 | 172642 | if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){ |
| 172590 | | - *piFirst = iChild; |
| 172643 | + *piFirst = (i64)iChild; |
| 172591 | 172644 | piFirst = 0; |
| 172592 | 172645 | } |
| 172593 | 172646 | |
| 172594 | 172647 | if( piLast && cmp<0 ){ |
| 172595 | | - *piLast = iChild; |
| 172648 | + *piLast = (i64)iChild; |
| 172596 | 172649 | piLast = 0; |
| 172597 | 172650 | } |
| 172598 | 172651 | |
| 172599 | 172652 | iChild++; |
| 172600 | 172653 | }; |
| 172601 | 172654 | |
| 172602 | | - if( piFirst ) *piFirst = iChild; |
| 172603 | | - if( piLast ) *piLast = iChild; |
| 172655 | + if( piFirst ) *piFirst = (i64)iChild; |
| 172656 | + if( piLast ) *piLast = (i64)iChild; |
| 172604 | 172657 | |
| 172605 | 172658 | finish_scan: |
| 172606 | 172659 | sqlite3_free(zBuffer); |
| 172607 | 172660 | return rc; |
| 172608 | 172661 | } |
| | @@ -189485,11 +189538,11 @@ |
| 189485 | 189538 | ** any double-quotes or backslash characters contained within the |
| 189486 | 189539 | ** string. |
| 189487 | 189540 | */ |
| 189488 | 189541 | static void jsonAppendString(JsonString *p, const char *zIn, u32 N){ |
| 189489 | 189542 | u32 i; |
| 189490 | | - if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; |
| 189543 | + if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return; |
| 189491 | 189544 | p->zBuf[p->nUsed++] = '"'; |
| 189492 | 189545 | for(i=0; i<N; i++){ |
| 189493 | 189546 | unsigned char c = ((unsigned const char*)zIn)[i]; |
| 189494 | 189547 | if( c=='"' || c=='\\' ){ |
| 189495 | 189548 | json_simple_escape: |
| | @@ -201243,11 +201296,13 @@ |
| 201243 | 201296 | ); |
| 201244 | 201297 | if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){ |
| 201245 | 201298 | zSep = ""; |
| 201246 | 201299 | for(iCol=0; iCol<pIter->nCol; iCol++){ |
| 201247 | 201300 | const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol); |
| 201248 | | - if( zQuoted[0]=='N' ){ |
| 201301 | + if( zQuoted==0 ){ |
| 201302 | + p->rc = SQLITE_NOMEM; |
| 201303 | + }else if( zQuoted[0]=='N' ){ |
| 201249 | 201304 | bFailed = 1; |
| 201250 | 201305 | break; |
| 201251 | 201306 | } |
| 201252 | 201307 | zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted); |
| 201253 | 201308 | zSep = ", "; |
| | @@ -204615,32 +204670,18 @@ |
| 204615 | 204670 | } |
| 204616 | 204671 | else if( flags & SQLITE_OPEN_WAL ){ |
| 204617 | 204672 | rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0); |
| 204618 | 204673 | if( pDb ){ |
| 204619 | 204674 | if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){ |
| 204620 | | - /* This call is to open a *-wal file. Intead, open the *-oal. This |
| 204621 | | - ** code ensures that the string passed to xOpen() is terminated by a |
| 204622 | | - ** pair of '\0' bytes in case the VFS attempts to extract a URI |
| 204623 | | - ** parameter from it. */ |
| 204624 | | - const char *zBase = zName; |
| 204625 | | - size_t nCopy; |
| 204626 | | - char *zCopy; |
| 204675 | + /* This call is to open a *-wal file. Intead, open the *-oal. */ |
| 204676 | + size_t nOpen; |
| 204627 | 204677 | if( rbuIsVacuum(pDb->pRbu) ){ |
| 204628 | | - zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main"); |
| 204629 | | - zBase = sqlite3_filename_wal(zBase); |
| 204630 | | - } |
| 204631 | | - nCopy = strlen(zBase); |
| 204632 | | - zCopy = sqlite3_malloc64(nCopy+2); |
| 204633 | | - if( zCopy ){ |
| 204634 | | - memcpy(zCopy, zBase, nCopy); |
| 204635 | | - zCopy[nCopy-3] = 'o'; |
| 204636 | | - zCopy[nCopy] = '\0'; |
| 204637 | | - zCopy[nCopy+1] = '\0'; |
| 204638 | | - zOpen = (const char*)(pFd->zDel = zCopy); |
| 204639 | | - }else{ |
| 204640 | | - rc = SQLITE_NOMEM; |
| 204641 | | - } |
| 204678 | + zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main"); |
| 204679 | + zOpen = sqlite3_filename_wal(zOpen); |
| 204680 | + } |
| 204681 | + nOpen = strlen(zOpen); |
| 204682 | + ((char*)zOpen)[nOpen-3] = 'o'; |
| 204642 | 204683 | pFd->pRbu = pDb->pRbu; |
| 204643 | 204684 | } |
| 204644 | 204685 | pDb->pWalFd = pFd; |
| 204645 | 204686 | } |
| 204646 | 204687 | } |
| | @@ -222669,11 +222710,10 @@ |
| 222669 | 222710 | }else{ |
| 222670 | 222711 | /* The following could be done by calling fts5SegIterLoadNPos(). But |
| 222671 | 222712 | ** this block is particularly performance critical, so equivalent |
| 222672 | 222713 | ** code is inlined. */ |
| 222673 | 222714 | int nSz; |
| 222674 | | - assert( p->rc==SQLITE_OK ); |
| 222675 | 222715 | assert_nc( pIter->iLeafOffset<=pIter->pLeaf->nn ); |
| 222676 | 222716 | fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz); |
| 222677 | 222717 | pIter->bDel = (nSz & 0x0001); |
| 222678 | 222718 | pIter->nPos = nSz>>1; |
| 222679 | 222719 | assert_nc( pIter->nPos>=0 ); |
| | @@ -230123,11 +230163,11 @@ |
| 230123 | 230163 | int nArg, /* Number of args */ |
| 230124 | 230164 | sqlite3_value **apUnused /* Function arguments */ |
| 230125 | 230165 | ){ |
| 230126 | 230166 | assert( nArg==0 ); |
| 230127 | 230167 | UNUSED_PARAM2(nArg, apUnused); |
| 230128 | | - sqlite3_result_text(pCtx, "fts5: 2021-04-27 17:18:10 ff3538ae37a02f4f36a15cddd1245171e724aac9c84b2e576980fd3806302775", -1, SQLITE_TRANSIENT); |
| 230168 | + sqlite3_result_text(pCtx, "fts5: 2021-05-14 15:37:00 cf63abbe559d04f993f99a37d41ba4a97c0261094f1d4cc05cfa23b1e11731f5", -1, SQLITE_TRANSIENT); |
| 230129 | 230169 | } |
| 230130 | 230170 | |
| 230131 | 230171 | /* |
| 230132 | 230172 | ** Return true if zName is the extension on one of the shadow tables used |
| 230133 | 230173 | ** by this module. |
| | @@ -235049,12 +235089,12 @@ |
| 235049 | 235089 | } |
| 235050 | 235090 | #endif /* SQLITE_CORE */ |
| 235051 | 235091 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 235052 | 235092 | |
| 235053 | 235093 | /************** End of stmt.c ************************************************/ |
| 235054 | | -#if __LINE__!=235054 |
| 235094 | +#if __LINE__!=235094 |
| 235055 | 235095 | #undef SQLITE_SOURCE_ID |
| 235056 | | -#define SQLITE_SOURCE_ID "2021-04-28 17:37:26 65ec39f0f092fe29e1d4e9e96cf07a73d2ef7ce2c41b6f1cd3ab23546adaalt2" |
| 235096 | +#define SQLITE_SOURCE_ID "2021-05-14 15:37:00 cf63abbe559d04f993f99a37d41ba4a97c0261094f1d4cc05cfa23b1e117alt2" |
| 235057 | 235097 | #endif |
| 235058 | 235098 | /* Return the source-id for this library */ |
| 235059 | 235099 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 235060 | 235100 | /************************** End of sqlite3.c ******************************/ |
| 235061 | 235101 | |