Lines Matching +full:fast +full:- +full:json +full:- +full:stable +full:- +full:stringify
17 ** language. The code for the "sqlite3" command-line shell is also in a
20 ** The content in this amalgamation comes from Fossil check-in
51 ** NO_TEST - The branches on this line are not
56 ** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
60 ** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
64 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
69 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
144 ** 2015-03-02
182 ** large file support, or if the OS is windows, these should be no-ops.
188 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
197 ** on 2008-11-28.) These days, all Linux kernels support large files, so
214 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
221 ** so the GCC_VERSION macro will be set to a correct non-zero value even
274 ** some MinGW-specific macros). When compiling for MinGW, either the
297 /* Optionally #include a user-defined header, whereby compilation options
315 ** 2001-09-15
326 ** presents to client programs. If a C-function, structure, datatype,
337 ** The official C-language API documentation for SQLite is derived
382 ** that require non-default calling conventions.
407 ** These no-op macros are used in front of interfaces to mark those
409 ** should not use deprecated interfaces - they are supported for backwards
433 ** CAPI3REF: Compile-Time Library Version Numbers
449 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
451 ** a string which identifies a particular check-in of SQLite
453 ** string contains the date and time of the check-in (UTC) and a SHA1
454 ** or SHA3-256 hash of the entire source tree. If the source code has
464 #define SQLITE_SOURCE_ID "2024-03-24 21:15:01 d68fb8b5dbb8305e00d2dd14d8fe6b3d9f67e2459102ff16…
467 ** CAPI3REF: Run-Time Library Version Numbers
504 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
513 ** returning the N-th compile time option string. ^If N is out of range,
538 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
556 ** This interface only reports on the compile-time mutex setting
562 ** sqlite3_threadsafe() function shows only the compile-time setting of
563 ** thread safety, not any run-time changes to that setting made by
588 ** CAPI3REF: 64-Bit Integer Types
591 ** Because there is no cross-platform way to specify 64-bit integer types
592 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
599 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
622 ** substitute integer for floating-point.
664 ** argument is a harmless no-op.
677 ** CAPI3REF: One-Step Query Execution Interface
685 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
686 ** semicolon-separate SQL statements passed into its 2nd argument,
708 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
758 /* beginning-of-error-codes */
789 /* end-of-error-codes */
797 ** these result codes are too coarse-grained. They do not provide as
903 ** [sqlite3_open_v2()] has historically be a no-op and might become an
959 ** read-only media and cannot be changed even by processes with
1021 ** (Third-party VFS implementations might also make the distinction
1056 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1080 ** requested lock, then the call to xLock() is a no-op.
1083 ** to xUnlock() is a no-op.
1096 ** locking strategy (for example to use dot-file locks), to inquire
1143 ** fails to zero-fill short reads might seem to work. However,
1144 ** failure to zero-fill short reads will eventually lead to
1200 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1202 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1212 ** point to an integer (type int) containing the new chunk-size to use
1214 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1225 ** the [rollback journal] or the [write-ahead log]) for a particular database
1238 ** this file-control is NULL. However, if the database file is being synced
1239 ** as part of a multi-database commit, the argument points to a nul-terminated
1240 ** string containing the transactions super-journal file name. VFSes that
1257 ** anti-virus programs. By default, the windows VFS will retry file read,
1283 ** WAL mode. If the integer is -1, then it is overwritten with the current
1288 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1292 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1293 ** mode. If the integer is -1, then it is overwritten with the current
1294 ** zero-damage mode setting.
1305 ** final bottom-level VFS are written into memory obtained from
1309 ** all file-control actions, there is no guarantee that this will actually
1311 ** pointer in case this file-control is not implemented. This file-control
1315 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1319 ** to a pointer to the top-level VFS.)^
1321 ** upper-most shim only.
1338 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1340 ** of the result string if the string is non-NULL.
1346 ** it is able to override built-in [PRAGMA] statements.
1350 ** file-control may be invoked by SQLite on the database file handle
1352 ** to the connection's busy-handler callback. The argument is of type (void**)
1353 ** - an array of two (void *) values. The first (void *) actually points
1355 ** busy-handler, this function should be invoked with the second (void *) in
1356 ** the array as the only argument. If it returns non-zero, then the operation
1361 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1371 ** maximum number of bytes that will be used for memory-mapped I/O.
1377 ** file-control is used internally to implement [PRAGMA mmap_size].
1383 ** The argument is a zero-terminated string. Higher layers in the
1385 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1410 ** Applications should <em>not</em> use this file-control.
1459 ** The parameter is a pointer to a 32-bit signed integer that contains
1460 ** the value that M is to be set to. Before returning, the 32-bit signed
1465 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1491 ** file to the database file, but before the *-shm file is updated to
1496 ** whether or not there is a database client in another process with a wal-mode
1498 ** (void*) argument passed with this file-control should be a pointer to a
1502 ** the database is not a wal-mode db, or if there is no such connection in any
1512 ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1513 ** purges the contents of the in-memory page cache. If there is an open
1514 ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1592 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1635 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1653 ** consist of a single "-" character followed by no more than
1654 ** 11 alphanumeric and/or "-" characters.
1669 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1689 ** the open of a journal file a no-op. Writes to this journal would
1690 ** also be no-ops, and any attempt to read the journal would return
1692 ** file will be doing page-aligned sector reads and writes in a random
1731 ** flag is never actually used and is not implemented in the built-in
1734 ** non-zero error code if there is an I/O error or if the name of
1736 ** is returned, then non-zero or zero is written into *pResOut to indicate
1750 ** of good-quality randomness into zOut. The return value is
1758 ** a 24-hour day).
1784 void *pAppData; /* Pointer to application-specific data */
1894 ** are harmless no-ops.)^
1899 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1922 ** compile-time option, then the automatic calls to sqlite3_initialize()
1931 ** The sqlite3_os_init() routine does operating-system specific
1947 ** (using the [SQLITE_OS_OTHER=1] compile-time
1949 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1987 ** implementation of an application-defined [sqlite3_os_init()].
1991 ** then this routine returns a non-zero [error code].
2005 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2018 ** and low-level memory allocation routines.
2030 ** Note that SQLite comes with several [built-in memory allocators]
2035 ** memory allocator that simulates memory out-of-memory conditions in
2102 ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2117 ** non-zero [error code] if a discontinued or unsupported configuration option
2123 ** [threading mode] to Single-thread. In other words, it disables
2126 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2128 ** value of Single-thread and so [sqlite3_config()] will return
2134 ** [threading mode] to Multi-thread. In other words, it disables
2138 ** are enabled so that SQLite will be safe to use in a multi-threaded
2141 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2142 ** it is not possible to set the Multi-thread [threading mode] and
2157 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2166 ** alternative low-level memory allocation routines to be used in place of
2194 ** disabled, the following SQLite interfaces become non-operational:
2215 ** This configuration option is a no-op if an application-defined page
2218 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2226 ** argument must be either a NULL pointer or a pointer to an 8-byte
2233 ** ^If pMem is NULL and N is non-zero, then each database connection
2236 ** of -1024*N bytes if N is negative, . ^If additional
2249 ** An 8-byte aligned pointer to the memory,
2256 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2264 ** The argument specifies alternative low-level mutex routines to be used
2268 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2281 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2314 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2316 ** passed through as the first parameter to the application-defined logger
2324 ** In a multi-threaded application, the application-defined logger
2329 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2346 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2347 ** if that compile-time option is omitted.
2357 ** They are retained for backwards compatibility but are now no-ops.
2363 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2379 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2386 ** compile-time maximum mmap size set by the
2387 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2389 ** changed to its compile-time default.
2394 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2395 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2411 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2420 ** becomes the [statement journal] spill-to-disk threshold.
2423 ** Or if the threshold is -1, statement journals are always held
2429 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2434 ** of type (int) - the new value of the sorter-reference size threshold.
2438 ** of a table column that its values are likely to be very large - larger
2439 ** than the configured sorter-reference size threshold - then a reference
2445 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2450 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2453 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2455 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2456 ** compile-time option is not set, then the default maximum is 1073741824.
2462 ** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2470 ** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2488 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2489 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2516 ** non-zero [error code] if a discontinued or unsupported configuration option
2532 ** must be aligned to an 8-byte boundary. ^If the second argument to
2568 ** triggers in the main database schema or in the schemas of ATTACH-ed
2585 ** views in the main database schema or in the schemas of ATTACH-ed
2592 ** [FTS3] full-text search engine extension.
2607 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2609 ** When the first argument to this interface is 1, then only the C-API is
2611 ** this interface is 0, then both the C-API and the SQL function are disabled.
2612 ** If the first argument is -1, then no changes are made to state of either the
2613 ** C-API or the SQL function.
2636 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2639 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2640 ** have been disabled - 0 if they are not disabled, 1 if they are.
2663 ** behavior. The first parameter passed to this operation is an integer -
2667 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2725 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2734 ** the legacy [double-quoted string literal] misfeature for DML statements
2736 ** default value of this setting is determined by the [-DSQLITE_DQS]
2737 ** compile-time option.
2743 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2745 ** default value of this setting is determined by the [-DSQLITE_DQS]
2746 ** compile-time option.
2773 ** created database file to have a schema format version number (the 4-byte
2800 ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2815 ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2861 ** has a unique 64-bit signed
2942 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2964 ** any changes performed by sub-triggers, as the sqlite3_changes()
2965 ** value will be saved and restored after each sub-trigger has run.)^
3033 ** CAPI3REF: Interrupt A Long-Running Query
3039 ** or Ctrl-C where the user wants a long query operation to halt
3064 ** SQL statements is a no-op and has no effect on SQL statements
3077 ** These routines are useful during command-line input to determine if the
3083 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3098 ** then the return value from sqlite3_complete16() will be non-zero
3101 ** The input to [sqlite3_complete()] must be a zero-terminated
3102 ** UTF-8 string.
3104 ** The input to [sqlite3_complete16()] must be a zero-terminated
3105 ** UTF-16 string in native byte order.
3112 ** KEYWORDS: {busy-handler callback} {busy handler}
3134 ** ^If the callback returns non-zero, then another attempt
3210 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3212 ** to zero-terminated strings that contain the names of the columns.
3214 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3226 ** -----------------------
3248 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3280 ** These routines are work-alikes of the "printf()" family of functions
3284 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3285 ** See the [built-in printf()] documentation for details.
3308 ** guarantees that the buffer is always zero-terminated. ^The first
3311 ** written will be n-1 characters.
3315 ** See also: [built-in printf()], [printf() SQL function]
3327 ** does not include operating-system specific [VFS] implementation. The
3338 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3339 ** of a signed 32-bit integer.
3344 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3349 ** might result if sqlite3_free() is called with a non-NULL pointer that
3369 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3370 ** of a 32-bit signed integer.
3385 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3409 ** routines, which form the built-in memory allocation subsystem.
3414 ** value of [sqlite3_memory_used()] since the high-water mark
3421 ** ^The memory high-water mark is reset to the current value of
3424 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3431 ** CAPI3REF: Pseudo-Random Number Generator
3433 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3436 ** the built-in random() and randomblob() SQL functions. This interface allows
3447 ** non-NULL P then the pseudo-randomness is generated
3454 ** CAPI3REF: Compile-Time Authorization Callbacks
3485 ** to the callback are either NULL pointers or zero-terminated strings
3513 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3532 ** statement might be re-prepared during [sqlite3_step()] due to a
3578 ** is the name of the inner-most trigger or view that is responsible for
3580 ** top-level SQL code.
3630 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3634 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3636 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3641 ** the original statement text and an estimate of wall-clock time
3661 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3683 ** interface by using the X argument when X begins with "--" and invoking
3690 ** X argument points to a 64-bit integer which is approximately
3720 ** M argument should be the bitwise OR-ed combination of
3772 ** ^If the progress callback returns non-zero, the operation is
3796 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3797 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3808 ** ^The default encoding will be UTF-8 for databases created using
3810 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3824 ** <dd>The database is opened in read-only mode. If the database does
3832 ** read-write mode fails due to OS-level permissions, an attempt is
3833 ** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3835 ** read-write.</dd>)^
3851 ** <dd>The database will be opened as an in-memory database. The database
3852 ** is named by the "filename" argument for the purposes of cache-sharing,
3857 ** <dd>The new database connection will use the "multi-thread"
3875 ** this option is a no-op.
3901 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3912 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3913 ** is created for the connection. ^This in-memory database will vanish when
3921 ** on-disk database will be created. ^This private database will be
3931 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3954 ** SQLite and its built-in [VFSes] interpret the
3969 ** ^If "ro" is specified, then the database is opened for read-only
3972 ** "rw", then the database is opened for read-write (but not create)
3976 ** set to "memory" then a pure [in-memory database] that never reads
4003 ** read-only media. ^When immutable is set, SQLite assumes that the
4005 ** privilege, and so the database is opened read-only and all locking
4030 ** <tr><td style="white-space:nowrap">
4034 ** necessary - space characters can be used literally
4037 ** Open file "data.db" in the current directory for read-only access.
4038 ** Regardless of whether or not shared-cache mode is enabled by
4040 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4041 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4042 ** that uses dot-files in place of posix advisory locking.
4050 ** percent sign - "%" - followed by exactly two hexadecimal digits
4052 ** URI filename are interpreted, they are encoded using UTF-8 and all
4054 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4058 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4060 ** characters must be converted to UTF-8 prior to passing them into
4070 const char *filename, /* Database filename (UTF-8) */
4074 const void *filename, /* Database filename (UTF-16) */
4078 const char *filename, /* Database filename (UTF-8) */
4115 ** case or if the value begins with a non-zero number. The
4123 ** 64-bit signed integer and returns that integer, or D if P does not
4128 ** the value) of the N-th query parameter for filename F, or a NULL
4130 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4189 ** ^If X is the name of a rollback or WAL-mode journal file that is
4195 ** only. It is not a general-purpose interface.
4239 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4275 ** change the value of the error code. The error-code preserving
4286 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4287 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4294 ** ^The sqlite3_errstr() interface returns the English-language text
4295 ** that describes the [result code], as UTF-8.
4304 ** SQL, then the sqlite3_error_offset() function returns -1.
4339 ** The life-cycle of a prepared statement object usually goes like this:
4354 ** CAPI3REF: Run-time Limits
4367 ** set at compile-time by a C preprocessor macro called
4376 ** simply invoke this interface with the third parameter set to -1.
4378 ** Run-time limits are intended for use in applications that manage
4391 ** New run-time limit categories may be added in future releases.
4396 ** CAPI3REF: Run-Time Limit Categories
4400 ** that can be lowered at run-time using [sqlite3_limit()].
4486 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4509 ** To execute an SQL statement, it must first be compiled into a byte-code
4518 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4519 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4520 ** as a convenience. The UTF-16 interfaces work by converting the
4521 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4528 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4530 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4531 ** and sqlite3_prepare16_v3() use UTF-16.
4537 ** If the caller knows that the supplied string is nul-terminated, then
4540 ** the nul-terminator.
4590 ** ^The specific value of a WHERE-clause [parameter] might influence the
4591 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4593 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4605 const char *zSql, /* SQL statement, UTF-8 encoded */
4612 const char *zSql, /* SQL statement, UTF-8 encoded */
4619 const char *zSql, /* SQL statement, UTF-8 encoded */
4627 const void *zSql, /* SQL statement, UTF-16 encoded */
4634 const void *zSql, /* SQL statement, UTF-16 encoded */
4641 const void *zSql, /* SQL statement, UTF-16 encoded */
4652 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4656 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4659 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4675 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4676 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4687 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4699 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4703 ** Note that [application-defined SQL functions] or
4707 ** change the database file through side-effects:
4733 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4735 ** read-only no-op if the table already exists, but
4795 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4832 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4843 ** implementation of [application-defined SQL functions] are protected.
4861 ** is always first parameter to [application-defined SQL functions].
4862 ** The application-defined SQL function implementation will pass this
4912 ** it should be a pointer to well-formed UTF8 text.
4914 ** it should be a pointer to well-formed UTF16 text.
4916 ** it should be a pointer to a well-formed unicode string that is
4920 ** [[byte-order determination rules]] ^The byte-order of
4921 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4938 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4978 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4980 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5052 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5063 ** ^If the value N is out of range or if the N-th parameter is
5065 ** always in UTF-8 encoding even if the named parameter was
5066 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5083 ** name must be given in UTF-8 even if the original statement
5084 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5125 ** interface returns a pointer to a zero-terminated UTF-8 string
5126 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5127 ** UTF-16 string. ^The first parameter is the [prepared statement]
5138 ** (for example during a conversion from UTF-8 to UTF-16) then a
5157 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5166 ** ^The names returned are the original un-aliased names of the
5172 ** ^The left-most column is column 0 for these routines.
5181 ** UTF-16 encoded strings and the other functions return UTF-8.
5184 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5208 ** ^The returned string is always UTF-8 encoded.
5221 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5271 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5297 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5327 ** will return non-zero if previous call to [sqlite3_step](P) returned
5329 ** where it always returns zero since each step of that multi-step
5343 ** <li> 64-bit signed integer
5344 ** <li> 64-bit IEEE floating point number
5377 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5378 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5379 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5380 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5385 ** or a UTF-8 TEXT result in bytes
5387 ** <td>→ <td>Size of UTF-16
5439 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5441 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5442 ** the string to UTF-8 and then returns the number of bytes.
5444 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5448 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5450 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5451 ** the string to UTF-16 and then returns the number of bytes.
5453 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5464 ** even empty strings, are always zero-terminated. ^The return
5465 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5481 ** [application-defined SQL functions] or [virtual tables], not within
5482 ** top-level application code.
5500 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5521 ** sqlite3_column_text16() is called. A zero-terminator might
5523 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5525 ** to UTF-16.</li>
5526 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5528 ** to UTF-8.</li>
5531 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5562 ** fail if an out-of-memory error occurs during a format conversion.
5563 ** Only the following subset of interfaces are subject to out-of-memory
5574 ** If an out-of-memory error occurs, then the return value from these
5576 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5610 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5625 ** object back to its initial state, ready to be re-executed.
5677 ** connection then application-defined SQL functions must be added
5681 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5682 ** representation, exclusive of the zero-terminator. ^Note that the name
5683 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5689 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5692 ** parameter is less than -1 or greater than 127 then the behavior is
5711 ** deterministic. The built-in [random()] SQL function is an example of a
5722 ** all application-defined SQL functions that do not need to be
5736 ** pointers to C-language functions that implement the SQL function or
5746 ** C-language callbacks that implement the new function. xStep and xFinal
5747 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5749 ** non-NULL, in which case the new function may be used as either an aggregate
5752 ** [user-defined window functions|available here].
5767 ** SQL function is used. ^A function implementation with a non-negative
5776 ** ^Built-in functions may be overloaded by new application-defined functions.
5778 ** ^An application-defined function is permitted to call other
5833 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5834 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5835 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5862 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5867 ** [application-defined SQL function]
5868 ** that has side-effects or that could potentially leak sensitive information.
5871 ** modified to invoke the application-defined function in ways that are
5875 ** [application-defined SQL functions], regardless of whether or not they
5879 ** that do not have access to the application-defined functions.
5898 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5900 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5902 ** security-adverse side-effects and information-leaks.
5907 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5908 ** This flag instructs SQLite to omit some corner-case optimizations that
5914 ** a non-zero subtype was specified by the function argument expression.
5918 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5922 ** might become a no-op if the function is used as term in an
5964 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5965 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5967 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5968 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5970 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5971 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5974 ** or a UTF-8 TEXT in bytes
5976 ** <td>→ <td>Size of UTF-16
5994 ** implement [application-defined SQL functions] and [virtual tables].
6004 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6005 ** in the native byte-order of the host machine. ^The
6007 ** extract UTF-16 strings as big-endian and little-endian respectively.
6048 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6063 ** fail if an out-of-memory error occurs during a format conversion.
6064 ** Only the following subset of interfaces are subject to out-of-memory
6077 ** If an out-of-memory error occurs, then the return value from these
6079 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6127 ** an [application-defined SQL function] argument V. The subtype
6132 ** Every [application-defined SQL function] that invoke this interface
6153 ** then sqlite3_value_free(V) is a harmless no-op.
6214 ** the application-defined function is running.
6234 ** These functions may be used by (non-aggregate) SQL functions to
6239 ** regular-expression matching function. The compiled version of the regular
6247 ** value to the application-defined function. ^N is zero for the left-most
6253 ** N-th argument of the application-defined function. ^Subsequent
6279 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6285 ** function parameters that are compile-time constants, including literal
6288 ** The value of the N parameter to these interfaces should be non-negative.
6313 ** If P and X are both non-NULL, then the destructor X is invoked with
6316 ** <li> An out-of-memory error occurs during the call to
6372 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6388 ** an application-defined function to be the BLOB whose content is pointed
6393 ** interfaces set the result of the application-defined function to be
6397 ** an application-defined function to be a floating point value specified
6405 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6406 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6407 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6412 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6430 ** of the application-defined function to be the 32-bit signed integer
6433 ** of the application-defined function to be the 64-bit signed integer
6437 ** of the application-defined function to be NULL.
6441 ** set the return value of the application-defined function to be
6442 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6443 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6445 ** application-defined function to be a text string in an encoding
6455 ** is non-negative, then as many bytes (not characters) of the text
6456 ** pointed to by the 2nd parameter are taken as the application-defined
6457 ** function result. If the 3rd parameter is non-negative, then it
6464 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6480 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6482 ** byte-order specified by the BOM. ^The byte-order specified by
6483 ** the BOM at the beginning of the text overrides the byte-order
6486 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6497 ** the application-defined function to be a copy of the
6508 ** also associates the host-language pointer P or type T with that
6510 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6518 ** than the one containing the application-defined function that received
6550 ** the result from the [application-defined SQL function] with
6557 ** Every [application-defined SQL function] that invokes this interface
6565 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6568 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6580 ** ^The name of the collation is a UTF-8 string
6582 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6645 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6688 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6689 ** the names are passed as UTF-16 in machine native byte order.
6690 ** ^A call to either function replaces the existing collation-needed callback.
6756 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6798 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6799 ** TemporaryFolder->Path->Data();
6802 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6815 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6854 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6859 ** the current directory on the sub-platforms of Win32 where that concept is
6863 ** UTF-8 or UTF-16, respectively.
6882 ** CAPI3REF: Test For Auto-Commit Mode
6886 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6890 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6892 ** If certain kinds of errors occur on a statement within a multi-statement
6923 ** for the N-th database on database connection D, or a NULL pointer of N is
6925 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6933 ** remember the string long-term should make their own copy. Applications that
6935 ** threads should mutex-protect calls to this API and should make their own
6947 ** connection D, or if database N is a temporary or in-memory database, then
6952 ** is [DETACH]-ed or until the database connection closes.
6973 ** CAPI3REF: Determine if a database is read-only
6977 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6996 ** a valid schema, then -1 is returned.
7062 ** ^If the callback on a commit hook function returns non-zero,
7084 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7086 ** hook returning non-zero, just as it would be with any other rollback.
7106 ** the schema-name of the attached database that is being autovacuumed,
7114 ** <p>^If there are multiple ATTACH-ed database files that are being
7229 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7230 ** compile-time option is recommended because the
7256 ** shared cache mode should be enabled per-database connection via
7260 ** 32-bit integer is atomic.
7262 ** See Also: [SQLite Shared-Cache Mode]
7270 ** of heap memory by deallocating non-essential memory allocations
7272 ** pages to improve performance is an example of non-essential memory.
7275 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7289 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7324 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7348 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7349 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7382 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7386 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7453 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7465 ** with various operating-system specific extensions added.
7474 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7513 ** [extension loading] while evaluating user-entered SQL, the following API
7521 ** ^This interface enables or disables both the C-API
7524 ** to enable or disable only the C-API.)^
7564 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7666 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7678 ** expr on the right-hand side can be evaluated (and thus the constraint
7693 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7699 ** non-zero.
7703 ** the right-hand side of the corresponding aConstraint[] is evaluated
7704 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7733 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7764 int iColumn; /* Column constrained. -1 for ROWID */
7767 int iTermOffset; /* Used internally - xBestIndex should ignore */
7809 ** ^The left-hand operand of the operator is given by the corresponding
7810 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7813 ** operators have no left-hand operand, and so for those operators the
7822 ** The right-hand operands for each constraint might be accessible using
7823 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7825 ** in the input SQL. If the right-hand operand is another column or an
7829 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7834 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
8029 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
8031 ** read-only access.
8067 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8083 ** and the built-in [zeroblob] SQL function may be used to create a
8084 ** zero-filled blob to read or write using the incremental-blob interface.
8114 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8136 ** ^If the blob handle being closed was opened for read-write access, and if
8137 ** the database is in auto-commit mode and there are no other open read-write
8145 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8172 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8201 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8250 ** ^Names are zero-terminated UTF-8 strings.
8281 ** is selected automatically at compile-time. The following
8292 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8297 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8334 ** cases where it really needs one. If a faster non-recursive mutex
8381 ** then any of the four routines behaves as a no-op.
8394 ** An instance of this structure defines the low-level routines
8444 ** xMutexInit() must be no-ops.
8449 ** memory allocation for a fast or recursive mutex.
8490 ** the routine should return 1. This seems counter-intuitive since
8494 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8510 ** next. Applications that override the built-in mutex logic must be
8526 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8541 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8547 ** CAPI3REF: Low-Level Control Of Database Files
8661 ** by enclosing in double-quotes) so as not to confuse the parser.
8666 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8669 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8675 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8687 ** <li> Put all identifier names inside double-quotes. This is the official
8698 ** compile-time options. For example, "VACUUM" is not a keyword if
8699 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8710 ** An instance of the sqlite3_str object contains a dynamically-sized
8734 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8774 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8779 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8780 ** S must contain at least N non-zero bytes of content. To append a
8781 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8785 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8788 ** single-byte character C onto the end of [sqlite3_str] object X.
8814 ** [SQLITE_NOMEM] following any out-of-memory error, or
8821 ** zero-termination byte.
8856 ** SQLITE_OK on success and a non-zero [error code] on failure.
8859 ** be represented by a 32-bit integer, then the values returned by
8877 ** These integer constants designate various run-time status parameters
8885 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8970 ** non-zero [error code] on failure.
8986 ** The [sqlite3_db_status()] interface will return a non-zero error code
8996 ** satisfied using lookaside memory. Only the high-water value is meaningful;
9004 ** Only the high-water value is meaningful;
9012 ** Only the high-water value is meaningful;
9035 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9075 ** to disk all at once. When pages spill mid-transaction, that introduces
9146 ** A non-zero value in this counter may indicate an opportunity to
9152 ** A non-zero value in this counter may indicate an opportunity to
9181 ** step was bypassed because a Bloom filter returned not-found. The
9251 ** The built-in page cache is recommended for most uses.
9266 ** built-in default page cache is used instead of the application defined
9298 ** false if it is used for an in-memory database. The cache implementation
9309 ** suggested maximum cache-size (number of pages stored by) the cache
9355 ** as its second argument. If the third parameter, discard, is non-zero,
9389 ** is not obligated to free any memory, but well-behaved implementations should
9448 ** for copying in-memory databases to or from persistent files.
9454 ** ^The source database is read-locked only while it is being read;
9488 ** there is already a read or read-write transaction open on the
9520 ** <li> the destination database was opened read-only, or
9521 ** <li> the destination database is using write-ahead-log journaling
9523 ** <li> the destination database is an in-memory database and the
9527 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9528 ** the [sqlite3_busy_handler | busy-handler function]
9530 ** busy-handler returns non-zero before the lock is available, then
9551 ** sqlite3_backup_step(), the source database may be modified mid-way
9568 ** active write-transaction on the destination database is rolled back.
9575 ** ^If an out-of-memory condition or IO error occurred during any prior
9644 ** ^When running in shared-cache mode, a database operation may fail with
9645 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9646 ** individual tables within the shared-cache cannot be obtained. See
9647 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9651 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9655 ** ^Shared-cache locks are released when a database connection concludes
9659 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9669 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9675 ** ^If the blocked connection is attempting to obtain a write-lock on a
9676 ** shared-cache table, and more than one other connection currently holds
9677 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9680 ** ^(There may be at most one unlock-notify callback registered by a
9682 ** blocked connection already has a registered unlock-notify callback,
9685 ** unlock-notify callback is canceled. ^The blocked connections
9686 ** unlock-notify callback may also be canceled by closing the blocked
9689 ** The unlock-notify callback is not reentrant. If an application invokes
9690 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9698 ** When an unlock-notify callback is registered, the application provides a
9702 ** an unlock-notify callback is a pointer to an array of void* pointers,
9706 ** more than one blocked connection that has registered for an unlock-notify
9716 ** Assuming that after registering for an unlock-notify callback a
9727 ** unlock-notify callback is registered. The system is said to be in
9728 ** a deadlocked state if connection A has registered for an unlock-notify
9730 ** B has itself registered for an unlock-notify callback when connection
9733 ** registered for an unlock-notify callback on the conclusion of connection
9745 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9746 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9766 ** and extensions to compare the contents of two buffers containing UTF-8
9767 ** strings in a case-independent fashion, using the same definition of "case
9783 ** Note that this routine returns zero on a match and non-zero if the strings
9800 ** insensitive - equivalent upper and lower case ASCII characters match
9806 ** Note that this routine returns zero on a match and non-zero if the strings
9830 ** a fixed-length buffer on the stack. If the log message is longer than
9837 ** CAPI3REF: Write-Ahead Log Commit Hook
9844 ** the associated write-lock on the database released)^, so the implementation
9850 ** ^The third parameter is the name of the database that was written to -
9851 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9852 ** is the number of pages currently in the write-ahead log file,
9863 ** A single database handle may have at most a single write-ahead log callback
9865 ** previously registered write-ahead log callback. ^The return value is
9878 ** CAPI3REF: Configure an auto-checkpoint
9885 ** more frames in the [write-ahead log] file. ^Passing zero or
9900 ** ^Every new [database connection] defaults to having the auto-checkpoint
9916 ** [write-ahead log] for database X on [database connection] D to be
9917 ** transferred into the database file and for the write-ahead log to
9943 ** in the log were checkpointed. ^The [busy-handler callback]
9950 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9959 ** [busy-handler callback])
9972 ** the log file or to -1 if the checkpoint could not run because
9976 ** was called) or to -1 if the checkpoint could not run due to an error or
9984 ** busy-handler configured, it will not be invoked in this case.
9988 ** obtained immediately, and a busy-handler is configured, it is invoked and
9989 ** the writer lock retried until either the busy-handler returns 0 or the lock
9990 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9991 ** database readers as described above. ^If the busy-handler returns 0 before
9994 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10010 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10086 ** If X is non-zero, then the virtual table implementation guarantees
10130 ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10161 ** [xUpdate] method understands as a "no-change" value.
10302 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10303 ** only sees a single value from the right-hand side of the IN operator
10307 ** table to see all values on the right-hand of the IN operator all at
10312 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10313 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10315 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10316 ** by which the virtual table can ask SQLite if all-at-once processing
10322 ** the IN operator all-at-once, respectively. ^Thus when the third
10323 ** parameter (F) is non-negative, this interface is the mechanism by
10332 ** (non-zero), that means that the constraint is an IN operator
10333 ** that can be processed all-at-once. ^If the constraint is not an IN
10334 ** operator or cannot be processed all-at-once, then the interface returns
10337 ** ^(All-at-once processing of the IN operator is selected if both of the
10341 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10343 ** use the N-th constraint.
10346 ** non-negative had F>=1.
10350 ** the traditional one-at-a-time processing strategy for the IN constraint.
10351 ** ^If both conditions are true, then the argvIndex-th parameter to the
10354 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10360 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10370 ** a parameter that was previously selected for all-at-once IN constraint
10373 ** an xFilter argument that was selected for all-at-once IN constraint
10376 ** ^(Use these routines to access all values on the right-hand side
10418 ** J being a 0-based index into P->aConstraint[], then this routine
10419 ** attempts to set *V to the value of the right-hand operand of
10420 ** that constraint if the right-hand operand is known. ^If the
10421 ** right-hand operand is not known, then *V is set to a NULL pointer.
10424 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10430 ** the right-hand operand of a constraint is a literal value in the original
10431 ** SQL statement. If the right-hand operand is an expression or a reference
10436 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10445 ** "Right-Hand Side".
10480 ** not available, the output variable is set to -1 if the value is numeric,
10486 ** set to the total number of times that the X-th loop has run.</dd>
10490 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10495 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10502 ** to a zero-terminated UTF-8 string containing the name of the index or table
10503 ** used for the X-th loop.
10507 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10508 ** description for the X-th loop.
10512 ** id for the X-th query plan element. The id value is unique within the
10513 ** statement. The select-id is the same value as is output in the first
10524 ** according to the processor time-stamp counter, that elapsed while the
10526 ** all query elements - if it is unavailable the output variable is
10527 ** set to -1.
10550 ** compile-time option.
10558 ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10568 ** for. Query elements are numbered starting from zero. A value of -1 may be
10570 ** - less than -1 or greater than or equal to the total number of query
10571 ** elements used to implement the statement - a non-zero value is returned and
10597 ** CAPI3REF: Zero Scan-Status Counters
10602 ** This API is only available if the library is built with pre-processor
10608 ** CAPI3REF: Flush caches to disk mid-transaction
10611 ** ^If a write-transaction is open on [database connection] D when the
10613 ** pages in the pager-cache that are not currently in use are written out
10617 ** interface flushes caches for all schemas - "main", "temp", and
10622 ** immediately and there is a busy-handler callback configured, it is invoked
10630 ** example an IO error or out-of-memory condition), then processing is
10641 ** CAPI3REF: The pre-update hook.
10645 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10721 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10722 ** triggers; or 2 for changes resulting from triggers called by top-level
10726 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10731 ** pre-update hook is being invoked for some other reason, including a
10732 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10758 ** CAPI3REF: Low-level system error code
10763 ** The return value is OS-dependent. For example, on unix systems, after
10803 ** If there is not already a read-transaction open on schema S when
10835 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10869 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10870 ** SQLITE_IOERR error code - is returned, then the final state of the
10884 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10901 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10968 ** For an ordinary on-disk database file, the serialization is just a
10969 ** copy of the disk file. For an in-memory database or a "TEMP" database,
11013 ** Zero or more of the following constants can be OR-ed together for
11017 ** a pointer to contiguous in-memory database that it is currently using,
11019 ** a contiguous in-memory database, then this option causes
11021 ** using a contiguous in-memory database if it has been initialized by a
11031 ** reopen S as an in-memory database based on the serialization contained
11035 ** permitted to add content to the in-memory database as long as the total
11097 ** should be treated as read-only.
11101 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11154 /* The double-precision datatype used by RTree depends on the
11155 ** SQLITE_RTREE_INT_ONLY compile-time option.
11165 ** R-Tree geometry query as follows:
11190 ** Register a 2nd-generation geometry callback named zScore that can be
11191 ** used as part of an R-Tree geometry query as follows:
11297 ** is not possible for an application to register a pre-update hook on a
11300 ** which a pre-update hook is already defined. The results of attempting
11381 ** disabled - it does not. A newly created session object is enabled.
11388 ** no-op, and may be used to query the current state of the session.
11465 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11471 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11475 ** conflict-handler callback) then the X'' value is returned. The application
11523 ** database row along with the updated values for each updated non-primary-key
11532 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11534 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11547 ** are sorted in the same order in which they were attached (or auto-attached)
11561 ** recorded once - the first time a row with said primary key is inserted,
11568 ** The session object therefore accumulates two types of records - those
11586 ** found and one or more of the non-primary key fields have been
11603 ** This may appear to have some counter-intuitive effects if a single row
11619 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11641 ** does not have a primary key, this function is a no-op (but does not return
11661 ** used to update the table in database zFrom (call this the "from-table")
11663 ** object (call this the "to-table"). Specifically:
11666 ** <li> For each row (primary key) that exists in the to-table but not in
11667 ** the from-table, an INSERT record is added to the session object.
11669 ** <li> For each row (primary key) that exists in the to-table but not in
11670 ** the from-table, a DELETE record is added to the session object.
11673 ** different non-PK values in each, an UPDATE record is added to the
11718 ** Because the non-primary key "old.*" fields are omitted, no
11737 ** Return non-zero if no changes to attached tables have been recorded by
11745 ** are restored. However, if this function returns non-zero, then it is
11832 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11856 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11869 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11872 ** or until the conflict-handler function returns.
11919 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11928 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11959 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11993 ** conflict-handler callback by [sqlite3changeset_apply()] with either
12004 ** "conflicting row" associated with the current conflict-handler callback
12042 ** function with an iterator passed to a conflict-handler by
12050 ** returned. This is to allow the following pattern (pseudo-code):
12247 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12248 ** <tr><th style="white-space:pre">Existing Change </th>
12249 ** <th style="white-space:pre">New Change </th>
12299 ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12357 ** Otherwise, if the return value is non-zero or the xFilter argument to
12393 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12397 ** returned by each invocation of the conflict-handler function. Refer to
12406 ** stored in all non-primary key columns also match the values stored in
12410 ** the non-primary key fields contains a value different from the original
12411 ** row value stored in the changeset, the conflict-handler function is
12414 ** only the values of those non-primary key fields are compared against
12415 ** the current database contents - any trailing database table columns
12419 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12424 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12443 ** This includes the case where the INSERT operation is re-attempted because
12451 ** stored in all modified non-primary key columns also match the values
12455 ** the modified non-primary key fields contains a value different from an
12456 ** original row value stored in the changeset, the conflict-handler function
12458 ** UPDATE changes only contain values for non-primary key fields that are
12460 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12463 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12467 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12485 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12583 ** Values that may be passed as the second argument to a conflict-handler.
12590 ** (non primary-key) fields modified by the update do not contain the
12751 ** combined on a per-field basis, not per-row. This means that in the
12835 ** corresponding non-streaming API functions:
12837 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12838 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12848 ** Non-streaming functions that accept changesets (or patchsets) as input
12853 ** low-memory environment is required to handle very large changesets, the
13117 ** non-negative but less than the number of columns in the table, return
13132 ** non-negative but less than the number of columns in the table, set
13146 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13244 ** If the bClear argument is non-zero, then the auxiliary data is cleared
13266 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13268 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13274 ** modify this structure directly - it should only be used as shown above
13282 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13296 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13298 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13307 ** xPhraseFirstColumn() set iCol to -1).
13376 ** The second and third arguments are an array of nul-terminated strings
13394 ** by argument pText. pText may or may not be nul-terminated. The first
13402 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13407 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13411 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13416 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13449 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13483 ** still appears to contain just two phrases - "(first OR 1st)"
13494 ** when tokenizing query text (it should not - to do so would be
13541 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13642 ** autoconf-based build
13670 ** The hard limit is the ability of a 32-bit signed integer
13671 ** to count the size: 2^31-1 or 2147483647.
13689 ** tell you that in a well-normalized database, you usually should
13747 ** The suggested maximum number of in-memory pages to use for
13750 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13752 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13753 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13756 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13770 ** counted using a signed 8-bit integer which has a maximum value of 127
13782 ** as a signed 32-bit integer can hold.
13789 ** imposed by the use of 16-bit offsets within each page.
13795 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13796 ** compiled with the default page-size limit will not be able to rollback
13819 ** device characteristics (sector-size and atomic write() support),
13836 ** This value can be lowered (or raised) at run-time using that the
13867 #pragma warn -rch /* unreachable code */
13868 #pragma warn -ccc /* Condition is always true or false */
13869 #pragma warn -aus /* Assigned value is never used */
13870 #pragma warn -csu /* Comparing signed and unsigned */
13871 #pragma warn -spa /* Suspicious pointer arithmetic */
13879 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13912 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13925 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13926 #else /* Generates a warning - but it always works */
13971 ** SEH support if the -DSQLITE_OMIT_SEH option is given.
13983 ** level of threadsafety. 2 means the library is multithreaded - multiple
13991 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13999 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
14005 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14012 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14041 # error "Two or more of the following compile-time configuration options\
14079 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14144 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14156 ** of SQLite to unexpected behavior - to make the code "self-healing"
14162 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
14248 ** Return true (non-zero) if the input is an integer that is too large
14249 ** to fit in 32-bits. This macro is used inside of various testcase()
14250 ** macros to verify that we have tested SQLite for large-file support.
14277 ** This is the header file for the generic hash-table implementation
14288 ** The internals of this structure are intended to be opaque -- client
14295 ** All elements of the hash table are on a single doubly-linked list.
14299 ** the global doubly-linked list. The contents of the bucket are the
14300 ** element pointed to plus the next _ht.count-1 elements in the list.
14319 ** structure. All elements are stored on a single doubly-linked list.
14350 #define sqliteHashFirst(H) ((H)->first)
14351 #define sqliteHashNext(E) ((E)->next)
14352 #define sqliteHashData(E) ((E)->data)
14353 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14354 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14359 #define sqliteHashCount(H) ((H)->count)
14569 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14574 ** substitute integer for floating-point
14605 ** the VDBE-level file format changes. The following macros define the
14616 ** changed at run-time using a pragma.
14624 ** on the command-line
14654 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14656 ** The default value of "20" was chosen to minimize the run-time of the
14657 ** speedtest1 test program with options: --shrink-memory --reprepare
14671 ** The compile-time options SQLITE_MMAP_READWRITE and
14684 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14715 ** types can be conveniently redefined at compile-type. Like this:
14717 ** cc '-DUINTPTR_TYPE=long long int' ...
14757 typedef sqlite_int64 i64; /* 8-byte signed integer */
14758 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14759 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14760 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14761 typedef INT16_TYPE i16; /* 2-byte signed integer */
14762 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14763 typedef INT8_TYPE i8; /* 1-byte signed integer */
14771 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14780 ** Estimated quantities used for query planning are stored as 16-bit
14782 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14791 ** 1 -> 0 20 -> 43 10000 -> 132
14792 ** 2 -> 10 25 -> 46 25000 -> 146
14793 ** 3 -> 16 100 -> 66 1000000 -> 199
14794 ** 4 -> 20 1000 -> 99 1048576 -> 200
14795 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14800 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14843 ** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
14848 ** |-----------------| FALSE
14849 ** |-------|
14853 ** |-----------------|
14854 ** |-------| TRUE
14858 ** |-----------------|
14859 ** |-------| FALSE
14866 ** and whether or not that determination is run-time or compile-time.
14868 ** For best performance, an attempt is made to guess at the byte-order
14869 ** using C-preprocessor macros. If that is unsuccessful, or if
14870 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14871 ** at run-time.
14876 ** -DSQLITE_BYTEORDER=1234
14880 ** -DSQLITE_BYTEORDER=4321
14882 ** to cause the build to work for little-endian or big-endian processors,
14885 #ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
14923 ** Constants for the largest and smallest possible 64-bit signed integers.
14924 ** These macros are designed to work correctly on both 32-bit and 64-bit
14929 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14933 ** to force 8-byte alignment on 64-bit architectures.
14938 ** pointers in size, and so it is a no-op on systems where the pointer
14954 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14959 ** underlying malloc() implementation might return us 4-byte aligned
14960 ** pointers. In that case, only verify 4-byte alignment.
14963 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
14965 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
14977 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14997 ** default MMAP_SIZE is specified at compile-time, make sure that it does
15021 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15033 ** 0x00000008 Result-set wildcard expansion
15039 ** 0x00000200 Count-of-view optimization
15044 ** 0x00004000 Push-down optimization
15045 ** 0x00008000 After all FROM-clause analysis
15066 ** (---any--) Top-level block structure
15067 ** 0x-------F High-level debug messages
15068 ** 0x----FFF- More detail
15069 ** 0xFFFF---- Low-level debug messages
15095 ** An instance of the following structure is used to store the busy-handler
15100 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15134 ** The root-page of the schema table.
15153 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
15172 ** directly, we use its constant as a key to lookup the run-time allocated
15174 ** for the run-time allocated buffer.
15177 ** macros become no-ops and have zero performance impact.
15278 ** Changing this from a 64-bit to a 32-bit type limits the number of
15300 #define ALLBITS ((Bitmask)-1)
15301 #define TOPBIT (((Bitmask)1)<<(BMS-1))
15330 ** This header file (together with is companion C source-code file
15334 ** This header file is #include-ed by sqliteInt.h and thus ends up
15342 ** necessary pre-processor macros for it.
15358 ** This file contains pre-processor directives related to operating system
15379 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15442 ** a no-op
15475 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15477 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15478 ** Mcafee started using SQLite in their anti-virus product and it
15524 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15632 ** sqlite3_malloc() to obtain space for the file-handle structure.
15668 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15691 ** is devoted to storing a super-journal name - there are no more pages to
15695 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15696 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15704 #define PAGER_MEMORY 0x0002 /* In-memory database */
15709 #define PAGER_LOCKINGMODE_QUERY -1
15720 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15725 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15726 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15732 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15754 ** that make up the Pager sub-system API. See source code comments for
15906 ** This header file defines the interface that the sqlite B-Tree file
15919 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15926 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15927 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15940 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15955 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15956 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
16010 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16013 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16016 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
16017 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
16037 ** For example, the free-page-count field is located at byte offset 36 of
16038 ** the database file header. The incr-vacuum-flag field is located at
16042 ** It is a read-only number computed by the pager. But we merge it with
16055 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
16069 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16070 ** column of the b-tree of the cursor. The Expr tree will not contain
16071 ** any function calls nor subqueries nor references to b-trees other than
16074 ** The design of the _RANGE hint is aid b-tree implementations that try
16075 ** to prefetch content from remote machines - to provide those
16100 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
16105 ** For read-only cursors the wrFlag argument is always zero. For read-write
16116 ** by this, the native b-tree engine of SQLite, but it is available to
16118 ** b-tree system. For alternative storage engines in which a delete of
16122 ** and DELETE operations as no-ops, and any READ operation against a
16125 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
16131 int wrFlag, /* 1 for writing. 0 for read-only */
16276 ** Enter and Leave procedures no-ops.
16363 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16390 u32 iSrcLine; /* Source-code line that generated this opcode
16402 ** A sub-routine used to implement a trigger program.
16405 VdbeOp *aOp; /* Array of opcodes for sub-program */
16411 SubProgram *pNext; /* Next sub-program already visited */
16431 #define P4_STATIC (-1) /* Pointer to a static string */
16432 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16433 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16434 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16435 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16437 #define P4_FREE_IF_LE (-6)
16438 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16439 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16440 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16441 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
16442 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
16443 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16444 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16445 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16446 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16447 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16448 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16549 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16550 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16551 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16580 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16597 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16614 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16650 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16728 ** Additional non-public SQLITE_PREPARE_* flags
16783 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16788 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16865 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16902 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16907 ** // in distinguishing equal and not-equal.
16910 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16911 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16919 ** is responsible for keeping track of this and reporting byte-code branches
17057 /* Modify the page-size after the cache has been created. */
17079 /* Change a page number. Used by incr-vacuum. */
17121 /* Set and get the suggested cache-size for the specified pager-cache.
17124 ** the total number of pages cached by purgeable pager-caches to the sum
17125 ** of the suggested cache-sizes.
17132 /* Set or get the suggested spill-size for the specified pager-cache.
17134 ** The spill-size is the minimum number of pages in cache before the cache
17198 ** at start-time.
17200 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
17203 ** start-time.
17205 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17207 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17224 ** If this is a no-op implementation, implement everything as macros.
17245 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17265 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17266 ** In other words, the zero-based numbers are used for all external interfaces
17267 ** and the one-based values are used internally.
17295 ** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17324 ** Db.pSchema->flags field.
17326 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17327 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17328 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17329 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17332 ** Allowed values for the DB.pSchema->flags field.
17352 ** Lookaside malloc is a set of fixed-size buffers that can be used
17374 ** in a performance-critical path. sz should be set by to szTrue whenever
17379 ** come off of pFree first, then pInit as a fallback. This dual-list
17380 ** allows use to compute a high-water mark - the maximum number of allocations
17381 ** outstanding at any point in the past - by subtracting the number of
17384 ** Enhancement on 2019-12-12: Two-size-lookaside
17388 ** The two-size-lookaside enhancement breaks up the lookaside allocation
17389 ** into two pools: One of 128-byte slots and the other of the default size
17390 ** (1200-byte) slots. Allocations are filled from the small-pool first,
17391 ** failing over to the full-size pool if that does not work. Thus more
17408 void *pMiddle; /* First byte past end of full-size buffers and
17413 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17419 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17420 #define EnableLookaside db->lookaside.bDisable--;\
17421 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17423 /* Size of the smaller allocations in two-size lookaside */
17431 ** A hash table for built-in function definitions. (Application-defined
17514 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17522 u8 autoCommit; /* The auto-commit flag. */
17526 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17532 u8 noSharedCache; /* True if no shared-cache backends */
17580 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17618 int nSavepoint; /* Number of non-transaction savepoints */
17619 int nStatement; /* Number of nested statement-transactions */
17649 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17650 #define ENC(db) ((db)->enc)
17655 ** C-compilers still do not accept LL integer literals.
17698 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17699 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17705 #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17722 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17743 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17747 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17749 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17757 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17762 #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17764 #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
17770 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17771 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17777 #define ConstFactorOk(P) ((P)->okConstFactor)
17792 ** structure. For global built-in functions (ex: substr(), max(), count())
17794 ** For per-connection application-defined functions, a pointer to this
17795 ** structure is held in the db->aHash hash table.
17797 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17798 ** field is used by per-connection app-def functions.
17801 i8 nArg; /* Number of arguments. -1 means unlimited */
17805 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17808 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17817 ** This structure encapsulates a user-function destructor callback (as
17850 ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
17857 ** See multiple instances of tag-20230109-1.
17861 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17864 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17865 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17866 #define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
17867 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17868 /* 0x0200 -- available for reuse */
17869 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17873 ** single query - might change over time */
17874 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17876 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17881 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17882 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17886 /* Identifier numbers for each in-line function */
17904 ** as the user-data (sqlite3_user_data()) for the function. If
17915 ** zName is the name of a function that is implemented by in-line
17921 ** zName is the name of a test-only function implemented by in-line
17930 ** a single query. The iArg is ignored. The user-data is always set
17934 ** For math-library functions. xPtr is an arbitrary pointer.
17939 ** ignored and the user-data for these functions is set to an
17940 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17958 ** available as the function user-data (sqlite3_user_data()). The
18023 char *zName; /* Savepoint name (nul-terminated) */
18084 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18115 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
18135 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18147 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
18201 ** database connections, even when the rest of the in-memory database
18211 ** database schema are initially stored in a linked-list pointed to by
18218 ** When an in-memory Table object is deleted (for example when the
18269 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18300 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18301 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18311 #define TF_Readonly 0x00000001 /* Read-only system table */
18322 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18323 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18338 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18339 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18347 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18349 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18357 ** only works for non-virtual tables (ordinary tables and views) and is
18362 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18363 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18365 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18374 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18375 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18377 /* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18399 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18402 ** from-table == child-table
18403 ** to-table == parent-table
18406 ** which is attached to the from-table. The to-table need not exist when
18407 ** the from-table is created. The existence of the to-table is not checked.
18421 /* EV: R-30323-21917 */
18487 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18515 ** pKeyInfo->nField.
18519 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18523 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18524 ** multiple entries in the b-tree with the same key (when only looking
18525 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18530 ** get and equal results when comparing this structure to a b-tree record.
18534 ** b-tree.
18537 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18576 ** algorithm to employ when an attempt is made to insert a non-unique
18580 ** for a fast test to see if an index can serve as a covering index.
18583 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18585 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18586 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18596 ** number (it cannot - the database page is not allocated until the VDBE
18621 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18633 IndexSample *aSample; /* Samples of the left-most key */
18634 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18635 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18649 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18652 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18657 #define XN_ROWID (-1) /* Indexed column is the rowid */
18658 #define XN_EXPR (-2) /* Indexed column is an expression */
18690 const char *z; /* Text of the token. Not NULL-terminated! */
18714 int sortingIdxPTab; /* Cursor number of pseudo-table */
18748 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18751 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18753 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18756 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18757 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18758 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18763 ** the option is available (at compile-time).
18778 ** to represent the greater-than-or-equal-to operator in the expression
18792 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18811 ** subquery gives a constant result, then iTable is -1. If the subquery
18816 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18847 int iValue; /* Non-negative integer value if EP_IntValue */
18872 ** TK_TRIGGER: 1 -> new, 0 -> old
18877 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18880 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18944 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18945 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18946 #define ExprSetProperty(E,P) (E)->flags|=(P)
18947 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18948 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18949 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18950 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
18955 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18956 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18957 #define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
18958 #define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
18959 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18960 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18961 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18962 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18963 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18972 ** processes but is a no-op for delivery.
18975 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18976 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18977 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18997 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
19007 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
19023 ** ---------- -------------------------
19083 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19092 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
19103 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19114 ** In the colUsed field, the high-order bit (bit 63) is set if the table
19115 ** contains more than 63 columns and the 64-th or later column is used.
19133 int regResult; /* Registers holding results of a co-routine */
19138 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19139 unsigned isCorrelated :1; /* True if sub-query is correlated */
19141 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19147 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19159 ExprList *pFuncArg; /* Arguments to table-valued-function */
19209 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
19212 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
19215 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
19244 ** NameContexts can be nested. When resolving names, the inner-most
19260 ExprList *pEList; /* Optional list of result-set columns */
19301 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19309 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19310 ** conflict-target clause.) The pUpsertTargetWhere is the optional
19415 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
19422 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
19430 ** identified by pDest->iSDParm.
19432 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
19434 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19439 ** the side-effects of functions.
19446 ** in register pDest->iSDParm then abandon the rest
19450 ** row of result as the key in table pDest->iSDParm.
19451 ** Apply the affinity pDest->affSdst before storing
19454 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19460 ** SRT_Coroutine Generate a co-routine that returns a new row of
19462 ** of the co-routine is stored in register pDest->iSDParm
19463 ** and the result row is stored in pDest->nDest registers
19464 ** starting with pDest->iSdst.
19466 ** SRT_Table Store results in temporary table pDest->iSDParm.
19472 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19473 ** But also use temporary table pDest->iSDParm+1 as
19477 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
19481 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19483 ** index at pDest->iSDParm+1 hold all prior stores.
19486 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19487 ** table is an intkey table - in this case the first
19489 ** key. If (pDest->iSDParm>0), then the table is an index
19490 ** table. (pDest->iSDParm) is the number of key columns in
19502 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19508 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19536 ** the code generator needs. We have to keep per-table autoincrement
19555 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19637 ** The nTableLock and aTableLock variables are only used if the shared-cache
19638 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19639 ** used to store the set of table-locks required by the statement being
19672 ** of the base register during check-constraint eval */
19684 int nMaxArg; /* Max args passed to user function by sub-program */
19691 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19736 int nHeight; /* Expression tree height of current sub-select */
19748 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19770 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19772 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19781 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19787 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19793 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19817 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19821 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19825 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19861 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19862 the <column-list> is stored here */
19881 ** that is a part of a trigger-program.
19886 ** the first step of the trigger-program.
19893 ** orconf -> stores the ON CONFLICT algorithm
19894 ** pSelect -> The content to be inserted - either a SELECT statement or
19896 ** zTarget -> Dequoted name of the table to insert into.
19897 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19898 ** statement, then this stores the column-names to be
19900 ** pUpsert -> The ON CONFLICT clauses for an Upsert
19903 ** zTarget -> Dequoted name of the table to delete from.
19904 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19908 ** zTarget -> Dequoted name of the table to update.
19909 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19911 ** pExprList -> A list of the columns to update and the expressions to update
19916 ** pSelect -> The SELECT statement
19919 ** pExprList -> The list of expressions that follow the RETURNING keyword.
19935 TriggerStep *pNext; /* Next in the link-list */
19936 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19966 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19970 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19973 ** The following object is the header for an "RCStr" or "reference-counted
19984 ** 3. Make a (read-only) copy of a read-only RCStr string using
20015 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20018 ** be used on trunk check-ins. They are a temporary mechanism available
20023 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
20025 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
20040 u8 bUseCis; /* Use covering indices for full-scans */
20045 int neverCorrupt; /* Database is always well-formed */
20048 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20049 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20050 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20051 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20061 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20063 /* The above might be initialized to non-zero. The following need to always
20099 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20101 /* vvvv--- must be last ---vvv */
20109 ** the assert is only valid on a well-formed database. Instead of:
20121 ** things that are always true for well-formed databases.
20126 ** Context pointer passed down through the tree-walk.
20135 u16 mWFlags; /* Use-dependent flags */
20169 const char *zType; /* Type of the container - used for error messages */
20170 const Token *pName; /* Name of the container - used for error messages */
20198 ** Return code from the parse-tree walking primitives and their
20269 ** data structures on sqlite3DebugPrintf() using a tree-like view.
20286 ** (2) All window functions in a single SELECT form a linked-list
20356 ** Assuming zIn points to the first byte of a UTF-8 character,
20357 ** advance zIn to point to the first byte of the next UTF-8 character.
20368 ** routines that report the line-number on which the error originated
20413 ** The ctype.h header is needed for non-ASCII systems. It is also
20549 # define MAN754 ((((u64)1)<<52)-1)
20575 char sign; /* '+' or '-' */
20713 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20714 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20723 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20984 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20985 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
21048 ** Routines to read and write variable-length integers. These used to
21274 ** The interface to the LEMON-generated parser
21335 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
21411 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21413 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21415 ** provided (enforcement of FK constraints requires the triggers sub-system).
21466 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21520 ** sqlite3IoTrace is a pointer to a printf-like routine used to
21553 ** passed back to non-lookaside free() routines. Asserts such as the
21554 ** example above are placed on the non-lookaside free() routines to verify
21557 ** All of this is no-op for a production build. It only comes into
21558 ** play when the SQLITE_MEMDEBUG compile-time option is used.
21565 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21609 # define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
21631 ** all of the platform-specific files (os_*.c) and is #included into those
21643 ** switch. The following code should catch this problem at compile-time.
21658 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21682 || sqlite3_io_error_pending-- == 1 ) \
21697 sqlite3_diskfull_pending--; \
21740 ** This file implements routines used to report what compile-time options
21743 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21747 ** autoconf-based build
21754 /* These macros are provided to "stringify" the value of the define
21761 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21767 ** An array of names of all compile-time options. This array should
21768 ** be sorted A-Z.
21771 ** only a handful of compile-time options, so most times this array is usually
21806 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21810 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21812 "COMPILER=gcc-" __VERSION__,
22532 /* An array to map all upper-case characters into their corresponding
22533 ** lower-case character.
22535 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22575 /* All of the upper-to-lower conversion data is above. The following
22592 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22593 ** be undefined behavior. That's goofy, but the C-standards people thought
22601 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22602 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22603 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22606 ** The following 256 byte lookup table is used to support SQLites built-in
22615 ** SQLite identifier character 0x40 $, _, or non-ascii
22619 ** case. i.e. if the character is a lower-case ASCII character.
22620 ** If x is a lower-case ASCII character, then its upper-case equivalent
22621 ** is (x - 0x20). Therefore toupper() can be implemented as:
22628 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22630 ** non-ASCII UTF character. Hence the test for whether or not a character is
22639 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22671 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22675 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22676 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22678 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22686 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22687 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22688 ** that compile-time option is omitted.
22694 # error "Compile-time disabling of covering index scan using the\
22695 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22711 ** before 3.12.0). -1 means always keep the entire statement journal in
22721 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22723 ** and N is the number of slots. The lookaside-configuration can be
22724 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22725 ** or at run-time for an individual database connection using
22728 ** With the two-size-lookaside enhancement, less lookaside is required.
22729 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22730 ** and 93 128-byte slots, which is more lookaside than is available
22731 ** using the older 1200,100 configuration without two-size-lookaside.
22742 /* The default maximum size of an in-memory database created using
22805 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
22818 ** Hash table for global functions - functions common to all
22820 ** read-only.
22839 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22846 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
23006 ** * A b-tree cursor
23007 ** - In the main database or in an ephemeral database
23008 ** - On either an index or a table
23011 ** * A one-row "pseudotable" stored in a single register
23016 i8 iDb; /* Index of cursor database in db->aDb[] */
23025 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23028 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23077 /* Return true if P is a null-only cursor
23080 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
23102 ** When a sub-program is executed (OP_Program), a structure of this type
23105 ** values stored in the Vdbe struct. When the sub-program is finished,
23107 ** restoring the state of the VM to as it was before the sub-program
23143 i64 nDbChange; /* Value of db->nChange */
23178 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23197 ** UPDATE no-change flag set
23203 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23207 ** length Mem.n. Zero-terminated if
23263 /* Return TRUE if Mem X contains dynamically allocated content - anything
23267 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
23273 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
23276 ** True if Mem X is a NULL-nochng type.
23279 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
23280 && (X)->n==0 && (X)->u.nZero==0)
23291 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
23357 int iSelectID; /* The "Select-ID" for this loop */
23362 /* The DblquoteStr object holds the text of a double-quoted
23366 ** list is consulted for each double-quoted identifier to see if the
23414 i64 startTime; /* Time when query started - used for profiling */
23428 bft changeCntOn:1; /* True to update the change-counter */
23433 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23439 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23446 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23486 ** set of values on the right-hand side of an IN constraint.
23502 ** single-byte varint.
23660 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23705 ** we have to locate the state vector at run-time. In the more common
23735 ** The value of N is added to the current status value and the high-water
23739 ** mark is unchanged. N must be non-negative for StatusDown().
23759 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
23831 p = p->pNext; in countLookasideSlots()
23841 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
23842 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
23844 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
23845 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
23847 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
23848 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
23858 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
23859 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
23867 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
23872 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
23874 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
23875 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
23876 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
23877 db->lookaside.pFree = 0; in sqlite3_db_status()
23880 p = db->lookaside.pSmallFree; in sqlite3_db_status()
23882 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
23883 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
23884 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
23885 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
23898 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
23899 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
23901 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
23903 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
23918 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
23919 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
23945 db->pnBytesFreed = &nByte; in sqlite3_db_status()
23946 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
23947 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
23948 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
23949 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
23954 pSchema->tblHash.count in sqlite3_db_status()
23955 + pSchema->trigHash.count in sqlite3_db_status()
23956 + pSchema->idxHash.count in sqlite3_db_status()
23957 + pSchema->fkeyHash.count in sqlite3_db_status()
23959 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
23960 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
23961 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
23962 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
23964 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
23967 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
23972 db->pnBytesFreed = 0; in sqlite3_db_status()
23973 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
23990 db->pnBytesFreed = &nByte; in sqlite3_db_status()
23991 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
23992 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
23993 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ in sqlite3_db_status()
23996 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
23997 db->pnBytesFreed = 0; in sqlite3_db_status()
23999 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
24021 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24022 if( db->aDb[i].pBt ){ in sqlite3_db_status()
24023 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
24027 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
24028 /* IMP: R-54100-20147 */ in sqlite3_db_status()
24029 /* IMP: R-29431-39229 */ in sqlite3_db_status()
24034 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
24039 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
24040 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
24048 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
24068 ** There is only one exported symbol in this file - the function
24077 ** 1970-01-01 00:00:00 is JD 2440587.5
24078 ** 2000-01-01 00:00:00 is JD 2451544.5
24080 ** This implementation requires years to be expressed as a 4-digit number
24081 ** which means that only dates between 0000-01-01 and 9999-12-31 can
24087 ** use the julian calendar for dates prior to 1582-10-15 and for some
24095 ** ISBN 0-943396-61-1
24096 ** Willmann-Bell, Inc
24143 ** of a four-character format specifiers ABCD is:
24157 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24158 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24159 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24160 ** the 2-digit day which is the last integer in the set.
24173 char N = zFormat[0] - '0'; in getDigits()
24174 char min = zFormat[1] - '0'; in getDigits()
24179 max = aMx[zFormat[2] - 'a']; in getDigits()
24182 while( N-- ){ in getDigits()
24186 val = val*10 + *zDate - '0'; in getDigits()
24203 ** Parse a timezone extension on the end of a date-time.
24206 ** (+/-)HH:MM
24213 ** of change in p->tz and return 0. If a parser error occurs,
24214 ** return non-zero.
24223 p->tz = 0; in parseTimezone()
24225 if( c=='-' ){ in parseTimezone()
24226 sgn = -1; in parseTimezone()
24240 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
24243 p->tzSet = 1; in parseTimezone()
24271 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
24280 p->validJD = 0; in parseHhMmSs()
24281 p->rawS = 0; in parseHhMmSs()
24282 p->validHMS = 1; in parseHhMmSs()
24283 p->h = h; in parseHhMmSs()
24284 p->m = m; in parseHhMmSs()
24285 p->s = s + ms; in parseHhMmSs()
24287 p->validTZ = (p->tz!=0)?1:0; in parseHhMmSs()
24296 p->isError = 1; in datetimeError()
24300 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24301 ** that the YYYY-MM-DD is according to the Gregorian calendar.
24308 if( p->validJD ) return; in computeJD()
24309 if( p->validYMD ){ in computeJD()
24310 Y = p->Y; in computeJD()
24311 M = p->M; in computeJD()
24312 D = p->D; in computeJD()
24314 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
24318 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
24323 Y--; in computeJD()
24327 B = 2 - A + (A/4); in computeJD()
24330 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
24331 p->validJD = 1; in computeJD()
24332 if( p->validHMS ){ in computeJD()
24333 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); in computeJD()
24334 if( p->validTZ ){ in computeJD()
24335 p->iJD -= p->tz*60000; in computeJD()
24336 p->validYMD = 0; in computeJD()
24337 p->validHMS = 0; in computeJD()
24338 p->validTZ = 0; in computeJD()
24346 ** YYYY-MM-DD HH:MM:SS.FFF
24347 ** YYYY-MM-DD HH:MM:SS
24348 ** YYYY-MM-DD HH:MM
24349 ** YYYY-MM-DD
24352 ** on success and 1 if the input string is not a well-formed
24358 if( zDate[0]=='-' ){ in parseYyyyMmDd()
24364 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
24372 p->validHMS = 0; in parseYyyyMmDd()
24376 p->validJD = 0; in parseYyyyMmDd()
24377 p->validYMD = 1; in parseYyyyMmDd()
24378 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
24379 p->M = M; in parseYyyyMmDd()
24380 p->D = D; in parseYyyyMmDd()
24381 if( p->validTZ ){ in parseYyyyMmDd()
24393 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
24394 if( p->iJD>0 ){ in setDateTimeToCurrent()
24395 p->validJD = 1; in setDateTimeToCurrent()
24406 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
24409 p->s = r; in setRawDateNumber()
24410 p->rawS = 1; in setRawDateNumber()
24412 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
24413 p->validJD = 1; in setRawDateNumber()
24423 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
24427 ** In the first form, the +/-HH:MM is always optional. The fractional
24451 p->useSubsec = 1; in parseDateOrTime()
24457 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24480 if( p->validYMD ) return; in computeYMD()
24481 if( !p->validJD ){ in computeYMD()
24482 p->Y = 2000; in computeYMD()
24483 p->M = 1; in computeYMD()
24484 p->D = 1; in computeYMD()
24485 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
24489 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
24490 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
24491 A = Z + 1 + A - (A/4); in computeYMD()
24493 C = (int)((B - 122.1)/365.25); in computeYMD()
24495 E = (int)((B-D)/30.6001); in computeYMD()
24497 p->D = B - D - X1; in computeYMD()
24498 p->M = E<14 ? E-1 : E-13; in computeYMD()
24499 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
24501 p->validYMD = 1; in computeYMD()
24509 if( p->validHMS ) return; in computeHMS()
24511 day_ms = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
24512 p->s = (day_ms % 60000)/1000.0; in computeHMS()
24514 p->m = day_min % 60; in computeHMS()
24515 p->h = day_min / 60; in computeHMS()
24516 p->rawS = 0; in computeHMS()
24517 p->validHMS = 1; in computeHMS()
24532 p->validYMD = 0; in clearYMD_HMS_TZ()
24533 p->validHMS = 0; in clearYMD_HMS_TZ()
24534 p->validTZ = 0; in clearYMD_HMS_TZ()
24544 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24558 ** using whatever operating-system specific localtime facility that
24560 ** non-zero on any kind of error.
24562 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24565 ** invoked in place of the OS-defined localtime() function.
24567 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24633 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ in toLocaltime()
24634 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ in toLocaltime()
24636 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in toLocaltime()
24643 iYearDiff = (2000 + x.Y%4) - x.Y; in toLocaltime()
24647 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24650 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24653 sqlite3_result_error(pCtx, "local time unavailable", -1); in toLocaltime()
24656 p->Y = sLocal.tm_year + 1900 - iYearDiff; in toLocaltime()
24657 p->M = sLocal.tm_mon + 1; in toLocaltime()
24658 p->D = sLocal.tm_mday; in toLocaltime()
24659 p->h = sLocal.tm_hour; in toLocaltime()
24660 p->m = sLocal.tm_min; in toLocaltime()
24661 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; in toLocaltime()
24662 p->validYMD = 1; in toLocaltime()
24663 p->validHMS = 1; in toLocaltime()
24664 p->validJD = 0; in toLocaltime()
24665 p->rawS = 0; in toLocaltime()
24666 p->validTZ = 0; in toLocaltime()
24667 p->isError = 0; in toLocaltime()
24677 ** Where NNN is an arbitrary floating-point number and "days" can be one
24700 if( !p->rawS || p->validJD ){ in autoAdjustDate()
24701 p->rawS = 0; in autoAdjustDate()
24702 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ in autoAdjustDate()
24703 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ in autoAdjustDate()
24705 double r = p->s*1000.0 + 210866760000000.0; in autoAdjustDate()
24707 p->iJD = (sqlite3_int64)(r + 0.5); in autoAdjustDate()
24708 p->validJD = 1; in autoAdjustDate()
24709 p->rawS = 0; in autoAdjustDate()
24714 ** Process a modifier to a date-time stamp. The modifiers are
24755 if( idx>1 ) return 1; /* IMP: R-33611-57934 */ in parseModifier()
24765 ** Always interpret the prior number as a julian-day value. If this in parseModifier()
24771 if( idx>1 ) return 1; /* IMP: R-31176-64601 */ in parseModifier()
24772 if( p->validJD && p->rawS ){ in parseModifier()
24774 p->rawS = 0; in parseModifier()
24796 ** Treat the current value of p->s as the number of in parseModifier()
24799 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
24800 if( idx>1 ) return 1; /* IMP: R-49255-55373 */ in parseModifier()
24801 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
24804 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
24805 p->validJD = 1; in parseModifier()
24806 p->rawS = 0; in parseModifier()
24812 if( p->tzSet==0 ){ in parseModifier()
24819 iGuess = iOrigJD = p->iJD; in parseModifier()
24824 iGuess -= iErr; in parseModifier()
24830 iErr = new.iJD - iOrigJD; in parseModifier()
24833 p->iJD = iGuess; in parseModifier()
24834 p->validJD = 1; in parseModifier()
24835 p->tzSet = 1; in parseModifier()
24848 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
24855 p->validTZ = 0; in parseModifier()
24856 p->validJD = 0; in parseModifier()
24858 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
24859 if( Z>n ) Z -= 7; in parseModifier()
24860 p->iJD += (n - Z)*86400000; in parseModifier()
24883 p->useSubsec = 1; in parseModifier()
24888 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
24891 p->validHMS = 1; in parseModifier()
24892 p->h = p->m = 0; in parseModifier()
24893 p->s = 0.0; in parseModifier()
24894 p->rawS = 0; in parseModifier()
24895 p->validTZ = 0; in parseModifier()
24896 p->validJD = 0; in parseModifier()
24898 p->D = 1; in parseModifier()
24901 p->M = 1; in parseModifier()
24902 p->D = 1; in parseModifier()
24910 case '-': in parseModifier()
24929 if( z[n]=='-' ){ in parseModifier()
24938 if( z[n]=='-' ){ in parseModifier()
24939 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the in parseModifier()
24941 ** the range 0-11 and DD is limited to 0-30. in parseModifier()
24943 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */ in parseModifier()
24945 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
24948 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
24954 p->validJD = 0; in parseModifier()
24955 if( z0=='-' ){ in parseModifier()
24956 p->Y -= Y; in parseModifier()
24957 p->M -= M; in parseModifier()
24958 D = -D; in parseModifier()
24960 p->Y += Y; in parseModifier()
24961 p->M += M; in parseModifier()
24963 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
24964 p->Y += x; in parseModifier()
24965 p->M -= x*12; in parseModifier()
24967 p->validHMS = 0; in parseModifier()
24968 p->validYMD = 0; in parseModifier()
24969 p->iJD += (i64)D*86400000; in parseModifier()
24984 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
24996 tx.iJD -= 43200000; in parseModifier()
24998 tx.iJD -= day*86400000; in parseModifier()
24999 if( z0=='-' ) tx.iJD = -tx.iJD; in parseModifier()
25002 p->iJD += tx.iJD; in parseModifier()
25013 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
25016 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
25020 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
25026 p->M += (int)r; in parseModifier()
25027 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25028 p->Y += x; in parseModifier()
25029 p->M -= x*12; in parseModifier()
25030 p->validJD = 0; in parseModifier()
25031 r -= (int)r; in parseModifier()
25038 p->Y += y; in parseModifier()
25039 p->validJD = 0; in parseModifier()
25040 r -= (int)r; in parseModifier()
25045 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); in parseModifier()
25061 ** Process time function arguments. argv[0] is a date-time stamp.
25098 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
25129 ** the unix epoch of 1970-01-01 00:00:00 GMT.
25140 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); in unixepochFunc()
25142 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); in unixepochFunc()
25150 ** Return YYYY-MM-DD HH:MM:SS
25163 if( Y<0 ) Y = -Y; in datetimeFunc()
25168 zBuf[5] = '-'; in datetimeFunc()
25171 zBuf[8] = '-'; in datetimeFunc()
25199 zBuf[0] = '-'; in datetimeFunc()
25202 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); in datetimeFunc()
25252 ** Return YYYY-MM-DD
25265 if( Y<0 ) Y = -Y; in dateFunc()
25270 zBuf[5] = '-'; in dateFunc()
25273 zBuf[8] = '-'; in dateFunc()
25278 zBuf[0] = '-'; in dateFunc()
25293 ** %H hour 00-24
25294 ** %j day of year 000-366
25296 ** %m month 01-12
25297 ** %M minute 00-59
25298 ** %s seconds since 1970-01-01
25299 ** %S seconds 00-59
25300 ** %w day of week 0-6 Sunday==0
25301 ** %W week of year 00-53
25302 ** %Y year 0000-9999
25319 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
25321 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); in strftimeFunc()
25328 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25345 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); in strftimeFunc()
25356 if( h>12 ) h -= 12; in strftimeFunc()
25369 nDay = (int)((x.iJD-y.iJD+43200000)/86400000); in strftimeFunc()
25373 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7); in strftimeFunc()
25407 (x.iJD - 21086676*(i64)10000000)/1000.0); in strftimeFunc()
25409 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
25443 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
25481 ** +YYYY-MM-DD HH:MM:SS.SSS
25483 ** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
25489 ** ISO-8601 string. The unix timestamps are not supported by this
25508 Y = d1.Y - d2.Y; in timediffFunc()
25514 M = d1.M - d2.M; in timediffFunc()
25516 Y--; in timediffFunc()
25525 M--; in timediffFunc()
25528 Y--; in timediffFunc()
25530 d2.M--; in timediffFunc()
25533 d2.Y--; in timediffFunc()
25538 d1.iJD -= d2.iJD; in timediffFunc()
25541 sign = '-'; in timediffFunc()
25542 Y = d2.Y - d1.Y; in timediffFunc()
25548 M = d2.M - d1.M; in timediffFunc()
25550 Y--; in timediffFunc()
25559 M--; in timediffFunc()
25562 Y--; in timediffFunc()
25572 d1.iJD = d2.iJD - d1.iJD; in timediffFunc()
25580 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f", in timediffFunc()
25581 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s); in timediffFunc()
25603 ** If the library is compiled to omit the full-scale date and time
25609 ** This function uses the C-library functions time(), gmtime()
25611 ** as the user-data for the function.
25630 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
25641 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
25654 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
25655 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), in sqlite3RegisterDateTimeFunctions()
25656 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
25657 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
25658 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
25659 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
25666 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
25667 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
25699 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
25717 ** from within OsOpen()), but some third-party implementations may.
25757 if( pId->pMethods ){ in sqlite3OsClose()
25758 pId->pMethods->xClose(pId); in sqlite3OsClose()
25759 pId->pMethods = 0; in sqlite3OsClose()
25764 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
25768 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
25771 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
25775 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
25779 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
25784 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
25788 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
25792 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
25804 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
25814 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM in sqlite3OsFileControl()
25822 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
25829 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
25832 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
25836 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
25840 if( NEVER(id->pMethods==0) ) return 0; in sqlite3OsDeviceCharacteristics()
25841 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
25845 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
25848 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
25851 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
25861 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
25869 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
25872 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
25875 /* No-op stubs to use when memory-mapped I/O is disabled */
25903 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
25904 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
25910 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; in sqlite3OsDelete()
25919 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
25929 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
25934 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ in sqlite3OsDlOpen()
25935 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
25938 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
25941 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
25944 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
25954 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
25959 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
25962 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
25966 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
25972 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
25973 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
25976 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
25991 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
26049 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
26051 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
26063 /* No-op */ in vfsUnlink()
26065 vfsList = pVfs->pNext; in vfsUnlink()
26068 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
26069 p = p->pNext; in vfsUnlink()
26071 if( p->pNext==pVfs ){ in vfsUnlink()
26072 p->pNext = pVfs->pNext; in vfsUnlink()
26096 pVfs->pNext = vfsList; in sqlite3_vfs_register()
26099 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
26100 vfsList->pNext = pVfs; in sqlite3_vfs_register()
26142 ** Most malloc failures are non-benign. After they occur, SQLite
26166 ** we have to locate the state vector at run-time. In the more common
26196 ** indicates that subsequent malloc failures are non-benign.
26227 ** This file contains a no-op memory allocation drivers for use when
26237 ** used when no other memory allocator is specified using compile-time
26243 ** No-op versions of all memory allocation routines
26256 ** Populate the low-level memory allocation function pointers in
26289 ** This file contains low-level memory allocation drivers for when
26290 ** SQLite will use the standard C-library malloc/realloc/free interface
26293 ** This file contains implementations of the low-level memory allocation
26301 ** C-preprocessor macro summary:
26308 ** a different name, using a separate -D
26324 ** used when no other memory allocator is specified using compile-time
26344 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
26349 ** Use standard C library malloc and free on non-Apple systems.
26366 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
26400 ** For this low-level routine, we are guaranteed that nByte>0 because
26434 ** For this low-level routine, we already know that pPrior!=0 since
26436 ** by higher-level routines.
26444 p--; in sqlite3MemFree()
26461 p--; in sqlite3MemSize()
26470 ** For this low-level interface, we know that pPrior!=0. Cases where
26471 ** pPrior==0 while have been intercepted by higher-level routine and
26473 ** cases where nByte<=0 will have been intercepted by higher-level
26489 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
26490 p--; in sqlite3MemRealloc()
26550 ** Populate the low-level memory allocation function pointers in
26583 ** This file contains low-level memory allocation drivers for when
26584 ** SQLite will use the standard C-library malloc/realloc/free interface
26589 ** This file contains implementations of the low-level memory allocation
26615 ** ------------------------------------------------------------------------
26617 ** ------------------------------------------------------------------------
26701 if( i>NCSIZE-1 ){ in adjustStats()
26702 i = NCSIZE - 1; in adjustStats()
26711 mem.nCurrent[i]--; in adjustStats()
26729 p--; in sqlite3MemsysGetHeader()
26730 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
26731 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
26739 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
26752 return (int)pHdr->iSize; in sqlite3MemSize()
26785 ** Fill a buffer with pseudo-random bytes. This is used to preset
26794 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
26799 nByte -= 4; in randomFill()
26801 while( nByte-- > 0 ){ in randomFill()
26802 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
26830 pHdr->pNext = 0; in sqlite3MemMalloc()
26831 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
26833 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
26838 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
26839 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
26840 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
26841 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
26844 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
26845 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
26848 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
26851 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
26856 pHdr->iSize = nByte; in sqlite3MemMalloc()
26861 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
26879 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
26881 if( pHdr->pPrev ){ in sqlite3MemFree()
26882 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
26883 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
26886 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
26888 if( pHdr->pNext ){ in sqlite3MemFree()
26889 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
26890 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
26893 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
26896 z -= pHdr->nTitle; in sqlite3MemFree()
26897 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
26898 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
26899 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
26917 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
26921 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
26922 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
26923 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
26931 ** Populate the low-level memory allocation function pointers in
26955 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
26956 pHdr->eType = eType; in sqlite3MemdebugSetType()
26974 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
26975 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
26996 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
26997 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
27026 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
27035 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
27037 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
27038 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
27057 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
27059 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
27061 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
27062 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
27065 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
27066 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
27071 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
27077 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
27079 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
27080 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
27132 ** mean that the library will use a memory-pool by default, just that
27173 ** two fields form a double-linked list of chunks of related sizes.
27210 ** True if we are evaluating an out-of-memory callback.
27238 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
27271 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
27273 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
27274 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
27277 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
27306 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
27307 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
27308 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
27311 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
27350 ** size parameters for check-out and return a pointer to the
27357 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
27358 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
27359 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
27360 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
27361 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
27362 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
27374 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
27384 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
27386 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
27387 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
27388 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
27389 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
27390 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
27391 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
27392 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
27422 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
27426 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
27427 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
27432 size = i + size/4 - prev; in memsys3Merge()
27433 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
27434 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
27435 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
27475 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
27477 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
27483 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
27516 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
27543 i = p - mem3.aPool; in memsys3FreeUnsafe()
27544 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
27545 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27547 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
27548 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
27549 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
27554 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
27555 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
27556 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
27559 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27560 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27561 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27563 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
27564 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
27566 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
27567 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
27568 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
27575 ** size returned omits the 8-byte header overhead. This only
27582 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
27583 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
27593 return ((n+11)&~7) - 4; in memsys3Roundup()
27633 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
27662 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
27708 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
27714 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
27719 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
27725 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
27727 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
27731 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
27736 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
27745 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
27750 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
27751 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
27767 ** Populate the low-level memory allocation function pointers in
27828 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
27837 ** N >= M*(1 + log2(n)/2) - n + 1
27866 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
27901 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
27932 ** structures, return a pointer to the idx-th such link.
27946 next = MEM5LINK(i)->next; in memsys5Unlink()
27947 prev = MEM5LINK(i)->prev; in memsys5Unlink()
27951 MEM5LINK(prev)->next = next; in memsys5Unlink()
27954 MEM5LINK(next)->prev = prev; in memsys5Unlink()
27969 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
27970 MEM5LINK(i)->prev = -1; in memsys5Link()
27973 MEM5LINK(x)->prev = i; in memsys5Link()
27995 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
28050 iBin--; in memsys5MallocUnsafe()
28061 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
28088 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
28090 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
28092 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
28097 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
28100 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
28105 mem5.currentCount--; in memsys5FreeUnsafe()
28106 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
28115 iBuddy = iBlock - size; in memsys5FreeUnsafe()
28177 ** memsys5Round(). Hence nBytes is always a non-negative power
28186 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
28209 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
28231 ** Examples: memsys5Log(1) -> 0
28232 ** memsys5Log(2) -> 1
28233 ** memsys5Log(4) -> 2
28234 ** memsys5Log(5) -> 3
28235 ** memsys5Log(8) -> 3
28236 ** memsys5Log(9) -> 4
28240 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
28265 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
28283 mem5.aiFreelist[ii] = -1; in memsys5Init()
28287 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
28337 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
28428 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
28439 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
28449 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
28452 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
28483 p->iType = iType; in checkMutexAlloc()
28486 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
28491 p = &staticMutexes[iType-2]; in checkMutexAlloc()
28494 if( p->mutex==0 ){ in checkMutexAlloc()
28495 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
28496 if( p->mutex==0 ){ in checkMutexAlloc()
28516 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
28520 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
28535 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
28536 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
28540 "illegal multi-threaded access to database connection" in checkMutexEnter()
28543 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
28551 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
28559 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
28589 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
28590 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
28618 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
28619 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
28620 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
28621 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
28622 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
28623 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
28624 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
28625 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
28627 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
28716 ** this function is a no-op.
28760 ** here are place-holders. Applications can substitute working
28761 ** mutex routines at start-time using the
28834 return p==0 || p->cnt>0; in debugMutexHeld()
28838 return p==0 || p->cnt==0; in debugMutexNotheld()
28853 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
28860 pNew->id = id; in debugMutexAlloc()
28861 pNew->cnt = 0; in debugMutexAlloc()
28867 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
28872 pNew = &aStatic[id-2]; in debugMutexAlloc()
28873 pNew->id = id; in debugMutexAlloc()
28885 assert( p->cnt==0 ); in debugMutexFree()
28886 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
28908 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
28909 p->cnt++; in debugMutexEnter()
28913 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
28914 p->cnt++; in debugMutexTry()
28927 p->cnt--; in debugMutexLeave()
28928 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
28950 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
28951 ** is used regardless of the run-time threadsafety setting.
28991 ** home-grown mutexes. Encapsulate these conditions into a single #define.
29034 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29040 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
29043 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
29096 ** cases where it really needs one. If a faster non-recursive mutex
29137 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29143 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
29147 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
29155 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
29157 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
29164 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
29169 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
29174 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
29186 assert( p->nRef==0 ); in pthreadMutexFree()
29188 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
29191 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
29213 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
29218 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
29219 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
29221 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
29228 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
29229 p->nRef++; in pthreadMutexEnter()
29231 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29232 assert( p->nRef==0 ); in pthreadMutexEnter()
29233 p->owner = self; in pthreadMutexEnter()
29234 p->nRef = 1; in pthreadMutexEnter()
29238 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
29240 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
29242 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
29243 p->owner = pthread_self(); in pthreadMutexEnter()
29244 p->nRef++; in pthreadMutexEnter()
29249 if( p->trace ){ in pthreadMutexEnter()
29250 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
29256 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
29261 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
29262 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
29264 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
29271 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
29272 p->nRef++; in pthreadMutexTry()
29274 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29275 assert( p->nRef==0 ); in pthreadMutexTry()
29276 p->owner = self; in pthreadMutexTry()
29277 p->nRef = 1; in pthreadMutexTry()
29284 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
29286 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
29288 p->owner = pthread_self(); in pthreadMutexTry()
29289 p->nRef++; in pthreadMutexTry()
29298 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
29299 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
29314 p->nRef--; in pthreadMutexLeave()
29315 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
29317 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
29320 if( p->nRef==0 ){ in pthreadMutexLeave()
29321 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29324 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
29328 if( p->trace ){ in pthreadMutexLeave()
29329 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
29434 ** Determine if we are dealing with Windows CE - which has a much reduced
29462 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
29517 return p->nRef!=0 && p->owner==GetCurrentThreadId(); in winMutexHeld()
29521 return p->nRef==0 || p->owner!=tid; in winMutexNotheld2()
29566 static int winMutex_isNt = -1; /* <0 means "need to query" */
29644 ** cases where it really needs one. If a faster non-recursive mutex
29670 p->id = iType; in winMutexAlloc()
29673 p->trace = 1; in winMutexAlloc()
29677 InitializeCriticalSectionEx(&p->mutex, 0, 0); in winMutexAlloc()
29679 InitializeCriticalSection(&p->mutex); in winMutexAlloc()
29686 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ in winMutexAlloc()
29691 p = &winMutex_staticMutexes[iType-2]; in winMutexAlloc()
29694 InterlockedCompareExchange(&p->trace, 1, 0); in winMutexAlloc()
29700 assert( p==0 || p->id==iType ); in winMutexAlloc()
29712 assert( p->nRef==0 && p->owner==0 ); in winMutexFree()
29713 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ in winMutexFree()
29714 DeleteCriticalSection(&p->mutex); in winMutexFree()
29740 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexEnter()
29745 EnterCriticalSection(&p->mutex); in winMutexEnter()
29747 assert( p->nRef>0 || p->owner==0 ); in winMutexEnter()
29748 p->owner = tid; in winMutexEnter()
29749 p->nRef++; in winMutexEnter()
29750 if( p->trace ){ in winMutexEnter()
29751 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexEnter()
29752 tid, p->id, p, p->trace, p->nRef)); in winMutexEnter()
29763 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexTry()
29777 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); in winMutexTry()
29782 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ in winMutexTry()
29784 p->owner = tid; in winMutexTry()
29785 p->nRef++; in winMutexTry()
29793 if( p->trace ){ in winMutexTry()
29794 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", in winMutexTry()
29795 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); in winMutexTry()
29813 assert( p->nRef>0 ); in winMutexLeave()
29814 assert( p->owner==tid ); in winMutexLeave()
29815 p->nRef--; in winMutexLeave()
29816 if( p->nRef==0 ) p->owner = 0; in winMutexLeave()
29817 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in winMutexLeave()
29820 LeaveCriticalSection(&p->mutex); in winMutexLeave()
29822 if( p->trace ){ in winMutexLeave()
29823 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexLeave()
29824 tid, p->id, p, p->trace, p->nRef)); in winMutexLeave()
29871 ** Attempt to release up to n bytes of non-essential memory currently
29872 ** held by SQLite. An example of non-essential memory is memory used to
29879 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine in sqlite3_release_memory()
29880 ** is a no-op returning zero if SQLite is not compiled with in sqlite3_release_memory()
29922 ** no-op.
29937 ** Set the soft heap-size limit for the library. An argument of
29938 ** zero disables the limit. A negative argument is a no-op used to
29953 if( rc ) return -1; in sqlite3_soft_heap_limit64()
29968 excess = sqlite3_memory_used() - n; in sqlite3_soft_heap_limit64()
29978 ** Set the hard heap-size limit for the library. An argument of zero
29979 ** disables the hard heap limit. A negative argument is a no-op used
29993 if( rc ) return -1; in sqlite3_hard_heap_limit64()
30028 ** Return true if the heap is currently under memory pressure - in other
30086 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal in mallocWithAlarm()
30096 if( nUsed >= mem0.alarmThreshold - nFull ){ in mallocWithAlarm()
30101 if( nUsed >= mem0.hardLimit - nFull ){ in mallocWithAlarm()
30132 ** This provides a 256-byte safety margin for defense against 32-bit
30161 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ in sqlite3Malloc()
30188 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); in isLookaside()
30204 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; in lookasideMallocSize()
30206 return db->lookaside.szTrue; in lookasideMallocSize()
30221 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ in sqlite3DbMallocSize()
30223 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbMallocSize()
30224 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30228 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbMallocSize()
30229 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
30230 return db->lookaside.szTrue; in sqlite3DbMallocSize()
30246 if( p==0 ) return; /* IMP: R-49053-54554 */ in sqlite3_free()
30262 ** *db->pnBytesFreed.
30265 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); in measureAllocationSize()
30270 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
30271 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
30274 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFreeNN()
30277 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbFreeNN()
30279 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbFreeNN()
30281 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30285 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbFreeNN()
30286 db->lookaside.pSmallFree = pBuf; in sqlite3DbFreeNN()
30290 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbFreeNN()
30292 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
30294 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbFreeNN()
30296 pBuf->pNext = db->lookaside.pFree; in sqlite3DbFreeNN()
30297 db->lookaside.pFree = pBuf; in sqlite3DbFreeNN()
30301 if( db->pnBytesFreed ){ in sqlite3DbFreeNN()
30314 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbNNFreeNN()
30316 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbNNFreeNN()
30318 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbNNFreeNN()
30320 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30324 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbNNFreeNN()
30325 db->lookaside.pSmallFree = pBuf; in sqlite3DbNNFreeNN()
30329 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbNNFreeNN()
30331 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
30333 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbNNFreeNN()
30335 pBuf->pNext = db->lookaside.pFree; in sqlite3DbNNFreeNN()
30336 db->lookaside.pFree = pBuf; in sqlite3DbNNFreeNN()
30340 if( db->pnBytesFreed ){ in sqlite3DbNNFreeNN()
30350 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFree()
30363 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ in sqlite3Realloc()
30366 sqlite3_free(pOld); /* IMP: R-26507-47431 */ in sqlite3Realloc()
30374 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second in sqlite3Realloc()
30384 nDiff = nNew - nOld; in sqlite3Realloc()
30386 mem0.alarmThreshold-nDiff ){ in sqlite3Realloc()
30388 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ in sqlite3Realloc()
30402 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); in sqlite3Realloc()
30408 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ in sqlite3Realloc()
30420 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ in sqlite3_realloc()
30464 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); in dbMallocRawFinish()
30473 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
30501 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30502 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30503 if( n>db->lookaside.sz ){ in sqlite3DbMallocRawNN()
30504 if( !db->lookaside.bDisable ){ in sqlite3DbMallocRawNN()
30505 db->lookaside.anStat[1]++; in sqlite3DbMallocRawNN()
30506 }else if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30513 if( (pBuf = db->lookaside.pSmallFree)!=0 ){ in sqlite3DbMallocRawNN()
30514 db->lookaside.pSmallFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30515 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30517 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ in sqlite3DbMallocRawNN()
30518 db->lookaside.pSmallInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30519 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30524 if( (pBuf = db->lookaside.pFree)!=0 ){ in sqlite3DbMallocRawNN()
30525 db->lookaside.pFree = pBuf->pNext; in sqlite3DbMallocRawNN()
30526 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30528 }else if( (pBuf = db->lookaside.pInit)!=0 ){ in sqlite3DbMallocRawNN()
30529 db->lookaside.pInit = pBuf->pNext; in sqlite3DbMallocRawNN()
30530 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
30533 db->lookaside.anStat[2]++; in sqlite3DbMallocRawNN()
30537 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
30538 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
30539 if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
30556 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbRealloc()
30557 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ in sqlite3DbRealloc()
30559 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ in sqlite3DbRealloc()
30563 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ in sqlite3DbRealloc()
30564 if( n<=db->lookaside.szTrue ) return p; in sqlite3DbRealloc()
30573 if( db->mallocFailed==0 ){ in dbReallocFinish()
30589 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); in dbReallocFinish()
30650 ** at least one non-space character */ in sqlite3DbSpanDup()
30654 n = (int)(zEnd - zStart); in sqlite3DbSpanDup()
30655 while( sqlite3Isspace(zStart[n-1]) ) n--; in sqlite3DbSpanDup()
30669 ** Call this routine to record the fact that an OOM (out-of-memory) error
30670 ** has happened. This routine will set db->mallocFailed, and also
30682 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ in sqlite3OomFault()
30683 db->mallocFailed = 1; in sqlite3OomFault()
30684 if( db->nVdbeExec>0 ){ in sqlite3OomFault()
30685 AtomicStore(&db->u1.isInterrupted, 1); in sqlite3OomFault()
30688 if( db->pParse ){ in sqlite3OomFault()
30690 sqlite3ErrorMsg(db->pParse, "out of memory"); in sqlite3OomFault()
30691 db->pParse->rc = SQLITE_NOMEM_BKPT; in sqlite3OomFault()
30692 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ in sqlite3OomFault()
30693 pParse->nErr++; in sqlite3OomFault()
30694 pParse->rc = SQLITE_NOMEM; in sqlite3OomFault()
30703 ** db->mallocFailed flag as necessary.
30709 if( db->mallocFailed && db->nVdbeExec==0 ){ in sqlite3OomClear()
30710 db->mallocFailed = 0; in sqlite3OomClear()
30711 AtomicStore(&db->u1.isInterrupted, 0); in sqlite3OomClear()
30712 assert( db->lookaside.bDisable>0 ); in sqlite3OomClear()
30721 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ in apiHandleError()
30726 return rc & db->errMask; in apiHandleError()
30738 ** If an OOM as occurred, then the connection error-code (the value
30743 ** Otherwise the read (and possible write) of db->mallocFailed in sqlite3ApiExit()
30747 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3ApiExit()
30748 if( db->mallocFailed || rc ){ in sqlite3ApiExit()
30762 ** This file contains code for a set of "printf"-like routines. These
30773 #define etRADIX 0 /* non-decimal integer types. %x %o */
30789 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
30790 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
30797 ** An "etByte" is an 8-bit unsigned value.
30826 static const char aPrefix[] = "-x0\000X0";
30868 p->accError = eError; in sqlite3StrAccumSetError()
30869 if( p->mxAlloc ) sqlite3_str_reset(p); in sqlite3StrAccumSetError()
30870 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); in sqlite3StrAccumSetError()
30877 if( p->nArg<=p->nUsed ) return 0; in getIntArg()
30878 return sqlite3_value_int64(p->apArg[p->nUsed++]); in getIntArg()
30881 if( p->nArg<=p->nUsed ) return 0.0; in getDoubleArg()
30882 return sqlite3_value_double(p->apArg[p->nUsed++]); in getDoubleArg()
30885 if( p->nArg<=p->nUsed ) return 0; in getTextArg()
30886 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); in getTextArg()
30900 if( pAccum->accError ) return 0; in printfTempBuf()
30901 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ in printfTempBuf()
30905 z = sqlite3DbMallocRaw(pAccum->db, n); in printfTempBuf()
30922 ** Hard limit on the precision of floating-point conversions.
30942 etByte flag_leftjustify; /* True if "-" flag is present */ in sqlite3_str_vappendf()
30952 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ in sqlite3_str_vappendf()
30969 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
30972 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ in sqlite3_str_vappendf()
30986 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); in sqlite3_str_vappendf()
30999 precision = -1; in sqlite3_str_vappendf()
31002 case '-': flag_leftjustify = 1; break; in sqlite3_str_vappendf()
31022 unsigned wx = c - '0'; in sqlite3_str_vappendf()
31024 wx = wx*10 + c - '0'; in sqlite3_str_vappendf()
31036 fmt--; in sqlite3_str_vappendf()
31048 width = width >= -2147483647 ? -width : 0; in sqlite3_str_vappendf()
31070 precision = precision >= -2147483647 ? -precision : -1; in sqlite3_str_vappendf()
31076 px = px*10 + c - '0'; in sqlite3_str_vappendf()
31088 --fmt; in sqlite3_str_vappendf()
31103 xtype = infop->type; in sqlite3_str_vappendf()
31114 ** flag_leftjustify TRUE if a '-' is present or if the in sqlite3_str_vappendf()
31119 ** always non-negative. Zero is the default. in sqlite3_str_vappendf()
31121 ** is -1. in sqlite3_str_vappendf()
31126 assert( precision>=(-1) ); in sqlite3_str_vappendf()
31137 if( infop->flags & FLAG_SIGNED ){ in sqlite3_str_vappendf()
31152 testcase( v==(-1) ); in sqlite3_str_vappendf()
31155 prefix = '-'; in sqlite3_str_vappendf()
31175 if( flag_zeropad && precision<width-(prefix!=0) ){ in sqlite3_str_vappendf()
31176 precision = width-(prefix!=0); in sqlite3_str_vappendf()
31178 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ in sqlite3_str_vappendf()
31189 bufpt = &zOut[nOut-1]; in sqlite3_str_vappendf()
31196 *(--bufpt) = zOrd[x*2+1]; in sqlite3_str_vappendf()
31197 *(--bufpt) = zOrd[x*2]; in sqlite3_str_vappendf()
31200 const char *cset = &aDigits[infop->charset]; in sqlite3_str_vappendf()
31201 u8 base = infop->base; in sqlite3_str_vappendf()
31203 *(--bufpt) = cset[longvalue%base]; in sqlite3_str_vappendf()
31207 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31209 *(--bufpt) = '0'; /* Zero pad */ in sqlite3_str_vappendf()
31213 int nn = (length - 1)/3; /* Number of "," to insert */ in sqlite3_str_vappendf()
31214 int ix = (length - 1)%3 + 1; in sqlite3_str_vappendf()
31215 bufpt -= nn; in sqlite3_str_vappendf()
31218 ix--; in sqlite3_str_vappendf()
31221 nn--; in sqlite3_str_vappendf()
31226 if( prefix ) *(--bufpt) = prefix; /* Add sign */ in sqlite3_str_vappendf()
31227 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ in sqlite3_str_vappendf()
31230 pre = &aPrefix[infop->prefix]; in sqlite3_str_vappendf()
31231 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; in sqlite3_str_vappendf()
31233 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
31254 iRound = -precision; in sqlite3_str_vappendf()
31271 memcpy(buf, "-Inf", 5); in sqlite3_str_vappendf()
31273 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31274 /* no-op */ in sqlite3_str_vappendf()
31284 if( s.sign=='-' ){ in sqlite3_str_vappendf()
31285 prefix = '-'; in sqlite3_str_vappendf()
31290 exp = s.iDP-1; in sqlite3_str_vappendf()
31291 if( xtype==etGENERIC && precision>0 ) precision--; in sqlite3_str_vappendf()
31299 if( exp<-4 || exp>precision ){ in sqlite3_str_vappendf()
31302 precision = precision - exp; in sqlite3_str_vappendf()
31311 e2 = s.iDP - 1; in sqlite3_str_vappendf()
31334 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
31345 for(e2++; e2<0 && precision>0; precision--, e2++){ in sqlite3_str_vappendf()
31349 while( (precision--)>0 ){ in sqlite3_str_vappendf()
31354 while( bufpt[-1]=='0' ) *(--bufpt) = 0; in sqlite3_str_vappendf()
31356 if( bufpt[-1]=='.' ){ in sqlite3_str_vappendf()
31360 *(--bufpt) = 0; in sqlite3_str_vappendf()
31366 exp = s.iDP - 1; in sqlite3_str_vappendf()
31367 *(bufpt++) = aDigits[infop->charset]; in sqlite3_str_vappendf()
31369 *(bufpt++) = '-'; exp = -exp; in sqlite3_str_vappendf()
31385 length = (int)(bufpt-zOut); in sqlite3_str_vappendf()
31392 int nPad = width - length; in sqlite3_str_vappendf()
31393 for(i=width; i>=nPad; i--){ in sqlite3_str_vappendf()
31394 bufpt[i] = bufpt[i-nPad]; in sqlite3_str_vappendf()
31397 while( nPad-- ) bufpt[i++] = '0'; in sqlite3_str_vappendf()
31404 *(va_arg(ap,int*)) = pAccum->nChar; in sqlite3_str_vappendf()
31451 width -= precision-1; in sqlite3_str_vappendf()
31453 sqlite3_str_appendchar(pAccum, width-1, ' '); in sqlite3_str_vappendf()
31457 precision--; in sqlite3_str_vappendf()
31460 if( nPrior > precision-1 ) nPrior = precision - 1; in sqlite3_str_vappendf()
31462 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ in sqlite3_str_vappendf()
31465 if( pAccum->accError ) break; in sqlite3_str_vappendf()
31467 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); in sqlite3_str_vappendf()
31468 precision -= nPrior; in sqlite3_str_vappendf()
31486 if( pAccum->nChar==0 in sqlite3_str_vappendf()
31487 && pAccum->mxAlloc in sqlite3_str_vappendf()
31490 && pAccum->accError==0 in sqlite3_str_vappendf()
31495 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31496 pAccum->zText = bufpt; in sqlite3_str_vappendf()
31497 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); in sqlite3_str_vappendf()
31498 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
31499 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3_str_vappendf()
31510 while( precision-- > 0 && z[0] ){ in sqlite3_str_vappendf()
31513 length = (int)(z - (unsigned char*)bufpt); in sqlite3_str_vappendf()
31522 /* Adjust width to account for extra bytes in UTF-8 characters */ in sqlite3_str_vappendf()
31523 int ii = length - 1; in sqlite3_str_vappendf()
31524 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; in sqlite3_str_vappendf()
31549 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ in sqlite3_str_vappendf()
31576 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31581 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); in sqlite3_str_vappendf()
31582 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); in sqlite3_str_vappendf()
31588 if( pToken && pToken->n ){ in sqlite3_str_vappendf()
31589 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); in sqlite3_str_vappendf()
31590 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); in sqlite3_str_vappendf()
31598 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31601 if( pItem->zAlias && !flag_altform2 ){ in sqlite3_str_vappendf()
31602 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31603 }else if( pItem->zName ){ in sqlite3_str_vappendf()
31604 if( pItem->zDatabase ){ in sqlite3_str_vappendf()
31605 sqlite3_str_appendall(pAccum, pItem->zDatabase); in sqlite3_str_vappendf()
31608 sqlite3_str_appendall(pAccum, pItem->zName); in sqlite3_str_vappendf()
31609 }else if( pItem->zAlias ){ in sqlite3_str_vappendf()
31610 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31612 Select *pSel = pItem->pSelect; in sqlite3_str_vappendf()
31614 if( pSel->selFlags & SF_NestedFrom ){ in sqlite3_str_vappendf()
31615 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); in sqlite3_str_vappendf()
31617 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); in sqlite3_str_vappendf()
31636 width -= length; in sqlite3_str_vappendf()
31646 sqlite3DbFree(pAccum->db, zExtra); in sqlite3_str_vappendf()
31665 if( db->errByteOffset!=(-2) ) return; in sqlite3RecordErrorByteOffset()
31666 pParse = db->pParse; in sqlite3RecordErrorByteOffset()
31668 zText =pParse->zTail; in sqlite3RecordErrorByteOffset()
31672 db->errByteOffset = (int)(z-zText); in sqlite3RecordErrorByteOffset()
31682 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) in sqlite3RecordErrorOffsetOfExpr()
31684 pExpr = pExpr->pLeft; in sqlite3RecordErrorOffsetOfExpr()
31687 db->errByteOffset = pExpr->w.iOfst; in sqlite3RecordErrorOffsetOfExpr()
31699 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ in sqlite3StrAccumEnlarge()
31700 if( p->accError ){ in sqlite3StrAccumEnlarge()
31701 testcase(p->accError==SQLITE_TOOBIG); in sqlite3StrAccumEnlarge()
31702 testcase(p->accError==SQLITE_NOMEM); in sqlite3StrAccumEnlarge()
31705 if( p->mxAlloc==0 ){ in sqlite3StrAccumEnlarge()
31707 return p->nAlloc - p->nChar - 1; in sqlite3StrAccumEnlarge()
31709 char *zOld = isMalloced(p) ? p->zText : 0; in sqlite3StrAccumEnlarge()
31710 i64 szNew = p->nChar + N + 1; in sqlite3StrAccumEnlarge()
31711 if( szNew+p->nChar<=p->mxAlloc ){ in sqlite3StrAccumEnlarge()
31714 szNew += p->nChar; in sqlite3StrAccumEnlarge()
31716 if( szNew > p->mxAlloc ){ in sqlite3StrAccumEnlarge()
31721 p->nAlloc = (int)szNew; in sqlite3StrAccumEnlarge()
31723 if( p->db ){ in sqlite3StrAccumEnlarge()
31724 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
31726 zNew = sqlite3Realloc(zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
31729 assert( p->zText!=0 || p->nChar==0 ); in sqlite3StrAccumEnlarge()
31730 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); in sqlite3StrAccumEnlarge()
31731 p->zText = zNew; in sqlite3StrAccumEnlarge()
31732 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); in sqlite3StrAccumEnlarge()
31733 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3StrAccumEnlarge()
31748 testcase( p->nChar + (i64)N > 0x7fffffff ); in sqlite3_str_appendchar()
31749 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ in sqlite3_str_appendchar()
31752 while( (N--)>0 ) p->zText[p->nChar++] = c; in sqlite3_str_appendchar()
31759 ** This is a helper routine to sqlite3_str_append() that does special-case
31761 ** sqlite3_str_append() routine can use fast calling semantics.
31766 memcpy(&p->zText[p->nChar], z, N); in enlargeAndAppend()
31767 p->nChar += N; in enlargeAndAppend()
31777 assert( p->zText!=0 || p->nChar==0 || p->accError ); in sqlite3_str_append()
31779 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); in sqlite3_str_append()
31780 if( p->nChar+N >= p->nAlloc ){ in sqlite3_str_append()
31783 assert( p->zText ); in sqlite3_str_append()
31784 p->nChar += N; in sqlite3_str_append()
31785 memcpy(&p->zText[p->nChar-N], z, N); in sqlite3_str_append()
31790 ** Append the complete text of zero-terminated string z[] to the p string.
31798 ** Finish off a string by making sure it is zero-terminated.
31804 assert( p->mxAlloc>0 && !isMalloced(p) ); in strAccumFinishRealloc()
31805 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); in strAccumFinishRealloc()
31807 memcpy(zText, p->zText, p->nChar+1); in strAccumFinishRealloc()
31808 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in strAccumFinishRealloc()
31812 p->zText = zText; in strAccumFinishRealloc()
31816 if( p->zText ){ in sqlite3StrAccumFinish()
31817 p->zText[p->nChar] = 0; in sqlite3StrAccumFinish()
31818 if( p->mxAlloc>0 && !isMalloced(p) ){ in sqlite3StrAccumFinish()
31822 return p->zText; in sqlite3StrAccumFinish()
31830 if( p->accError ){ in sqlite3ResultStrAccum()
31831 sqlite3_result_error_code(pCtx, p->accError); in sqlite3ResultStrAccum()
31834 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); in sqlite3ResultStrAccum()
31866 return p ? p->accError : SQLITE_NOMEM; in sqlite3_str_errcode()
31871 return p ? p->nChar : 0; in sqlite3_str_length()
31876 if( p==0 || p->nChar==0 ) return 0; in sqlite3_str_value()
31877 p->zText[p->nChar] = 0; in sqlite3_str_value()
31878 return p->zText; in sqlite3_str_value()
31886 sqlite3DbFree(p->db, p->zText); in sqlite3_str_reset()
31887 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; in sqlite3_str_reset()
31889 p->nAlloc = 0; in sqlite3_str_reset()
31890 p->nChar = 0; in sqlite3_str_reset()
31891 p->zText = 0; in sqlite3_str_reset()
31899 ** memory is used if not NULL. db->mallocFailed is set appropriately
31909 p->zText = zBase; in sqlite3StrAccumInit()
31910 p->db = db; in sqlite3StrAccumInit()
31911 p->nAlloc = n; in sqlite3StrAccumInit()
31912 p->mxAlloc = mx; in sqlite3StrAccumInit()
31913 p->nChar = 0; in sqlite3StrAccumInit()
31914 p->accError = 0; in sqlite3StrAccumInit()
31915 p->printfFlags = 0; in sqlite3StrAccumInit()
31923 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); in sqlite3_str_new()
31932 ** %-conversion extensions.
31940 db->aLimit[SQLITE_LIMIT_LENGTH]); in sqlite3VMPrintf()
31952 ** %-conversion extensions.
31965 ** %-conversion extensions.
31989 ** %-conversion extensions.
32053 ** stack space on small-stack systems when logging is disabled.
32115 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
32138 p--; in sqlite3RCStrRef()
32139 p->nRCRef++; in sqlite3RCStrRef()
32150 p--; in sqlite3RCStrUnref()
32151 assert( p->nRCRef>0 ); in sqlite3RCStrUnref()
32152 if( p->nRCRef>=2 ){ in sqlite3RCStrUnref()
32153 p->nRCRef--; in sqlite3RCStrUnref()
32171 p->nRCRef = 1; in sqlite3RCStrNew()
32183 p--; in sqlite3RCStrResize()
32184 assert( p->nRCRef==1 ); in sqlite3RCStrResize()
32197 ** 2015-06-08
32229 p->iLevel++; in sqlite3TreeViewPush()
32232 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; in sqlite3TreeViewPush()
32241 p->iLevel--; in sqlite3TreeViewPop()
32242 if( p->iLevel<0 ){ in sqlite3TreeViewPop()
32259 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ in sqlite3TreeViewLine()
32260 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); in sqlite3TreeViewLine()
32262 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); in sqlite3TreeViewLine()
32298 int colMoreToFollow = i<(nCol - 1); in sqlite3TreeViewColumnList()
32313 printf(" X-%s", z); in sqlite3TreeViewColumnList()
32321 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); in sqlite3TreeViewColumnList()
32332 ** Generate a human-readable description of a WITH clause.
32337 if( pWith->nCte==0 ) return; in sqlite3TreeViewWith()
32338 if( pWith->pOuter ){ in sqlite3TreeViewWith()
32339 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); in sqlite3TreeViewWith()
32343 if( pWith->nCte>0 ){ in sqlite3TreeViewWith()
32345 for(i=0; i<pWith->nCte; i++){ in sqlite3TreeViewWith()
32348 const struct Cte *pCte = &pWith->a[i]; in sqlite3TreeViewWith()
32350 sqlite3_str_appendf(&x, "%s", pCte->zName); in sqlite3TreeViewWith()
32351 if( pCte->pCols && pCte->pCols->nExpr>0 ){ in sqlite3TreeViewWith()
32354 for(j=0; j<pCte->pCols->nExpr; j++){ in sqlite3TreeViewWith()
32355 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); in sqlite3TreeViewWith()
32360 if( pCte->eM10d!=M10d_Any ){ in sqlite3TreeViewWith()
32362 pCte->eM10d==M10d_No ? "NOT " : ""); in sqlite3TreeViewWith()
32364 if( pCte->pUse ){ in sqlite3TreeViewWith()
32365 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, in sqlite3TreeViewWith()
32366 pCte->pUse->nUse); in sqlite3TreeViewWith()
32369 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); in sqlite3TreeViewWith()
32370 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); in sqlite3TreeViewWith()
32378 ** Generate a human-readable description of a SrcList object.
32383 for(i=0; i<pSrc->nSrc; i++){ in sqlite3TreeViewSrcList()
32384 const SrcItem *pItem = &pSrc->a[i]; in sqlite3TreeViewSrcList()
32390 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); in sqlite3TreeViewSrcList()
32391 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32393 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed); in sqlite3TreeViewSrcList()
32395 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ in sqlite3TreeViewSrcList()
32396 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); in sqlite3TreeViewSrcList()
32397 }else if( pItem->fg.jointype & JT_LEFT ){ in sqlite3TreeViewSrcList()
32398 sqlite3_str_appendf(&x, " LEFT-JOIN"); in sqlite3TreeViewSrcList()
32399 }else if( pItem->fg.jointype & JT_RIGHT ){ in sqlite3TreeViewSrcList()
32400 sqlite3_str_appendf(&x, " RIGHT-JOIN"); in sqlite3TreeViewSrcList()
32401 }else if( pItem->fg.jointype & JT_CROSS ){ in sqlite3TreeViewSrcList()
32402 sqlite3_str_appendf(&x, " CROSS-JOIN"); in sqlite3TreeViewSrcList()
32404 if( pItem->fg.jointype & JT_LTORJ ){ in sqlite3TreeViewSrcList()
32407 if( pItem->fg.fromDDL ){ in sqlite3TreeViewSrcList()
32410 if( pItem->fg.isCte ){ in sqlite3TreeViewSrcList()
32411 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); in sqlite3TreeViewSrcList()
32413 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ in sqlite3TreeViewSrcList()
32416 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); in sqlite3TreeViewSrcList()
32417 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); in sqlite3TreeViewSrcList()
32418 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); in sqlite3TreeViewSrcList()
32419 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); in sqlite3TreeViewSrcList()
32420 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); in sqlite3TreeViewSrcList()
32421 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); in sqlite3TreeViewSrcList()
32424 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); in sqlite3TreeViewSrcList()
32426 if( pItem->pSelect ) n++; in sqlite3TreeViewSrcList()
32427 if( pItem->fg.isTabFunc ) n++; in sqlite3TreeViewSrcList()
32428 if( pItem->fg.isUsing ) n++; in sqlite3TreeViewSrcList()
32429 if( pItem->fg.isUsing ){ in sqlite3TreeViewSrcList()
32430 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); in sqlite3TreeViewSrcList()
32432 if( pItem->pSelect ){ in sqlite3TreeViewSrcList()
32433 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
32434 Table *pTab = pItem->pTab; in sqlite3TreeViewSrcList()
32435 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); in sqlite3TreeViewSrcList()
32437 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) ); in sqlite3TreeViewSrcList()
32438 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0); in sqlite3TreeViewSrcList()
32440 if( pItem->fg.isTabFunc ){ in sqlite3TreeViewSrcList()
32441 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); in sqlite3TreeViewSrcList()
32448 ** Generate a human-readable description of a Select object.
32454 sqlite3TreeViewLine(pView, "nil-SELECT"); in sqlite3TreeViewSelect()
32458 if( p->pWith ){ in sqlite3TreeViewSelect()
32459 sqlite3TreeViewWith(pView, p->pWith, 1); in sqlite3TreeViewSelect()
32464 if( p->selFlags & SF_WhereBegin ){ in sqlite3TreeViewSelect()
32469 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), in sqlite3TreeViewSelect()
32470 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), in sqlite3TreeViewSelect()
32471 p->selId, p, p->selFlags, in sqlite3TreeViewSelect()
32472 (int)p->nSelectRow in sqlite3TreeViewSelect()
32476 if( p->pPrior ){ in sqlite3TreeViewSelect()
32480 if( p->pSrc && p->pSrc->nSrc ) n++; in sqlite3TreeViewSelect()
32481 if( p->pWhere ) n++; in sqlite3TreeViewSelect()
32482 if( p->pGroupBy ) n++; in sqlite3TreeViewSelect()
32483 if( p->pHaving ) n++; in sqlite3TreeViewSelect()
32484 if( p->pOrderBy ) n++; in sqlite3TreeViewSelect()
32485 if( p->pLimit ) n++; in sqlite3TreeViewSelect()
32487 if( p->pWin ) n++; in sqlite3TreeViewSelect()
32488 if( p->pWinDefn ) n++; in sqlite3TreeViewSelect()
32491 if( p->pEList ){ in sqlite3TreeViewSelect()
32492 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); in sqlite3TreeViewSelect()
32494 n--; in sqlite3TreeViewSelect()
32496 if( p->pWin ){ in sqlite3TreeViewSelect()
32498 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32499 sqlite3TreeViewLine(pView, "window-functions"); in sqlite3TreeViewSelect()
32500 for(pX=p->pWin; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32501 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32506 if( p->pSrc && p->pSrc->nSrc ){ in sqlite3TreeViewSelect()
32507 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
32509 sqlite3TreeViewSrcList(pView, p->pSrc); in sqlite3TreeViewSelect()
32512 if( p->pWhere ){ in sqlite3TreeViewSelect()
32513 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewSelect()
32514 sqlite3TreeViewExpr(pView, p->pWhere, 0); in sqlite3TreeViewSelect()
32517 if( p->pGroupBy ){ in sqlite3TreeViewSelect()
32518 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); in sqlite3TreeViewSelect()
32520 if( p->pHaving ){ in sqlite3TreeViewSelect()
32521 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); in sqlite3TreeViewSelect()
32522 sqlite3TreeViewExpr(pView, p->pHaving, 0); in sqlite3TreeViewSelect()
32526 if( p->pWinDefn ){ in sqlite3TreeViewSelect()
32528 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); in sqlite3TreeViewSelect()
32529 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
32530 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
32535 if( p->pOrderBy ){ in sqlite3TreeViewSelect()
32536 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); in sqlite3TreeViewSelect()
32538 if( p->pLimit ){ in sqlite3TreeViewSelect()
32539 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); in sqlite3TreeViewSelect()
32540 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); in sqlite3TreeViewSelect()
32541 if( p->pLimit->pRight ){ in sqlite3TreeViewSelect()
32542 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); in sqlite3TreeViewSelect()
32543 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); in sqlite3TreeViewSelect()
32548 if( p->pPrior ){ in sqlite3TreeViewSelect()
32550 switch( p->op ){ in sqlite3TreeViewSelect()
32557 p = p->pPrior; in sqlite3TreeViewSelect()
32601 ** Generate a human-readable explanation for a Window object
32606 if( pWin->pFilter ){ in sqlite3TreeViewWindow()
32608 sqlite3TreeViewExpr(pView, pWin->pFilter, 0); in sqlite3TreeViewWindow()
32610 if( pWin->eFrmType==TK_FILTER ) return; in sqlite3TreeViewWindow()
32613 if( pWin->zName ){ in sqlite3TreeViewWindow()
32614 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); in sqlite3TreeViewWindow()
32618 if( pWin->zBase ) nElement++; in sqlite3TreeViewWindow()
32619 if( pWin->pOrderBy ) nElement++; in sqlite3TreeViewWindow()
32620 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++; in sqlite3TreeViewWindow()
32621 if( pWin->eExclude ) nElement++; in sqlite3TreeViewWindow()
32622 if( pWin->zBase ){ in sqlite3TreeViewWindow()
32623 sqlite3TreeViewPush(&pView, (--nElement)>0); in sqlite3TreeViewWindow()
32624 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); in sqlite3TreeViewWindow()
32627 if( pWin->pPartition ){ in sqlite3TreeViewWindow()
32628 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); in sqlite3TreeViewWindow()
32630 if( pWin->pOrderBy ){ in sqlite3TreeViewWindow()
32631 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); in sqlite3TreeViewWindow()
32633 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){ in sqlite3TreeViewWindow()
32636 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; in sqlite3TreeViewWindow()
32637 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; in sqlite3TreeViewWindow()
32639 pWin->bImplicitFrame ? " (implied)" : ""); in sqlite3TreeViewWindow()
32640 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); in sqlite3TreeViewWindow()
32641 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); in sqlite3TreeViewWindow()
32642 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); in sqlite3TreeViewWindow()
32645 if( pWin->eExclude ){ in sqlite3TreeViewWindow()
32648 switch( pWin->eExclude ){ in sqlite3TreeViewWindow()
32654 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); in sqlite3TreeViewWindow()
32668 ** Generate a human-readable explanation for a Window Function object
32674 pWin->pWFunc->zName, pWin->pWFunc->nArg); in sqlite3TreeViewWinFunc()
32681 ** Generate a human-readable explanation of an expression tree.
32693 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ in sqlite3TreeViewExpr()
32697 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); in sqlite3TreeViewExpr()
32699 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
32702 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
32710 if( pExpr->pAggInfo!=0 ){ in sqlite3TreeViewExpr()
32711 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); in sqlite3TreeViewExpr()
32717 switch( pExpr->op ){ in sqlite3TreeViewExpr()
32720 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
32724 if( pExpr->iTable<0 ){ in sqlite3TreeViewExpr()
32727 if( pExpr->op2 ){ in sqlite3TreeViewExpr()
32728 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
32733 pExpr->iColumn, zFlgs, zOp2); in sqlite3TreeViewExpr()
32737 pExpr->iTable, pExpr->iColumn, in sqlite3TreeViewExpr()
32738 pExpr->y.pTab, zFlgs); in sqlite3TreeViewExpr()
32741 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32746 if( pExpr->flags & EP_IntValue ){ in sqlite3TreeViewExpr()
32747 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); in sqlite3TreeViewExpr()
32749 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32756 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32762 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32777 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32784 pExpr->u.zToken, pExpr->iColumn); in sqlite3TreeViewExpr()
32788 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); in sqlite3TreeViewExpr()
32793 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); in sqlite3TreeViewExpr()
32800 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32801 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32838 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" in sqlite3TreeViewExpr()
32840 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); in sqlite3TreeViewExpr()
32841 assert( pExpr->pRight ); in sqlite3TreeViewExpr()
32842 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op in sqlite3TreeViewExpr()
32844 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); in sqlite3TreeViewExpr()
32851 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32852 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32859 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE in sqlite3TreeViewExpr()
32864 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", in sqlite3TreeViewExpr()
32865 pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
32866 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32879 pFarg = pExpr->x.pList; in sqlite3TreeViewExpr()
32881 pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0; in sqlite3TreeViewExpr()
32887 if( pExpr->op==TK_AGG_FUNCTION ){ in sqlite3TreeViewExpr()
32889 pExpr->op2, pExpr->u.zToken, zFlgs, in sqlite3TreeViewExpr()
32890 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, in sqlite3TreeViewExpr()
32891 pExpr->iAgg, pExpr->pAggInfo); in sqlite3TreeViewExpr()
32892 }else if( pExpr->op2!=0 ){ in sqlite3TreeViewExpr()
32895 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
32897 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; in sqlite3TreeViewExpr()
32898 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; in sqlite3TreeViewExpr()
32899 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; in sqlite3TreeViewExpr()
32900 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; in sqlite3TreeViewExpr()
32902 pExpr->u.zToken, zFlgs, zOp2); in sqlite3TreeViewExpr()
32904 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
32907 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32908 if( pExpr->pLeft ){ in sqlite3TreeViewExpr()
32909 Expr *pOB = pExpr->pLeft; in sqlite3TreeViewExpr()
32910 assert( pOB->op==TK_ORDER ); in sqlite3TreeViewExpr()
32912 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY"); in sqlite3TreeViewExpr()
32923 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY"); in sqlite3TreeViewExpr()
32929 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32930 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32935 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32936 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32942 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32943 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); in sqlite3TreeViewExpr()
32946 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); in sqlite3TreeViewExpr()
32951 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
32953 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32955 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
32968 ** X is stored in pExpr->pLeft. in sqlite3TreeViewExpr()
32969 ** Y is stored in pExpr->pList->a[0].pExpr. in sqlite3TreeViewExpr()
32970 ** Z is stored in pExpr->pList->a[1].pExpr. in sqlite3TreeViewExpr()
32974 pX = pExpr->pLeft; in sqlite3TreeViewExpr()
32976 assert( pExpr->x.pList->nExpr==2 ); in sqlite3TreeViewExpr()
32977 pY = pExpr->x.pList->a[0].pExpr; in sqlite3TreeViewExpr()
32978 pZ = pExpr->x.pList->a[1].pExpr; in sqlite3TreeViewExpr()
32987 ** to a column in the new.* or old.* pseudo-tables available to in sqlite3TreeViewExpr()
32989 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn in sqlite3TreeViewExpr()
32990 ** is set to the column of the pseudo-table to read, or to -1 to in sqlite3TreeViewExpr()
32994 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); in sqlite3TreeViewExpr()
32999 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33001 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33007 switch( pExpr->affExpr ){ in sqlite3TreeViewExpr()
33014 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); in sqlite3TreeViewExpr()
33020 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33021 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33027 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); in sqlite3TreeViewExpr()
33032 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", in sqlite3TreeViewExpr()
33033 pExpr->iColumn, pExpr->iTable-1, in sqlite3TreeViewExpr()
33034 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); in sqlite3TreeViewExpr()
33035 assert( ExprUseXSelect(pExpr->pLeft) ); in sqlite3TreeViewExpr()
33036 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); in sqlite3TreeViewExpr()
33040 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); in sqlite3TreeViewExpr()
33041 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33048 tmp.op = pExpr->op2; in sqlite3TreeViewExpr()
33053 if( pExpr->iColumn<=0 ){ in sqlite3TreeViewExpr()
33057 pExpr->iColumn-1); in sqlite3TreeViewExpr()
33062 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); in sqlite3TreeViewExpr()
33068 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33069 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33072 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33079 ** Generate a human-readable explanation of an expression list.
33092 for(i=0; i<pList->nExpr; i++){ in sqlite3TreeViewBareExprList()
33093 int j = pList->a[i].u.x.iOrderByCol; in sqlite3TreeViewBareExprList()
33094 char *zName = pList->a[i].zEName; in sqlite3TreeViewBareExprList()
33095 int moreToFollow = i<pList->nExpr - 1; in sqlite3TreeViewBareExprList()
33101 switch( pList->a[i].fg.eEName ){ in sqlite3TreeViewBareExprList()
33106 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); in sqlite3TreeViewBareExprList()
33107 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); in sqlite3TreeViewBareExprList()
33108 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); in sqlite3TreeViewBareExprList()
33109 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); in sqlite3TreeViewBareExprList()
33122 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); in sqlite3TreeViewBareExprList()
33141 ** Generate a human-readable explanation of an id-list.
33154 for(i=0; i<pList->nId; i++){ in sqlite3TreeViewBareIdList()
33155 char *zName = pList->a[i].zName; in sqlite3TreeViewBareIdList()
33156 int moreToFollow = i<pList->nId - 1; in sqlite3TreeViewBareIdList()
33160 if( pList->eU4==EU4_NONE ){ in sqlite3TreeViewBareIdList()
33162 }else if( pList->eU4==EU4_IDX ){ in sqlite3TreeViewBareIdList()
33163 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx); in sqlite3TreeViewBareIdList()
33165 assert( pList->eU4==EU4_EXPR ); in sqlite3TreeViewBareIdList()
33166 if( pList->a[i].u4.pExpr==0 ){ in sqlite3TreeViewBareIdList()
33170 sqlite3TreeViewPush(&pView, i<pList->nId-1); in sqlite3TreeViewBareIdList()
33171 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0); in sqlite3TreeViewBareIdList()
33191 ** Generate a human-readable explanation of a list of Upsert objects
33202 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); in sqlite3TreeViewUpsert()
33204 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); in sqlite3TreeViewUpsert()
33205 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); in sqlite3TreeViewUpsert()
33206 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); in sqlite3TreeViewUpsert()
33207 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); in sqlite3TreeViewUpsert()
33208 if( pUpsert->pUpsertWhere ){ in sqlite3TreeViewUpsert()
33209 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewUpsert()
33210 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); in sqlite3TreeViewUpsert()
33214 pUpsert = pUpsert->pNextUpsert; in sqlite3TreeViewUpsert()
33221 ** Generate a human-readable diagram of the data structure that go
33243 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33248 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33254 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33260 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewDelete()
33263 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
33269 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewDelete()
33277 ** Generate a human-readable diagram of the data structure that go
33310 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33315 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33321 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); in sqlite3TreeViewInsert()
33324 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33325 sqlite3TreeViewLine(pView, "DATA-SOURCE"); in sqlite3TreeViewInsert()
33330 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); in sqlite3TreeViewInsert()
33333 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
33339 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewInsert()
33347 ** Generate a human-readable diagram of the data structure that go
33382 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33387 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33393 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); in sqlite3TreeViewUpdate()
33396 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33402 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewUpdate()
33405 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33411 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
33417 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewUpdate()
33425 ** Show a human-readable graph of a TriggerStep
33436 moreToFollow || (showFullList && pStep->pNext!=0)); in sqlite3TreeViewTriggerStep()
33438 if( cnt++ && pStep->pNext==0 ){ in sqlite3TreeViewTriggerStep()
33442 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); in sqlite3TreeViewTriggerStep()
33443 }while( showFullList && (pStep = pStep->pNext)!=0 ); in sqlite3TreeViewTriggerStep()
33448 ** Show a human-readable graph of a Trigger
33459 moreToFollow || (showFullList && pTrigger->pNext!=0)); in sqlite3TreeViewTrigger()
33461 if( cnt++ && pTrigger->pNext==0 ){ in sqlite3TreeViewTrigger()
33465 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); in sqlite3TreeViewTrigger()
33467 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); in sqlite3TreeViewTrigger()
33469 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); in sqlite3TreeViewTrigger()
33476 ** These simplified versions of the tree-view routines omit unnecessary
33522 ** This file contains code to implement a pseudo-random number
33541 /* The RFC-7539 ChaCha20 block function
33543 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
33572 /* The "wsdPrng" macro will resolve to the pseudo-random number generator in sqlite3_randomness()
33574 ** we have to locate the state vector at run-time. In the more common in sqlite3_randomness()
33626 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); in sqlite3_randomness()
33627 wsdPrng.n -= N; in sqlite3_randomness()
33632 N -= wsdPrng.n; in sqlite3_randomness()
33683 ** This file presents a simple cross-platform threading interface for
33696 ** single-threaded if desired.
33708 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33731 /* This routine is never used in single-threaded mode */ in sqlite3ThreadCreate()
33738 p->xTask = xTask; in sqlite3ThreadCreate()
33739 p->pIn = pIn; in sqlite3ThreadCreate()
33747 rc = pthread_create(&p->tid, 0, xTask, pIn); in sqlite3ThreadCreate()
33750 p->done = 1; in sqlite3ThreadCreate()
33751 p->pOut = xTask(pIn); in sqlite3ThreadCreate()
33763 if( p->done ){ in sqlite3ThreadJoin()
33764 *ppOut = p->pOut; in sqlite3ThreadJoin()
33767 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; in sqlite3ThreadJoin()
33780 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33806 assert( p->id==GetCurrentThreadId() ); in sqlite3ThreadProc()
33808 assert( p->xTask!=0 ); in sqlite3ThreadProc()
33809 p->pResult = p->xTask(p->pIn); in sqlite3ThreadProc()
33836 p->xTask = xTask; in sqlite3ThreadCreate()
33837 p->pIn = pIn; in sqlite3ThreadCreate()
33838 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); in sqlite3ThreadCreate()
33839 if( p->tid==0 ){ in sqlite3ThreadCreate()
33843 if( p->xTask==0 ){ in sqlite3ThreadCreate()
33844 p->id = GetCurrentThreadId(); in sqlite3ThreadCreate()
33845 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
33860 if( p->xTask==0 ){ in sqlite3ThreadJoin()
33861 /* assert( p->id==GetCurrentThreadId() ); */ in sqlite3ThreadJoin()
33863 assert( p->tid==0 ); in sqlite3ThreadJoin()
33865 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); in sqlite3ThreadJoin()
33866 rc = sqlite3Win32Wait((HANDLE)p->tid); in sqlite3ThreadJoin()
33868 bRc = CloseHandle((HANDLE)p->tid); in sqlite3ThreadJoin()
33871 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; in sqlite3ThreadJoin()
33880 /********************************* Single-Threaded **************************/
33909 p->xTask = xTask; in sqlite3ThreadCreate()
33910 p->pIn = pIn; in sqlite3ThreadCreate()
33912 p->xTask = 0; in sqlite3ThreadCreate()
33913 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
33924 if( p->xTask ){ in sqlite3ThreadJoin()
33925 *ppOut = p->xTask(p->pIn); in sqlite3ThreadJoin()
33927 *ppOut = p->pResult; in sqlite3ThreadJoin()
33943 /****************************** End Single-Threaded *************************/
33959 ** This file contains routines used to translate between UTF-8,
33960 ** UTF-16, UTF-16BE, and UTF-16LE.
33962 ** Notes on UTF-8:
33964 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
33971 ** Notes on UTF-16: (with wwww+1==uuuuu)
33973 ** Word-0 Word-1 Value
33979 ** 0xff 0xfe little-endian utf-16 follows
33980 ** 0xfe 0xff big-endian utf-16 follows
33997 ** a multi-byte UTF8 character.
34036 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34037 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34048 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
34049 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34056 ** Translate a single UTF-8 character. Return the unicode value.
34063 ** Notes On Invalid UTF-8:
34065 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
34066 ** be encoded as a multi-byte character. Any multi-byte character that
34070 ** If a multi-byte character attempts to encode a value between
34074 ** byte of a character are interpreted as single-byte characters
34078 ** * This routine accepts over-length UTF8 encodings
34079 ** for unicode values 0x80 and greater. It does not change over-length
34085 c = sqlite3Utf8Trans1[c-0xc0]; \
34099 ** For this routine, we assume the UTF8 string is always zero-terminated. in sqlite3Utf8Read()
34103 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8Read()
34137 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); in sqlite3VdbeMemTranslate()
34138 assert( pMem->flags&MEM_Str ); in sqlite3VdbeMemTranslate()
34139 assert( pMem->enc!=desiredEnc ); in sqlite3VdbeMemTranslate()
34140 assert( pMem->enc!=0 ); in sqlite3VdbeMemTranslate()
34141 assert( pMem->n>=0 ); in sqlite3VdbeMemTranslate()
34153 /* If the translation is between UTF-16 little and big endian, then in sqlite3VdbeMemTranslate()
34157 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34165 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34166 zTerm = &zIn[pMem->n&~1]; in sqlite3VdbeMemTranslate()
34173 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34179 /* When converting from UTF-16, the maximum growth results from in sqlite3VdbeMemTranslate()
34180 ** translating a 2-byte character to a 4-byte UTF-8 character. in sqlite3VdbeMemTranslate()
34182 ** nul-terminator. in sqlite3VdbeMemTranslate()
34184 pMem->n &= ~1; in sqlite3VdbeMemTranslate()
34185 len = 2 * (sqlite3_int64)pMem->n + 1; in sqlite3VdbeMemTranslate()
34187 /* When converting from UTF-8 to UTF-16 the maximum growth is caused in sqlite3VdbeMemTranslate()
34188 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 in sqlite3VdbeMemTranslate()
34190 ** nul-terminator. in sqlite3VdbeMemTranslate()
34192 len = 2 * (sqlite3_int64)pMem->n + 2; in sqlite3VdbeMemTranslate()
34201 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
34202 zTerm = &zIn[pMem->n]; in sqlite3VdbeMemTranslate()
34203 zOut = sqlite3DbMallocRaw(pMem->db, len); in sqlite3VdbeMemTranslate()
34209 if( pMem->enc==SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
34211 /* UTF-8 -> UTF-16 Little-endian */ in sqlite3VdbeMemTranslate()
34218 /* UTF-8 -> UTF-16 Big-endian */ in sqlite3VdbeMemTranslate()
34224 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34228 if( pMem->enc==SQLITE_UTF16LE ){ in sqlite3VdbeMemTranslate()
34229 /* UTF-16 Little-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34241 zIn -= 2; in sqlite3VdbeMemTranslate()
34258 /* UTF-16 Big-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
34270 zIn -= 2; in sqlite3VdbeMemTranslate()
34287 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
34290 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); in sqlite3VdbeMemTranslate()
34292 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); in sqlite3VdbeMemTranslate()
34294 pMem->flags = c; in sqlite3VdbeMemTranslate()
34295 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
34296 pMem->z = (char*)zOut; in sqlite3VdbeMemTranslate()
34297 pMem->zMalloc = pMem->z; in sqlite3VdbeMemTranslate()
34298 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); in sqlite3VdbeMemTranslate()
34316 ** This routine checks for a byte-order mark at the beginning of the
34317 ** UTF-16 string stored in *pMem. If one is present, it is removed and
34319 ** byte-swapping, it just sets Mem.enc appropriately.
34328 assert( pMem->n>=0 ); in sqlite3VdbeMemHandleBom()
34329 if( pMem->n>1 ){ in sqlite3VdbeMemHandleBom()
34330 u8 b1 = *(u8 *)pMem->z; in sqlite3VdbeMemHandleBom()
34331 u8 b2 = *(((u8 *)pMem->z) + 1); in sqlite3VdbeMemHandleBom()
34343 pMem->n -= 2; in sqlite3VdbeMemHandleBom()
34344 memmove(pMem->z, &pMem->z[2], pMem->n); in sqlite3VdbeMemHandleBom()
34345 pMem->z[pMem->n] = '\0'; in sqlite3VdbeMemHandleBom()
34346 pMem->z[pMem->n+1] = '\0'; in sqlite3VdbeMemHandleBom()
34347 pMem->flags |= MEM_Term; in sqlite3VdbeMemHandleBom()
34348 pMem->enc = bom; in sqlite3VdbeMemHandleBom()
34356 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
34369 zTerm = (const u8*)(-1); in sqlite3Utf8CharLen()
34379 /* This test function is not currently used by the automated test-suite.
34384 ** Translate UTF-8 to UTF-8.
34386 ** This has the effect of making sure that the string is well-formed
34387 ** UTF-8. Miscoded characters are removed.
34389 ** The translation is done in-place and aborted if the output
34404 return (int)(zOut - zStart); in sqlite3Utf8To8()
34410 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
34411 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
34422 if( db->mallocFailed ){ in sqlite3Utf16to8()
34426 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34427 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
34428 assert( m.z || db->mallocFailed ); in sqlite3Utf16to8()
34433 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
34435 ** in pZ. nChar must be non-negative.
34449 return (int)(z-(unsigned char const *)zIn) in sqlite3Utf16ByteLen()
34450 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); in sqlite3Utf16ByteLen()
34469 n = (int)(z-zBuf); in sqlite3UtfSelfTest()
34478 assert( (z-zBuf)==n ); in sqlite3UtfSelfTest()
34515 ** sqlite3FaultSim() function only returns non-zero during testing.
34517 ** During testing, if the test harness has set a fault-sim callback using
34519 ** each call to sqlite3FaultSim() is relayed to that application-supplied
34520 ** callback and the integer return value form the application-supplied
34558 ** Return true if the floating point value is NaN or +Inf or -Inf.
34571 ** lower 30 bits of a 32-bit signed integer.
34586 ** The column type is an extra string stored after the zero-terminator on
34590 if( pCol->colFlags & COLFLAG_HASTYPE ){ in sqlite3ColumnType()
34591 return pCol->zCnName + strlen(pCol->zCnName) + 1; in sqlite3ColumnType()
34592 }else if( pCol->eCType ){ in sqlite3ColumnType()
34593 assert( pCol->eCType<=SQLITE_N_STDTYPE ); in sqlite3ColumnType()
34594 return (char*)sqlite3StdType[pCol->eCType-1]; in sqlite3ColumnType()
34601 ** Helper function for sqlite3Error() - called rarely. Broken out into
34606 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorFinish()
34617 db->errCode = err_code; in sqlite3Error()
34618 if( err_code || db->pErr ){ in sqlite3Error()
34621 db->errByteOffset = -1; in sqlite3Error()
34631 db->errCode = SQLITE_OK; in sqlite3ErrorClear()
34632 db->errByteOffset = -1; in sqlite3ErrorClear()
34633 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorClear()
34647 for(ii=0; ii<db->nDb; ii++){ in sqlite3SystemError()
34648 if( db->aDb[ii].pBt ){ in sqlite3SystemError()
34649 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); in sqlite3SystemError()
34651 db->iSysErrno = iErr; in sqlite3SystemError()
34661 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); in sqlite3SystemError()
34671 ** assumed to be encoded in UTF-8.
34679 db->errCode = err_code; in sqlite3ErrorWithMsg()
34683 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ in sqlite3ErrorWithMsg()
34689 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); in sqlite3ErrorWithMsg()
34697 sqlite3 *db = p->db; in sqlite3ProgressCheck()
34698 if( AtomicLoad(&db->u1.isInterrupted) ){ in sqlite3ProgressCheck()
34699 p->nErr++; in sqlite3ProgressCheck()
34700 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
34703 if( db->xProgress ){ in sqlite3ProgressCheck()
34704 if( p->rc==SQLITE_INTERRUPT ){ in sqlite3ProgressCheck()
34705 p->nProgressSteps = 0; in sqlite3ProgressCheck()
34706 }else if( (++p->nProgressSteps)>=db->nProgressOps ){ in sqlite3ProgressCheck()
34707 if( db->xProgress(db->pProgressArg) ){ in sqlite3ProgressCheck()
34708 p->nErr++; in sqlite3ProgressCheck()
34709 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
34711 p->nProgressSteps = 0; in sqlite3ProgressCheck()
34718 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
34730 sqlite3 *db = pParse->db; in sqlite3ErrorMsg()
34732 assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); in sqlite3ErrorMsg()
34733 db->errByteOffset = -2; in sqlite3ErrorMsg()
34737 if( db->errByteOffset<-1 ) db->errByteOffset = -1; in sqlite3ErrorMsg()
34738 if( db->suppressErr ){ in sqlite3ErrorMsg()
34740 if( db->mallocFailed ){ in sqlite3ErrorMsg()
34741 pParse->nErr++; in sqlite3ErrorMsg()
34742 pParse->rc = SQLITE_NOMEM; in sqlite3ErrorMsg()
34745 pParse->nErr++; in sqlite3ErrorMsg()
34746 sqlite3DbFree(db, pParse->zErrMsg); in sqlite3ErrorMsg()
34747 pParse->zErrMsg = zMsg; in sqlite3ErrorMsg()
34748 pParse->rc = SQLITE_ERROR; in sqlite3ErrorMsg()
34749 pParse->pWith = 0; in sqlite3ErrorMsg()
34760 if( db==0 || (pParse = db->pParse)==0 ) return errCode; in sqlite3ErrorToParser()
34761 pParse->rc = errCode; in sqlite3ErrorToParser()
34762 pParse->nErr++; in sqlite3ErrorToParser()
34767 ** Convert an SQL-style quoted string into a normal string by removing
34768 ** the quote characters. The conversion is done in-place. If the
34770 ** is a no-op.
34772 ** The input string must be zero-terminated. A new zero-terminator
34775 ** The return value is -1 if no dequoting occurs or the length of the
34779 ** 2002-02-14: This routine is extended to remove MS-Access style
34780 ** brackets from around identifiers. For example: "[a-b-c]" becomes
34781 ** "a-b-c".
34807 assert( sqlite3Isquote(p->u.zToken[0]) ); in sqlite3DequoteExpr()
34808 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; in sqlite3DequoteExpr()
34809 sqlite3Dequote(p->u.zToken); in sqlite3DequoteExpr()
34816 ** "abc" -> abc
34817 ** "ab""cd" -> (not possible because of the interior "")
34821 ** is always a no-op.
34825 if( p->n<2 ) return; in sqlite3DequoteToken()
34826 if( !sqlite3Isquote(p->z[0]) ) return; in sqlite3DequoteToken()
34827 for(i=1; i<p->n-1; i++){ in sqlite3DequoteToken()
34828 if( sqlite3Isquote(p->z[i]) ) return; in sqlite3DequoteToken()
34830 p->n -= 2; in sqlite3DequoteToken()
34831 p->z++; in sqlite3DequoteToken()
34838 p->z = z; in sqlite3TokenInit()
34839 p->n = sqlite3Strlen30(z); in sqlite3TokenInit()
34842 /* Convenient short-hand */
34849 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
34851 ** the contents of two buffers containing UTF-8 strings in a
34852 ** case-independent fashion, using the same definition of "case
34857 return zRight ? -1 : 0; in sqlite3_stricmp()
34874 c = (int)UpperToLower[c] - (int)UpperToLower[x]; in sqlite3StrICmp()
34885 return zRight ? -1 : 0; in sqlite3_strnicmp()
34891 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } in sqlite3_strnicmp()
34892 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; in sqlite3_strnicmp()
34896 ** Compute an 8-bit hash on a string that is insensitive to case differences
34908 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
34911 ** T. J. Dekker, "A Floating-Point Technique for Extending the
34912 ** Available Precision". 1971-07-26.
34918 ** binary64 rather than be carried around in an extended-precision in dekkerMul2()
34929 tx = x[0] - hx; in dekkerMul2()
34933 ty = y - hy; in dekkerMul2()
34937 cc = p - c + q + tx*ty; in dekkerMul2()
34940 x[1] = c - x[0]; in dekkerMul2()
34949 ** uses the encoding enc. The string is not necessarily zero-terminated.
34957 ** -1 => Not a valid number, but has a valid prefix which
34962 ** [+-]digits[E[+-]digits]
34963 ** [+-]digits.[digits][E[+-]digits]
34964 ** [+-].digits[E[+-]digits]
34986 int eValid = 1; /* True exponent is either not used or is well-formed */ in sqlite3AtoF()
34988 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ in sqlite3AtoF()
35004 for(i=3-enc; i<length && z[i]==0; i+=2){} in sqlite3AtoF()
35005 if( i<length ) eType = -100; in sqlite3AtoF()
35015 if( *z=='-' ){ in sqlite3AtoF()
35016 sign = -1; in sqlite3AtoF()
35024 s = s*10 + (*z - '0'); in sqlite3AtoF()
35026 if( s>=((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35027 /* skip non-significant significand digits in sqlite3AtoF()
35041 if( s<((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
35042 s = s*10 + (*z - '0'); in sqlite3AtoF()
35043 d--; in sqlite3AtoF()
35060 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ in sqlite3AtoF()
35063 if( *z=='-' ){ in sqlite3AtoF()
35064 esign = -1; in sqlite3AtoF()
35071 e = e<10000 ? (e*10 + (*z - '0')) : 10000; in sqlite3AtoF()
35083 *pResult = sign<0 ? -0.0 : +0.0; in sqlite3AtoF()
35093 e--; in sqlite3AtoF()
35105 while( e>=100 ){ e-=100; r *= 1.0e+100L; } in sqlite3AtoF()
35106 while( e>=10 ){ e-=10; r *= 1.0e+10L; } in sqlite3AtoF()
35107 while( e>=1 ){ e-=1; r *= 1.0e+01L; } in sqlite3AtoF()
35109 while( e<=-100 ){ e+=100; r *= 1.0e-100L; } in sqlite3AtoF()
35110 while( e<=-10 ){ e+=10; r *= 1.0e-10L; } in sqlite3AtoF()
35111 while( e<=-1 ){ e+=1; r *= 1.0e-01L; } in sqlite3AtoF()
35128 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); in sqlite3AtoF()
35131 e -= 100; in sqlite3AtoF()
35132 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3AtoF()
35135 e -= 10; in sqlite3AtoF()
35139 e -= 1; in sqlite3AtoF()
35143 while( e<=-100 ){ in sqlite3AtoF()
35145 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3AtoF()
35147 while( e<=-10 ){ in sqlite3AtoF()
35149 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3AtoF()
35151 while( e<=-1 ){ in sqlite3AtoF()
35153 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3AtoF()
35159 if( sign<0 ) *pResult = -*pResult; in sqlite3AtoF()
35163 /* return true if number and no extra non-whitespace characters after */ in sqlite3AtoF()
35167 return -1; in sqlite3AtoF()
35180 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
35192 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; in sqlite3Int64ToText()
35196 i = sizeof(zTemp)-2; in sqlite3Int64ToText()
35197 zTemp[sizeof(zTemp)-1] = 0; in sqlite3Int64ToText()
35198 while( 1 /*exit-by-break*/ ){ in sqlite3Int64ToText()
35202 i--; in sqlite3Int64ToText()
35204 if( v<0 ) zTemp[--i] = '-'; in sqlite3Int64ToText()
35205 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); in sqlite3Int64ToText()
35206 return sizeof(zTemp)-1-i; in sqlite3Int64ToText()
35210 ** Compare the 19-character string zNum against the text representation
35221 ** will return -8.
35229 c = (zNum[i*incr]-pow63[i])*10; in compare2pow63()
35232 c = zNum[18*incr] - '8'; in compare2pow63()
35233 testcase( c==(-1) ); in compare2pow63()
35241 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
35246 ** -1 Not even a prefix of the input text looks like an integer
35247 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35248 ** 1 Excess non-space text after the integer value
35249 ** 2 Integer too large for a 64-bit signed integer or is malformed
35253 ** The string is not necessarily zero-terminated. The encoding is
35262 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ in sqlite3Atoi64()
35273 for(i=3-enc; i<length && zNum[i]==0; i+=2){} in sqlite3Atoi64()
35280 if( *zNum=='-' ){ in sqlite3Atoi64()
35290 u = u*10 + c - '0'; in sqlite3Atoi64()
35297 ** from clang and -fsanitize=undefined. This test and assignment make in sqlite3Atoi64()
35299 ** them, but we must appease the undefined-behavior pharisees. */ in sqlite3Atoi64()
35302 *pNum = -(i64)u; in sqlite3Atoi64()
35308 rc = -1; in sqlite3Atoi64()
35309 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ in sqlite3Atoi64()
35315 rc = 1; /* Extra non-space text after the integer */ in sqlite3Atoi64()
35326 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ in sqlite3Atoi64()
35340 assert( u-1==LARGEST_INT64 ); in sqlite3Atoi64()
35348 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
35349 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
35354 ** 0 Successful transformation. Fits in a 64-bit signed integer.
35356 ** 2 Integer too large for a 64-bit signed integer or is malformed
35371 if( k-i>16 ) return 2; in sqlite3DecOrHexToI64()
35377 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); in sqlite3DecOrHexToI64()
35384 ** If zNum represents an integer that will fit in 32-bits, then set
35389 ** Any non-numeric characters that following zNum are ignored.
35391 ** input number to be zero-terminated.
35397 if( zNum[0]=='-' ){ in sqlite3GetInt32()
35424 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ in sqlite3GetInt32()
35431 ** 2^31 -> 2147483648 in sqlite3GetInt32()
35437 testcase( v-neg==2147483647 ); in sqlite3GetInt32()
35438 if( v-neg>2147483647 ){ in sqlite3GetInt32()
35442 v = -v; in sqlite3GetInt32()
35449 ** Return a 32-bit integer value extracted from a string. If the
35459 ** Decode a floating-point value into an approximate decimal
35463 ** n is positive. Or round to -n signficant digits after the
35468 ** stored in p->z[] which is a often (but not always) a pointer
35469 ** into the middle of p->zBuf[]. There are p->n significant digits.
35470 ** The p->z[] array is *not* zero-terminated.
35476 p->isSpecial = 0; in sqlite3FpDecode()
35477 p->z = p->zBuf; in sqlite3FpDecode()
35482 p->sign = '-'; in sqlite3FpDecode()
35483 r = -r; in sqlite3FpDecode()
35485 p->sign = '+'; in sqlite3FpDecode()
35486 p->n = 1; in sqlite3FpDecode()
35487 p->iDP = 1; in sqlite3FpDecode()
35488 p->z = "0"; in sqlite3FpDecode()
35491 p->sign = '+'; in sqlite3FpDecode()
35496 p->isSpecial = 1 + (v!=0x7ff0000000000000LL); in sqlite3FpDecode()
35497 p->n = 0; in sqlite3FpDecode()
35498 p->iDP = 0; in sqlite3FpDecode()
35508 while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; } in sqlite3FpDecode()
35509 while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; } in sqlite3FpDecode()
35510 while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; } in sqlite3FpDecode()
35512 while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; } in sqlite3FpDecode()
35513 while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; } in sqlite3FpDecode()
35514 while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; } in sqlite3FpDecode()
35518 /* If high-precision floating point is not available using "long double", in sqlite3FpDecode()
35519 ** then use Dekker-style double-double computation to increase the in sqlite3FpDecode()
35533 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3FpDecode()
35537 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3FpDecode()
35541 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3FpDecode()
35544 while( rr[0]<9.223372036854774784e-83 ){ in sqlite3FpDecode()
35545 exp -= 100; in sqlite3FpDecode()
35546 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3FpDecode()
35549 exp -= 10; in sqlite3FpDecode()
35553 exp -= 1; in sqlite3FpDecode()
35557 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; in sqlite3FpDecode()
35562 i = sizeof(p->zBuf)-1; in sqlite3FpDecode()
35564 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } in sqlite3FpDecode()
35565 assert( i>=0 && i<sizeof(p->zBuf)-1 ); in sqlite3FpDecode()
35566 p->n = sizeof(p->zBuf) - 1 - i; in sqlite3FpDecode()
35567 assert( p->n>0 ); in sqlite3FpDecode()
35568 assert( p->n<sizeof(p->zBuf) ); in sqlite3FpDecode()
35569 p->iDP = p->n + exp; in sqlite3FpDecode()
35571 iRound = p->iDP - iRound; in sqlite3FpDecode()
35572 if( iRound==0 && p->zBuf[i+1]>='5' ){ in sqlite3FpDecode()
35574 p->zBuf[i--] = '0'; in sqlite3FpDecode()
35575 p->n++; in sqlite3FpDecode()
35576 p->iDP++; in sqlite3FpDecode()
35579 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ in sqlite3FpDecode()
35580 char *z = &p->zBuf[i+1]; in sqlite3FpDecode()
35582 p->n = iRound; in sqlite3FpDecode()
35584 int j = iRound-1; in sqlite3FpDecode()
35585 while( 1 /*exit-by-break*/ ){ in sqlite3FpDecode()
35590 p->z[i--] = '1'; in sqlite3FpDecode()
35591 p->n++; in sqlite3FpDecode()
35592 p->iDP++; in sqlite3FpDecode()
35595 j--; in sqlite3FpDecode()
35600 p->z = &p->zBuf[i+1]; in sqlite3FpDecode()
35601 assert( i+p->n < sizeof(p->zBuf) ); in sqlite3FpDecode()
35602 while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; } in sqlite3FpDecode()
35606 ** Try to convert z into an unsigned 32-bit integer. Return true on
35615 v = v*10 + z[i] - '0'; in sqlite3GetUInt32()
35624 ** The variable-length integer encoding is as follows:
35631 ** 7 bits - A
35632 ** 14 bits - BA
35633 ** 21 bits - BBA
35634 ** 28 bits - BBBA
35635 ** 35 bits - BBBBA
35636 ** 42 bits - BBBBBA
35637 ** 49 bits - BBBBBBA
35638 ** 56 bits - BBBBBBBA
35639 ** 64 bits - BBBBBBBBC
35643 ** Write a 64-bit variable-length integer to memory starting at p[0].
35647 ** A variable-length integer consists of the lower 7 bits of each byte
35658 for(i=7; i>=0; i--){ in putVarint64()
35671 for(i=0, j=n-1; j>=0; j--, i++){ in putVarint64()
35703 ** Read a 64-bit variable-length integer from memory starting at p[0].
35846 b = p[-4]; in sqlite3GetVarint()
35857 ** Read a 32-bit variable-length integer from memory starting at p[0].
35860 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
35864 ** single-byte case. All code should use the MACRO version as
35865 ** this function assumes the single-byte case has already been handled.
35871 /* Assume that the single-byte case has already been handled by in sqlite3GetVarint32()
35876 /* This is the two-byte case */ in sqlite3GetVarint32()
35881 /* This is the three-byte case */ in sqlite3GetVarint32()
35898 ** 64-bit integer.
35908 ** Read or write a four-byte big-endian integer value.
35975 n--; in sqlite3HexToBlob()
36018 eOpenState = db->eOpenState; in sqlite3SafetyCheckOk()
36031 eOpenState = db->eOpenState; in sqlite3SafetyCheckSickOrOk()
36044 ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
36045 ** the other 64-bit signed integer at *pA and store the result in *pA.
36055 testcase( iB==-1 ); testcase( iB==0 ); in sqlite3AddInt64()
36057 testcase( iA>0 && LARGEST_INT64 - iA == iB ); in sqlite3AddInt64()
36058 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); in sqlite3AddInt64()
36059 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; in sqlite3AddInt64()
36061 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); in sqlite3AddInt64()
36062 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); in sqlite3AddInt64()
36063 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; in sqlite3AddInt64()
36075 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); in sqlite3SubInt64()
36077 *pA -= iB; in sqlite3SubInt64()
36080 return sqlite3AddInt64(pA, -iB); in sqlite3SubInt64()
36098 if( -iA>LARGEST_INT64/-iB ) return 1; in sqlite3MulInt64()
36107 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
36108 ** if the integer has a value of -2147483648, return +2147483647
36113 return -x; in sqlite3AbsInt32()
36118 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
36124 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
36129 ** test.db-journal => test.nal
36130 ** test.db-wal => test.wal
36131 ** test.db-shm => test.shm
36132 ** test.db-mj7f3319fa => test.9fa
36141 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} in sqlite3FileSuffix3()
36142 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); in sqlite3FileSuffix3()
36160 5, 5, 5, /* 12-14 */ in sqlite3LogEstAdd()
36161 4, 4, 4, 4, /* 15-18 */ in sqlite3LogEstAdd()
36162 3, 3, 3, 3, 3, 3, /* 19-24 */ in sqlite3LogEstAdd()
36163 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ in sqlite3LogEstAdd()
36168 return a+x[a-b]; in sqlite3LogEstAdd()
36172 return b+x[b-a]; in sqlite3LogEstAdd()
36185 while( x<8 ){ y -= 10; x <<= 1; } in sqlite3LogEst()
36188 int i = 60 - __builtin_clzll(x); in sqlite3LogEst()
36192 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ in sqlite3LogEst()
36196 return a[x&7] + y - 10; in sqlite3LogEst()
36210 e = (a>>52) - 1022; in sqlite3LogEstFromDouble()
36221 if( n>=5 ) n -= 2; in sqlite3LogEstToInt()
36222 else if( n>=1 ) n -= 1; in sqlite3LogEstToInt()
36224 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); in sqlite3LogEstToInt()
36231 ** db->mallocFailed flag is set.
36244 ** is always zero-terminated.
36331 ** High-resolution hardware timer used for debugging and testing only.
36350 ** This file contains inline asm code for retrieving "high-performance"
36357 ** The following routine only works on Pentium-class (or newer) processors.
36359 ** processor and returns that value. This can be used for high-res
36441 ** This is the implementation of generic hash-tables
36454 pNew->first = 0;
36455 pNew->count = 0;
36456 pNew->htsize = 0;
36457 pNew->ht = 0;
36468 elem = pH->first;
36469 pH->first = 0;
36470 sqlite3_free(pH->ht);
36471 pH->ht = 0;
36472 pH->htsize = 0;
36474 HashElem *next_elem = elem->next;
36478 pH->count = 0;
36487 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
36490 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
36508 pHead = pEntry->count ? pEntry->chain : 0;
36509 pEntry->count++;
36510 pEntry->chain = pNew;
36515 pNew->next = pHead;
36516 pNew->prev = pHead->prev;
36517 if( pHead->prev ){ pHead->prev->next = pNew; }
36518 else { pH->first = pNew; }
36519 pHead->prev = pNew;
36521 pNew->next = pH->first;
36522 if( pH->first ){ pH->first->prev = pNew; }
36523 pNew->prev = 0;
36524 pH->first = pNew;
36543 if( new_size==pH->htsize ) return 0;
36559 sqlite3_free(pH->ht);
36560 pH->ht = new_ht;
36561 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
36563 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
36564 unsigned int h = strHash(elem->pKey) % new_size;
36565 next_elem = elem->next;
36586 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
36588 h = strHash(pKey) % pH->htsize;
36589 pEntry = &pH->ht[h];
36590 elem = pEntry->chain;
36591 count = pEntry->count;
36594 elem = pH->first;
36595 count = pH->count;
36600 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
36603 elem = elem->next;
36604 count--;
36618 if( elem->prev ){
36619 elem->prev->next = elem->next;
36621 pH->first = elem->next;
36623 if( elem->next ){
36624 elem->next->prev = elem->prev;
36626 if( pH->ht ){
36627 pEntry = &pH->ht[h];
36628 if( pEntry->chain==elem ){
36629 pEntry->chain = elem->next;
36631 assert( pEntry->count>0 );
36632 pEntry->count--;
36635 pH->count--;
36636 if( pH->count==0 ){
36637 assert( pH->first==0 );
36638 assert( pH->count==0 );
36650 return findElementWithHash(pH, pKey, 0)->data;
36675 if( elem->data ){
36676 void *old_data = elem->data;
36680 elem->data = data;
36681 elem->pKey = pKey;
36688 new_elem->pKey = pKey;
36689 new_elem->data = data;
36690 pH->count++;
36691 if( pH->count>=10 && pH->count > 2*pH->htsize ){
36692 if( rehash(pH, pH->count*2) ){
36693 assert( pH->htsize>0 );
36694 h = strHash(pKey) % pH->htsize;
36697 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
36774 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
36775 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
36776 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
36805 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
36822 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
36839 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
36875 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
36911 ** 2022-09-06
36954 ** VFS - the database and the rollback journal.
36963 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
37078 /* Forward declarations for the low-level storage engine
37094 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
37098 ** underlying key/value store to use - either "local" or "session".
37100 ** Both zKey and zData are zero-terminated pure text strings.
37114 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
37127 ** this routine is a no-op.
37133 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
37144 ** not counting the final zero terminator. Return -1 if the key does
37164 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37165 return -1;
37171 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
37180 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
37181 return -1;
37183 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
37186 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
37195 ** Maintenance reminder: if this struct changes in any way, the JSON
37201 ** that JSON description.
37213 ** for JavaScript-side implementations in WASM builds. In such builds
37242 ** ---------------
37244 ** * Non-zero bytes are encoded as upper-case hexadecimal
37246 ** * A sequence of one or more zero-bytes that are not at the
37247 ** beginning of the buffer are encoded as a little-endian
37248 ** base-26 number using a..z. "a" means 0. "b" means 1,
37252 ** of hexadecimal and base-26 numbers, it is always clear where
37264 /* A sequence of 1 or more zeros is stored as a little-endian
37265 ** base-26 number using a..z as the digits. So one zero is "b".
37271 i += k-1;
37283 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37284 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37285 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37286 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
37287 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37288 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37289 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37290 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37292 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37293 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37294 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37295 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37296 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37297 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37298 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37299 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37322 n += (c - 'a')*mult;
37326 if( j+n>nOut ) return -1;
37342 ** Decode a complete journal file. Allocate space in pFile->aJrnl
37343 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
37346 ** The first few characters of the text encoding will be a little-endian
37347 ** base-26 number (digits a..z) that is the total number of bytes
37348 ** in the decoded journal file image. This base-26 number is followed
37350 ** separator is required to act as a terminator for the base-26 number.
37353 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
37354 const char *zTxt, /* Text encoding. Zero-terminated */
37362 n += (zTxt[i] - 'a')*mult;
37365 sqlite3_free(pFile->aJrnl);
37366 pFile->aJrnl = sqlite3_malloc64( n );
37367 if( pFile->aJrnl==0 ){
37368 pFile->nJrnl = 0;
37371 pFile->nJrnl = n;
37372 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
37373 if( n<pFile->nJrnl ){
37374 sqlite3_free(pFile->aJrnl);
37375 pFile->aJrnl = 0;
37376 pFile->nJrnl = 0;
37386 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
37392 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
37398 ** Close an kvvfs-file.
37403 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
37404 pFile->isJournal ? "journal" : "db"));
37405 sqlite3_free(pFile->aJrnl);
37406 sqlite3_free(pFile->aData);
37411 ** Read from the -journal file.
37420 assert( pFile->isJournal );
37421 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37422 if( pFile->aJrnl==0 ){
37423 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
37430 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
37433 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
37435 if( iOfst+iAmt>pFile->nJrnl ){
37438 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
37455 char *aData = pFile->aData;
37458 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37463 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
37466 pFile->szPage = iAmt;
37472 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
37473 aData, SQLITE_KVOS_SZ-1);
37481 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
37493 memset(zBuf+n, 0, iAmt-n);
37501 ** Write into the -journal file.
37511 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37513 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
37514 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
37518 pFile->aJrnl = aNew;
37519 if( pFile->nJrnl<iOfst ){
37520 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
37522 pFile->nJrnl = iEnd;
37524 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
37540 char *aData = pFile->aData;
37541 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
37543 assert( (iAmt & (iAmt-1))==0 );
37544 assert( pFile->szPage<0 || pFile->szPage==iAmt );
37545 pFile->szPage = iAmt;
37549 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
37552 if( iOfst+iAmt > pFile->szDb ){
37553 pFile->szDb = iOfst + iAmt;
37559 ** Truncate an kvvfs-file.
37563 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
37565 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
37566 sqlite3_free(pFile->aJrnl);
37567 pFile->aJrnl = 0;
37568 pFile->nJrnl = 0;
37573 if( pFile->szDb>size
37574 && pFile->szPage>0
37575 && (size % pFile->szPage)==0
37579 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
37580 pgno = 1 + size/pFile->szPage;
37581 pgnoMax = 2 + pFile->szDb/pFile->szPage;
37584 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
37587 pFile->szDb = size;
37594 ** Sync an kvvfs-file.
37600 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
37601 if( pFile->nJrnl<=0 ){
37604 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
37608 n = pFile->nJrnl;
37615 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
37616 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
37625 ** Return the current file-size of an kvvfs-file.
37629 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
37630 *pSize = pFile->nJrnl;
37635 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
37636 if( pFile->szDb>=0 ){
37637 *pSize = pFile->szDb;
37645 ** Lock an kvvfs-file.
37649 assert( !pFile->isJournal );
37650 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
37653 pFile->szDb = kvvfsReadFileSize(pFile);
37659 ** Unlock an kvvfs-file.
37663 assert( !pFile->isJournal );
37664 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
37666 pFile->szDb = -1;
37672 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
37681 ** File control method. For custom operations on an kvvfs-file.
37692 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
37693 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
37702 ** Return the sector-size in bytes for an kvvfs-file.
37709 ** Return the device characteristic flags supported by an kvvfs-file.
37733 pFile->isJournal = 0;
37734 pFile->base.pMethods = &kvvfs_db_io_methods;
37736 if( strcmp(zName, "local-journal")==0
37737 || strcmp(zName, "session-journal")==0
37739 pFile->isJournal = 1;
37740 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
37745 pFile->zClass = "session";
37747 pFile->zClass = "local";
37749 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
37750 if( pFile->aData==0 ){
37753 pFile->aJrnl = 0;
37754 pFile->nJrnl = 0;
37755 pFile->szPage = -1;
37756 pFile->szDb = -1;
37762 ** ensure the file-system modifications are synced to disk before
37766 if( strcmp(zPath, "local-journal")==0 ){
37769 if( strcmp(zPath, "session-journal")==0 ){
37786 if( strcmp(zPath, "local-journal")==0 ){
37789 if( strcmp(zPath, "session-journal")==0 ){
37822 if( nOut<nPath+1 ) nPath = nOut - 1;
37874 ** This routine is called initialize the KV-vfs as the default VFS.
37904 ** This file contains the VFS implementation for unix-like operating systems
37910 ** use flock(), dot-files, various proprietary locking schemas, or simply
37920 ** * General-purpose declarations and utility functions.
37924 ** + for no-op locks
37925 ** + for dot-file locks
37933 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
37946 ** 3. Dot-file locking,
38006 ** -DHAVE_GETHOSTUUID=0
38007 ** -DHAVE_GETHOSTUUID=1
38010 ** -DSQLITE_ENABLE_LOCKING_STYLE.
38052 ** If we are to be thread-safe, include the pthreads header.
38073 ** Maximum supported path-length.
38092 # define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
38093 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
38132 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
38161 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
38188 ** transaction counter in bytes 24-27 of database files are updated
38253 ** The threadid macro resolves to the thread-id or to 0. Used for
38274 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
38275 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
38283 ** Linux-specific IOCTL magic numbers used for controlling F2FS
38301 ** which always has the same well-defined interface.
38312 ** Many system calls are accessed through pointer-to-functions so that
38341 ** DJGPP. But it is DOS - what did you expect?
38494 ** log if they come from non-root processes. So avoid calling fchown() if
38574 int i = -1;
38578 for(i=0; i<ArraySize(aSyscall)-1; i++){
38606 ** The m parameter will be non-zero only when creating -wal, -journal,
38607 ** and -shm files. We want those files to have *exactly* the same
38609 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
38634 fd = -1;
38705 ** integer lock-type.
38724 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
38725 ** command-line option on the compiler. This code is normally
38741 if( p->l_type==F_RDLCK ){
38743 }else if( p->l_type==F_WRLCK ){
38745 }else if( p->l_type==F_UNLCK ){
38750 assert( p->l_whence==SEEK_SET );
38754 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
38755 (int)p->l_pid, s);
38756 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
38769 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
38789 /* On Android, ftruncate() always uses 32-bit offsets, even if
38850 ** a performance-critical path, so it is sufficient to put these
38875 ** Changes are made in-place. Return the new name length.
38877 ** The original filename is in z[0..n-1]. Return the number of
38882 while( n>1 && z[n-1]=='/' ){ n--; }
38891 while( j>0 && z[j-1]!='/' ){ j--; }
38892 if( j>0 ){ j--; }
38923 pNew->zCanonicalName = (char*)&pNew[1];
38924 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
38925 n = vxworksSimplifyName(pNew->zCanonicalName, n);
38932 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
38933 if( pCandidate->nName==n
38934 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
38937 pCandidate->nRef++;
38944 pNew->nRef = 1;
38945 pNew->nName = n;
38946 pNew->pNext = vxworksFileList;
38958 assert( pId->nRef>0 );
38959 pId->nRef--;
38960 if( pId->nRef==0 ){
38962 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
38964 *pp = pId->pNext;
39022 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
39050 ** on linux - with NPTL a lock created by thread A can override locks
39051 ** in thread B. But there is no way to know at compile-time which
39053 ** compile-time whether or not thread A can override locks on thread B.
39054 ** One has to do a run-time check to discover the behavior of the
39074 ** sizes ino_t at only 32-bits instead of 64-bits. (See
39075 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
39076 ** To work around this, always allocate 64-bits for the inode number.
39077 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
39144 assert( pFile->pInode );
39145 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
39148 assert( pFile->pInode );
39149 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
39155 ** This function - unixLogErrorAtLine(), is only ever called via the macro
39160 ** errno and, if possible, the human-readable equivalent from strerror() or
39166 ** failed (e.g. "unlink", "open") and the associated file-system path,
39180 ** the strerror() function to obtain the human-readable error message
39203 strerror_r(iErrno, aErr, sizeof(aErr)-1);
39209 /* Non-threadsafe build, use strerror(). */
39215 "os_unix.c:%d: (%d) %s(%s) - %s",
39244 pFile ? pFile->zPath : 0, lineno);
39249 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
39253 pFile->lastErrno = error;
39257 ** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
39260 unixInodeInfo *pInode = pFile->pInode;
39264 for(p=pInode->pUnused; p; p=pNext){
39265 pNext = p->pNext;
39266 robust_close(pFile, p->fd, __LINE__);
39269 pInode->pUnused = 0;
39279 unixInodeInfo *pInode = pFile->pInode;
39283 pInode->nRef--;
39284 if( pInode->nRef==0 ){
39285 assert( pInode->pShmNode==0 );
39286 sqlite3_mutex_enter(pInode->pLockMutex);
39288 sqlite3_mutex_leave(pInode->pLockMutex);
39289 if( pInode->pPrev ){
39290 assert( pInode->pPrev->pNext==pInode );
39291 pInode->pPrev->pNext = pInode->pNext;
39294 inodeList = pInode->pNext;
39296 if( pInode->pNext ){
39297 assert( pInode->pNext->pPrev==pInode );
39298 pInode->pNext->pPrev = pInode->pPrev;
39300 sqlite3_mutex_free(pInode->pLockMutex);
39322 struct stat statbuf; /* Low-level file information */
39327 /* Get low-level information about the file that we can used to
39330 fd = pFile->h;
39335 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
39342 ** incorrectly for zero-size files. See ticket #3260. To work
39351 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
39368 fileId.pId = pFile->pId;
39374 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
39375 pInode = pInode->pNext;
39383 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
39385 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
39386 if( pInode->pLockMutex==0 ){
39391 pInode->nRef = 1;
39393 pInode->pNext = inodeList;
39394 pInode->pPrev = 0;
39395 if( inodeList ) inodeList->pPrev = pInode;
39398 pInode->nRef++;
39409 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
39412 return pFile->pInode!=0 &&
39413 (osStat(pFile->zPath, &buf)!=0
39414 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
39433 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
39435 rc = osFstat(pFile->h, &buf);
39437 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
39441 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
39445 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
39449 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
39458 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39469 assert( pFile->eFileLock<=SHARED_LOCK );
39470 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
39473 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39480 if( !reserved && !pFile->pInode->bProcessLock ){
39486 if( osFcntl(pFile->h, F_GETLK, &lock) ){
39495 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
39496 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
39506 ** Set a posix-advisory-lock.
39510 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
39515 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
39526 int tm = pFile->iBusyTimeout;
39536 tm--;
39544 ** Attempt to set a system-lock on the file pFile. The lock is
39547 ** If the pFile was opened read/write from unix-excl, then the only lock
39550 ** operations become no-ops. Locking operations still happen internally,
39555 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
39556 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
39557 ** and is read-only.
39559 ** Zero is returned if the call completes successfully, or -1 if a call
39564 unixInodeInfo *pInode = pFile->pInode;
39566 assert( sqlite3_mutex_held(pInode->pLockMutex) );
39567 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
39568 if( pInode->bProcessLock==0 ){
39570 assert( pInode->nLock==0 );
39575 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
39577 pInode->bProcessLock = 1;
39578 pInode->nLock++;
39583 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
39589 ** Lock the file with the lock specified by parameter eFileLock - one
39603 ** UNLOCKED -> SHARED
39604 ** SHARED -> RESERVED
39605 ** SHARED -> EXCLUSIVE
39606 ** RESERVED -> (PENDING) -> EXCLUSIVE
39607 ** PENDING -> EXCLUSIVE
39615 ** lock primitives (called read-locks and write-locks below, to avoid
39624 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
39625 ** byte'. If this is successful, 'shared byte range' is read-locked
39628 ** and Windows95 lacks a shared-lock capability. So on Windows95, a
39630 ** Windows95 is now pretty much extinct, but this work-around for the
39631 ** lack of shared-locks on Windows95 lives on, for backwards
39635 ** A RESERVED lock is implemented by grabbing a write-lock on the
39640 ** a write-lock on the entire 'shared byte range'. Since all other locks
39641 ** require a read-lock on one of the bytes within this range, this ensures
39646 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
39650 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
39660 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
39661 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
39662 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
39669 if( pFile->eFileLock>=eFileLock ){
39670 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
39680 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
39682 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
39684 /* This mutex is needed because pFile->pInode is shared across threads
39686 pInode = pFile->pInode;
39687 sqlite3_mutex_enter(pInode->pLockMutex);
39692 if( (pFile->eFileLock!=pInode->eFileLock &&
39693 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
39704 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
39706 assert( pFile->eFileLock==0 );
39707 assert( pInode->nShared>0 );
39708 pFile->eFileLock = SHARED_LOCK;
39709 pInode->nShared++;
39710 pInode->nLock++;
39722 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK)
39734 pFile->eFileLock = PENDING_LOCK;
39735 pInode->eFileLock = PENDING_LOCK;
39744 assert( pInode->nShared==0 );
39745 assert( pInode->eFileLock==0 );
39748 /* Now get the read-lock */
39772 pFile->eFileLock = SHARED_LOCK;
39773 pInode->nLock++;
39774 pInode->nShared = 1;
39776 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
39785 assert( 0!=pFile->eFileLock );
39808 /* Set up the transaction-counter change checking flags when
39814 && pFile->eFileLock<=SHARED_LOCK
39817 pFile->transCntrChng = 0;
39818 pFile->dbUpdate = 0;
39819 pFile->inNormalWrite = 1;
39824 pFile->eFileLock = eFileLock;
39825 pInode->eFileLock = eFileLock;
39829 sqlite3_mutex_leave(pInode->pLockMutex);
39830 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
39840 unixInodeInfo *pInode = pFile->pInode;
39841 UnixUnusedFd *p = pFile->pPreallocatedUnused;
39843 p->pNext = pInode->pUnused;
39844 pInode->pUnused = p;
39845 pFile->h = -1;
39846 pFile->pPreallocatedUnused = 0;
39854 ** the requested locking level, this routine is a no-op.
39869 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
39870 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
39874 if( pFile->eFileLock<=eFileLock ){
39877 pInode = pFile->pInode;
39878 sqlite3_mutex_enter(pInode->pLockMutex);
39879 assert( pInode->nShared!=0 );
39880 if( pFile->eFileLock>SHARED_LOCK ){
39881 assert( pInode->eFileLock==pFile->eFileLock );
39892 pFile->inNormalWrite = 0;
39896 ** before establishing the readlock - to avoid a race condition we downgrade
39912 off_t divSize = SHARED_SIZE - 1;
39918 if( unixFileLock(pFile, &lock)==(-1) ){
39928 if( unixFileLock(pFile, &lock)==(-1) ){
39939 lock.l_len = SHARED_SIZE-divSize;
39940 if( unixFileLock(pFile, &lock)==(-1) ){
39971 pInode->eFileLock = SHARED_LOCK;
39983 pInode->nShared--;
39984 if( pInode->nShared==0 ){
39989 pInode->eFileLock = NO_LOCK;
39993 pInode->eFileLock = NO_LOCK;
39994 pFile->eFileLock = NO_LOCK;
40002 pInode->nLock--;
40003 assert( pInode->nLock>=0 );
40004 if( pInode->nLock==0 ) closePendingFds(pFile);
40008 sqlite3_mutex_leave(pInode->pLockMutex);
40010 pFile->eFileLock = eFileLock;
40020 ** the requested locking level, this routine is a no-op.
40024 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
40049 if( pFile->h>=0 ){
40050 robust_close(pFile, pFile->h, __LINE__);
40051 pFile->h = -1;
40054 if( pFile->pId ){
40055 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40056 osUnlink(pFile->pId->zCanonicalName);
40058 vxworksReleaseFileId(pFile->pId);
40059 pFile->pId = 0;
40063 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
40064 osUnlink(pFile->zPath);
40065 sqlite3_free(*(char**)&pFile->zPath);
40066 pFile->zPath = 0;
40069 OSTRACE(("CLOSE %-3d\n", pFile->h));
40070 OpenCounter(-1);
40071 sqlite3_free(pFile->pPreallocatedUnused);
40082 unixInodeInfo *pInode = pFile->pInode;
40093 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
40094 sqlite3_mutex_enter(pInode->pLockMutex);
40095 if( pInode->nLock ){
40098 ** descriptor to pInode->pUnused list. It will be automatically closed
40103 sqlite3_mutex_leave(pInode->pLockMutex);
40105 assert( pFile->pShm==0 );
40115 ****************************** No-op Locking **********************************
40121 ** This locking mode is appropriate for use on read-only databases
40122 ** (ex: databases that are burned into CD-ROM, for example.) It can
40152 /******************* End of the no-op lock implementation *********************
40156 ************************* Begin dot-file Locking ******************************
40186 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40201 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
40202 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
40208 ** Lock the file with the lock specified by parameter eFileLock - one
40222 ** UNLOCKED -> SHARED
40223 ** SHARED -> RESERVED
40224 ** SHARED -> (PENDING) -> EXCLUSIVE
40225 ** RESERVED -> (PENDING) -> EXCLUSIVE
40226 ** PENDING -> EXCLUSIVE
40236 char *zLockFile = (char *)pFile->lockingContext;
40243 if( pFile->eFileLock > NO_LOCK ){
40244 pFile->eFileLock = eFileLock;
40271 pFile->eFileLock = eFileLock;
40280 ** the requested locking level, this routine is a no-op.
40286 char *zLockFile = (char *)pFile->lockingContext;
40290 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
40291 pFile->eFileLock, osGetpid(0)));
40294 /* no-op if possible */
40295 if( pFile->eFileLock==eFileLock ){
40303 pFile->eFileLock = SHARED_LOCK;
40320 pFile->eFileLock = NO_LOCK;
40331 sqlite3_free(pFile->lockingContext);
40334 /****************** End of the dot-file lock implementation *******************
40342 ** flock() locking is like dot-file locking in that the various
40343 ** fine-grain locking levels supported by SQLite are collapsed into
40370 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40383 if( pFile->eFileLock>SHARED_LOCK ){
40390 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
40393 lrc = robust_flock(pFile->h, LOCK_UN);
40412 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
40425 ** Lock the file with the lock specified by parameter eFileLock - one
40439 ** UNLOCKED -> SHARED
40440 ** SHARED -> RESERVED
40441 ** SHARED -> (PENDING) -> EXCLUSIVE
40442 ** RESERVED -> (PENDING) -> EXCLUSIVE
40443 ** PENDING -> EXCLUSIVE
40461 if (pFile->eFileLock > NO_LOCK) {
40462 pFile->eFileLock = eFileLock;
40468 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
40477 pFile->eFileLock = eFileLock;
40479 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
40495 ** the requested locking level, this routine is a no-op.
40501 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
40502 pFile->eFileLock, osGetpid(0)));
40505 /* no-op if possible */
40506 if( pFile->eFileLock==eFileLock ){
40512 pFile->eFileLock = eFileLock;
40517 if( robust_flock(pFile->h, LOCK_UN) ){
40523 pFile->eFileLock = NO_LOCK;
40547 ** Semaphore locking is like dot-lock and flock in that it really only
40557 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40570 if( pFile->eFileLock>SHARED_LOCK ){
40576 sem_t *pSem = pFile->pInode->pSem;
40578 if( sem_trywait(pSem)==-1 ){
40585 reserved = (pFile->eFileLock < SHARED_LOCK);
40592 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
40599 ** Lock the file with the lock specified by parameter eFileLock - one
40613 ** UNLOCKED -> SHARED
40614 ** SHARED -> RESERVED
40615 ** SHARED -> (PENDING) -> EXCLUSIVE
40616 ** RESERVED -> (PENDING) -> EXCLUSIVE
40617 ** PENDING -> EXCLUSIVE
40629 sem_t *pSem = pFile->pInode->pSem;
40634 if (pFile->eFileLock > NO_LOCK) {
40635 pFile->eFileLock = eFileLock;
40641 if( sem_trywait(pSem)==-1 ){
40647 pFile->eFileLock = eFileLock;
40658 ** the requested locking level, this routine is a no-op.
40662 sem_t *pSem = pFile->pInode->pSem;
40666 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
40667 pFile->eFileLock, osGetpid(0)));
40670 /* no-op if possible */
40671 if( pFile->eFileLock==eFileLock ){
40677 pFile->eFileLock = eFileLock;
40682 if ( sem_post(pSem)==-1 ) {
40690 pFile->eFileLock = NO_LOCK;
40723 ** on Apple Macintosh computers - both OS9 and OSX.
40725 ** Third-party implementations of AFP are available. But this code here
40752 ** This is a utility for setting or clearing a bit-range lock on an
40771 pb.fd = pFile->h;
40774 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
40777 if ( err==-1 ) {
40800 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40812 context = (afpLockingContext *) pFile->lockingContext;
40813 if( context->reserved ){
40817 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
40819 if( pFile->pInode->eFileLock>SHARED_LOCK ){
40827 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40831 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40841 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
40842 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
40849 ** Lock the file with the lock specified by parameter eFileLock - one
40863 ** UNLOCKED -> SHARED
40864 ** SHARED -> RESERVED
40865 ** SHARED -> (PENDING) -> EXCLUSIVE
40866 ** RESERVED -> (PENDING) -> EXCLUSIVE
40867 ** PENDING -> EXCLUSIVE
40875 unixInodeInfo *pInode = pFile->pInode;
40876 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40879 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
40880 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40881 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
40887 if( pFile->eFileLock>=eFileLock ){
40888 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
40898 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
40900 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
40902 /* This mutex is needed because pFile->pInode is shared across threads
40904 pInode = pFile->pInode;
40905 sqlite3_mutex_enter(pInode->pLockMutex);
40910 if( (pFile->eFileLock!=pInode->eFileLock &&
40911 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
40922 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
40924 assert( pFile->eFileLock==0 );
40925 assert( pInode->nShared>0 );
40926 pFile->eFileLock = SHARED_LOCK;
40927 pInode->nShared++;
40928 pInode->nLock++;
40937 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
40940 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
40954 assert( pInode->nShared==0 );
40955 assert( pInode->eFileLock==0 );
40958 /* Now get the read-lock SHARED_LOCK */
40961 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
40962 lrc1 = afpSetLock(context->dbPath, pFile,
40963 SHARED_FIRST+pInode->sharedByte, 1, 1);
40965 lrc1Errno = pFile->lastErrno;
40968 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40980 pFile->eFileLock = SHARED_LOCK;
40981 pInode->nLock++;
40982 pInode->nShared = 1;
40984 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40994 assert( 0!=pFile->eFileLock );
40995 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
40997 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40999 context->reserved = 1;
41008 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
41009 pInode->sharedByte, 1, 0)) ){
41012 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
41014 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
41015 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
41033 pFile->eFileLock = eFileLock;
41034 pInode->eFileLock = eFileLock;
41036 pFile->eFileLock = PENDING_LOCK;
41037 pInode->eFileLock = PENDING_LOCK;
41041 sqlite3_mutex_leave(pInode->pLockMutex);
41042 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
41052 ** the requested locking level, this routine is a no-op.
41058 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41062 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
41063 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
41067 if( pFile->eFileLock<=eFileLock ){
41070 pInode = pFile->pInode;
41071 sqlite3_mutex_enter(pInode->pLockMutex);
41072 assert( pInode->nShared!=0 );
41073 if( pFile->eFileLock>SHARED_LOCK ){
41074 assert( pInode->eFileLock==pFile->eFileLock );
41085 assert( pFile->inNormalWrite==0
41086 || pFile->dbUpdate==0
41087 || pFile->transCntrChng==1 );
41088 pFile->inNormalWrite = 0;
41091 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
41092 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
41093 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
41094 /* only re-establish the shared lock if necessary */
41095 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41096 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
41101 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
41102 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
41104 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
41105 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
41107 context->reserved = 0;
41110 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
41111 pInode->eFileLock = SHARED_LOCK;
41120 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41121 pInode->nShared--;
41122 if( pInode->nShared==0 ){
41124 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
41127 pInode->eFileLock = NO_LOCK;
41128 pFile->eFileLock = NO_LOCK;
41132 pInode->nLock--;
41133 assert( pInode->nLock>=0 );
41134 if( pInode->nLock==0 ) closePendingFds(pFile);
41138 sqlite3_mutex_leave(pInode->pLockMutex);
41140 pFile->eFileLock = eFileLock;
41155 if( pFile->pInode ){
41156 unixInodeInfo *pInode = pFile->pInode;
41157 sqlite3_mutex_enter(pInode->pLockMutex);
41158 if( pInode->nLock ){
41161 ** descriptor to pInode->aPending. It will be automatically closed when
41166 sqlite3_mutex_leave(pInode->pLockMutex);
41169 sqlite3_free(pFile->lockingContext);
41179 ** is available. If you don't compile for a mac, then the "unix-afp"
41194 ** the requested locking level, this routine is a no-op.
41210 **************** Non-locking sqlite3_file methods *****************************
41234 assert( id->h>2 );
41237 got = osPread(id->h, pBuf, cnt, offset);
41238 SimulateIOError( got = -1 );
41240 got = osPread64(id->h, pBuf, cnt, offset);
41241 SimulateIOError( got = -1 );
41243 newOffset = lseek(id->h, offset, SEEK_SET);
41244 SimulateIOError( newOffset = -1 );
41247 return -1;
41249 got = osRead(id->h, pBuf, cnt);
41258 cnt -= got;
41265 OSTRACE(("READ %-3d %5d %7lld %llu\n",
41266 id->h, got+prior, offset-prior, TIMER_ELAPSED));
41287 /* If this is a database file (not a journal, super-journal or temp
41290 assert( pFile->pPreallocatedUnused==0
41299 if( offset<pFile->mmapSize ){
41300 if( offset+amt <= pFile->mmapSize ){
41301 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
41304 int nCopy = pFile->mmapSize - offset;
41305 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
41307 amt -= nCopy;
41317 /* pFile->lastErrno has been set by seekAndRead().
41324 switch( pFile->lastErrno ){
41338 /* Unread parts of the buffer must be zero-filled */
41339 memset(&((char*)pBuf)[got], 0, amt-got);
41345 ** Attempt to seek the file-descriptor passed as the first argument to
41347 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
41373 SimulateIOError( iSeek = -1 );
41375 rc = -1;
41383 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
41391 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
41398 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
41417 /* If this is a database file (not a journal, super-journal or temp
41420 assert( pFile->pPreallocatedUnused==0
41428 ** doing a hot-journal rollback or a write to some file other than a
41433 if( pFile->inNormalWrite ){
41434 pFile->dbUpdate = 1; /* The database has been modified */
41441 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
41442 pFile->transCntrChng = 1; /* The transaction counter has changed */
41451 if( offset<pFile->mmapSize ){
41452 if( offset+amt <= pFile->mmapSize ){
41453 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
41456 int nCopy = pFile->mmapSize - offset;
41457 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
41459 amt -= nCopy;
41466 amt -= wrote;
41470 SimulateIOError(( wrote=(-1), amt=1 ));
41474 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
41499 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
41569 ** no-op. But go ahead and call fstat() to validate the file
41602 if( rc==-1 && errno==ENOTSUP ){
41608 if( OS_VXWORKS && rc!= -1 ){
41621 ** The directory file descriptor is used for only one thing - to
41630 ** replace this routine with a harmless no-op. To make this routine
41631 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
41639 int fd = -1;
41643 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
41652 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
41667 ** has been created by fsync-ing the directory that contains the file.
41672 ** will not roll back - possibly leading to database corruption.
41692 OSTRACE(("SYNC %-3d\n", pFile->h));
41693 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
41697 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
41701 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
41704 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
41706 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
41708 rc = osOpenDirectory(pFile->zPath, &dirfd);
41716 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
41730 /* If the user has configured a chunk-size for this file, truncate the
41735 if( pFile->szChunk>0 ){
41736 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
41739 rc = robust_ftruncate(pFile->h, nByte);
41742 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41746 ** doing a hot-journal rollback or a write to some file other than a
41749 ** when restoring a database using the backup API from a zero-length
41752 if( pFile->inNormalWrite && nByte==0 ){
41753 pFile->transCntrChng = 1;
41762 if( nByte<pFile->mmapSize ){
41763 pFile->mmapSize = nByte;
41778 rc = osFstat(((unixFile*)id)->h, &buf);
41782 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", ((unixFile*)id)->zPath);
41786 /* When opening a zero-size database, the findInodeInfo() procedure
41788 ** in the OS-X msdos filesystem. In order to avoid problems with upper
41800 ** Handler for proxy-locking file-control verbs. Defined below in the
41808 ** file-control operation. Enlarge the database to nBytes in size
41809 ** (rounded up to the next chunk-size). If the database is already
41810 ** nBytes or larger, this routine is a no-op.
41813 if( pFile->szChunk>0 ){
41817 if( osFstat(pFile->h, &buf) ){
41818 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", pFile->zPath);
41821 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
41830 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
41837 ** at offset (nSize-1), to set the size of the file correctly.
41841 int nBlk = buf.st_blksize; /* File-system block size */
41845 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
41848 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
41849 if( iWrite>=nSize ) iWrite = nSize - 1;
41858 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
41860 if( pFile->szChunk<=0 ){
41861 if( robust_ftruncate(pFile->h, nByte) ){
41863 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41877 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
41879 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
41883 *pArg = (pFile->ctrlFlags & mask)!=0;
41885 pFile->ctrlFlags &= ~mask;
41887 pFile->ctrlFlags |= mask;
41905 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
41909 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
41913 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
41919 *(int*)pArg = pFile->eFileLock;
41923 *(int*)pArg = pFile->lastErrno;
41927 pFile->szChunk = *(int *)pArg;
41946 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
41950 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
41952 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
41963 int iOld = pFile->iBusyTimeout;
41964 pFile->iBusyTimeout = *(int*)pArg;
41979 ** 64-bit type. */
41984 *(i64*)pArg = pFile->mmapSizeMax;
41985 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
41986 pFile->mmapSizeMax = newLimit;
41987 if( pFile->mmapSize>0 ){
41989 rc = unixMapfile(pFile, -1);
42002 ((unixFile*)id)->dbUpdate = 0;
42026 ** If pFd->sectorSize is non-zero when this function is called, it is a
42027 ** no-op. Otherwise, the values of pFd->sectorSize and
42028 ** pFd->deviceCharacteristics are set according to the file-system
42036 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
42037 if( pFd->sectorSize==0 ){
42043 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
42045 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
42050 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
42051 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
42054 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42061 if( pFile->sectorSize == 0 ){
42064 /* Set defaults for non-supported filesystems */
42065 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42066 pFile->deviceCharacteristics = 0;
42067 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
42072 pFile->sectorSize = fsInfo.f_bsize;
42073 pFile->deviceCharacteristics =
42081 pFile->sectorSize = fsInfo.f_bsize;
42082 pFile->deviceCharacteristics =
42084 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
42091 pFile->sectorSize = fsInfo.f_bsize;
42092 pFile->deviceCharacteristics =
42100 pFile->sectorSize = fsInfo.f_bsize;
42101 pFile->deviceCharacteristics =
42103 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42108 pFile->sectorSize = fsInfo.f_bsize;
42109 pFile->deviceCharacteristics =
42111 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
42116 pFile->deviceCharacteristics =
42125 if( pFile->sectorSize % 512 != 0 ){
42126 pFile->deviceCharacteristics = 0;
42127 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
42138 ** if two files are created in the same file-system directory (i.e.
42145 return pFd->sectorSize;
42154 ** words, after a power-loss event, parts of the file that were never
42155 ** written might end up being altered.) However, non-PSOW behavior is very,
42158 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
42164 return pFd->deviceCharacteristics;
42192 ** When multiple threads all reference the same wal-index, each thread
42194 ** of this unixShmNode object. In other words, each wal-index is opened
42209 ** The following fields are read-only after the object is created:
42223 int szRegion; /* Size of shared-memory regions */
42225 u8 isReadonly; /* True if read-only */
42227 char **apRegion; /* Array of mapped shared-memory regions */
42230 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
42243 ** are read-only thereafter:
42248 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
42254 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
42268 ** wal-mode transactions in other processes on database file pFile. If
42277 if( pFile->pShm){
42278 unixShmNode *pShmNode = pFile->pShm->pShmNode;
42285 f.l_len = SQLITE_SHM_NLOCK - 3;
42287 sqlite3_mutex_enter(pShmNode->pShmMutex);
42288 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
42293 sqlite3_mutex_leave(pShmNode->pShmMutex);
42301 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
42303 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
42312 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
42317 pShmNode = pFile->pInode->pShmNode;
42318 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42319 assert( pShmNode->nRef>0 || unixMutexHeld() );
42327 if( pShmNode->hShm>=0 ){
42334 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
42335 if( res==-1 ){
42337 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
42347 OSTRACE(("SHM-LOCK "));
42348 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
42352 pShmNode->exclMask &= ~mask;
42353 pShmNode->sharedMask &= ~mask;
42355 OSTRACE(("read-lock %d ok", ofst));
42356 pShmNode->exclMask &= ~mask;
42357 pShmNode->sharedMask |= mask;
42360 OSTRACE(("write-lock %d ok", ofst));
42361 pShmNode->exclMask |= mask;
42362 pShmNode->sharedMask &= ~mask;
42368 OSTRACE(("read-lock failed"));
42371 OSTRACE(("write-lock %d failed", ofst));
42374 OSTRACE((" - afterwards %03x,%03x\n",
42375 pShmNode->sharedMask, pShmNode->exclMask));
42385 ** current system page-size.
42388 ** to use 64KB pages - in this case each mapping must cover at least two
42394 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
42402 ** This is not a VFS shared-memory method; it is a utility function called
42403 ** by VFS shared-memory methods.
42406 unixShmNode *p = pFd->pInode->pShmNode;
42408 if( p && ALWAYS(p->nRef==0) ){
42411 assert( p->pInode==pFd->pInode );
42412 sqlite3_mutex_free(p->pShmMutex);
42413 for(i=0; i<p->nRegion; i+=nShmPerMap){
42414 if( p->hShm>=0 ){
42415 osMunmap(p->apRegion[i], p->szRegion);
42417 sqlite3_free(p->apRegion[i]);
42420 sqlite3_free(p->apRegion);
42421 if( p->hShm>=0 ){
42422 robust_close(pFd, p->hShm, __LINE__);
42423 p->hShm = -1;
42425 p->pInode->pShmNode = 0;
42437 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
42446 ** and proceed with opening the *-shm file.
42450 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
42457 ** EXCLUSIVE failed just before truncating the *-shm file, then this
42458 ** process might open and use the *-shm file without truncating it.
42459 ** And if the *-shm file has been corrupted by a power failure or
42465 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
42468 if( pShmNode->isReadonly ){
42469 pShmNode->isUnlocked = 1;
42473 /* The first connection to attach must truncate the -shm file. We
42475 ** -shm header size) rather than 0 as a system debugging aid, to
42476 ** help detect if a -shm file truncation is legitimate or is the work
42478 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
42479 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
42494 ** Open a shared-memory area associated with open database file pDbFd.
42497 ** The file used to implement shared-memory is in the same directory
42499 ** file with the "-shm" suffix added. For example, if the database file
42501 ** for shared memory will be called "/home/user1/config.db-shm".
42507 ** database to end up using different files for shared memory -
42508 ** meaning that their memory would not really be shared - resulting
42510 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
42511 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
42515 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
42518 ** When opening a new shared-memory file, if no other instances of that
42522 ** If the original database file (pDbFd) is using the "unix-excl" VFS
42540 assert( pDbFd->pShm==0 );
42547 pInode = pDbFd->pInode;
42548 pShmNode = pInode->pShmNode;
42552 const char *zBasePath = pDbFd->zPath;
42556 ** a new *-shm file is created, an attempt will be made to create it
42559 if( osFstat(pDbFd->h, &sStat) ){
42560 rc = unixLogError(SQLITE_IOERR_FSTAT, "fstat", pDbFd->zPath);
42575 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
42578 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
42581 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
42582 sqlite3FileSuffix3(pDbFd->zPath, zShm);
42584 pShmNode->hShm = -1;
42585 pDbFd->pInode->pShmNode = pShmNode;
42586 pShmNode->pInode = pDbFd->pInode;
42588 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
42589 if( pShmNode->pShmMutex==0 ){
42595 if( pInode->bProcessLock==0 ){
42596 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
42597 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
42600 if( pShmNode->hShm<0 ){
42601 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
42603 if( pShmNode->hShm<0 ){
42607 pShmNode->isReadonly = 1;
42614 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
42622 p->pShmNode = pShmNode;
42624 p->id = pShmNode->nextShmId++;
42626 pShmNode->nRef++;
42627 pDbFd->pShm = p;
42634 ** at pShmNode->pFirst. This must be done while holding the
42635 ** pShmNode->pShmMutex.
42637 sqlite3_mutex_enter(pShmNode->pShmMutex);
42638 p->pNext = pShmNode->pFirst;
42639 pShmNode->pFirst = p;
42640 sqlite3_mutex_leave(pShmNode->pShmMutex);
42653 ** shared-memory associated with the database file fd. Shared-memory regions
42654 ** are numbered starting from zero. Each shared-memory region is szRegion
42659 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
42662 ** bExtend is non-zero and the requested shared-memory region has not yet
42665 ** If the shared-memory region has already been allocated or is allocated by
42684 /* If the shared-memory file has not yet been opened, open it now. */
42685 if( pDbFd->pShm==0 ){
42690 p = pDbFd->pShm;
42691 pShmNode = p->pShmNode;
42692 sqlite3_mutex_enter(pShmNode->pShmMutex);
42693 if( pShmNode->isUnlocked ){
42696 pShmNode->isUnlocked = 0;
42698 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
42699 assert( pShmNode->pInode==pDbFd->pInode );
42700 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
42701 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42706 if( pShmNode->nRegion<nReqRegion ){
42711 pShmNode->szRegion = szRegion;
42713 if( pShmNode->hShm>=0 ){
42715 ** Check to see if it has been allocated (i.e. if the wal-index file is
42718 if( osFstat(pShmNode->hShm, &sStat) ){
42746 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
42747 const char *zFile = pShmNode->zFilename;
42758 pShmNode->apRegion, nReqRegion*sizeof(char *)
42764 pShmNode->apRegion = apNew;
42765 while( pShmNode->nRegion<nReqRegion ){
42769 if( pShmNode->hShm>=0 ){
42771 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
42772 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
42775 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
42788 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
42790 pShmNode->nRegion += nShmPerMap;
42795 if( pShmNode->nRegion>iRegion ){
42796 *pp = pShmNode->apRegion[iRegion];
42800 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
42801 sqlite3_mutex_leave(pShmNode->pShmMutex);
42806 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
42816 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42819 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42822 if( pX->exclMask & (1<<i) ){
42824 aLock[i] = -1;
42825 }else if( pX->sharedMask & (1<<i) ){
42832 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
42833 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
42838 ** Change the lock state for a shared-memory segment.
42858 p = pDbFd->pShm;
42860 pShmNode = p->pShmNode;
42862 aLock = pShmNode->aLock;
42864 assert( pShmNode==pDbFd->pInode->pShmNode );
42865 assert( pShmNode->pInode==pDbFd->pInode );
42873 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
42874 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42890 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
42892 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
42893 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
42894 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
42898 mask = (1<<(ofst+n)) - (1<<ofst);
42900 sqlite3_mutex_enter(pShmNode->pShmMutex);
42903 if( (p->exclMask|p->sharedMask) & mask ){
42908 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
42918 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
42920 aLock[ofst]--;
42925 p->exclMask &= ~mask;
42926 p->sharedMask &= ~mask;
42931 assert( (p->exclMask & (1<<ofst))==0 );
42932 if( (p->sharedMask & mask)==0 ){
42941 p->sharedMask |= mask;
42950 assert( (p->sharedMask & mask)==0 );
42951 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
42958 ** also update the in-memory values. */
42962 assert( (p->sharedMask & mask)==0 );
42963 p->exclMask |= mask;
42965 aLock[ii] = -1;
42971 sqlite3_mutex_leave(pShmNode->pShmMutex);
42972 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
42973 p->id, osGetpid(0), p->sharedMask, p->exclMask));
42987 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42988 assert( fd->pMethods->xLock==nolockLock
42996 ** Close a connection to shared-memory. Delete the underlying
43000 ** routine is a harmless no-op.
43004 int deleteFlag /* Delete shared-memory if true */
43007 unixShmNode *pShmNode; /* The underlying shared-memory file */
43012 p = pDbFd->pShm;
43014 pShmNode = p->pShmNode;
43016 assert( pShmNode==pDbFd->pInode->pShmNode );
43017 assert( pShmNode->pInode==pDbFd->pInode );
43021 sqlite3_mutex_enter(pShmNode->pShmMutex);
43022 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
43023 *pp = p->pNext;
43027 pDbFd->pShm = 0;
43028 sqlite3_mutex_leave(pShmNode->pShmMutex);
43030 /* If pShmNode->nRef has reached 0, then close the underlying
43031 ** shared-memory file, too */
43034 assert( pShmNode->nRef>0 );
43035 pShmNode->nRef--;
43036 if( pShmNode->nRef==0 ){
43037 if( deleteFlag && pShmNode->hShm>=0 ){
43038 osUnlink(pShmNode->zFilename);
43060 assert( pFd->nFetchOut==0 );
43061 if( pFd->pMapRegion ){
43062 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
43063 pFd->pMapRegion = 0;
43064 pFd->mmapSize = 0;
43065 pFd->mmapSizeActual = 0;
43089 int h = pFd->h; /* File descriptor open on db file */
43090 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
43091 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
43095 assert( pFd->nFetchOut==0 );
43096 assert( nNew>pFd->mmapSize );
43097 assert( nNew<=pFd->mmapSizeMax );
43099 assert( pFd->mmapSizeActual>=pFd->mmapSize );
43103 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
43108 i64 nReuse = pFd->mmapSize;
43111 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
43117 osMunmap(pReq, nOrig-nReuse);
43124 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
43127 osMunmap(pNew, nNew - nReuse);
43149 unixLogError(SQLITE_OK, zErr, pFd->zPath);
43154 pFd->mmapSizeMax = 0;
43156 pFd->pMapRegion = (void *)pNew;
43157 pFd->mmapSize = pFd->mmapSizeActual = nNew;
43161 ** Memory map or remap the file opened by file-descriptor pFd (if the file
43164 ** outstanding xFetch() references to it, this function is a no-op.
43166 ** If parameter nByte is non-negative, then it is the requested size of
43177 assert( nMap>=0 || pFd->nFetchOut==0 );
43178 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43179 if( pFd->nFetchOut>0 ) return SQLITE_OK;
43182 struct stat statbuf; /* Low-level file information */
43183 if( osFstat(pFd->h, &statbuf) ){
43188 if( nMap>pFd->mmapSizeMax ){
43189 nMap = pFd->mmapSizeMax;
43192 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
43193 if( nMap!=pFd->mmapSize ){
43220 if( pFd->mmapSizeMax>0 ){
43226 if( pFd->pMapRegion==0 ){
43227 int rc = unixMapfile(pFd, -1);
43230 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
43231 *pp = &((u8 *)pFd->pMapRegion)[iOff];
43232 pFd->nFetchOut++;
43240 ** If the third argument is non-NULL, then this function releases a
43257 assert( (p==0)==(pFd->nFetchOut==0) );
43260 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
43263 pFd->nFetchOut--;
43268 assert( pFd->nFetchOut>=0 );
43286 ** of "finder" functions. A finder-function is used to locate the appropriate
43289 ** the correct finder-function for that VFS.
43292 ** object. The only interesting finder-function is autolockIoFinder, which
43296 ** For finder-function F, two objects are created:
43298 ** (1) The real finder-function named "FImpt()".
43305 ** directly at the finder-function since C90 rules prevent a void*
43422 ** The proxy locking method is a "super-method" in the sense that it
43424 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
43493 if( statfs(filePath, &fsInfo) != -1 ){
43505 ** Test byte-range lock using fcntl(). If the call succeeds,
43506 ** assume that the file-system supports POSIX style locks.
43512 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
43552 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
43590 assert( pNew->pInode==NULL );
43595 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
43596 pNew->h = h;
43597 pNew->pVfs = pVfs;
43598 pNew->zPath = zFilename;
43599 pNew->ctrlFlags = (u8)ctrlFlags;
43601 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
43605 pNew->ctrlFlags |= UNIXFILE_PSOW;
43607 if( strcmp(pVfs->zName,"unix-excl")==0 ){
43608 pNew->ctrlFlags |= UNIXFILE_EXCL;
43612 pNew->pId = vxworksFindFileId(zFilename);
43613 if( pNew->pId==0 ){
43622 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
43627 pNew->lockingContext = (void*)zFilename;
43637 rc = findInodeInfo(pNew, &pNew->pInode);
43650 ** handle h - as it is guaranteed that no posix locks will be released
43658 h = -1;
43669 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
43676 pCtx->dbPath = zFilename;
43677 pCtx->reserved = 0;
43680 rc = findInodeInfo(pNew, &pNew->pInode);
43682 sqlite3_free(pNew->lockingContext);
43684 h = -1;
43705 pNew->lockingContext = zLockFile;
43714 rc = findInodeInfo(pNew, &pNew->pInode);
43715 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
43716 char *zSemName = pNew->pInode->aSemName;
43719 pNew->pId->zCanonicalName);
43722 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
43723 if( pNew->pInode->pSem == SEM_FAILED ){
43725 pNew->pInode->aSemName[0] = '\0';
43736 h = -1;
43738 pNew->ctrlFlags |= UNIXFILE_DELETE;
43744 pId->pMethods = pLockingStyle;
43797 ** pVfs->mxPathname bytes.
43804 /* It's odd to simulate an io-error here, but really this is just
43805 ** using the io-error infrastructure to test that SQLite handles this
43820 zBuf[nBuf-2] = 0;
43823 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
43835 ** Routine to transform a unixFile into a proxy-locking unixFile.
43836 ** Implementation in the proxy-lock division, but used by unixOpen()
43844 ** file (not a journal or super-journal file) identified by pathname
43850 ** other file descriptor open on the same file is holding a file-lock.
43856 ** such file descriptor is located, -1 is returned.
43874 ** ignored and -1 is returned. The caller will try to open a new file
43883 while( pInode && (pInode->fileId.dev!=sStat.st_dev
43884 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
43885 pInode = pInode->pNext;
43889 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
43890 sqlite3_mutex_enter(pInode->pLockMutex);
43892 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
43895 *pp = pUnused->pNext;
43897 sqlite3_mutex_leave(pInode->pLockMutex);
43937 ** this function queries the file-system for the permissions on the
43966 ** "<path to db>-journal"
43967 ** "<path to db>-wal"
43968 ** "<path to db>-journalNN"
43969 ** "<path to db>-walNN"
43975 ** a '-' character. However in 8+3 filename mode, or if a corrupt
43976 ** rollback journal specifies a super-journal with a goofy name, then
43977 ** the '-' might be missing or the '-' might be the first character in
43980 nDb = sqlite3Strlen30(zPath) - 1;
43982 if( zPath[nDb]=='-' ){
43988 nDb--;
44017 ** ReadWrite() -> (READWRITE | CREATE)
44018 ** ReadOnly() -> (READONLY)
44019 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
44021 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
44035 int fd = -1; /* File descriptor returned by open() */
44054 /* If creating a super- or main-file journal, this function will open
44055 ** a file-descriptor on the directory too. The first time unixSync()
44082 /* The main DB, main journal, WAL file and super-journal are never
44089 /* Assert that the upper layer has set one of the "file-type" flags. */
44116 fd = pUnused->fd;
44123 p->pPreallocatedUnused = pUnused;
44125 /* Database filenames are double-zero terminated if they are not
44133 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
44139 /* Generated temporary filenames are always double-zero terminated
44160 assert( !p->pPreallocatedUnused );
44165 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
44173 /* Failed to open the file for read/write access. Try read-only. */
44190 ** the case. The chown() system call will be a no-op if the current
44209 if( p->pPreallocatedUnused ){
44210 p->pPreallocatedUnused->fd = fd;
44211 p->pPreallocatedUnused->flags =
44230 p->openFlags = openFlags;
44235 if( fstatfs(fd, &fsInfo) == -1 ){
44241 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44244 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
44260 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
44265 ** never use proxy, NULL means use proxy for non-local files only. */
44278 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
44296 sqlite3_free(p->pPreallocatedUnused);
44314 if( osUnlink(zPath)==(-1) ){
44383 int rc; /* Non-zero following any error */
44398 const char *zName, /* Name to append to pPath. Not zero-terminated */
44406 if( pPath->nUsed>1 ){
44407 assert( pPath->zOut[0]=='/' );
44408 while( pPath->zOut[--pPath->nUsed]!='/' ){}
44413 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
44414 pPath->rc = SQLITE_ERROR;
44417 pPath->zOut[pPath->nUsed++] = '/';
44418 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
44419 pPath->nUsed += nName;
44421 if( pPath->rc==SQLITE_OK ){
44424 pPath->zOut[pPath->nUsed] = 0;
44425 zIn = pPath->zOut;
44428 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
44433 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
44434 pPath->rc = SQLITE_CANTOPEN_BKPT;
44437 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
44438 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
44439 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
44444 pPath->nUsed = 0;
44446 pPath->nUsed -= nName + 1;
44459 const char *zPath /* Path to append to pPath. Is zero-terminated */
44466 appendOnePathElement(pPath, &zPath[j], i-j);
44474 ** is stored as a nul-terminated string in the buffer pointed to by
44478 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
44496 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
44538 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
44541 ** use dlsym() with -pedantic-errors?
44549 ** This work-around is unlikely to work correctly on any system where
44578 ** errors. The reports issued by valgrind are incorrect - we would
44580 ** uninitialized space in zBuf - but valgrind errors tend to worry
44628 ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
44648 ** The following variable, if set to a non-zero value, is interpreted as
44712 ** low-level error message when operating-system problems come up
44731 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
44733 ** meta-layer over top of the primitive locking implemented above. For
44736 ** been defined - so that the primitive locking methods are available
44741 ** The default locking schemes in SQLite use byte-range locks on the
44746 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
44754 ** SHARED_RANGE 0x40000002 -> 0x40000200
44758 ** the read cache when byte-range locks are present. Enabling the read
44761 ** close-to-open semantics for ensuring cache coherency
44765 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
44774 ** -----------------
44806 ** -----------------------
44816 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
44817 ** by taking an sqlite-style shared lock on the conch file, reading the
44821 ** is patterned after the database file name as ".<databasename>-conch".
44829 ** The proxy file - a single-byte file used for all advisory file locks
44845 ** ---------------------
44849 ** Database files accessed on non-local file systems are
44894 int conchHeld; /* 1 if the conch is held, -1 if lockless */
44927 if( lPath[len-1]!='/' ){
44956 if( lockPath[i] == '/' && (i - start > 0) ){
44958 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
44959 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
44991 int fd = -1;
45000 ** 2. if that fails, and this is a lock file (not-conch), try creating
45002 ** 3. if that fails, try to open the file read-only
45007 fd = pUnused->fd;
45048 pNew->openFlags = openFlags;
45052 pUnused->fd = fd;
45053 pUnused->flags = openFlags;
45054 pNew->pPreallocatedUnused = pUnused;
45112 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
45124 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45125 unixFile *conchFile = pCtx->conchFile;
45128 char *cPath = pCtx->conchFilePath;
45132 int fd = -1;
45133 int rc = -1;
45136 /* create a new path by replace the trailing '-conch' with '-break' */
45139 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
45144 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
45165 robust_close(pFile, conchFile->h, __LINE__);
45166 conchFile->h = fd;
45167 conchFile->openFlags = O_RDWR | O_CREAT;
45184 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45185 unixFile *conchFile = pCtx->conchFile;
45192 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45202 if( osFstat(conchFile->h, &buf) ){
45221 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
45243 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
45246 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
45256 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
45262 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45264 if( pCtx->conchHeld!=0 ){
45267 unixFile *conchFile = pCtx->conchFile;
45280 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
45281 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45297 storeLastErrno(pFile, conchFile->lastErrno);
45309 ** retry with a new auto-generated path
45317 if( !pCtx->lockProxyPath ){
45318 /* for auto-named local lock file, just check the host ID and we'll
45322 size_t pathLen = (readLen - PROXY_PATHINDEX);
45325 pathLen=MAXPATHLEN-1;
45335 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
45336 readLen-PROXY_PATHINDEX)
45344 if( (conchFile->openFlags&O_RDWR) == 0 ){
45350 if( !pCtx->lockProxyPath ){
45351 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
45360 futimes(conchFile->h, NULL);
45362 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
45378 if( pCtx->lockProxyPath!=NULL ){
45379 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
45385 robust_ftruncate(conchFile->h, writeSize);
45387 full_fsync(conchFile->h,0,0);
45393 int err = osFstat(pFile->h, &buf);
45399 osFchmod(conchFile->h, cmode);
45402 rc = osFchmod(conchFile->h, cmode);
45403 }while( rc==(-1) && errno==EINTR );
45419 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
45422 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
45423 if( rc==SQLITE_OK && pFile->openFlags ){
45425 if( pFile->h>=0 ){
45426 robust_close(pFile, pFile->h, __LINE__);
45428 pFile->h = -1;
45429 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
45432 pFile->h = fd;
45438 if( rc==SQLITE_OK && !pCtx->lockProxy ){
45439 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
45440 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
45443 ** so try again via auto-naming
45455 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
45456 if( !pCtx->lockProxyPath ){
45462 pCtx->conchHeld = 1;
45464 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
45466 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
45467 afpCtx->dbPath = pCtx->lockProxyPath;
45470 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
45472 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
45475 } while (1); /* in case we need to retry the :auto: lock file -
45488 pCtx = (proxyLockingContext *)pFile->lockingContext;
45489 conchFile = pCtx->conchFile;
45490 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
45491 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
45493 if( pCtx->conchHeld>0 ){
45494 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
45496 pCtx->conchHeld = 0;
45497 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
45515 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
45527 for( i=(len-1); i>=0; i-- ){
45539 /* append the "-conch" suffix to the file */
45540 memcpy(&conchPath[i+1], "-conch", 7);
45547 /* Takes a fully configured proxy locking-style unix file and switches
45551 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
45552 char *oldPath = pCtx->lockProxyPath;
45555 if( pFile->eFileLock!=NO_LOCK ){
45564 unixFile *lockProxy = pCtx->lockProxy;
45565 pCtx->lockProxy=NULL;
45566 pCtx->conchHeld = 0;
45568 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
45573 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
45588 if( pFile->pMethod == &afpIoMethods ){
45591 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
45592 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
45596 if( pFile->pMethod == &dotlockIoMethods ){
45599 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
45600 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
45603 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
45604 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
45614 ** ->lockingContext
45615 ** ->pMethod
45623 if( pFile->eFileLock!=NO_LOCK ){
45633 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
45642 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
45644 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
45645 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
45647 ** (c) the file system is read-only, then enable no-locking access.
45655 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
45657 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
45662 pCtx->conchHeld = -1; /* read only FS/ lockless */
45668 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
45672 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
45673 if( pCtx->dbPath==NULL ){
45681 pCtx->oldLockingContext = pFile->lockingContext;
45682 pFile->lockingContext = pCtx;
45683 pCtx->pOldMethod = pFile->pMethod;
45684 pFile->pMethod = &proxyIoMethods;
45686 if( pCtx->conchFile ){
45687 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
45688 sqlite3_free(pCtx->conchFile);
45690 sqlite3DbFree(0, pCtx->lockProxyPath);
45691 sqlite3_free(pCtx->conchFilePath);
45694 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
45708 if( pFile->pMethod == &proxyIoMethods ){
45709 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
45711 if( pCtx->lockProxyPath ){
45712 *(const char **)pArg = pCtx->lockProxyPath;
45724 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
45727 /* turn off proxy locking - not supported. If support is added for
45733 /* turn off proxy locking - already off - NOOP */
45740 (proxyLockingContext*)pFile->lockingContext;
45742 || (pCtx->lockProxyPath &&
45743 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
45766 ** above this point are all utilities. The lock-related methods of the
45767 ** proxy-locking sqlite3_io_method object follow.
45774 ** to a non-zero value otherwise *pResOut is set to zero. The return value
45781 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45782 if( pCtx->conchHeld>0 ){
45783 unixFile *proxy = pCtx->lockProxy;
45784 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
45793 ** Lock the file with the lock specified by parameter eFileLock - one
45807 ** UNLOCKED -> SHARED
45808 ** SHARED -> RESERVED
45809 ** SHARED -> (PENDING) -> EXCLUSIVE
45810 ** RESERVED -> (PENDING) -> EXCLUSIVE
45811 ** PENDING -> EXCLUSIVE
45820 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45821 if( pCtx->conchHeld>0 ){
45822 unixFile *proxy = pCtx->lockProxy;
45823 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
45824 pFile->eFileLock = proxy->eFileLock;
45838 ** the requested locking level, this routine is a no-op.
45844 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45845 if( pCtx->conchHeld>0 ){
45846 unixFile *proxy = pCtx->lockProxy;
45847 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
45848 pFile->eFileLock = proxy->eFileLock;
45862 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45863 unixFile *lockProxy = pCtx->lockProxy;
45864 unixFile *conchFile = pCtx->conchFile;
45868 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
45870 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
45873 pCtx->lockProxy = 0;
45876 if( pCtx->conchHeld ){
45880 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
45884 sqlite3DbFree(0, pCtx->lockProxyPath);
45885 sqlite3_free(pCtx->conchFilePath);
45886 sqlite3DbFree(0, pCtx->dbPath);
45888 pFile->lockingContext = pCtx->oldLockingContext;
45889 pFile->pMethod = pCtx->pOldMethod;
45891 return pFile->pMethod->xClose(id);
45911 ** This routine registers all VFS implementations for unix-like operating
45928 ** when compiling with -pedantic-errors on GCC.)
45931 ** finder-function. The finder-function returns a pointer to the
45934 ** macro for addition information on finder-functions.
45982 UNIXVFS("unix-none", nolockIoFinder ),
45983 UNIXVFS("unix-dotfile", dotlockIoFinder ),
45984 UNIXVFS("unix-excl", posixIoFinder ),
45986 UNIXVFS("unix-namedsem", semIoFinder ),
45989 UNIXVFS("unix-posix", posixIoFinder ),
45992 UNIXVFS("unix-flock", flockIoFinder ),
45995 UNIXVFS("unix-afp", afpIoFinder ),
45996 UNIXVFS("unix-nfs", nfsIoFinder ),
45997 UNIXVFS("unix-proxy", proxyIoFinder ),
46002 /* Double-check that the aSyscall[] array has been constructed
46028 ** READ-0 UNIX_SHM_BASE+3 123
46029 ** READ-1 UNIX_SHM_BASE+4 124
46030 ** READ-2 UNIX_SHM_BASE+5 125
46031 ** READ-3 UNIX_SHM_BASE+6 126
46032 ** READ-4 UNIX_SHM_BASE+7 127
46035 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
46049 ** This routine is a no-op for unix.
46103 ** based on the sub-platform)?
46111 ** based on the sub-platform)?
46199 ** characters, so we allocate 4 bytes per character assuming worst-case of
46200 ** 4-bytes-per-character for UTF8.
46223 ** Returns non-zero if the character should be treated as a directory
46287 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
46300 typedef struct winShm winShm; /* A connection to shared-memory */
46301 typedef struct winShmNode winShmNode; /* A region of shared-memory */
46359 BOOL bNoLock; /* Non-zero if locking is disabled. */
46373 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
46383 * If this is non-zero, an isolated heap will be created by the native Win32
46389 * WARNING: It is important to note that when this setting is non-zero and the
46401 * This is the maximum possible initial size of the Win32-specific heap, in
46409 * This is the extra space for the initial size of the Win32-specific heap,
46422 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
46429 * Win32-specific heap. It cannot be negative.
46435 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
46441 * initial size of the Win32-specific heap to exceed the maximum amount
46450 * The initial size of the Win32-specific heap. This value may be zero.
46459 * The maximum size of the Win32-specific heap. This value may be zero.
46475 ** The winMemData structure stores information required by the Win32-specific
46559 ** Many system calls are accessed through pointer-to-functions so that
47176 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
47288 int i = -1;
47292 for(i=0; i<ArraySize(aSyscall)-1; i++){
47307 ** "pnLargest" argument, if non-zero, will be used to return the size of the
47397 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
47398 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
47399 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
47465 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
47493 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
47518 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
47613 if( n==(SIZE_T)-1 ){
47635 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
47636 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
47639 if( !pWinMemData->hHeap ){
47647 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
47649 if( !pWinMemData->hHeap ){
47656 pWinMemData->bOwned = TRUE;
47657 assert( pWinMemData->bOwned );
47660 pWinMemData->hHeap = osGetProcessHeap();
47661 if( !pWinMemData->hHeap ){
47666 pWinMemData->bOwned = FALSE;
47667 assert( !pWinMemData->bOwned );
47669 assert( pWinMemData->hHeap!=0 );
47670 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
47672 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47684 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
47685 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
47687 if( pWinMemData->hHeap ){
47688 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
47690 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
47692 if( pWinMemData->bOwned ){
47693 if( !osHeapDestroy(pWinMemData->hHeap) ){
47695 osGetLastError(), (void*)pWinMemData->hHeap);
47697 pWinMemData->bOwned = FALSE;
47699 pWinMemData->hHeap = NULL;
47704 ** Populate the low-level memory allocation function pointers in
47731 ** Convert a UTF-8 string to Microsoft Unicode.
47739 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
47747 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
47757 ** Convert a Microsoft Unicode string to UTF-8.
47765 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
47773 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
47793 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
47802 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
47812 ** Convert a Microsoft Unicode string to a multi-byte character string,
47822 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
47830 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
47840 ** Convert a multi-byte character string to UTF-8.
47858 ** Convert a UTF-8 string to a multi-byte character string.
47973 ** it accepts a UTF-8 string.
48017 ** it accepts a UTF-16 string.
48050 ** is zero if the error message fits in the buffer, or non-zero
48130 ** This function - winLogErrorAtLine() - is only ever called via the macro
48135 ** error code and, if possible, the human-readable equivalent from
48141 ** failed and the associated file-system path, if any.
48161 "os_win.c:%d: (%lu) %s(%s) - %s",
48170 ** will be retried following a locking error - probably caused by
48187 ** non-zero if the error code is transient in nature and the operation
48275 y.tm_year = pTm.wYear - 1900;
48276 y.tm_mon = pTm.wMonth - 1;
48290 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
48324 memset(&pFile->local, 0, sizeof(pFile->local));
48334 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
48335 if (!pFile->hMutex){
48336 pFile->lastErrno = osGetLastError();
48338 return winLogError(SQLITE_IOERR, pFile->lastErrno,
48343 winceMutexAcquire(pFile->hMutex);
48346 ** case-sensitive, take advantage of that by uppercasing the mutex name
48350 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
48355 ** must be zero-initialized */
48364 if( pFile->hShared ){
48365 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
48368 if( !pFile->shared ){
48369 pFile->lastErrno = osGetLastError();
48370 winLogError(SQLITE_IOERR, pFile->lastErrno,
48373 osCloseHandle(pFile->hShared);
48374 pFile->hShared = NULL;
48379 if( pFile->hShared==NULL ){
48381 pFile->lastErrno = lastErrno;
48382 winLogError(SQLITE_IOERR, pFile->lastErrno,
48386 winceMutexRelease(pFile->hMutex);
48387 osCloseHandle(pFile->hMutex);
48388 pFile->hMutex = NULL;
48394 memset(pFile->shared, 0, sizeof(winceLock));
48397 winceMutexRelease(pFile->hMutex);
48405 if (pFile->hMutex){
48407 winceMutexAcquire(pFile->hMutex);
48411 if (pFile->local.nReaders){
48412 pFile->shared->nReaders --;
48414 if (pFile->local.bReserved){
48415 pFile->shared->bReserved = FALSE;
48417 if (pFile->local.bPending){
48418 pFile->shared->bPending = FALSE;
48420 if (pFile->local.bExclusive){
48421 pFile->shared->bExclusive = FALSE;
48424 /* De-reference and close our copy of the shared memory handle */
48425 osUnmapViewOfFile(pFile->shared);
48426 osCloseHandle(pFile->hShared);
48429 winceMutexRelease(pFile->hMutex);
48430 osCloseHandle(pFile->hMutex);
48431 pFile->hMutex = NULL;
48451 if (!pFile->hMutex) return TRUE;
48452 winceMutexAcquire(pFile->hMutex);
48457 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
48458 pFile->shared->bExclusive = TRUE;
48459 pFile->local.bExclusive = TRUE;
48464 /* Want a read-only lock? */
48467 if (pFile->shared->bExclusive == 0){
48468 pFile->local.nReaders ++;
48469 if (pFile->local.nReaders == 1){
48470 pFile->shared->nReaders ++;
48480 if (pFile->shared->bPending == 0) {
48481 pFile->shared->bPending = TRUE;
48482 pFile->local.bPending = TRUE;
48490 if (pFile->shared->bReserved == 0) {
48491 pFile->shared->bReserved = TRUE;
48492 pFile->local.bReserved = TRUE;
48497 winceMutexRelease(pFile->hMutex);
48517 if (!pFile->hMutex) return TRUE;
48518 winceMutexAcquire(pFile->hMutex);
48523 if (pFile->local.bExclusive){
48525 pFile->local.bExclusive = FALSE;
48526 pFile->shared->bExclusive = FALSE;
48531 else if (pFile->local.nReaders){
48534 pFile->local.nReaders --;
48535 if (pFile->local.nReaders == 0)
48537 pFile->shared->nReaders --;
48546 if (pFile->local.bPending){
48547 pFile->local.bPending = FALSE;
48548 pFile->shared->bPending = FALSE;
48555 if (pFile->local.bReserved) {
48556 pFile->local.bReserved = FALSE;
48557 pFile->shared->bReserved = FALSE;
48562 winceMutexRelease(pFile->hMutex);
48642 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
48648 ** Otherwise, set pFile->lastErrno and return non-zero.
48657 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
48663 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
48669 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
48673 pFile->lastErrno = lastErrno;
48674 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
48675 "winSeekFile", pFile->zPath);
48676 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
48680 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
48691 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
48694 pFile->lastErrno = osGetLastError();
48695 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
48696 "winSeekFile", pFile->zPath);
48697 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
48701 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
48729 assert( pFile->pShm==0 );
48731 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
48733 osGetCurrentProcessId(), pFile, pFile->h));
48740 rc = osCloseHandle(pFile->h);
48746 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
48747 if( pAppData==NULL || !pAppData->bNoLock ){
48751 if( pFile->zDeleteOnClose ){
48754 osDeleteFileW(pFile->zDeleteOnClose)==0
48755 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
48760 sqlite3_free(pFile->zDeleteOnClose);
48764 pFile->h = NULL;
48766 OpenCounter(-1);
48768 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
48771 "winClose", pFile->zPath);
48798 pFile->h, pBuf, amt, offset, pFile->locktype));
48803 if( offset<pFile->mmapSize ){
48804 if( offset+amt <= pFile->mmapSize ){
48805 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
48806 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48807 osGetCurrentProcessId(), pFile, pFile->h));
48810 int nCopy = (int)(pFile->mmapSize - offset);
48811 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
48813 amt -= nCopy;
48822 osGetCurrentProcessId(), pFile, pFile->h));
48825 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
48830 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
48835 pFile->lastErrno = lastErrno;
48837 osGetCurrentProcessId(), pFile, pFile->h));
48838 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
48839 "winRead", pFile->zPath);
48843 /* Unread parts of the buffer must be zero-filled */
48844 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
48846 osGetCurrentProcessId(), pFile, pFile->h));
48851 osGetCurrentProcessId(), pFile, pFile->h));
48876 pFile->h, pBuf, amt, offset, pFile->locktype));
48881 if( offset<pFile->mmapSize ){
48882 if( offset+amt <= pFile->mmapSize ){
48883 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
48884 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48885 osGetCurrentProcessId(), pFile, pFile->h));
48888 int nCopy = (int)(pFile->mmapSize - offset);
48889 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
48891 amt -= nCopy;
48919 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
48921 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
48937 nRem -= nWrite;
48940 pFile->lastErrno = lastErrno;
48946 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
48947 || ( pFile->lastErrno==ERROR_DISK_FULL )){
48949 osGetCurrentProcessId(), pFile, pFile->h));
48950 return winLogError(SQLITE_FULL, pFile->lastErrno,
48951 "winWrite1", pFile->zPath);
48954 osGetCurrentProcessId(), pFile, pFile->h));
48955 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
48956 "winWrite2", pFile->zPath);
48961 osGetCurrentProcessId(), pFile, pFile->h));
48974 if( pFile->nFetchOut>0 ){
48975 /* File truncation is a no-op if there are outstanding memory mapped
48981 ** No real harm comes of this - the database file is not corrupted,
48985 ** The only feasible work-around is to defer the truncation until after
48986 ** all references to memory-mapped content are closed. That is doable,
48989 ** now to simply make transactions a no-op if there are pending reads. We
48999 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
49001 /* If the user has configured a chunk-size for this file, truncate the
49006 if( pFile->szChunk>0 ){
49007 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
49011 if( pFile->pMapRegion ){
49012 oldMmapSize = pFile->mmapSize;
49019 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
49021 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49022 "winTruncate1", pFile->zPath);
49023 }else if( 0==osSetEndOfFile(pFile->h) &&
49025 pFile->lastErrno = lastErrno;
49026 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
49027 "winTruncate2", pFile->zPath);
49033 winMapfile(pFile, -1);
49041 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
49087 osGetCurrentProcessId(), pFile, pFile->h, flags,
49088 pFile->locktype));
49100 ** no-op
49103 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49104 osGetCurrentProcessId(), pFile, pFile->h));
49108 if( pFile->pMapRegion ){
49109 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
49110 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49112 pFile, pFile->pMapRegion));
49114 pFile->lastErrno = osGetLastError();
49115 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
49117 pFile, pFile->pMapRegion));
49118 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49119 "winSync1", pFile->zPath);
49123 rc = osFlushFileBuffers(pFile->h);
49127 osGetCurrentProcessId(), pFile, pFile->h));
49130 pFile->lastErrno = osGetLastError();
49132 osGetCurrentProcessId(), pFile, pFile->h));
49133 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
49134 "winSync2", pFile->zPath);
49149 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
49154 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
49158 pFile->lastErrno = osGetLastError();
49159 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49160 "winFileSize", pFile->zPath);
49169 lowerBits = osGetFileSize(pFile->h, &upperBits);
49173 pFile->lastErrno = lastErrno;
49174 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
49175 "winFileSize", pFile->zPath);
49180 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
49222 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49229 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
49231 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
49239 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
49240 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49241 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49245 pFile->lastErrno = osGetLastError();
49248 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
49258 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
49260 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
49264 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
49268 pFile->lastErrno = lastErrno;
49269 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
49270 "winUnlockReadLock", pFile->zPath);
49272 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
49277 ** Lock the file with the lock specified by parameter locktype - one
49291 ** UNLOCKED -> SHARED
49292 ** SHARED -> RESERVED
49293 ** SHARED -> (PENDING) -> EXCLUSIVE
49294 ** RESERVED -> (PENDING) -> EXCLUSIVE
49295 ** PENDING -> EXCLUSIVE
49305 int newLocktype; /* Set pFile->locktype to this value before exiting */
49312 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49318 if( pFile->locktype>=locktype ){
49319 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
49323 /* Do not allow any kind of write-lock on a read-only database
49325 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
49331 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
49333 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
49339 newLocktype = pFile->locktype;
49340 if( pFile->locktype==NO_LOCK
49341 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
49344 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
49347 ** around problems caused by indexing and/or anti-virus software on
49353 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
49354 pFile->h, cnt, res));
49356 pFile->lastErrno = lastErrno;
49358 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
49359 pFile->h, cnt, sqlite3ErrName(rc)));
49373 assert( pFile->locktype==NO_LOCK );
49385 assert( pFile->locktype==SHARED_LOCK );
49386 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
49404 assert( pFile->locktype>=SHARED_LOCK );
49406 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
49420 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
49429 pFile->lastErrno = lastErrno;
49431 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
49432 pFile->h, locktype, newLocktype));
49434 pFile->locktype = (u8)newLocktype;
49436 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
49443 ** non-zero, otherwise zero.
49450 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
49453 if( pFile->locktype>=RESERVED_LOCK ){
49455 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
49457 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
49459 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
49462 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
49465 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
49466 pFile->h, pResOut, *pResOut));
49475 ** the requested locking level, this routine is a no-op.
49488 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
49489 type = pFile->locktype;
49491 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
49496 "winUnlock", pFile->zPath);
49500 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
49506 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
49508 pFile->locktype = (u8)locktype;
49510 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
49515 ****************************** No-op Locking **********************************
49521 ** This locking mode is appropriate for use on read-only databases
49522 ** (ex: databases that are burned into CD-ROM, for example.) It can
49549 /******************* End of the no-op lock implementation *********************
49554 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
49556 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
49560 *pArg = (pFile->ctrlFlags & mask)!=0;
49562 pFile->ctrlFlags &= ~mask;
49564 pFile->ctrlFlags |= mask;
49579 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
49582 *(int*)pArg = pFile->locktype;
49583 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49587 *(int*)pArg = (int)pFile->lastErrno;
49588 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49592 pFile->szChunk = *(int *)pArg;
49593 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49597 if( pFile->szChunk>0 ){
49608 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49611 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49616 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49621 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49625 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
49626 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49641 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49646 *phFile = pFile->h;
49647 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
49653 HANDLE hOldFile = pFile->h;
49654 pFile->h = *phFile;
49657 hOldFile, pFile->h));
49663 int rc = winGetTempname(pFile->pVfs, &zTFile);
49667 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49680 ** least a 64-bit type. */
49685 *(i64*)pArg = pFile->mmapSizeMax;
49686 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
49687 pFile->mmapSizeMax = newLimit;
49688 if( pFile->mmapSize>0 ){
49690 rc = winMapfile(pFile, -1);
49693 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
49698 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
49708 ** if two files are created in the same file-system directory (i.e.
49723 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
49765 ** log-summary, each thread has its own winFile object, but they all
49767 ** log-summary is opened only once per process.
49775 ** The following fields are read-only after the object is created:
49790 int szRegion; /* Size of shared-memory regions */
49792 u8 isReadonly; /* True if read-only */
49821 ** are read-only thereafter:
49826 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
49853 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
49861 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
49863 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
49864 pFile->hFile.h, lockType, ofst, nByte));
49866 /* Release/Acquire the system-level lock */
49868 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
49873 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
49879 pFile->lastErrno = osGetLastError();
49883 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
49884 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
49885 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
49897 ** This is not a VFS shared-memory method; it is a utility function called
49898 ** by VFS shared-memory methods.
49904 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
49908 if( p->nRef==0 ){
49910 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
49911 for(i=0; i<p->nRegion; i++){
49912 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
49913 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
49916 bRc = osCloseHandle(p->aRegion[i].hMap);
49917 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
49921 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
49923 winClose((sqlite3_file *)&p->hFile);
49929 winDelete(pVfs, p->zFilename, 0);
49933 *pp = p->pNext;
49934 sqlite3_free(p->aRegion);
49937 pp = &p->pNext;
49949 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
49955 if( pShmNode->isReadonly ){
49956 pShmNode->isUnlocked = 1;
49959 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
49962 "winLockSharedMemory", pShmNode->zFilename);
49974 ** Open the shared-memory area associated with database file pDbFd.
49976 ** When opening a new shared-memory file, if no other instances of that
49987 assert( pDbFd->pShm==0 ); /* Not previously opened */
49994 nName = sqlite3Strlen30(pDbFd->zPath);
50000 pNew->zFilename = (char*)&pNew[1];
50001 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
50002 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
50008 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
50012 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
50022 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50023 pShmNode->pNext = winShmNodeList;
50027 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50028 if( pShmNode->mutex==0 ){
50034 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50039 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50040 (sqlite3_file*)&pShmNode->hFile,
50044 pShmNode->zFilename);
50047 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
50054 p->pShmNode = pShmNode;
50056 p->id = pShmNode->nextShmId++;
50058 pShmNode->nRef++;
50059 pDbFd->pShm = p;
50066 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50069 sqlite3_mutex_enter(pShmNode->mutex);
50070 p->pNext = pShmNode->pFirst;
50071 pShmNode->pFirst = p;
50072 sqlite3_mutex_leave(pShmNode->mutex);
50078 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50086 ** Close a connection to shared-memory. Delete the underlying
50093 winFile *pDbFd; /* Database holding shared-memory */
50095 winShmNode *pShmNode; /* The underlying shared-memory file */
50099 p = pDbFd->pShm;
50101 pShmNode = p->pShmNode;
50105 sqlite3_mutex_enter(pShmNode->mutex);
50106 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50107 *pp = p->pNext;
50111 pDbFd->pShm = 0;
50112 sqlite3_mutex_leave(pShmNode->mutex);
50114 /* If pShmNode->nRef has reached 0, then close the underlying
50115 ** shared-memory file, too */
50117 assert( pShmNode->nRef>0 );
50118 pShmNode->nRef--;
50119 if( pShmNode->nRef==0 ){
50120 winShmPurge(pDbFd->pVfs, deleteFlag);
50128 ** Change the lock state for a shared-memory segment.
50137 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50144 pShmNode = p->pShmNode;
50155 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
50157 sqlite3_mutex_enter(pShmNode->mutex);
50162 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50164 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
50165 allMask |= pX->sharedMask;
50168 /* Unlock the system-level locks */
50177 p->exclMask &= ~mask;
50178 p->sharedMask &= ~mask;
50187 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50188 if( (pX->exclMask & mask)!=0 ){
50192 allShared |= pX->sharedMask;
50206 p->sharedMask |= mask;
50212 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
50213 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
50225 assert( (p->sharedMask & mask)==0 );
50226 p->exclMask |= mask;
50230 sqlite3_mutex_leave(pShmNode->mutex);
50231 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
50232 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
50247 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
50254 ** shared-memory associated with the database file fd. Shared-memory regions
50255 ** are numbered starting from zero. Each shared-memory region is szRegion
50260 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
50263 ** isWrite is non-zero and the requested shared-memory region has not yet
50266 ** If the shared-memory region has already been allocated or is allocated by
50279 winShm *pShm = pDbFd->pShm;
50288 pShm = pDbFd->pShm;
50291 pShmNode = pShm->pShmNode;
50293 sqlite3_mutex_enter(pShmNode->mutex);
50294 if( pShmNode->isUnlocked ){
50297 pShmNode->isUnlocked = 0;
50299 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
50301 if( pShmNode->nRegion<=iRegion ){
50304 sqlite3_int64 sz; /* Current size of wal-index file */
50306 pShmNode->szRegion = szRegion;
50309 ** Check to see if it has been allocated (i.e. if the wal-index file is
50312 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
50315 "winShmMap1", pDbFd->zPath);
50323 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
50327 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
50330 "winShmMap2", pDbFd->zPath);
50337 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
50343 pShmNode->aRegion = apNew;
50345 if( pShmNode->isReadonly ){
50350 while( pShmNode->nRegion<=iRegion ){
50351 HANDLE hMap = NULL; /* file-mapping handle */
50355 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
50359 hMap = osCreateFileMappingW(pShmNode->hFile.h,
50363 hMap = osCreateFileMappingA(pShmNode->hFile.h,
50367 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
50368 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
50371 int iOffset = pShmNode->nRegion*szRegion;
50375 iOffset - iOffsetShift, szRegion + iOffsetShift
50379 0, iOffset - iOffsetShift, szRegion + iOffsetShift
50382 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
50383 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
50387 pShmNode->lastErrno = osGetLastError();
50388 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
50389 "winShmMap3", pDbFd->zPath);
50394 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
50395 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
50396 pShmNode->nRegion++;
50401 if( pShmNode->nRegion>iRegion ){
50404 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
50409 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
50410 sqlite3_mutex_leave(pShmNode->mutex);
50427 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
50429 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
50430 pFile->mmapSize, pFile->mmapSizeMax));
50431 if( pFile->pMapRegion ){
50432 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
50433 pFile->lastErrno = osGetLastError();
50434 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
50436 pFile->pMapRegion));
50437 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
50438 "winUnmapfile1", pFile->zPath);
50440 pFile->pMapRegion = 0;
50441 pFile->mmapSize = 0;
50443 if( pFile->hMap!=NULL ){
50444 if( !osCloseHandle(pFile->hMap) ){
50445 pFile->lastErrno = osGetLastError();
50446 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
50447 osGetCurrentProcessId(), pFile, pFile->hMap));
50448 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
50449 "winUnmapfile2", pFile->zPath);
50451 pFile->hMap = NULL;
50453 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
50459 ** Memory map or remap the file opened by file-descriptor pFd (if the file
50462 ** outstanding xFetch() references to it, this function is a no-op.
50464 ** If parameter nByte is non-negative, then it is the requested size of
50478 assert( nMap>=0 || pFd->nFetchOut==0 );
50479 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
50482 if( pFd->nFetchOut>0 ) return SQLITE_OK;
50487 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
50492 if( nMap>pFd->mmapSizeMax ){
50493 nMap = pFd->mmapSizeMax;
50495 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
50497 if( nMap==0 && pFd->mmapSize>0 ){
50500 if( nMap!=pFd->mmapSize ){
50507 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
50513 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
50515 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
50519 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
50523 if( pFd->hMap==NULL ){
50524 pFd->lastErrno = osGetLastError();
50525 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
50526 "winMapfile1", pFd->zPath);
50528 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
50535 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
50537 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
50540 osCloseHandle(pFd->hMap);
50541 pFd->hMap = NULL;
50542 pFd->lastErrno = osGetLastError();
50543 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
50544 "winMapfile2", pFd->zPath);
50546 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
50550 pFd->pMapRegion = pNew;
50551 pFd->mmapSize = nMap;
50554 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
50582 if( pFd->mmapSizeMax>0 ){
50588 if( pFd->pMapRegion==0 ){
50589 int rc = winMapfile(pFd, -1);
50596 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
50597 assert( pFd->pMapRegion!=0 );
50598 *pp = &((u8 *)pFd->pMapRegion)[iOff];
50599 pFd->nFetchOut++;
50610 ** If the third argument is non-NULL, then this function releases a
50626 assert( (p==0)==(pFd->nFetchOut==0) );
50629 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
50635 pFd->nFetchOut--;
50639 ** is unnecessary can be omitted - potentially improving
50644 assert( pFd->nFetchOut>=0 );
50732 ** supports for filenames into UTF-8. Space to hold the result is
50751 ** Convert a UTF-8 filename into whatever form the underlying
50771 ** This function returns non-zero if the specified UTF-8 string buffer
50779 if( winIsDirSep(zBuf[nLen-1]) ){
50819 /* It's odd to simulate an io-error here, but really this is just
50820 ** using the io-error infrastructure to test that SQLite handles this
50828 nMax = pVfs->mxPathname; nBuf = nMax + 2;
50831 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50839 nDir = nMax - (nPre + 15);
50844 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
50850 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50892 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50905 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50913 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
50920 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
50926 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50944 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50950 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50962 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50972 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50977 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50987 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
51001 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51019 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
51023 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
51031 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
51037 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
51083 const char *zName, /* Name of the file (UTF-8) */
51100 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
51142 /* The main DB, main journal, WAL file and super-journal are never
51149 /* Assert that the upper layer has set one of the "file-type" flags. */
51158 pFile->h = INVALID_HANDLE_VALUE;
51180 /* Database filenames are double-zero terminated if they are not
51326 pFile->lastErrno = lastErrno;
51327 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
51344 pAppData = (winVfsAppData*)pVfs->pAppData;
51349 && ((pAppData==NULL) || !pAppData->bNoLock)
51355 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
51360 pFile->zDeleteOnClose = zConverted;
51368 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
51369 pFile->pVfs = pVfs;
51370 pFile->h = h;
51372 pFile->ctrlFlags |= WINFILE_RDONLY;
51377 pFile->ctrlFlags |= WINFILE_PSOW;
51379 pFile->lastErrno = NO_ERROR;
51380 pFile->zPath = zName;
51382 pFile->hMap = NULL;
51383 pFile->pMapRegion = 0;
51384 pFile->mmapSize = 0;
51385 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
51548 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
51594 ** Returns non-zero if the specified path name starts with the "long path"
51605 ** Returns non-zero if the specified path name starts with a drive letter
51615 ** Returns non-zero if the specified path name should be used verbatim. If
51616 ** non-zero is returned from this function, the calling function must simply
51617 ** use the provided path name verbatim -OR- resolve it into a full path name
51653 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
51679 assert( nFull>=pVfs->mxPathname );
51687 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
51693 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
51703 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51709 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
51715 zRelative, zOut, pVfs->mxPathname+1)<0 ){
51725 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
51744 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51747 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
51753 /* It's odd to simulate an io-error here, but really this is just
51754 ** using the io-error infrastructure to test that SQLite handles this
51766 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51827 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
51858 int nFull = pVfs->mxPathname+1;
51933 for(j=0, k=p->i; j<sz; j++){
51934 p->a[k++] ^= x[j];
51935 if( k>=p->na ) k = 0;
51937 p->i = k;
51938 p->nXor += sz;
52009 ** The following variable, if set to a non-zero value, is interpreted as
52028 /* FILETIME structure is a 64-bit value representing the number of
52029 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
52036 /* 2^32 - to avoid use of LL and warnings in gcc */
52086 ** buffer with a nul-terminated UTF-8 encoded error message
52092 ** is zero if the error message fits in the buffer, or non-zero
52093 ** otherwise (if the message was truncated). If non-zero is returned,
52094 ** then it is not necessary to include the nul-terminator character
52151 "win32-longpath", /* zName */
52176 "win32-none", /* zName */
52201 "win32-longpath-none", /* zName */
52222 /* Double-check that the aSyscall[] array has been constructed
52275 ** 2016-09-07
52286 ** This file implements an in-memory VFS. A database is held as a contiguous
52302 /* Access to a lower-level VFS that (might) implement dynamic loading,
52305 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
52365 ** File-scope variables for holding the memdb files that are accessible
52469 sqlite3_mutex_enter(p->pMutex);
52472 sqlite3_mutex_leave(p->pMutex);
52479 ** Close an memdb-file.
52484 MemStore *p = ((MemFile*)pFile)->pStore;
52485 if( p->zFName ){
52494 if( p->nRef==1 ){
52495 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
52508 p->nRef--;
52509 if( p->nRef<=0 ){
52510 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
52511 sqlite3_free(p->aData);
52514 sqlite3_mutex_free(p->pMutex);
52523 ** Read data from an memdb-file.
52531 MemStore *p = ((MemFile*)pFile)->pStore;
52533 if( iOfst+iAmt>p->sz ){
52535 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
52539 memcpy(zBuf, p->aData+iOfst, iAmt);
52549 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
52552 if( newSz>p->szMax ){
52556 if( newSz>p->szMax ) newSz = p->szMax;
52557 pNew = sqlite3Realloc(p->aData, newSz);
52559 p->aData = pNew;
52560 p->szAlloc = newSz;
52565 ** Write data to an memdb-file.
52573 MemStore *p = ((MemFile*)pFile)->pStore;
52575 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
52581 if( iOfst+iAmt>p->sz ){
52583 if( iOfst+iAmt>p->szAlloc
52589 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
52590 p->sz = iOfst+iAmt;
52592 memcpy(p->aData+iOfst, z, iAmt);
52598 ** Truncate an memdb-file.
52605 MemStore *p = ((MemFile*)pFile)->pStore;
52608 if( size>p->sz ){
52612 p->sz = size;
52619 ** Sync an memdb-file.
52628 ** Return the current file-size of an memdb-file.
52631 MemStore *p = ((MemFile*)pFile)->pStore;
52633 *pSize = p->sz;
52639 ** Lock an memdb-file.
52643 MemStore *p = pThis->pStore;
52645 if( eLock<=pThis->eLock ) return SQLITE_OK;
52648 assert( p->nWrLock==0 || p->nWrLock==1 );
52649 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
52650 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
52652 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
52657 assert( pThis->eLock==SQLITE_LOCK_NONE );
52658 if( p->nWrLock>0 ){
52661 p->nRdLock++;
52668 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
52669 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
52670 if( p->nWrLock>0 ){
52673 p->nWrLock = 1;
52681 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
52682 if( p->nRdLock>1 ){
52684 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
52685 p->nWrLock = 1;
52691 if( rc==SQLITE_OK ) pThis->eLock = eLock;
52697 ** Unlock an memdb-file.
52701 MemStore *p = pThis->pStore;
52702 if( eLock>=pThis->eLock ) return SQLITE_OK;
52707 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
52708 p->nWrLock--;
52711 if( pThis->eLock>SQLITE_LOCK_SHARED ){
52712 p->nWrLock--;
52714 p->nRdLock--;
52717 pThis->eLock = eLock;
52725 ** occur on an in-memory database.
52735 ** File control method. For custom operations on an memdb-file.
52738 MemStore *p = ((MemFile*)pFile)->pStore;
52742 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
52747 if( iLimit<p->sz ){
52749 iLimit = p->szMax;
52751 iLimit = p->sz;
52754 p->szMax = iLimit;
52764 ** Return the sector-size in bytes for an memdb-file.
52772 ** Return the device characteristic flags supported by an memdb-file.
52782 /* Fetch a page of a memory-mapped file */
52789 MemStore *p = ((MemFile*)pFile)->pStore;
52791 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
52794 p->nMmap++;
52795 *pp = (void*)(p->aData + iOfst);
52801 /* Release a memory-mapped page */
52803 MemStore *p = ((MemFile*)pFile)->pStore;
52807 p->nMmap--;
52836 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
52858 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
52859 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52860 p->zFName = (char*)&p[1];
52861 memcpy(p->zFName, zName, szName+1);
52862 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
52863 if( p->pMutex==0 ){
52864 memdb_g.nMemStore--;
52869 p->nRef = 1;
52873 p->nRef++;
52882 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
52883 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52885 pFile->pStore = p;
52889 pFd->pMethods = &memdb_io_methods;
52894 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
52898 ** ensure the file-system modifications are synced to disk before
52945 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
52950 ** utf-8 string describing the most recent error encountered associated
52954 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
52961 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
52968 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
52976 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
52984 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
52992 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
52997 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
53000 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
53012 if( p->base.pMethods!=&memdb_io_methods ) return 0;
53013 pStore = p->pStore;
53015 if( pStore->zFName!=0 ) p = 0;
53046 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53049 if( piSize ) *piSize = -1;
53052 MemStore *pStore = p->pStore;
53053 assert( pStore->pMutex==0 );
53054 if( piSize ) *piSize = pStore->sz;
53056 pOut = pStore->aData;
53058 pOut = sqlite3_malloc64( pStore->sz );
53059 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
53063 pBt = db->aDb[iDb].pBt;
53067 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
53086 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
53126 sqlite3_mutex_enter(db->mutex);
53127 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
53138 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
53142 db->init.iDb = (u8)iDb;
53143 db->init.reopenMemdb = 1;
53145 db->init.reopenMemdb = 0;
53154 MemStore *pStore = p->pStore;
53155 pStore->aData = pData;
53157 pStore->sz = szDb;
53158 pStore->szAlloc = szBuf;
53159 pStore->szMax = szBuf;
53160 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
53161 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
53163 pStore->mFlags = mFlags;
53172 sqlite3_mutex_leave(db->mutex);
53191 sz = pLower->szOsFile;
53216 ** This file implements an object that represents a fixed-length
53220 ** journalled during a transaction, or which pages have the "dont-write"
53249 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
53266 ** sub-dividing and re-hashing. */
53271 ** no fewer collisions than the no-op *1. */
53291 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
53300 u32 nSet; /* Number of bits that are set - only valid for aHash
53324 p->iSize = iSize;
53330 ** Check to see if the i-th bit is set. Return true or false.
53336 i--;
53337 if( i>=p->iSize ) return 0;
53338 while( p->iDivisor ){
53339 u32 bin = i/p->iDivisor;
53340 i = i%p->iDivisor;
53341 p = p->u.apSub[bin];
53346 if( p->iSize<=BITVEC_NBIT ){
53347 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
53350 while( p->u.aHash[h] ){
53351 if( p->u.aHash[h]==i ) return 1;
53362 ** Set the i-th bit. Return 0 on success and an error code if
53365 ** This routine might cause sub-bitmaps to be allocated. Failing
53366 ** to get the memory needed to hold the sub-bitmap is the only
53377 assert( i<=p->iSize );
53378 i--;
53379 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
53380 u32 bin = i/p->iDivisor;
53381 i = i%p->iDivisor;
53382 if( p->u.apSub[bin]==0 ){
53383 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
53384 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
53386 p = p->u.apSub[bin];
53388 if( p->iSize<=BITVEC_NBIT ){
53389 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
53395 /* worrying about sub-dividing and re-hashing. */
53396 if( !p->u.aHash[h] ){
53397 if (p->nSet<(BITVEC_NINT-1)) {
53406 if( p->u.aHash[h]==i ) return SQLITE_OK;
53409 } while( p->u.aHash[h] );
53414 if( p->nSet>=BITVEC_MXHASH ){
53417 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
53421 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
53422 memset(p->u.apSub, 0, sizeof(p->u.apSub));
53423 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
53433 p->nSet++;
53434 p->u.aHash[h] = i;
53439 ** Clear the i-th bit.
53447 i--;
53448 while( p->iDivisor ){
53449 u32 bin = i/p->iDivisor;
53450 i = i%p->iDivisor;
53451 p = p->u.apSub[bin];
53456 if( p->iSize<=BITVEC_NBIT ){
53457 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
53461 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
53462 memset(p->u.aHash, 0, sizeof(p->u.aHash));
53463 p->nSet = 0;
53466 u32 h = BITVEC_HASH(aiValues[j]-1);
53467 p->nSet++;
53468 while( p->u.aHash[h] ){
53472 p->u.aHash[h] = aiValues[j];
53483 if( p->iDivisor ){
53486 sqlite3BitvecDestroy(p->u.apSub[i]);
53497 return p->iSize;
53539 ** If a memory allocation error occurs, return -1.
53544 int rc = -1;
53567 i = aOp[pc+2] - 1;
53579 if( (--aOp[pc+1]) > 0 ) nx = 0;
53595 ** match (rc==0). Change rc to non-zero if a discrepancy
53600 + (sqlite3BitvecSize(pBitvec) - sz);
53646 ** such that p was added to the list more recently than p->pDirtyNext.
53651 ** page to eject from the cache mid-transaction. It is better to eject
53694 pPg = (PgHdr*)pLower->pExtra;
53695 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
53696 a = (unsigned char *)pLower->pBuf;
53707 if( pCache->pCache==0 ) return;
53711 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
53713 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
53714 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
53731 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53738 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53761 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
53762 pCache = pPg->pCache;
53764 if( pPg->flags & PGHDR_CLEAN ){
53765 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
53768 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
53769 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
53770 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
53771 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
53775 if( pPg->flags & PGHDR_WRITEABLE ){
53776 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
53814 PCache *p = pPage->pCache;
53818 pPage->pgno));
53820 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
53821 assert( pPage->pDirtyPrev || pPage==p->pDirty );
53824 if( p->pSynced==pPage ){
53825 p->pSynced = pPage->pDirtyPrev;
53828 if( pPage->pDirtyNext ){
53829 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
53831 assert( pPage==p->pDirtyTail );
53832 p->pDirtyTail = pPage->pDirtyPrev;
53834 if( pPage->pDirtyPrev ){
53835 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
53841 assert( pPage==p->pDirty );
53842 p->pDirty = pPage->pDirtyNext;
53843 assert( p->bPurgeable || p->eCreate==2 );
53844 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
53845 assert( p->bPurgeable==0 || p->eCreate==1 );
53846 p->eCreate = 2;
53851 pPage->pDirtyPrev = 0;
53852 pPage->pDirtyNext = p->pDirty;
53853 if( pPage->pDirtyNext ){
53854 assert( pPage->pDirtyNext->pDirtyPrev==0 );
53855 pPage->pDirtyNext->pDirtyPrev = pPage;
53857 p->pDirtyTail = pPage;
53858 if( p->bPurgeable ){
53859 assert( p->eCreate==2 );
53860 p->eCreate = 1;
53863 p->pDirty = pPage;
53869 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
53870 if( !p->pSynced
53871 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
53873 p->pSynced = pPage;
53881 ** being used for an in-memory database, this function is a no-op.
53884 if( p->pCache->bPurgeable ){
53885 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
53886 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
53887 pcacheDump(p->pCache);
53892 ** Compute the number of pages of cache requested. p->szCache is the
53896 if( p->szCache>=0 ){
53897 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
53899 return p->szCache;
53902 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
53906 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
53919 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
53920 ** built-in default page cache is used instead of the application defined
53929 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
53960 p->szPage = 1;
53961 p->szExtra = szExtra;
53963 p->bPurgeable = bPurgeable;
53964 p->eCreate = 2;
53965 p->xStress = xStress;
53966 p->pStress = pStress;
53967 p->szCache = 100;
53968 p->szSpill = 1;
53978 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
53979 if( pCache->szPage ){
53982 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
53983 pCache->bPurgeable
53987 if( pCache->pCache ){
53988 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53990 pCache->pCache = pNew;
53991 pCache->szPage = szPage;
54030 assert( pCache->pCache!=0 );
54032 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
54041 eCreate = createFlag & pCache->eCreate;
54043 assert( createFlag==0 || pCache->eCreate==eCreate );
54044 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
54045 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
54069 if( pCache->eCreate==2 ) return 0;
54071 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
54072 /* Find a dirty page to write-out and recycle. First try to find a
54073 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
54081 for(pPg=pCache->pSynced;
54082 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
54083 pPg=pPg->pDirtyPrev
54085 pCache->pSynced = pPg;
54087 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
54093 "spill page %d making room for %d - cache used: %d/%d",
54094 pPg->pgno, pgno,
54095 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
54098 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
54099 rc = pCache->xStress(pCache->pStress, pPg);
54106 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
54126 pPgHdr = (PgHdr*)pPage->pExtra;
54127 assert( pPgHdr->pPage==0 );
54128 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
54129 pPgHdr->pPage = pPage;
54130 pPgHdr->pData = pPage->pBuf;
54131 pPgHdr->pExtra = (void *)&pPgHdr[1];
54132 memset(pPgHdr->pExtra, 0, 8);
54133 pPgHdr->pCache = pCache;
54134 pPgHdr->pgno = pgno;
54135 pPgHdr->flags = PGHDR_CLEAN;
54153 pPgHdr = (PgHdr *)pPage->pExtra;
54155 if( !pPgHdr->pPage ){
54158 pCache->nRefSum++;
54159 pPgHdr->nRef++;
54169 assert( p->nRef>0 );
54170 p->pCache->nRefSum--;
54171 if( (--p->nRef)==0 ){
54172 if( p->flags&PGHDR_CLEAN ){
54185 assert(p->nRef>0);
54187 p->nRef++;
54188 p->pCache->nRefSum++;
54197 assert( p->nRef==1 );
54199 if( p->flags&PGHDR_DIRTY ){
54202 p->pCache->nRefSum--;
54203 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
54211 assert( p->nRef>0 );
54213 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
54214 p->flags &= ~PGHDR_DONT_WRITE;
54215 if( p->flags & PGHDR_CLEAN ){
54216 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
54217 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
54218 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
54232 assert( (p->flags & PGHDR_DIRTY)!=0 );
54233 assert( (p->flags & PGHDR_CLEAN)==0 );
54235 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54236 p->flags |= PGHDR_CLEAN;
54237 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
54239 if( p->nRef==0 ){
54249 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
54250 while( (p = pCache->pDirty)!=0 ){
54260 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
54261 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54262 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
54264 pCache->pSynced = pCache->pDirtyTail;
54272 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54273 p->flags &= ~PGHDR_NEED_SYNC;
54275 pCache->pSynced = pCache->pDirtyTail;
54282 PCache *pCache = p->pCache;
54284 assert( p->nRef>0 );
54287 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
54288 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
54290 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
54291 assert( pXPage->nRef==0 );
54292 pXPage->nRef++;
54293 pCache->nRefSum++;
54296 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
54297 p->pgno = newPgno;
54298 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
54314 if( pCache->pCache ){
54318 for(p=pCache->pDirty; p; p=pNext){
54319 pNext = p->pDirtyNext;
54324 assert( p->pgno>0 );
54325 if( p->pgno>pgno ){
54326 assert( p->flags&PGHDR_DIRTY );
54330 if( pgno==0 && pCache->nRefSum ){
54332 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
54334 ** pCache->nRefSum>0 */
54335 memset(pPage1->pBuf, 0, pCache->szPage);
54339 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
54347 assert( pCache->pCache!=0 );
54349 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
54368 if( pA->pgno<pB->pgno ){
54369 pTail->pDirty = pA;
54371 pA = pA->pDirty;
54373 pTail->pDirty = pB;
54377 pTail->pDirty = pB;
54379 pB = pB->pDirty;
54381 pTail->pDirty = pA;
54406 pIn = p->pDirty;
54407 p->pDirty = 0;
54408 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
54417 if( NEVER(i==N_SORT_BUCKET-1) ){
54437 for(p=pCache->pDirty; p; p=p->pDirtyNext){
54438 p->pDirty = p->pDirtyNext;
54440 return pcacheSortDirtyList(pCache->pDirty);
54450 return pCache->nRefSum;
54457 return p->nRef;
54464 assert( pCache->pCache!=0 );
54465 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
54470 ** Get the suggested cache-size value.
54478 ** Set the suggested cache-size value.
54481 assert( pCache->pCache!=0 );
54482 pCache->szCache = mxPage;
54483 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
54488 ** Set the suggested cache-spill value. Make no changes if if the
54489 ** argument is zero. Return the effective cache-spill size, which will
54494 assert( p->pCache!=0 );
54497 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
54499 p->szSpill = mxPage;
54502 if( res<p->szSpill ) res = p->szSpill;
54510 assert( pCache->pCache!=0 );
54511 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
54516 ** in the page-cache hierarchy.
54528 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
54537 return (pCache->pDirty!=0);
54549 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
54577 ** -------------------------------------------------------------
54579 ** -------------------------------------------------------------
54595 ** size can vary according to architecture, compile-time options, and
54601 ** allocations. However, the btree layer needs a small (16-byte) overrun
54616 ** (1) The general-purpose memory allocator - sqlite3Malloc()
54617 ** (2) Global page-cache memory provided using sqlite3_config() with
54619 ** (3) PCache-local bulk allocation.
54629 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
54633 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
54649 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
54659 ** but causes a 2-byte gap in the structure for most architectures (since
54660 ** pointers must be either 4 or 8-byte aligned). As this structure is located
54662 ** corrupt, code at the b-tree layer may overread the page buffer and
54668 ** The pLruNext and pLruPrev pointers form a double-linked circular list
54689 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
54690 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
54718 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
54724 ** open database file (including each in-memory database and each
54739 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
54756 PgHdr1 **apHash; /* Hash table for fast lookup by key */
54757 PgHdr1 *pFree; /* List of unused pcache-local pages */
54758 void *pBulk; /* Bulk memory used by pcache-local */
54792 ** reading because (1) most platforms read a 32-bit integer atomically and
54809 # define pcache1EnterMutex(X) assert((X)->mutex==0)
54810 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
54813 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
54814 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
54824 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
54843 while( n-- ){
54845 p->pNext = pcache1.pFree;
54854 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
54855 ** true if pCache->pFree ends up containing one or more free pages.
54862 if( pCache->nMax<3 ) return 0;
54865 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
54867 szBulk = -1024 * (i64)pcache1.nInitPage;
54869 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
54870 szBulk = pCache->szAlloc*(i64)pCache->nMax;
54872 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
54875 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
54877 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
54878 pX->page.pBuf = zBulk;
54879 pX->page.pExtra = &pX[1];
54880 pX->isBulkLocal = 1;
54881 pX->isAnchor = 0;
54882 pX->pNext = pCache->pFree;
54883 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
54884 pCache->pFree = pX;
54885 zBulk += pCache->szAlloc;
54886 }while( --nBulk );
54888 return pCache->pFree!=0;
54907 pcache1.pFree = pcache1.pFree->pNext;
54908 pcache1.nFreeSlot--;
54945 pSlot->pNext = pcache1.pFree;
54992 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54993 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
54994 assert( pCache->pFree!=0 );
54995 p = pCache->pFree;
54996 pCache->pFree = p->pNext;
54997 p->pNext = 0;
55004 assert( pCache->pGroup==&pcache1.grp );
55005 pcache1LeaveMutex(pCache->pGroup);
55008 pPg = pcache1Alloc(pCache->szAlloc);
55011 pcache1EnterMutex(pCache->pGroup);
55014 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55015 p->page.pBuf = pPg;
55016 p->page.pExtra = &p[1];
55017 p->isBulkLocal = 0;
55018 p->isAnchor = 0;
55019 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55021 (*pCache->pnPurgeable)++;
55031 pCache = p->pCache;
55032 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
55033 if( p->isBulkLocal ){
55034 p->pNext = pCache->pFree;
55035 pCache->pFree = p;
55037 pcache1Free(p->page.pBuf);
55039 (*pCache->pnPurgeable)--;
55077 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
55098 assert( sqlite3_mutex_held(p->pGroup->mutex) );
55100 nNew = p->nHash*2;
55105 pcache1LeaveMutex(p->pGroup);
55106 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
55108 if( p->nHash ){ sqlite3EndBenignMalloc(); }
55109 pcache1EnterMutex(p->pGroup);
55111 for(i=0; i<p->nHash; i++){
55113 PgHdr1 *pNext = p->apHash[i];
55115 unsigned int h = pPage->iKey % nNew;
55116 pNext = pPage->pNext;
55117 pPage->pNext = apNew[h];
55121 sqlite3_free(p->apHash);
55122 p->apHash = apNew;
55123 p->nHash = nNew;
55130 ** LRU list, then this function is a no-op.
55137 assert( pPage->pLruNext );
55138 assert( pPage->pLruPrev );
55139 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
55140 pPage->pLruPrev->pLruNext = pPage->pLruNext;
55141 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
55142 pPage->pLruNext = 0;
55143 /* pPage->pLruPrev = 0;
55145 assert( pPage->isAnchor==0 );
55146 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
55147 pPage->pCache->nRecyclable--;
55161 PCache1 *pCache = pPage->pCache;
55164 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55165 h = pPage->iKey % pCache->nHash;
55166 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
55167 *pp = (*pp)->pNext;
55169 pCache->nPage--;
55178 PGroup *pGroup = pCache->pGroup;
55180 assert( sqlite3_mutex_held(pGroup->mutex) );
55181 while( pGroup->nPurgeable>pGroup->nMaxPage
55182 && (p=pGroup->lru.pLruPrev)->isAnchor==0
55184 assert( p->pCache->pGroup==pGroup );
55189 if( pCache->nPage==0 && pCache->pBulk ){
55190 sqlite3_free(pCache->pBulk);
55191 pCache->pBulk = pCache->pFree = 0;
55206 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
55208 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
55209 assert( pCache->iMaxKey >= iLimit );
55210 assert( pCache->nHash > 0 );
55211 if( pCache->iMaxKey - iLimit < pCache->nHash ){
55216 h = iLimit % pCache->nHash;
55217 iStop = pCache->iMaxKey % pCache->nHash;
55218 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
55222 h = pCache->nHash/2;
55223 iStop = h - 1;
55228 assert( h<pCache->nHash );
55229 pp = &pCache->apHash[h];
55231 if( pPage->iKey>=iLimit ){
55232 pCache->nPage--;
55233 *pp = pPage->pNext;
55237 pp = &pPage->pNext;
55242 h = (h+1) % pCache->nHash;
55244 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
55261 ** private PGroup (mode-1). pcache1.separateCache is false if the single
55262 ** PGroup in pcache1.grp is used for all page caches (mode-2).
55264 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
55266 ** * Use a unified cache in single-threaded applications that have
55267 ** configured a start-time buffer for use as page-cache memory using
55268 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
55271 ** * Otherwise use separate caches (mode-1)
55325 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
55333 pGroup->mxPinned = 10;
55338 if( pGroup->lru.isAnchor==0 ){
55339 pGroup->lru.isAnchor = 1;
55340 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
55342 pCache->pGroup = pGroup;
55343 pCache->szPage = szPage;
55344 pCache->szExtra = szExtra;
55345 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
55346 pCache->bPurgeable = (bPurgeable ? 1 : 0);
55349 pCache->nMin = 10;
55350 pGroup->nMinPage += pCache->nMin;
55351 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55352 pCache->pnPurgeable = &pGroup->nPurgeable;
55354 pCache->pnPurgeable = &pCache->nPurgeableDummy;
55357 if( pCache->nHash==0 ){
55374 if( pCache->bPurgeable ){
55375 PGroup *pGroup = pCache->pGroup;
55378 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
55379 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
55381 pGroup->nMaxPage += (n - pCache->nMax);
55382 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55383 pCache->nMax = n;
55384 pCache->n90pct = pCache->nMax*9/10;
55397 if( pCache->bPurgeable ){
55398 PGroup *pGroup = pCache->pGroup;
55401 savedMaxPage = pGroup->nMaxPage;
55402 pGroup->nMaxPage = 0;
55404 pGroup->nMaxPage = savedMaxPage;
55415 pcache1EnterMutex(pCache->pGroup);
55416 n = pCache->nPage;
55417 pcache1LeaveMutex(pCache->pGroup);
55436 PGroup *pGroup = pCache->pGroup;
55440 assert( pCache->nPage >= pCache->nRecyclable );
55441 nPinned = pCache->nPage - pCache->nRecyclable;
55442 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
55443 assert( pCache->n90pct == pCache->nMax*9/10 );
55445 nPinned>=pGroup->mxPinned
55446 || nPinned>=pCache->n90pct
55447 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
55452 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
55453 assert( pCache->nHash>0 && pCache->apHash );
55456 if( pCache->bPurgeable
55457 && !pGroup->lru.pLruPrev->isAnchor
55458 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
55461 pPage = pGroup->lru.pLruPrev;
55465 pOther = pPage->pCache;
55466 if( pOther->szAlloc != pCache->szAlloc ){
55470 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
55482 unsigned int h = iKey % pCache->nHash;
55483 pCache->nPage++;
55484 pPage->iKey = iKey;
55485 pPage->pNext = pCache->apHash[h];
55486 pPage->pCache = pCache;
55487 pPage->pLruNext = 0;
55488 /* pPage->pLruPrev = 0;
55490 *(void **)pPage->page.pExtra = 0;
55491 pCache->apHash[h] = pPage;
55492 if( iKey>pCache->iMaxKey ){
55493 pCache->iMaxKey = iKey;
55509 ** For a non-purgeable cache (a cache used as the storage for an in-memory
55512 ** a non-purgeable cache.
55555 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
55567 pPage = pCache->apHash[iKey % pCache->nHash];
55568 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
55596 pcache1EnterMutex(pCache->pGroup);
55598 assert( pPage==0 || pCache->iMaxKey>=iKey );
55599 pcache1LeaveMutex(pCache->pGroup);
55613 assert( pCache->bPurgeable || createFlag!=1 );
55614 assert( pCache->bPurgeable || pCache->nMin==0 );
55615 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
55616 assert( pCache->nMin==0 || pCache->bPurgeable );
55617 assert( pCache->nHash>0 );
55619 if( pCache->pGroup->mutex ){
55641 PGroup *pGroup = pCache->pGroup;
55643 assert( pPage->pCache==pCache );
55649 assert( pPage->pLruNext==0 );
55652 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
55656 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
55657 pPage->pLruPrev = &pGroup->lru;
55658 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
55660 pCache->nRecyclable++;
55663 pcache1LeaveMutex(pCache->pGroup);
55679 assert( pPage->iKey==iOld );
55680 assert( pPage->pCache==pCache );
55683 pcache1EnterMutex(pCache->pGroup);
55686 hOld = iOld%pCache->nHash;
55687 pp = &pCache->apHash[hOld];
55689 pp = &(*pp)->pNext;
55691 *pp = pPage->pNext;
55694 hNew = iNew%pCache->nHash;
55695 pPage->iKey = iNew;
55696 pPage->pNext = pCache->apHash[hNew];
55697 pCache->apHash[hNew] = pPage;
55698 if( iNew>pCache->iMaxKey ){
55699 pCache->iMaxKey = iNew;
55702 pcache1LeaveMutex(pCache->pGroup);
55714 pcache1EnterMutex(pCache->pGroup);
55715 if( iLimit<=pCache->iMaxKey ){
55717 pCache->iMaxKey = iLimit-1;
55719 pcache1LeaveMutex(pCache->pGroup);
55729 PGroup *pGroup = pCache->pGroup;
55730 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
55732 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
55733 assert( pGroup->nMaxPage >= pCache->nMax );
55734 pGroup->nMaxPage -= pCache->nMax;
55735 assert( pGroup->nMinPage >= pCache->nMin );
55736 pGroup->nMinPage -= pCache->nMin;
55737 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
55740 sqlite3_free(pCache->pBulk);
55741 sqlite3_free(pCache->apHash);
55801 && p->isAnchor==0
55803 nFree += pcache1MemSize(p->page.pBuf);
55827 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
55887 ** a non-zero batch number, it will see all prior INSERTs.
55915 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
55973 p->pChunk = 0;
55974 p->db = db;
55975 p->pEntry = 0;
55976 p->pLast = 0;
55977 p->pForest = 0;
55978 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
55979 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
55980 p->rsFlags = ROWSET_SORTED;
55981 p->iBatch = 0;
55994 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
55995 pNextChunk = pChunk->pNextChunk;
55996 sqlite3DbFree(p->db, pChunk);
55998 p->pChunk = 0;
55999 p->nFresh = 0;
56000 p->pEntry = 0;
56001 p->pLast = 0;
56002 p->pForest = 0;
56003 p->rsFlags = ROWSET_SORTED;
56013 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
56021 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
56026 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
56030 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
56034 pNew->pNextChunk = p->pChunk;
56035 p->pChunk = pNew;
56036 p->pFresh = pNew->aEntry;
56037 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
56039 p->nFresh--;
56040 return p->pFresh++;
56054 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
56058 pEntry->v = rowid;
56059 pEntry->pRight = 0;
56060 pLast = p->pLast;
56062 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
56065 p->rsFlags &= ~ROWSET_SORTED;
56067 pLast->pRight = pEntry;
56069 p->pEntry = pEntry;
56071 p->pLast = pEntry;
56090 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
56091 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
56092 if( pA->v<=pB->v ){
56093 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
56094 pA = pA->pRight;
56096 pTail->pRight = pB;
56100 pTail = pTail->pRight = pB;
56101 pB = pB->pRight;
56103 pTail->pRight = pA;
56121 pNext = pIn->pRight;
56122 pIn->pRight = 0;
56150 if( pIn->pLeft ){
56152 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
56153 p->pRight = pIn;
56157 if( pIn->pRight ){
56158 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
56162 assert( (*ppLast)->pRight==0 );
56185 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
56189 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
56193 pLeft = rowSetNDeepTree(ppList, iDepth-1);
56195 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
56200 p->pLeft = pLeft;
56201 *ppList = p->pRight;
56202 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
56205 *ppList = p->pRight;
56206 p->pLeft = p->pRight = 0;
56222 pList = p->pRight;
56223 p->pLeft = p->pRight = 0;
56227 pList = p->pRight;
56228 p->pLeft = pLeft;
56229 p->pRight = rowSetNDeepTree(&pList, iDepth);
56249 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
56252 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56253 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56254 p->pEntry = rowSetEntrySort(p->pEntry);
56256 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
56260 if( p->pEntry ){
56261 *pRowid = p->pEntry->v;
56262 p->pEntry = p->pEntry->pRight;
56263 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
56278 ** on pRowSet->pEntry, then sort those entries into the forest at
56279 ** pRowSet->pForest so that they can be tested.
56285 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
56290 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
56291 p = pRowSet->pEntry;
56293 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
56294 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
56298 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56299 ppPrevTree = &pTree->pRight;
56300 if( pTree->pLeft==0 ){
56301 pTree->pLeft = rowSetListToTree(p);
56305 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
56306 pTree->pLeft = 0;
56313 pTree->v = 0;
56314 pTree->pRight = 0;
56315 pTree->pLeft = rowSetListToTree(p);
56318 pRowSet->pEntry = 0;
56319 pRowSet->pLast = 0;
56320 pRowSet->rsFlags |= ROWSET_SORTED;
56322 pRowSet->iBatch = iBatch;
56328 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
56329 p = pTree->pLeft;
56331 if( p->v<iRowid ){
56332 p = p->pRight;
56333 }else if( p->v>iRowid ){
56334 p = p->pLeft;
56380 ** This header file defines the interface to the write-ahead logging
56421 /* Connection to a write-ahead log (WAL) file.
56426 /* Open and close a connection to a write-ahead log. */
56434 ** snapshot is like a read-transaction. It is the state of the database
56443 /* Read a page from the write-ahead log, if it is present. */
56470 Wal *pWal, /* Write-ahead log connection */
56494 /* Return true if the argument is non-NULL and the WAL module is using
56495 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
56496 ** WAL module is using shared-memory, return false.
56510 ** stored in each frame (i.e. the db page-size when the WAL was created).
56581 ** (6) If a super-journal file is used, then all writes to the database file
56582 ** are synced prior to the super-journal being deleted.
56598 ** method is a no-op, but that does not change the fact the SQLite will
56609 ** (11) A database file is well-formed at the beginning and at the conclusion
56633 ** to print out file-descriptors.
56636 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
56639 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
56647 ** OPEN <------+------+
56650 ** +---------> READER-------+ |
56653 ** |<-------WRITER_LOCKED------> ERROR
56656 ** |<------WRITER_CACHEMOD-------->|
56659 ** |<-------WRITER_DBMOD---------->|
56662 ** +<------WRITER_FINISHED-------->+
56667 ** OPEN -> READER [sqlite3PagerSharedLock]
56668 ** READER -> OPEN [pager_unlock]
56670 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
56671 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
56672 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
56673 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
56674 ** WRITER_*** -> READER [pager_end_transaction]
56676 ** WRITER_*** -> ERROR [pager_error]
56677 ** ERROR -> OPEN [pager_unlock]
56683 ** state - the file may or may not be locked and the database size is
56693 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
56694 ** was) in exclusive-locking mode, a user-level read transaction is
56698 ** it opens a read-transaction on the database and returns to state
56699 ** OPEN after the read-transaction is completed. However a connection
56701 ** this state even after the read-transaction is closed. The only way
56705 ** * A read transaction may be active (but a write-transaction cannot).
56707 ** * The dbSize variable may be trusted (even if a user-level read
56711 ** * Even if a read-transaction is not open, it is guaranteed that
56712 ** there is no hot-journal in the file-system.
56716 ** The pager moves to this state from READER when a write-transaction
56718 ** required to start a write-transaction are held, but no actual
56733 ** * If the connection is open in rollback-mode, a RESERVED or greater
56735 ** * If the connection is open in WAL-mode, a WAL write transaction
56774 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
56790 ** The ERROR state is entered when an IO or disk-full error (including
56792 ** difficult to be sure that the in-memory pager state (cache contents,
56793 ** db size etc.) are consistent with the contents of the file-system.
56795 ** Temporary pager files may enter the ERROR state, but in-memory pagers
56799 ** the contents of the page-cache may be left in an inconsistent state.
56811 ** page-cache and any other in-memory state at the same time. Everything
56812 ** is reloaded from disk (and, if necessary, hot-journal rollback performed)
56813 ** when a read-transaction is next opened on the pager (transitioning
56829 ** In other cases, the error is returned to the b-tree layer. The b-tree
56833 ** Condition (3) is necessary because it can be triggered by a read-only
56835 ** code were simply returned to the user, the b-tree layer would not
56837 ** read-only statement cannot leave the pager in an internally inconsistent
56843 ** * The pager is not an in-memory pager.
56853 ** state. There are two exceptions: immediately after exclusive-mode has
56869 ** following locking-states, according to the lock currently held on
56877 ** pagerUnlockDb() take a conservative approach - eLock is always updated
56888 ** from ERROR to OPEN state. At this point there may be a hot-journal file
56889 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
56893 ** of hot-journal detection.
56899 ** a hot-journal may be mistaken for a journal being created by an active
56906 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
56907 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
56908 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
56933 ** set to 0. If a journal-header is written into the main journal while
56936 ** journal before the journal-header. This is required during savepoint
56945 Pgno iSubRec; /* Index of first record in sub-journal */
56970 ** For a real on-disk database, the current lock held on the database file -
56973 ** For a temporary or in-memory database (neither of which require any
56985 ** This boolean variable is used to make sure that the change-counter
56986 ** (the 4-byte header field at byte offset 24 of the database file) is
56989 ** It is set to true when the change-counter field is updated, which
56994 ** updating the change-counter is omitted for the current transaction.
56997 ** need only update the change-counter once, for the first transaction
57003 ** (or may not) specify a super-journal name to be written into the
57006 ** Whether or not a journal file contains a super-journal pointer affects
57009 ** If a journal file does not contain a super-journal pointer, it is
57011 ** it does contain a super-journal pointer the journal file is finalized
57015 ** Journal files that contain super-journal pointers cannot be finalized
57016 ** simply by overwriting the first journal-header with zeroes, as the
57017 ** super-journal pointer could interfere with hot-journal rollback of any
57027 ** This variables control the behavior of cache-spills (calls made by
57029 ** to the file-system in order to free up memory).
57041 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
57042 ** is larger than the database page-size in order to prevent a journal sync
57047 ** This is a boolean variable. If true, then any required sub-journal
57048 ** is opened as an in-memory journal file. If false, then in-memory
57049 ** sub-journals are only used for in-memory pager files.
57052 ** write-transaction is opened.
57063 ** is not an integer multiple of the page-size, the value stored in
57064 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
57066 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
57069 ** During a write-transaction, if pages with page-numbers greater than
57080 ** Throughout a write-transaction, dbFileSize contains the size of
57082 ** write-transaction is first opened, and updated when VFS calls are made
57102 ** write-transaction is opened (at the same time as dbFileSize and
57105 ** size-hint passed to the method call. See pager_write_pagelist() for
57113 ** sub-codes.
57139 u8 readOnly; /* True for a read-only database */
57141 u8 memVfs; /* VFS-implemented memory database */
57154 u8 changeCountDone; /* Set after incrementing the change-counter */
57155 u8 setSuper; /* Super-jrnl name is written into jrnl */
57156 u8 doNotSpill; /* Do not spill the cache when non-zero */
57157 u8 subjInMemory; /* True to use in-memory sub-journals */
57165 int nRec; /* Pages journalled since last j-header written */
57166 u32 cksumInit; /* Quasi-random value added to every checksum */
57167 u32 nSubRec; /* Number of records written to sub-journal */
57171 sqlite3_file *sjfd; /* File descriptor for sub-journal */
57184 ** End of the routinely-changing class members
57187 u16 nExtra; /* Add this many bytes to each in-memory page */
57208 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
57209 char *zWal; /* File name for write-ahead log */
57226 ** a non-testing build. These variables are not thread-safe.
57245 ** written, semi-random garbage data might appear in the journal
57252 ** of a 32-bit checksum on each page of data. The checksum covers both
57253 ** the page number and the pPager->pageSize bytes of data for the page.
57254 ** This cksum is initialized to a 32-bit random value that appears in the
57270 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
57276 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
57279 ** The macro MEMDB is true if we are dealing with an in-memory database.
57287 # define MEMDB pPager->memDb
57292 ** interfaces to access the database using memory-mapped I/O.
57295 # define USEFETCH(x) ((x)->bUseFetch)
57302 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
57306 ** if( isOpen(pPager->jfd) ){ ...
57310 ** if( pPager->jfd->pMethods ){ ...
57312 #define isOpen(pFd) ((pFd)->pMethods!=0)
57317 ** by the b-tree layer. This is the case if:
57324 if( pPager->fd->pMethods==0 ) return 0;
57325 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
57327 if( pPager->pWal ){
57330 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
57339 # define pagerUseWal(x) ((x)->pWal!=0)
57361 assert( p->eState==PAGER_OPEN
57362 || p->eState==PAGER_READER
57363 || p->eState==PAGER_WRITER_LOCKED
57364 || p->eState==PAGER_WRITER_CACHEMOD
57365 || p->eState==PAGER_WRITER_DBMOD
57366 || p->eState==PAGER_WRITER_FINISHED
57367 || p->eState==PAGER_ERROR
57370 /* Regardless of the current state, a temp-file connection always behaves
57372 ** the change-counter field, so the changeCountDone flag is always set.
57374 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
57375 assert( p->tempFile==0 || pPager->changeCountDone );
57377 /* If the useJournal flag is clear, the journal-mode must be "OFF".
57378 ** And if the journal-mode is "OFF", the journal file must not be open.
57380 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
57381 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
57383 /* Check that MEMDB implies noSync. And an in-memory journal. Since
57384 ** this means an in-memory pager performs no IO at all, it cannot encounter
57386 ** a journal file. (although the in-memory journal implementation may
57388 ** is therefore not possible for an in-memory pager to enter the ERROR
57392 assert( !isOpen(p->fd) );
57393 assert( p->noSync );
57394 assert( p->journalMode==PAGER_JOURNALMODE_OFF
57395 || p->journalMode==PAGER_JOURNALMODE_MEMORY
57397 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
57404 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
57405 assert( p->eLock!=PENDING_LOCK );
57407 switch( p->eState ){
57410 assert( pPager->errCode==SQLITE_OK );
57411 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
57415 assert( pPager->errCode==SQLITE_OK );
57416 assert( p->eLock!=UNKNOWN_LOCK );
57417 assert( p->eLock>=SHARED_LOCK );
57421 assert( p->eLock!=UNKNOWN_LOCK );
57422 assert( pPager->errCode==SQLITE_OK );
57424 assert( p->eLock>=RESERVED_LOCK );
57426 assert( pPager->dbSize==pPager->dbOrigSize );
57427 assert( pPager->dbOrigSize==pPager->dbFileSize );
57428 assert( pPager->dbOrigSize==pPager->dbHintSize );
57429 assert( pPager->setSuper==0 );
57433 assert( p->eLock!=UNKNOWN_LOCK );
57434 assert( pPager->errCode==SQLITE_OK );
57441 assert( p->eLock>=RESERVED_LOCK );
57442 assert( isOpen(p->jfd)
57443 || p->journalMode==PAGER_JOURNALMODE_OFF
57444 || p->journalMode==PAGER_JOURNALMODE_WAL
57447 assert( pPager->dbOrigSize==pPager->dbFileSize );
57448 assert( pPager->dbOrigSize==pPager->dbHintSize );
57452 assert( p->eLock==EXCLUSIVE_LOCK );
57453 assert( pPager->errCode==SQLITE_OK );
57455 assert( p->eLock>=EXCLUSIVE_LOCK );
57456 assert( isOpen(p->jfd)
57457 || p->journalMode==PAGER_JOURNALMODE_OFF
57458 || p->journalMode==PAGER_JOURNALMODE_WAL
57459 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57461 assert( pPager->dbOrigSize<=pPager->dbHintSize );
57465 assert( p->eLock==EXCLUSIVE_LOCK );
57466 assert( pPager->errCode==SQLITE_OK );
57468 assert( isOpen(p->jfd)
57469 || p->journalMode==PAGER_JOURNALMODE_OFF
57470 || p->journalMode==PAGER_JOURNALMODE_WAL
57471 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57480 assert( pPager->errCode!=SQLITE_OK );
57481 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
57514 , p->zFilename
57515 , p->eState==PAGER_OPEN ? "OPEN" :
57516 p->eState==PAGER_READER ? "READER" :
57517 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
57518 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
57519 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
57520 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
57521 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
57522 , (int)p->errCode
57523 , p->eLock==NO_LOCK ? "NO_LOCK" :
57524 p->eLock==RESERVED_LOCK ? "RESERVED" :
57525 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
57526 p->eLock==SHARED_LOCK ? "SHARED" :
57527 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
57528 , p->exclusiveMode ? "exclusive" : "normal"
57529 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
57530 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
57531 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
57532 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
57533 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
57534 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
57535 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
57536 , p->journalOff, p->journalHdr
57537 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
57556 if( pPager->errCode ){
57557 pPager->xGet = getPageError;
57560 pPager->xGet = getPageMMap;
57563 pPager->xGet = getPageNormal;
57568 ** Return true if it is necessary to write page *pPg into the sub-journal.
57569 ** A page needs to be written into the sub-journal if there exists one
57572 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
57573 ** * The bit corresponding to the page-number is not set in
57577 Pager *pPager = pPg->pPager;
57579 Pgno pgno = pPg->pgno;
57581 for(i=0; i<pPager->nSavepoint; i++){
57582 p = &pPager->aSavepoint[i];
57583 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
57584 for(i=i+1; i<pPager->nSavepoint; i++){
57585 pPager->aSavepoint[i].bTruncateOnRelease = 0;
57598 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
57603 ** Read a 32-bit integer from the given file descriptor. Store the integer
57607 ** All values are stored on disk as big-endian.
57619 ** Write a 32-bit integer into a string buffer in big-endian byte order.
57625 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
57646 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
57649 if( isOpen(pPager->fd) ){
57650 assert( pPager->eLock>=eLock );
57651 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
57652 if( pPager->eLock!=UNKNOWN_LOCK ){
57653 pPager->eLock = (u8)eLock;
57657 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
57675 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
57676 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
57677 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
57678 pPager->eLock = (u8)eLock;
57686 ** This function determines whether or not the atomic-write or
57687 ** atomic-batch-write optimizations can be used with this pager. The
57688 ** atomic-write optimization can be used if:
57698 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
57699 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
57711 assert( isOpen(pPager->fd) );
57712 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
57718 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
57719 return -1;
57725 int nSector = pPager->sectorSize;
57726 int szPage = pPager->pageSize;
57748 ** Return a 32-bit hash of the page data for pPage.
57759 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
57762 pPage->pageHash = pager_pagehash(pPage);
57768 ** that the page is either dirty or still matches the calculated page-hash.
57772 Pager *pPager = pPg->pPager;
57773 assert( pPager->eState!=PAGER_ERROR );
57774 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
57786 ** This function attempts to read a super-journal file name from the
57789 ** used to store a super-journal file name at the end of a journal file.
57793 ** enough space to write the super-journal name). If the super-journal
57795 ** nul-terminator), then this is handled as if no super-journal name
57798 ** If a super-journal file name is present at the end of the journal
57800 ** nul-terminator byte is appended to the buffer following the
57801 ** super-journal file name.
57803 ** If it is determined that no super-journal file name is present
57811 u32 len; /* Length in bytes of super-journal name */
57820 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
57822 || len>szJ-16
57824 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
57825 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
57827 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
57832 /* See if the checksum matches the super-journal name */
57834 cksum -= zSuper[u];
57838 ** containing the super-journal filename is corrupted. This means
57840 ** super-journal filename.
57852 ** following the value in pPager->journalOff, assuming a sector
57853 ** size of pPager->sectorSize bytes.
57858 ** ---------------------------------------
57867 i64 c = pPager->journalOff;
57869 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
57873 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
57880 ** This function is a no-op if the journal file has not been written to
57883 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
57885 ** zero the 28-byte header at the start of the journal file. In either case,
57886 ** if the pager is not in no-sync mode, sync the journal file immediately
57889 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
57900 assert( isOpen(pPager->jfd) );
57901 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
57902 if( pPager->journalOff ){
57903 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
57907 rc = sqlite3OsTruncate(pPager->jfd, 0);
57910 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
57912 if( rc==SQLITE_OK && !pPager->noSync ){
57913 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
57924 rc = sqlite3OsFileSize(pPager->jfd, &sz);
57926 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
57939 ** - 8 bytes: Magic identifying journal format.
57940 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
57941 ** - 4 bytes: Random number used for page hash.
57942 ** - 4 bytes: Initial database page count.
57943 ** - 4 bytes: Sector size used by the process that wrote this journal.
57944 ** - 4 bytes: Database page size.
57946 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
57950 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
57951 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
57955 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57965 for(ii=0; ii<pPager->nSavepoint; ii++){
57966 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
57967 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
57971 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
57974 ** Write the nRec Field - the number of page records that follow this
57977 ** if in full-sync mode), the zero is overwritten with the true number
57987 ** * When the pager is in no-sync mode. Corruption can follow a
57993 assert( isOpen(pPager->fd) || pPager->noSync );
57994 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
57995 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
58005 /* The random check-hash initializer */
58006 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58007 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58021 sqlite3OsFileSize(pPager->jfd, &sz);
58023 assert( pPager->journalOff==journalHdrOffset(pPager) );
58024 assert( sqlite3JournalIsInMemory(pPager->jfd) );
58027 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
58030 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
58032 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
58035 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
58043 nHeader-(sizeof(aJournalMagic)+20));
58054 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
58057 ** The loop is required here in case the sector-size is larger than the
58063 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
58064 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
58065 assert( pPager->journalHdr <= pPager->journalOff );
58066 pPager->journalOff += nHeader;
58076 ** pPager->journalOff. See comments above function writeJournalHdr() for
58081 ** database before the transaction began, in pages. Also, pPager->cksumInit
58100 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
58106 pPager->journalOff = journalHdrOffset(pPager);
58107 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
58110 iHdrOff = pPager->journalOff;
58117 if( isHot || iHdrOff!=pPager->journalHdr ){
58118 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
58127 /* Read the first three 32-bit fields of the journal header: The nRec
58128 ** field, the checksum-initializer and the database size at the start
58131 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
58132 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
58133 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
58138 if( pPager->journalOff==0 ){
58139 u32 iPageSize; /* Page-size field of journal header */
58140 u32 iSectorSize; /* Sector-size field of journal header */
58142 /* Read the page-size and sector-size journal header fields. */
58143 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
58144 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
58149 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
58154 iPageSize = pPager->pageSize;
58157 /* Check that the values read from the page-size and sector-size fields
58164 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
58166 /* If the either the page-size or sector-size in the journal-header is
58167 ** invalid, then the process that wrote the journal-header must have
58174 /* Update the page-size to match the value read from the journal.
58178 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
58181 /* Update the assumed sector-size to match the value used by
58187 pPager->sectorSize = iSectorSize;
58190 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
58196 ** Write the supplied super-journal name into the journal file for pager
58197 ** pPager at the current location. The super-journal name must be the last
58198 ** thing written to a journal file. If the pager is in full-sync mode, the
58203 ** + N bytes: super-journal filename in utf-8.
58204 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
58205 ** + 4 bytes: super-journal name checksum.
58208 ** The super-journal page checksum is the sum of the bytes in the super-journal
58209 ** name, where each byte is interpreted as a signed 8-bit integer.
58212 ** this call is a no-op.
58221 assert( pPager->setSuper==0 );
58225 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58226 || !isOpen(pPager->jfd)
58230 pPager->setSuper = 1;
58231 assert( pPager->journalHdr <= pPager->journalOff );
58238 /* If in full-sync mode, advance to the next disk sector before writing
58239 ** the super-journal name. This is in case the previous page written to
58242 if( pPager->fullSync ){
58243 pPager->journalOff = journalHdrOffset(pPager);
58245 iHdrOff = pPager->journalOff;
58247 /* Write the super-journal data to the end of the journal file. If
58250 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
58251 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
58252 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
58253 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
58254 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
58259 pPager->journalOff += (nSuper+20);
58261 /* If the pager is in persistent-journal mode, then the physical
58262 ** journal-file may extend past the end of the super-journal name
58264 ** dangerous because the code to rollback a hot-journal file
58265 ** will not be able to find the super-journal name to determine
58271 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
58272 && jrnlSize>pPager->journalOff
58274 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
58280 ** Discard the entire contents of the in-memory page-cache.
58283 pPager->iDataVersion++;
58284 sqlite3BackupRestart(pPager->pBackup);
58285 sqlite3PcacheClear(pPager->pPCache);
58289 ** Return the pPager->iDataVersion value
58292 return pPager->iDataVersion;
58297 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
58302 for(ii=0; ii<pPager->nSavepoint; ii++){
58303 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
58305 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
58306 sqlite3OsClose(pPager->sjfd);
58308 sqlite3_free(pPager->aSavepoint);
58309 pPager->aSavepoint = 0;
58310 pPager->nSavepoint = 0;
58311 pPager->nSubRec = 0;
58323 for(ii=0; ii<pPager->nSavepoint; ii++){
58324 PagerSavepoint *p = &pPager->aSavepoint[ii];
58325 if( pgno<=p->nOrig ){
58326 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
58335 ** This function is a no-op if the pager is in exclusive mode and not
58339 ** If the pager is not in exclusive-access mode, the database file is
58340 ** completely unlocked. If the file is unlocked and the file-system does
58346 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
58347 ** or not, any journal file left in the file-system will be treated
58348 ** as a hot-journal and rolled back the next time a read-transaction
58353 assert( pPager->eState==PAGER_READER
58354 || pPager->eState==PAGER_OPEN
58355 || pPager->eState==PAGER_ERROR
58358 sqlite3BitvecDestroy(pPager->pInJournal);
58359 pPager->pInJournal = 0;
58363 assert( !isOpen(pPager->jfd) );
58364 sqlite3WalEndReadTransaction(pPager->pWal);
58365 pPager->eState = PAGER_OPEN;
58366 }else if( !pPager->exclusiveMode ){
58368 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
58382 || 1!=(pPager->journalMode & 5)
58384 sqlite3OsClose(pPager->jfd);
58393 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
58394 pPager->eLock = UNKNOWN_LOCK;
58398 ** without clearing the error code. This is intentional - the error
58401 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
58402 pPager->eState = PAGER_OPEN;
58408 ** normal and exclusive-locking mode.
58410 assert( pPager->errCode==SQLITE_OK || !MEMDB );
58411 if( pPager->errCode ){
58412 if( pPager->tempFile==0 ){
58414 pPager->changeCountDone = 0;
58415 pPager->eState = PAGER_OPEN;
58417 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
58419 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
58420 pPager->errCode = SQLITE_OK;
58424 pPager->journalOff = 0;
58425 pPager->journalHdr = 0;
58426 pPager->setSuper = 0;
58433 ** the error-code about to be returned by a pager API function. The
58437 ** IOERR sub-codes, the pager enters the ERROR state and the error code
58441 ** The ERROR state indicates that the contents of the pager-cache
58443 ** the contents of the pager-cache. If a transaction was active when
58446 ** it were a hot-journal).
58452 pPager->errCode==SQLITE_FULL ||
58453 pPager->errCode==SQLITE_OK ||
58454 (pPager->errCode & 0xff)==SQLITE_IOERR
58457 pPager->errCode = rc;
58458 pPager->eState = PAGER_ERROR;
58474 ** * For non-TEMP databases, always sync to disk. This is necessary
58483 if( pPager->tempFile==0 ) return 1;
58485 if( !isOpen(pPager->fd) ) return 0;
58486 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
58492 ** after rollback of a hot-journal, or if an error occurs while opening
58493 ** the journal file or writing the very first journal-header of a
58498 ** exclusive than a RESERVED lock, it is a no-op.
58504 ** transaction. Nor will it be considered to be a hot-journal by this
58507 ** the current journal-mode (Pager.journalMode value), as follows:
58511 ** in-memory journal.
58530 ** If running in non-exclusive rollback mode, the lock on the file is
58548 ** is no write-transaction active but a RESERVED or greater lock is
58551 ** 1. After a successful hot-journal rollback, it is called with
58556 ** read-transaction, this function is called with eState==PAGER_READER
58557 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
58560 assert( pPager->eState!=PAGER_ERROR );
58561 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
58566 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
58567 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
58569 if( isOpen(pPager->jfd) ){
58573 if( sqlite3JournalIsInMemory(pPager->jfd) ){
58574 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
58575 sqlite3OsClose(pPager->jfd);
58576 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
58577 if( pPager->journalOff==0 ){
58580 rc = sqlite3OsTruncate(pPager->jfd, 0);
58581 if( rc==SQLITE_OK && pPager->fullSync ){
58587 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
58590 pPager->journalOff = 0;
58591 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
58592 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
58594 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
58595 pPager->journalOff = 0;
58598 ** a hot-journal was just rolled back. In this case the journal
58600 ** the database file, it will do so using an in-memory journal.
58602 int bDelete = !pPager->tempFile;
58603 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
58604 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
58605 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58606 || pPager->journalMode==PAGER_JOURNALMODE_WAL
58608 sqlite3OsClose(pPager->jfd);
58610 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
58616 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
58617 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
58620 p->pageHash = 0;
58626 sqlite3BitvecDestroy(pPager->pInJournal);
58627 pPager->pInJournal = 0;
58628 pPager->nRec = 0;
58631 sqlite3PcacheCleanAll(pPager->pPCache);
58633 sqlite3PcacheClearWritable(pPager->pPCache);
58635 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
58639 /* Drop the WAL write-lock, if any. Also, if the connection was in
58643 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
58645 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
58646 /* This branch is taken when committing a transaction in rollback-journal
58652 assert( pPager->eLock==EXCLUSIVE_LOCK );
58653 rc = pager_truncate(pPager, pPager->dbSize);
58657 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
58661 if( !pPager->exclusiveMode
58662 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
58666 pPager->eState = PAGER_READER;
58667 pPager->setSuper = 0;
58681 ** call to pager_unlock() will discard all in-memory pages, unlock
58683 ** means that there is a hot-journal left in the file-system, the next
58693 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
58695 if( pPager->eState>=PAGER_WRITER_LOCKED ){
58699 }else if( !pPager->exclusiveMode ){
58700 assert( pPager->eState==PAGER_READER );
58703 }else if( pPager->eState==PAGER_ERROR
58704 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58705 && isOpen(pPager->jfd)
58707 /* Special case for a ROLLBACK due to I/O error with an in-memory
58710 int errCode = pPager->errCode;
58711 u8 eLock = pPager->eLock;
58712 pPager->eState = PAGER_OPEN;
58713 pPager->errCode = SQLITE_OK;
58714 pPager->eLock = EXCLUSIVE_LOCK;
58716 pPager->errCode = errCode;
58717 pPager->eLock = eLock;
58723 ** Parameter aData must point to a buffer of pPager->pageSize bytes
58725 ** page of data and the current value of pPager->cksumInit.
58728 ** random initial value (pPager->cksumInit) and every 200th byte
58729 ** of the page data, starting with byte offset (pPager->pageSize%200).
58730 ** Each byte is interpreted as an 8-bit unsigned integer.
58739 ** though fast and simple, catches the mostly likely kind of corruption.
58742 u32 cksum = pPager->cksumInit; /* Checksum value to return */
58743 int i = pPager->pageSize-200; /* Loop counter */
58746 i -= 200;
58753 ** from the sub-journal (if isMainJrnl==0) and playback that page.
58757 ** The main rollback journal uses checksums - the statement journal does
58760 ** If the page number of the page record read from the (sub-)journal file
58770 ** If the page record is successfully read from the (sub-)journal file
58772 ** while reading the record from the (sub-)journal file or while writing
58774 ** is successfully read from the (sub-)journal file but appears to be
58778 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
58792 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
58805 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
58806 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
58808 aData = pPager->pTmpSpace;
58814 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
58815 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
58816 ** only reads from the main journal, not the sub-journal.
58818 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
58819 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
58821 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
58823 /* Read the page number and page data from the journal or sub-journal
58826 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
58829 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
58831 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
58842 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
58846 rc = read32bits(jfd, (*pOffset)-4, &cksum);
58862 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
58863 pPager->nReserve = ((u8*)aData)[20];
58870 ** An exception to the above rule: If the database is in no-sync mode
58880 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
58897 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
58907 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
58909 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
58910 (isMainJrnl?"main-journal":"sub-journal")
58913 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
58915 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
58917 if( isOpen(pPager->fd)
58918 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58921 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
58922 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
58926 ** This is usually safe even for an encrypted database - as the data
58928 ** is if the data was just read from an in-memory sub-journal. In that
58931 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
58933 if( pgno>pPager->dbFileSize ){
58934 pPager->dbFileSize = pgno;
58936 if( pPager->pBackup ){
58937 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
58941 ** the database and the page is not in-memory, there is a potential
58942 ** problem. When the page is next fetched by the b-tree layer, it
58948 ** if the page is on the free-list at the start of the transaction, then
58951 ** The solution is to add an in-memory page to the cache containing
58952 ** the data just read from the sub-journal. Mark the page as dirty
58953 ** and if the pager requires a journal-sync, then mark the page as
58954 ** requiring a journal-sync before it is written.
58957 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
58958 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
58960 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
58961 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
58973 pData = pPg->pData;
58974 memcpy(pData, (u8*)aData, pPager->pageSize);
58975 pPager->xReiniter(pPg);
58985 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
58993 ** Parameter zSuper is the name of a super-journal file. A single journal
58994 ** file that referred to the super-journal file has just been rolled back.
58995 ** This routine checks if it is possible to delete the super-journal file,
59001 ** When a super-journal file is created, it is populated with the names
59002 ** of all of its child journals, one after another, formatted as utf-8
59004 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
59007 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
59009 ** A super-journal file may only be deleted once all of its child
59012 ** This function reads the contents of the super-journal file into
59017 ** * if the child journal contains a reference to super-journal
59023 ** the file-system using sqlite3OsDelete().
59031 ** the entire contents of the super-journal file. This could be
59032 ** a couple of kilobytes or so - potentially larger than the page
59036 sqlite3_vfs *pVfs = pPager->pVfs;
59038 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
59039 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
59040 char *zSuperJournal = 0; /* Contents of super-journal file */
59041 i64 nSuperJournal; /* Size of super-journal file */
59043 char *zSuperPtr; /* Space to hold super-journal filename */
59048 ** If successful, open the super-journal file for reading.
59050 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
59057 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
59061 /* Load the entire super-journal file into space obtained from
59063 ** sufficient space (in zSuperPtr) to hold the names of super-journal
59064 ** files extracted from regular rollback-journals.
59068 nSuperPtr = pVfs->mxPathname+1;
59083 while( (zJournal-zSuperJournal)<nSuperJournal ){
59090 /* One of the journals pointed to by the super-journal exists.
59091 ** Open it and check if it points at the super-journal. If
59092 ** so, return without deleting the super-journal file.
59112 /* We have a match. Do not delete the super-journal file. */
59135 ** file in the file-system. This only happens when committing a transaction,
59136 ** or rolling back a transaction (including rolling back a hot-journal).
59139 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
59140 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
59155 assert( pPager->eState!=PAGER_ERROR );
59156 assert( pPager->eState!=PAGER_READER );
59160 if( isOpen(pPager->fd)
59161 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59164 int szPage = pPager->pageSize;
59165 assert( pPager->eLock==EXCLUSIVE_LOCK );
59167 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
59171 rc = sqlite3OsTruncate(pPager->fd, newSize);
59173 char *pTmp = pPager->pTmpSpace;
59175 testcase( (newSize-szPage) == currentSize );
59176 testcase( (newSize-szPage) > currentSize );
59177 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
59178 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
59181 pPager->dbFileSize = nPage;
59189 ** Return a sanitized version of the sector-size of OS file pFile. The
59208 ** super-journal pointers within created journal files.
59212 ** Otherwise, for non-temporary files, the effective sector size is
59219 ** pPager->sectorSize is to define the "blast radius" of bytes that
59227 assert( isOpen(pPager->fd) || pPager->tempFile );
59229 if( pPager->tempFile
59230 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
59236 pPager->sectorSize = 512;
59238 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
59249 ** (2) 4 byte big-endian integer which is the number of valid page records
59252 ** (3) 4 byte big-endian integer which is the initial value for the
59256 ** (5) 4 byte big-endian integer which is the sector size. The header
59258 ** (6) 4 byte big-endian integer which is the page size.
59262 ** + pPager->pageSize bytes of data.
59279 ** no-sync option for the journal. A power failure could lead to corruption
59283 ** If the file opened as the journal file is not a well-formed
59289 ** If an I/O or malloc() error occurs, the journal-file is not deleted
59300 sqlite3_vfs *pVfs = pPager->pVfs;
59307 char *zSuper = 0; /* Name of super-journal file if any */
59310 u32 savedPageSize = pPager->pageSize;
59315 assert( isOpen(pPager->jfd) );
59316 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
59321 /* Read the super-journal name from the journal, if it is present.
59322 ** If a super-journal file name is specified, but the file is not
59328 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
59332 zSuper = pPager->pTmpSpace;
59333 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
59341 pPager->journalOff = 0;
59363 ** working in no-sync mode. This means that the rest of the journal
59368 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
59369 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
59387 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
59388 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
59394 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
59399 pPager->dbSize = mxPg;
59400 if( pPager->mxPgno<mxPg ){
59401 pPager->mxPgno = mxPg;
59413 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
59418 pPager->journalOff = szJ;
59444 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
59448 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
59452 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
59456 ** malloc error that occurred after the change-counter was updated but
59457 ** before the transaction was committed, then the change-counter
59460 ** update the change-counter at all. This may lead to cache inconsistency
59464 pPager->changeCountDone = pPager->tempFile;
59467 /* Leave 4 bytes of space before the super-journal filename in memory.
59471 zSuper = &pPager->pTmpSpace[4];
59472 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
59476 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
59485 /* If there was a super-journal and this routine will return success,
59486 ** see if it is possible to delete the super-journal.
59488 assert( zSuper==&pPager->pTmpSpace[4] );
59489 memset(pPager->pTmpSpace, 0, 4);
59495 nPlayback, pPager->zJournal);
59510 ** pPg->pData. A shared lock or greater must be held on the database
59520 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
59526 assert( pPager->eState>=PAGER_READER && !MEMDB );
59527 assert( isOpen(pPager->fd) );
59530 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
59534 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
59538 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
59539 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
59545 if( pPg->pgno==1 ){
59552 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
59559 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
59561 u8 *dbFileVers = &((u8*)pPg->pData)[24];
59562 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
59566 PAGER_INCR(pPager->nRead);
59567 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
59569 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
59575 ** Update the value of the change-counter at offsets 24 and 92 in
59579 ** routine which only updates the change-counter if the update is actually
59580 ** needed, as determined by the pPager->changeCountDone state variable.
59587 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
59588 put32bits(((char*)pPg->pData)+24, change_counter);
59593 put32bits(((char*)pPg->pData)+92, change_counter);
59594 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
59623 pPager->xReiniter(pPg);
59637 sqlite3BackupRestart(pPager->pBackup);
59656 pPager->dbSize = pPager->dbOrigSize;
59657 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
59658 pList = sqlite3PcacheDirtyList(pPager->pPCache);
59660 PgHdr *pNext = pList->pDirty;
59661 rc = pagerUndoCallback((void *)pPager, pList->pgno);
59687 assert( pPager->pWal );
59691 for(p=pList; p && p->pDirty; p=p->pDirty){
59692 assert( p->pgno < p->pDirty->pgno );
59696 assert( pList->pDirty==0 || isCommit );
59704 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
59705 if( p->pgno<=nTruncate ){
59706 ppNext = &p->pDirty;
59714 pPager->aStat[PAGER_STAT_WRITE] += nList;
59716 if( pList->pgno==1 ) pager_write_changecounter(pList);
59717 rc = sqlite3WalFrames(pPager->pWal,
59718 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
59720 if( rc==SQLITE_OK && pPager->pBackup ){
59721 for(p=pList; p; p=p->pDirty){
59722 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
59727 pList = sqlite3PcacheDirtyList(pPager->pPCache);
59728 for(p=pList; p; p=p->pDirty){
59749 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
59756 sqlite3WalEndReadTransaction(pPager->pWal);
59758 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
59761 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59780 /* Query the WAL sub-system for the database size. The WalDbsize()
59783 ** available from the WAL sub-system if the log file is empty or
59786 assert( pPager->eState==PAGER_OPEN );
59787 assert( pPager->eLock>=SHARED_LOCK );
59788 assert( isOpen(pPager->fd) );
59789 assert( pPager->tempFile==0 );
59790 nPage = sqlite3WalDbsize(pPager->pWal);
59793 ** WAL sub-system, determine the page count based on the size of
59795 ** integer multiple of the page-size, round up the result.
59797 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
59799 int rc = sqlite3OsFileSize(pPager->fd, &n);
59803 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
59810 if( nPage>pPager->mxPgno ){
59811 pPager->mxPgno = (Pgno)nPage;
59820 ** Check if the *-wal file that corresponds to the database opened by pPager
59821 ** exists if the database is not empty, or verify that the *-wal file does
59824 ** If the database is not empty and the *-wal file exists, open the pager
59825 ** in WAL mode. If the database is empty or if no *-wal file exists and
59833 ** a WAL on a none-empty database, this ensures there is no race condition
59839 assert( pPager->eState==PAGER_OPEN );
59840 assert( pPager->eLock>=SHARED_LOCK );
59842 if( !pPager->tempFile ){
59845 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
59854 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
59856 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
59859 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
59860 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
59870 ** the entire super-journal file. The case pSavepoint==NULL occurs when
59874 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
59887 ** * Pages are then played back from the sub-journal file, starting
59901 ** (or transaction). No page with a page-number greater than this value
59906 i64 iHdrOff; /* End of first segment of main-journal records */
59910 assert( pPager->eState!=PAGER_ERROR );
59911 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59915 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
59924 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
59925 pPager->changeCountDone = pPager->tempFile;
59931 /* Use pPager->journalOff as the effective size of the main rollback
59934 ** past pPager->journalOff is off-limits to us.
59936 szJ = pPager->journalOff;
59942 ** greater than the current database size (pPager->dbSize) but those
59947 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
59948 pPager->journalOff = pSavepoint->iOffset;
59949 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
59950 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59954 pPager->journalOff = 0;
59959 ** of the main journal file. Continue to skip out-of-range pages and
59962 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
59970 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
59975 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
59977 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
59979 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
59980 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59984 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
59986 /* Finally, rollback pages from the sub-journal. Page that were
59988 ** will be skipped. Out-of-range pages are also skipped.
59992 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
59995 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
59997 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
59998 assert( offset==(i64)ii*(4+pPager->pageSize) );
60006 pPager->journalOff = szJ;
60013 ** Change the maximum number of in-memory pages that are allowed
60017 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
60021 ** Change the maximum number of in-memory pages that are allowed
60025 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
60033 sqlite3_file *fd = pPager->fd;
60034 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
60036 sz = pPager->szMmap;
60037 pPager->bUseFetch = (sz>0);
60039 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
60048 pPager->szMmap = szMmap;
60056 sqlite3PcacheShrink(pPager->pPCache);
60078 ** database (with some additional information - the nRec field
60079 ** of the journal header - being written in between the two
60089 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
60100 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
60115 if( pPager->tempFile ){
60116 pPager->noSync = 1;
60117 pPager->fullSync = 0;
60118 pPager->extraSync = 0;
60120 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
60121 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
60122 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
60124 if( pPager->noSync ){
60125 pPager->syncFlags = 0;
60127 pPager->syncFlags = SQLITE_SYNC_FULL;
60129 pPager->syncFlags = SQLITE_SYNC_NORMAL;
60131 pPager->walSyncFlags = (pPager->syncFlags<<2);
60132 if( pPager->fullSync ){
60133 pPager->walSyncFlags |= pPager->syncFlags;
60135 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
60136 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
60139 pPager->doNotSpill &= ~SPILLFLAG_OFF;
60141 pPager->doNotSpill |= SPILLFLAG_OFF;
60182 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
60190 ** The pager invokes the busy-handler if sqlite3OsLock() returns
60191 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
60195 ** (which occurs during hot-journal rollback). Summary:
60198 ** --------------------------------------------------------
60199 ** NO_LOCK -> SHARED_LOCK | Yes
60200 ** SHARED_LOCK -> RESERVED_LOCK | No
60201 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
60202 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
60204 ** If the busy-handler callback returns non-zero, the lock is
60210 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
60214 pPager->xBusyHandler = xBusyHandler;
60215 pPager->pBusyHandlerArg = pBusyHandlerArg;
60216 ap = (void **)&pPager->xBusyHandler;
60219 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
60227 ** is a no-op. The value returned is the error state error code (i.e.
60228 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
60237 ** * the database is either not an in-memory database or it is
60238 ** an in-memory database that currently consists of zero pages.
60262 ** is a no-op for that case anyhow.
60267 if( (pPager->memDb==0 || pPager->dbSize==0)
60268 && sqlite3PcacheRefCount(pPager->pPCache)==0
60269 && pageSize && pageSize!=(u32)pPager->pageSize
60274 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
60275 rc = sqlite3OsFileSize(pPager->fd, &nByte);
60278 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
60290 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
60293 sqlite3PageFree(pPager->pTmpSpace);
60294 pPager->pTmpSpace = pNew;
60295 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
60296 pPager->pageSize = pageSize;
60297 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
60303 *pPageSize = pPager->pageSize;
60305 if( nReserve<0 ) nReserve = pPager->nReserve;
60307 pPager->nReserve = (i16)nReserve;
60322 return pPager->pTmpSpace;
60334 pPager->mxPgno = mxPage;
60336 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
60337 /* assert( pPager->mxPgno>=pPager->dbSize ); */
60341 return pPager->mxPgno;
60349 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
60358 sqlite3_io_error_pending = -1;
60385 assert( isOpen(pPager->fd) || pPager->tempFile );
60393 if( isOpen(pPager->fd) ){
60395 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
60404 ** This function may only be called when a read-transaction is open on
60407 ** However, if the file is between 1 and <page-size> bytes in size, then
60411 assert( pPager->eState>=PAGER_READER );
60412 assert( pPager->eState!=PAGER_WRITER_FINISHED );
60413 *pnPage = (int)pPager->dbSize;
60419 ** a similar or greater lock is already held, this function is a no-op
60434 /* Check that this is either a no-op (because the requested lock is
60435 ** already held), or one of the transitions that the busy-handler
60439 assert( (pPager->eLock>=locktype)
60440 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
60441 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
60446 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
60452 ** following is true for all dirty pages currently in the page-cache:
60458 ** be necessary to write the current content out to the sub-journal.
60467 ** sub-journal rolled back the content could not be restored and the
60473 Pager *pPager = pPg->pPager;
60474 assert( pPg->flags&PGHDR_DIRTY );
60475 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
60476 Pgno pgno = pPg->pgno;
60478 for(i=0; i<pPg->pPager->nSavepoint; i++){
60479 PagerSavepoint *p = &pPager->aSavepoint[i];
60480 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
60485 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
60492 ** Truncate the in-memory database file image to nPage pages. This
60503 assert( pPager->dbSize>=nPage || CORRUPT_DB );
60504 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
60505 pPager->dbSize = nPage;
60520 ** This function is called before attempting a hot-journal rollback. It
60521 ** syncs the journal file to disk, then sets pPager->journalHdr to the
60525 ** Syncing a hot-journal to disk before attempting to roll it back ensures
60526 ** that if a power-failure occurs during the rollback, the process that
60535 if( !pPager->noSync ){
60536 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
60539 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
60563 if( pPager->pMmapFreelist ){
60564 *ppPage = p = pPager->pMmapFreelist;
60565 pPager->pMmapFreelist = p->pDirty;
60566 p->pDirty = 0;
60567 assert( pPager->nExtra>=8 );
60568 memset(p->pExtra, 0, 8);
60570 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
60572 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
60575 p->pExtra = (void *)&p[1];
60576 p->flags = PGHDR_MMAP;
60577 p->nRef = 1;
60578 p->pPager = pPager;
60581 assert( p->pExtra==(void *)&p[1] );
60582 assert( p->pPage==0 );
60583 assert( p->flags==PGHDR_MMAP );
60584 assert( p->pPager==pPager );
60585 assert( p->nRef==1 );
60587 p->pgno = pgno;
60588 p->pData = pData;
60589 pPager->nMmapOut++;
60600 Pager *pPager = pPg->pPager;
60601 pPager->nMmapOut--;
60602 pPg->pDirty = pPager->pMmapFreelist;
60603 pPager->pMmapFreelist = pPg;
60605 assert( pPager->fd->pMethods->iVersion>=3 );
60606 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
60615 for(p=pPager->pMmapFreelist; p; p=pNext){
60616 pNext = p->pDirty;
60623 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
60630 if( pPager->tempFile ) return SQLITE_OK;
60631 if( pPager->dbSize==0 ) return SQLITE_OK;
60632 assert( pPager->zFilename && pPager->zFilename[0] );
60633 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
60635 /* If the HAS_MOVED file-control is unimplemented, assume that the file
60661 u8 *pTmp = (u8*)pPager->pTmpSpace;
60667 /* pPager->errCode = 0; */
60668 pPager->exclusiveMode = 0;
60672 assert( db || pPager->pWal==0 );
60673 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
60678 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
60679 pPager->pWal = 0;
60694 ** back or finalize it. The next database user will have to do hot-journal
60697 if( isOpen(pPager->jfd) ){
60706 sqlite3OsClose(pPager->jfd);
60707 sqlite3OsClose(pPager->fd);
60709 sqlite3PcacheClose(pPager->pPCache);
60710 assert( !pPager->aSavepoint && !pPager->pInJournal );
60711 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
60722 return pPg->pgno;
60736 ** disk and can be restored in the event of a hot-journal rollback.
60738 ** If the Pager.noSync flag is set, then this function is a no-op.
60739 ** Otherwise, the actions required depend on the journal-mode and the
60740 ** device characteristics of the file-system, as follows:
60742 ** * If the journal file is an in-memory journal file, no action need
60748 ** been written following it. If the pager is operating in full-sync
60754 ** Or, in pseudo-code:
60756 ** if( NOT <in-memory journal> ){
60758 ** if( <full-sync mode> ) xSync(<journal file>);
60771 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60772 || pPager->eState==PAGER_WRITER_DBMOD
60780 if( !pPager->noSync ){
60781 assert( !pPager->tempFile );
60782 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
60783 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
60784 assert( isOpen(pPager->jfd) );
60788 ** that wrote to this database was operating in persistent-journal
60791 ** file happens to be a journal-header (written as part of the
60792 ** previous connection's transaction), and a crash or power-failure
60796 ** hot-journal rollback following recovery. It may roll back all
60798 ** out-of-date data that follows it. Database corruption.
60814 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
60817 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
60820 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
60827 ** full-synchronous mode, sync the journal first. This ensures that
60837 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
60840 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
60843 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
60845 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
60852 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
60853 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
60858 pPager->journalHdr = pPager->journalOff;
60860 pPager->nRec = 0;
60865 pPager->journalHdr = pPager->journalOff;
60873 sqlite3PcacheClearSyncFlags(pPager->pPCache);
60874 pPager->eState = PAGER_WRITER_DBMOD;
60882 ** in-memory pages in the list to the database file. The argument may
60884 ** a no-op.
60891 ** If the pager is a temp-file pager and the actual file-system file
60916 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
60917 assert( pPager->eLock==EXCLUSIVE_LOCK );
60918 assert( isOpen(pPager->fd) || pList->pDirty==0 );
60920 /* If the file is a temp-file has not yet been opened, open it now. It
60922 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
60924 if( !isOpen(pPager->fd) ){
60925 assert( pPager->tempFile && rc==SQLITE_OK );
60926 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
60932 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
60934 && pPager->dbHintSize<pPager->dbSize
60935 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
60937 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
60938 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
60939 pPager->dbHintSize = pPager->dbSize;
60943 Pgno pgno = pList->pgno;
60947 ** make the file smaller (presumably by auto-vacuum code). Do not write
60953 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
60954 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
60957 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
60958 if( pList->pgno==1 ) pager_write_changecounter(pList);
60960 pData = pList->pData;
60963 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
60970 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
60972 if( pgno>pPager->dbFileSize ){
60973 pPager->dbFileSize = pgno;
60975 pPager->aStat[PAGER_STAT_WRITE]++;
60978 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
60988 pList = pList->pDirty;
60995 ** Ensure that the sub-journal file is open. If it is already open, this
60996 ** function is a no-op.
61004 if( !isOpen(pPager->sjfd) ){
61009 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
61010 nStmtSpill = -1;
61012 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
61018 ** Append a record of the current state of page pPg to the sub-journal.
61020 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
61024 ** error code if the attempt to write to the sub-journal fails, or
61030 Pager *pPager = pPg->pPager;
61031 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61033 /* Open the sub-journal, if it has not already been opened */
61034 assert( pPager->useJournal );
61035 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
61036 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
61039 || pPg->pgno>pPager->dbOrigSize
61043 /* If the sub-journal was opened successfully (or was already open),
61046 void *pData = pPg->pData;
61047 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
61050 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
61051 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
61053 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
61058 pPager->nSubRec++;
61059 assert( pPager->nSavepoint>0 );
61060 rc = addToSavepointBitvecs(pPager, pPg->pgno);
61075 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
61095 assert( pPg->pPager==pPager );
61096 assert( pPg->flags&PGHDR_DIRTY );
61114 if( NEVER(pPager->errCode) ) return SQLITE_OK;
61115 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
61116 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
61117 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
61118 if( pPager->doNotSpill
61119 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
61120 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
61125 pPager->aStat[PAGER_STAT_SPILL]++;
61126 pPg->pDirty = 0;
61136 if( pPager->tempFile==0 ){
61137 rc = sqlite3JournalCreate(pPager->jfd);
61143 if( pPg->flags&PGHDR_NEED_SYNC
61144 || pPager->eState==PAGER_WRITER_CACHEMOD
61151 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
61158 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
61169 int rc = pPager->errCode;
61171 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
61174 PgHdr *pNext = pList->pDirty;
61175 if( pList->nRef==0 ){
61191 ** If zFilename is NULL then a randomly-named temporary file is created
61195 ** This can be used to implement an in-memory database.
61221 int nExtra, /* Extra bytes append to each in-memory page */
61229 int tempFile = 0; /* True for temp files (incl. in-memory files) */
61230 int memDb = 0; /* True if this is an in-memory file */
61232 int readOnly = 0; /* True if this is a read-only file */
61242 /* Figure out how much space is required for each journal file-handle
61243 ** (there are two of them, the main journal and the sub-journal). */
61267 nPathname = pVfs->mxPathname+1;
61289 nUriByte = (int)(&z[1] - zUri);
61291 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
61293 ** the database being opened will be more than pVfs->mxPathname
61296 ** check for a hot-journal before reading.
61312 ** Database file handle (pVfs->szOsFile bytes)
61313 ** Sub-journal file handle (journalFileSize bytes)
61323 ** Some 3rd-party software, over which we have no control, depends on
61327 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
61330 ** filename format expected by 3rd-party software should be as follows:
61332 ** - Main Database Path
61333 ** - \0
61334 ** - Multiple URI components consisting of:
61335 ** - Key
61336 ** - \0
61337 ** - Value
61338 ** - \0
61339 ** - \0
61340 ** - Journal Path
61341 ** - \0
61342 ** - WAL Path (zWALName)
61343 ** - \0
61354 ROUND8(pVfs->szOsFile) + /* The main db file */
61372 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
61373 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
61374 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61375 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61376 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
61381 pPager->zFilename = (char*)pPtr;
61394 pPager->zJournal = (char*)pPtr;
61396 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
61398 sqlite3FileSuffix3(zFilename,pPager->zJournal);
61399 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
61402 pPager->zJournal = 0;
61408 pPager->zWal = (char*)pPtr;
61410 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
61412 sqlite3FileSuffix3(zFilename, pPager->zWal);
61413 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
61416 pPager->zWal = 0;
61422 pPager->pVfs = pVfs;
61423 pPager->vfsFlags = vfsFlags;
61429 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
61431 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
61443 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
61447 if( szPageDflt<pPager->sectorSize ){
61448 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
61451 szPageDflt = (u32)pPager->sectorSize;
61468 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
61470 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
61480 ** This branch is also run for an in-memory database. An in-memory
61481 ** database is the same as a temp-file that is never written out to
61482 ** disk and uses an in-memory rollback journal.
61488 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
61489 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
61490 pPager->noLock = 1; /* Do no locking */
61498 assert( pPager->memDb==0 );
61499 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
61508 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
61514 sqlite3OsClose(pPager->fd);
61515 sqlite3PageFree(pPager->pTmpSpace);
61520 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
61521 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
61523 pPager->useJournal = (u8)useJournal;
61524 /* pPager->stmtOpen = 0; */
61525 /* pPager->stmtInUse = 0; */
61526 /* pPager->nRef = 0; */
61527 /* pPager->stmtSize = 0; */
61528 /* pPager->stmtJSize = 0; */
61529 /* pPager->nPage = 0; */
61530 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
61531 /* pPager->state = PAGER_UNLOCK; */
61532 /* pPager->errMask = 0; */
61533 pPager->tempFile = (u8)tempFile;
61537 pPager->exclusiveMode = (u8)tempFile;
61538 pPager->changeCountDone = pPager->tempFile;
61539 pPager->memDb = (u8)memDb;
61540 pPager->readOnly = (u8)readOnly;
61541 assert( useJournal || pPager->tempFile );
61543 /* pPager->pFirst = 0; */
61544 /* pPager->pFirstSynced = 0; */
61545 /* pPager->pLast = 0; */
61546 pPager->nExtra = (u16)nExtra;
61547 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
61548 assert( isOpen(pPager->fd) || tempFile );
61551 pPager->journalMode = PAGER_JOURNALMODE_OFF;
61553 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
61555 /* pPager->xBusyHandler = 0; */
61556 /* pPager->pBusyHandlerArg = 0; */
61557 pPager->xReiniter = xReinit;
61559 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
61560 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
61574 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61575 zName--;
61577 p = zName - 4 - sizeof(Pager*);
61580 return pPager->fd;
61587 ** the file-system for the given pager. A hot journal is one that
61588 ** needs to be played back. According to this function, a hot-journal
61602 ** This routine does not check if there is a super-journal filename
61603 ** at the end of the file. If there is, and that super-journal file
61605 ** case this routine will return a false-positive. The pager_playback()
61609 ** If a hot-journal file is found to exist, *pExists is set to 1 and
61610 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
61612 ** to determine whether or not a hot-journal file exists, the IO error
61616 sqlite3_vfs * const pVfs = pPager->pVfs;
61619 int jrnlOpen = !!isOpen(pPager->jfd);
61621 assert( pPager->useJournal );
61622 assert( isOpen(pPager->fd) );
61623 assert( pPager->eState==PAGER_OPEN );
61625 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
61631 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
61641 ** in fact there is none. This results in a false-positive which will
61644 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
61648 assert( pPager->tempFile==0 );
61662 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
61663 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
61669 ** at least one non-zero bytes at the start of the journal file.
61675 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
61679 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
61684 sqlite3OsClose(pPager->jfd);
61711 ** has been successfully called. If a shared-lock is already held when
61712 ** this function is called, it is a no-op.
61719 ** the SHARED lock, the file-system is checked for a hot-journal,
61720 ** which is played back if present. Following any hot-journal
61722 ** the 'change-counter' field of the database file header and
61725 ** 2) If the pager is running in exclusive-mode, and there are currently
61732 ** occurs while locking the database, checking for a hot-journal file or
61738 /* This routine is only called from b-tree and only when there are no
61742 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
61744 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
61745 assert( pPager->errCode==SQLITE_OK );
61747 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
61748 int bHotJournal = 1; /* True if there exists a hot journal-file */
61751 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
61755 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
61762 if( pPager->eLock<=SHARED_LOCK ){
61769 if( pPager->readOnly ){
61779 ** hot-journal back.
61796 ** in exclusive-access mode the file descriptor will be kept open
61797 ** and possibly used for a transaction later on. Also, write-access
61804 ** may mean that the pager was in the error-state when this
61807 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61808 sqlite3_vfs * const pVfs = pPager->pVfs;
61811 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
61815 assert( !pPager->tempFile );
61816 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
61817 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61820 sqlite3OsClose(pPager->jfd);
61827 ** playing back the hot-journal so that we don't end up with
61833 if( isOpen(pPager->jfd) ){
61837 rc = pager_playback(pPager, !pPager->tempFile);
61838 pPager->eState = PAGER_OPEN;
61840 }else if( !pPager->exclusiveMode ){
61846 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
61865 assert( pPager->eState==PAGER_OPEN );
61866 assert( (pPager->eLock==SHARED_LOCK)
61867 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
61871 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
61872 /* The shared-lock has just been acquired then check to
61876 ** single unnecessary sqlite3OsRead() call at the start-up.
61880 ** a 32-bit counter that is incremented with each change. The
61888 char dbFileVers[sizeof(pPager->dbFileVers)];
61891 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
61899 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
61909 sqlite3OsUnfetch(pPager->fd, 0, 0);
61914 /* If there is a WAL file in the file-system, open this database in WAL
61915 ** mode. Otherwise, the following function call is a no-op.
61919 assert( pPager->pWal==0 || rc==SQLITE_OK );
61928 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
61929 rc = pagerPagecount(pPager, &pPager->dbSize);
61936 assert( pPager->eState==PAGER_OPEN );
61938 pPager->eState = PAGER_READER;
61939 pPager->hasHeldSharedLock = 1;
61950 ** nothing to rollback, so this routine is a no-op.
61953 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
61954 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
61967 ** getPageNormal() -- The normal getter
61968 ** getPageError() -- Used if the pager is in an error state
61969 ** getPageMmap() -- Used if memory-mapped I/O is enabled
61991 ** a) When reading a free-list leaf page from the database, and
62009 ** to find a page in the in-memory cache first. If the page is not already
62011 ** just returns 0. This routine acquires a read-lock the first time it
62027 assert( pPager->errCode==SQLITE_OK );
62028 assert( pPager->eState>=PAGER_READER );
62030 assert( pPager->hasHeldSharedLock==1 );
62033 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
62036 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
62043 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
62045 assert( pPg->pgno==pgno );
62046 assert( pPg->pPager==pPager || pPg->pPager==0 );
62049 if( pPg->pPager && !noContent ){
62053 pPager->aStat[PAGER_STAT_HIT]++;
62068 pPg->pPager = pPager;
62070 assert( !isOpen(pPager->fd) || !MEMDB );
62071 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
62072 if( pgno>pPager->mxPgno ){
62074 if( pgno<=pPager->dbSize ){
62081 /* Failure to set the bits in the InJournal bit-vectors is benign.
62088 if( pgno<=pPager->dbOrigSize ){
62089 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
62096 memset(pPg->pData, 0, pPager->pageSize);
62099 assert( pPg->pPager==pPager );
62100 pPager->aStat[PAGER_STAT_MISS]++;
62121 /* The page getter for when memory-mapped I/O is enabled */
62132 /* It is acceptable to use a read-only (mmap) page for any page except
62133 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
62135 ** temporary or in-memory database. */
62137 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
62149 assert( pPager->eState>=PAGER_READER );
62151 assert( pPager->hasHeldSharedLock==1 );
62152 assert( pPager->errCode==SQLITE_OK );
62155 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
62163 rc = sqlite3OsFetch(pPager->fd,
62164 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
62167 if( pPager->eState>PAGER_READER || pPager->tempFile ){
62173 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
62199 assert( pPager->errCode!=SQLITE_OK );
62201 return pPager->errCode;
62217 rc = pPager->xGet(pPager, pgno, ppPage, flags);
62224 /* Normal, high-speed version of sqlite3PagerGet() */
62225 return pPager->xGet(pPager, pgno, ppPage, flags);
62230 ** Acquire a page if it is already in the in-memory cache. Do
62244 assert( pPager->pPCache!=0 );
62245 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
62246 assert( pPage==0 || pPager->hasHeldSharedLock );
62248 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
62258 ** The assert() at tag-20230419-2 proves that this constraint is always
62266 TESTONLY( Pager *pPager = pPg->pPager; )
62268 if( pPg->flags & PGHDR_MMAP ){
62269 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
62275 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
62283 assert( pPg->pgno==1 );
62284 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
62285 pPager = pPg->pPager;
62296 ** to the start of it. If there are active savepoints, open the sub-journal
62314 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
62316 assert( pPager->eState==PAGER_WRITER_LOCKED );
62318 assert( pPager->pInJournal==0 );
62320 /* If already in the error state, this function is a no-op. But on
62323 if( NEVER(pPager->errCode) ) return pPager->errCode;
62325 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62326 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
62327 if( pPager->pInJournal==0 ){
62332 if( !isOpen(pPager->jfd) ){
62333 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
62334 sqlite3MemJournalOpen(pPager->jfd);
62339 if( pPager->tempFile ){
62353 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
62357 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
62362 ** the sub-journal if necessary.
62366 pPager->nRec = 0;
62367 pPager->journalOff = 0;
62368 pPager->setSuper = 0;
62369 pPager->journalHdr = 0;
62375 sqlite3BitvecDestroy(pPager->pInJournal);
62376 pPager->pInJournal = 0;
62377 pPager->journalOff = 0;
62379 assert( pPager->eState==PAGER_WRITER_LOCKED );
62380 pPager->eState = PAGER_WRITER_CACHEMOD;
62387 ** Begin a write-transaction on the specified pager object. If a
62388 ** write-transaction has already been opened, this function is a no-op.
62395 ** If the subjInMemory argument is non-zero, then any sub-journal opened
62396 ** within this transaction will be opened as an in-memory file. This
62397 ** has no effect if the sub-journal is already opened (as it may be when
62399 ** sub-journal. If the subjInMemory argument is zero, then any required
62400 ** sub-journal is implemented in-memory if pPager is an in-memory database,
62406 if( pPager->errCode ) return pPager->errCode;
62407 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
62408 pPager->subjInMemory = (u8)subjInMemory;
62410 if( pPager->eState==PAGER_READER ){
62411 assert( pPager->pInJournal==0 );
62417 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
62422 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
62427 ** The busy-handler is not invoked if another connection already
62428 ** holds the write-lock. If possible, the upper layer will call it.
62430 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
62434 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
62449 ** transactions may copy data from the sub-journal into the database
62453 pPager->eState = PAGER_WRITER_LOCKED;
62454 pPager->dbHintSize = pPager->dbSize;
62455 pPager->dbFileSize = pPager->dbSize;
62456 pPager->dbOrigSize = pPager->dbSize;
62457 pPager->journalOff = 0;
62460 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
62461 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
62473 Pager *pPager = pPg->pPager;
62477 i64 iOff = pPager->journalOff;
62482 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
62484 assert( pPager->journalHdr<=pPager->journalOff );
62485 pData2 = pPg->pData;
62489 ** page in the block above, set the need-sync flag for the page.
62495 pPg->flags |= PGHDR_NEED_SYNC;
62497 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
62499 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
62501 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
62504 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
62505 pPager->journalOff, pPager->pageSize));
62508 PAGERID(pPager), pPg->pgno,
62509 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
62511 pPager->journalOff += 8 + pPager->pageSize;
62512 pPager->nRec++;
62513 assert( pPager->pInJournal!=0 );
62514 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
62517 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
62524 ** main journal or sub-journal as required. If the page is written into
62530 Pager *pPager = pPg->pPager;
62533 /* This routine is not called unless a write-transaction has already
62537 assert( pPager->eState==PAGER_WRITER_LOCKED
62538 || pPager->eState==PAGER_WRITER_CACHEMOD
62539 || pPager->eState==PAGER_WRITER_DBMOD
62542 assert( pPager->errCode==0 );
62543 assert( pPager->readOnly==0 );
62547 ** obtained the necessary locks to begin the write-transaction, but the
62555 if( pPager->eState==PAGER_WRITER_LOCKED ){
62559 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
62569 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
62570 if( pPager->pInJournal!=0
62571 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
62574 if( pPg->pgno<=pPager->dbOrigSize ){
62580 if( pPager->eState!=PAGER_WRITER_DBMOD ){
62581 pPg->flags |= PGHDR_NEED_SYNC;
62584 PAGERID(pPager), pPg->pgno,
62585 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
62589 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
62594 pPg->flags |= PGHDR_WRITEABLE;
62599 if( pPager->nSavepoint>0 ){
62604 if( pPager->dbSize<pPg->pgno ){
62605 pPager->dbSize = pPg->pgno;
62628 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
62629 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
62636 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
62637 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
62639 /* This trick assumes that both the page-size and sector-size are
62643 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
62645 nPageCount = pPager->dbSize;
62646 if( pPg->pgno>nPageCount ){
62647 nPage = (pPg->pgno - pg1)+1;
62648 }else if( (pg1+nPagePerSector-1)>nPageCount ){
62649 nPage = nPageCount+1-pg1;
62654 assert(pg1<=pPg->pgno);
62655 assert((pg1+nPage)>pPg->pgno);
62660 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
62665 if( pPage->flags&PGHDR_NEED_SYNC ){
62672 if( pPage->flags&PGHDR_NEED_SYNC ){
62690 pPage->flags |= PGHDR_NEED_SYNC;
62696 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
62697 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
62709 ** fit on a single disk sector. In this case all co-resident pages
62716 Pager *pPager = pPg->pPager;
62717 assert( (pPg->flags & PGHDR_MMAP)==0 );
62718 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62720 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
62721 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
62723 }else if( pPager->errCode ){
62724 return pPager->errCode;
62725 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
62726 assert( pPager->tempFile==0 );
62740 return pPg->flags & PGHDR_WRITEABLE;
62758 ** This optimization cannot be used with a temp-file, as the page may
62765 Pager *pPager = pPg->pPager;
62766 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
62767 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
62768 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
62769 pPg->flags |= PGHDR_DONT_WRITE;
62770 pPg->flags &= ~PGHDR_WRITEABLE;
62771 testcase( pPg->flags & PGHDR_NEED_SYNC );
62778 ** change-counter, stored as a 4-byte big-endian integer starting at
62782 ** But this only happens if the pPager->changeCountDone flag is false.
62792 ** The isDirectMode flag may only be non-zero if the library was compiled
62794 ** if isDirect is non-zero, then the database file is updated directly
62801 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62802 || pPager->eState==PAGER_WRITER_DBMOD
62807 ** atomic-write optimization is enabled in this build, then isDirect
62811 ** The idea is that if the atomic-write optimization is not
62824 if( !pPager->changeCountDone && pPager->dbSize>0 ){
62827 assert( !pPager->tempFile && isOpen(pPager->fd) );
62834 ** operating in direct-mode, make page 1 writable. When not in
62836 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
62849 assert( pPager->dbFileSize>0 );
62850 zBuf = pPgHdr->pData;
62852 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
62853 pPager->aStat[PAGER_STAT_WRITE]++;
62856 /* Update the pager's copy of the change-counter. Otherwise, the
62858 ** flushed (as the change-counter values will not match). */
62860 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
62861 pPager->changeCountDone = 1;
62864 pPager->changeCountDone = 1;
62875 ** Sync the database file to disk. This is a no-op for in-memory databases
62878 ** If successful, or if called on a pager for which it is a no-op, this
62884 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
62886 if( rc==SQLITE_OK && !pPager->noSync ){
62888 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
62894 ** This function may only be called while a write-transaction is active in
62895 ** rollback. If the connection is in WAL mode, this call is a no-op.
62905 int rc = pPager->errCode;
62908 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62909 || pPager->eState==PAGER_WRITER_DBMOD
62910 || pPager->eState==PAGER_WRITER_LOCKED
62922 ** of a super-journal file that should be written into the individual
62924 ** super-journal (a single database transaction).
62928 ** * The database file change-counter is updated,
62929 ** * the journal is synced (unless the atomic-write optimization is used),
62936 ** delete the super-journal file if specified).
62941 ** If the final parameter - noSync - is true, then the database file itself
62948 const char *zSuper, /* If not NULL, the super-journal name */
62953 assert( pPager->eState==PAGER_WRITER_LOCKED
62954 || pPager->eState==PAGER_WRITER_CACHEMOD
62955 || pPager->eState==PAGER_WRITER_DBMOD
62956 || pPager->eState==PAGER_ERROR
62961 if( NEVER(pPager->errCode) ) return pPager->errCode;
62967 pPager->zFilename, zSuper, pPager->dbSize));
62970 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
62972 assert( MEMDB==0 || pPager->tempFile );
62973 assert( isOpen(pPager->fd) || pPager->tempFile );
62975 /* If this is an in-memory db, or no pages have been written to, or this
62976 ** function has already been called, it is mostly a no-op. However, any
62978 sqlite3BackupRestart(pPager->pBackup);
62983 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62986 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
62989 pList->pDirty = 0;
62993 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
62997 sqlite3PcacheCleanAll(pPager->pPCache);
63000 /* The bBatch boolean is true if the batch-atomic-write commit method
63001 ** should be used. No rollback journal is created if batch-atomic-write
63005 sqlite3_file *fd = pPager->fd;
63008 && !pPager->noSync
63009 && sqlite3JournalIsInMemory(pPager->jfd);
63015 /* The following block updates the change-counter. Exactly how it
63016 ** does this depends on whether or not the atomic-update optimization
63020 ** * The file-system supports the atomic-write property for
63021 ** blocks of size page-size, and
63022 ** * This commit is not part of a multi-file transaction, and
63027 ** counter in 'indirect-mode'. If the optimization is compiled in but
63030 ** pager_incr_changecounter() to update the change-counter in indirect
63034 ** then call pager_incr_changecounter() to update the change-counter
63040 assert( isOpen(pPager->jfd)
63041 || pPager->journalMode==PAGER_JOURNALMODE_OFF
63042 || pPager->journalMode==PAGER_JOURNALMODE_WAL
63044 if( !zSuper && isOpen(pPager->jfd)
63045 && pPager->journalOff==jrnlBufferSize(pPager)
63046 && pPager->dbSize>=pPager->dbOrigSize
63047 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
63049 /* Update the db file change counter via the direct-write method. The
63050 ** following call will modify the in-memory representation of page 1
63052 ** directly to the database file. Because of the atomic-write
63053 ** property of the host file-system, this is safe.
63057 rc = sqlite3JournalCreate(pPager->jfd);
63066 rc = sqlite3JournalCreate(pPager->jfd);
63075 /* Write the super-journal name into the journal file. If a
63076 ** super-journal file name has already been written to the journal file,
63077 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
63083 ** If the atomic-update optimization is being used, this sync will not
63086 ** Because the change-counter page was just modified, unless the
63087 ** atomic-update optimization is used it is almost certain that the
63091 ** xSync() call will be changed to a no-op by the OS anyhow.
63096 pList = sqlite3PcacheDirtyList(pPager->pPCache);
63102 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
63103 char *pTmp = pPager->pTmpSpace;
63104 int szPage = (int)pPager->pageSize;
63106 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
63107 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
63118 rc = sqlite3JournalCreate(pPager->jfd);
63120 sqlite3OsClose(pPager->jfd);
63125 sqlite3OsClose(pPager->jfd);
63137 sqlite3PcacheCleanAll(pPager->pPCache);
63142 ** last page in the db image moved to the free-list. In this case the
63145 if( pPager->dbSize>pPager->dbFileSize ){
63146 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
63147 assert( pPager->eState==PAGER_WRITER_DBMOD );
63162 pPager->eState = PAGER_WRITER_FINISHED;
63171 ** synced to disk. The journal file still exists in the file-system
63173 ** be used as a hot-journal and the current transaction rolled back.
63177 ** for hot-journal rollback. Once this is done the transaction is
63189 if( NEVER(pPager->errCode) ) return pPager->errCode;
63190 pPager->iDataVersion++;
63192 assert( pPager->eState==PAGER_WRITER_LOCKED
63193 || pPager->eState==PAGER_WRITER_FINISHED
63194 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
63199 ** this transaction, the pager is running in exclusive-mode and is
63200 ** using persistent journals, then this function is a no-op.
63204 ** a hot-journal during hot-journal rollback, 0 changes will be made
63209 if( pPager->eState==PAGER_WRITER_LOCKED
63210 && pPager->exclusiveMode
63211 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
63213 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
63214 pPager->eState = PAGER_READER;
63219 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
63225 ** transaction are reverted and the current write-transaction is closed.
63235 ** in-memory cache pages to the state they were in when the transaction
63244 ** In WAL mode, all cache-entries containing data modified within the
63246 ** their pre-transaction state by re-reading data from the database or
63253 /* PagerRollback() is a no-op if called in READER or OPEN state. If
63258 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
63259 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
63263 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
63264 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
63266 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
63267 int eState = pPager->eState;
63274 pPager->errCode = SQLITE_ABORT;
63275 pPager->eState = PAGER_ERROR;
63283 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
63296 ** Return TRUE if the database file is opened read-only. Return FALSE
63300 return pPager->readOnly;
63308 return sqlite3PcacheRefCount(pPager->pPCache);
63317 int perPageSize = pPager->pageSize + pPager->nExtra
63319 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
63321 + pPager->pageSize;
63337 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
63338 a[1] = sqlite3PcachePagecount(pPager->pPCache);
63339 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
63340 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63341 a[4] = pPager->eState;
63342 a[5] = pPager->errCode;
63343 a[6] = pPager->aStat[PAGER_STAT_HIT];
63344 a[7] = pPager->aStat[PAGER_STAT_MISS];
63345 a[8] = 0; /* Used to be pPager->nOvfl */
63346 a[9] = pPager->nRead;
63347 a[10] = pPager->aStat[PAGER_STAT_WRITE];
63360 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
63376 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
63377 *pnVal += pPager->aStat[eStat];
63379 pPager->aStat[eStat] = 0;
63384 ** Return true if this is an in-memory or temp-file backed pager.
63387 return pPager->tempFile || pPager->memVfs;
63394 ** equal to nSavepoint, then this function is a no-op.
63397 ** occurs while opening the sub-journal file, then an IO error code is
63402 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
63406 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63408 assert( nSavepoint>nCurrent && pPager->useJournal );
63415 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
63420 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
63421 pPager->aSavepoint = aNew;
63425 aNew[ii].nOrig = pPager->dbSize;
63426 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
63427 aNew[ii].iOffset = pPager->journalOff;
63431 aNew[ii].iSubRec = pPager->nSubRec;
63432 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
63438 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
63440 pPager->nSavepoint = ii+1;
63442 assert( pPager->nSavepoint==nSavepoint );
63447 assert( pPager->eState>=PAGER_WRITER_LOCKED );
63450 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
63470 ** (the first created). A value of (Pager.nSavepoint-1) means operate
63472 ** (Pager.nSavepoint-1), then this function is a no-op.
63489 int rc = pPager->errCode;
63498 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
63507 for(ii=nNew; ii<pPager->nSavepoint; ii++){
63508 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
63510 pPager->nSavepoint = nNew;
63512 /* Truncate the sub-journal so that it only includes the parts
63515 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
63516 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
63517 /* Only truncate if it is an in-memory sub-journal. */
63518 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
63519 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
63520 rc = sqlite3OsTruncate(pPager->sjfd, sz);
63523 pPager->nSubRec = pRel->iSubRec;
63527 ** If this is a temp-file, it is possible that the journal file has
63531 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
63532 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
63543 pPager->journalMode==PAGER_JOURNALMODE_OFF
63544 && pPager->eState>=PAGER_WRITER_CACHEMOD
63546 pPager->errCode = SQLITE_ABORT;
63547 pPager->eState = PAGER_ERROR;
63559 ** Except, if the pager is in-memory only, then return an empty string if
63563 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
63564 ** participate in shared-cache.
63571 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
63574 return pPager->zFilename;
63582 return pPager->pVfs;
63591 return pPager->fd;
63600 return pPager->jfd;
63602 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
63610 return pPager->zJournal;
63623 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
63631 ** If the fourth argument, isCommit, is non-zero, then this page is being
63641 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
63645 assert( pPg->nRef>0 );
63646 assert( pPager->eState==PAGER_WRITER_CACHEMOD
63647 || pPager->eState==PAGER_WRITER_DBMOD
63651 /* In order to be able to rollback, an in-memory database must journal
63654 assert( pPager->tempFile || !MEMDB );
63655 if( pPager->tempFile ){
63662 ** sub-journal now. This is required to handle the following scenario:
63670 ** If page X were not written to the sub-journal here, it would not
63674 ** subjournalPage() may need to allocate space to store pPg->pgno into
63678 if( (pPg->flags & PGHDR_DIRTY)!=0
63685 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
63686 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
63688 /* If the journal needs to be sync()ed before page pPg->pgno can
63689 ** be written to, store pPg->pgno in local variable needSyncPgno.
63692 ** the journal needs to be sync()ed before database page pPg->pgno
63695 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
63696 needSyncPgno = pPg->pgno;
63697 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
63698 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
63699 assert( pPg->flags&PGHDR_DIRTY );
63702 /* If the cache contains a page with page-number pgno, remove it
63707 pPg->flags &= ~PGHDR_NEED_SYNC;
63709 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
63711 if( NEVER(pPgOld->nRef>1) ){
63715 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
63716 if( pPager->tempFile ){
63717 /* Do not discard pages from an in-memory database since we might
63719 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
63725 origPgno = pPg->pgno;
63729 /* For an in-memory database, make sure the original page continues
63733 if( pPager->tempFile && pPgOld ){
63739 /* If needSyncPgno is non-zero, then the journal file needs to be
63741 ** Currently, no such page exists in the page-cache and the
63743 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
63746 ** If the attempt to load the page into the page-cache fails, (due
63756 if( needSyncPgno<=pPager->dbOrigSize ){
63757 assert( pPager->pTmpSpace!=0 );
63758 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
63762 pPgHdr->flags |= PGHDR_NEED_SYNC;
63778 assert( pPg->pgno!=iNew );
63779 pPg->flags = flags;
63787 assert( pPg->nRef>0 || pPg->pPager->memDb );
63788 return pPg->pData;
63796 return pPg->pExtra;
63800 ** Get/set the locking-mode for this pager. Parameter eMode must be one
63803 ** the locking-mode is set to the value specified.
63807 ** locking-mode.
63815 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
63816 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
63817 pPager->exclusiveMode = (u8)eMode;
63819 return (int)pPager->exclusiveMode;
63823 ** Set the journal-mode for this pager. Parameter eMode must be one of:
63835 ** * An in-memory database can only have its journal_mode set to _OFF
63840 ** The returned indicate the current (possibly updated) journal-mode.
63843 u8 eOld = pPager->journalMode; /* Prior journalmode */
63857 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
63859 /* Do allow the journalmode of an in-memory database to be set to
63872 assert( pPager->eState!=PAGER_ERROR );
63873 pPager->journalMode = (u8)eMode;
63886 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
63887 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
63896 sqlite3OsClose(pPager->jfd);
63897 if( pPager->eLock>=RESERVED_LOCK ){
63898 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63901 int state = pPager->eState;
63906 if( pPager->eState==PAGER_READER ){
63911 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63918 assert( state==pPager->eState );
63921 sqlite3OsClose(pPager->jfd);
63926 return (int)pPager->journalMode;
63933 return (int)pPager->journalMode;
63943 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
63944 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
63949 ** Get/set the size-limit used for persistent journal files.
63951 ** Setting the size limit to -1 means no limit is enforced.
63952 ** An attempt to set a limit smaller than -1 is a no-op.
63955 if( iLimit>=-1 ){
63956 pPager->journalSizeLimit = iLimit;
63957 sqlite3WalLimit(pPager->pWal, iLimit);
63959 return pPager->journalSizeLimit;
63963 ** Return a pointer to the pPager->pBackup variable. The backup module
63969 return &pPager->pBackup;
63974 ** Unless this is an in-memory or temporary database, clear the pager cache.
63977 assert( MEMDB==0 || pPager->tempFile );
63978 if( pPager->tempFile==0 ) pager_reset(pPager);
63999 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
64011 if( pPager->pWal ){
64012 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
64013 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
64014 pPager->pBusyHandlerArg,
64015 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
64023 return sqlite3WalCallback(pPager->pWal);
64028 ** primitives necessary for write-ahead logging.
64031 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
64032 if( pPager->noLock ) return 0;
64033 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
64044 assert( pPager->eLock>=SHARED_LOCK );
64045 eOrigLock = pPager->eLock;
64058 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
64059 ** lock on the database file and use heap-memory to store the wal-index
64060 ** in. Otherwise, use the normal shared-memory.
64065 assert( pPager->pWal==0 && pPager->tempFile==0 );
64066 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
64068 /* If the pager is already in exclusive-mode, the WAL module will use
64069 ** heap-memory for the wal-index instead of the VFS shared-memory
64073 if( pPager->exclusiveMode ){
64081 rc = sqlite3WalOpen(pPager->pVfs,
64082 pPager->fd, pPager->zWal, pPager->exclusiveMode,
64083 pPager->journalSizeLimit, &pPager->pWal
64097 ** file (not a temp file or an in-memory database), and the WAL file
64103 ** If the pager is open on a temp-file (or in-memory database), or if
64109 int *pbOpen /* OUT: Set to true if call is a no-op */
64114 assert( pPager->eState==PAGER_OPEN || pbOpen );
64115 assert( pPager->eState==PAGER_READER || !pbOpen );
64117 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
64119 if( !pPager->tempFile && !pPager->pWal ){
64123 sqlite3OsClose(pPager->jfd);
64127 pPager->journalMode = PAGER_JOURNALMODE_WAL;
64128 pPager->eState = PAGER_OPEN;
64149 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
64151 /* If the log file is not already open, but does exist in the file-system,
64155 if( !pPager->pWal ){
64160 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
64169 ** the database file, the log and log-summary files will be deleted.
64171 if( rc==SQLITE_OK && pPager->pWal ){
64174 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
64175 pPager->pageSize, (u8*)pPager->pTmpSpace);
64176 pPager->pWal = 0;
64178 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
64186 ** If pager pPager is a wal-mode database not in exclusive locking mode,
64193 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
64194 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
64205 sqlite3WalDb(pPager->pWal, db);
64217 if( pPager->pWal ){
64218 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
64233 if( pPager->pWal ){
64234 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
64247 if( pPager->pWal ){
64248 rc = sqlite3WalSnapshotRecover(pPager->pWal);
64269 if( pPager->pWal ){
64270 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
64282 assert( pPager->pWal );
64283 sqlite3WalSnapshotUnlock(pPager->pWal);
64291 ** A read-lock must be held on the pager when this function is called. If
64298 assert( pPager->eState>=PAGER_READER );
64299 return sqlite3WalFramesize(pPager->pWal);
64305 return sqlite3WalSystemErrno(pPager->pWal);
64325 ** This file contains the implementation of a write-ahead log (WAL) used in
64328 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
64347 ** big-endian 32-bit unsigned integer values:
64353 ** 16: Salt-1, random integer incremented with each checkpoint
64354 ** 20: Salt-2, a different random integer changing with each ckpt
64355 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
64356 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
64358 ** Immediately following the wal-header are zero or more frames. Each
64359 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
64360 ** of page data. The frame-header is six big-endian 32-bit unsigned
64366 ** 8: Salt-1 (copied from the header)
64367 ** 12: Salt-2 (copied from the header)
64368 ** 16: Checksum-1.
64369 ** 20: Checksum-2.
64374 ** (1) The salt-1 and salt-2 values in the frame-header match
64375 ** salt values in the wal-header
64377 ** (2) The checksum values in the final 8 bytes of the frame-header
64382 ** The checksum is computed using 32-bit big-endian integers if the
64384 ** is computed using little-endian if the magic number is 0x377f0682.
64386 ** big-endian format regardless of which byte order is used to compute
64388 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
64391 ** for i from 0 to n-1 step 2:
64398 ** of the sequence being summed.) The s1 value spans all 32-bit
64401 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
64402 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
64403 ** The VFS.xSync operations serve as write barriers - all writes launched
64407 ** After each checkpoint, the salt-1 value is incremented and the salt-2
64436 ** data structure called the wal-index is maintained to expedite the
64439 ** WAL-INDEX FORMAT
64441 ** Conceptually, the wal-index is shared memory, though VFS implementations
64442 ** might choose to implement the wal-index using a mmapped file. Because
64443 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
64447 ** In the default unix and windows implementation, the wal-index is a mmapped
64448 ** file whose name is the database name with a "-shm" suffix added. For that
64449 ** reason, the wal-index is sometimes called the "shm" file.
64451 ** The wal-index is transient. After a crash, the wal-index can (and should
64453 ** to either truncate or zero the header of the wal-index when the last
64454 ** connection to it closes. Because the wal-index is transient, it can
64455 ** use an architecture-specific format; it does not have to be cross-platform.
64457 ** as big endian, the wal-index can store multi-byte values in the native
64460 ** The purpose of the wal-index is to answer this question quickly: Given
64465 ** The wal-index consists of a header region, followed by an one or
64468 ** The wal-index header contains the total number of frames within the WAL
64474 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
64476 ** wal-index. The values are:
64481 ** Each index block contains two sections, a page-mapping that contains the
64482 ** database page number associated with each wal frame, and a hash-table
64484 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
64485 ** for the first index block) 32-bit page numbers. The first entry in the
64486 ** first index-block contains the database page number corresponding to the
64491 ** The last index block in a wal-index usually contains less than the full
64492 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
64494 ** allocated size of the page-mapping array - the page-mapping array merely
64498 ** can be found by scanning the page-mapping sections of each index block
64504 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
64509 ** 1-based index of an entry in the mapping section of the same
64510 ** index block. Let K be the 1-based index of the largest entry in
64525 ** wrap-around.) Because the hash table is never more than half full,
64530 ** current index block. Otherwise the iMax-th mapping entry of the
64548 ** and to the wal-index) might be using a different value K1, where K1>K0.
64554 ** in the first place - which is what reader one wants. Meanwhile, the
64577 ** The maximum (and only) versions of the wal and wal-index formats
64581 ** values in the wal-header are correct and (b) the version field is not
64584 ** Similarly, if a client successfully reads a wal-index header (i.e. the
64586 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
64601 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
64611 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
64621 ** The following object holds a copy of the wal-index header content.
64623 ** The actual header in the wal-index consists of two copies of this
64630 ** Or it can be 1 to represent a 65536-byte page. The latter case was
64634 u32 iVersion; /* Wal-index version */
64638 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
64648 ** A copy of the following object occurs in the wal-index immediately
64671 ** holds read-lock K, then the value in aReadMark[K] is no greater than
64674 ** a special case; its value is never used and it exists as a place-holder
64703 ** We assume that 32-bit loads are atomic and so no locks are needed in
64716 ** This is a schematic view of the complete 136-byte header of the
64717 ** wal-index file (also known as the -shm file):
64719 ** +-----------------------------+
64721 ** +-----------------------------+ |
64723 ** +-----------------------------+ |
64725 ** +-------+-------+-------------+ |
64727 ** +-------+-------+-------------+ |
64729 ** +-----------------------------+ | WalIndexHdr object
64731 ** +-----------------------------+ |
64734 ** +-----------------------------+ |
64737 ** +-----------------------------+ |
64740 ** +-----------------------------+
64742 ** +-----------------------------+ |
64744 ** +-----------------------------+ |
64746 ** +-------+-------+-------------+ |
64748 ** +-------+-------+-------------+ | Second copy of the
64750 ** +-----------------------------+ |
64752 ** +-----------------------------+ |
64755 ** +-----------------------------+ |
64758 ** +-----------------------------+ |
64761 ** +-----------------------------+
64763 ** +-----------------------------+
64769 ** +-------+-------+------+------+
64771 ** +-------+-------+------+------+ ) 8 lock bytes
64773 ** +-------+-------+------+------+
64775 ** +-----------------------------+
64777 ** +-----------------------------+
64782 ** only support mandatory file-locks, we do not read or write data
64795 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
64796 ** big-endian format in the first 4 bytes of a WAL file.
64799 ** file are calculated by treating all data as an array of 32-bit
64800 ** big-endian words. Otherwise, they are calculated by interpreting
64801 ** all data as 32-bit little-endian words.
64806 ** Return the offset of frame iFrame in the write-ahead log file,
64808 ** is to the start of the write-ahead log frame-header.
64811 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
64815 ** An open write-ahead log file is represented by an instance of the
64826 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
64828 i16 readLock; /* Which read lock is being held. -1 for none */
64830 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
64837 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
64838 WalIndexHdr hdr; /* Wal-index header for current transaction */
64842 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
64877 ** Each page of the wal-index mapping contains a hash-table made up of
64891 ** walIteratorInit() - Create a new iterator,
64892 ** walIteratorNext() - Step an iterator,
64893 ** walIteratorFree() - Free an iterator.
64906 } aSegment[1]; /* One for every 32KB page in the wal-index */
64910 ** Define the parameters of the hash tables in the wal-index file. There
64911 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
64912 ** wal-index.
64914 ** Changing any of these constants will alter the wal-index format and
64922 ** The block of page numbers associated with the first hash-table in a
64923 ** wal-index is smaller than usual. This is so that there is a complete
64924 ** hash-table on each aligned 32KB page of the wal-index.
64926 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
64928 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
64934 ** Structured Exception Handling (SEH) is a Windows-specific technique
64935 ** for catching exceptions raised while accessing memory-mapped files.
64937 ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
64938 ** deal with system-level errors that arise during WAL -shm file processing.
64939 ** Without this compile-time option, any system-level faults that appear
64940 ** while accessing the memory-mapped -shm file will cause a process-wide
64949 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
64950 VVA_ONLY(pWal->nSehTry++);
64954 VVA_ONLY(pWal->nSehTry--); \
64955 assert( pWal->nSehTry==0 ); \
64958 /* Simulate a memory-mapping fault in the -shm file for testing purposes */
64964 ** indicates that the exception may have been caused by accessing the *-shm
64968 VVA_ONLY(pWal->nSehTry--);
64970 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
64972 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
64976 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
64986 ** thrown by the system if the *-shm file mapping is accessed after it
64991 assert( pWal->nSehTry>0 );
65017 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
65020 ** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
65029 #define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
65032 # define SEH_TRY VVA_ONLY(pWal->nSehTry++);
65033 # define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
65034 # define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
65041 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
65042 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
65045 ** If the wal-index is currently smaller the iPage pages then the size
65046 ** of the wal-index might be increased, but only if it is safe to do
65047 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
65048 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
65052 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
65056 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
65065 /* Enlarge the pWal->apWiData[] array if required */
65066 if( pWal->nWiData<=iPage ){
65069 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
65074 memset((void*)&apNew[pWal->nWiData], 0,
65075 sizeof(u32*)*(iPage+1-pWal->nWiData));
65076 pWal->apWiData = apNew;
65077 pWal->nWiData = iPage+1;
65081 assert( pWal->apWiData[iPage]==0 );
65082 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
65083 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
65084 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
65086 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
65087 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
65089 assert( pWal->apWiData[iPage]!=0
65091 || (pWal->writeLock==0 && iPage==0) );
65092 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
65096 pWal->readOnly |= WAL_SHM_RDONLY;
65103 *ppPage = pWal->apWiData[iPage];
65113 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
65120 ** Return a pointer to the WalCkptInfo structure in the wal-index.
65123 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65125 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
65129 ** Return a pointer to the WalIndexHdr structure in the wal-index.
65132 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65134 return (volatile WalIndexHdr*)pWal->apWiData[0];
65138 ** The argument to this macro must be of type u32. On a little-endian
65140 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
65142 ** of the input value as a little-endian integer.
65159 int nativeCksum, /* True for native byte-order, false for non-native */
65223 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65224 sqlite3OsShmBarrier(pWal->pDbFd);
65229 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
65231 ** might give false-positive TSAN warnings.
65233 ** See tag-20200519-1.
65242 ** Write the header information in pWal->hdr into the wal-index.
65244 ** The checksum on pWal->hdr is updated before it is written.
65250 assert( pWal->writeLock );
65251 pWal->hdr.isInit = 1;
65252 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
65253 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
65254 /* Possible TSAN false-positive. See tag-20200519-1 */
65255 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65257 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
65262 ** supplied by the caller. A frame-header is made up of a series of
65263 ** 4-byte big-endian integers, as follows:
65268 ** 8: Salt-1 (copied from the wal-header)
65269 ** 12: Salt-2 (copied from the wal-header)
65270 ** 16: Checksum-1.
65271 ** 20: Checksum-2.
65274 Wal *pWal, /* The write-ahead log */
65276 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
65280 int nativeCksum; /* True for native byte-order checksums */
65281 u32 *aCksum = pWal->hdr.aFrameCksum;
65285 if( pWal->iReCksum==0 ){
65286 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
65288 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65290 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65305 Wal *pWal, /* The write-ahead log */
65311 int nativeCksum; /* True for native byte-order checksums */
65312 u32 *aCksum = pWal->hdr.aFrameCksum;
65316 /* A frame is only valid if the salt values in the frame-header
65317 ** match the salt values in the wal-header.
65319 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
65331 ** all prior frames, the first 16 bytes of this frame-header,
65332 ** and the frame-data matches the checksum in the last 8
65333 ** bytes of this frame-header.
65335 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
65337 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
65361 return "WRITE-LOCK";
65363 return "CKPT-LOCK";
65365 return "RECOVER-LOCK";
65368 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
65369 lockIdx-WAL_READ_LOCK(0));
65378 ** A lock cannot be moved directly between shared and exclusive - it must go
65381 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
65385 if( pWal->exclusiveMode ) return SQLITE_OK;
65386 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
65388 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
65390 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65392 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
65397 if( pWal->exclusiveMode ) return;
65398 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
65401 pWal->lockMask &= ~(1 << lockIdx);
65403 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
65407 if( pWal->exclusiveMode ) return SQLITE_OK;
65408 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
65410 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
65412 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
65415 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
65421 if( pWal->exclusiveMode ) return;
65422 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
65425 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
65427 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
65433 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
65438 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
65439 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
65442 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
65447 ** of a page hash table in the wal-index. This becomes the return value
65452 volatile ht_slot *aHash; /* Start of the wal-index hash table */
65459 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
65462 ** Set output variable pLoc->aHash to point to the start of the hash table
65463 ** in the wal-index file. Set pLoc->iZero to one less than the frame
65466 ** (pLoc->iZero+N) in the log.
65468 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
65469 ** first frame indexed by the hash table, frame (pLoc->iZero).
65478 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
65481 if( pLoc->aPgno ){
65482 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
65484 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
65485 pLoc->iZero = 0;
65487 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
65496 ** Return the number of the wal-index page that contains the hash-table
65497 ** and page-number array that contain entries corresponding to WAL frame
65498 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
65502 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
65520 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
65522 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
65527 ** than pWal->hdr.mxFrame.
65529 ** This function is called whenever pWal->hdr.mxFrame is decreased due
65532 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
65534 ** pWal->hdr.mxFrame advances to the point where those hash tables are
65543 assert( pWal->writeLock );
65544 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
65545 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
65546 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
65548 if( pWal->hdr.mxFrame==0 ) return;
65550 /* Obtain pointers to the hash-table and page-number array containing
65551 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
65552 ** that the page said hash-table and array reside on is already mapped.(1)
65554 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
65555 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
65556 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
65557 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
65559 /* Zero all hash-table entries that correspond to frame numbers greater
65560 ** than pWal->hdr.mxFrame.
65562 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
65571 ** frame numbers greater than pWal->hdr.mxFrame.
65573 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
65596 ** Set an entry in the wal-index that will map database page number
65601 WalHashLoc sLoc; /* Wal-index hash table location */
65605 /* Assuming the wal-index file was successfully mapped, populate the
65610 int idx; /* Value to write to hash-table slot */
65613 idx = iFrame - sLoc.iZero;
65616 /* If this is the first entry to be added to this hash-table, zero the
65620 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
65629 ** the hash-table before writing any new entries.
65631 if( sLoc.aPgno[idx-1] ){
65633 assert( !sLoc.aPgno[idx-1] );
65636 /* Write the aPgno[] array entry and the hash-table slot. */
65639 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
65641 sLoc.aPgno[idx-1] = iPage;
65657 ** thing to check, so only do this occasionally - not on every
65679 ** Recover the wal-index by reading the write-ahead log file.
65682 ** wal-index to prevent other threads/processes from doing anything
65683 ** with the WAL or wal-index while recovery is running. The
65700 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
65703 assert( pWal->writeLock );
65704 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
65705 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
65712 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
65714 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
65721 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
65729 u32 iPg; /* Current 32KB wal-index page */
65733 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
65746 || szPage&(szPage-1)
65752 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
65753 pWal->szPage = szPage;
65754 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
65755 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
65758 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
65759 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
65761 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
65762 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
65787 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
65792 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
65798 pWal->apWiData[iPg] = aPrivate;
65806 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
65813 /* If nTruncate is non-zero, this is a commit record. */
65815 pWal->hdr.mxFrame = iFrame;
65816 pWal->hdr.nPage = nTruncate;
65817 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
65820 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
65821 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
65824 pWal->apWiData[iPg] = aShare;
65837 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
65868 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
65869 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
65872 /* Reset the checkpoint-header. This is safe because this thread is
65874 ** checkpointers. Then set the values of read-mark slots 1 through N.
65877 pInfo->nBackfill = 0;
65878 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
65879 pInfo->aReadMark[0] = 0;
65883 if( i==1 && pWal->hdr.mxFrame ){
65884 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
65886 pInfo->aReadMark[i] = READMARK_NOT_USED;
65900 if( pWal->hdr.nPage ){
65903 pWal->hdr.mxFrame, pWal->zWalName
65910 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
65915 ** Close an open wal-index.
65918 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
65920 for(i=0; i<pWal->nWiData; i++){
65921 sqlite3_free((void *)pWal->apWiData[i]);
65922 pWal->apWiData[i] = 0;
65925 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65926 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
65937 ** client from unlinking the WAL or wal-index file. If another process
65946 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
65949 int bNoShm, /* True to run in heap-memory mode */
65961 ** of these constants would result in an incompatible on-disk format
65962 ** for the -shm file. Any change that causes one of these asserts to
65966 ** This table also serves as a helpful cross-reference when trying to
65967 ** interpret hex dumps of the -shm file.
66007 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
66012 pRet->pVfs = pVfs;
66013 pRet->pWalFd = (sqlite3_file *)&pRet[1];
66014 pRet->pDbFd = pDbFd;
66015 pRet->readLock = -1;
66016 pRet->mxWalSize = mxWalSize;
66017 pRet->zWalName = zWalName;
66018 pRet->syncHeader = 1;
66019 pRet->padToSectorBoundary = 1;
66020 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
66022 /* Open file handle on the write-ahead log file. */
66024 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
66026 pRet->readOnly = WAL_RDONLY;
66031 sqlite3OsClose(pRet->pWalFd);
66035 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
66037 pRet->padToSectorBoundary = 0;
66049 if( pWal ) pWal->mxWalSize = iLimit;
66071 iMin = p->iPrior;
66073 for(i=p->nSegment-1; i>=0; i--){
66074 struct WalSegment *pSegment = &p->aSegment[i];
66075 while( pSegment->iNext<pSegment->nEntry ){
66076 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
66080 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
66084 pSegment->iNext++;
66088 *piPage = p->iPrior = iRet;
66116 const u32 *aContent, /* Pages in wal - keys for the sort */
66180 ht_slot *aList; /* Pointer to sub-list content */
66188 struct Sublist aSub[13]; /* Array of sub-lists */
66192 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
66201 assert( p->aList && p->nList<=(1<<iSub) );
66202 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
66203 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66214 assert( p->nList<=(1<<iSub) );
66215 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
66216 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
66226 assert( aContent[aList[i]] > aContent[aList[i-1]] );
66242 ** nBackfill or earlier may be included - excluding them is an optimization
66258 ht_slot *aTmp; /* Temp space used by merge-sort */
66264 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
66265 iLast = pWal->hdr.mxFrame;
66270 + (nSegment-1)*sizeof(struct WalSegment)
66279 p->nSegment = nSegment;
66292 nEntry = (int)(iLast - sLoc.iZero);
66294 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
66296 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
66303 p->aSegment[i].iZero = sLoc.iZero;
66304 p->aSegment[i].nEntry = nEntry;
66305 p->aSegment[i].aIndex = aIndex;
66306 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
66322 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
66327 if( pWal->db ){
66328 int tmout = pWal->db->busyTimeout;
66332 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
66345 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
66359 assert( pWal->readLock<0 || bLock==0 );
66361 assert( pWal->db );
66365 pWal->writeLock = 1;
66369 }else if( pWal->writeLock ){
66371 pWal->writeLock = 0;
66380 pWal->db = db;
66404 ** busy-handler function. Invoke it and retry the lock until either the
66405 ** lock is successfully obtained or the busy-handler returns 0.
66428 ** The cache of the wal-index header must be valid to call this function.
66429 ** Return the page-size in bytes used by the database.
66432 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
66441 ** file - there are no readers that may attempt to read a frame from
66444 ** This function updates the shared-memory structures so that the next
66449 ** new wal-index header. It should be passed a pseudo-random value (i.e.
66455 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
66456 pWal->nCkpt++;
66457 pWal->hdr.mxFrame = 0;
66459 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
66461 AtomicStore(&pInfo->nBackfill, 0);
66462 pInfo->nBackfillAttempted = 0;
66463 pInfo->aReadMark[1] = 0;
66464 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
66465 assert( pInfo->aReadMark[0]==0 );
66477 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
66483 ** in the WAL and can be recovered following a power-loss or hard reset.
66490 ** This routine uses and updates the nBackfill field of the wal-index header.
66509 int szPage; /* Database page-size */
66522 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
66524 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
66533 mxSafeFrame = pWal->hdr.mxFrame;
66534 mxPage = pWal->hdr.nPage;
66536 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
66538 assert( y<=pWal->hdr.mxFrame );
66542 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
66554 if( pInfo->nBackfill<mxSafeFrame ){
66555 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
66562 u32 nBackfill = pInfo->nBackfill;
66563 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
66566 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
66574 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
66575 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
66577 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
66580 ** maximum size of the pending-byte page (65536 bytes), then
66584 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
66595 if( AtomicLoad(&db->u1.isInterrupted) ){
66596 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
66604 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
66606 iOffset = (iDbpage-1)*(i64)szPage;
66608 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
66611 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
66615 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
66616 i64 szDb = pWal->hdr.nPage*(i64)szPage;
66618 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
66620 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
66624 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
66645 assert( pWal->writeLock );
66647 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
66652 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
66653 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
66656 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
66662 ** wal-index header in shared memory, as all subsequent reader or
66666 ** the wal-index header do not match the contents of the
66667 ** file-system. To avoid this, update the wal-index header to
66670 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
66672 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66691 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
66693 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
66697 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
66704 ** an exception thrown by reading from the *-shm mapping after it has become
66719 ** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
66724 if( pWal->exclusiveMode==0 ){
66728 u32 mUnlock = pWal->lockMask & ~(
66729 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
66730 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
66731 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
66738 sqlite3_free(pWal->pFree);
66739 pWal->pFree = 0;
66740 if( pWal->pWiValue ){
66741 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
66742 pWal->pWiValue = 0;
66755 if( pWal->exclusiveMode==0 ){
66759 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
66760 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
66761 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
66763 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
66766 assert( mExpect==pWal->lockMask );
66778 iRet = pWal->iSysErrno;
66779 pWal->iSysErrno = 0;
66800 int isDelete = 0; /* True to unlink wal and wal-index files */
66805 ** ordinary, rollback-mode locking methods, this guarantees that the
66808 ** the wal and wal-index files.
66813 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
66815 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
66816 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
66822 int bPersist = -1;
66824 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
66828 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
66831 }else if( pWal->mxWalSize>=0 ){
66835 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
66844 sqlite3OsClose(pWal->pWalFd);
66847 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
66851 sqlite3_free((void *)pWal->apWiData);
66858 ** Try to read the wal-index header. Return 0 on success and 1 if
66861 ** The wal-index is in shared memory. Another thread or process might
66868 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
66871 ** If the checksum cannot be verified return non-zero. If the header
66879 /* The first page of the wal-index must be mapped at this point. */
66880 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66885 ** from the file. If this happens, return non-zero.
66887 ** tag-20200519-1:
66888 ** There are two copies of the header at the beginning of the wal-index.
66892 ** give false-positive warnings about these accesses because the tools do not
66893 ** account for the double-read and the memory barrier. The use of mutexes
66899 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
66907 return 1; /* Malformed header - probably all zeros */
66909 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
66914 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
66916 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
66917 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
66918 testcase( pWal->szPage<=32768 );
66919 testcase( pWal->szPage>=65536 );
66930 #define WAL_RETRY (-1)
66933 ** Read the wal-index header from the wal-index and into pWal->hdr.
66934 ** If the wal-header appears to be corrupt, try to reconstruct the
66935 ** wal-index from the WAL before returning.
66937 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
66938 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
66941 ** If the wal-index header is successfully read, return SQLITE_OK.
66947 volatile u32 *page0; /* Chunk of wal-index containing header */
66949 /* Ensure that page 0 of the wal-index (the page that contains the
66950 ** wal-index header) is mapped. Return early if an error occurs here.
66957 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
66959 ** confirm that another write-capable connection has the shared-memory
66960 ** open, and hence the content of the shared-memory is unreliable,
66961 ** since the shared-memory might be inconsistent with the WAL file
66964 assert( pWal->writeLock==0 );
66965 assert( pWal->readOnly & WAL_SHM_RDONLY );
66966 pWal->bShmUnreliable = 1;
66967 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
66970 return rc; /* Any other non-OK return is just an error */
66973 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
66977 assert( page0!=0 || pWal->writeLock==0 );
66979 /* If the first page of the wal-index has been mapped, try to read the
66980 ** wal-index header immediately, without holding any lock. This usually
66981 ** works, but may fail if the wal-index header is corrupt or currently
66990 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
66996 int bWriteLock = pWal->writeLock;
66998 pWal->writeLock = 1;
67002 /* If the wal-index header is still malformed even while holding
67011 pWal->writeLock = 0;
67019 ** sure the wal-index was not constructed with some future format that
67022 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
67025 if( pWal->bShmUnreliable ){
67028 pWal->bShmUnreliable = 0;
67029 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
67035 pWal->exclusiveMode = WAL_NORMAL_MODE;
67042 ** Open a transaction in a connection where the shared-memory is read-only
67043 ** and where we cannot verify that there is a separate write-capable connection
67044 ** on hand to keep the shared-memory up-to-date with the WAL file.
67046 ** This can happen, for example, when the shared-memory is implemented by
67047 ** memory-mapping a *-shm file, where a prior writer has shut down and
67048 ** left the *-shm file on disk, and now the present connection is trying
67049 ** to use that database but lacks write permission on the *-shm file.
67054 ** The *-wal file has been read and an appropriate wal-index has been
67055 ** constructed in pWal->apWiData[] using heap memory instead of shared
67062 ** WAL_RETRY, then the heap memory wal-index has been discarded and
67064 ** beginning (including attempting to map the *-shm file).
67077 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
67079 assert( pWal->bShmUnreliable );
67080 assert( pWal->readOnly & WAL_SHM_RDONLY );
67081 assert( pWal->nWiData>0 && pWal->apWiData[0] );
67091 pWal->readLock = 0;
67093 /* Check to see if a separate writer has attached to the shared-memory area,
67094 ** thus making the shared-memory "reliable" again. Do this by invoking
67098 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
67099 ** cause the heap-memory WAL-index to be discarded and the actual
67111 ** even if some external agent does a "chmod" to make the shared-memory
67115 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
67116 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
67122 /* We reach this point only if the real shared-memory is still unreliable.
67123 ** Assume the in-memory WAL-index substitute is correct and load it
67124 ** into pWal->hdr.
67126 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
67131 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
67136 /* If the wal file is too small to contain a wal-header and the
67137 ** wal-index header has mxFrame==0, then it must be safe to proceed
67143 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
67148 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
67152 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
67154 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
67161 assert( (pWal->szPage & (pWal->szPage-1))==0 );
67162 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
67163 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
67172 ** wal file since the heap-memory wal-index was created. If so, the
67173 ** heap-memory wal-index is discarded and WAL_RETRY returned to
67175 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
67176 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
67177 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
67185 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67189 /* If nTruncate is non-zero, then a complete transaction has been
67197 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
67198 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
67204 for(i=0; i<pWal->nWiData; i++){
67205 sqlite3_free((void*)pWal->apWiData[i]);
67206 pWal->apWiData[i] = 0;
67208 pWal->bShmUnreliable = 0;
67227 ** to make a copy of the wal-index header into pWal->hdr. If the
67228 ** wal-index header has changed, *pChanged is set to 1 (as an indication
67230 ** flushed.) When useWal==1, the wal-index header is assumed to already
67241 ** bad luck when there is lots of contention for the wal-index, but that
67245 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
67246 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
67249 ** including frame number aReadMark[pWal->readLock]. The reader will
67250 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
67251 ** Or if pWal->readLock==0, then the reader will ignore the WAL
67254 ** this routine will always set pWal->readLock>0 on success.
67256 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
67266 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
67273 assert( pWal->readLock<0 ); /* Not currently locked */
67276 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
67284 ** paged out or take a page-fault that is time-consuming to resolve,
67298 VVA_ONLY( pWal->lockError = 1; )
67301 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
67302 sqlite3OsSleep(pWal->pVfs, nDelay);
67307 if( pWal->bShmUnreliable==0 ){
67319 if( pWal->apWiData[0]==0 ){
67324 ** code that determines whether or not the shared-memory region
67338 else if( pWal->bShmUnreliable ){
67343 assert( pWal->nWiData>0 );
67344 assert( pWal->apWiData[0]!=0 );
67347 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
67349 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
67358 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
67375 pWal->readLock = 0;
67385 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
67389 mxFrame = pWal->hdr.mxFrame;
67391 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
67392 mxFrame = pWal->pSnapshot->mxFrame;
67396 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
67403 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
67409 AtomicStore(pInfo->aReadMark+i,mxFrame);
67420 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
67428 /* Now that the read-lock has been obtained, check that neither the
67429 ** value in the aReadMark[] array or the contents of the wal-index
67432 ** It is necessary to check that the wal-index header did not change
67433 ** between the time it was read and when the shared-lock was obtained
67436 ** that occur later in the log than pWal->hdr.mxFrame may have been
67439 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
67442 ** Before checking that the live wal-index header has not changed
67445 ** to read any frames earlier than minFrame from the wal file - they
67449 ** nBackfill and checking that the wal-header in shared-memory still
67450 ** matches the one cached in pWal->hdr, it is guaranteed that the
67451 ** checkpointer that set nBackfill was not working with a wal-index
67452 ** header newer than that cached in pWal->hdr. If it were, that could
67454 ** a version of page X that lies before pWal->minFrame (call that version
67457 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
67460 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
67462 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
67464 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
67465 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
67470 assert( mxReadMark<=pWal->hdr.mxFrame );
67471 pWal->readLock = (i16)mxI;
67482 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
67483 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
67485 int szPage = (int)pWal->szPage;
67489 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
67492 u32 i = pInfo->nBackfillAttempted;
67493 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
67501 assert( i - sLoc.iZero - 1 >=0 );
67502 pgno = sLoc.aPgno[i-sLoc.iZero-1];
67503 iDbOff = (i64)(pgno-1) * szPage;
67507 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
67510 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
67518 pInfo->nBackfillAttempted = i-1;
67533 ** This is only really safe if the file-system is such that any page
67547 assert( pWal->readLock>=0 );
67550 void *pBuf1 = sqlite3_malloc(pWal->szPage);
67551 void *pBuf2 = sqlite3_malloc(pWal->szPage);
67555 pWal->ckptLock = 1;
67560 pWal->ckptLock = 0;
67582 WalIndexHdr *pSnapshot = pWal->pSnapshot;
67585 assert( pWal->ckptLock==0 );
67586 assert( pWal->nSehTry>0 );
67590 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
67598 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
67601 ** before checking pInfo->nBackfillAttempted. */
67623 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
67625 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
67626 ** is populated with the wal-index header corresponding to the head
67634 ** pSnapshot->mxFrame into the database file. Note that the
67639 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
67640 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
67644 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
67645 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
67648 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
67649 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
67651 assert( pWal->readLock>0 );
67652 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
67658 /* A client using a non-current snapshot may not ignore any frames
67664 pWal->minFrame = 1;
67685 ** it takes a snapshot of the state of the WAL and wal-index for the current
67687 ** Other threads might append new content to the WAL and wal-index but
67706 ** read-lock.
67710 if( pWal->readLock>=0 ){
67711 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
67712 pWal->readLock = -1;
67730 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
67735 assert( pWal->readLock>=0 || pWal->lockError );
67737 /* If the "last page" field of the wal-index header snapshot is 0, then
67739 ** in this case as an optimization. Likewise, if pWal->readLock==0,
67743 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
67753 ** that adds entries to the wal-index (and possibly to this hash
67757 ** read transaction was opened may have been written incorrectly -
67764 ** if we had exclusive access to the hash-table:
67767 ** This condition filters out normal hash-table collisions.
67771 ** table after the current read-transaction had started.
67773 iMinHash = walFramePage(pWal->minFrame);
67774 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
67790 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
67794 if( (nCollide--)==0 ){
67804 ** of the wal-index file content. Make sure the results agree with the
67809 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
67810 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
67861 sz = pWal->hdr.szPage;
67867 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
67874 if( pWal && ALWAYS(pWal->readLock>=0) ){
67875 return pWal->hdr.nPage;
67898 /* If the write-lock is already held, then it was obtained before the
67899 ** read-transaction was even opened, making this call a no-op.
67901 if( pWal->writeLock ){
67902 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
67909 assert( pWal->readLock>=0 );
67910 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
67912 if( pWal->readOnly ){
67923 pWal->writeLock = 1;
67930 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
67938 pWal->writeLock = 0;
67948 if( pWal->writeLock ){
67950 pWal->writeLock = 0;
67951 pWal->iReCksum = 0;
67952 pWal->truncateOnCommit = 0;
67959 ** function moves the write-pointer back to the start of the transaction.
67971 if( ALWAYS(pWal->writeLock) ){
67972 Pgno iMax = pWal->hdr.mxFrame;
67976 /* Restore the clients cache of the wal-index header to the state it
67979 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
67981 for(iFrame=pWal->hdr.mxFrame+1;
67987 ** (b) has an outstanding reference, then xUndo is either a no-op
67999 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
68013 assert( pWal->writeLock );
68014 aWalData[0] = pWal->hdr.mxFrame;
68015 aWalData[1] = pWal->hdr.aFrameCksum[0];
68016 aWalData[2] = pWal->hdr.aFrameCksum[1];
68017 aWalData[3] = pWal->nCkpt;
68029 assert( pWal->writeLock );
68030 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
68032 if( aWalData[3]!=pWal->nCkpt ){
68033 /* This savepoint was opened immediately after the write-transaction
68038 aWalData[3] = pWal->nCkpt;
68041 if( aWalData[0]<pWal->hdr.mxFrame ){
68042 pWal->hdr.mxFrame = aWalData[0];
68043 pWal->hdr.aFrameCksum[0] = aWalData[1];
68044 pWal->hdr.aFrameCksum[1] = aWalData[2];
68059 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
68063 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
68070 if( pWal->readLock==0 ){
68072 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
68073 if( pInfo->nBackfill>0 ){
68076 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68081 ** wal-index header to reflect this.
68084 ** at this point. But updating the actual wal-index header is also
68088 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68094 pWal->readLock = -1;
68110 ** the next fsync should occur - passed from sqlite3WalFrames() into
68123 ** Do a sync when crossing the p->iSyncPoint boundary.
68136 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
68137 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
68138 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
68141 iAmt -= iFirstAmt;
68143 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
68144 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
68147 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
68162 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
68163 pData = pPage->pData;
68164 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
68168 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
68181 const int szPage = pWal->szPage;/* Database page size */
68184 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
68194 ** wal-file header. Otherwise, read them from the frame header of the
68196 assert( pWal->iReCksum>0 );
68197 if( pWal->iReCksum==1 ){
68200 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
68202 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
68203 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
68204 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
68206 iRead = pWal->iReCksum;
68207 pWal->iReCksum = 0;
68210 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
68217 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
68226 ** Write a set of frames to the log. The caller must hold the write-lock
68231 int szPage, /* Database page-size in bytes */
68249 assert( pWal->writeLock );
68256 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
68258 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
68263 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
68264 iFirst = pLive->mxFrame+1;
68268 ** log file, instead of appending to it at pWal->hdr.mxFrame.
68278 iFrame = pWal->hdr.mxFrame;
68280 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
68281 u32 aCksum[2]; /* Checksum for wal-header */
68286 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
68287 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
68288 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
68289 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
68293 pWal->szPage = szPage;
68294 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
68295 pWal->hdr.aFrameCksum[0] = aCksum[0];
68296 pWal->hdr.aFrameCksum[1] = aCksum[1];
68297 pWal->truncateOnCommit = 1;
68299 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
68300 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
68307 ** an out-of-order write following a WAL restart could result in
68312 if( pWal->syncHeader ){
68313 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
68317 if( (int)pWal->szPage!=szPage ){
68323 w.pFd = pWal->pWalFd;
68331 for(p=pList; p; p=p->pDirty){
68336 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
68338 if( iFirst && (p->pDirty || isCommit==0) ){
68340 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
68345 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
68346 pWal->iReCksum = iWrite;
68348 pData = p->pData;
68349 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
68351 p->flags &= ~PGHDR_WAL_APPEND;
68358 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
68363 p->flags |= PGHDR_WAL_APPEND;
68367 if( isCommit && pWal->iReCksum ){
68388 if( pWal->padToSectorBoundary ){
68389 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
68390 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
68411 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
68412 i64 sz = pWal->mxWalSize;
68413 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
68417 pWal->truncateOnCommit = 0;
68420 /* Append data to the wal-index. It is not necessary to lock the
68421 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
68425 iFrame = pWal->hdr.mxFrame;
68426 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
68427 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
68429 rc = walIndexAppend(pWal, iFrame, p->pgno);
68434 nExtra--;
68435 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
68440 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
68443 pWal->hdr.mxFrame = iFrame;
68445 pWal->hdr.iChange++;
68446 pWal->hdr.nPage = nTruncate;
68448 /* If this is a commit, update the wal-index header too. */
68451 pWal->iCallback = iFrame;
68460 ** Write a set of frames to the log. The caller must hold the write-lock
68468 int szPage, /* Database page-size in bytes */
68489 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
68505 int isChanged = 0; /* True if a new wal-index header is loaded */
68509 assert( pWal->ckptLock==0 );
68510 assert( pWal->writeLock==0 );
68512 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
68516 if( pWal->readOnly ) return SQLITE_READONLY;
68520 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
68524 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
68526 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
68529 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
68536 pWal->ckptLock = 1;
68538 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
68542 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
68543 ** immediately, and a busy-handler is configured, it is invoked and the
68544 ** writer lock retried until either the busy-handler returns 0 or the
68550 pWal->writeLock = 1;
68560 /* Read the wal-index header. */
68566 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
68567 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
68573 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
68581 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
68583 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
68590 /* If a new wal-index header was loaded before the checkpoint was
68591 ** performed, then the pager-cache associated with pWal is now
68592 ** out of date. So zero the cached wal-index header to ensure that
68596 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
68604 if( pWal->ckptLock ){
68606 pWal->ckptLock = 0;
68623 ret = pWal->iCallback;
68624 pWal->iCallback = 0;
68635 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
68637 ** transition out of exclusive-mode is successful, return 1. This
68642 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
68644 ** WAL is already in exclusive-locking mode - meaning that this
68645 ** routine is a no-op. The pager must already hold the exclusive lock
68648 ** If op is negative, then do a dry-run of the op==1 case but do
68655 assert( pWal->writeLock==0 );
68656 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
68658 /* pWal->readLock is usually set, but might be -1 if there was a
68659 ** prior error while attempting to acquire are read-lock. This cannot
68662 ** upgrade to exclusive-mode following such an error.
68665 assert( pWal->readLock>=0 || pWal->lockError );
68667 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
68670 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
68671 pWal->exclusiveMode = WAL_NORMAL_MODE;
68672 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
68673 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
68675 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
68681 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
68682 assert( pWal->readLock>=0 );
68683 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
68684 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
68687 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
68693 ** Return true if the argument is non-NULL and the WAL module is using
68694 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
68695 ** WAL module is using shared-memory, return false.
68698 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
68711 assert( pWal->readLock>=0 && pWal->writeLock==0 );
68713 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
68721 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
68728 /* Try to open on pSnapshot when the next read-transaction starts
68734 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
68738 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
68747 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
68748 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
68749 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
68750 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
68771 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
68772 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
68798 ** read-lock. This function returns the database page-size if it is known,
68802 assert( pWal==0 || pWal->readLock>=0 );
68803 return (pWal ? pWal->szPage : 0);
68810 return pWal->pWalFd;
68847 ** This file implements an external (disk-based) database using BTrees.
68851 ** "Sorting And Searching", pages 473-480. Addison-Wesley
68857 ** ----------------------------------------------------------------
68858 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
68859 ** ----------------------------------------------------------------
68864 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
68886 ** page, or a pointer-map page.
68905 ** 40 60 15 4-byte meta values passed to higher layers
68910 ** 52 4 Largest root-page (auto/incr_vacuum)
68911 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
68914 ** 68 4 Application-ID
68916 ** 92 4 The version-valid-for number
68919 ** All of the integer values are big-endian (most significant byte first).
68927 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
68942 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
68945 ** |----------------|
68947 ** |----------------|
68949 ** |----------------|
68953 ** |----------------|
68956 ** |----------------| ^ Grows upwards
68959 ** |----------------|
68978 ** The cell pointer array contains zero or more 2-byte numbers which are
69009 ** appears first. A variable-length integer may not be more than 9 bytes long.
69011 ** allows a 64-bit integer to be encoded in 9 bytes.
69034 ** filled with data (pagesize - 4 bytes). The last page can have as little
69057 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
69064 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
69075 ** You can change this value at compile-time by specifying a
69076 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
69077 ** header must be exactly 16 bytes including the zero-terminator so
69089 ** first byte of on-disk image of every BTree page.
69099 ** is derived from the raw on-disk page content.
69106 ** stored in MemPage.pBt->mutex.
69110 u8 intKey; /* True if table b-trees. False for index b-trees */
69123 int nFree; /* Number of free bytes on the page. -1 for unknown */
69126 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
69127 ** non-overflow cell */
69131 u8 *aDataEnd; /* One byte past the end of the entire page - not just
69133 ** corruption-induced buffer overflow. */
69189 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
69203 ** If the shared-data extension is enabled, there may be multiple users
69215 # error wrong numeric code for no-transaction
69218 # error wrong numeric code for read-transaction
69221 # error wrong numeric code for write-transaction
69244 ** If a BtShared client fails to obtain a write-lock on a database
69245 ** table (because there exists one or more read-locks on the table),
69246 ** the shared-cache enters 'pending-lock' state and isPending is
69249 ** The shared-cache leaves the 'pending lock' state when either of
69255 ** while in the 'pending-lock' state, no connection may start a new
69258 ** This feature is included to help prevent writer-starvation.
69267 u8 autoVacuum; /* True if auto-vacuum is enabled */
69268 u8 incrVacuum; /* True if incr-vacuum is enabled */
69272 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
69275 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
69276 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
69285 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
69286 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
69290 BtLock *pLock; /* List of locks held on this shared-btree struct */
69306 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
69308 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
69320 u16 nSize; /* Size of the cell content on the main b-tree page */
69324 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
69327 ** root-node and 3 for all other internal nodes.
69336 ** b-tree within a database file.
69346 ** found at self->pBt->mutex.
69354 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
69355 ** sqlite3BtreePrevious() is no-op if skipNext<0.
69380 u8 curIntKey; /* Value of apPage[0]->intKey */
69382 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
69385 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
69411 ** Cursor is valid except that the Cursor.skipNext field is non-zero
69413 ** operation should be a no-op.
69438 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
69441 ** These macros define the location of the pointer-map entry for a
69446 ** PTRMAP_PAGENO returns the database page number of the pointer-map
69450 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
69452 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
69456 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
69473 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
69476 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
69484 ** overflow pages. The page-number identifies the previous
69487 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
69500 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
69501 assert( p->pBt->inTransaction>=p->inTrans );
69506 ** if the database supports auto-vacuum or not. Because it is used
69512 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
69523 ** the database. As the integrity-check proceeds, for each page used in
69524 ** the database the corresponding bit is set. This allows integrity-check to
69531 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
69543 u32 *heap; /* Min-heap used for analyzing cell coverage */
69548 ** Routines to read or write a two- and four-byte big-endian integer values.
69557 ** two-byte aligned address. get2byteAligned() is only used for accessing the
69576 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
69578 ** p->locked boolean to true.
69581 assert( p->locked==0 );
69582 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
69583 assert( sqlite3_mutex_held(p->db->mutex) );
69585 sqlite3_mutex_enter(p->pBt->mutex);
69586 p->pBt->db = p->db;
69587 p->locked = 1;
69591 ** Release the BtShared mutex associated with B-Tree handle p and
69592 ** clear the p->locked boolean.
69595 BtShared *pBt = p->pBt;
69596 assert( p->locked==1 );
69597 assert( sqlite3_mutex_held(pBt->mutex) );
69598 assert( sqlite3_mutex_held(p->db->mutex) );
69599 assert( p->db==pBt->db );
69601 sqlite3_mutex_leave(pBt->mutex);
69602 p->locked = 0;
69612 ** and this routine is a no-op. The underlying mutex is non-recursive.
69617 ** by all database connections. The p->pNext is a list of other
69620 ** p, then first unlock all of the others on p->pNext, then wait
69629 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
69630 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
69631 assert( p->pNext==0 || p->pNext->db==p->db );
69632 assert( p->pPrev==0 || p->pPrev->db==p->db );
69633 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
69636 assert( !p->locked || p->wantToLock>0 );
69637 assert( p->sharable || p->wantToLock==0 );
69640 assert( sqlite3_mutex_held(p->db->mutex) );
69644 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
69646 if( !p->sharable ) return;
69647 p->wantToLock++;
69648 if( p->locked ) return;
69665 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
69666 p->pBt->db = p->db;
69667 p->locked = 1;
69676 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
69677 assert( pLater->sharable );
69678 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
69679 assert( !pLater->locked || pLater->wantToLock>0 );
69680 if( pLater->locked ){
69685 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
69686 if( pLater->wantToLock ){
69697 assert( sqlite3_mutex_held(p->db->mutex) );
69698 if( p->sharable ){
69699 assert( p->wantToLock>0 );
69700 p->wantToLock--;
69701 if( p->wantToLock==0 ){
69710 ** B-Tree is not marked as sharable.
69715 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
69716 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
69717 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
69718 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
69720 return (p->sharable==0 || p->locked);
69732 ** There is a corresponding leave-all procedures.
69743 assert( sqlite3_mutex_held(db->mutex) );
69744 for(i=0; i<db->nDb; i++){
69745 p = db->aDb[i].pBt;
69746 if( p && p->sharable ){
69751 db->noSharedCache = skipOk;
69754 if( db->noSharedCache==0 ) btreeEnterAll(db);
69759 assert( sqlite3_mutex_held(db->mutex) );
69760 for(i=0; i<db->nDb; i++){
69761 p = db->aDb[i].pBt;
69766 if( db->noSharedCache==0 ) btreeLeaveAll(db);
69778 if( !sqlite3_mutex_held(db->mutex) ){
69781 for(i=0; i<db->nDb; i++){
69783 p = db->aDb[i].pBt;
69784 if( p && p->sharable &&
69785 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
69796 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
69800 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
69808 if( db->pVfs==0 && db->nDb==0 ) return 1;
69810 assert( iDb>=0 && iDb<db->nDb );
69811 if( !sqlite3_mutex_held(db->mutex) ) return 0;
69813 p = db->aDb[iDb].pBt;
69815 return p->sharable==0 || p->locked==1;
69823 ** these two routines, all mutex operations are no-ops in that case and
69827 ** the ones below, are no-ops and are null #defines in btree.h.
69831 p->pBt->db = p->db;
69835 for(i=0; i<db->nDb; i++){
69836 Btree *p = db->aDb[i].pBt;
69838 p->pBt->db = p->db;
69853 sqlite3BtreeEnter(pCur->pBtree);
69857 sqlite3BtreeLeave(pCur->pBtree);
69877 ** This file implements an external (disk-based) database using BTrees.
69901 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
69906 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
69909 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
69922 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
69967 ** shared-cache table level locks. If the library is compiled with the
69968 ** shared-cache feature disabled, then there is only ever one user
69970 ** So define the lock related functions as no-ops.
69985 u64 n = pBt->nSeek;
69986 pBt->nSeek = 0;
69997 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
70005 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
70016 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
70028 ** For example, when writing to a table with root-page iRoot via
70041 ** hold a write-lock on the schema table (root page 1). This is also
70046 Pgno iRoot, /* Root page of b-tree */
70047 int isIndex, /* True if iRoot is the root of an index b-tree */
70050 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
70055 ** and has the read-uncommitted flag set, then no lock is required.
70058 if( (pBtree->sharable==0)
70059 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
70066 ** the correct locks are held. So do not bother - just return true.
70069 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
70073 /* Figure out the root-page that the lock should be held on. For table
70074 ** b-trees, this is just the root page of the b-tree being read or
70075 ** written. For index b-trees, it is the root page of the associated
70080 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
70082 if( pIdx->tnum==iRoot ){
70089 iTab = pIdx->pTable->tnum;
70097 /* Search for the required lock. Either a write-lock on root-page iTab, a
70098 ** write-lock on the schema table, or (if the client is reading) a
70099 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
70100 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
70101 if( pLock->pBtree==pBtree
70102 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
70103 && pLock->eLock>=eLockType
70125 ** read-uncommitted flag set, then it is OK for the other object to
70135 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70136 if( p->pgnoRoot==iRoot
70137 && p->pBtree!=pBtree
70138 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
70149 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
70154 BtShared *pBt = p->pBt;
70159 assert( p->db!=0 );
70160 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
70162 /* If requesting a write-lock, then the Btree must have an open write
70166 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
70167 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
70169 /* This routine is a no-op if the shared-cache is not enabled */
70170 if( !p->sharable ){
70177 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
70178 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
70182 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70183 /* The condition (pIter->eLock!=eLock) in the following if(...)
70186 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
70192 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
70193 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
70194 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
70195 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
70197 assert( p==pBt->pWriter );
70198 pBt->btsFlags |= BTS_PENDING;
70209 ** Add a lock on the table with root-page iTable to the shared-btree used
70226 BtShared *pBt = p->pBt;
70232 assert( p->db!=0 );
70234 /* A connection with the read-uncommitted flag set will never try to
70235 ** obtain a read-lock using this function. The only read-lock obtained
70236 ** by a connection in read-uncommitted mode is on the sqlite_schema
70238 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
70240 /* This function should only be called on a sharable b-tree after it
70241 ** has been determined that no other b-tree holds a conflicting lock. */
70242 assert( p->sharable );
70246 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70247 if( pIter->iTable==iTable && pIter->pBtree==p ){
70261 pLock->iTable = iTable;
70262 pLock->pBtree = p;
70263 pLock->pNext = pBt->pLock;
70264 pBt->pLock = pLock;
70268 ** and the requested lock. This means if a write-lock was already held
70269 ** and a read-lock requested, we don't incorrectly downgrade the lock.
70272 if( eLock>pLock->eLock ){
70273 pLock->eLock = eLock;
70290 BtShared *pBt = p->pBt;
70291 BtLock **ppIter = &pBt->pLock;
70294 assert( p->sharable || 0==*ppIter );
70295 assert( p->inTrans>0 );
70299 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
70300 assert( pLock->pBtree->inTrans>=pLock->eLock );
70301 if( pLock->pBtree==p ){
70302 *ppIter = pLock->pNext;
70303 assert( pLock->iTable!=1 || pLock==&p->lock );
70304 if( pLock->iTable!=1 ){
70308 ppIter = &pLock->pNext;
70312 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
70313 if( pBt->pWriter==p ){
70314 pBt->pWriter = 0;
70315 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
70316 }else if( pBt->nTransaction==2 ){
70326 pBt->btsFlags &= ~BTS_PENDING;
70331 ** This function changes all write-locks held by Btree p into read-locks.
70334 BtShared *pBt = p->pBt;
70335 if( pBt->pWriter==p ){
70337 pBt->pWriter = 0;
70338 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
70339 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
70340 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
70341 pLock->eLock = READ_LOCK;
70359 return sqlite3_mutex_held(p->pBt->mutex);
70363 ** database connetion. This is important in shared-cache mode. If the database
70364 ** connection pointers get out-of-sync, it is possible for routines like
70367 ** statements only and for the purpose of double-checking that the btree code
70368 ** does keep the database connection pointers up-to-date.
70372 return (p->pBtree->db==p->pBt->db);
70380 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
70383 ** Invalidate the overflow page-list cache for all cursors opened
70388 assert( sqlite3_mutex_held(pBt->mutex) );
70389 for(p=pBt->pCursor; p; p=p->pNext){
70402 ** cursors open on any row within the table with root-page pgnoRoot.
70415 assert( pBtree->hasIncrblobCur );
70417 pBtree->hasIncrblobCur = 0;
70418 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
70419 if( (p->curFlags & BTCF_Incrblob)!=0 ){
70420 pBtree->hasIncrblobCur = 1;
70421 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
70422 p->eState = CURSOR_INVALID;
70435 ** when a page that previously contained data becomes a free-list leaf
70440 ** free-list leaf pages:
70443 ** a free-list leaf page, the page is not written to the database
70444 ** (as free-list leaf pages contain no meaningful data). Sometimes
70448 ** 2) When a free-list leaf page is reused, its content is not read
70454 ** a page is moved to the free-list and then reused within the same
70456 ** it is moved to the free-list and it is also not journalled when it
70457 ** is extracted from the free-list and reused, then the original data
70462 ** moved to become a free-list leaf page, the corresponding bit is
70463 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
70470 if( !pBt->pHasContent ){
70471 assert( pgno<=pBt->nPage );
70472 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
70473 if( !pBt->pHasContent ){
70477 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
70478 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
70486 ** This function is called when a free-list leaf page is removed from the
70487 ** free-list for reuse. It returns false if it is safe to retrieve the
70488 ** page from the pager layer with the 'no-content' flag set. True otherwise.
70491 Bitvec *p = pBt->pHasContent;
70497 ** invoked at the conclusion of each write-transaction.
70500 sqlite3BitvecDestroy(pBt->pHasContent);
70501 pBt->pHasContent = 0;
70509 if( pCur->iPage>=0 ){
70510 for(i=0; i<pCur->iPage; i++){
70511 releasePageNotNull(pCur->apPage[i]);
70513 releasePageNotNull(pCur->pPage);
70514 pCur->iPage = -1;
70521 ** function saves the current cursor key in variables pCur->nKey and
70522 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
70526 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
70527 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
70528 ** set to point to a malloced buffer pCur->nKey bytes in size containing
70533 assert( CURSOR_VALID==pCur->eState );
70534 assert( 0==pCur->pKey );
70537 if( pCur->curIntKey ){
70539 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
70544 ** up to the size of 1 varint plus 1 8-byte value when the cursor
70548 pCur->nKey = sqlite3BtreePayloadSize(pCur);
70549 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
70551 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
70553 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
70554 pCur->pKey = pKey;
70562 assert( !pCur->curIntKey || !pCur->pKey );
70576 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
70577 assert( 0==pCur->pKey );
70580 if( pCur->curFlags & BTCF_Pinned ){
70583 if( pCur->eState==CURSOR_SKIPNEXT ){
70584 pCur->eState = CURSOR_VALID;
70586 pCur->skipNext = 0;
70592 pCur->eState = CURSOR_REQUIRESEEK;
70595 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
70604 ** the table with root-page iRoot. "Saving the cursor position" means that
70615 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
70625 assert( sqlite3_mutex_held(pBt->mutex) );
70626 assert( pExcept==0 || pExcept->pBt==pBt );
70627 for(p=pBt->pCursor; p; p=p->pNext){
70628 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
70631 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
70646 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
70647 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
70653 testcase( p->iPage>=0 );
70657 p = p->pNext;
70667 sqlite3_free(pCur->pKey);
70668 pCur->pKey = 0;
70669 pCur->eState = CURSOR_INVALID;
70688 KeyInfo *pKeyInfo = pCur->pKeyInfo;
70693 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
70698 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
70717 assert( pCur->eState>=CURSOR_REQUIRESEEK );
70718 if( pCur->eState==CURSOR_FAULT ){
70719 return pCur->skipNext;
70721 pCur->eState = CURSOR_INVALID;
70725 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
70728 sqlite3_free(pCur->pKey);
70729 pCur->pKey = 0;
70730 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
70731 if( skipNext ) pCur->skipNext = skipNext;
70732 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
70733 pCur->eState = CURSOR_SKIPNEXT;
70740 (p->eState>=CURSOR_REQUIRESEEK ? \
70760 assert( sizeof(pCur->eState)==1 );
70792 assert( pCur->eState!=CURSOR_VALID );
70798 if( pCur->eState!=CURSOR_VALID ){
70839 pCur->hints = x;
70846 ** number for the pointer-map page that contains the entry for the
70856 assert( sqlite3_mutex_held(pBt->mutex) );
70858 nPagesPerMapPage = (pBt->usableSize/5)+1;
70859 iPtrMap = (pgno-2)/nPagesPerMapPage;
70873 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
70874 ** a no-op. If an error occurs, the appropriate error code is written
70886 assert( sqlite3_mutex_held(pBt->mutex) );
70887 /* The super-journal page number must never be used as a pointer map page */
70890 assert( pBt->autoVacuum );
70896 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
70913 assert( offset <= (int)pBt->usableSize-5 );
70917 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
70943 assert( sqlite3_mutex_held(pBt->mutex) );
70946 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
70957 assert( offset <= (int)pBt->usableSize-5 );
70979 ** 4-byte child pointer found on interior pages, if there is one.
70984 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
70986 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
70992 ** on a single B-tree page. Make necessary adjustments to the CellInfo
71013 minLocal = pPage->minLocal;
71014 maxLocal = pPage->maxLocal;
71015 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
71019 pInfo->nLocal = (u16)surplus;
71021 pInfo->nLocal = (u16)minLocal;
71023 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
71032 maxLocal = pPage->maxLocal;
71038 minLocal = pPage->minLocal;
71039 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
71063 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71064 assert( pPage->leaf==0 );
71065 assert( pPage->childPtrSize==4 );
71069 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
71070 pInfo->nPayload = 0;
71071 pInfo->nLocal = 0;
71072 pInfo->pPayload = 0;
71084 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71085 assert( pPage->leaf==0 || pPage->leaf==1 );
71086 assert( pPage->intKeyLeaf );
71087 assert( pPage->childPtrSize==0 );
71108 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
71111 ** This routine is a high-runner.
71145 pInfo->nKey = *(i64*)&iKey;
71146 pInfo->nPayload = nPayload;
71147 pInfo->pPayload = pIter;
71148 testcase( nPayload==pPage->maxLocal );
71149 testcase( nPayload==(u32)pPage->maxLocal+1 );
71150 if( nPayload<=pPage->maxLocal ){
71154 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71155 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71156 pInfo->nLocal = (u16)nPayload;
71169 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71170 assert( pPage->leaf==0 || pPage->leaf==1 );
71171 assert( pPage->intKeyLeaf==0 );
71172 pIter = pCell + pPage->childPtrSize;
71182 pInfo->nKey = nPayload;
71183 pInfo->nPayload = nPayload;
71184 pInfo->pPayload = pIter;
71185 testcase( nPayload==pPage->maxLocal );
71186 testcase( nPayload==(u32)pPage->maxLocal+1 );
71187 if( nPayload<=pPage->maxLocal ){
71191 pInfo->nSize = nPayload + (u16)(pIter - pCell);
71192 if( pInfo->nSize<4 ) pInfo->nSize = 4;
71193 pInfo->nLocal = (u16)nPayload;
71203 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
71211 ** data area of the btree-page. The return number includes the cell
71231 pPage->xParseCell(pPage, pCell, &debuginfo);
71234 assert( pPage->childPtrSize==4 );
71244 testcase( nSize==pPage->maxLocal );
71245 testcase( nSize==(u32)pPage->maxLocal+1 );
71246 if( nSize<=pPage->maxLocal ){
71247 nSize += (u32)(pIter - pCell);
71250 int minLocal = pPage->minLocal;
71251 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71252 testcase( nSize==pPage->maxLocal );
71253 testcase( nSize==(u32)pPage->maxLocal+1 );
71254 if( nSize>pPage->maxLocal ){
71257 nSize += 4 + (u16)(pIter - pCell);
71273 pPage->xParseCell(pPage, pCell, &debuginfo);
71276 assert( pPage->childPtrSize==0 );
71286 testcase( nSize==pPage->maxLocal );
71287 testcase( nSize==(u32)pPage->maxLocal+1 );
71288 if( nSize<=pPage->maxLocal ){
71289 nSize += (u32)(pIter - pCell);
71292 int minLocal = pPage->minLocal;
71293 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71294 testcase( nSize==pPage->maxLocal );
71295 testcase( nSize==(u32)pPage->maxLocal+1 );
71296 if( nSize>pPage->maxLocal ){
71299 nSize += 4 + (u16)(pIter - pCell);
71314 pPage->xParseCell(pPage, pCell, &debuginfo);
71319 assert( pPage->childPtrSize==4 );
71322 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
71323 return (u16)(pIter - pCell);
71336 pPage->xParseCell(pPage, pCell, &debuginfo);
71348 /* pIter now points at the 64-bit integer key value, a variable length
71359 testcase( nSize==pPage->maxLocal );
71360 testcase( nSize==(u32)pPage->maxLocal+1 );
71361 if( nSize<=pPage->maxLocal ){
71362 nSize += (u32)(pIter - pCell);
71365 int minLocal = pPage->minLocal;
71366 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
71367 testcase( nSize==pPage->maxLocal );
71368 testcase( nSize==(u32)pPage->maxLocal+1 );
71369 if( nSize>pPage->maxLocal ){
71372 nSize += 4 + (u16)(pIter - pCell);
71383 return pPage->xCellSize(pPage, findCell(pPage, iCell));
71391 ** pointer to an overflow page, insert an entry into the pointer-map for
71398 pPage->xParseCell(pPage, pCell, &info);
71401 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
71406 ovfl = get4byte(&pCell[info.nSize-4]);
71407 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
71415 ** page so that there are no free-blocks on the free-block list.
71420 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
71421 ** b-tree page so that there are no freeblocks or fragment bytes, all
71427 int pc; /* Address of the i-th cell */
71441 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71442 assert( pPage->pBt!=0 );
71443 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
71444 assert( pPage->nOverflow==0 );
71445 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71446 data = pPage->aData;
71447 hdr = pPage->hdrOffset;
71448 cellOffset = pPage->cellOffset;
71449 nCell = pPage->nCell;
71452 usableSize = pPage->pBt->usableSize;
71457 ** offsets to each pointer in the cell-pointer array than it is to
71461 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
71464 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
71478 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
71485 assert( cbrk+(iFree-top) <= usableSize );
71486 memmove(&data[cbrk], &data[top], iFree-top);
71498 iCellLast = usableSize - 4;
71501 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
71505 u8 *pAddr; /* The i-th cell pointer */
71517 size = pPage->xCellSize(pPage, &src[pc]);
71518 cbrk -= size;
71532 assert( pPage->nFree>=0 );
71533 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
71540 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
71541 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71546 ** Search the free-list on page pPg for space to store a cell nByte bytes in
71548 ** from the free-list.
71550 ** If no suitable space can be found on the free-list, return NULL.
71560 const int hdr = pPg->hdrOffset; /* Offset to page header */
71561 u8 * const aData = pPg->aData; /* Page data */
71566 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
71571 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
71572 ** freeblock form a big-endian integer which is the size of the freeblock
71573 ** in bytes, including the 4-byte header. */
71576 if( (x = size - nByte)>=0 ){
71580 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
71584 /* Remove the slot from the free-list. Update the number of
71594 /* The slot remains on the free-list. Reduce its size to account
71611 if( pc>maxPC+nByte-4 ){
71619 ** Allocate nByte bytes of space from within the B-Tree page passed
71620 ** as the first argument. Write into *pIdx the index into pPage->aData[]
71632 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
71633 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
71639 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71640 assert( pPage->pBt );
71641 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71643 assert( pPage->nFree>=nByte );
71644 assert( pPage->nOverflow==0 );
71645 assert( nByte < (int)(pPage->pBt->usableSize-8) );
71647 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
71648 gap = pPage->cellOffset + 2*pPage->nCell;
71650 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
71653 ** However, that integer is too large to be stored in a 2-byte unsigned
71658 if( top==0 && pPage->pBt->usableSize==65536 ){
71663 }else if( top>(int)pPage->pBt->usableSize ){
71678 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
71679 *pIdx = g2 = (int)(pSpace-data);
71695 assert( pPage->nCell>0 || CORRUPT_DB );
71696 assert( pPage->nFree>=0 );
71697 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
71710 top -= nByte;
71712 assert( top+nByte <= (int)pPage->pBt->usableSize );
71718 ** Return a section of the pPage->aData to the freelist.
71719 ** The first byte of the new free block is pPage->aData[iStart]
71738 unsigned char *data = pPage->aData; /* Page content */
71741 assert( pPage->pBt!=0 );
71742 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71743 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
71744 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
71745 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71747 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
71752 hdr = pPage->hdrOffset;
71764 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
71776 nFrag = iFreeBlk - iEnd;
71779 if( iEnd > pPage->pBt->usableSize ){
71782 iSize = iEnd - iStart;
71794 nFrag += iStart - iPtrEnd;
71795 iSize = iEnd - iPtr;
71800 data[hdr+7] -= nFrag;
71804 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
71823 pPage->nFree += iOrigSize;
71840 BtShared *pBt; /* A copy of pPage->pBt */
71842 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
71843 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71844 pBt = pPage->pBt;
71845 pPage->max1bytePayload = pBt->max1bytePayload;
71847 pPage->childPtrSize = 0;
71848 pPage->leaf = 1;
71850 pPage->intKeyLeaf = 1;
71851 pPage->xCellSize = cellSizePtrTableLeaf;
71852 pPage->xParseCell = btreeParseCellPtr;
71853 pPage->intKey = 1;
71854 pPage->maxLocal = pBt->maxLeaf;
71855 pPage->minLocal = pBt->minLeaf;
71857 pPage->intKey = 0;
71858 pPage->intKeyLeaf = 0;
71859 pPage->xCellSize = cellSizePtrIdxLeaf;
71860 pPage->xParseCell = btreeParseCellPtrIndex;
71861 pPage->maxLocal = pBt->maxLocal;
71862 pPage->minLocal = pBt->minLocal;
71864 pPage->intKey = 0;
71865 pPage->intKeyLeaf = 0;
71866 pPage->xCellSize = cellSizePtrIdxLeaf;
71867 pPage->xParseCell = btreeParseCellPtrIndex;
71871 pPage->childPtrSize = 4;
71872 pPage->leaf = 0;
71874 pPage->intKey = 0;
71875 pPage->intKeyLeaf = 0;
71876 pPage->xCellSize = cellSizePtr;
71877 pPage->xParseCell = btreeParseCellPtrIndex;
71878 pPage->maxLocal = pBt->maxLocal;
71879 pPage->minLocal = pBt->minLocal;
71881 pPage->intKeyLeaf = 0;
71882 pPage->xCellSize = cellSizePtrNoPayload;
71883 pPage->xParseCell = btreeParseCellPtrNoPayload;
71884 pPage->intKey = 1;
71885 pPage->maxLocal = pBt->maxLeaf;
71886 pPage->minLocal = pBt->minLeaf;
71888 pPage->intKey = 0;
71889 pPage->intKeyLeaf = 0;
71890 pPage->xCellSize = cellSizePtr;
71891 pPage->xParseCell = btreeParseCellPtrIndex;
71900 ** in the pPage->nFree field.
71903 int pc; /* Address of a freeblock within pPage->aData[] */
71905 u8 *data; /* Equal to pPage->aData */
71912 assert( pPage->pBt!=0 );
71913 assert( pPage->pBt->db!=0 );
71914 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71915 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
71916 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
71917 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
71918 assert( pPage->isInit==1 );
71919 assert( pPage->nFree<0 );
71921 usableSize = pPage->pBt->usableSize;
71922 hdr = pPage->hdrOffset;
71923 data = pPage->aData;
71924 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
71928 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
71929 iCellLast = usableSize - 4;
71932 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
71936 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
71940 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
71967 ** of the cell-content area plus the number of free bytes within
71968 ** the cell-content area. If this is greater than the usable-size
71970 ** serves to verify that the offset to the start of the cell-content
71976 pPage->nFree = (u16)(nFree - iCellFirst);
71989 int pc; /* Address of a freeblock within pPage->aData[] */
71990 u8 *data; /* Equal to pPage->aData */
71994 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
71995 usableSize = pPage->pBt->usableSize;
71996 iCellLast = usableSize - 4;
71997 data = pPage->aData;
71998 cellOffset = pPage->cellOffset;
71999 if( !pPage->leaf ) iCellLast--;
72000 for(i=0; i<pPage->nCell; i++){
72007 sz = pPage->xCellSize(pPage, &data[pc]);
72020 ** not contain a well-formed database page, then return
72022 ** guarantee that the page is well-formed. It only shows that
72026 u8 *data; /* Equal to pPage->aData */
72029 assert( pPage->pBt!=0 );
72030 assert( pPage->pBt->db!=0 );
72031 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72032 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
72033 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
72034 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
72035 assert( pPage->isInit==0 );
72037 pBt = pPage->pBt;
72038 data = pPage->aData + pPage->hdrOffset;
72039 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
72040 ** the b-tree page type. */
72044 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72045 pPage->maskPage = (u16)(pBt->pageSize - 1);
72046 pPage->nOverflow = 0;
72047 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
72048 pPage->aCellIdx = data + pPage->childPtrSize + 8;
72049 pPage->aDataEnd = pPage->aData + pBt->pageSize;
72050 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
72051 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72053 pPage->nCell = get2byte(&data[3]);
72054 if( pPage->nCell>MX_CELL(pBt) ){
72058 testcase( pPage->nCell==MX_CELL(pBt) );
72059 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
72063 assert( pPage->nCell>0
72064 || get2byteNotZero(&data[5])==(int)pBt->usableSize
72066 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
72067 pPage->isInit = 1;
72068 if( pBt->db->flags & SQLITE_CellSizeCk ){
72079 unsigned char *data = pPage->aData;
72080 BtShared *pBt = pPage->pBt;
72081 u8 hdr = pPage->hdrOffset;
72084 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
72085 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72086 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
72087 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72088 assert( sqlite3_mutex_held(pBt->mutex) );
72089 if( pBt->btsFlags & BTS_FAST_SECURE ){
72090 memset(&data[hdr], 0, pBt->usableSize - hdr);
72096 put2byte(&data[hdr+5], pBt->usableSize);
72097 pPage->nFree = (u16)(pBt->usableSize - first);
72099 pPage->cellOffset = first;
72100 pPage->aDataEnd = &data[pBt->pageSize];
72101 pPage->aCellIdx = &data[first];
72102 pPage->aDataOfst = &data[pPage->childPtrSize];
72103 pPage->nOverflow = 0;
72104 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
72105 pPage->maskPage = (u16)(pBt->pageSize - 1);
72106 pPage->nCell = 0;
72107 pPage->isInit = 1;
72117 if( pgno!=pPage->pgno ){
72118 pPage->aData = sqlite3PagerGetData(pDbPage);
72119 pPage->pDbPage = pDbPage;
72120 pPage->pBt = pBt;
72121 pPage->pgno = pgno;
72122 pPage->hdrOffset = pgno==1 ? 100 : 0;
72124 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72149 assert( sqlite3_mutex_held(pBt->mutex) );
72150 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
72163 assert( sqlite3_mutex_held(pBt->mutex) );
72164 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
72173 ** error, return ((unsigned int)-1).
72176 return pBt->nPage;
72180 return btreePagecount(p->pBt);
72190 int bReadOnly /* True for a read-only page */
72195 assert( sqlite3_mutex_held(pBt->mutex) );
72201 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
72207 if( pPage->isInit==0 ){
72216 assert( pPage->pgno==pgno || CORRUPT_DB );
72217 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
72229 assert( pPage->aData );
72230 assert( pPage->pBt );
72231 assert( pPage->pDbPage!=0 );
72232 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72233 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72234 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72235 sqlite3PagerUnrefNotNull(pPage->pDbPage);
72242 assert( pPage->aData );
72243 assert( pPage->pBt );
72244 assert( pPage->pDbPage!=0 );
72245 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
72246 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
72247 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72248 sqlite3PagerUnrefPageOne(pPage->pDbPage);
72268 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
72273 (*ppPage)->isInit = 0;
72293 if( pPage->isInit ){
72294 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72295 pPage->isInit = 0;
72302 ** the call for every page that comes in for re-initializing. */
72313 assert( pBt->db );
72314 assert( sqlite3_mutex_held(pBt->db->mutex) );
72315 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
72323 ** be exclusively in memory, or it might use a disk-based memory cache.
72327 ** If zFilename is ":memory:" then an in-memory database is created
72340 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
72357 /* Set the variable isMemdb to true for an in-memory database, or
72358 ** false for a file-based database.
72370 assert( sqlite3_mutex_held(db->mutex) );
72389 p->inTrans = TRANS_NONE;
72390 p->db = db;
72392 p->lock.pBtree = p;
72393 p->lock.iTable = 1;
72404 int nFullPathname = pVfs->mxPathname+1;
72408 p->sharable = 1;
72434 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
72435 assert( pBt->nRef>0 );
72436 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
72437 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
72439 for(iDb=db->nDb-1; iDb>=0; iDb--){
72440 Btree *pExisting = db->aDb[iDb].pBt;
72441 if( pExisting && pExisting->pBt==pBt ){
72449 p->pBt = pBt;
72450 pBt->nRef++;
72464 p->sharable = 1;
72481 /* Suppress false-positive compiler warning from PVS-Studio */
72489 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
72492 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
72493 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
72498 pBt->openFlags = (u8)flags;
72499 pBt->db = db;
72500 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
72501 p->pBt = pBt;
72503 pBt->pCursor = 0;
72504 pBt->pPage1 = 0;
72505 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
72507 pBt->btsFlags |= BTS_SECURE_DELETE;
72509 pBt->btsFlags |= BTS_OVERWRITE;
72511 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
72512 ** determined by the 2-byte integer located at an offset of 16 bytes from
72514 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
72515 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
72516 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
72517 pBt->pageSize = 0;
72519 /* If the magic name ":memory:" will create an in-memory database, then
72520 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
72523 ** regular file-name. In this case the auto-vacuum applies as per normal.
72526 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
72527 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
72532 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
72533 ** determined by the one-byte unsigned integer found at an offset of 20
72536 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72538 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
72539 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
72542 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
72544 pBt->usableSize = pBt->pageSize - nReserve;
72545 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
72550 pBt->nRef = 1;
72551 if( p->sharable ){
72555 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
72556 if( pBt->mutex==0 ){
72562 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
72574 if( p->sharable ){
72577 for(i=0; i<db->nDb; i++){
72578 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
72579 while( pSib->pPrev ){ pSib = pSib->pPrev; }
72580 if( (uptr)p->pBt<(uptr)pSib->pBt ){
72581 p->pNext = pSib;
72582 p->pPrev = 0;
72583 pSib->pPrev = p;
72585 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
72586 pSib = pSib->pNext;
72588 p->pNext = pSib->pNext;
72589 p->pPrev = pSib;
72590 if( p->pNext ){
72591 p->pNext->pPrev = p;
72593 pSib->pNext = p;
72604 if( pBt && pBt->pPager ){
72605 sqlite3PagerClose(pBt->pPager, 0);
72613 /* If the B-Tree was successfully opened, set the pager-cache size to the
72614 ** default value. Except, when opening on an existing shared pager-cache,
72615 ** do not change the pager-cache size.
72621 pFile = sqlite3PagerFile(pBt->pPager);
72622 if( pFile->pMethods ){
72623 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
72646 assert( sqlite3_mutex_notheld(pBt->mutex) );
72649 pBt->nRef--;
72650 if( pBt->nRef<=0 ){
72652 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
72655 while( ALWAYS(pList) && pList->pNext!=pBt ){
72656 pList=pList->pNext;
72659 pList->pNext = pBt->pNext;
72663 sqlite3_mutex_free(pBt->mutex);
72675 ** Make sure pBt->pTmpSpace points to an allocation of
72676 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
72681 assert( pBt->pTmpSpace==0 );
72684 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
72685 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
72686 if( pBt->pTmpSpace==0 ){
72687 BtCursor *pCur = pBt->pCursor;
72688 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
72693 /* One of the uses of pBt->pTmpSpace is to format cells before
72706 ** left-child pointer to the beginning of a cell.
72708 memset(pBt->pTmpSpace, 0, 8);
72709 pBt->pTmpSpace += 4;
72714 ** Free the pBt->pTmpSpace allocation
72717 if( pBt->pTmpSpace ){
72718 pBt->pTmpSpace -= 4;
72719 sqlite3PageFree(pBt->pTmpSpace);
72720 pBt->pTmpSpace = 0;
72728 BtShared *pBt = p->pBt;
72731 assert( sqlite3_mutex_held(p->db->mutex) );
72737 BtCursor *pCur = pBt->pCursor;
72740 pCur = pCur->pNext;
72741 assert( pTmp->pBtree!=p );
72748 ** The call to sqlite3BtreeRollback() drops any table-locks held by
72754 /* If there are still other outstanding references to the shared-btree
72756 ** up the shared-btree.
72758 assert( p->wantToLock==0 && p->locked==0 );
72759 if( !p->sharable || removeFromSharingList(pBt) ){
72765 assert( !pBt->pCursor );
72766 sqlite3PagerClose(pBt->pPager, p->db);
72767 if( pBt->xFreeSchema && pBt->pSchema ){
72768 pBt->xFreeSchema(pBt->pSchema);
72770 sqlite3DbFree(0, pBt->pSchema);
72776 assert( p->wantToLock==0 );
72777 assert( p->locked==0 );
72778 if( p->pPrev ) p->pPrev->pNext = p->pNext;
72779 if( p->pNext ) p->pNext->pPrev = p->pPrev;
72794 BtShared *pBt = p->pBt;
72795 assert( sqlite3_mutex_held(p->db->mutex) );
72797 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
72813 BtShared *pBt = p->pBt;
72815 assert( sqlite3_mutex_held(p->db->mutex) );
72817 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
72828 BtShared *pBt = p->pBt;
72829 assert( sqlite3_mutex_held(p->db->mutex) );
72831 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
72842 ** is a very low but non-zero probability of damage. Level 3 reduces the
72850 BtShared *pBt = p->pBt;
72851 assert( sqlite3_mutex_held(p->db->mutex) );
72853 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
72882 BtShared *pBt = p->pBt;
72885 pBt->nReserveWanted = nReserve;
72886 x = pBt->pageSize - pBt->usableSize;
72888 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
72894 ((pageSize-1)&pageSize)==0 ){
72896 assert( !pBt->pCursor );
72898 pBt->pageSize = (u32)pageSize;
72901 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
72902 pBt->usableSize = pBt->pageSize - (u16)nReserve;
72903 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72912 return p->pBt->pageSize;
72917 ** may only be called if it is guaranteed that the b-tree mutex is already
72921 ** known that the shared b-tree mutex is held, but the mutex on the
72928 assert( sqlite3_mutex_held(p->pBt->mutex) );
72929 n = p->pBt->pageSize - p->pBt->usableSize;
72940 ** The amount of reserve can only grow - never shrink.
72945 n1 = (int)p->pBt->nReserveWanted;
72960 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
72971 ** newFlag==(-1) No changes
72976 ** freelist leaf pages are not written back to the database. Thus in-page
72990 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
72991 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
72993 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
72999 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
73000 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
73001 ** is disabled. The default value for the auto-vacuum property is
73008 BtShared *pBt = p->pBt;
73013 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
73016 pBt->autoVacuum = av ?1:0;
73017 pBt->incrVacuum = av==2 ?1:0;
73025 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
73035 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
73036 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
73045 ** If the user has not set the safety-level for this database connection
73046 ** using "PRAGMA synchronous", and if the safety-level is not already
73055 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
73056 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
73057 if( pDb->bSyncSet==0
73058 && pDb->safety_level!=safety_level
73059 && pDb!=&db->aDb[1]
73061 pDb->safety_level = safety_level;
73062 sqlite3PagerSetFlags(pBt->pPager,
73063 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
73080 ** well-formed database file, then SQLITE_CORRUPT is returned.
73090 assert( sqlite3_mutex_held(pBt->mutex) );
73091 assert( pBt->pPage1==0 );
73092 rc = sqlite3PagerSharedLock(pBt->pPager);
73100 nPage = get4byte(28+(u8*)pPage1->aData);
73101 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
73102 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
73105 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
73111 u8 *page1 = pPage1->aData;
73113 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
73122 pBt->btsFlags |= BTS_READ_ONLY;
73129 pBt->btsFlags |= BTS_READ_ONLY;
73140 ** may not be the latest version - there may be a newer one in the log
73143 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
73145 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
73161 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
73170 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
73171 ** determined by the 2-byte integer located at an offset of 16 bytes from
73174 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
73176 if( ((pageSize-1)&pageSize)!=0
73183 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
73187 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
73188 ** determined by the one-byte unsigned integer found at an offset of 20
73190 usableSize = pageSize - page1[20];
73191 if( (u32)pageSize!=pBt->pageSize ){
73193 ** of BtShared.pageSize, we have discovered that the page-size is
73194 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
73196 ** again with the correct page-size.
73199 pBt->usableSize = usableSize;
73200 pBt->pageSize = pageSize;
73201 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73203 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
73204 pageSize-usableSize);
73208 if( sqlite3WritableSchema(pBt->db)==0 ){
73215 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
73221 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73222 pBt->pageSize = pageSize;
73223 pBt->usableSize = usableSize;
73225 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
73226 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
73232 ** cells can will fit on one page. We assume a 10-byte page header.
73234 ** 2-byte pointer to the cell
73235 ** 4-byte child pointer
73236 ** 9-byte nKey value
73237 ** 4-byte nData value
73238 ** 4-byte overflow page pointer
73239 ** So a cell consists of a 2-byte pointer, a header which is as much as
73243 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
73244 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
73245 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
73246 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
73247 if( pBt->maxLocal>127 ){
73248 pBt->max1bytePayload = 127;
73250 pBt->max1bytePayload = (u8)pBt->maxLocal;
73252 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
73253 pBt->pPage1 = pPage1;
73254 pBt->nPage = nPage;
73259 pBt->pPage1 = 0;
73279 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
73280 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
73281 && pCur->eState!=CURSOR_FAULT ) r++;
73293 ** If there is a transaction in progress, this routine is a no-op.
73296 assert( sqlite3_mutex_held(pBt->mutex) );
73297 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
73298 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
73299 MemPage *pPage1 = pBt->pPage1;
73300 assert( pPage1->aData );
73301 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
73302 pBt->pPage1 = 0;
73317 assert( sqlite3_mutex_held(pBt->mutex) );
73318 if( pBt->nPage>0 ){
73321 pP1 = pBt->pPage1;
73323 data = pP1->aData;
73324 rc = sqlite3PagerWrite(pP1->pDbPage);
73328 data[16] = (u8)((pBt->pageSize>>8)&0xff);
73329 data[17] = (u8)((pBt->pageSize>>16)&0xff);
73332 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
73333 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
73337 memset(&data[24], 0, 100-24);
73339 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
73341 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
73342 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
73343 put4byte(&data[36 + 4*4], pBt->autoVacuum);
73344 put4byte(&data[36 + 7*4], pBt->incrVacuum);
73346 pBt->nPage = 1;
73359 p->pBt->nPage = 0;
73360 rc = newDatabase(p->pBt);
73366 ** Attempt to start a new transaction. A write-transaction
73367 ** is started if the second argument is nonzero, otherwise a read-
73371 ** upgraded to exclusive by calling this routine a second time - the
73374 ** A write-transaction must be started before attempting any
73388 ** if there is one. But if there was previously a read-lock, do not
73389 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
73390 ** returned when there is already a read-lock in order to avoid a deadlock.
73405 BtShared *pBt = p->pBt;
73406 Pager *pPager = pBt->pPager;
73412 /* If the btree is already in a write-transaction, or it
73413 ** is already in a read-transaction and a read-transaction
73414 ** is requested, this is a no-op.
73416 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
73419 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
73421 if( (p->db->flags & SQLITE_ResetDatabase)
73424 pBt->btsFlags &= ~BTS_READ_ONLY;
73427 /* Write transactions are not possible on a read-only database */
73428 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
73437 ** on this shared-btree structure and a second write transaction is
73440 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
73441 || (pBt->btsFlags & BTS_PENDING)!=0
73443 pBlock = pBt->pWriter->db;
73446 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
73447 if( pIter->pBtree!=p ){
73448 pBlock = pIter->pBtree->db;
73454 sqlite3ConnectionBlocked(p->db, pBlock);
73461 /* Any read-only or read-write transaction implies a read-lock on
73462 ** page 1. So if some other shared-cache client already has a write-lock
73467 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
73468 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
73470 sqlite3PagerWalDb(pPager, p->db);
73475 if( pBt->pPage1==0 && wrflag ){
73476 assert( pBt->inTransaction==TRANS_NONE );
73482 /* Call lockBtree() until either pBt->pPage1 is populated or
73484 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
73485 ** reading page 1 it discovers that the page-size of the database
73486 ** file is not pBt->pageSize. In this case lockBtree() will update
73487 ** pBt->pageSize to the page-size of the file on disk.
73489 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
73492 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
73495 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
73498 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
73511 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
73519 if( p->inTrans==TRANS_NONE ){
73520 pBt->nTransaction++;
73522 if( p->sharable ){
73523 assert( p->lock.pBtree==p && p->lock.iTable==1 );
73524 p->lock.eLock = READ_LOCK;
73525 p->lock.pNext = pBt->pLock;
73526 pBt->pLock = &p->lock;
73530 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
73531 if( p->inTrans>pBt->inTransaction ){
73532 pBt->inTransaction = p->inTrans;
73535 MemPage *pPage1 = pBt->pPage1;
73537 assert( !pBt->pWriter );
73538 pBt->pWriter = p;
73539 pBt->btsFlags &= ~BTS_EXCLUSIVE;
73540 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
73543 /* If the db-size header field is incorrect (as it may be if an old
73546 ** re-read the database size from page 1 if a savepoint or transaction
73549 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
73550 rc = sqlite3PagerWrite(pPage1->pDbPage);
73552 put4byte(&pPage1->aData[28], pBt->nPage);
73561 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
73566 ** the sub-journal is not already open, then it will be opened here.
73568 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
73578 if( p->sharable
73579 || p->inTrans==TRANS_NONE
73580 || (p->inTrans==TRANS_READ && wrflag!=0)
73584 pBt = p->pBt;
73586 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
73591 ** the sub-journal is not already open, then it will be opened here.
73593 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
73602 ** Set the pointer-map entries for all children of page pPage. Also, if
73610 BtShared *pBt = pPage->pBt;
73611 Pgno pgno = pPage->pgno;
73613 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73614 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
73616 nCell = pPage->nCell;
73623 if( !pPage->leaf ){
73629 if( !pPage->leaf ){
73630 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
73642 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
73645 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
73648 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
73652 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73653 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73656 if( get4byte(pPage->aData)!=iFrom ){
73659 put4byte(pPage->aData, iTo);
73665 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
73667 nCell = pPage->nCell;
73673 pPage->xParseCell(pPage, pCell, &info);
73675 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
73678 if( iFrom==get4byte(pCell+info.nSize-4) ){
73679 put4byte(pCell+info.nSize-4, iTo);
73684 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
73696 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
73699 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
73711 ** the journal needs to be sync()ed before database page pDbPage->pgno
73719 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
73724 Pgno iDbPage = pDbPage->pgno;
73725 Pager *pPager = pBt->pPager;
73730 assert( sqlite3_mutex_held(pBt->mutex) );
73731 assert( pDbPage->pBt==pBt );
73737 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
73741 pDbPage->pgno = iFreePage;
73743 /* If pDbPage was a btree-page, then it may have child pages and/or cells
73757 Pgno nextOvfl = get4byte(pDbPage->aData);
73775 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
73793 ** Perform a single step of an incremental-vacuum. If successful, return
73798 ** More specifically, this function attempts to re-organize the database so
73804 ** If the bCommit parameter is non-zero, this function assumes that the
73806 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
73810 Pgno nFreeList; /* Number of pages still on the free-list */
73813 assert( sqlite3_mutex_held(pBt->mutex) );
73820 nFreeList = get4byte(&pBt->pPage1->aData[36]);
73835 /* Remove the page from the files free-list. This is not required
73836 ** if bCommit is non-zero. In that case, the free-list will be
73864 ** looping until a free-page located within the first nFin pages
73897 iLastPg--;
73899 pBt->bDoTruncate = 1;
73900 pBt->nPage = iLastPg;
73906 ** The database opened by the first argument is an auto-vacuum database
73908 ** size of the database in pages following an auto-vacuum operation.
73915 nEntry = pBt->usableSize/5;
73916 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
73917 nFin = nOrig - nFree - nPtrmap;
73919 nFin--;
73922 nFin--;
73929 ** A write-transaction must be opened before calling this function.
73938 BtShared *pBt = p->pBt;
73941 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
73942 if( !pBt->autoVacuum ){
73946 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
73958 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73959 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
73971 ** is committed for an auto-vacuum database.
73981 pBt = p->pBt;
73982 pPager = pBt->pPager;
73985 assert( sqlite3_mutex_held(pBt->mutex) );
73987 assert(pBt->autoVacuum);
73988 if( !pBt->incrVacuum ){
73998 ** is either a pointer-map page or the pending-byte page. If one
74004 nFree = get4byte(&pBt->pPage1->aData[36]);
74005 db = p->db;
74006 if( db->xAutovacPages ){
74008 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
74009 if( db->aDb[iDb].pBt==p ) break;
74011 nVac = db->xAutovacPages(
74012 db->pAutovacPagesArg,
74013 db->aDb[iDb].zDbSName,
74016 pBt->pageSize
74032 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
74036 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74038 put4byte(&pBt->pPage1->aData[32], 0);
74039 put4byte(&pBt->pPage1->aData[36], 0);
74041 put4byte(&pBt->pPage1->aData[28], nFin);
74042 pBt->bDoTruncate = 1;
74043 pBt->nPage = nFin;
74059 ** This routine does the first phase of a two-phase commit. This routine
74071 ** This call is a no-op if no write-transaction is currently active on pBt.
74074 ** the name of a super-journal file that should be written into the
74075 ** individual journal file, or is NULL, indicating no super-journal file
74078 ** When this is called, the super-journal should already have been
74082 ** the write-transaction for this database file is to delete the journal.
74086 if( p->inTrans==TRANS_WRITE ){
74087 BtShared *pBt = p->pBt;
74090 if( pBt->autoVacuum ){
74097 if( pBt->bDoTruncate ){
74098 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
74101 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
74112 BtShared *pBt = p->pBt;
74113 sqlite3 *db = p->db;
74117 pBt->bDoTruncate = 0;
74119 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
74121 ** handle, downgrade to a read-only transaction. The other statements
74124 p->inTrans = TRANS_READ;
74130 if( p->inTrans!=TRANS_NONE ){
74132 pBt->nTransaction--;
74133 if( 0==pBt->nTransaction ){
74134 pBt->inTransaction = TRANS_NONE;
74140 p->inTrans = TRANS_NONE;
74150 ** This routine implements the second phase of a 2-phase commit. The
74162 ** is non-zero then this b-tree transaction is part of a multi-file
74164 ** (by deleting a super-journal file) and the caller will ignore this
74166 ** reset the b-tree objects internal state to indicate that the write
74175 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
74179 /* If the handle has a write-transaction open, commit the shared-btrees
74182 if( p->inTrans==TRANS_WRITE ){
74184 BtShared *pBt = p->pBt;
74185 assert( pBt->inTransaction==TRANS_WRITE );
74186 assert( pBt->nTransaction>0 );
74187 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
74192 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
74193 pBt->inTransaction = TRANS_READ;
74227 ** flag is true, then only write-cursors need be tripped - read-only
74231 ** rolled back modified the database schema. In this case b-tree root
74236 ** saving the current position of a read-only cursor, all cursors,
74237 ** including all read-cursors are tripped.
74249 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
74250 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
74251 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
74260 p->eState = CURSOR_FAULT;
74261 p->skipNext = errCode;
74271 ** Set the pBt->nPage field correctly, according to the current
74272 ** state of the database. Assume pBt->pPage1 is valid.
74275 int nPage = get4byte(&pPage1->aData[28]);
74277 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
74278 testcase( pBt->nPage!=(u32)nPage );
74279 pBt->nPage = nPage;
74295 BtShared *pBt = p->pBt;
74314 if( p->inTrans==TRANS_WRITE ){
74317 assert( TRANS_WRITE==pBt->inTransaction );
74318 rc2 = sqlite3PagerRollback(pBt->pPager);
74323 /* The rollback may have destroyed the pPage1->aData value. So
74325 ** sure pPage1->aData is set correctly. */
74331 pBt->inTransaction = TRANS_READ;
74351 ** A statement sub-transaction is implemented as an anonymous savepoint. The
74353 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
74354 ** are no active savepoints and no other statement-transactions open,
74360 BtShared *pBt = p->pBt;
74362 assert( p->inTrans==TRANS_WRITE );
74363 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74365 assert( iStatement>p->db->nSavepoint );
74366 assert( pBt->inTransaction==TRANS_WRITE );
74372 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
74384 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
74391 if( p && p->inTrans==TRANS_WRITE ){
74392 BtShared *pBt = p->pBt;
74394 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
74400 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
74403 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
74404 pBt->nPage = 0;
74407 btreeSetNPage(pBt, pBt->pPage1);
74409 /* pBt->nPage might be zero if the database was corrupt when
74411 assert( CORRUPT_DB || pBt->nPage>0 );
74420 ** iTable. If a read-only cursor is requested, it is assumed that
74421 ** the caller already has at least a read-only transaction open
74422 ** on the database already. If a write-cursor is requested, then
74439 ** 3: The database must be writable (not on read-only media)
74450 ** operations on this cursor can be no-ops and all READ operations can
74451 ** return a null row (2-bytes: 0x01 0x00).
74454 ** root page of a b-tree. If it is not, then the cursor acquired
74463 int wrFlag, /* 1 to write. 0 read-only */
74467 BtShared *pBt = p->pBt; /* Shared b-tree handle */
74477 ** b-tree database, the connection is holding the required table locks,
74485 assert( p->inTrans>TRANS_NONE );
74486 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
74487 assert( pBt->pPage1 && pBt->pPage1->aData );
74488 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
74501 pCur->pgnoRoot = iTable;
74502 pCur->iPage = -1;
74503 pCur->pKeyInfo = pKeyInfo;
74504 pCur->pBtree = p;
74505 pCur->pBt = pBt;
74506 pCur->curFlags = 0;
74509 for(pX=pBt->pCursor; pX; pX=pX->pNext){
74510 if( pX->pgnoRoot==iTable ){
74511 pX->curFlags |= BTCF_Multiple;
74512 pCur->curFlags = BTCF_Multiple;
74515 pCur->eState = CURSOR_INVALID;
74516 pCur->pNext = pBt->pCursor;
74517 pBt->pCursor = pCur;
74519 pCur->curFlags |= BTCF_WriteFlag;
74520 pCur->curPagerFlags = 0;
74521 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
74523 pCur->curPagerFlags = PAGER_GET_READONLY;
74530 int wrFlag, /* 1 to write. 0 read-only */
74543 int wrFlag, /* 1 to write. 0 read-only */
74547 if( p->sharable ){
74559 ** to users so they cannot do the sizeof() themselves - they must call
74572 ** of run-time by skipping the initialization of those elements.
74583 Btree *pBtree = pCur->pBtree;
74585 BtShared *pBt = pCur->pBt;
74587 assert( pBt->pCursor!=0 );
74588 if( pBt->pCursor==pCur ){
74589 pBt->pCursor = pCur->pNext;
74591 BtCursor *pPrev = pBt->pCursor;
74593 if( pPrev->pNext==pCur ){
74594 pPrev->pNext = pCur->pNext;
74597 pPrev = pPrev->pNext;
74602 sqlite3_free(pCur->aOverflow);
74603 sqlite3_free(pCur->pKey);
74604 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
74607 assert( pBtree->sharable==0 );
74612 pCur->pBtree = 0;
74627 if( a->nKey!=b->nKey ) return 0;
74628 if( a->pPayload!=b->pPayload ) return 0;
74629 if( a->nPayload!=b->nPayload ) return 0;
74630 if( a->nLocal!=b->nLocal ) return 0;
74631 if( a->nSize!=b->nSize ) return 0;
74637 btreeParseCell(pCur->pPage, pCur->ix, &info);
74638 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
74644 if( pCur->info.nSize==0 ){
74645 pCur->curFlags |= BTCF_ValidNKey;
74646 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
74655 ** that is currently pointing to a row in a (non-empty) table.
74659 return pCur && pCur->eState==CURSOR_VALID;
74664 return pCur->eState==CURSOR_VALID;
74675 assert( pCur->eState==CURSOR_VALID );
74676 assert( pCur->curIntKey );
74678 return pCur->info.nKey;
74685 assert( (pCur->curFlags & BTCF_Pinned)==0 );
74686 pCur->curFlags |= BTCF_Pinned;
74689 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
74690 pCur->curFlags &= ~BTCF_Pinned;
74699 assert( pCur->eState==CURSOR_VALID );
74701 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
74702 (i64)(pCur->info.pPayload - pCur->pPage->aData);
74710 ** The caller must guarantee that the cursor is pointing to a non-NULL
74716 assert( pCur->eState==CURSOR_VALID );
74718 return pCur->info.nPayload;
74736 assert( pCur->eState==CURSOR_VALID );
74737 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
74743 ** linked list of overflow pages. If possible, it uses the auto-vacuum
74744 ** pointer-map data instead of reading the content of page ovfl to do so.
74756 ** the pointer-map was used to obtain the value for *pPgnoNext), then
74769 assert( sqlite3_mutex_held(pBt->mutex) );
74774 ** autovacuum pointer-map pages. Guess that the next page in
74779 if( pBt->autoVacuum ){
74803 next = get4byte(pPage->aData);
74831 int eOp, /* 0 -> copy from page, 1 -> copy to page */
74864 ** the overflow page-list cache array (BtCursor.aOverflow).
74868 ** Once an overflow page-list cache has been allocated, it must be
74870 ** the cursor is moved to a different row. Additionally, in auto-vacuum
74871 ** mode, the following events may invalidate an overflow page-list cache.
74882 int eOp /* zero to read. non-zero to write. */
74887 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
74888 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
74895 assert( pCur->eState==CURSOR_VALID );
74896 if( pCur->ix>=pPage->nCell ){
74902 aPayload = pCur->info.pPayload;
74903 assert( offset+amt <= pCur->info.nPayload );
74905 assert( aPayload > pPage->aData );
74906 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
74909 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
74916 if( offset<pCur->info.nLocal ){
74918 if( a+offset>pCur->info.nLocal ){
74919 a = pCur->info.nLocal - offset;
74921 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
74924 amt -= a;
74926 offset -= pCur->info.nLocal;
74931 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
74934 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
74943 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
74944 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
74945 if( pCur->aOverflow==0
74946 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
74949 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
74954 pCur->aOverflow = aNew;
74957 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
74958 pCur->curFlags |= BTCF_ValidOvfl;
74960 /* If the overflow page-list cache has been allocated and the
74964 if( pCur->aOverflow[offset/ovflSize] ){
74966 nextPage = pCur->aOverflow[iIdx];
74973 /* If required, populate the overflow page-list cache. */
74974 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
74975 assert( pCur->aOverflow[iIdx]==0
74976 || pCur->aOverflow[iIdx]==nextPage
74978 pCur->aOverflow[iIdx] = nextPage;
74984 ** page-list cache, if any, then fall back to the getOverflowPage()
74987 assert( pCur->curFlags & BTCF_ValidOvfl );
74988 assert( pCur->pBtree->db==pBt->db );
74989 if( pCur->aOverflow[iIdx+1] ){
74990 nextPage = pCur->aOverflow[iIdx+1];
74994 offset -= ovflSize;
75001 a = ovflSize - offset;
75009 ** 3) there are no dirty pages in the page-cache
75010 ** 4) the database is file-backed, and
75015 ** output buffer, bypassing the page-cache altogether. This speeds
75020 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
75021 && &pBuf[-4]>=pBufStart /* (6) */
75023 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
75025 u8 *aWrite = &pBuf[-4];
75028 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
75029 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
75037 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
75048 amt -= a;
75069 ** pCur can be pointing to either a table or an index b-tree.
75071 ** pCur is pointing to an index b-tree then the key section is read.
75083 assert( pCur->eState==CURSOR_VALID );
75084 assert( pCur->iPage>=0 && pCur->pPage );
75101 if ( pCur->eState==CURSOR_INVALID ){
75109 if( pCur->eState==CURSOR_VALID ){
75121 ** the key if index btrees (pPage->intKey==0) and is the data for
75122 ** table btrees (pPage->intKey==1). The number of bytes of available
75142 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
75143 assert( pCur->eState==CURSOR_VALID );
75144 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75146 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75147 assert( pCur->info.nSize>0 );
75148 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
75149 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
75150 amt = pCur->info.nLocal;
75151 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
75155 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
75158 return (void*)pCur->info.pPayload;
75165 ** b-tree page. Write the number of available bytes into *pAmt.
75185 ** This function returns SQLITE_CORRUPT if the page-header flags field of
75187 ** if an intkey page appears to be the parent of a non-intkey page, or
75188 ** vice-versa).
75193 assert( pCur->eState==CURSOR_VALID );
75194 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
75195 assert( pCur->iPage>=0 );
75196 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
75199 pCur->info.nSize = 0;
75200 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75201 pCur->aiIdx[pCur->iPage] = pCur->ix;
75202 pCur->apPage[pCur->iPage] = pCur->pPage;
75203 pCur->ix = 0;
75204 pCur->iPage++;
75205 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
75206 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
75208 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75210 releasePage(pCur->pPage);
75214 pCur->pPage = pCur->apPage[--pCur->iPage];
75221 ** Page pParent is an internal (non-leaf) tree page. This function
75222 ** asserts that page number iChild is the left-child if the iIdx'th
75224 ** cells in pParent, that page number iChild is the right-child of
75230 assert( iIdx<=pParent->nCell );
75231 if( iIdx==pParent->nCell ){
75232 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
75244 ** pCur->idx is set to the cell index that contains the pointer
75246 ** right-most child page then pCur->idx is set to one more than
75252 assert( pCur->eState==CURSOR_VALID );
75253 assert( pCur->iPage>0 );
75254 assert( pCur->pPage );
75256 pCur->apPage[pCur->iPage-1],
75257 pCur->aiIdx[pCur->iPage-1],
75258 pCur->pPage->pgno
75260 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
75261 pCur->info.nSize = 0;
75262 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75263 pCur->ix = pCur->aiIdx[pCur->iPage-1];
75264 pLeaf = pCur->pPage;
75265 pCur->pPage = pCur->apPage[--pCur->iPage];
75270 ** Move the cursor to point to the root page of its b-tree structure.
75277 ** If the b-tree structure is empty, the cursor state is set to
75283 ** page-header flags indicate that the [virtual] root-page is the expected
75284 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
75286 ** indicating a table b-tree, or if the caller did specify a KeyInfo
75288 ** b-tree).
75298 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
75299 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
75301 if( pCur->iPage>=0 ){
75302 if( pCur->iPage ){
75303 releasePageNotNull(pCur->pPage);
75304 while( --pCur->iPage ){
75305 releasePageNotNull(pCur->apPage[pCur->iPage]);
75307 pRoot = pCur->pPage = pCur->apPage[0];
75310 }else if( pCur->pgnoRoot==0 ){
75311 pCur->eState = CURSOR_INVALID;
75314 assert( pCur->iPage==(-1) );
75315 if( pCur->eState>=CURSOR_REQUIRESEEK ){
75316 if( pCur->eState==CURSOR_FAULT ){
75317 assert( pCur->skipNext!=SQLITE_OK );
75318 return pCur->skipNext;
75322 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
75323 pCur->curPagerFlags);
75325 pCur->eState = CURSOR_INVALID;
75328 pCur->iPage = 0;
75329 pCur->curIntKey = pCur->pPage->intKey;
75331 pRoot = pCur->pPage;
75332 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
75334 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
75335 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
75336 ** NULL, the caller expects a table b-tree. If this is not the case,
75341 ** if pCur->iPage>=0). But this is not so if the database is corrupted
75342 ** in such a way that page pRoot is linked into a second b-tree table
75344 assert( pRoot->intKey==1 || pRoot->intKey==0 );
75345 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
75346 return SQLITE_CORRUPT_PAGE(pCur->pPage);
75350 pCur->ix = 0;
75351 pCur->info.nSize = 0;
75352 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
75354 if( pRoot->nCell>0 ){
75355 pCur->eState = CURSOR_VALID;
75356 }else if( !pRoot->leaf ){
75358 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
75359 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
75360 pCur->eState = CURSOR_VALID;
75363 pCur->eState = CURSOR_INVALID;
75370 ** Move the cursor down to the left-most leaf entry beneath the
75373 ** The left-most leaf is the one with the smallest key - the first
75382 assert( pCur->eState==CURSOR_VALID );
75383 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
75384 assert( pCur->ix<pPage->nCell );
75385 pgno = get4byte(findCell(pPage, pCur->ix));
75392 ** Move the cursor down to the right-most leaf entry beneath the
75395 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
75396 ** finds the right-most entry beneath the *page*.
75398 ** The right-most entry is the one with the largest key - the last
75407 assert( pCur->eState==CURSOR_VALID );
75408 while( !(pPage = pCur->pPage)->leaf ){
75409 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
75410 pCur->ix = pPage->nCell;
75414 pCur->ix = pPage->nCell-1;
75415 assert( pCur->info.nSize==0 );
75416 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
75428 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75431 assert( pCur->pPage->nCell>0 );
75435 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
75449 assert( pCur->eState==CURSOR_VALID );
75453 pCur->curFlags |= BTCF_AtLast;
75455 pCur->curFlags &= ~BTCF_AtLast;
75458 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75466 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75468 /* If the cursor already points to the last entry, this is a no-op. */
75469 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
75472 ** to the last entry in the b-tree. */
75474 for(ii=0; ii<pCur->iPage; ii++){
75475 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
75477 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
75478 testcase( pCur->ix!=pCur->pPage->nCell-1 );
75479 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
75480 assert( pCur->pPage->leaf );
75520 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75522 assert( pCur->pKeyInfo==0 );
75523 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
75527 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
75528 if( pCur->info.nKey==intKey ){
75532 if( pCur->info.nKey<intKey ){
75533 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
75534 *pRes = -1;
75541 if( pCur->info.nKey+1==intKey ){
75546 if( pCur->info.nKey==intKey ){
75557 pCur->pBtree->nSeek++; /* Performance measurement during testing */
75563 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75564 *pRes = -1;
75569 assert( pCur->pPage );
75570 assert( pCur->pPage->isInit );
75571 assert( pCur->eState==CURSOR_VALID );
75572 assert( pCur->pPage->nCell > 0 );
75573 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
75574 assert( pCur->curIntKey );
75579 MemPage *pPage = pCur->pPage;
75582 /* pPage->nCell must be greater than zero. If this is the root-page
75584 ** not run. If this is not the root-page, then the moveToChild() routine
75586 ** be the right kind (index or table) of b-tree page. Otherwise
75588 assert( pPage->nCell>0 );
75589 assert( pPage->intKey );
75591 upr = pPage->nCell-1;
75593 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
75597 if( pPage->intKeyLeaf ){
75599 if( pCell>=pPage->aDataEnd ){
75607 if( lwr>upr ){ c = -1; break; }
75609 upr = idx-1;
75613 pCur->ix = (u16)idx;
75614 if( !pPage->leaf ){
75618 pCur->curFlags |= BTCF_ValidNKey;
75619 pCur->info.nKey = nCellKey;
75620 pCur->info.nSize = 0;
75628 assert( lwr==upr+1 || !pPage->leaf );
75629 assert( pPage->isInit );
75630 if( pPage->leaf ){
75631 assert( pCur->ix<pCur->pPage->nCell );
75632 pCur->ix = (u16)idx;
75638 if( lwr>=pPage->nCell ){
75639 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
75643 pCur->ix = (u16)lwr;
75648 pCur->info.nSize = 0;
75649 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75654 ** Compare the "idx"-th cell on the page the cursor pCur is currently
75675 MemPage *pPage = pCur->pPage;
75681 if( nCell<=pPage->max1bytePayload ){
75682 /* This branch runs if the record-size field of the cell is a
75684 ** b-tree page. */
75685 testcase( pCell+nCell+1==pPage->aDataEnd );
75688 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
75690 /* The record-size field is a 2 byte varint and the record
75691 ** fits entirely on the main b-tree page. */
75692 testcase( pCell+nCell+2==pPage->aDataEnd );
75703 ** Return true (non-zero) if pCur is current pointing to the last
75708 assert( pCur->eState==CURSOR_VALID );
75709 for(i=0; i<pCur->iPage; i++){
75710 MemPage *pPage = pCur->apPage[i];
75711 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
75739 ** The pIdxKey->eqSeen field is set to 1 if there
75751 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75753 assert( pCur->pKeyInfo!=0 );
75756 pCur->pBtree->nSeek++; /* Performance measurement during testing */
75760 pIdxKey->errCode = 0;
75761 assert( pIdxKey->default_rc==1
75762 || pIdxKey->default_rc==0
75763 || pIdxKey->default_rc==-1
75778 if( pCur->eState==CURSOR_VALID
75779 && pCur->pPage->leaf
75783 if( pCur->ix==pCur->pPage->nCell-1
75784 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
75785 && pIdxKey->errCode==SQLITE_OK
75790 if( pCur->iPage>0
75792 && pIdxKey->errCode==SQLITE_OK
75794 pCur->curFlags &= ~BTCF_ValidOvfl;
75795 if( !pCur->pPage->isInit ){
75800 pIdxKey->errCode = SQLITE_OK;
75806 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75807 *pRes = -1;
75814 assert( pCur->pPage );
75815 assert( pCur->pPage->isInit );
75816 assert( pCur->eState==CURSOR_VALID );
75817 assert( pCur->pPage->nCell > 0 );
75818 assert( pCur->curIntKey==0 );
75823 MemPage *pPage = pCur->pPage;
75826 /* pPage->nCell must be greater than zero. If this is the root-page
75828 ** not run. If this is not the root-page, then the moveToChild() routine
75830 ** be the right kind (index or table) of b-tree page. Otherwise
75832 assert( pPage->nCell>0 );
75833 assert( pPage->intKey==0 );
75835 upr = pPage->nCell-1;
75841 /* The maximum supported page-size is 65536 bytes. This means that
75842 ** the maximum number of record bytes stored on an index B-Tree
75843 ** page is less than 16384 bytes and may be stored as a 2-byte
75846 ** stored entirely within the b-tree page by inspecting the first
75850 if( nCell<=pPage->max1bytePayload ){
75851 /* This branch runs if the record-size field of the cell is a
75853 ** b-tree page. */
75854 testcase( pCell+nCell+1==pPage->aDataEnd );
75857 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
75859 /* The record-size field is a 2 byte varint and the record
75860 ** fits entirely on the main b-tree page. */
75861 testcase( pCell+nCell+2==pPage->aDataEnd );
75874 u8 * const pCellBody = pCell - pPage->childPtrSize;
75876 pPage->xParseCell(pPage, pCellBody, &pCur->info);
75877 nCell = (int)pCur->info.nKey;
75882 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
75891 pCur->ix = (u16)idx;
75894 pCur->curFlags &= ~BTCF_ValidOvfl;
75903 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
75904 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
75909 upr = idx-1;
75914 pCur->ix = (u16)idx;
75915 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
75922 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
75923 assert( pPage->isInit );
75924 if( pPage->leaf ){
75925 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75926 pCur->ix = (u16)idx;
75931 if( lwr>=pPage->nCell ){
75932 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
75937 /* This block is similar to an in-lined version of:
75939 ** pCur->ix = (u16)lwr;
75943 pCur->info.nSize = 0;
75944 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75945 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
75948 pCur->aiIdx[pCur->iPage] = (u16)lwr;
75949 pCur->apPage[pCur->iPage] = pCur->pPage;
75950 pCur->ix = 0;
75951 pCur->iPage++;
75952 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
75954 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
75956 releasePage(pCur->pPage);
75960 pCur->pPage = pCur->apPage[--pCur->iPage];
75964 ***** End of in-lined moveToChild() call */
75967 pCur->info.nSize = 0;
75968 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75985 return (CURSOR_VALID!=pCur->eState);
75998 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76003 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
76004 if( NEVER(pCur->pPage->leaf==0) ) return -1;
76006 n = pCur->pPage->nCell;
76007 for(i=0; i<pCur->iPage; i++){
76008 n *= pCur->apPage[i]->nCell;
76039 if( pCur->eState!=CURSOR_VALID ){
76040 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
76045 if( CURSOR_INVALID==pCur->eState ){
76048 if( pCur->eState==CURSOR_SKIPNEXT ){
76049 pCur->eState = CURSOR_VALID;
76050 if( pCur->skipNext>0 ) return SQLITE_OK;
76054 pPage = pCur->pPage;
76055 idx = ++pCur->ix;
76056 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76057 if( !pPage->isInit ){
76061 if( idx>=pPage->nCell ){
76062 if( !pPage->leaf ){
76063 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76068 if( pCur->iPage==0 ){
76069 pCur->eState = CURSOR_INVALID;
76073 pPage = pCur->pPage;
76074 }while( pCur->ix>=pPage->nCell );
76075 if( pPage->intKey ){
76081 if( pPage->leaf ){
76092 pCur->info.nSize = 0;
76093 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76094 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
76095 pPage = pCur->pPage;
76096 if( (++pCur->ix)>=pPage->nCell ){
76097 pCur->ix--;
76100 if( pPage->leaf ){
76132 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
76133 assert( pCur->info.nSize==0 );
76134 if( pCur->eState!=CURSOR_VALID ){
76139 if( CURSOR_INVALID==pCur->eState ){
76142 if( CURSOR_SKIPNEXT==pCur->eState ){
76143 pCur->eState = CURSOR_VALID;
76144 if( pCur->skipNext<0 ) return SQLITE_OK;
76148 pPage = pCur->pPage;
76149 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
76150 if( !pPage->isInit ){
76153 if( !pPage->leaf ){
76154 int idx = pCur->ix;
76159 while( pCur->ix==0 ){
76160 if( pCur->iPage==0 ){
76161 pCur->eState = CURSOR_INVALID;
76166 assert( pCur->info.nSize==0 );
76167 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
76169 pCur->ix--;
76170 pPage = pCur->pPage;
76171 if( pPage->intKey && !pPage->leaf ){
76183 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
76184 pCur->info.nSize = 0;
76185 if( pCur->eState!=CURSOR_VALID
76186 || pCur->ix==0
76187 || pCur->pPage->leaf==0
76191 pCur->ix--;
76212 ** anywhere on the free-list, then it is guaranteed to be returned. If
76232 assert( sqlite3_mutex_held(pBt->mutex) );
76233 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
76234 pPage1 = pBt->pPage1;
76236 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
76238 n = get4byte(&pPage1->aData[36]);
76239 testcase( n==mxPage-1 );
76246 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
76249 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
76250 ** shows that the page 'nearby' is somewhere on the free-list, then
76251 ** the entire-list will be searched for that page.
76258 assert( pBt->autoVacuum );
76270 /* Decrement the free-list count by 1. Set iTrunk to the index of the
76271 ** first free-list trunk page. iPrevTrunk is initially 1.
76273 rc = sqlite3PagerWrite(pPage1->pDbPage);
76275 put4byte(&pPage1->aData[36], n-1);
76278 ** is not true. Otherwise, it runs once for each trunk-page on the
76279 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
76285 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
76288 iTrunk = get4byte(&pPrevTrunk->aData[0]);
76290 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
76293 iTrunk = get4byte(&pPage1->aData[32]);
76297 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
76306 assert( pTrunk->aData!=0 );
76307 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
76309 k = get4byte(&pTrunk->aData[4]);
76315 rc = sqlite3PagerWrite(pTrunk->pDbPage);
76320 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
76323 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
76324 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
76338 rc = sqlite3PagerWrite(pTrunk->pDbPage);
76344 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
76346 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
76350 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
76354 ** pointers to free-list leaves. The first leaf becomes a trunk
76358 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
76368 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
76373 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
76374 put4byte(&pNewTrunk->aData[4], k-1);
76375 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
76378 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
76379 put4byte(&pPage1->aData[32], iNewTrunk);
76381 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
76385 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
76389 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
76395 unsigned char *aData = pTrunk->aData;
76409 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
76411 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
76436 *pPgno, closest+1, k, pTrunk->pgno, n-1));
76437 rc = sqlite3PagerWrite(pTrunk->pDbPage);
76439 if( closest<k-1 ){
76442 put4byte(&aData[4], k-1);
76446 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
76463 ** pager layer with the 'no-content' flag set. This prevents the pager
76465 ** current transaction has already run one or more incremental-vacuum
76468 ** not set the no-content flag. This causes the pager to load and journal
76473 ** file on disk. So the effects of disabling the no-content optimization
76477 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
76479 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
76481 pBt->nPage++;
76482 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
76485 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
76486 /* If *pPgno refers to a pointer-map page, allocate two new pages
76488 ** becomes a new pointer-map page, the second is used by the caller.
76491 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
76492 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
76493 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
76495 rc = sqlite3PagerWrite(pPg->pDbPage);
76499 pBt->nPage++;
76500 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
76503 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
76504 *pPgno = pBt->nPage;
76509 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
76522 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
76523 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
76528 ** This function is used to add page iPage to the database file free-list.
76529 ** It is assumed that the page is not already a part of the free-list.
76540 MemPage *pTrunk = 0; /* Free-list trunk page */
76541 Pgno iTrunk = 0; /* Page number of free-list trunk page */
76542 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
76545 u32 nFree; /* Initial number of pages on free-list */
76547 assert( sqlite3_mutex_held(pBt->mutex) );
76549 assert( !pMemPage || pMemPage->pgno==iPage );
76551 if( iPage<2 || iPage>pBt->nPage ){
76556 sqlite3PagerRef(pPage->pDbPage);
76562 rc = sqlite3PagerWrite(pPage1->pDbPage);
76564 nFree = get4byte(&pPage1->aData[36]);
76565 put4byte(&pPage1->aData[36], nFree+1);
76567 if( pBt->btsFlags & BTS_SECURE_DELETE ){
76572 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
76576 memset(pPage->aData, 0, pPage->pBt->pageSize);
76579 /* If the database supports auto-vacuum, write an entry in the pointer-map
76587 /* Now manipulate the actual database free-list structure. There are two
76588 ** possibilities. If the free-list is currently empty, or if the first
76589 ** trunk page in the free-list is full, then this page will become a
76590 ** new free-list trunk page. Otherwise, it will become a leaf of the
76591 ** first trunk page in the current free-list. This block tests if it
76592 ** is possible to add the page as a new free-list leaf.
76597 iTrunk = get4byte(&pPage1->aData[32]);
76607 nLeaf = get4byte(&pTrunk->aData[4]);
76608 assert( pBt->usableSize>32 );
76609 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
76613 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
76618 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
76621 ** usableSize/4 - 8 entries will be reported as corrupt. In order
76623 ** we will continue to restrict the number of entries to usableSize/4 - 8
76626 ** to read "usableSize/4-2" instead of "usableSize/4-8".
76628 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
76633 rc = sqlite3PagerWrite(pTrunk->pDbPage);
76635 put4byte(&pTrunk->aData[4], nLeaf+1);
76636 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
76637 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
76638 sqlite3PagerDontWrite(pPage->pDbPage);
76642 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
76648 ** the page being freed as a leaf page of the first trunk in the free-list.
76649 ** Possibly because the free-list is empty, or possibly because the
76650 ** first trunk in the free-list is full. Either way, the page being freed
76651 ** will become the new first trunk page in the free-list.
76656 rc = sqlite3PagerWrite(pPage->pDbPage);
76660 put4byte(pPage->aData, iTrunk);
76661 put4byte(&pPage->aData[4], 0);
76662 put4byte(&pPage1->aData[32], iPage);
76663 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
76667 pPage->isInit = 0;
76675 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
76693 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76694 assert( pInfo->nLocal!=pInfo->nPayload );
76695 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
76696 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
76697 if( pCell + pInfo->nSize > pPage->aDataEnd ){
76701 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
76702 pBt = pPage->pBt;
76703 assert( pBt->usableSize > 4 );
76704 ovflPageSize = pBt->usableSize - 4;
76705 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
76707 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
76709 while( nOvfl-- ){
76724 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
76731 ** freePage2() may zero the page contents if secure-delete mode is
76742 sqlite3PagerUnref(pOvfl->pDbPage);
76757 pPage->xParseCell(pPage, pCell, &sInfo); \
76772 ** Note that pCell does not necessary need to point to the pPage->aData
76774 ** be constructed in this temporary area then copied into pPage->aData
76794 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76798 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
76799 || sqlite3PagerIswriteable(pPage->pDbPage) );
76802 nHeader = pPage->childPtrSize;
76803 if( pPage->intKey ){
76804 nPayload = pX->nData + pX->nZero;
76805 pSrc = pX->pData;
76806 nSrc = pX->nData;
76807 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
76809 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
76811 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
76812 nSrc = nPayload = (int)pX->nKey;
76813 pSrc = pX->pKey;
76819 if( nPayload<=pPage->maxLocal ){
76830 memset(pPayload+nSrc, 0, nPayload-nSrc);
76837 mn = pPage->minLocal;
76838 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
76839 testcase( n==pPage->maxLocal );
76840 testcase( n==pPage->maxLocal+1 );
76841 if( n > pPage->maxLocal ) n = mn;
76847 pBt = pPage->pBt;
76864 pPage->xParseCell(pPage, pCell, &info);
76865 assert( nHeader==(int)(info.pPayload - pCell) );
76866 assert( info.nKey==pX->nKey );
76879 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76883 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
76884 || sqlite3PagerIswriteable(pPage->pDbPage) );
76894 nPayload -= n;
76898 nSrc -= n;
76899 spaceLeft -= n;
76903 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
76904 if( pBt->autoVacuum ){
76914 /* If the database supports auto-vacuum, and the second or subsequent
76915 ** overflow page is being allocated, add an entry to the pointer-map
76919 ** to the pointer-map. If we write nothing to this pointer-map slot,
76924 if( pBt->autoVacuum && rc==SQLITE_OK ){
76939 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76943 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
76944 || sqlite3PagerIswriteable(pPage->pDbPage) );
76949 pPrior = pOvfl->aData;
76951 pPayload = &pOvfl->aData[4];
76952 spaceLeft = pBt->usableSize - 4;
76960 ** Remove the i-th cell from pPage. This routine effects pPage only.
76969 u8 *data; /* pPage->aData */
76976 assert( idx<pPage->nCell );
76978 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76979 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76980 assert( pPage->nFree>=0 );
76981 data = pPage->aData;
76982 ptr = &pPage->aCellIdx[2*idx];
76983 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
76985 hdr = pPage->hdrOffset;
76987 testcase( pc+sz==pPage->pBt->usableSize );
76988 if( pc+sz > pPage->pBt->usableSize ){
76997 pPage->nCell--;
76998 if( pPage->nCell==0 ){
77001 put2byte(&data[hdr+5], pPage->pBt->usableSize);
77002 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
77003 - pPage->childPtrSize - 8;
77005 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
77006 put2byte(&data[hdr+3], pPage->nCell);
77007 pPage->nFree += 2;
77018 ** in pPage->apOvfl[] and make it point to the cell content (either
77020 ** Allocating a new entry in pPage->aCell[] implies that
77021 ** pPage->nOverflow is incremented.
77033 int i, /* New cell becomes the i-th cell of the page */
77037 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
77042 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77044 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77045 assert( MX_CELL(pPage->pBt)<=10921 );
77046 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77047 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77048 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77049 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77050 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77051 assert( pPage->nFree>=0 );
77053 if( pPage->nOverflow || sz+2>pPage->nFree ){
77059 j = pPage->nOverflow++;
77060 /* Comparison against ArraySize-1 since we hold back one extra slot
77063 assert( j < ArraySize(pPage->apOvfl)-1 );
77064 pPage->apOvfl[j] = pCell;
77065 pPage->aiOvfl[j] = (u16)i;
77072 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77073 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77075 int rc = sqlite3PagerWrite(pPage->pDbPage);
77079 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77080 data = pPage->aData;
77081 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77087 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77088 assert( idx+sz <= (int)pPage->pBt->usableSize );
77089 pPage->nFree -= (u16)(2 + sz);
77095 memcpy(&data[idx+4], pCell+4, sz-4);
77097 pIns = pPage->aCellIdx + i*2;
77098 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77100 pPage->nCell++;
77102 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77103 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77105 if( pPage->pBt->autoVacuum ){
77130 int i, /* New cell becomes the i-th cell of the page */
77137 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
77139 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
77140 assert( MX_CELL(pPage->pBt)<=10921 );
77141 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
77142 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
77143 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
77144 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77145 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
77146 assert( pPage->nFree>=0 );
77147 assert( pPage->nOverflow==0 );
77148 if( sz+2>pPage->nFree ){
77149 j = pPage->nOverflow++;
77150 /* Comparison against ArraySize-1 since we hold back one extra slot
77153 assert( j < ArraySize(pPage->apOvfl)-1 );
77154 pPage->apOvfl[j] = pCell;
77155 pPage->aiOvfl[j] = (u16)i;
77162 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
77163 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
77165 int rc = sqlite3PagerWrite(pPage->pDbPage);
77169 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
77170 data = pPage->aData;
77171 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
77177 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
77178 assert( idx+sz <= (int)pPage->pBt->usableSize );
77179 pPage->nFree -= (u16)(2 + sz);
77181 pIns = pPage->aCellIdx + i*2;
77182 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
77184 pPage->nCell++;
77186 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
77187 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
77189 if( pPage->pBt->autoVacuum ){
77215 ** tunable - as if you could change them and recompile and it would all work.
77230 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
77242 ** -----------
77244 ** -----------
77247 ** --------- --------- ---------
77248 ** |Child-1| |Child-2| |Child-3|
77249 ** --------- --------- ---------
77253 ** 1. All cells from Child-1 in order
77255 ** 3. All cells from Child-2 in order
77257 ** 5. All cells from Child-3 in order
77259 ** For a table-btree (with rowids) the items 2 and 4 are empty because
77263 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
77267 ** ixNx[0] = Number of cells in Child-1.
77268 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
77269 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
77270 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
77273 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
77276 ** ixNx[0] = Number of cells in Child-1.
77277 ** ixNx[1] = Number of cells in Child-1 and Child-2.
77296 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
77300 MemPage *pRef = p->pRef;
77301 u16 *szCell = p->szCell;
77302 assert( idx>=0 && idx+N<=p->nCell );
77304 assert( p->apCell[idx]!=0 );
77306 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
77309 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
77312 N--;
77320 assert( N>=0 && N<p->nCell );
77321 assert( p->szCell[N]==0 );
77322 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
77323 return p->szCell[N];
77326 assert( N>=0 && N<p->nCell );
77327 if( p->szCell[N] ) return p->szCell[N];
77332 ** Array apCell[] contains pointers to nCell b-tree page cells. The
77350 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
77351 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
77352 const int usableSize = pPg->pBt->usableSize;
77357 u8 *pCellptr = pPg->aCellIdx;
77358 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
77360 int k; /* Current slot in pCArray->apEnd[] */
77361 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
77367 memcpy(&pTmp[j], &aData[j], usableSize - j);
77369 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i; k++){}
77370 pSrcEnd = pCArray->apEnd[k];
77374 u8 *pCell = pCArray->apCell[i];
77375 u16 sz = pCArray->szCell[i];
77379 pCell = &pTmp[pCell - aData];
77386 pData -= sz;
77387 put2byte(pCellptr, (pData - aData));
77391 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
77394 if( pCArray->ixNx[k]<=i ){
77396 pSrcEnd = pCArray->apEnd[k];
77400 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
77401 pPg->nCell = nCell;
77402 pPg->nOverflow = 0;
77405 put2byte(&aData[hdr+3], pPg->nCell);
77406 put2byte(&aData[hdr+5], pData - aData);
77412 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
77415 ** will fit), non-zero is returned. Otherwise, if the cells are added
77418 ** Argument pCellptr points to the first entry in the cell-pointer array
77420 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
77422 ** that it is safe to overwrite this part of the cell-pointer array.
77430 ** end of the space required by this page for the cell-pointer area (for
77431 ** all cells - not just those inserted by the current call). If the content
77433 ** cells in apCell[], then the cells do not fit and non-zero is returned.
77437 u8 *pBegin, /* End of cell-pointer array */
77438 u8 **ppData, /* IN/OUT: Page content-area pointer */
77439 u8 *pCellptr, /* Pointer to cell-pointer area */
77444 int i = iFirst; /* Loop counter - cell index to insert */
77445 u8 *aData = pPg->aData; /* Complete page */
77448 int k; /* Current slot in pCArray->apEnd[] */
77450 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
77452 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i ; k++){}
77453 pEnd = pCArray->apEnd[k];
77457 assert( pCArray->szCell[i]!=0 );
77458 sz = pCArray->szCell[i];
77460 if( (pData - pBegin)<sz ) return 1;
77461 pData -= sz;
77464 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
77467 assert( (pSlot+sz)<=pCArray->apCell[i]
77468 || pSlot>=(pCArray->apCell[i]+sz)
77470 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
77471 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
77477 memmove(pSlot, pCArray->apCell[i], sz);
77478 put2byte(pCellptr, (pSlot - aData));
77482 if( pCArray->ixNx[k]<=i ){
77484 pEnd = pCArray->apEnd[k];
77492 ** The pCArray object contains pointers to b-tree cells and their sizes.
77495 ** that is currently stored within the body of pPg to the pPg free-list.
77496 ** The cell-pointers and other fields of the page are not updated.
77498 ** This function returns the total number of cells added to the free-list.
77506 u8 * const aData = pPg->aData;
77507 u8 * const pEnd = &aData[pPg->pBt->usableSize];
77508 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
77517 u8 *pCell = pCArray->apCell[i];
77525 sz = pCArray->szCell[i]; assert( sz>0 );
77526 iOfst = (u16)(pCell - aData);
77540 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
77553 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
77560 ** balanced. The current page, pPg, has pPg->nCell cells starting with
77561 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
77567 ** The pPg->nFree field is invalid when this function returns. It is the
77577 u8 * const aData = pPg->aData;
77578 const int hdr = pPg->hdrOffset;
77579 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
77580 int nCell = pPg->nCell; /* Cells stored on pPg */
77584 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
77588 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
77589 memcpy(pTmp, aData, pPg->pBt->usableSize);
77595 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
77597 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
77598 nCell -= nShift;
77601 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
77603 nCell -= nTail;
77608 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
77612 int nAdd = MIN(nNew,iOld-iNew);
77613 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
77615 pCellptr = pPg->aCellIdx;
77625 for(i=0; i<pPg->nOverflow; i++){
77626 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
77628 pCellptr = &pPg->aCellIdx[iCell * 2];
77630 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
77643 pCellptr = &pPg->aCellIdx[nCell*2];
77646 iNew+nCell, nNew-nCell, pCArray
77649 pPg->nCell = nNew;
77650 pPg->nOverflow = 0;
77652 put2byte(&aData[hdr+3], pPg->nCell);
77653 put2byte(&aData[hdr+5], pData - aData);
77657 u8 *pCell = pCArray->apCell[i+iNew];
77658 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
77659 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
77660 pCell = &pTmp[pCell - aData];
77663 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
77679 ** a new entry is being inserted on the extreme right-end of the
77683 ** Instead of trying to balance the 3 right-most leaf pages, just add
77684 ** a new page to the right-hand side and put the one new entry in
77690 ** pPage is the leaf page which is the right-most page in the tree.
77692 ** which is also the right-most entry on the page.
77701 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
77706 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
77707 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77708 assert( pPage->nOverflow==1 );
77710 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
77711 assert( pPage->nFree>=0 );
77712 assert( pParent->nFree>=0 );
77714 /* Allocate a new page. This page will become the right-sibling of
77723 u8 *pCell = pPage->apOvfl[0];
77724 u16 szCell = pPage->xCellSize(pPage, pCell);
77728 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
77729 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
77735 b.apEnd[0] = pPage->aDataEnd;
77742 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
77744 /* If this is an auto-vacuum database, update the pointer map
77754 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
77755 if( szCell>pNew->minLocal ){
77761 ** consists of a 4-byte page number (the page number of pPage) and
77765 ** To find the largest key value on pPage, first find the right-most
77767 ** record-length (a variable length integer at most 32-bits in size)
77769 ** The first of the while(...) loops below skips over the record-length
77773 pCell = findCell(pPage, pPage->nCell-1);
77781 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
77782 0, pPage->pgno);
77785 /* Set the right-child pointer of pParent to point to the new page. */
77786 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
77800 ** for setting pointer-map entries.
77808 BtShared *pBt = pPage->pBt;
77809 assert( pPage->isInit );
77811 for(j=0; j<pPage->nCell; j++){
77816 pPage->xParseCell(pPage, z, &info);
77818 Pgno ovfl = get4byte(&z[info.nSize-4]);
77820 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
77822 if( !pPage->leaf ){
77825 assert( n==pPage->pgno && e==PTRMAP_BTREE );
77828 if( !pPage->leaf ){
77829 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
77831 assert( n==pPage->pgno && e==PTRMAP_BTREE );
77839 ** This function is used to copy the contents of the b-tree node stored
77841 ** the pointer-map entries for each child page are updated so that the
77857 BtShared * const pBt = pFrom->pBt;
77858 u8 * const aFrom = pFrom->aData;
77859 u8 * const aTo = pTo->aData;
77860 int const iFromHdr = pFrom->hdrOffset;
77861 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
77866 assert( pFrom->isInit );
77867 assert( pFrom->nFree>=iToHdr );
77868 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
77870 /* Copy the b-tree node content from page pFrom to page pTo. */
77872 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
77873 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
77880 pTo->isInit = 0;
77888 /* If this is an auto-vacuum database, update the pointer-map entries
77889 ** for any b-tree or overflow pages that pTo now contains the pointers to.
77931 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
77940 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
77941 int isRoot, /* True if pParent is a root-page */
77949 int nxDiv; /* Next divider slot in pParent->aCell[] */
77954 int pageFlags; /* Value of pPage->aData[0] */
77960 u8 *pRight; /* Location in parent of right-sibling pointer */
77961 u8 *apDiv[NB-1]; /* Divider cells in pParent */
77962 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
77964 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
77973 pBt = pParent->pBt;
77974 assert( sqlite3_mutex_held(pBt->mutex) );
77975 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77982 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
77983 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
77988 assert( pParent->nFree>=0 );
78001 i = pParent->nOverflow + pParent->nCell;
78009 nxDiv = i-2+bBulk;
78011 nxDiv = iParentIdx-1;
78013 i = 2-bBulk;
78016 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
78017 pRight = &pParent->aData[pParent->hdrOffset+8];
78019 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
78030 if( apOld[i]->nFree<0 ){
78037 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
78038 if( (i--)==0 ) break;
78040 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
78041 apDiv[i] = pParent->apOvfl[0];
78043 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78044 pParent->nOverflow = 0;
78046 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
78048 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
78057 ** But not if we are in secure-delete mode. In secure-delete mode,
78062 if( pBt->btsFlags & BTS_FAST_SECURE ){
78067 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
78068 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
78070 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
78073 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
78077 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
78087 + pBt->pageSize; /* aSpace1 */
78089 assert( szScratch<=7*(int)pBt->pageSize );
78116 leafCorrection = b.pRef->leaf*4;
78117 leafData = b.pRef->intKeyLeaf;
78120 int limit = pOld->nCell;
78121 u8 *aData = pOld->aData;
78122 u16 maskPage = pOld->maskPage;
78123 u8 *piCell = aData + pOld->cellOffset;
78127 /* Verify that all sibling pages are of the same "type" (table-leaf,
78128 ** table-interior, index-leaf, or index-interior).
78130 if( pOld->aData[0]!=apOld[0]->aData[0] ){
78152 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
78153 if( pOld->nOverflow>0 ){
78154 if( NEVER(limit<pOld->aiOvfl[0]) ){
78158 limit = pOld->aiOvfl[0];
78164 for(k=0; k<pOld->nOverflow; k++){
78165 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
78166 b.apCell[b.nCell] = pOld->apOvfl[k];
78170 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
78177 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
78180 if( i<nOld-1 && !leafData){
78187 assert( sz<=pBt->maxLocal+23 );
78188 assert( iSpace1 <= (int)pBt->pageSize );
78192 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
78193 if( !pOld->leaf ){
78195 assert( pOld->hdrOffset==0 || CORRUPT_DB );
78198 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
78205 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
78217 ** size of all cells on the i-th page and cntNew[] which is the index
78224 ** szNew[i]: Spaced used on the i-th sibling page.
78226 ** the right of the i-th sibling page.
78230 usableSpace = pBt->usableSize - 12 + leafCorrection;
78233 b.apEnd[k] = p->aDataEnd;
78235 if( k && b.ixNx[k]==b.ixNx[k-1] ){
78236 k--; /* Omit b.ixNx[] entry for child pages with no cells */
78240 b.apEnd[k] = pParent->aDataEnd;
78243 assert( p->nFree>=0 );
78244 szNew[i] = usableSpace - p->nFree;
78245 for(j=0; j<p->nOverflow; j++){
78246 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
78257 szNew[k-1] = 0;
78258 cntNew[k-1] = b.nCell;
78260 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
78261 szNew[i] -= sz;
78270 cntNew[i]--;
78284 szNew[i+1] -= sz;
78288 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
78297 ** always nearly full, while the right-most sibling might be nearly empty.
78302 ** be so out of balance as to be illegal. For example, the right-most
78305 for(i=k-1; i>0; i--){
78307 int szLeft = szNew[i-1]; /* Size of sibling on the left */
78308 int r; /* Index of right-most cell in left sibling */
78311 r = cntNew[i-1] - 1;
78312 d = r + 1 - leafData;
78321 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
78325 szLeft -= szR + 2;
78326 cntNew[i-1] = r;
78327 r--;
78328 d--;
78331 szNew[i-1] = szLeft;
78332 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
78338 /* Sanity check: For a non-corrupt database file one of the following
78345 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
78347 apOld[0]->pgno, apOld[0]->nCell,
78348 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
78349 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
78355 pageFlags = apOld[0]->aData[0];
78361 rc = sqlite3PagerWrite(pNew->pDbPage);
78363 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
78378 /* Set the pointer-map entry for the new sibling page. */
78380 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
78401 aPgno[i] = apNew[i]->pgno;
78402 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
78403 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
78405 for(i=0; i<nNew-1; i++){
78408 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
78417 Pgno pgnoA = apNew[i]->pgno;
78418 Pgno pgnoB = apNew[iB]->pgno;
78419 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
78420 u16 fgA = apNew[i]->pDbPage->flags;
78421 u16 fgB = apNew[iB]->pDbPage->flags;
78422 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
78423 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
78424 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
78425 apNew[i]->pgno = pgnoB;
78426 apNew[iB]->pgno = pgnoA;
78432 apNew[0]->pgno, szNew[0], cntNew[0],
78433 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
78434 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
78435 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
78436 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
78437 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
78438 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
78439 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
78440 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
78443 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78445 assert( apNew[nNew-1]!=0 );
78446 put4byte(pRight, apNew[nNew-1]->pgno);
78448 /* If the sibling pages are not leaves, ensure that the right-child pointer
78449 ** of the right-most new sibling page is set to the value that was
78450 ** originally in the same field of the right-most old sibling page. */
78452 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
78453 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
78468 ** associated with the right-child of each sibling may also need to be
78475 int cntOldNext = pNew->nCell + pNew->nOverflow;
78486 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
78500 || pNew->pgno!=aPgno[iOld]
78501 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
78504 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
78506 if( cachedCellSize(&b,i)>pNew->minLocal ){
78515 for(i=0; i<nNew-1; i++){
78528 if( !pNew->leaf ){
78529 memcpy(&pNew->aData[8], pCell, 4);
78531 /* If the tree is a leaf-data tree, and the siblings are leaves,
78533 ** cell consists of the integer key for the right-most cell of
78534 ** the sibling-page assembled above only.
78537 j--;
78538 pNew->xParseCell(pNew, b.apCell[j], &info);
78543 pCell -= 4;
78544 /* Obscure case for non-leaf-data trees: If the cell at pCell was
78551 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
78557 sz = pParent->xCellSize(pParent, pCell);
78561 assert( sz<=pBt->maxLocal+23 );
78562 assert( iOvflSpace <= (int)pBt->pageSize );
78569 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
78571 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
78578 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
78580 ** the left-hand sibling apNew[iPg-1] has been updated.
78584 ** the right-hand sibling apNew[iPg+1] has been updated.
78588 ** The iPg value in the following loop starts at nNew-1 goes down
78589 ** to 0, then back up to nNew-1 again, thus making two passes over
78596 for(i=1-nNew; i<nNew; i++){
78597 int iPg = i<0 ? -i : i;
78603 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
78610 ** only after iPg-1 has already been updated. */
78611 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
78621 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
78622 iNew = cntNew[iPg-1] + !leafData;
78623 nNewCell = cntNew[iPg] - iNew;
78629 apNew[iPg]->nFree = usableSpace-szNew[iPg];
78630 assert( apNew[iPg]->nOverflow==0 );
78631 assert( apNew[iPg]->nCell==nNewCell );
78641 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
78642 /* The root page of the b-tree now contains no cells. The only sibling
78643 ** page is the right-child of the parent. Copy the contents of the
78645 ** b-tree structure by one. This is described as the "balance-shallower"
78646 ** sub-algorithm in some documentation.
78648 ** If this is an auto-vacuum database, the call to copyNodeContent()
78649 ** sets all pointer-map entries corresponding to database image pages
78658 rc = defragmentPage(apNew[0], -1);
78660 assert( apNew[0]->nFree ==
78661 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
78662 - apNew[0]->nCell*2)
78668 /* Fix the pointer map entries associated with the right-child of each
78672 u32 key = get4byte(&apNew[i]->aData[8]);
78673 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
78677 assert( pParent->isInit );
78688 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
78715 ** This function is called when the root page of a b-tree structure is
78720 ** page is then overwritten to make it an empty page with the right-child
78723 ** Before returning, all pointer-map entries corresponding to pages
78724 ** that the new child-page now contains pointers to are updated. The
78725 ** entry corresponding to the new right-child pointer of the root
78737 BtShared *pBt = pRoot->pBt; /* The BTree */
78739 assert( pRoot->nOverflow>0 );
78740 assert( sqlite3_mutex_held(pBt->mutex) );
78742 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
78743 ** page that will become the new right-child of pPage. Copy the contents
78746 rc = sqlite3PagerWrite(pRoot->pDbPage);
78748 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
78751 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
78759 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
78760 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
78761 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
78763 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
78766 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
78767 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
78768 memcpy(pChild->apOvfl, pRoot->apOvfl,
78769 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
78770 pChild->nOverflow = pRoot->nOverflow;
78772 /* Zero the contents of pRoot. Then install pChild as the right-child. */
78773 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
78774 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
78782 ** on the same B-tree as pCur.
78785 ** pointing to the same b-tree. If an insert occurs on one SQL table
78787 ** table linked to the same b-tree. If the secondary insert causes a
78793 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
78795 && pOther->eState==CURSOR_VALID
78796 && pOther->pPage==pCur->pPage
78824 MemPage *pPage = pCur->pPage;
78826 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
78827 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
78833 }else if( (iPage = pCur->iPage)==0 ){
78834 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
78835 /* The root page of the b-tree is overfull. In this case call the
78836 ** balance_deeper() function to create a new child for the root-page
78837 ** and copy the current contents of the root-page to it. The
78838 ** next iteration of the do-loop will balance the child page.
78842 rc = balance_deeper(pPage, &pCur->apPage[1]);
78844 pCur->iPage = 1;
78845 pCur->ix = 0;
78846 pCur->aiIdx[0] = 0;
78847 pCur->apPage[0] = pPage;
78848 pCur->pPage = pCur->apPage[1];
78849 assert( pCur->pPage->nOverflow );
78854 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
78860 MemPage * const pParent = pCur->apPage[iPage-1];
78861 int const iIdx = pCur->aiIdx[iPage-1];
78863 rc = sqlite3PagerWrite(pParent->pDbPage);
78864 if( rc==SQLITE_OK && pParent->nFree<0 ){
78869 if( pPage->intKeyLeaf
78870 && pPage->nOverflow==1
78871 && pPage->aiOvfl[0]==pPage->nCell
78872 && pParent->pgno!=1
78873 && pParent->nCell==iIdx
78878 ** happens, the next iteration of the do-loop will balance pParent
78897 ** become overfull or underfull. The next iteration of the do-loop
78902 ** A subsequent iteration of the do-loop will deal with this by
78904 ** but it doesn't deal with overflow cells - just moves them to a
78911 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
78913 pCur->hints&BTREE_BULKLOAD);
78929 pPage->nOverflow = 0;
78931 /* The next iteration of the do-loop balances the parent page. */
78933 pCur->iPage--;
78934 assert( pCur->iPage>=0 );
78935 pCur->pPage = pCur->apPage[pCur->iPage];
78955 int nData = pX->nData - iOffset;
78961 int rc = sqlite3PagerWrite(pPage->pDbPage);
78963 memset(pDest + i, 0, iAmt - i);
78970 iAmt-nData);
78974 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
78975 int rc = sqlite3PagerWrite(pPage->pDbPage);
78981 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
78996 int iOffset; /* Next byte of pX->pData to write */
78997 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
78999 MemPage *pPage = pCur->pPage; /* Page being written */
79004 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
79007 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79008 0, pCur->info.nLocal);
79012 iOffset = pCur->info.nLocal;
79015 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
79016 pBt = pPage->pBt;
79017 ovflPageSize = pBt->usableSize - 4;
79021 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
79025 ovflPgno = get4byte(pPage->aData);
79027 ovflPageSize = nTotal - iOffset;
79029 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
79032 sqlite3PagerUnref(pPage->pDbPage);
79044 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
79045 MemPage *pPage = pCur->pPage; /* Page being written */
79047 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
79048 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
79052 if( pCur->info.nLocal==nTotal ){
79054 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
79055 0, pCur->info.nLocal);
79078 ** If the seekResult parameter is non-zero, then a successful call to
79089 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
79090 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
79100 int loc = seekResult; /* -1: before desired location +1: after */
79104 Btree *p = pCur->pBtree;
79109 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
79113 ** In some cases, the call to btreeMoveto() below is a no-op. For
79114 ** example, when inserting data into a table with auto-generated integer
79117 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
79122 if( pCur->curFlags & BTCF_Multiple ){
79123 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
79125 if( loc && pCur->iPage<0 ){
79138 if( pCur->eState>=CURSOR_REQUIRESEEK ){
79139 testcase( pCur->eState==CURSOR_REQUIRESEEK );
79140 testcase( pCur->eState==CURSOR_FAULT );
79146 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
79147 && p->pBt->inTransaction==TRANS_WRITE
79148 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
79149 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
79152 ** expecting an index b-tree, then the caller should be inserting blob
79156 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
79158 if( pCur->pKeyInfo==0 ){
79159 assert( pX->pKey==0 );
79160 /* If this is an insert into a table b-tree, invalidate any incrblob
79162 if( p->hasIncrblobCur ){
79163 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
79171 assert( pCur->curFlags & BTCF_ValidNKey );
79172 assert( pX->nKey==pCur->info.nKey );
79181 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
79184 assert( pX->nData>=0 && pX->nZero>=0 );
79185 if( pCur->info.nSize!=0
79186 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
79197 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
79215 if( pX->nMem ){
79217 r.pKeyInfo = pCur->pKeyInfo;
79218 r.aMem = pX->aMem;
79219 r.nField = pX->nMem;
79224 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
79236 if( pCur->info.nKey==pX->nKey ){
79238 x2.pData = pX->pKey;
79239 x2.nData = pX->nKey;
79245 assert( pCur->eState==CURSOR_VALID
79246 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
79248 pPage = pCur->pPage;
79249 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
79250 assert( pPage->leaf || !pPage->intKey );
79251 if( pPage->nFree<0 ){
79252 if( NEVER(pCur->eState>CURSOR_INVALID) ){
79253 /* ^^^^^--- due to the moveToRoot() call above */
79262 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
79264 assert( pPage->isInit || CORRUPT_DB );
79265 newCell = p->pBt->pTmpSpace;
79270 szNew = p->pBt->nPreformatSize;
79272 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
79274 pPage->xParseCell(pPage, newCell, &info);
79276 Pgno ovfl = get4byte(&newCell[szNew-4]);
79277 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
79285 assert( szNew==pPage->xCellSize(pPage, newCell) );
79286 assert( szNew <= MX_CELL_SIZE(p->pBt) );
79287 idx = pCur->ix;
79288 pCur->info.nSize = 0;
79292 if( idx>=pPage->nCell ){
79295 rc = sqlite3PagerWrite(pPage->pDbPage);
79300 if( !pPage->leaf ){
79304 testcase( pCur->curFlags & BTCF_ValidOvfl );
79307 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
79317 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
79319 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
79322 if( oldCell+szNew > pPage->aDataEnd ){
79330 }else if( loc<0 && pPage->nCell>0 ){
79331 assert( pPage->leaf );
79332 idx = ++pCur->ix;
79333 pCur->curFlags &= ~BTCF_ValidNKey;
79335 assert( pPage->leaf );
79338 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
79339 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
79353 ** multiple records into an intkey b-tree using a single cursor (as can
79356 ** the b-tree if possible. If the cursor is left pointing to the last
79361 if( pPage->nOverflow ){
79363 pCur->curFlags &= ~(BTCF_ValidNKey);
79370 pCur->pPage->nOverflow = 0;
79371 pCur->eState = CURSOR_INVALID;
79374 if( pCur->pKeyInfo ){
79375 assert( pCur->pKey==0 );
79376 pCur->pKey = sqlite3Malloc( pX->nKey );
79377 if( pCur->pKey==0 ){
79380 memcpy(pCur->pKey, pX->pKey, pX->nKey);
79383 pCur->eState = CURSOR_REQUIRESEEK;
79384 pCur->nKey = pX->nKey;
79387 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
79409 BtShared *pBt = pDest->pBt;
79410 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
79416 if( pSrc->info.nPayload<0x80 ){
79417 *(aOut++) = pSrc->info.nPayload;
79419 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
79421 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
79422 nIn = pSrc->info.nLocal;
79423 aIn = pSrc->info.pPayload;
79424 if( aIn+nIn>pSrc->pPage->aDataEnd ){
79427 nRem = pSrc->info.nPayload;
79428 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
79430 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
79434 Pager *pSrcPager = pSrc->pBt->pPager;
79441 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
79442 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
79443 if( nOut<pSrc->info.nPayload ){
79445 pBt->nPreformatSize += 4;
79449 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
79452 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
79456 nRem -= nOut;
79462 nOut -= nCopy;
79463 nIn -= nCopy;
79475 nIn = pSrc->pBt->usableSize - 4;
79486 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
79491 pPgnoOut = pPageOut->aData;
79494 nOut = MIN(pBt->usableSize - 4, nRem);
79518 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
79523 Btree *p = pCur->pBtree;
79524 BtShared *pBt = p->pBt;
79534 assert( pBt->inTransaction==TRANS_WRITE );
79535 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
79536 assert( pCur->curFlags & BTCF_WriteFlag );
79537 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
79538 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
79540 if( pCur->eState!=CURSOR_VALID ){
79541 if( pCur->eState>=CURSOR_REQUIRESEEK ){
79543 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
79544 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
79549 assert( pCur->eState==CURSOR_VALID );
79551 iCellDepth = pCur->iPage;
79552 iCellIdx = pCur->ix;
79553 pPage = pCur->pPage;
79554 if( pPage->nCell<=iCellIdx ){
79558 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
79561 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
79567 ** will cause a b-tree rebalance, then this is done by saving the cursor
79583 if( !pPage->leaf
79584 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
79585 (int)(pBt->usableSize*2/3)
79586 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
79588 /* A b-tree rebalance will be required after deleting this entry.
79602 ** sub-tree headed by the child page of the cell being deleted. This makes
79604 if( !pPage->leaf ){
79612 if( pCur->curFlags & BTCF_Multiple ){
79613 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
79617 /* If this is a delete operation to remove a row from a table b-tree,
79619 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
79620 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
79626 rc = sqlite3PagerWrite(pPage->pDbPage);
79633 ** is currently pointing to the largest entry in the sub-tree headed
79634 ** by the child-page of the cell that was just deleted from an internal
79637 if( !pPage->leaf ){
79638 MemPage *pLeaf = pCur->pPage;
79643 if( pLeaf->nFree<0 ){
79647 if( iCellDepth<pCur->iPage-1 ){
79648 n = pCur->apPage[iCellDepth+1]->pgno;
79650 n = pCur->pPage->pgno;
79652 pCell = findCell(pLeaf, pLeaf->nCell-1);
79653 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
79654 nCell = pLeaf->xCellSize(pLeaf, pCell);
79656 pTmp = pBt->pTmpSpace;
79658 rc = sqlite3PagerWrite(pLeaf->pDbPage);
79660 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
79662 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
79681 assert( pCur->pPage->nOverflow==0 );
79682 assert( pCur->pPage->nFree>=0 );
79683 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
79685 ** then balance() will always be a no-op. No need to invoke it. */
79690 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
79691 releasePageNotNull(pCur->pPage);
79692 pCur->iPage--;
79693 while( pCur->iPage>iCellDepth ){
79694 releasePage(pCur->apPage[pCur->iPage--]);
79696 pCur->pPage = pCur->apPage[pCur->iPage];
79702 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
79703 assert( pPage==pCur->pPage || CORRUPT_DB );
79704 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
79705 pCur->eState = CURSOR_SKIPNEXT;
79706 if( iCellIdx>=pPage->nCell ){
79707 pCur->skipNext = -1;
79708 pCur->ix = pPage->nCell-1;
79710 pCur->skipNext = 1;
79716 pCur->eState = CURSOR_REQUIRESEEK;
79736 BtShared *pBt = p->pBt;
79740 int ptfFlags; /* Page-type flags for the root page of new table */
79743 assert( pBt->inTransaction==TRANS_WRITE );
79744 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
79752 if( pBt->autoVacuum ){
79753 Pgno pgnoMove; /* Move a page here to make room for the root-page */
79758 ** out to be an overflow page, delete all overflow page-map caches
79764 ** root page of the new table should go. meta[3] is the largest root-page
79765 ** created so far, so the new root-page is (meta[3]+1).
79773 /* The new root-page may not be allocated on a pointer-map page, or the
79792 /* pgnoRoot is the page that will be used for the root-page of
79836 rc = sqlite3PagerWrite(pRoot->pDbPage);
79845 /* Update the pointer-map and meta-data with the new root-page number. */
79856 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
79868 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
79875 sqlite3PagerUnref(pRoot->pDbPage);
79876 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
79905 assert( sqlite3_mutex_held(pBt->mutex) );
79911 if( (pBt->openFlags & BTREE_SINGLE)==0
79912 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
79917 hdr = pPage->hdrOffset;
79918 for(i=0; i<pPage->nCell; i++){
79920 if( !pPage->leaf ){
79927 if( !pPage->leaf ){
79928 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
79930 if( pPage->intKey ) pnChange = 0;
79933 testcase( !pPage->intKey );
79934 *pnChange += pPage->nCell;
79938 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
79939 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
79961 BtShared *pBt = p->pBt;
79963 assert( p->inTrans==TRANS_WRITE );
79969 ** is the root of a table b-tree - if it is not, the following call is
79970 ** a no-op). */
79971 if( p->hasIncrblobCur ){
79986 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
80012 BtShared *pBt = p->pBt;
80015 assert( p->inTrans==TRANS_WRITE );
80035 if( pBt->autoVacuum ){
80040 /* If the table being dropped is the table with the largest root-page
80049 /* The table being dropped does not have the largest root-page
80051 ** gap left by the deleted root-page.
80074 /* Set the new 'max-root-page' value in the database header. This
80076 ** be a root-page number, less one again if that is the
80079 maxRootPgno--;
80082 maxRootPgno--;
80104 ** This function may only be called if the b-tree connection already
80107 ** Read the meta-information out of a database file. Meta[0]
80110 ** is read-only, the others are read/write.
80124 BtShared *pBt = p->pBt;
80127 assert( p->inTrans>TRANS_NONE );
80129 assert( pBt->pPage1 );
80133 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
80135 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
80138 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
80139 ** database, mark the database as read-only. */
80142 pBt->btsFlags |= BTS_READ_ONLY;
80150 ** Write meta-information back into the database. Meta[0] is
80151 ** read-only and may not be written.
80154 BtShared *pBt = p->pBt;
80159 assert( p->inTrans==TRANS_WRITE );
80160 assert( pBt->pPage1!=0 );
80161 pP1 = pBt->pPage1->aData;
80162 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
80167 assert( pBt->autoVacuum || iMeta==0 );
80169 pBt->incrVacuum = (u8)iMeta;
80178 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
80179 ** number of entries in the b-tree and write the result to *pnEntry.
80196 ** page in the B-Tree structure (not including overflow pages).
80198 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
80200 MemPage *pPage; /* Current page of the b-tree */
80202 /* If this is a leaf page or the tree is not an int-key tree, then
80206 pPage = pCur->pPage;
80207 if( pPage->leaf || !pPage->intKey ){
80208 nEntry += pPage->nCell;
80214 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
80216 ** to visit is the right-child of its parent.
80221 if( pPage->leaf ){
80223 if( pCur->iPage==0 ){
80224 /* All pages of the b-tree have been visited. Return successfully. */
80229 }while ( pCur->ix>=pCur->pPage->nCell );
80231 pCur->ix++;
80232 pPage = pCur->pPage;
80236 ** points at. This is the right-child if (iIdx==pPage->nCell).
80238 iIdx = pCur->ix;
80239 if( iIdx==pPage->nCell ){
80240 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
80255 return p->pBt->pPager;
80263 pCheck->rc = SQLITE_NOMEM;
80264 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
80265 if( pCheck->nErr==0 ) pCheck->nErr++;
80273 sqlite3 *db = pCheck->db;
80274 if( AtomicLoad(&db->u1.isInterrupted) ){
80275 pCheck->rc = SQLITE_INTERRUPT;
80276 pCheck->nErr++;
80277 pCheck->mxErr = 0;
80280 if( db->xProgress ){
80281 assert( db->nProgressOps>0 );
80282 pCheck->nStep++;
80283 if( (pCheck->nStep % db->nProgressOps)==0
80284 && db->xProgress(db->pProgressArg)
80286 pCheck->rc = SQLITE_INTERRUPT;
80287 pCheck->nErr++;
80288 pCheck->mxErr = 0;
80304 if( !pCheck->mxErr ) return;
80305 pCheck->mxErr--;
80306 pCheck->nErr++;
80308 if( pCheck->errMsg.nChar ){
80309 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
80311 if( pCheck->zPfx ){
80312 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
80313 pCheck->v0, pCheck->v1, pCheck->v2);
80315 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
80317 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
80326 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
80330 assert( pCheck->aPgRef!=0 );
80331 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
80332 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
80339 assert( pCheck->aPgRef!=0 );
80340 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
80341 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
80347 ** reference to the page, add an error message to pCheck->zErrMsg.
80354 if( iPage>pCheck->nCkPage || iPage==0 ){
80368 ** Check that the entry in the pointer-map for page iChild maps to
80382 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
80409 int nErrAtStart = pCheck->nErr;
80410 while( iPage!=0 && pCheck->mxErr ){
80414 N--;
80415 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
80423 if( pCheck->pBt->autoVacuum ){
80427 if( n>pCheck->pBt->usableSize/4-2 ){
80430 N--;
80435 if( pCheck->pBt->autoVacuum ){
80441 N -= n;
80446 /* If this database supports auto-vacuum and iPage is not the last
80447 ** page in this overflow list, check that the pointer-map entry for
80450 if( pCheck->pBt->autoVacuum && N>0 ){
80459 if( N && nErrAtStart==pCheck->nErr ){
80463 expected-N, expected);
80469 ** An implementation of a min-heap.
80479 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
80508 aHeap[0]--;
80545 int depth = -1, d2; /* Depth of a subtree */
80561 u32 *heap = 0; /* Min-heap used for checking cell coverage */
80562 u32 x, prev = 0; /* Next and previous entry on the min-heap */
80563 const char *saved_zPfx = pCheck->zPfx;
80564 int saved_v1 = pCheck->v1;
80565 int saved_v2 = pCheck->v2;
80571 if( pCheck->mxErr==0 ) goto end_of_check;
80572 pBt = pCheck->pBt;
80573 usableSize = pBt->usableSize;
80576 pCheck->zPfx = "Tree %u page %u: ";
80577 pCheck->v1 = iPage;
80581 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
80587 savedIsInit = pPage->isInit;
80588 pPage->isInit = 0;
80600 data = pPage->aData;
80601 hdr = pPage->hdrOffset;
80604 pCheck->zPfx = "Tree %u page %u cell %u: ";
80608 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
80611 assert( pPage->nCell==nCell );
80613 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
80614 ** immediately follows the b-tree page header. */
80615 cellStart = hdr + 12 - 4*pPage->leaf;
80616 assert( pPage->aCellIdx==&data[cellStart] );
80617 pCellIdx = &data[cellStart + 2*(nCell-1)];
80619 if( !pPage->leaf ){
80620 /* Analyze the right-child page of internal pages */
80623 if( pBt->autoVacuum ){
80624 pCheck->zPfx = "Tree %u page %u right child: ";
80633 heap = pCheck->heap;
80637 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
80639 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
80643 pCheck->v2 = i;
80646 pCellIdx -= 2;
80647 if( pc<contentOffset || pc>usableSize-4 ){
80649 pc, contentOffset, usableSize-4);
80654 pPage->xParseCell(pPage, pCell, &info);
80662 if( pPage->intKey ){
80674 assert( pc + info.nSize - 4 <= usableSize );
80675 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
80676 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
80678 if( pBt->autoVacuum ){
80685 if( !pPage->leaf ){
80689 if( pBt->autoVacuum ){
80700 /* Populate the coverage-checking heap for leaf pages */
80701 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
80708 pCheck->zPfx = 0;
80709 if( doCoverageCheck && pCheck->mxErr>0 ){
80710 /* For leaf pages, the min-heap has already been initialized and the
80713 if( !pPage->leaf ){
80714 heap = pCheck->heap;
80716 for(i=nCell-1; i>=0; i--){
80719 size = pPage->xCellSize(pPage, &data[pc]);
80720 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
80723 /* Add the freeblocks to the min-heap
80725 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
80732 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
80735 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
80736 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
80737 ** big-endian integer which is the offset in the b-tree page of the next
80741 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
80744 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
80747 /* Analyze the min-heap looking for overlap between cells and/or
80750 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
80755 ** The loop below pulls entries from the min-heap in order and compares
80761 prev = contentOffset - 1; /* Implied first min-heap entry */
80768 nFrag += (x>>16) - (prev&0xffff) - 1;
80772 nFrag += usableSize - (prev&0xffff) - 1;
80773 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
80774 ** is stored in the fifth field of the b-tree page header.
80775 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
80786 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
80788 pCheck->zPfx = saved_zPfx;
80789 pCheck->v1 = saved_v1;
80790 pCheck->v2 = saved_v2;
80801 ** A read-only or read-write transaction must be opened before calling
80806 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
80810 ** root pages is incomplete. This is a "partial integrity-check". This
80829 BtShared *pBt = p->pBt;
80830 u64 savedDbFlags = pBt->db->flags;
80845 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
80846 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
80851 sCheck.pPager = pBt->pPager;
80865 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
80878 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
80879 get4byte(&pBt->pPage1->aData[36]));
80887 if( pBt->autoVacuum ){
80891 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
80898 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
80905 testcase( pBt->db->flags & SQLITE_CellSizeCk );
80906 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
80911 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
80918 pBt->db->flags = savedDbFlags;
80929 /* If the database supports auto-vacuum, make sure no tables contain
80930 ** references to pointer-map pages.
80933 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
80937 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
80957 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
80965 ** an empty string if the database is in-memory or a TEMP database.
80971 assert( p->pBt->pPager!=0 );
80972 return sqlite3PagerFilename(p->pBt->pPager, 1);
80984 assert( p->pBt->pPager!=0 );
80985 return sqlite3PagerJournalname(p->pBt->pPager);
80993 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
80994 return p ? p->inTrans : 0;
81002 ** transaction on the shared-cache the argument Btree is connected to.
81009 BtShared *pBt = p->pBt;
81011 if( pBt->inTransaction!=TRANS_NONE ){
81014 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
81027 assert( sqlite3_mutex_held(p->db->mutex) );
81028 return p->nBackup!=0;
81033 ** a single shared-btree. The memory is used by client code for its own
81034 ** purposes (for example, to store a high-level schema associated with
81035 ** the shared-btree). The btree layer manages reference counting issues.
81037 ** The first time this is called on a shared-btree, nBytes bytes of memory
81046 ** Just before the shared-btree is closed, the function passed as the
81052 BtShared *pBt = p->pBt;
81054 if( !pBt->pSchema && nBytes ){
81055 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
81056 pBt->xFreeSchema = xFree;
81059 return pBt->pSchema;
81069 assert( sqlite3_mutex_held(p->db->mutex) );
81086 assert( p->inTrans!=TRANS_NONE );
81087 if( p->sharable ){
81117 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
81118 assert( pCsr->curFlags & BTCF_Incrblob );
81124 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
81125 if( pCsr->eState!=CURSOR_VALID ){
81131 ** version of the b-tree page modified by the accessPayload call below.
81137 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
81143 ** (c) the connection holds a write-lock on the table (if required),
81144 ** (d) there are no conflicting read-locks, and
81147 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
81150 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
81151 && pCsr->pBt->inTransaction==TRANS_WRITE );
81152 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
81153 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
81154 assert( pCsr->pPage->intKey );
81163 pCur->curFlags |= BTCF_Incrblob;
81164 pCur->pBtree->hasIncrblobCur = 1;
81174 BtShared *pBt = pBtree->pBt;
81182 pBt->btsFlags &= ~BTS_NO_WAL;
81183 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
81187 u8 *aData = pBt->pPage1->aData;
81191 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81200 pBt->btsFlags &= ~BTS_NO_WAL;
81209 return (pCsr->hints & mask)!=0;
81213 ** Return true if the given Btree is read-only.
81216 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
81225 ** If no transaction is active and the database is not a temp-db, clear
81226 ** the in-memory pager cache.
81229 BtShared *pBt = p->pBt;
81230 if( pBt->inTransaction==TRANS_NONE ){
81231 sqlite3PagerClearCache(pBt->pPager);
81240 return p->sharable;
81249 testcase( p->sharable );
81250 return p->pBt->nRef;
81278 Btree *pDest; /* Destination b-tree file */
81280 int bDestLocked; /* True once a write-transaction is open on pDest */
81284 Btree *pSrc; /* Source b-tree file */
81302 ** structure may be accessed via two groups of thread-safe entry points:
81316 ** backup_pagecount() are not thread-safe functions. If they are called
81324 ** Non-sharable Btrees (in-memory databases for example), do not have
81360 return pDb->aDb[i].pBt;
81369 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
81374 ** Check that there is no open read-transaction on the b-tree passed as the
81376 ** is an open read-transaction, return SQLITE_ERROR and leave an error
81418 sqlite3_mutex_enter(pSrcDb->mutex);
81419 sqlite3_mutex_enter(pDestDb->mutex);
81428 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
81439 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
81440 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
81441 p->pDestDb = pDestDb;
81442 p->pSrcDb = pSrcDb;
81443 p->iNext = 1;
81444 p->isAttached = 0;
81446 if( 0==p->pSrc || 0==p->pDest
81447 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
81459 p->pSrc->nBackup++;
81462 sqlite3_mutex_leave(pDestDb->mutex);
81463 sqlite3_mutex_leave(pSrcDb->mutex);
81487 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
81488 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
81489 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
81495 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
81496 assert( p->bDestLocked );
81497 assert( !isFatalError(p->rc) );
81498 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
81506 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
81509 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
81527 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
81539 ** this function is a no-op.
81559 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
81560 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
81561 p->pNext = *pp;
81563 p->isAttached = 1;
81567 ** Copy nPage pages from the source b-tree to the destination.
81578 sqlite3_mutex_enter(p->pSrcDb->mutex);
81579 sqlite3BtreeEnter(p->pSrc);
81580 if( p->pDestDb ){
81581 sqlite3_mutex_enter(p->pDestDb->mutex);
81584 rc = p->rc;
81586 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
81587 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
81589 int nSrcPage = -1; /* Size of source db in pages */
81592 /* If the source pager is currently in a write-transaction, return
81595 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
81601 /* If there is no open read-transaction on the source database, open
81605 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
81606 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
81616 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
81621 if( SQLITE_OK==rc && p->bDestLocked==0
81622 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
81623 (int*)&p->iDestSchema))
81625 p->bDestLocked = 1;
81630 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
81631 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
81632 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
81640 /* Now that there is a read-lock on the source database, query the
81643 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
81645 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
81646 const Pgno iSrcPg = p->iNext; /* Source page number */
81647 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
81655 p->iNext++;
81658 p->nPagecount = nSrcPage;
81659 p->nRemaining = nSrcPage+1-p->iNext;
81660 if( p->iNext>(Pgno)nSrcPage ){
81662 }else if( !p->isAttached ){
81668 ** is to make sure that the schema-version really does change in
81674 rc = sqlite3BtreeNewDb(p->pDest);
81678 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
81681 if( p->pDestDb ){
81682 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
81685 rc = sqlite3BtreeSetVersion(p->pDest, 2);
81702 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
81703 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
81706 nDestTruncate = (nSrcPage+ratio-1)/ratio;
81707 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
81708 nDestTruncate--;
81716 /* If the source page-size is smaller than the destination page-size,
81722 ** pending-byte page in the source database may need to be
81735 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
81747 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
81791 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
81801 ** "committing" a read-only transaction cannot fail.
81805 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
81806 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
81813 p->rc = rc;
81815 if( p->pDestDb ){
81816 sqlite3_mutex_leave(p->pDestDb->mutex);
81818 sqlite3BtreeLeave(p->pSrc);
81819 sqlite3_mutex_leave(p->pSrcDb->mutex);
81833 pSrcDb = p->pSrcDb;
81834 sqlite3_mutex_enter(pSrcDb->mutex);
81835 sqlite3BtreeEnter(p->pSrc);
81836 if( p->pDestDb ){
81837 sqlite3_mutex_enter(p->pDestDb->mutex);
81841 if( p->pDestDb ){
81842 p->pSrc->nBackup--;
81844 if( p->isAttached ){
81845 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
81848 pp = &(*pp)->pNext;
81851 *pp = p->pNext;
81855 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
81858 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
81859 if( p->pDestDb ){
81860 sqlite3Error(p->pDestDb, rc);
81863 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
81865 sqlite3BtreeLeave(p->pSrc);
81866 if( p->pDestDb ){
81867 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
81887 return p->nRemaining;
81901 return p->nPagecount;
81923 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81924 if( !isFatalError(p->rc) && iPage<p->iNext ){
81930 assert( p->pDestDb );
81931 sqlite3_mutex_enter(p->pDestDb->mutex);
81933 sqlite3_mutex_leave(p->pDestDb->mutex);
81936 p->rc = rc;
81939 }while( (p = p->pNext)!=0 );
81958 for(p=pBackup; p; p=p->pNext){
81959 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81960 p->iNext = 1;
81982 if( pFd->pMethods ){
81995 b.pSrcDb = pFrom->db;
82004 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
82011 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
82049 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
82062 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
82068 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
82071 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
82073 if( p->flags & MEM_Null ){
82075 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
82082 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
82085 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82086 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82087 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
82090 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
82098 assert( (p->flags & MEM_Cleared)==0 );
82102 assert( p->szMalloc==0
82103 || (p->flags==MEM_Undefined
82104 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
82105 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
82115 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
82117 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
82118 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
82119 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
82120 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
82133 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
82135 if( p->flags & MEM_Int ){
82137 /* Work-around for GCC bug
82140 assert( (p->flags&MEM_Int)*2==sizeof(x) );
82141 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
82142 p->n = sqlite3Int64ToText(x, zBuf);
82144 p->n = sqlite3Int64ToText(p->u.i, zBuf);
82149 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
82151 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
82152 p->n = acc.nChar;
82169 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
82184 if( (p->flags & MEM_Str)==0 ) return 1;
82185 if( p->db && p->db->mallocFailed ) return 1;
82186 if( p->flags & MEM_Term ){
82187 /* Insure that the string is properly zero-terminated. Pay particular
82188 ** attention to the case where p->n is odd */
82189 if( p->szMalloc>0 && p->z==p->zMalloc ){
82190 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
82191 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
82193 assert( p->z[p->n]==0 );
82194 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
82195 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
82197 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
82200 z = p->z;
82203 if( p->enc!=SQLITE_UTF8 ){
82205 if( p->enc==SQLITE_UTF16BE ) z++;
82222 ** routine is a no-op.
82236 if( !(pMem->flags&MEM_Str) ){
82237 pMem->enc = desiredEnc;
82240 if( pMem->enc==desiredEnc ){
82243 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82253 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
82254 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
82260 ** Make sure pMem->z points to a writable allocation of at least n bytes.
82263 ** pMem->z into the new allocation. pMem must be either a string or
82265 ** in pMem->z is discarded.
82270 testcase( pMem->db==0 );
82274 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
82275 testcase( bPreserve && pMem->z==0 );
82277 assert( pMem->szMalloc==0
82278 || (pMem->flags==MEM_Undefined
82279 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
82280 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
82281 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
82282 if( pMem->db ){
82283 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
82285 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
82286 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
82287 pMem->z = pMem->zMalloc;
82291 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
82292 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
82294 if( pMem->zMalloc==0 ){
82296 pMem->z = 0;
82297 pMem->szMalloc = 0;
82300 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
82303 if( bPreserve && pMem->z ){
82304 assert( pMem->z!=pMem->zMalloc );
82305 memcpy(pMem->zMalloc, pMem->z, pMem->n);
82307 if( (pMem->flags&MEM_Dyn)!=0 ){
82308 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
82309 pMem->xDel((void *)(pMem->z));
82312 pMem->z = pMem->zMalloc;
82313 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
82318 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
82319 ** If pMem->zMalloc already meets or exceeds the requested size, this
82320 ** routine is a no-op.
82323 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
82332 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
82333 if( pMem->szMalloc<szNew ){
82336 assert( (pMem->flags & MEM_Dyn)==0 );
82337 pMem->z = pMem->zMalloc;
82338 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
82343 ** If pMem is already a string, detect if it is a zero-terminated
82347 ** this routine is a no-op.
82350 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){
82354 if( pMem->enc!=SQLITE_UTF8 ) return;
82355 if( NEVER(pMem->z==0) ) return;
82356 if( pMem->flags & MEM_Dyn ){
82357 if( pMem->xDel==sqlite3_free
82358 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
82360 pMem->z[pMem->n] = 0;
82361 pMem->flags |= MEM_Term;
82364 if( pMem->xDel==sqlite3RCStrUnref ){
82365 /* Blindly assume that all RCStr objects are zero-terminated */
82366 pMem->flags |= MEM_Term;
82369 }else if( pMem->szMalloc >= pMem->n+1 ){
82370 pMem->z[pMem->n] = 0;
82371 pMem->flags |= MEM_Term;
82381 ** to be a double-zero byte at an even byte boundary in order to
82386 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
82389 pMem->z[pMem->n] = 0;
82390 pMem->z[pMem->n+1] = 0;
82391 pMem->z[pMem->n+2] = 0;
82392 pMem->flags |= MEM_Term;
82404 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82406 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
82408 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
82413 pMem->flags &= ~MEM_Ephem;
82415 pMem->pScopyFrom = 0;
82422 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
82429 assert( pMem->flags & MEM_Zero );
82430 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
82433 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82436 nByte = pMem->n + pMem->u.nZero;
82438 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
82444 assert( pMem->z!=0 );
82445 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
82447 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
82448 pMem->n += pMem->u.nZero;
82449 pMem->flags &= ~(MEM_Zero|MEM_Term);
82459 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82460 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
82461 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
82462 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
82487 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82488 assert( !(pMem->flags&MEM_Zero) );
82489 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
82490 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
82496 pMem->enc = 0;
82500 vdbeMemRenderNum(nByte, pMem->z, pMem);
82501 assert( pMem->z!=0 );
82502 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
82503 pMem->enc = SQLITE_UTF8;
82504 pMem->flags |= MEM_Str|MEM_Term;
82505 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
82523 assert( pMem->db!=0 );
82524 assert( pFunc->xFinalize!=0 );
82525 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
82526 assert( sqlite3_mutex_held(pMem->db->mutex) );
82530 t.db = pMem->db;
82535 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
82536 assert( (pMem->flags & MEM_Dyn)==0 );
82537 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
82554 assert( pFunc->xValue!=0 );
82555 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
82556 assert( pAccum->db!=0 );
82557 assert( sqlite3_mutex_held(pAccum->db->mutex) );
82563 ctx.enc = ENC(pAccum->db);
82564 pFunc->xValue(&ctx);
82579 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
82581 if( p->flags&MEM_Agg ){
82582 sqlite3VdbeMemFinalize(p, p->u.pDef);
82583 assert( (p->flags & MEM_Agg)==0 );
82584 testcase( p->flags & MEM_Dyn );
82586 if( p->flags&MEM_Dyn ){
82587 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
82588 p->xDel((void *)p->z);
82590 p->flags = MEM_Null;
82595 ** by p->xDel and memory in p->zMalloc.
82605 if( p->szMalloc ){
82606 sqlite3DbFreeNN(p->db, p->zMalloc);
82607 p->szMalloc = 0;
82609 p->z = 0;
82624 if( VdbeMemDynamic(p) || p->szMalloc ){
82634 if( p->szMalloc ) vdbeMemClear(p);
82641 ** a floating-point then the value returned is the integer part.
82644 ** an SQL-NULL value, return 0.
82650 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
82656 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82658 flags = pMem->flags;
82661 return pMem->u.i;
82663 return sqlite3RealToI64(pMem->u.r);
82664 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
82680 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
82685 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82687 if( pMem->flags & MEM_Real ){
82688 return pMem->u.r;
82689 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
82690 testcase( pMem->flags & MEM_IntReal );
82691 return (double)pMem->u.i;
82692 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
82705 testcase( pMem->flags & MEM_IntReal );
82706 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
82707 if( pMem->flags & MEM_Null ) return ifNull;
82717 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
82719 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82722 if( pMem->flags & MEM_IntReal ){
82725 i64 ix = sqlite3RealToI64(pMem->u.r);
82729 ** (1) the round-trip conversion real->int->real is a no-op, and
82737 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
82738 pMem->u.i = ix;
82749 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82753 pMem->u.i = sqlite3VdbeIntValue(pMem);
82764 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82767 pMem->u.r = sqlite3VdbeRealValue(pMem);
82777 ** For some versions of GCC on 32-bit machines, if you do the more obvious
82779 ** though the r1 and (double)i values are bit-for-bit the same.
82785 && i >= -2251799813685248LL && i < 2251799813685248LL);
82793 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
82808 testcase( pMem->flags & MEM_Int );
82809 testcase( pMem->flags & MEM_Real );
82810 testcase( pMem->flags & MEM_IntReal );
82811 testcase( pMem->flags & MEM_Null );
82812 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
82815 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
82816 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82817 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
82818 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
82819 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
82821 pMem->u.i = ix;
82827 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
82828 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
82840 if( pMem->flags & MEM_Null ) return SQLITE_OK;
82843 if( (pMem->flags & MEM_Blob)==0 ){
82845 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
82846 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
82848 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
82868 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
82870 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
82871 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
82872 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
82888 pMem->flags = flags;
82889 pMem->db = db;
82890 pMem->szMalloc = 0;
82910 pMem->flags = MEM_Null;
82924 pMem->flags = MEM_Blob|MEM_Zero;
82925 pMem->n = 0;
82927 pMem->u.nZero = n;
82928 pMem->enc = SQLITE_UTF8;
82929 pMem->z = 0;
82937 assert( pMem->z!=0 );
82938 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
82939 memset(pMem->z, 0, nByte);
82940 pMem->n = n>0?n:0;
82941 pMem->flags = MEM_Blob;
82942 pMem->enc = SQLITE_UTF8;
82950 ** a 64-bit integer.
82954 pMem->u.i = val;
82955 pMem->flags = MEM_Int;
82966 pMem->u.i = val;
82967 pMem->flags = MEM_Int;
82971 /* A no-op destructor */
82984 assert( pMem->flags==MEM_Null );
82986 pMem->u.zPType = zPType ? zPType : "";
82987 pMem->z = pPtr;
82988 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
82989 pMem->eSubtype = 'p';
82990 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
83001 pMem->u.r = val;
83002 pMem->flags = MEM_Real;
83013 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
83014 && pMem->xDel==sqlite3RowSetDelete;
83026 sqlite3 *db = pMem->db;
83033 pMem->z = (char*)p;
83034 pMem->flags = MEM_Blob|MEM_Dyn;
83035 pMem->xDel = sqlite3RowSetDelete;
83041 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
83044 assert( p->db!=0 );
83045 if( p->flags & (MEM_Str|MEM_Blob) ){
83046 int n = p->n;
83047 if( p->flags & MEM_Zero ){
83048 n += p->u.nZero;
83050 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
83061 ** This is used for testing and debugging only - to help ensure that shallow
83067 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
83068 if( pX->pScopyFrom==pMem ){
83070 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
83072 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
83080 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
83081 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
83084 ** undefined so that we can quickly detect the shallow-copy error */
83085 pX->flags = MEM_Undefined;
83086 pX->pScopyFrom = 0;
83089 pMem->pScopyFrom = 0;
83095 ** pTo are freed. The pFrom->z field is not duplicated. If
83096 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
83106 assert( pTo->db==pFrom->db );
83109 if( (pFrom->flags&MEM_Static)==0 ){
83110 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
83112 pTo->flags |= srcType;
83126 pTo->flags &= ~MEM_Dyn;
83127 if( pTo->flags&(MEM_Str|MEM_Blob) ){
83128 if( 0==(pFrom->flags&MEM_Static) ){
83129 pTo->flags |= MEM_Ephem;
83144 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
83145 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
83146 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
83150 pFrom->flags = MEM_Null;
83151 pFrom->szMalloc = 0;
83174 ** non-negative for blobs.
83183 i64 nByte = n; /* New value for pMem->n */
83185 u16 flags; /* New value for pMem->flags */
83188 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
83198 if( pMem->db ){
83199 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
83220 sqlite3DbFree(pMem->db, (void*)z);
83226 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
83244 memcpy(pMem->z, z, nAlloc);
83247 pMem->z = (char *)z;
83249 pMem->zMalloc = pMem->z;
83250 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
83252 pMem->xDel = xDel;
83257 pMem->n = (int)(nByte & 0x7fffffff);
83258 pMem->flags = flags;
83259 pMem->enc = enc;
83278 ** pMem->zMalloc to hold the content from the btree, if possible. New
83279 ** pMem->zMalloc space will be allocated if necessary. The calling routine
83293 pMem->flags = MEM_Null;
83298 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
83300 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
83301 pMem->flags = MEM_Blob;
83302 pMem->n = (int)amt;
83323 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
83324 assert( pMem->z!=0 );
83327 pMem->flags = MEM_Blob|MEM_Ephem;
83328 pMem->n = (int)amt;
83339 ** to a zero-terminated version of that string.
83343 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
83346 assert( (pVal->flags & (MEM_Null))==0 );
83347 if( pVal->flags & (MEM_Blob|MEM_Str) ){
83349 pVal->flags |= MEM_Str;
83350 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
83353 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
83354 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
83359 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
83362 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
83364 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
83365 || pVal->db->mallocFailed );
83366 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
83368 return pVal->z;
83380 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
83386 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
83389 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
83391 return pVal->z;
83393 if( pVal->flags&MEM_Null ){
83403 ** that third-party extensions can get access to it?
83407 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)
83408 && (pVal->flags & MEM_Dyn)!=0
83409 && pVal->xDel==xFree
83423 p->flags = MEM_Null;
83424 p->db = db;
83445 ** Otherwise, if the second argument is non-zero, then this function is
83454 UnpackedRecord *pRec = p->ppRec[0];
83457 Index *pIdx = p->pIdx; /* Index being probed */
83460 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
83465 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
83466 if( pRec->pKeyInfo ){
83467 assert( pRec->pKeyInfo->nAllField==nCol );
83468 assert( pRec->pKeyInfo->enc==ENC(db) );
83469 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
83471 pRec->aMem[i].flags = MEM_Null;
83472 pRec->aMem[i].db = db;
83480 p->ppRec[0] = pRec;
83483 pRec->nField = p->iVal+1;
83484 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
83485 return &pRec->aMem[p->iVal];
83532 assert( (p->flags & EP_TokenOnly)==0 );
83534 pList = p->x.pList;
83535 if( pList ) nVal = pList->nExpr;
83537 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
83542 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83543 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
83555 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
83570 pFunc->xSFunc(&ctx, nVal, apVal);
83573 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
83580 pCtx->pParse->nErr++;
83587 pCtx->pParse->rc = rc;
83629 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
83630 if( op==TK_REGISTER ) op = pExpr->op2;
83636 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
83641 aff = sqlite3AffinityType(pExpr->u.zToken,0);
83642 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
83648 /* zero-blobs only come from functions, not literal values. And
83659 ** case when the value is -9223372036854775808.
83662 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
83663 pExpr = pExpr->pLeft;
83664 op = pExpr->op;
83665 negInt = -1;
83666 zNeg = "-";
83673 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
83675 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
83677 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
83684 assert( (pVal->flags & MEM_IntReal)==0 );
83685 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
83686 testcase( pVal->flags & MEM_Int );
83687 testcase( pVal->flags & MEM_Real );
83688 pVal->flags &= ~MEM_Str;
83694 /* This branch happens for multiple negative signs. Ex: -(-5) */
83695 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
83699 if( pVal->flags & MEM_Real ){
83700 pVal->u.r = -pVal->u.r;
83701 }else if( pVal->u.i==SMALLEST_INT64 ){
83703 pVal->u.r = -(double)SMALLEST_INT64;
83705 pVal->u.r = LARGEST_INT64;
83709 pVal->u.i = -pVal->u.i;
83722 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
83723 assert( pExpr->u.zToken[1]=='\'' );
83726 zVal = &pExpr->u.zToken[2];
83727 nVal = sqlite3Strlen30(zVal)-1;
83742 pVal->flags = MEM_Int;
83743 pVal->u.i = pExpr->u.zToken[4]==0;
83753 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
83814 sqlite3 *db = pParse->db;
83819 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
83825 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
83827 int iBindVar = pExpr->iColumn;
83828 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
83829 if( (v = pParse->pReprepare)!=0 ){
83832 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
83834 pVal->db = pParse->db;
83841 assert( pVal==0 || pVal->db==db );
83892 if( pExpr==0 || pExpr->op!=TK_SELECT ){
83903 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
83935 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
83977 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
83978 pMem->enc = ENC(db);
83990 int nCol = pRec->pKeyInfo->nAllField;
83991 Mem *aMem = pRec->aMem;
83996 sqlite3KeyInfoUnref(pRec->pKeyInfo);
84021 sqlite3DbFreeNN(((Mem*)v)->db, v);
84030 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
84034 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
84035 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
84036 return p->n;
84038 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
84039 return p->n;
84041 if( (p->flags & MEM_Blob)!=0 ){
84042 if( p->flags & MEM_Zero ){
84043 return p->n + p->u.nZero;
84045 return p->n;
84048 if( p->flags & MEM_Null ) return 0;
84079 sqlite3 *db = pParse->db;
84083 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
84084 p->db = db;
84085 if( db->pVdbe ){
84086 db->pVdbe->ppVPrev = &p->pVNext;
84088 p->pVNext = db->pVdbe;
84089 p->ppVPrev = &db->pVdbe;
84090 db->pVdbe = p;
84091 assert( p->eVdbeState==VDBE_INIT_STATE );
84092 p->pParse = pParse;
84093 pParse->pVdbe = p;
84094 assert( pParse->aLabel==0 );
84095 assert( pParse->nLabel==0 );
84096 assert( p->nOpAlloc==0 );
84097 assert( pParse->szOpAlloc==0 );
84106 return p->pParse;
84114 sqlite3DbFree(p->db, p->zErrMsg);
84116 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
84125 p->prepFlags = prepFlags;
84127 p->expmask = 0;
84129 assert( p->zSql==0 );
84130 p->zSql = sqlite3DbStrNDup(p->db, z, n);
84135 ** Add a new element to the Vdbe->pDblStr list.
84141 sizeof(*pStr)+n+1-sizeof(pStr->z));
84143 pStr->pNextStr = p->pDblStr;
84144 p->pDblStr = pStr;
84145 memcpy(pStr->z, z, n+1);
84153 ** zId of length nId is a double-quoted identifier. Check to see if
84158 const char *zId /* The double-quoted identifier, already dequoted */
84162 if( pVdbe->pDblStr==0 ) return 0;
84163 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
84164 if( strcmp(zId, pStr->z)==0 ) return 1;
84171 ** Swap byte-code between two VDBE structures.
84183 assert( pA->db==pB->db );
84187 pTmp = pA->pVNext;
84188 pA->pVNext = pB->pVNext;
84189 pB->pVNext = pTmp;
84190 ppTmp = pA->ppVPrev;
84191 pA->ppVPrev = pB->ppVPrev;
84192 pB->ppVPrev = ppTmp;
84193 zTmp = pA->zSql;
84194 pA->zSql = pB->zSql;
84195 pB->zSql = zTmp;
84197 zTmp = pA->zNormSql;
84198 pA->zNormSql = pB->zNormSql;
84199 pB->zNormSql = zTmp;
84201 pB->expmask = pA->expmask;
84202 pB->prepFlags = pA->prepFlags;
84203 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
84204 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
84212 ** If an out-of-memory error occurs while resizing the array, return
84219 Parse *p = v->pParse;
84221 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
84229 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
84230 : (sqlite3_int64)v->nOpAlloc+nOp);
84232 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
84238 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
84239 sqlite3OomFault(p->db);
84244 assert( nNew>=(v->nOpAlloc+nOp) );
84245 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
84247 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
84248 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
84249 v->aOp = pNew;
84281 assert( p->nOpAlloc<=p->nOp );
84283 assert( p->nOpAlloc>p->nOp );
84295 if( p->db->mallocFailed==0 ){
84296 VdbeOp *pOp = &p->aOp[addr];
84297 pOp->p4type = P4_INT32;
84298 pOp->p4.i = p4;
84329 i = p->nOp;
84330 assert( p->eVdbeState==VDBE_INIT_STATE );
84332 if( p->nOpAlloc<=i ){
84335 assert( p->aOp!=0 );
84336 p->nOp++;
84337 pOp = &p->aOp[i];
84339 pOp->opcode = (u8)op;
84340 pOp->p5 = 0;
84341 pOp->p1 = p1;
84342 pOp->p2 = p2;
84343 pOp->p3 = p3;
84344 pOp->p4.p = 0;
84345 pOp->p4type = P4_NOTUSED;
84350 pOp->zComment = 0;
84353 pOp->nExec = 0;
84354 pOp->nCycle = 0;
84357 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84358 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
84359 test_addop_breakpoint(i, &p->aOp[i]);
84363 pOp->iSrcLine = 0;
84381 i = p->nOp;
84382 if( p->nOpAlloc<=i ){
84385 p->nOp++;
84386 pOp = &p->aOp[i];
84388 pOp->opcode = (u8)op;
84389 pOp->p5 = 0;
84390 pOp->p1 = p1;
84391 pOp->p2 = p2;
84392 pOp->p3 = p3;
84393 pOp->p4.i = p4;
84394 pOp->p4type = P4_INT32;
84399 pOp->zComment = 0;
84402 pOp->nExec = 0;
84403 pOp->nCycle = 0;
84406 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84407 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
84408 test_addop_breakpoint(i, &p->aOp[i]);
84412 pOp->iSrcLine = 0;
84500 Vdbe *v = pParse->pVdbe;
84505 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
84506 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
84508 assert( pParse->db->mallocFailed );
84509 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
84512 pCtx->pOut = 0;
84513 pCtx->pFunc = (FuncDef*)pFunc;
84514 pCtx->pVdbe = 0;
84515 pCtx->isError = 0;
84516 pCtx->argc = nArg;
84517 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
84550 if( pParse->addrExplain==0 ) return 0;
84551 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
84552 return pOp->p2;
84577 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
84585 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
84587 v = pParse->pVdbe;
84588 iThis = v->nOp;
84589 addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
84591 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
84593 pParse->addrExplain = iThis;
84595 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
84605 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
84621 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
84622 sqlite3MayAbort(p->pParse);
84625 /* Insert the end of a co-routine
84631 ** co-routine has its own independent set of registers, because co-routines
84633 ** that could cause problems if two or more co-routines are using the same
84636 v->pParse->nTempReg = 0;
84637 v->pParse->nRangeReg = 0;
84649 ** always negative and P2 values are suppose to be non-negative.
84656 ** Parse.aLabel[x] Stores the address that the x-th label resolves
84658 ** labels stores -1, but that is not required.
84666 return --pParse->nLabel;
84675 int nNewSize = 10 - p->nLabel;
84676 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
84677 nNewSize*sizeof(p->aLabel[0]));
84678 if( p->aLabel==0 ){
84679 p->nLabelAlloc = 0;
84683 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
84685 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
84688 p->nLabelAlloc = nNewSize;
84689 p->aLabel[j] = v->nOp;
84693 Parse *p = v->pParse;
84695 assert( v->eVdbeState==VDBE_INIT_STATE );
84696 assert( j<-p->nLabel );
84699 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84700 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
84703 if( p->nLabelAlloc + p->nLabel < 0 ){
84706 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
84707 p->aLabel[j] = v->nOp;
84723 for(i=1; ALWAYS(i<p->nOp); i++){
84724 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
84725 p->aOp[1].opcode = OP_Noop;
84735 ** in a Vdbe main program and each of the sub-programs (triggers) it may
84746 ** sqlite3DbFree(v->db, sIter.apSub);
84755 int iSub; /* 0 = main program, 1 = first sub-program etc. */
84758 Vdbe *v = p->v;
84763 if( p->iSub<=p->nSub ){
84765 if( p->iSub==0 ){
84766 aOp = v->aOp;
84767 nOp = v->nOp;
84769 aOp = p->apSub[p->iSub-1]->aOp;
84770 nOp = p->apSub[p->iSub-1]->nOp;
84772 assert( p->iAddr<nOp );
84774 pRet = &aOp[p->iAddr];
84775 p->iAddr++;
84776 if( p->iAddr==nOp ){
84777 p->iSub++;
84778 p->iAddr = 0;
84781 if( pRet->p4type==P4_SUBPROGRAM ){
84782 int nByte = (p->nSub+1)*sizeof(SubProgram*);
84784 for(j=0; j<p->nSub; j++){
84785 if( p->apSub[j]==pRet->p4.pProgram ) break;
84787 if( j==p->nSub ){
84788 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
84789 if( !p->apSub ){
84792 p->apSub[p->nSub++] = pRet->p4.pProgram;
84805 ** sub-programs contains any of the following:
84822 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
84838 int opcode = pOp->opcode;
84845 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
84850 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
84854 ** where a "DELETE FROM tbl" has a statement-journal but does not
84855 ** require one. This is not so bad - it is an inefficiency, not a bug. */
84856 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
84861 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
84866 sqlite3DbFree(v->db, sIter.apSub);
84873 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
84877 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
84886 || (pC->eCurType!=CURTYPE_SORTER
84887 && pC->eCurType!=CURTYPE_PSEUDO
84888 && !pC->isEphemeral)
84890 p->nWrite++;
84901 assert( p->nWrite==0 || p->usesStmtJournal );
84929 Parse *pParse = p->pParse;
84930 int *aLabel = pParse->aLabel;
84932 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
84933 p->readOnly = 1;
84934 p->bIsReader = 0;
84935 pOp = &p->aOp[p->nOp-1];
84936 assert( p->aOp[0].opcode==OP_Init );
84944 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
84947 switch( pOp->opcode ){
84949 if( pOp->p2!=0 ) p->readOnly = 0;
84954 p->bIsReader = 1;
84962 p->readOnly = 0;
84963 p->bIsReader = 1;
84967 assert( pOp->p2>=0 );
84972 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
84977 assert( (pOp - p->aOp) >= 3 );
84978 assert( pOp[-1].opcode==OP_Integer );
84979 n = pOp[-1].p1;
84986 if( pOp->p2<0 ){
84988 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
84989 ** have non-negative values for P2. */
84990 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
84991 assert( ADDR(pOp->p2)<-pParse->nLabel );
84992 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
84993 pOp->p2 = aLabel[ADDR(pOp->p2)];
84999 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
85000 ** have non-negative values for P2. */
85001 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
85003 assert( pOp>p->aOp );
85004 pOp--;
85008 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
85009 pParse->aLabel = 0;
85011 pParse->nLabel = 0;
85013 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
85038 Vdbe *v, /* The byte-code program under construction */
85048 pParse = v->pParse;
85050 if( pParse->nErr ) return;
85052 assert( iLast<v->nOp );
85053 pOp = &v->aOp[iFirst];
85055 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
85056 int iDest = pOp->p2; /* Jump destination */
85058 if( pOp->opcode==OP_Gosub ) continue;
85059 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull ){
85060 /* This is a deliberately taken illegal branch. tag-20230325-2 */
85066 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
85069 iDest = pParse->aLabel[j];
85073 for(; j<v->nOp; j++){
85074 VdbeOp *pX = &v->aOp[j];
85075 if( pX->opcode==OP_Return ){
85076 if( pX->p1==iRetReg ) break;
85079 if( pX->opcode==OP_Noop ) continue;
85080 if( pX->opcode==OP_Explain ) continue;
85108 assert( p->eVdbeState==VDBE_INIT_STATE );
85109 return p->nOp;
85118 ** sqlite3VdbeAddOpList() will always be non-NULL.
85122 assert( p->nOp + N <= p->nOpAlloc );
85136 for(i=0; i<p->nOp; i++){
85137 assert( p->aOp[i].opcode!=OP_ResultRow );
85165 VdbeOp *aOp = p->aOp;
85166 assert( aOp && !p->db->mallocFailed );
85169 assert( DbMaskAllZero(p->btreeMask) );
85172 *pnOp = p->nOp;
85173 p->aOp = 0;
85181 ** Non-zero P2 arguments to jump instructions are automatically adjusted
85188 int iLineno /* Source-file line number of first opcode */
85193 assert( p->eVdbeState==VDBE_INIT_STATE );
85194 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
85197 pFirst = pOut = &p->aOp[p->nOp];
85199 pOut->opcode = aOp->opcode;
85200 pOut->p1 = aOp->p1;
85201 pOut->p2 = aOp->p2;
85202 assert( aOp->p2>=0 );
85203 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
85204 pOut->p2 += p->nOp;
85206 pOut->p3 = aOp->p3;
85207 pOut->p4type = P4_NOTUSED;
85208 pOut->p4.p = 0;
85209 pOut->p5 = 0;
85211 pOut->zComment = 0;
85214 pOut->iSrcLine = iLineno+i;
85219 if( p->db->flags & SQLITE_VdbeAddopTrace ){
85220 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
85224 p->nOp += nOp;
85240 if( IS_STMT_SCANSTATUS(p->db) ){
85241 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
85243 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
85245 ScanStatus *pNew = &aNew[p->nScan++];
85247 pNew->addrExplain = addrExplain;
85248 pNew->addrLoop = addrLoop;
85249 pNew->addrVisit = addrVisit;
85250 pNew->nEst = nEst;
85251 pNew->zName = sqlite3DbStrDup(p->db, zName);
85252 p->aScan = aNew;
85270 if( IS_STMT_SCANSTATUS(p->db) ){
85273 for(ii=p->nScan-1; ii>=0; ii--){
85274 pScan = &p->aScan[ii];
85275 if( pScan->addrExplain==addrExplain ) break;
85279 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
85280 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
85281 if( pScan->aAddrRange[ii]==0 ){
85282 pScan->aAddrRange[ii] = addrStart;
85283 pScan->aAddrRange[ii+1] = addrEnd;
85302 if( IS_STMT_SCANSTATUS(p->db) ){
85305 for(ii=p->nScan-1; ii>=0; ii--){
85306 pScan = &p->aScan[ii];
85307 if( pScan->addrExplain==addrExplain ) break;
85311 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
85312 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
85325 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
85329 sqlite3VdbeGetOp(p,addr)->p1 = val;
85332 assert( addr>=0 || p->db->mallocFailed );
85333 sqlite3VdbeGetOp(p,addr)->p2 = val;
85337 sqlite3VdbeGetOp(p,addr)->p3 = val;
85340 assert( p->nOp>0 || p->db->mallocFailed );
85341 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
85351 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
85352 pOp->p5 |= OPFLAG_TYPEOFARG;
85361 sqlite3VdbeChangeP2(p, addr, p->nOp);
85367 ** the previous opcode (and is thus a no-op) then simply back up
85372 ** strives to omit useless byte-code like this:
85378 if( addr==p->nOp-1 ){
85379 assert( p->aOp[addr].opcode==OP_Once
85380 || p->aOp[addr].opcode==OP_If
85381 || p->aOp[addr].opcode==OP_FkIfZero );
85382 assert( p->aOp[addr].p4type==0 );
85384 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
85386 p->nOp--;
85388 sqlite3VdbeChangeP2(p, addr, p->nOp);
85399 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
85408 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
85413 freeEphemeralFunction(db, p->pFunc);
85431 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
85445 if( db->pnBytesFreed==0 ){
85453 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
85457 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
85472 Op *pOp = &aOp[nOp-1];
85474 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
85476 sqlite3DbFree(db, pOp->zComment);
85479 pOp--;
85487 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
85491 p->pNext = pVdbe->pProgram;
85492 pVdbe->pProgram = p;
85499 return pVdbe->pProgram!=0;
85507 if( p->db->mallocFailed ) return 0;
85508 assert( addr>=0 && addr<p->nOp );
85509 pOp = &p->aOp[addr];
85510 freeP4(p->db, pOp->p4type, pOp->p4.p);
85511 pOp->p4type = P4_NOTUSED;
85512 pOp->p4.z = 0;
85513 pOp->opcode = OP_Noop;
85522 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
85523 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
85541 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
85542 assert( pParse->pVdbe );
85544 assert( iFirst+N-1<=pParse->nMem );
85549 N--;
85551 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
85552 mask &= ~MASKBIT32(N-1);
85553 N--;
85557 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
85558 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
85586 if( pOp->p4type ){
85587 freeP4(p->db, pOp->p4type, pOp->p4.p);
85588 pOp->p4type = 0;
85589 pOp->p4.p = 0;
85592 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
85595 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
85596 pOp->p4type = P4_DYNAMIC;
85603 db = p->db;
85604 assert( p->eVdbeState==VDBE_INIT_STATE );
85605 assert( p->aOp!=0 || db->mallocFailed );
85606 if( db->mallocFailed ){
85610 assert( p->nOp>0 );
85611 assert( addr<p->nOp );
85613 addr = p->nOp - 1;
85615 pOp = &p->aOp[addr];
85616 if( n>=0 || pOp->p4type ){
85623 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
85624 pOp->p4type = P4_INT32;
85627 pOp->p4.p = (void*)zP4;
85628 pOp->p4type = (signed char)n;
85635 ** to the value defined by the arguments. This is a high-speed
85646 if( p->db->mallocFailed ){
85647 freeP4(p->db, n, pP4);
85650 assert( p->nOp>0 );
85651 pOp = &p->aOp[p->nOp-1];
85652 assert( pOp->p4type==P4_NOTUSED );
85653 pOp->p4type = n;
85654 pOp->p4.p = pP4;
85663 Vdbe *v = pParse->pVdbe;
85674 ** insert a No-op and add the comment to that new instruction. This
85679 assert( p->nOp>0 || p->aOp==0 );
85680 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
85681 if( p->nOp ){
85682 assert( p->aOp );
85683 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
85684 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
85711 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
85716 ** Return the opcode for a given address. The address must be non-negative.
85732 assert( p->eVdbeState==VDBE_INIT_STATE );
85733 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
85734 if( p->db->mallocFailed ){
85737 return &p->aOp[addr];
85744 return sqlite3VdbeGetOp(p, p->nOp - 1);
85753 if( c=='1' ) return pOp->p1;
85754 if( c=='2' ) return pOp->p2;
85755 if( c=='3' ) return pOp->p3;
85756 if( c=='4' ) return pOp->p4.i;
85757 return pOp->p5;
85768 ** "PX" -> "r[X]"
85769 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
85770 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
85771 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
85774 sqlite3 *db, /* Optional - Oom error reporting only */
85786 zOpName = sqlite3OpcodeName(pOp->opcode);
85802 if( pOp->zComment && pOp->zComment[0] ){
85803 sqlite3_str_appendall(&x, pOp->zComment);
85820 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
85823 sqlite3_context *pCtx = pOp->p4.pCtx;
85824 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
85826 }else if( pCtx->argc>1 ){
85827 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
85830 x.nChar -= 2;
85836 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
85845 if( !seenCom && pOp->zComment ){
85846 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
85848 }else if( pOp->zComment ){
85849 sqlite3_str_appendall(&x, pOp->zComment);
85865 switch( pExpr->op ){
85868 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
85871 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
85877 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
85881 if( pExpr->iColumn<0 ){
85884 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
85922 displayP4Expr(p, pExpr->pLeft);
85923 if( pExpr->pRight ){
85925 displayP4Expr(p, pExpr->pRight);
85943 switch( pOp->p4type ){
85946 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
85947 assert( pKeyInfo->aSortFlags!=0 );
85948 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
85949 for(j=0; j<pKeyInfo->nKeyField; j++){
85950 CollSeq *pColl = pKeyInfo->aColl[j];
85951 const char *zColl = pColl ? pColl->zName : "";
85954 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
85955 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
85963 displayP4Expr(&x, pOp->p4.pExpr);
85969 CollSeq *pColl = pOp->p4.pColl;
85970 assert( pColl->enc<4 );
85971 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
85972 encnames[pColl->enc]);
85976 FuncDef *pDef = pOp->p4.pFunc;
85977 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
85981 FuncDef *pDef = pOp->p4.pCtx->pFunc;
85982 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
85986 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
85990 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
85994 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
85998 Mem *pMem = pOp->p4.pMem;
85999 if( pMem->flags & MEM_Str ){
86000 zP4 = pMem->z;
86001 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
86002 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
86003 }else if( pMem->flags & MEM_Real ){
86004 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
86005 }else if( pMem->flags & MEM_Null ){
86008 assert( pMem->flags & MEM_Blob );
86015 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
86022 u32 *ai = pOp->p4.ai;
86036 zP4 = pOp->p4.pTab->zName;
86040 zP4 = pOp->p4.z;
86052 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
86056 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
86057 ** p->btreeMask of databases that will require a lock.
86060 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
86061 assert( i<(int)sizeof(p->btreeMask)*8 );
86062 DbMaskSet(p->btreeMask, i);
86063 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
86064 DbMaskSet(p->lockMask, i);
86070 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
86074 ** that the correct busy-handler callback is invoked if required.
86076 ** If SQLite is not threadsafe but does support shared-cache mode, then
86081 ** If SQLite is not threadsafe and does not support shared-cache mode, this
86082 ** function is a no-op.
86084 ** The p->btreeMask field is a bitmask of all btrees that the prepared
86085 ** statement p will ever use. Let N be the number of bits in p->btreeMask
86095 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86096 db = p->db;
86097 aDb = db->aDb;
86098 nDb = db->nDb;
86100 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86116 db = p->db;
86117 aDb = db->aDb;
86118 nDb = db->nDb;
86120 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
86126 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
86139 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
86153 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
86154 zP4 ? zP4 : "", pOp->p5,
86167 ** This is a high-runner, so only those fields that really do need to
86182 p->flags = flags;
86183 p->db = db;
86184 p->szMalloc = 0;
86186 p->pScopyFrom = 0;
86189 }while( (--N)>0 );
86204 sqlite3 *db = p->db;
86205 if( db->pnBytesFreed ){
86207 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
86221 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
86222 ** sqlite3MemRelease() were called from here. With -O2, this jumps
86227 testcase( p->flags & MEM_Agg );
86228 testcase( p->flags & MEM_Dyn );
86229 if( p->flags&(MEM_Agg|MEM_Dyn) ){
86230 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
86232 p->flags = MEM_Undefined;
86233 }else if( p->szMalloc ){
86234 sqlite3DbNNFreeNN(db, p->zMalloc);
86235 p->szMalloc = 0;
86236 p->flags = MEM_Undefined;
86240 p->flags = MEM_Undefined;
86255 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
86271 pFrame->pParent = pFrame->v->pDelFrame;
86272 pFrame->v->pDelFrame = pFrame;
86292 int nSub = 0; /* Number of sub-vdbes seen so far */
86293 SubProgram **apSub = 0; /* Array of sub-vdbes */
86304 ** encountered, but p->pc will eventually catch up to nRow.
86306 nRow = p->nOp;
86308 if( pSub->flags&MEM_Blob ){
86311 nSub = pSub->n/sizeof(Vdbe*);
86312 apSub = (SubProgram **)pSub->z;
86315 nRow += apSub[i]->nOp;
86322 p->rc = SQLITE_OK;
86326 if( i<p->nOp ){
86329 aOp = p->aOp;
86334 i -= p->nOp;
86337 for(j=0; i>=apSub[j]->nOp; j++){
86338 i -= apSub[j]->nOp;
86339 assert( i<apSub[j]->nOp || j+1<nSub );
86341 aOp = apSub[j]->aOp;
86346 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
86356 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
86357 if( p->rc!=SQLITE_OK ){
86361 apSub = (SubProgram **)pSub->z;
86364 pSub->n = nSub*sizeof(SubProgram*);
86365 nRow += aOp[i].p4.pProgram->nOp;
86372 if( pOp->opcode==OP_OpenRead ) break;
86373 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
86374 if( pOp->opcode==OP_ReopenIdx ) break;
86398 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
86400 for(i=0; i<p->nChildCsr; i++){
86401 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
86403 releaseMemArray(aMem, p->nChildMem);
86404 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
86405 sqlite3DbFree(p->v->db, p);
86416 ** When p->explain==1, each instruction is listed. When
86417 ** p->explain==2, only OP_Explain instructions are listed and these
86418 ** are shown in a different format. p->explain==2 is used to implement
86420 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
86424 ** When p->explain==1, first the main program is listed, then each of
86431 sqlite3 *db = p->db; /* The database connection */
86434 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
86435 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
86439 assert( p->explain );
86440 assert( p->eVdbeState==VDBE_RUN_STATE );
86441 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
86445 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
86449 if( p->rc==SQLITE_NOMEM ){
86461 assert( p->nMem>9 );
86462 pSub = &p->aMem[9];
86468 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
86472 if( AtomicLoad(&db->u1.isInterrupted) ){
86473 p->rc = SQLITE_INTERRUPT;
86475 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
86478 if( p->explain==2 ){
86479 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
86480 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
86481 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
86482 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
86483 assert( p->nResColumn==4 );
86486 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
86487 -1, SQLITE_UTF8, SQLITE_STATIC);
86488 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
86489 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
86490 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
86492 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
86496 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
86501 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
86502 assert( p->nResColumn==8 );
86504 p->pResultRow = pMem;
86505 if( db->mallocFailed ){
86506 p->rc = SQLITE_NOMEM;
86509 p->rc = SQLITE_OK;
86524 if( p->zSql ){
86525 z = p->zSql;
86526 }else if( p->nOp>=1 ){
86527 const VdbeOp *pOp = &p->aOp[0];
86528 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
86529 z = pOp->p4.z;
86542 int nOp = p->nOp;
86546 pOp = &p->aOp[0];
86547 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
86550 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
86554 if( z[i-1]!=' ' ){
86577 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
86596 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
86599 if( nByte <= p->nFree ){
86600 p->nFree -= nByte;
86601 pBuf = &p->pSpace[p->nFree];
86603 p->nNeeded += nByte;
86619 assert( p->eVdbeState==VDBE_INIT_STATE
86620 || p->eVdbeState==VDBE_READY_STATE
86621 || p->eVdbeState==VDBE_HALT_STATE );
86625 assert( p->nOp>0 );
86627 p->eVdbeState = VDBE_READY_STATE;
86630 for(i=0; i<p->nMem; i++){
86631 assert( p->aMem[i].db==p->db );
86634 p->pc = -1;
86635 p->rc = SQLITE_OK;
86636 p->errorAction = OE_Abort;
86637 p->nChange = 0;
86638 p->cacheCtr = 1;
86639 p->minWriteFileFormat = 255;
86640 p->iStatement = 0;
86641 p->nFkConstraint = 0;
86643 for(i=0; i<p->nOp; i++){
86644 p->aOp[i].nExec = 0;
86645 p->aOp[i].nCycle = 0;
86681 assert( p->nOp>0 );
86683 assert( p->eVdbeState==VDBE_INIT_STATE );
86684 assert( pParse==p->pParse );
86685 p->pVList = pParse->pVList;
86686 pParse->pVList = 0;
86687 db = p->db;
86688 assert( db->mallocFailed==0 );
86689 nVar = pParse->nVar;
86690 nMem = pParse->nMem;
86691 nCursor = pParse->nTab;
86692 nArg = pParse->nMaxArg;
86706 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
86707 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
86709 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
86714 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
86715 if( pParse->explain ){
86717 p->explain = pParse->explain;
86718 p->nResColumn = 12 - 4*p->explain;
86720 p->expired = 0;
86728 ** This two-pass approach that reuses as much memory as possible from
86733 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
86734 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
86735 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
86736 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
86738 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
86740 if( !db->mallocFailed ){
86741 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
86742 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
86743 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
86744 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
86748 if( db->mallocFailed ){
86749 p->nVar = 0;
86750 p->nCursor = 0;
86751 p->nMem = 0;
86753 p->nCursor = nCursor;
86754 p->nVar = (ynVar)nVar;
86755 initMemArray(p->aVar, nVar, db, MEM_Null);
86756 p->nMem = nMem;
86757 initMemArray(p->aMem, nMem, db, MEM_Undefined);
86758 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
86771 VdbeTxtBlbCache *pCache = pCx->pCache;
86772 assert( pCx->colCache );
86773 pCx->colCache = 0;
86774 pCx->pCache = 0;
86775 if( pCache->pCValue ){
86776 sqlite3RCStrUnref(pCache->pCValue);
86777 pCache->pCValue = 0;
86779 sqlite3DbFree(p->db, pCache);
86783 if( pCx->colCache ){
86787 switch( pCx->eCurType ){
86789 sqlite3VdbeSorterClose(p->db, pCx);
86793 assert( pCx->uc.pCursor!=0 );
86794 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
86799 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
86800 const sqlite3_module *pModule = pVCur->pVtab->pModule;
86801 assert( pVCur->pVtab->nRef>0 );
86802 pVCur->pVtab->nRef--;
86803 pModule->xClose(pVCur);
86815 for(i=0; i<p->nCursor; i++){
86816 VdbeCursor *pC = p->apCsr[i];
86819 p->apCsr[i] = 0;
86826 ** is used, for example, when a trigger sub-program is halted to restore
86830 Vdbe *v = pFrame->v;
86832 v->aOp = pFrame->aOp;
86833 v->nOp = pFrame->nOp;
86834 v->aMem = pFrame->aMem;
86835 v->nMem = pFrame->nMem;
86836 v->apCsr = pFrame->apCsr;
86837 v->nCursor = pFrame->nCursor;
86838 v->db->lastRowid = pFrame->lastRowid;
86839 v->nChange = pFrame->nChange;
86840 v->db->nChange = pFrame->nDbChange;
86841 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
86842 v->pAuxData = pFrame->pAuxData;
86843 pFrame->pAuxData = 0;
86844 return pFrame->pc;
86856 if( p->pFrame ){
86858 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
86860 p->pFrame = 0;
86861 p->nFrame = 0;
86863 assert( p->nFrame==0 );
86865 releaseMemArray(p->aMem, p->nMem);
86866 while( p->pDelFrame ){
86867 VdbeFrame *pDel = p->pDelFrame;
86868 p->pDelFrame = pDel->pParent;
86873 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
86874 assert( p->pAuxData==0 );
86885 sqlite3 *db = p->db;
86887 if( p->nResAlloc ){
86888 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
86889 sqlite3DbFree(db, p->aColName);
86892 p->nResColumn = p->nResAlloc = (u16)nResColumn;
86893 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
86894 if( p->aColName==0 ) return;
86895 initMemArray(p->aColName, n, db, MEM_Null);
86917 assert( idx<p->nResAlloc );
86919 if( p->db->mallocFailed ){
86923 assert( p->aColName!=0 );
86924 pColName = &(p->aColName[idx+var*p->nResAlloc]);
86925 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
86926 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
86933 ** write-transaction spanning more than one database file, this routine
86934 ** takes care of the super-journal trickery.
86938 int nTrans = 0; /* Number of databases with an active write-transaction
86939 ** that are candidates for a two-phase commit using a
86940 ** super-journal */
86953 ** be done before determining whether a super-journal file is
86962 ** one database file has an open write transaction, a super-journal
86965 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86966 Btree *pBt = db->aDb[i].pBt;
86968 /* Whether or not a database might need a super-journal depends upon
86970 ** journal modes use a super-journal and which do not */
86983 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
86998 /* If there are any write-transactions at all, invoke the commit hook */
86999 if( needXcommit && db->xCommitCallback ){
87000 rc = db->xCommitCallback(db->pCommitArg);
87006 /* The simple case - no more than one database file (not counting the
87008 ** super-journal.
87012 ** that case we do not support atomic multi-file commits, so use the
87015 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
87018 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87019 Btree *pBt = db->aDb[i].pBt;
87030 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87031 Btree *pBt = db->aDb[i].pBt;
87041 /* The complex case - There is a multi-file write-transaction active.
87042 ** This requires a super-journal file to ensure the transaction is
87047 sqlite3_vfs *pVfs = db->pVfs;
87048 char *zSuper = 0; /* File-name for the super-journal */
87049 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
87056 /* Select a super-journal file name */
87074 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
87076 /* The antipenultimate character of the super-journal name must
87078 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
87083 /* Open the super-journal. */
87090 sqlite3DbFree(db, zSuper-4);
87095 ** super-journal file. If an error occurs at this point close
87096 ** and delete the super-journal file. All the individual journal files
87097 ** still have 'null' as the super-journal pointer, so they will roll
87100 for(i=0; i<db->nDb; i++){
87101 Btree *pBt = db->aDb[i].pBt;
87113 sqlite3DbFree(db, zSuper-4);
87119 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
87127 sqlite3DbFree(db, zSuper-4);
87132 ** sets the super-journal pointer in each individual journal. If
87133 ** an error occurs here, do not delete the super-journal file.
87137 ** super-journal file will be orphaned. But we cannot delete it,
87138 ** in case the super-journal file name was written into the journal
87141 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
87142 Btree *pBt = db->aDb[i].pBt;
87150 sqlite3DbFree(db, zSuper-4);
87154 /* Delete the super-journal file. This commits the transaction. After
87159 sqlite3DbFree(db, zSuper-4);
87174 for(i=0; i<db->nDb; i++){
87175 Btree *pBt = db->aDb[i].pBt;
87194 ** This is an internal self-check only - it is not an essential processing
87197 ** This is a no-op if NDEBUG is defined.
87205 p = db->pVdbe;
87209 if( p->readOnly==0 ) nWrite++;
87210 if( p->bIsReader ) nRead++;
87212 p = p->pVNext;
87214 assert( cnt==db->nVdbeActive );
87215 assert( nWrite==db->nVdbeWrite );
87216 assert( nRead==db->nVdbeRead );
87223 ** If the Vdbe passed as the first argument opened a statement-transaction,
87233 sqlite3 *const db = p->db;
87236 const int iSavepoint = p->iStatement-1;
87239 assert( db->nStatement>0 );
87240 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
87242 for(i=0; i<db->nDb; i++){
87244 Btree *pBt = db->aDb[i].pBt;
87257 db->nStatement--;
87258 p->iStatement = 0;
87273 db->nDeferredCons = p->nStmtDefCons;
87274 db->nDeferredImmCons = p->nStmtDefImmCons;
87279 if( p->db->nStatement && p->iStatement ){
87298 sqlite3 *db = p->db;
87299 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
87300 || (!deferred && p->nFkConstraint>0)
87302 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
87303 p->errorAction = OE_Abort;
87305 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
87327 sqlite3 *db = p->db;
87345 assert( p->eVdbeState==VDBE_RUN_STATE );
87346 if( db->mallocFailed ){
87347 p->rc = SQLITE_NOMEM_BKPT;
87354 if( p->bIsReader ){
87355 int mrc; /* Primary error code from p->rc */
87363 if( p->rc ){
87364 mrc = p->rc & 0xff;
87373 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
87378 ** Even if the statement is read-only, it is important to perform
87380 ** occurred while writing to the journal, sub-journal or database
87385 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
87386 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
87394 db->autoCommit = 1;
87395 p->nChange = 0;
87401 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
87405 /* If the auto-commit flag is set and this is the only active writer
87412 && db->autoCommit
87413 && db->nVdbeWrite==(p->readOnly==0)
87415 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
87418 if( NEVER(p->readOnly) ){
87423 }else if( db->flags & SQLITE_CorruptRdOnly ){
87425 db->flags &= ~SQLITE_CorruptRdOnly;
87427 /* The auto-commit flag is true, the vdbe program was successful
87433 if( rc==SQLITE_BUSY && p->readOnly ){
87438 p->rc = rc;
87440 p->nChange = 0;
87442 db->nDeferredCons = 0;
87443 db->nDeferredImmCons = 0;
87444 db->flags &= ~(u64)SQLITE_DeferFKs;
87447 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
87448 p->nChange = 0;
87451 p->nChange = 0;
87453 db->nStatement = 0;
87455 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
87457 }else if( p->errorAction==OE_Abort ){
87462 db->autoCommit = 1;
87463 p->nChange = 0;
87467 /* If eStatementOp is non-zero, then a statement transaction needs to
87476 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
87477 p->rc = rc;
87478 sqlite3DbFree(db, p->zErrMsg);
87479 p->zErrMsg = 0;
87483 db->autoCommit = 1;
87484 p->nChange = 0;
87489 ** has been rolled back, update the database connection change-counter.
87491 if( p->changeCntOn ){
87493 sqlite3VdbeSetChanges(db, p->nChange);
87497 p->nChange = 0;
87505 db->nVdbeActive--;
87506 if( !p->readOnly ) db->nVdbeWrite--;
87507 if( p->bIsReader ) db->nVdbeRead--;
87508 assert( db->nVdbeActive>=db->nVdbeRead );
87509 assert( db->nVdbeRead>=db->nVdbeWrite );
87510 assert( db->nVdbeWrite>=0 );
87511 p->eVdbeState = VDBE_HALT_STATE;
87513 if( db->mallocFailed ){
87514 p->rc = SQLITE_NOMEM_BKPT;
87517 /* If the auto-commit flag is set to true, then any locks that were held
87519 ** to invoke any required unlock-notify callbacks.
87521 if( db->autoCommit ){
87525 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
87526 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
87532 ** in p->rc. This routine sets that result back to SQLITE_OK.
87535 p->rc = SQLITE_OK;
87547 sqlite3 *db = p->db;
87548 int rc = p->rc;
87549 if( p->zErrMsg ){
87550 db->bBenignMalloc++;
87552 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
87553 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
87555 db->bBenignMalloc--;
87556 }else if( db->pErr ){
87557 sqlite3ValueSetNull(db->pErr);
87559 db->errCode = rc;
87560 db->errByteOffset = -1;
87570 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
87571 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
87572 assert( v->db->init.busy==0 );
87575 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
87577 sqlite3DbFree(v->db, zExpanded);
87602 db = p->db;
87608 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
87615 if( p->pc>=0 ){
87617 if( db->pErr || p->zErrMsg ){
87620 db->errCode = p->rc;
87629 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
87630 if( p->aMem ){
87631 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
87634 if( p->zErrMsg ){
87635 sqlite3DbFree(db, p->zErrMsg);
87636 p->zErrMsg = 0;
87638 p->pResultRow = 0;
87640 p->nWrite = 0;
87649 fprintf(out, "---- ");
87650 for(i=0; i<p->nOp; i++){
87651 fprintf(out, "%02x", p->aOp[i].opcode);
87654 if( p->zSql ){
87656 fprintf(out, "-- ");
87657 for(i=0; (c = p->zSql[i])!=0; i++){
87658 if( pc=='\n' ) fprintf(out, "-- ");
87664 for(i=0; i<p->nOp; i++){
87666 i64 cnt = p->aOp[i].nExec;
87667 i64 cycles = p->aOp[i].nCycle;
87674 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
87680 return p->rc & db->errMask;
87692 if( p->eVdbeState>=VDBE_READY_STATE ){
87694 assert( (rc & p->db->errMask)==rc );
87720 || (pAux->iAuxOp==iOp
87721 && pAux->iAuxArg>=0
87722 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
87724 testcase( pAux->iAuxArg==31 );
87725 if( pAux->xDeleteAux ){
87726 pAux->xDeleteAux(pAux->pAux);
87728 *pp = pAux->pNextAux;
87731 pp= &pAux->pNextAux;
87747 assert( p->db==0 || p->db==db );
87748 if( p->aColName ){
87749 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
87750 sqlite3DbNNFreeNN(db, p->aColName);
87752 for(pSub=p->pProgram; pSub; pSub=pNext){
87753 pNext = pSub->pNext;
87754 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
87757 if( p->eVdbeState!=VDBE_INIT_STATE ){
87758 releaseMemArray(p->aVar, p->nVar);
87759 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
87760 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
87762 vdbeFreeOpArray(db, p->aOp, p->nOp);
87763 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
87765 sqlite3DbFree(db, p->zNormSql);
87768 for(pThis=p->pDblStr; pThis; pThis=pNxt){
87769 pNxt = pThis->pNextStr;
87777 for(i=0; i<p->nScan; i++){
87778 sqlite3DbFree(db, p->aScan[i].zName);
87780 sqlite3DbFree(db, p->aScan);
87792 db = p->db;
87794 assert( sqlite3_mutex_held(db->mutex) );
87796 if( db->pnBytesFreed==0 ){
87797 assert( p->ppVPrev!=0 );
87798 *p->ppVPrev = p->pVNext;
87799 if( p->pVNext ){
87800 p->pVNext->ppVPrev = p->ppVPrev;
87816 assert( p->deferredMoveto );
87817 assert( p->isTable );
87818 assert( p->eCurType==CURTYPE_BTREE );
87819 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
87825 p->deferredMoveto = 0;
87826 p->cacheStatus = CACHE_STALE;
87839 assert( p->eCurType==CURTYPE_BTREE );
87840 assert( p->uc.pCursor!=0 );
87841 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
87842 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
87843 p->cacheStatus = CACHE_STALE;
87844 if( isDifferentRow ) p->nullRow = 1;
87853 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
87854 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
87866 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
87871 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
87878 ** serial-type and data blob separately.
87883 ** -------------- --------------- ---------------
87895 ** N>=12 and even (N-12)/2 BLOB
87896 ** N>=13 and odd (N-13)/2 text
87904 ** Return the serial-type for the value stored in pMem.
87908 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
87909 ** opcode in the byte-code engine. But by moving this routine in-line, we
87915 int flags = pMem->flags;
87925 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
87926 i64 i = pMem->u.i;
87951 ** as an integer, then we might as well make it an 8-byte floating
87953 pMem->u.r = (double)pMem->u.i;
87954 pMem->flags &= ~MEM_IntReal;
87955 pMem->flags |= MEM_Real;
87964 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
87965 assert( pMem->n>=0 );
87966 n = (u32)pMem->n;
87968 n += pMem->u.nZero;
87996 ** Return the length of the data corresponding to the supplied serial-type.
88000 return (serial_type-12)/2;
88003 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
88013 ** If we are on an architecture with mixed-endian floating
88017 ** For most architectures, this is a no-op.
88019 ** (later): It is reported to me that the mixed-endian problem
88021 ** that early versions of GCC stored the two words of a 64-bit
88029 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
88033 ** (2007-08-30) Frank van Vugt has studied this problem closely
88036 ** emulation that uses only 32-bit mantissas instead of a full
88037 ** 48-bits as required by the IEEE standard. (This is the
88040 ** the necessary byte swapping is carried out using a 64-bit integer
88041 ** rather than a 64-bit float. Frank assures us that the code here
88064 ** big-endian integer. Return the equivalent native integer
88090 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
88091 ** twos-complement integer. */
88092 pMem->u.i = *(i64*)&x;
88093 pMem->flags = MEM_Int;
88094 testcase( pMem->u.i<0 );
88096 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
88101 ** defined that 64-bit floating point values really are mixed
88110 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88112 memcpy(&pMem->u.r, &x, sizeof(x));
88113 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
88123 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
88125 memcpy(&pMem->u.r, &x, sizeof(x));
88127 pMem->flags = MEM_Null;
88130 pMem->flags = MEM_Real;
88140 ** UPDATE no-change flag set */
88141 pMem->flags = MEM_Null|MEM_Zero;
88142 pMem->n = 0;
88143 pMem->u.nZero = 0;
88148 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
88149 pMem->flags = MEM_Null;
88153 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
88155 pMem->u.i = ONE_BYTE_INT(buf);
88156 pMem->flags = MEM_Int;
88157 testcase( pMem->u.i<0 );
88160 case 2: { /* 2-byte signed integer */
88161 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
88162 ** twos-complement integer. */
88163 pMem->u.i = TWO_BYTE_INT(buf);
88164 pMem->flags = MEM_Int;
88165 testcase( pMem->u.i<0 );
88168 case 3: { /* 3-byte signed integer */
88169 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
88170 ** twos-complement integer. */
88171 pMem->u.i = THREE_BYTE_INT(buf);
88172 pMem->flags = MEM_Int;
88173 testcase( pMem->u.i<0 );
88176 case 4: { /* 4-byte signed integer */
88177 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
88178 ** twos-complement integer. */
88179 pMem->u.i = FOUR_BYTE_INT(buf);
88181 /* Work around a sign-extension bug in the HP compiler for HP/UX */
88182 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
88184 pMem->flags = MEM_Int;
88185 testcase( pMem->u.i<0 );
88188 case 5: { /* 6-byte signed integer */
88189 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
88190 ** twos-complement integer. */
88191 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
88192 pMem->flags = MEM_Int;
88193 testcase( pMem->u.i<0 );
88196 case 6: /* 8-byte signed integer */
88205 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
88206 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
88207 pMem->u.i = serial_type-8;
88208 pMem->flags = MEM_Int;
88212 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
88214 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
88215 ** (N-13)/2 bytes in length. */
88217 pMem->z = (char *)buf;
88218 pMem->n = (serial_type-12)/2;
88219 pMem->flags = aFlag[serial_type&1];
88244 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
88245 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
88247 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
88248 assert( pKeyInfo->aSortFlags!=0 );
88249 p->pKeyInfo = pKeyInfo;
88250 p->nField = pKeyInfo->nKeyField + 1;
88255 ** Given the nKey-byte encoding of a record in pKey[], populate the
88270 Mem *pMem = p->aMem;
88272 p->default_rc = 0;
88281 pMem->enc = pKeyInfo->enc;
88282 pMem->db = pKeyInfo->db;
88283 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
88284 pMem->szMalloc = 0;
88285 pMem->z = 0;
88289 if( (++u)>=p->nField ) break;
88296 sqlite3VdbeMemSetNull(pMem-1);
88298 assert( u<=pKeyInfo->nKeyField + 1 );
88299 p->nField = u;
88328 pKeyInfo = pPKey2->pKeyInfo;
88329 if( pKeyInfo->db==0 ) return 1;
88330 mem1.enc = pKeyInfo->enc;
88331 mem1.db = pKeyInfo->db;
88347 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
88348 assert( pKeyInfo->aSortFlags!=0 );
88349 assert( pKeyInfo->nKeyField>0 );
88385 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
88386 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
88389 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
88390 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
88392 rc = -rc;
88394 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
88395 rc = -rc; /* Invert the result for DESC sort order. */
88400 }while( idx1<szHdr1 && i<pPKey2->nField );
88411 rc = pPKey2->default_rc;
88418 if( pKeyInfo->db->mallocFailed ) return 1;
88427 ** limit given by pKeyInfo->nAllField.
88429 ** If this constraint is not satisfied, it means that the high-speed
88453 assert( nField <= pKeyInfo->nAllField );
88463 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
88471 if( pMem1->enc==pColl->enc ){
88474 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
88480 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
88481 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
88484 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
88485 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
88490 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
88500 ** with MEM_Zero. Return true if it could be a zero-blob.
88517 int n1 = pB1->n;
88518 int n2 = pB2->n;
88520 /* It is possible to have a Blob value that has some non-zero content
88524 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
88525 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
88527 if( (pB1->flags|pB2->flags) & MEM_Zero ){
88528 if( pB1->flags & pB2->flags & MEM_Zero ){
88529 return pB1->u.nZero - pB2->u.nZero;
88530 }else if( pB1->flags & MEM_Zero ){
88531 if( !isAllZero(pB2->z, pB2->n) ) return -1;
88532 return pB1->u.nZero - n2;
88534 if( !isAllZero(pB1->z, pB1->n) ) return +1;
88535 return n1 - pB2->u.nZero;
88538 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
88540 return n1 - n2;
88554 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
88569 return (x<r) ? -1 : (x>r);
88573 if( r<-9223372036854775808.0 ) return +1;
88574 if( r>=9223372036854775808.0 ) return -1;
88576 if( i<y ) return -1;
88582 return (s<r) ? -1 : (s>r);
88599 f1 = pMem1->flags;
88600 f2 = pMem2->flags;
88608 return (f2&MEM_Null) - (f1&MEM_Null);
88620 if( pMem1->u.i < pMem2->u.i ) return -1;
88621 if( pMem1->u.i > pMem2->u.i ) return +1;
88625 if( pMem1->u.r < pMem2->u.r ) return -1;
88626 if( pMem1->u.r > pMem2->u.r ) return +1;
88633 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
88635 if( pMem1->u.i < pMem2->u.i ) return -1;
88636 if( pMem1->u.i > pMem2->u.i ) return +1;
88639 return -1;
88646 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
88648 return -1;
88662 return -1;
88665 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
88666 assert( pMem1->enc==SQLITE_UTF8 ||
88667 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
88673 assert( !pColl || pColl->xCmp );
88688 ** The first argument passed to this function is a serial-type that
88689 ** corresponds to an integer - all values between 1 and 9 inclusive
88725 return (serial_type - 8);
88737 ** If argument bSkip is non-zero, it is assumed that the caller has already
88741 ** fields that appear in both keys are equal, then pPKey2->default_rc is
88744 ** If database corruption is discovered, set pPKey2->errCode to
88746 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
88747 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
88759 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
88788 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88793 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
88795 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
88796 assert( pPKey2->pKeyInfo->nKeyField>0 );
88798 while( 1 /*exit-by-break*/ ){
88802 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
88803 testcase( pRhs->flags & MEM_Int );
88804 testcase( pRhs->flags & MEM_IntReal );
88808 rc = serial_type==10 ? -1 : +1;
88810 rc = -1;
88813 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
88816 i64 rhs = pRhs->u.i;
88818 rc = -1;
88826 else if( pRhs->flags & MEM_Real ){
88833 rc = serial_type==10 ? -1 : +1;
88835 rc = -1;
88839 rc = -1; /* mem1 is a NaN */
88840 }else if( mem1.u.r<pRhs->u.r ){
88841 rc = -1;
88842 }else if( mem1.u.r>pRhs->u.r ){
88849 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
88855 else if( pRhs->flags & MEM_Str ){
88859 rc = -1;
88863 mem1.n = (serial_type - 12) / 2;
88867 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
88869 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88871 }else if( pKeyInfo->aColl[i] ){
88872 mem1.enc = pKeyInfo->enc;
88873 mem1.db = pKeyInfo->db;
88877 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
88880 int nCmp = MIN(mem1.n, pRhs->n);
88881 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
88882 if( rc==0 ) rc = mem1.n - pRhs->n;
88888 else if( pRhs->flags & MEM_Blob ){
88889 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
88893 rc = -1;
88895 int nStr = (serial_type - 12) / 2;
88899 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88901 }else if( pRhs->flags & MEM_Zero ){
88905 rc = nStr - pRhs->u.nZero;
88908 int nCmp = MIN(nStr, pRhs->n);
88909 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
88910 if( rc==0 ) rc = nStr - pRhs->n;
88929 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
88933 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
88935 rc = -rc;
88944 if( i==pPKey2->nField ) break;
88950 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88964 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
88965 || pPKey2->pKeyInfo->db->mallocFailed
88967 pPKey2->eqSeen = 1;
88968 return pPKey2->default_rc;
88981 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
88999 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89002 case 1: { /* 1-byte signed integer */
89007 case 2: { /* 2-byte signed integer */
89012 case 3: { /* 3-byte signed integer */
89017 case 4: { /* 4-byte signed integer */
89023 case 5: { /* 6-byte signed integer */
89028 case 6: { /* 8-byte signed integer */
89055 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
89056 v = pPKey2->u.i;
89058 res = pPKey2->r1;
89060 res = pPKey2->r2;
89061 }else if( pPKey2->nField>1 ){
89067 ** fields. Return pPKey2->default_rc in this case. */
89068 res = pPKey2->default_rc;
89069 pPKey2->eqSeen = 1;
89079 ** uses the collation sequence BINARY and (c) that the size-of-header varint
89090 assert( pPKey2->aMem[0].flags & MEM_Str );
89091 assert( pPKey2->aMem[0].n == pPKey2->n );
89092 assert( pPKey2->aMem[0].z == pPKey2->u.z );
89093 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
89103 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
89105 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
89111 nStr = (serial_type-12) / 2;
89113 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
89116 nCmp = MIN( pPKey2->n, nStr );
89117 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
89120 res = pPKey2->r2;
89122 res = pPKey2->r1;
89124 res = nStr - pPKey2->n;
89126 if( pPKey2->nField>1 ){
89129 res = pPKey2->default_rc;
89130 pPKey2->eqSeen = 1;
89133 res = pPKey2->r2;
89135 res = pPKey2->r1;
89142 || pPKey2->pKeyInfo->db->mallocFailed
89154 ** that the size-of-header varint that occurs at the start of each record
89166 if( p->pKeyInfo->nAllField<=13 ){
89167 int flags = p->aMem[0].flags;
89168 if( p->pKeyInfo->aSortFlags[0] ){
89169 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
89172 p->r1 = 1;
89173 p->r2 = -1;
89175 p->r1 = -1;
89176 p->r2 = 1;
89179 p->u.i = p->aMem[0].u.i;
89186 && p->pKeyInfo->aColl[0]==0
89189 p->u.z = p->aMem[0].z;
89190 p->n = p->aMem[0].n;
89215 ** than 2GiB are support - anything large must be database corruption.
89217 ** this code can safely assume that nCellKey is 32-bits
89240 /* The last field of the index should be an integer - the ROWID.
89242 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
89261 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
89296 assert( pC->eCurType==CURTYPE_BTREE );
89297 pCur = pC->uc.pCursor;
89321 assert( sqlite3_mutex_held(db->mutex) );
89322 db->nChange = nChange;
89323 db->nTotalChange += nChange;
89331 v->changeCntOn = 1;
89340 ** programs obsolete. Removing user-defined functions or collating
89354 for(p = db->pVdbe; p; p=p->pVNext){
89355 p->expired = iCode+1;
89363 return v->db;
89370 return v->prepFlags;
89384 Mem *pMem = &v->aVar[iVar-1];
89385 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
89386 if( 0==(pMem->flags & MEM_Null) ){
89387 sqlite3_value *pRet = sqlite3ValueNew(v->db);
89400 ** to sqlite3_reoptimize() that re-preparing the statement may result
89405 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
89407 v->expmask |= 0x80000000;
89409 v->expmask |= ((u32)1 << (iVar-1));
89418 ** throw an error if it is given inputs that would make it non-deterministic.
89419 ** This routine is invoked by date/time functions that use non-deterministic
89425 if( pCtx->pVdbe==0 ) return 1;
89427 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
89428 if( pOp->opcode==OP_PureFunc ){
89431 if( pOp->p5 & NC_IsCheck ){
89433 }else if( pOp->p5 & NC_GenCol ){
89438 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
89439 pCtx->pFunc->zName, zContext);
89440 sqlite3_result_error(pCtx, zMsg, -1);
89451 ** byte-code register values correctly initialized.
89454 if( pExpr->op==TK_REGISTER ){
89455 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
89468 if( pVtab->zErrMsg ){
89469 sqlite3 *db = p->db;
89470 sqlite3DbFree(db, p->zErrMsg);
89471 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
89472 sqlite3_free(pVtab->zErrMsg);
89473 pVtab->zErrMsg = 0;
89482 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
89493 Mem *pMem = &p->aMem[i];
89494 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
89503 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
89509 Vdbe *v, /* Vdbe pre-update hook is invoked by */
89518 sqlite3 *db = v->db;
89521 const char *zTbl = pTab->zName;
89525 if( pTab->tabFlags & TF_WithoutRowid ){
89526 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
89527 }else if( pTab->tabFlags & TF_HasVirtual ){
89528 nRealCol = pTab->nNVCol;
89530 nRealCol = pTab->nCol;
89534 assert( db->pPreUpdate==0 );
89541 iKey2 = v->aMem[iReg].u.i;
89548 assert( pCsr->eCurType==CURTYPE_BTREE );
89549 assert( pCsr->nField==nRealCol
89550 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
89559 preupdate.keyinfo.nKeyField = pTab->nCol;
89566 db->pPreUpdate = &preupdate;
89567 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
89568 db->pPreUpdate = 0;
89574 for(i=0; i<pCsr->nField; i++){
89605 ** Return TRUE (non-zero) of the statement supplied as an argument needs
89614 return p==0 || p->expired;
89624 if( p->db==0 ){
89648 assert( p->startTime>0 );
89649 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
89650 assert( db->init.busy==0 );
89651 assert( p->zSql!=0 );
89652 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
89653 iElapse = (iNow - p->startTime)*1000000;
89655 if( db->xProfile ){
89656 db->xProfile(db->pProfileArg, p->zSql, iElapse);
89659 if( db->mTrace & SQLITE_TRACE_PROFILE ){
89660 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
89662 p->startTime = 0;
89669 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
89671 # define checkProfileCallback(DB,P) /*no-op*/
89686 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
89687 ** pointer is a harmless no-op. */
89691 sqlite3 *db = v->db;
89693 sqlite3_mutex_enter(db->mutex);
89695 assert( v->eVdbeState>=VDBE_READY_STATE );
89718 sqlite3 *db = v->db;
89719 sqlite3_mutex_enter(db->mutex);
89723 assert( (rc & (db->errMask))==rc );
89725 sqlite3_mutex_leave(db->mutex);
89738 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
89741 for(i=0; i<p->nVar; i++){
89742 sqlite3VdbeMemRelease(&p->aVar[i]);
89743 p->aVar[i].flags = MEM_Null;
89745 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
89746 if( p->expmask ){
89747 p->expired = 1;
89760 if( p->flags & (MEM_Blob|MEM_Str) ){
89762 assert( p->flags==MEM_Null && p->z==0 );
89765 p->flags |= MEM_Blob;
89766 return p->n ? p->z : 0;
89788 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
89792 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
89795 && p->eSubtype=='p'
89796 && strcmp(p->u.zPType, zPType)==0
89798 return (void*)p->z;
89817 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
89818 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
89891 if( pVal->flags & MEM_Null ){
89893 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
89895 }else if( pVal->flags & MEM_Int ){
89897 }else if( pVal->flags & MEM_Str ){
89900 assert( eType == aType[pVal->flags&MEM_AffMask] );
89903 return aType[pVal->flags&MEM_AffMask];
89906 return pVal->enc;
89911 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
89916 return (pVal->flags&MEM_FromBind)!=0;
89928 pNew->flags &= ~MEM_Dyn;
89929 pNew->db = 0;
89930 if( pNew->flags&(MEM_Str|MEM_Blob) ){
89931 pNew->flags &= ~(MEM_Static|MEM_Dyn);
89932 pNew->flags |= MEM_Ephem;
89937 }else if( pNew->flags & MEM_Null ){
89939 pNew->flags &= ~(MEM_Term|MEM_Subtype);
89953 ** The following routines are used by user-defined functions to specify
89970 Mem *pOut = pCtx->pOut;
89983 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
90024 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90040 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90051 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90052 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
90058 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90059 pCtx->isError = SQLITE_ERROR;
90060 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
90067 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90068 pCtx->isError = SQLITE_ERROR;
90069 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
90076 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90077 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
90083 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90084 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
90090 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90091 sqlite3VdbeMemSetNull(pCtx->pOut);
90106 pOut = pCtx->pOut;
90107 assert( sqlite3_mutex_held(pOut->db->mutex) );
90109 pOut->flags = MEM_Null;
90118 if( pCtx->pFunc!=0
90119 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90124 pCtx->pFunc->zName);
90125 sqlite3_result_error(pCtx, zErr, -1);
90129 pOut = pCtx->pOut;
90130 assert( sqlite3_mutex_held(pOut->db->mutex) );
90131 pOut->eSubtype = eSubtype & 0xff;
90132 pOut->flags |= MEM_Subtype;
90146 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90162 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90172 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
90182 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90191 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90200 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90214 pOut = pCtx->pOut;
90215 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90217 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
90231 pOut = pCtx->pOut;
90232 assert( sqlite3_mutex_held(pOut->db->mutex) );
90233 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
90238 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
90241 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
90248 pCtx->isError = errCode ? errCode : -1;
90250 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
90252 if( pCtx->pOut->flags & MEM_Null ){
90253 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
90263 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90264 pCtx->isError = SQLITE_TOOBIG;
90265 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
90274 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90275 sqlite3VdbeMemSetNull(pCtx->pOut);
90276 pCtx->isError = SQLITE_NOMEM_BKPT;
90277 sqlite3OomFault(pCtx->pOut->db);
90283 ** test-control.
90286 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90287 if( pCtx->pOut->flags & MEM_Int ){
90288 pCtx->pOut->flags &= ~MEM_Int;
90289 pCtx->pOut->flags |= MEM_IntReal;
90303 for(i=0; i<db->nDb; i++){
90304 Btree *pBt = db->aDb[i].pBt;
90310 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
90311 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
90334 db = p->db;
90335 if( p->eVdbeState!=VDBE_RUN_STATE ){
90337 if( p->eVdbeState==VDBE_READY_STATE ){
90338 if( p->expired ){
90339 p->rc = SQLITE_SCHEMA;
90341 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
90343 ** error has occurred, then return the error code in p->rc to the
90356 if( db->nVdbeActive==0 ){
90357 AtomicStore(&db->u1.isInterrupted, 0);
90360 assert( db->nVdbeWrite>0 || db->autoCommit==0
90361 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
90365 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
90366 && !db->init.busy && p->zSql ){
90367 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
90369 assert( p->startTime==0 );
90373 db->nVdbeActive++;
90374 if( p->readOnly==0 ) db->nVdbeWrite++;
90375 if( p->bIsReader ) db->nVdbeRead++;
90376 p->pc = 0;
90377 p->eVdbeState = VDBE_RUN_STATE;
90380 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
90385 ** This "automatic-reset" change is not technically an incompatibility,
90391 ** returns, and those were broken by the automatic-reset change. As a
90392 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
90398 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
90406 assert( p->eVdbeState==VDBE_READY_STATE );
90412 p->rcApp = SQLITE_OK;
90415 if( p->explain ){
90420 db->nVdbeExec++;
90422 db->nVdbeExec--;
90426 assert( p->rc==SQLITE_OK );
90427 assert( db->mallocFailed==0 );
90428 db->errCode = SQLITE_ROW;
90435 p->pResultRow = 0;
90436 if( rc==SQLITE_DONE && db->autoCommit ){
90437 assert( p->rc==SQLITE_OK );
90438 p->rc = doWalCallbacks(db);
90439 if( p->rc!=SQLITE_OK ){
90442 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
90444 ** error has occurred, then return the error code in p->rc to the
90451 db->errCode = rc;
90452 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
90453 p->rc = SQLITE_NOMEM_BKPT;
90454 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
90459 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
90463 return (rc&db->errMask);
90467 ** This is the top-level implementation of sqlite3_step(). Call
90480 db = v->db;
90481 sqlite3_mutex_enter(db->mutex);
90484 int savedPc = v->pc;
90495 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
90496 sqlite3DbFree(db, v->zErrMsg);
90497 if( !db->mallocFailed ){
90498 v->zErrMsg = sqlite3DbStrDup(db, zErr);
90499 v->rc = rc = sqlite3ApiExit(db, rc);
90501 v->zErrMsg = 0;
90502 v->rc = rc = SQLITE_NOMEM_BKPT;
90511 ** SQLITE_SCHEMA. tag-20220401a */
90512 v->minWriteFileFormat = 254;
90514 assert( v->expired==0 );
90516 sqlite3_mutex_leave(db->mutex);
90529 assert( p && p->pFunc );
90531 return p->pFunc->pUserData;
90538 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
90548 assert( p && p->pOut );
90550 return p->pOut->db;
90564 ** performance by substituting a NULL result, or some other light-weight
90573 return sqlite3_value_nochange(p->pOut);
90601 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
90604 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
90606 assert( pVal->eSubtype=='p' );
90607 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
90608 pRhs = (ValueList*)pVal->z;
90611 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
90614 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
90615 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
90616 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
90622 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
90623 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
90627 sqlite3_value *pOut = pRhs->pOut;
90630 pOut->enc = ENC(pOut->db);
90631 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
90668 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
90669 assert( p->pVdbe!=0 );
90672 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
90675 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
90683 ** its pMem->z element.
90686 Mem *pMem = p->pMem;
90687 assert( (pMem->flags & MEM_Agg)==0 );
90690 pMem->z = 0;
90693 pMem->flags = MEM_Agg;
90694 pMem->u.pDef = p->pFunc;
90695 if( pMem->z ){
90696 memset(pMem->z, 0, nByte);
90699 return (void*)pMem->z;
90708 assert( p && p->pFunc && p->pFunc->xFinalize );
90709 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
90711 if( (p->pMem->flags & MEM_Agg)==0 ){
90714 return (void*)p->pMem->z;
90720 ** the user-function defined by pCtx.
90722 ** The left-most argument is 0.
90734 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90736 if( pCtx->pVdbe==0 ) return 0;
90738 assert( pCtx->pVdbe!=0 );
90740 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
90741 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
90742 return pAuxData->pAux;
90750 ** argument to the user-function defined by pCtx. Any previous value is
90753 ** The left-most argument is 0.
90771 pVdbe= pCtx->pVdbe;
90772 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
90779 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
90780 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
90785 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
90787 pAuxData->iAuxOp = pCtx->iOp;
90788 pAuxData->iAuxArg = iArg;
90789 pAuxData->pNextAux = pVdbe->pAuxData;
90790 pVdbe->pAuxData = pAuxData;
90791 if( pCtx->isError==0 ) pCtx->isError = -1;
90792 }else if( pAuxData->xDeleteAux ){
90793 pAuxData->xDeleteAux(pAuxData->pAux);
90796 pAuxData->pAux = pAux;
90797 pAuxData->xDeleteAux = xDelete;
90817 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
90818 return p->pMem->n;
90828 return pVm->nResColumn;
90837 if( pVm==0 || pVm->pResultRow==0 ) return 0;
90838 return pVm->nResColumn;
90847 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
90848 ** instead of an 8-byte one. This all works fine, except that when
90850 ** that a Mem structure is located on an 8-byte boundary. To prevent
90852 ** using gcc, we force nullMem to be 8-byte aligned using the magical
90890 assert( pVm->db );
90891 sqlite3_mutex_enter(pVm->db->mutex);
90892 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
90893 pOut = &pVm->pResultRow[i];
90895 sqlite3Error(pVm->db, SQLITE_RANGE);
90928 assert( p->db!=0 );
90929 assert( sqlite3_mutex_held(p->db->mutex) );
90930 p->rc = sqlite3ApiExit(p->db, p->rc);
90931 sqlite3_mutex_leave(p->db->mutex);
90981 if( pOut->flags&MEM_Static ){
90982 pOut->flags &= ~MEM_Static;
90983 pOut->flags |= MEM_Ephem;
91028 ** Convert the N-th element of pStmt->pColName[] into a string using
91062 db = p->db;
91064 sqlite3_mutex_enter(db->mutex);
91066 if( p->explain ){
91068 n = p->explain==1 ? 8 : 4;
91071 int i = iExplainColNames16[N + 8*p->explain - 8];
91074 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
91078 n = p->nResColumn;
91080 u8 prior_mallocFailed = db->mallocFailed;
91084 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
91088 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
91093 assert( db->mallocFailed==0 || db->mallocFailed==1 );
91094 if( db->mallocFailed > prior_mallocFailed ){
91100 sqlite3_mutex_leave(db->mutex);
91198 ** The error code stored in database p->db is overwritten with the return
91206 sqlite3_mutex_enter(p->db->mutex);
91207 if( p->eVdbeState!=VDBE_READY_STATE ){
91208 sqlite3Error(p->db, SQLITE_MISUSE_BKPT);
91209 sqlite3_mutex_leave(p->db->mutex);
91211 "bind on a busy prepared statement: [%s]", p->zSql);
91214 if( i>=(unsigned int)p->nVar ){
91215 sqlite3Error(p->db, SQLITE_RANGE);
91216 sqlite3_mutex_leave(p->db->mutex);
91219 pVar = &p->aVar[i];
91221 pVar->flags = MEM_Null;
91222 p->db->errCode = SQLITE_OK;
91227 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
91233 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
91234 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
91235 p->expired = 1;
91255 rc = vdbeUnbind(p, (u32)(i-1));
91258 pVar = &p->aVar[i-1];
91261 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
91264 sqlite3Error(p->db, rc);
91265 rc = sqlite3ApiExit(p->db, rc);
91268 sqlite3_mutex_leave(p->db->mutex);
91304 rc = vdbeUnbind(p, (u32)(i-1));
91306 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
91307 sqlite3_mutex_leave(p->db->mutex);
91317 rc = vdbeUnbind(p, (u32)(i-1));
91319 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
91320 sqlite3_mutex_leave(p->db->mutex);
91327 rc = vdbeUnbind(p, (u32)(i-1));
91329 sqlite3_mutex_leave(p->db->mutex);
91342 rc = vdbeUnbind(p, (u32)(i-1));
91344 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
91345 sqlite3_mutex_leave(p->db->mutex);
91390 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
91394 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
91396 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
91401 if( pValue->flags & MEM_Zero ){
91402 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
91404 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
91409 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
91410 pValue->enc);
91423 rc = vdbeUnbind(p, (u32)(i-1));
91426 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
91428 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
91430 sqlite3_mutex_leave(p->db->mutex);
91440 sqlite3_mutex_enter(p->db->mutex);
91441 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
91447 rc = sqlite3ApiExit(p->db, rc);
91448 sqlite3_mutex_leave(p->db->mutex);
91458 return p ? p->nVar : 0;
91465 ** The result is always UTF-8.
91470 return sqlite3VListNumToName(p->pVList, i);
91480 return sqlite3VListNameToNum(p->pVList, zName, nName);
91493 assert( pTo->db==pFrom->db );
91494 assert( pTo->nVar==pFrom->nVar );
91495 sqlite3_mutex_enter(pTo->db->mutex);
91496 for(i=0; i<pFrom->nVar; i++){
91497 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
91499 sqlite3_mutex_leave(pTo->db->mutex);
91519 if( pFrom->nVar!=pTo->nVar ){
91522 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
91523 if( pTo->expmask ){
91524 pTo->expired = 1;
91526 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
91527 if( pFrom->expmask ){
91528 pFrom->expired = 1;
91541 return pStmt ? ((Vdbe*)pStmt)->db : 0;
91549 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
91557 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
91569 sqlite3_mutex_enter(v->db->mutex);
91570 if( ((int)v->explain)==eMode ){
91574 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
91576 }else if( v->eVdbeState!=VDBE_READY_STATE ){
91578 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
91580 v->explain = eMode;
91583 v->explain = eMode;
91585 v->haveEqpOps = eMode==2;
91587 if( v->explain ){
91588 v->nResColumn = 12 - 4*v->explain;
91590 v->nResColumn = v->nResAlloc;
91592 sqlite3_mutex_leave(v->db->mutex);
91601 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
91618 sqlite3_mutex_enter(pDb->mutex);
91620 pNext = (sqlite3_stmt*)pDb->pVdbe;
91622 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
91624 sqlite3_mutex_leave(pDb->mutex);
91636 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
91643 sqlite3 *db = pVdbe->db;
91644 sqlite3_mutex_enter(db->mutex);
91646 db->pnBytesFreed = (int*)&v;
91647 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
91648 db->lookaside.pEnd = db->lookaside.pStart;
91650 db->pnBytesFreed = 0;
91651 db->lookaside.pEnd = db->lookaside.pTrueEnd;
91652 sqlite3_mutex_leave(db->mutex);
91654 v = pVdbe->aCounter[op];
91655 if( resetFlag ) pVdbe->aCounter[op] = 0;
91665 return p ? p->zSql : 0;
91685 sqlite3_mutex_enter(p->db->mutex);
91687 sqlite3_mutex_leave(p->db->mutex);
91700 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
91701 sqlite3_mutex_enter(p->db->mutex);
91702 p->zNormSql = sqlite3Normalize(p, p->zSql);
91703 sqlite3_mutex_leave(p->db->mutex);
91705 return p->zNormSql;
91724 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
91731 ** This function is called from within a pre-update callback to retrieve
91744 p = db->pPreUpdate;
91746 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
91747 if( !p || p->op==SQLITE_INSERT ){
91751 if( p->pPk ){
91752 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
91754 if( iIdx>=p->pCsr->nField || iIdx<0 ){
91760 if( p->pUnpacked==0 ){
91764 assert( p->pCsr->eCurType==CURTYPE_BTREE );
91765 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
91768 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
91770 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
91771 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
91777 p->aRecord = aRec;
91780 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
91781 if( iIdx==p->pTab->iPKey ){
91782 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
91783 }else if( iIdx>=p->pUnpacked->nField ){
91785 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
91786 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
91787 testcase( pMem->flags & MEM_Int );
91788 testcase( pMem->flags & MEM_IntReal );
91801 ** This function is called from within a pre-update callback to retrieve
91807 p = db!=0 ? db->pPreUpdate : 0;
91809 p = db->pPreUpdate;
91811 return (p ? p->keyinfo.nKeyField : 0);
91817 ** This function is designed to be called from within a pre-update callback
91821 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
91822 ** top-level trigger etc.).
91830 p = db!=0 ? db->pPreUpdate : 0;
91832 p = db->pPreUpdate;
91834 return (p ? p->v->nFrame : 0);
91840 ** This function is designed to be called from within a pre-update callback
91846 p = db!=0 ? db->pPreUpdate : 0;
91848 p = db->pPreUpdate;
91850 return (p ? p->iBlobWrite : -1);
91856 ** This function is called from within a pre-update callback to retrieve
91869 p = db->pPreUpdate;
91870 if( !p || p->op==SQLITE_DELETE ){
91874 if( p->pPk && p->op!=SQLITE_UPDATE ){
91875 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
91877 if( iIdx>=p->pCsr->nField || iIdx<0 ){
91882 if( p->op==SQLITE_INSERT ){
91883 /* For an INSERT, memory cell p->iNewReg contains the serialized record
91885 UnpackedRecord *pUnpack = p->pNewUnpacked;
91887 Mem *pData = &p->v->aMem[p->iNewReg];
91890 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
91895 p->pNewUnpacked = pUnpack;
91897 pMem = &pUnpack->aMem[iIdx];
91898 if( iIdx==p->pTab->iPKey ){
91899 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
91900 }else if( iIdx>=pUnpack->nField ){
91904 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
91909 assert( p->op==SQLITE_UPDATE );
91910 if( !p->aNew ){
91911 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
91912 if( !p->aNew ){
91917 assert( iIdx>=0 && iIdx<p->pCsr->nField );
91918 pMem = &p->aNew[iIdx];
91919 if( pMem->flags==0 ){
91920 if( iIdx==p->pTab->iPKey ){
91921 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
91923 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
91960 aOp = p->aOp;
91961 nOp = p->nOp;
91962 if( p->pFrame ){
91964 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
91965 aOp = pFrame->aOp;
91966 nOp = pFrame->nOp;
91983 pScan = &p->aScan[idx];
91987 for(idx=0; idx<p->nScan; idx++){
91988 pScan = &p->aScan[idx];
91989 if( pScan->zName ){
91990 iScan--;
91995 if( idx>=p->nScan ) return 1;
91999 if( pScan->addrLoop>0 ){
92000 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
92002 *(sqlite3_int64*)pOut = -1;
92007 if( pScan->addrVisit>0 ){
92008 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
92010 *(sqlite3_int64*)pOut = -1;
92016 LogEst x = pScan->nEst;
92025 *(const char**)pOut = pScan->zName;
92029 if( pScan->addrExplain ){
92030 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
92037 if( pScan->addrExplain ){
92038 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
92040 *(int*)pOut = -1;
92045 if( pScan->addrExplain ){
92046 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
92048 *(int*)pOut = -1;
92054 if( pScan->aAddrRange[0]==0 ){
92055 res = -1;
92058 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
92059 int iIns = pScan->aAddrRange[ii];
92060 int iEnd = pScan->aAddrRange[ii+1];
92071 if( pOp->p1!=iEnd ) continue;
92072 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
92108 for(ii=0; p!=0 && ii<p->nOp; ii++){
92109 Op *pOp = &p->aOp[ii];
92110 pOp->nExec = 0;
92111 pOp->nCycle = 0;
92133 ** The Vdbe parse-tree explainer is also found here.
92141 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
92166 ** This function returns a pointer to a nul-terminated string in memory
92170 ** then the returned string holds a copy of zRawSql with "-- " prepended
92176 ** with large (multi-megabyte) strings and blobs.
92186 ** parameter index is known, locate the value in p->aVar[]. Then render
92205 db = p->db;
92206 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
92207 if( db->nVdbeExec>1 ){
92211 sqlite3_str_append(&out, "-- ", 3);
92212 assert( (zRawSql - zStart) > 0 );
92213 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
92215 }else if( p->nVar==0 ){
92244 assert( idx>0 && idx<=p->nVar );
92245 pVar = &p->aVar[idx-1];
92246 if( pVar->flags & MEM_Null ){
92248 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
92249 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
92250 }else if( pVar->flags & MEM_Real ){
92251 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
92252 }else if( pVar->flags & MEM_Str ){
92259 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
92267 nOut = pVar->n;
92271 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
92274 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
92276 if( nOut<pVar->n ){
92277 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
92283 }else if( pVar->flags & MEM_Zero ){
92284 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
92287 assert( pVar->flags & MEM_Blob );
92289 nOut = pVar->n;
92294 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
92298 if( nOut<pVar->n ){
92299 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
92388 ** use this information to make sure that the zero-blob functionality
92395 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
92396 sqlite3_max_blobsize = p->n;
92406 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
92408 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
92464 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
92465 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
92466 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
92468 ** In other words, if M is 2, then I is either 0 (for fall-through) or
92470 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
92481 ** always taken, the flags should be 0x05 since the fall-through and
92483 ** flags should be 0x06 since only the fall-through approach is allowed.
92486 ** interested in equal or not-equal. In other words, I==0 and I==2
92491 ** should be no-ops except for special builds used to measure test coverage.
92496 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
92500 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
92512 ** iSrcLine - the line number of the VdbeCoverage() macro, with
92514 ** I - Mask of bits 0x07 indicating which cases are are
92516 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
92521 ** M - Type of jump. Same as M argument above
92546 if( ((P)->flags&MEM_Ephem)!=0 \
92550 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
92577 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
92578 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
92580 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
92588 assert( iCur>=0 && iCur<p->nCursor );
92589 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
92590 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
92591 p->apCsr[iCur] = 0;
92596 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
92597 ** to hold cursors, it is faster to in-line the logic. */
92598 assert( pMem->flags==MEM_Undefined );
92599 assert( (pMem->flags & MEM_Dyn)==0 );
92600 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
92601 if( pMem->szMalloc<nByte ){
92602 if( pMem->szMalloc>0 ){
92603 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
92605 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
92606 if( pMem->zMalloc==0 ){
92607 pMem->szMalloc = 0;
92610 pMem->szMalloc = nByte;
92613 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
92615 pCx->eCurType = eCurType;
92616 pCx->nField = nField;
92617 pCx->aOffset = &pCx->aType[nField];
92619 pCx->uc.pCursor = (BtCursor*)
92620 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
92621 sqlite3BtreeCursorZero(pCx->uc.pCursor);
92639 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
92659 u8 enc = pRec->enc;
92661 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
92662 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
92664 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
92665 pRec->flags |= MEM_Int;
92667 pRec->u.r = rValue;
92668 pRec->flags |= MEM_Real;
92671 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
92674 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
92675 pRec->flags &= ~MEM_Str;
92685 ** floating-point representation if an integer representation
92699 ** No-op. pRec is unchanged.
92709 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
92710 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
92711 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
92722 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
92723 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
92724 testcase( pRec->flags & MEM_Int );
92725 testcase( pRec->flags & MEM_Real );
92726 testcase( pRec->flags & MEM_IntReal );
92730 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
92765 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
92771 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
92772 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
92774 pMem->u.i = 0;
92777 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
92779 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
92780 pMem->u.i = ix;
92785 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
92786 pMem->u.i = ix;
92796 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
92797 ** But it does set pMem->u.r and pMem->u.i appropriately.
92800 assert( (pMem->flags & MEM_Null)==0
92801 || pMem->db==0 || pMem->db->mallocFailed );
92802 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
92803 testcase( pMem->flags & MEM_Int );
92804 testcase( pMem->flags & MEM_Real );
92805 testcase( pMem->flags & MEM_IntReal );
92806 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
92808 assert( pMem->flags & (MEM_Str|MEM_Blob) );
92809 testcase( pMem->flags & MEM_Str );
92810 testcase( pMem->flags & MEM_Blob );
92821 int f = pMem->flags;
92839 for(i=0; i<25 && i<pMem->n; i++){
92840 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
92843 for(i=0; i<25 && i<pMem->n; i++){
92844 char z = pMem->z[i];
92849 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
92866 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
92867 for(j=0; j<25 && j<pMem->n; j++){
92868 c = pMem->z[j];
92871 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
92873 sqlite3_str_appendf(pStr, "(0-term)");
92884 if( p->flags & MEM_Undefined ){
92886 }else if( p->flags & MEM_Null ){
92887 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
92888 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
92889 printf(" si:%lld", p->u.i);
92890 }else if( (p->flags & (MEM_IntReal))!=0 ){
92891 printf(" ir:%lld", p->u.i);
92892 }else if( p->flags & MEM_Int ){
92893 printf(" i:%lld", p->u.i);
92895 }else if( p->flags & MEM_Real ){
92896 printf(" r:%.17g", p->u.r);
92907 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
92912 if( p->pScopyFrom ){
92913 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
92932 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
92938 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
92947 ** the number of non-transaction savepoints currently in the
92957 for(p=db->pSavepoint; p; p=p->pNext) n++;
92958 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
92964 ** Return the register of pOp->p2 after first preparing it to be
92969 pOut->flags = MEM_Int;
92974 assert( pOp->p2>0 );
92975 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
92976 pOut = &p->aMem[pOp->p2];
92978 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
92981 pOut->flags = MEM_Int;
92987 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
92988 ** with pOp->p3. Return the hash.
92994 assert( pOp->p4type==P4_INT32 );
92995 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
92997 if( p->flags & (MEM_Int|MEM_IntReal) ){
92998 h += p->u.i;
92999 }else if( p->flags & MEM_Real ){
93001 }else if( p->flags & (MEM_Str|MEM_Blob) ){
93005 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
93022 int t, /* The serial-type code for the column value */
93029 sqlite3 *db = pDest->db;
93030 int encoding = pDest->enc;
93032 assert( pC->eCurType==CURTYPE_BTREE );
93033 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG;
93034 if( len > 4000 && pC->pKeyInfo==0 ){
93041 ** Only do this on table-btrees so that writes to index-btrees do not
93047 if( pC->colCache==0 ){
93048 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
93049 if( pC->pCache==0 ) return SQLITE_NOMEM;
93050 pC->colCache = 1;
93052 pCache = pC->pCache;
93053 if( pCache->pCValue==0
93054 || pCache->iCol!=iCol
93055 || pCache->cacheStatus!=cacheStatus
93056 || pCache->colCacheCtr!=colCacheCtr
93057 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
93059 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
93060 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
93062 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
93067 pCache->iCol = iCol;
93068 pCache->cacheStatus = cacheStatus;
93069 pCache->colCacheCtr = colCacheCtr;
93070 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
93072 pBuf = pCache->pCValue;
93079 pDest->flags |= MEM_Term;
93085 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
93087 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
93089 pDest->z[len] = 0;
93090 pDest->flags |= MEM_Term;
93093 pDest->flags &= ~MEM_Ephem;
93109 return azTypes[sqlite3_value_type(pMem)-1];
93119 Op *aOp = p->aOp; /* Copy of p->aOp */
93127 sqlite3 *db = p->db; /* The database */
93135 Mem *aMem = p->aMem; /* Copy of p->aMem */
93147 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
93148 if( DbMaskNonZero(p->lockMask) ){
93152 if( db->xProgress ){
93153 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
93154 assert( 0 < db->nProgressOps );
93155 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
93160 if( p->rc==SQLITE_NOMEM ){
93165 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
93166 testcase( p->rc!=SQLITE_OK );
93167 p->rc = SQLITE_OK;
93168 assert( p->bIsReader || p->readOnly!=0 );
93169 p->iCurrentTime = 0;
93170 assert( p->explain==0 );
93171 db->busyHandler.nBusy = 0;
93172 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
93176 if( p->pc==0
93177 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
93182 if( p->db->flags & SQLITE_VdbeListing ){
93184 for(i=0; i<p->nOp; i++){
93188 if( p->db->flags & SQLITE_VdbeEQP ){
93189 for(i=0; i<p->nOp; i++){
93197 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
93201 for(pOp=&aOp[p->pc]; 1; pOp++){
93206 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
93210 pOp->nExec++;
93211 pnCycle = &pOp->nCycle;
93212 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
93215 pOp->nExec++;
93216 pnCycle = &pOp->nCycle;
93217 *pnCycle -= sqlite3Hwtime();
93224 if( db->flags & SQLITE_VdbeTrace ){
93225 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
93226 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
93236 sqlite3_interrupt_count--;
93246 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
93248 assert( pOp->p1>0 );
93249 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
93250 assert( memIsValid(&aMem[pOp->p1]) );
93251 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
93252 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
93255 assert( pOp->p2>0 );
93256 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
93257 assert( memIsValid(&aMem[pOp->p2]) );
93258 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
93259 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
93262 assert( pOp->p3>0 );
93263 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93264 assert( memIsValid(&aMem[pOp->p3]) );
93265 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
93266 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
93269 assert( pOp->p2>0 );
93270 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
93271 memAboutToChange(p, &aMem[pOp->p2]);
93274 assert( pOp->p3>0 );
93275 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93276 memAboutToChange(p, &aMem[pOp->p3]);
93284 switch( pOp->opcode ){
93291 ** the switch statement will break with convention and be flush-left. Another
93329 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
93339 if( pOp->p5 ){
93340 assert( pOp->p2 < (int)(pOp - aOp) );
93341 assert( pOp->p2 > 1 );
93342 pOp = &aOp[pOp->p2 - 2];
93349 pOp = &aOp[pOp->p2 - 1];
93360 ** faster according to "valgrind --tool=cachegrind" */
93362 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
93367 ** If the progress callback returns non-zero, exit the virtual machine with
93370 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
93371 assert( db->nProgressOps!=0 );
93372 nProgressLimit += db->nProgressOps;
93373 if( db->xProgress(db->pProgressArg) ){
93390 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
93391 pIn1 = &aMem[pOp->p1];
93394 pIn1->flags = MEM_Int;
93395 pIn1->u.i = (int)(pOp-aOp);
93396 REGISTER_TRACE(pOp->p1, pIn1);
93407 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
93414 ** P2 is not used by the byte-code engine. However, if P2 is positive
93419 ** value is a byte-code indentation hint. See tag-20220407a in
93423 pIn1 = &aMem[pOp->p1];
93424 if( pIn1->flags & MEM_Int ){
93425 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
93426 pOp = &aOp[pIn1->u.i];
93427 }else if( ALWAYS(pOp->p3) ){
93445 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
93446 assert( pOp->p2>=0 && pOp->p2<p->nOp );
93447 assert( pOp->p3>=0 && pOp->p3<p->nOp );
93448 pOut = &aMem[pOp->p1];
93450 pOut->u.i = pOp->p3 - 1;
93451 pOut->flags = MEM_Int;
93452 if( pOp->p2==0 ) break;
93457 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
93458 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
93459 pOp = &aOp[pOp->p2 - 1];
93473 pIn1 = &aMem[pOp->p1];
93474 assert( pIn1->flags==MEM_Int );
93475 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
93476 pCaller = &aOp[pIn1->u.i];
93477 assert( pCaller->opcode==OP_Yield );
93478 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
93479 pOp = &aOp[pCaller->p2 - 1];
93480 pIn1->flags = MEM_Undefined;
93499 pIn1 = &aMem[pOp->p1];
93501 pIn1->flags = MEM_Int;
93502 pcDest = (int)pIn1->u.i;
93503 pIn1->u.i = (int)(pOp - aOp);
93504 REGISTER_TRACE(pOp->p1, pIn1);
93514 ** value in register P3 is not NULL, then this routine is a no-op.
93518 pIn3 = &aMem[pOp->p3];
93520 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
93522 if( (pIn3->flags & MEM_Null)==0 ) break;
93562 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
93568 assert( pOp->p1!=SQLITE_INTERNAL );
93570 if( p->pFrame && pOp->p1==SQLITE_OK ){
93571 /* Halt the sub-program. Return control to the parent frame. */
93572 pFrame = p->pFrame;
93573 p->pFrame = pFrame->pParent;
93574 p->nFrame--;
93575 sqlite3VdbeSetChanges(db, p->nChange);
93577 if( pOp->p2==OE_Ignore ){
93578 /* Instruction pcx is the OP_Program that invoked the sub-program
93580 ** instruction is set to OE_Ignore, then the sub-program is throwing
93583 pcx = p->aOp[pcx].p2-1;
93585 aOp = p->aOp;
93586 aMem = p->aMem;
93590 p->rc = pOp->p1;
93591 p->errorAction = (u8)pOp->p2;
93592 assert( pOp->p5<=4 );
93593 if( p->rc ){
93594 if( pOp->p5 ){
93597 testcase( pOp->p5==1 );
93598 testcase( pOp->p5==2 );
93599 testcase( pOp->p5==3 );
93600 testcase( pOp->p5==4 );
93601 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
93602 if( pOp->p4.z ){
93603 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
93606 sqlite3VdbeError(p, "%s", pOp->p4.z);
93608 pcx = (int)(pOp - aOp);
93609 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
93614 p->rc = SQLITE_BUSY;
93616 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
93617 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
93618 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
93626 ** The 32-bit integer value P1 is written into register P2.
93630 pOut->u.i = pOp->p1;
93637 ** P4 is a pointer to a 64-bit integer value.
93642 assert( pOp->p4.pI64!=0 );
93643 pOut->u.i = *pOp->p4.pI64;
93651 ** P4 is a pointer to a 64-bit floating point value.
93656 pOut->flags = MEM_Real;
93657 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
93658 pOut->u.r = *pOp->p4.pReal;
93666 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
93672 assert( pOp->p4.z!=0 );
93674 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
93678 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
93682 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
93684 pOut->szMalloc = 0;
93685 pOut->flags |= MEM_Static;
93686 if( pOp->p4type==P4_DYNAMIC ){
93687 sqlite3DbFree(db, pOp->p4.z);
93689 pOp->p4type = P4_DYNAMIC;
93690 pOp->p4.z = pOut->z;
93691 pOp->p1 = pOut->n;
93694 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
93697 pOp->opcode = OP_String;
93716 assert( pOp->p4.z!=0 );
93718 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
93719 pOut->z = pOp->p4.z;
93720 pOut->n = pOp->p1;
93721 pOut->enc = encoding;
93724 if( pOp->p3>0 ){
93725 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93726 pIn3 = &aMem[pOp->p3];
93727 assert( pIn3->flags & MEM_Int );
93728 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
93737 ** Mark the beginning of a subroutine that can be entered in-line
93741 ** If the subroutine is entered in-line, then the OP_Return will simply
93747 ** a no-op that simply falls through to the next instruction (assuming that
93749 ** entered in-line, then the OP_Return will cause in-line execution to
93764 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
93773 cnt = pOp->p3-pOp->p2;
93774 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93775 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
93776 pOut->n = 0;
93778 pOut->uTemp = 0;
93784 pOut->flags = nullFlag;
93785 pOut->n = 0;
93786 cnt--;
93800 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
93801 pOut = &aMem[pOp->p1];
93802 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
93811 ** a zero-filled blob that is P1 bytes long in P2.
93814 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
93816 if( pOp->p4.z==0 ){
93817 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
93820 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
93822 pOut->enc = encoding;
93838 assert( pOp->p1>0 && pOp->p1<=p->nVar );
93839 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
93840 pVar = &p->aVar[pOp->p1 - 1];
93844 pOut = &aMem[pOp->p2];
93847 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
93848 pOut->flags |= MEM_Static|MEM_FromBind;
93856 ** Move the P3 values in register P1..P1+P3-1 over into
93857 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
93859 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
93867 n = pOp->p3;
93868 p1 = pOp->p1;
93869 p2 = pOp->p2;
93876 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
93877 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
93882 pIn1->pScopyFrom = 0;
93884 for(i=1; i<p->nMem; i++){
93895 }while( --n );
93915 n = pOp->p3;
93916 pIn1 = &aMem[pOp->p1];
93917 pOut = &aMem[pOp->p2];
93923 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
93924 pOut->flags &= ~MEM_Subtype;
93927 pOut->pScopyFrom = 0;
93929 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
93930 if( (n--)==0 ) break;
93951 pIn1 = &aMem[pOp->p1];
93952 pOut = &aMem[pOp->p2];
93956 pOut->pScopyFrom = pIn1;
93957 pOut->mScopyFlags = pIn1->flags;
93971 pIn1 = &aMem[pOp->p1];
93972 assert( (pIn1->flags & MEM_Int)!=0 );
93973 pOut = &aMem[pOp->p2];
93974 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
93982 ** constraint violations, this is a no-op.
93999 ** The registers P1 through P1+P2-1 contain a single row of
94002 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
94006 assert( p->nResColumn==pOp->p2 );
94007 assert( pOp->p1>0 || CORRUPT_DB );
94008 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
94010 p->cacheCtr = (p->cacheCtr + 2)|1;
94011 p->pResultRow = &aMem[pOp->p1];
94014 Mem *pMem = p->pResultRow;
94016 for(i=0; i<pOp->p2; i++){
94018 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
94028 if( db->mallocFailed ) goto no_mem;
94029 if( db->mTrace & SQLITE_TRACE_ROW ){
94030 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
94032 p->pc = (int)(pOp - aOp) + 1;
94055 pIn1 = &aMem[pOp->p1];
94056 pIn2 = &aMem[pOp->p2];
94057 pOut = &aMem[pOp->p3];
94060 flags1 = pIn1->flags;
94062 testcase( pIn2->flags & MEM_Null );
94063 if( (flags1 | pIn2->flags) & MEM_Null ){
94069 flags1 = pIn1->flags & ~MEM_Str;
94072 flags1 = pIn1->flags & ~MEM_Str;
94074 flags2 = pIn2->flags;
94077 flags2 = pIn2->flags & ~MEM_Str;
94080 flags2 = pIn2->flags & ~MEM_Str;
94082 nByte = pIn1->n + pIn2->n;
94083 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94091 memcpy(pOut->z, pIn2->z, pIn2->n);
94092 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
94093 pIn2->flags = flags2;
94095 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
94096 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
94097 pIn1->flags = flags1;
94099 pOut->z[nByte]=0;
94100 pOut->z[nByte+1] = 0;
94101 pOut->flags |= MEM_Term;
94102 pOut->n = (int)nByte;
94103 pOut->enc = encoding;
94124 ** Synopsis: r[P3]=r[P2]-r[P1]
94158 pIn1 = &aMem[pOp->p1];
94159 type1 = pIn1->flags;
94160 pIn2 = &aMem[pOp->p2];
94161 type2 = pIn2->flags;
94162 pOut = &aMem[pOp->p3];
94165 iA = pIn1->u.i;
94166 iB = pIn2->u.i;
94167 switch( pOp->opcode ){
94173 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
94179 if( iA==-1 ) iA = 1;
94184 pOut->u.i = iB;
94195 switch( pOp->opcode ){
94197 case OP_Subtract: rB -= rA; break;
94209 if( iA==-1 ) iA = 1;
94215 pOut->u.i = rB;
94221 pOut->u.r = rB;
94236 ** be returned. This is used by the built-in min(), max() and nullif()
94245 ** publicly. Only built-in functions have access to this feature.
94248 assert( pOp->p4type==P4_COLLSEQ );
94249 if( pOp->p1 ){
94250 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
94258 ** Take the bit-wise AND of the values in register P1 and P2 and
94265 ** Take the bit-wise OR of the values in register P1 and P2 and
94294 pIn1 = &aMem[pOp->p1];
94295 pIn2 = &aMem[pOp->p2];
94296 pOut = &aMem[pOp->p3];
94297 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
94303 op = pOp->opcode;
94314 op = 2*OP_ShiftLeft + 1 - op;
94315 iB = iB>(-64) ? -iB : 64;
94319 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
94326 /* Sign-extend on a right shift of a negative number */
94327 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
94332 pOut->u.i = iA;
94346 pIn1 = &aMem[pOp->p1];
94349 pIn1->u.i += pOp->p2;
94361 pIn1 = &aMem[pOp->p1];
94362 if( (pIn1->flags & MEM_Int)==0 ){
94364 if( (pIn1->flags & MEM_Int)==0 ){
94366 if( pOp->p2==0 ){
94390 pIn1 = &aMem[pOp->p1];
94391 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
94392 testcase( pIn1->flags & MEM_Int );
94393 testcase( pIn1->flags & MEM_IntReal );
94395 REGISTER_TRACE(pOp->p1, pIn1);
94418 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
94419 testcase( pOp->p2==SQLITE_AFF_TEXT );
94420 testcase( pOp->p2==SQLITE_AFF_BLOB );
94421 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
94422 testcase( pOp->p2==SQLITE_AFF_INTEGER );
94423 testcase( pOp->p2==SQLITE_AFF_REAL );
94424 pIn1 = &aMem[pOp->p1];
94428 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
94431 REGISTER_TRACE(pOp->p1, pIn1);
94442 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
94486 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
94536 u16 flags1; /* Copy of initial value of pIn1->flags */
94537 u16 flags3; /* Copy of initial value of pIn3->flags */
94539 pIn1 = &aMem[pOp->p1];
94540 pIn3 = &aMem[pOp->p3];
94541 flags1 = pIn1->flags;
94542 flags3 = pIn3->flags;
94545 if( pIn3->u.i > pIn1->u.i ){
94546 if( sqlite3aGTb[pOp->opcode] ){
94547 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94552 }else if( pIn3->u.i < pIn1->u.i ){
94553 if( sqlite3aLTb[pOp->opcode] ){
94554 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94557 iCompare = -1;
94560 if( sqlite3aEQb[pOp->opcode] ){
94561 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94567 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94572 if( pOp->p5 & SQLITE_NULLEQ ){
94578 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
94579 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
94585 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
94593 if( pOp->p5 & SQLITE_JUMPIFNULL ){
94601 /* Neither operand is NULL and we couldn't do the special high-speed
94602 ** integer comparison case. So do a general-case comparison. */
94603 affinity = pOp->p5 & SQLITE_AFF_MASK;
94608 assert( flags3==pIn3->flags || CORRUPT_DB );
94609 flags3 = pIn3->flags;
94617 testcase( pIn1->flags & MEM_Int );
94618 testcase( pIn1->flags & MEM_Real );
94619 testcase( pIn1->flags & MEM_IntReal );
94621 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
94622 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
94626 testcase( pIn3->flags & MEM_Int );
94627 testcase( pIn3->flags & MEM_Real );
94628 testcase( pIn3->flags & MEM_IntReal );
94630 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
94631 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
94634 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
94635 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
94647 res2 = sqlite3aLTb[pOp->opcode];
94649 res2 = sqlite3aEQb[pOp->opcode];
94651 res2 = sqlite3aGTb[pOp->opcode];
94657 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
94658 pIn3->flags = flags3;
94659 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
94660 pIn1->flags = flags1;
94662 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
94684 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
94687 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
94712 assert( pOp->p4type==P4_INTARRAY );
94713 assert( pOp->p4.ai );
94720 ** Synopsis: r[P1@P3] <-> r[P2@P3]
94722 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
94723 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
94752 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
94756 assert( pOp[-1].opcode==OP_Permutation );
94757 assert( pOp[-1].p4type==P4_INTARRAY );
94758 aPermute = pOp[-1].p4.ai + 1;
94761 n = pOp->p3;
94762 pKeyInfo = pOp->p4.pKeyInfo;
94765 p1 = pOp->p1;
94766 p2 = pOp->p2;
94771 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
94772 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
94774 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
94775 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
94784 assert( i<pKeyInfo->nKeyField );
94785 pColl = pKeyInfo->aColl[i];
94786 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
94790 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
94793 iCompare = -iCompare;
94795 if( bRev ) iCompare = -iCompare;
94812 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
94815 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
94817 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
94819 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
94849 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
94850 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
94851 if( pOp->opcode==OP_And ){
94858 pOut = &aMem[pOp->p3];
94862 pOut->u.i = v1;
94889 assert( pOp->p4type==P4_INT32 );
94890 assert( pOp->p4.i==0 || pOp->p4.i==1 );
94891 assert( pOp->p3==0 || pOp->p3==1 );
94892 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
94893 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
94905 pIn1 = &aMem[pOp->p1];
94906 pOut = &aMem[pOp->p2];
94907 if( (pIn1->flags & MEM_Null)==0 ){
94919 ** ones-complement of the P1 value into register P2. If P1 holds
94923 pIn1 = &aMem[pOp->p1];
94924 pOut = &aMem[pOp->p2];
94926 if( (pIn1->flags & MEM_Null)==0 ){
94927 pOut->flags = MEM_Int;
94928 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
94936 ** encountered on each invocation of the byte-code program. Jump to P2
94939 ** Top-level programs determine first invocation by comparing the P1
94947 ** because the self-altering code trick does not work for recursive
94952 assert( p->aOp[0].opcode==OP_Init );
94953 if( p->pFrame ){
94954 iAddr = (int)(pOp - p->aOp);
94955 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
94959 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
94961 if( p->aOp[0].p1==pOp->p1 ){
94967 pOp->p1 = p->aOp[0].p1;
94974 ** is considered true if it is numeric and non-zero. If the value
94975 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
94979 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
94989 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
94993 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
95005 pIn1 = &aMem[pOp->p1];
95006 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
95007 if( (pIn1->flags & MEM_Null)!=0 ){
95026 ** If P1 is -1, then P3 is a register number and the datatype is taken
95049 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
95050 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
95051 if( pOp->p1>=0 ){
95052 pC = p->apCsr[pOp->p1];
95054 assert( pOp->p3>=0 );
95055 if( pOp->p3<pC->nHdrParsed ){
95056 serialType = pC->aType[pOp->p3];
95083 typeMask = 1 << (pOp->p4.i - 1);
95091 assert( memIsValid(&aMem[pOp->p3]) );
95092 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
95099 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
95100 if( typeMask & pOp->p5 ){
95114 if( (aMem[pOp->p1].flags & MEM_Null)!=0
95115 || (aMem[pOp->p3].flags & MEM_Null)!=0
95117 sqlite3VdbeMemSetNull(aMem + pOp->p2);
95119 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
95130 pIn1 = &aMem[pOp->p1];
95131 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
95132 if( (pIn1->flags & MEM_Null)==0 ){
95146 ** If P1 is not an open cursor, then this opcode is a no-op.
95150 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95151 pC = p->apCsr[pOp->p1];
95152 if( pC && pC->nullRow ){
95153 sqlite3VdbeMemSetNull(aMem + pOp->p3);
95173 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
95177 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95178 pC = p->apCsr[pOp->p1];
95179 pOut = &p->aMem[pOp->p3];
95180 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
95183 if( pC->deferredMoveto ){
95187 if( sqlite3BtreeEof(pC->uc.pCursor) ){
95190 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
95202 ** information about the format of the data.) Extract the P2-th column
95222 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
95223 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
95231 u64 offset64; /* 64-bit offset */
95235 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95236 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95237 pC = p->apCsr[pOp->p1];
95238 p2 = (u32)pOp->p2;
95242 assert( p2<(u32)pC->nField
95243 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
95244 aOffset = pC->aOffset;
95245 assert( aOffset==pC->aType+pC->nField );
95246 assert( pC->eCurType!=CURTYPE_VTAB );
95247 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
95248 assert( pC->eCurType!=CURTYPE_SORTER );
95250 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
95251 if( pC->nullRow ){
95252 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
95253 /* For the special case of as pseudo-cursor, the seekResult field
95255 pReg = &aMem[pC->seekResult];
95256 assert( pReg->flags & MEM_Blob );
95258 pC->payloadSize = pC->szRow = pReg->n;
95259 pC->aRow = (u8*)pReg->z;
95261 pDest = &aMem[pOp->p3];
95267 pCrsr = pC->uc.pCursor;
95268 if( pC->deferredMoveto ){
95270 assert( !pC->isEphemeral );
95271 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
95272 pC = pC->pAltCursor;
95273 p2 = iMap - 1;
95283 assert( pC->eCurType==CURTYPE_BTREE );
95286 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
95287 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
95288 assert( pC->szRow<=pC->payloadSize );
95289 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
95291 pC->cacheStatus = p->cacheCtr;
95292 if( (aOffset[0] = pC->aRow[0])<0x80 ){
95293 pC->iHdrOffset = 1;
95295 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
95297 pC->nHdrParsed = 0;
95299 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
95300 /* pC->aRow does not have to hold the entire row, but it does at least
95301 ** need to cover the header of the record. If pC->aRow does not contain
95304 pC->aRow = 0;
95305 pC->szRow = 0;
95313 ** 3-byte type for each of the maximum of 32768 columns plus three
95316 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
95321 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
95333 zData = pC->aRow;
95334 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
95338 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
95345 ** parsed and valid information is in aOffset[] and pC->aType[].
95347 if( pC->nHdrParsed<=p2 ){
95349 ** to extract additional fields up through the p2+1-th field
95351 if( pC->iHdrOffset<aOffset[0] ){
95353 if( pC->aRow==0 ){
95355 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
95359 zData = pC->aRow;
95362 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
95364 i = pC->nHdrParsed;
95366 zHdr = zData + pC->iHdrOffset;
95370 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
95375 pC->aType[i] = t;
95386 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
95387 || (offset64 > pC->payloadSize)
95393 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
95398 pC->nHdrParsed = i;
95399 pC->iHdrOffset = (u32)(zHdr - zData);
95400 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
95409 if( pC->nHdrParsed<=p2 ){
95410 pDest = &aMem[pOp->p3];
95412 if( pOp->p4type==P4_MEM ){
95413 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
95420 t = pC->aType[p2];
95423 /* Extract the content for the p2+1-th column. Control can only
95424 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
95427 assert( p2<pC->nHdrParsed );
95429 pDest = &aMem[pOp->p3];
95435 assert( t==pC->aType[p2] );
95436 if( pC->szRow>=aOffset[p2+1] ){
95438 ** page - where the content is not on an overflow page */
95439 zData = pC->aRow + aOffset[p2];
95444 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
95448 pDest->n = len = (t-12)/2;
95449 pDest->enc = encoding;
95450 if( pDest->szMalloc < len+2 ){
95451 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
95452 pDest->flags = MEM_Null;
95455 pDest->z = pDest->zMalloc;
95457 memcpy(pDest->z, zData, len);
95458 pDest->z[len] = 0;
95459 pDest->z[len+1] = 0;
95460 pDest->flags = aFlag[t&1];
95464 pDest->enc = encoding;
95465 assert( pDest->db==db );
95467 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0
95490 p->cacheCtr, colCacheCtr, pDest);
95501 REGISTER_TRACE(pOp->p3, pDest);
95506 pOp = &aOp[aOp[0].p3-1];
95526 ** is zero. When P3 is non-zero, no type checking occurs for
95533 ** <li> P2 should be the number of non-virtual columns in the
95545 assert( pOp->p4type==P4_TABLE );
95546 pTab = pOp->p4.pTab;
95547 assert( pTab->tabFlags & TF_Strict );
95548 assert( pTab->nNVCol==pOp->p2 );
95549 aCol = pTab->aCol;
95550 pIn1 = &aMem[pOp->p1];
95551 for(i=0; i<pTab->nCol; i++){
95554 if( pOp->p3 ){ pIn1++; continue; }
95556 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
95558 if( (pIn1->flags & MEM_Null)==0 ){
95561 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
95566 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
95570 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
95574 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
95575 assert( (pIn1->flags & MEM_IntReal)==0 );
95576 if( pIn1->flags & MEM_Int ){
95579 ** so that we keep the high-resolution integer value but know that
95581 testcase( pIn1->u.i==140737488355328LL );
95582 testcase( pIn1->u.i==140737488355327LL );
95583 testcase( pIn1->u.i==-140737488355328LL );
95584 testcase( pIn1->u.i==-140737488355329LL );
95585 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
95586 pIn1->flags |= MEM_IntReal;
95587 pIn1->flags &= ~MEM_Int;
95589 pIn1->u.r = (double)pIn1->u.i;
95590 pIn1->flags |= MEM_Real;
95591 pIn1->flags &= ~MEM_Int;
95593 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
95604 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
95607 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
95612 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
95613 pTab->zName, aCol[i].zCnName);
95623 ** P4 is a string that is P2 characters long. The N-th character of the
95624 ** string indicates the column affinity that should be used for the N-th
95630 zAffinity = pOp->p4.z;
95632 assert( pOp->p2>0 );
95633 assert( zAffinity[pOp->p2]==0 );
95634 pIn1 = &aMem[pOp->p1];
95635 while( 1 /*exit-by-break*/ ){
95636 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
95639 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
95642 ** so that we keep the high-resolution integer value but know that
95644 testcase( pIn1->u.i==140737488355328LL );
95645 testcase( pIn1->u.i==140737488355327LL );
95646 testcase( pIn1->u.i==-140737488355328LL );
95647 testcase( pIn1->u.i==-140737488355329LL );
95648 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
95649 pIn1->flags |= MEM_IntReal;
95650 pIn1->flags &= ~MEM_Int;
95652 pIn1->u.r = (double)pIn1->u.i;
95653 pIn1->flags |= MEM_Real;
95654 pIn1->flags &= ~(MEM_Int|MEM_Str);
95657 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
95672 ** P4 may be a string that is P2 characters long. The N-th character of the
95673 ** string indicates the column affinity that should be used for the N-th
95682 ** compile-time option is enabled:
95685 ** of the right-most table that can be null-trimmed.
95689 ** accept no-change records with serial_type 10. This value is
95711 ** ------------------------------------------------------------------------
95712 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
95713 ** ------------------------------------------------------------------------
95720 ** hdr-size field is also a varint which is the offset from the beginning
95726 nField = pOp->p1;
95727 zAffinity = pOp->p4.z;
95728 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
95730 nField = pOp->p2;
95731 pLast = &pData0[nField-1];
95734 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
95735 pOut = &aMem[pOp->p3];
95745 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
95746 pRec->flags |= MEM_IntReal;
95747 pRec->flags &= ~(MEM_Int);
95749 REGISTER_TRACE((int)(pRec-aMem), pRec);
95759 ** have a non-NULL default value. Also, the record must be left with
95761 ** index of the right-most column with a non-NULL default value */
95762 if( pOp->p5 ){
95763 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
95764 pLast--;
95765 nField--;
95772 ** the Mem.uTemp field of each term should hold the serial-type that will
95776 ** --------------- ---------------
95778 ** 1 1-byte signed integer
95779 ** 2 2-byte signed integer
95780 ** 3 3-byte signed integer
95781 ** 4 4-byte signed integer
95782 ** 5 6-byte signed integer
95783 ** 6 8-byte signed integer
95799 if( pRec->flags & MEM_Null ){
95800 if( pRec->flags & MEM_Zero ){
95804 ** Give such values a special internal-use-only serial-type of 10
95808 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
95810 pRec->uTemp = 10;
95812 pRec->uTemp = 0;
95815 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
95817 i64 i = pRec->u.i;
95819 testcase( pRec->flags & MEM_Int );
95820 testcase( pRec->flags & MEM_IntReal );
95833 if( (i&1)==i && p->minWriteFileFormat>=4 ){
95834 pRec->uTemp = 8+(u32)uu;
95837 pRec->uTemp = 1;
95841 pRec->uTemp = 2;
95844 pRec->uTemp = 3;
95847 pRec->uTemp = 4;
95850 pRec->uTemp = 5;
95853 if( pRec->flags & MEM_IntReal ){
95855 ** as an integer, then we might as well make it an 8-byte floating
95857 pRec->u.r = (double)pRec->u.i;
95858 pRec->flags &= ~MEM_IntReal;
95859 pRec->flags |= MEM_Real;
95860 pRec->uTemp = 7;
95862 pRec->uTemp = 6;
95865 }else if( pRec->flags & MEM_Real ){
95868 pRec->uTemp = 7;
95870 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
95871 assert( pRec->n>=0 );
95872 len = (u32)pRec->n;
95873 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
95874 if( pRec->flags & MEM_Zero ){
95875 serial_type += pRec->u.nZero*2;
95878 len += pRec->u.nZero;
95880 nZero += pRec->u.nZero;
95885 pRec->uTemp = serial_type;
95888 pRec--;
95891 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
95909 ** the new record. The output register (pOp->p3) is not allowed to
95913 if( nByte+nZero<=pOut->szMalloc ){
95916 pOut->z = pOut->zMalloc;
95920 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
95927 pOut->n = (int)nByte;
95928 pOut->flags = MEM_Blob;
95930 pOut->u.nZero = nZero;
95931 pOut->flags |= MEM_Zero;
95934 zHdr = (u8 *)pOut->z;
95945 while( 1 /*exit-by-break*/ ){
95946 serial_type = pRec->uTemp;
95947 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
95949 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
95958 assert( sizeof(v)==sizeof(pRec->u.r) );
95959 memcpy(&v, &pRec->u.r, sizeof(v));
95962 v = pRec->u.i;
95980 if( serial_type>=14 && pRec->n>0 ){
95981 assert( pRec->z!=0 );
95982 memcpy(zPayload, pRec->z, pRec->n);
95983 zPayload += pRec->n;
95987 if( pRec->n ){
95988 assert( pRec->z!=0 );
95989 memcpy(zPayload, pRec->z, pRec->n);
95990 zPayload += pRec->n;
95996 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
95997 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
95999 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96000 REGISTER_TRACE(pOp->p3, pOut);
96011 ** every btree page of the table. But if P3 is non-zero, an estimate
96018 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
96019 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
96021 if( pOp->p3 ){
96029 pOut->u.i = nEntry;
96050 p1 = pOp->p1;
96051 zName = pOp->p4.z;
96056 assert( db->pSavepoint==0 || db->autoCommit==0 );
96058 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
96060 assert( p->bIsReader );
96063 if( db->nVdbeWrite>0 ){
96067 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
96076 ** that the db->aVTrans[] array is empty. */
96077 assert( db->autoCommit==0 || db->nVTrans==0 );
96079 db->nStatement+db->nSavepoint);
96086 pNew->zName = (char *)&pNew[1];
96087 memcpy(pNew->zName, zName, nName+1);
96091 if( db->autoCommit ){
96092 db->autoCommit = 0;
96093 db->isTransactionSavepoint = 1;
96095 db->nSavepoint++;
96099 pNew->pNext = db->pSavepoint;
96100 db->pSavepoint = pNew;
96101 pNew->nDeferredCons = db->nDeferredCons;
96102 pNew->nDeferredImmCons = db->nDeferredImmCons;
96112 pSavepoint = db->pSavepoint;
96113 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
96114 pSavepoint = pSavepoint->pNext
96121 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
96125 sqlite3VdbeError(p, "cannot release savepoint - "
96134 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
96139 db->autoCommit = 1;
96141 p->pc = (int)(pOp - aOp);
96142 db->autoCommit = 0;
96143 p->rc = rc = SQLITE_BUSY;
96146 rc = p->rc;
96148 db->autoCommit = 0;
96150 db->isTransactionSavepoint = 0;
96154 iSavepoint = db->nSavepoint - iSavepoint - 1;
96156 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
96157 for(ii=0; ii<db->nDb; ii++){
96158 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
96167 for(ii=0; ii<db->nDb; ii++){
96168 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
96176 db->mDbFlags |= DBFLAG_SchemaChange;
96183 while( db->pSavepoint!=pSavepoint ){
96184 pTmp = db->pSavepoint;
96185 db->pSavepoint = pTmp->pNext;
96187 db->nSavepoint--;
96195 assert( pSavepoint==db->pSavepoint );
96196 db->pSavepoint = pSavepoint->pNext;
96199 db->nSavepoint--;
96203 db->nDeferredCons = pSavepoint->nDeferredCons;
96204 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
96214 if( p->eVdbeState==VDBE_HALT_STATE ){
96223 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
96234 desiredAutoCommit = pOp->p1;
96235 iRollback = pOp->p2;
96238 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
96239 assert( p->bIsReader );
96241 if( desiredAutoCommit!=db->autoCommit ){
96245 db->autoCommit = 1;
96246 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
96250 sqlite3VdbeError(p, "cannot commit transaction - "
96257 db->autoCommit = (u8)desiredAutoCommit;
96260 p->pc = (int)(pOp - aOp);
96261 db->autoCommit = (u8)(1-desiredAutoCommit);
96262 p->rc = rc = SQLITE_BUSY;
96266 if( p->rc==SQLITE_OK ){
96275 (iRollback)?"cannot rollback - no transaction is active":
96276 "cannot commit - no transaction is active"));
96288 ** If P2 is non-zero, then a write-transaction is started, or if a
96289 ** read-transaction is already active, it is upgraded to a write-transaction.
96290 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
96298 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
96324 assert( p->bIsReader );
96325 assert( p->readOnly==0 || pOp->p2==0 );
96326 assert( pOp->p2>=0 && pOp->p2<=2 );
96327 assert( pOp->p1>=0 && pOp->p1<db->nDb );
96328 assert( DbMaskTest(p->btreeMask, pOp->p1) );
96330 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
96331 if( db->flags & SQLITE_QueryOnly ){
96341 pDb = &db->aDb[pOp->p1];
96342 pBt = pDb->pBt;
96345 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
96350 p->pc = (int)(pOp - aOp);
96351 p->rc = rc;
96357 if( p->usesStmtJournal
96358 && pOp->p2
96359 && (db->autoCommit==0 || db->nVdbeRead>1)
96362 if( p->iStatement==0 ){
96363 assert( db->nStatement>=0 && db->nSavepoint>=0 );
96364 db->nStatement++;
96365 p->iStatement = db->nSavepoint + db->nStatement;
96368 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
96370 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
96376 p->nStmtDefCons = db->nDeferredCons;
96377 p->nStmtDefImmCons = db->nDeferredImmCons;
96380 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
96382 && pOp->p5
96383 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
96386 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
96390 sqlite3DbFree(db, p->zErrMsg);
96391 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
96392 /* If the schema-cookie from the database file matches the cookie
96393 ** stored with the in-memory representation of the schema, do
96396 ** If virtual-tables are in use, this is not just an optimization.
96397 ** Often, v-tables store their data in other SQLite tables, which
96398 ** are queried from within xNext() and other v-table methods using
96399 ** prepared queries. If such a query is out-of-date, we do not want to
96401 ** v-table would have to be ready for the sqlite3_vtab structure itself
96403 ** a v-table method.
96405 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
96406 sqlite3ResetOneSchema(db, pOp->p1);
96408 p->expired = 1;
96414 p->changeCntOn = 0;
96428 ** There must be a read-lock on the database (either a transaction
96437 assert( p->bIsReader );
96438 iDb = pOp->p1;
96439 iCookie = pOp->p3;
96440 assert( pOp->p3<SQLITE_N_BTREE_META );
96441 assert( iDb>=0 && iDb<db->nDb );
96442 assert( db->aDb[iDb].pBt!=0 );
96443 assert( DbMaskTest(p->btreeMask, iDb) );
96445 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
96447 pOut->u.i = iMeta;
96462 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
96470 assert( pOp->p2<SQLITE_N_BTREE_META );
96471 assert( pOp->p1>=0 && pOp->p1<db->nDb );
96472 assert( DbMaskTest(p->btreeMask, pOp->p1) );
96473 assert( p->readOnly==0 );
96474 pDb = &db->aDb[pOp->p1];
96475 assert( pDb->pBt!=0 );
96476 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
96478 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
96479 if( pOp->p2==BTREE_SCHEMA_VERSION ){
96481 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
96482 db->mDbFlags |= DBFLAG_SchemaChange;
96483 sqlite3FkClearTriggerCache(db, pOp->p1);
96484 }else if( pOp->p2==BTREE_FILE_FORMAT ){
96486 pDb->pSchema->file_format = pOp->p3;
96488 if( pOp->p1==1 ){
96492 p->expired = 0;
96501 ** Open a read-only cursor for the database table whose root page is
96518 ** object, then table being opened must be an [index b-tree] where the
96520 ** sequence of that index b-tree. Otherwise, if P4 is an integer
96521 ** value, then the table being opened must be a [table b-tree] with a
96531 ** b-tree and if it is this opcode becomes a no-op. In other words,
96553 ** OPFLAG_P2ISREG bit is set in P5 - see below).
96557 ** object, then table being opened must be an [index b-tree] where the
96559 ** sequence of that index b-tree. Otherwise, if P4 is an integer
96560 ** value, then the table being opened must be a [table b-tree] with a
96592 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
96593 assert( pOp->p4type==P4_KEYINFO );
96594 pCur = p->apCsr[pOp->p1];
96595 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
96596 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
96597 assert( pCur->eCurType==CURTYPE_BTREE );
96598 sqlite3BtreeClearCursor(pCur->uc.pCursor);
96606 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
96607 assert( p->bIsReader );
96608 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
96609 || p->readOnly==0 );
96611 if( p->expired==1 ){
96618 p2 = (u32)pOp->p2;
96619 iDb = pOp->p3;
96620 assert( iDb>=0 && iDb<db->nDb );
96621 assert( DbMaskTest(p->btreeMask, iDb) );
96622 pDb = &db->aDb[iDb];
96623 pX = pDb->pBt;
96625 if( pOp->opcode==OP_OpenWrite ){
96627 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
96629 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
96630 p->minWriteFileFormat = pDb->pSchema->file_format;
96635 if( pOp->p5 & OPFLAG_P2ISREG ){
96637 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
96638 assert( pOp->opcode==OP_OpenWrite );
96641 assert( (pIn2->flags & MEM_Int)!=0 );
96643 p2 = (int)pIn2->u.i;
96650 if( pOp->p4type==P4_KEYINFO ){
96651 pKeyInfo = pOp->p4.pKeyInfo;
96652 assert( pKeyInfo->enc==ENC(db) );
96653 assert( pKeyInfo->db==db );
96654 nField = pKeyInfo->nAllField;
96655 }else if( pOp->p4type==P4_INT32 ){
96656 nField = pOp->p4.i;
96658 assert( pOp->p1>=0 );
96661 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
96663 pCur->iDb = iDb;
96664 pCur->nullRow = 1;
96665 pCur->isOrdered = 1;
96666 pCur->pgnoRoot = p2;
96668 pCur->wrFlag = wrFlag;
96670 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
96671 pCur->pKeyInfo = pKeyInfo;
96673 ** SQLite used to check if the root-page flags were sane at this point
96676 pCur->isTable = pOp->p4type!=P4_KEYINFO;
96681 testcase( pOp->p5 & OPFLAG_BULKCSR );
96682 testcase( pOp->p2 & OPFLAG_SEEKEQ );
96683 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
96684 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
96695 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
96701 pOrig = p->apCsr[pOp->p2];
96703 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
96705 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
96707 pCx->nullRow = 1;
96708 pCx->isEphemeral = 1;
96709 pCx->pKeyInfo = pOrig->pKeyInfo;
96710 pCx->isTable = pOrig->isTable;
96711 pCx->pgnoRoot = pOrig->pgnoRoot;
96712 pCx->isOrdered = pOrig->isOrdered;
96713 pCx->ub.pBtx = pOrig->ub.pBtx;
96714 pCx->noReuse = 1;
96715 pOrig->noReuse = 1;
96716 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
96717 pCx->pKeyInfo, pCx->uc.pCursor);
96731 ** the main database is read-only. The ephemeral
96748 ** can be used as zero-length data for OP_Insert. This is an optimization
96770 assert( pOp->p1>=0 );
96771 assert( pOp->p2>=0 );
96772 if( pOp->p3>0 ){
96775 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
96776 assert( pOp->opcode==OP_OpenEphemeral );
96777 assert( aMem[pOp->p3].flags & MEM_Null );
96778 aMem[pOp->p3].n = 0;
96779 aMem[pOp->p3].z = "";
96781 pCx = p->apCsr[pOp->p1];
96782 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
96786 assert( pCx->isEphemeral );
96787 pCx->seqCount = 0;
96788 pCx->cacheStatus = CACHE_STALE;
96789 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
96791 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
96793 pCx->isEphemeral = 1;
96794 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
96795 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
96798 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
96803 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
96805 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
96806 assert( pOp->p4type==P4_KEYINFO );
96807 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
96808 BTREE_BLOBKEY | pOp->p5);
96810 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
96811 assert( pKeyInfo->db==db );
96812 assert( pKeyInfo->enc==ENC(db) );
96813 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
96814 pKeyInfo, pCx->uc.pCursor);
96816 pCx->isTable = 0;
96818 pCx->pgnoRoot = SCHEMA_ROOT;
96819 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
96820 0, pCx->uc.pCursor);
96821 pCx->isTable = 1;
96824 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
96826 sqlite3BtreeClose(pCx->ub.pBtx);
96831 pCx->nullRow = 1;
96839 ** tables using an external merge-sort algorithm.
96841 ** If argument P3 is non-zero, then it indicates that the sorter may
96842 ** assume that a stable sort considering the first P3 fields of each
96848 assert( pOp->p1>=0 );
96849 assert( pOp->p2>=0 );
96850 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
96852 pCx->pKeyInfo = pOp->p4.pKeyInfo;
96853 assert( pCx->pKeyInfo->db==db );
96854 assert( pCx->pKeyInfo->enc==ENC(db) );
96855 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
96869 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96870 pC = p->apCsr[pOp->p1];
96872 if( (pC->seqCount++)==0 ){
96886 ** A pseudo-table created by this opcode is used to hold a single
96889 ** is the only cursor opcode that works with a pseudo-table.
96892 ** the pseudo-table.
96897 assert( pOp->p1>=0 );
96898 assert( pOp->p3>=0 );
96899 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
96901 pCx->nullRow = 1;
96902 pCx->seekResult = pOp->p2;
96903 pCx->isTable = 1;
96904 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
96906 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
96908 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
96909 assert( pOp->p5==0 );
96916 ** currently open, this instruction is a no-op.
96919 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96920 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
96921 p->apCsr[pOp->p1] = 0;
96930 ** table or index for cursor P1 are used. P4 is a 64-bit integer
96933 ** by the cursor. The high-order bit is set if any column after
96938 pC = p->apCsr[pOp->p1];
96939 assert( pC->eCurType==CURTYPE_BTREE );
96940 pC->maskUsed = *(u64*)pOp->p4.pI64;
96948 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96975 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
96993 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97011 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
97047 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97048 assert( pOp->p2!=0 );
97049 pC = p->apCsr[pOp->p1];
97051 assert( pC->eCurType==CURTYPE_BTREE );
97055 assert( pC->isOrdered );
97056 assert( pC->uc.pCursor!=0 );
97057 oc = pOp->opcode;
97059 pC->nullRow = 0;
97061 pC->seekOp = pOp->opcode;
97064 pC->deferredMoveto = 0;
97065 pC->cacheStatus = CACHE_STALE;
97066 if( pC->isTable ){
97069 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
97075 pIn3 = &aMem[pOp->p3];
97076 flags3 = pIn3->flags;
97081 newType = pIn3->flags; /* Record the type after applying numeric affinity */
97082 pIn3->flags = flags3; /* But convert the type back to its original */
97093 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
97098 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
97104 ** (x > 4.9) -> (x >= 5)
97105 ** (x <= 4.9) -> (x < 5)
97108 assert( OP_SeekGE==(OP_SeekGT-1) );
97109 assert( OP_SeekLT==(OP_SeekLE-1) );
97111 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
97123 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
97124 pC->movetoTarget = iKey; /* Used by OP_Delete */
97134 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
97136 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
97138 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
97139 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
97146 nField = pOp->p4.i;
97147 assert( pOp->p4type==P4_INT32 );
97149 r.pKeyInfo = pC->pKeyInfo;
97154 ** r.default_rc = -1;
97159 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
97160 assert( oc!=OP_SeekGT || r.default_rc==-1 );
97161 assert( oc!=OP_SeekLE || r.default_rc==-1 );
97165 r.aMem = &aMem[pOp->p3];
97171 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
97176 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
97191 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
97207 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
97220 res = sqlite3BtreeEof(pC->uc.pCursor);
97224 assert( pOp->p2>0 );
97237 ** Synopsis: Scan-ahead up to P1 rows
97249 ** This opcode helps to optimize IN operators on a multi-column index
97252 ** of the b-tree instead. A correct answer is obtained if this opcode
97253 ** is omitted or is a no-op.
97260 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
97272 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
97312 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
97315 assert( pOp->p2>=(int)(pOp-aOp)+2 );
97317 if( pOp->p5==0 ){
97319 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
97320 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
97321 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
97322 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
97323 || aOp[pOp->p2-1].opcode==OP_IdxGE );
97324 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
97327 assert( pOp->p2==(int)(pOp-aOp)+2 );
97328 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
97332 assert( pOp->p1>0 );
97333 pC = p->apCsr[pOp[1].p1];
97335 assert( pC->eCurType==CURTYPE_BTREE );
97336 assert( !pC->isTable );
97337 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
97339 if( db->flags&SQLITE_VdbeTrace ){
97340 printf("... cursor not valid - fall through\n");
97345 nStep = pOp->p1;
97347 r.pKeyInfo = pC->pKeyInfo;
97364 if( res>0 && pOp->p5==0 ){
97368 if( db->flags&SQLITE_VdbeTrace ){
97369 printf("... %d steps and then skip\n", pOp->p1 - nStep);
97379 if( db->flags&SQLITE_VdbeTrace ){
97380 printf("... %d steps and then success\n", pOp->p1 - nStep);
97389 if( db->flags&SQLITE_VdbeTrace ){
97390 printf("... fall through after %d steps\n", pOp->p1);
97396 nStep--;
97397 pC->cacheStatus = CACHE_STALE;
97398 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
97423 ** early, thus saving work. This is part of the IN-early-out optimization.
97425 ** P1 must be a valid b-tree cursor.
97429 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97430 pC = p->apCsr[pOp->p1];
97432 assert( pOp->p3>=pOp->p2 );
97433 if( pC->seekHit<pOp->p2 ){
97435 if( db->flags&SQLITE_VdbeTrace ){
97436 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
97439 pC->seekHit = pOp->p2;
97440 }else if( pC->seekHit>pOp->p3 ){
97442 if( db->flags&SQLITE_VdbeTrace ){
97443 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
97446 pC->seekHit = pOp->p3;
97460 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97461 pCur = p->apCsr[pOp->p1];
97462 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
97463 if( pCur==0 || pCur->nullRow ){
97524 ** This opcode is used in IN clause processing for a multi-column key.
97526 ** left-most element, and if there are no matches on the most recent
97545 ** record are not-NULL then a check is done to determine if any row in the
97561 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97562 pC = p->apCsr[pOp->p1];
97565 if( db->flags&SQLITE_VdbeTrace ){
97566 printf("seekHit is %d\n", pC->seekHit);
97569 if( pC->seekHit>=pOp->p4.i ) break;
97583 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
97586 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97587 assert( pOp->p4type==P4_INT32 );
97588 pC = p->apCsr[pOp->p1];
97591 pC->seekOp = pOp->opcode;
97593 r.aMem = &aMem[pOp->p3];
97594 assert( pC->eCurType==CURTYPE_BTREE );
97595 assert( pC->uc.pCursor!=0 );
97596 assert( pC->isTable==0 );
97597 r.nField = (u16)pOp->p4.i;
97600 r.pKeyInfo = pC->pKeyInfo;
97606 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
97609 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
97612 assert( r.aMem->flags & MEM_Blob );
97613 assert( pOp->opcode!=OP_NoConflict );
97617 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
97619 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
97620 pIdxKey->default_rc = 0;
97621 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
97627 alreadyExists = (pC->seekResult==0);
97628 pC->nullRow = 1-alreadyExists;
97629 pC->deferredMoveto = 0;
97630 pC->cacheStatus = CACHE_STALE;
97631 if( pOp->opcode==OP_Found ){
97639 if( pOp->opcode==OP_NoConflict ){
97651 if( pOp->opcode==OP_IfNoHope ){
97652 pC->seekHit = pOp->p4.i;
97674 ** (with arbitrary multi-value keys).
97693 ** P3 register to contain a non-integer value, in which case the jump is
97697 ** (with arbitrary multi-value keys).
97711 pIn3 = &aMem[pOp->p3];
97712 testcase( pIn3->flags & MEM_Int );
97713 testcase( pIn3->flags & MEM_IntReal );
97714 testcase( pIn3->flags & MEM_Real );
97715 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
97716 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
97717 /* If pIn3->u.i does not contain an integer, compute iKey as the
97731 pIn3 = &aMem[pOp->p3];
97732 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
97733 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97734 iKey = pIn3->u.i;
97736 pC = p->apCsr[pOp->p1];
97739 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
97741 assert( pC->isTable );
97742 assert( pC->eCurType==CURTYPE_BTREE );
97743 pCrsr = pC->uc.pCursor;
97748 pC->movetoTarget = iKey; /* Used by OP_Delete */
97749 pC->nullRow = 0;
97750 pC->cacheStatus = CACHE_STALE;
97751 pC->deferredMoveto = 0;
97753 pC->seekResult = res;
97756 if( pOp->p2==0 ){
97775 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97776 assert( p->apCsr[pOp->p1]!=0 );
97777 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
97779 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
97812 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97813 pC = p->apCsr[pOp->p1];
97815 assert( pC->isTable );
97816 assert( pC->eCurType==CURTYPE_BTREE );
97817 assert( pC->uc.pCursor!=0 );
97820 ** thing) is obtained in a two-step algorithm.
97832 assert( pC->isTable );
97844 if( !pC->useRandomRowid ){
97845 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
97850 v = 1; /* IMP: R-61914-48074 */
97852 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
97853 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
97855 pC->useRandomRowid = 1;
97857 v++; /* IMP: R-29538-34987 */
97863 if( pOp->p3 ){
97865 assert( pOp->p3>0 );
97866 if( p->pFrame ){
97867 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
97869 assert( pOp->p3<=pFrame->nMem );
97870 pMem = &pFrame->aMem[pOp->p3];
97873 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
97874 pMem = &aMem[pOp->p3];
97879 REGISTER_TRACE(pOp->p3, pMem);
97881 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
97882 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
97883 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
97886 if( v<pMem->u.i+1 ){
97887 v = pMem->u.i + 1;
97889 pMem->u.i = v;
97892 if( pC->useRandomRowid ){
97893 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
97897 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
97903 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
97909 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
97912 assert( v>0 ); /* EV: R-40812-03570 */
97914 pC->deferredMoveto = 0;
97915 pC->cacheStatus = CACHE_STALE;
97917 pOut->u.i = v;
97946 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
97949 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
97950 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
97963 const char *zDb; /* database name - used by the update hook */
97964 Table *pTab; /* Table structure - used by update and pre-update hooks */
97967 pData = &aMem[pOp->p2];
97968 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97970 pC = p->apCsr[pOp->p1];
97972 assert( pC->eCurType==CURTYPE_BTREE );
97973 assert( pC->deferredMoveto==0 );
97974 assert( pC->uc.pCursor!=0 );
97975 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
97976 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
97977 REGISTER_TRACE(pOp->p2, pData);
97980 pKey = &aMem[pOp->p3];
97981 assert( pKey->flags & MEM_Int );
97983 REGISTER_TRACE(pOp->p3, pKey);
97984 x.nKey = pKey->u.i;
97986 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
97987 assert( pC->iDb>=0 );
97988 zDb = db->aDb[pC->iDb].zDbSName;
97989 pTab = pOp->p4.pTab;
97990 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
97997 /* Invoke the pre-update hook, if any */
97999 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
98000 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
98002 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
98003 /* Prevent post-update hook from running in cases when it should not */
98007 if( pOp->p5 & OPFLAG_ISNOOP ) break;
98010 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
98011 if( pOp->p5 & OPFLAG_NCHANGE ){
98012 p->nChange++;
98013 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
98015 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
98016 x.pData = pData->z;
98017 x.nData = pData->n;
98018 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
98019 if( pData->flags & MEM_Zero ){
98020 x.nZero = pData->u.nZero;
98026 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
98027 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
98030 pC->deferredMoveto = 0;
98031 pC->cacheStatus = CACHE_STALE;
98034 /* Invoke the update-hook if required. */
98037 assert( db->xUpdateCallback!=0 );
98038 assert( pTab->aCol!=0 );
98039 db->xUpdateCallback(db->pUpdateArg,
98040 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
98041 zDb, pTab->zName, x.nKey);
98049 ** of table - intkey or index. This opcode is used as part of copying
98062 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
98063 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
98065 pDest = p->apCsr[pOp->p1];
98066 pSrc = p->apCsr[pOp->p2];
98067 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
98068 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
98080 ** the next Next instruction will be a no-op. As a result, in this case
98095 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
98099 ** P1 must not be pseudo-table. It has to be a real table with
98103 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
98105 ** this case. Specifically, if one is configured, the pre-update hook is
98106 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
98119 opflags = pOp->p2;
98120 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98121 pC = p->apCsr[pOp->p1];
98123 assert( pC->eCurType==CURTYPE_BTREE );
98124 assert( pC->uc.pCursor!=0 );
98125 assert( pC->deferredMoveto==0 );
98129 if( pOp->p4type==P4_TABLE
98130 && HasRowid(pOp->p4.pTab)
98131 && pOp->p5==0
98132 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
98135 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
98137 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98138 assert( CORRUPT_DB || pC->movetoTarget==iKey );
98142 /* If the update-hook or pre-update-hook will be invoked, set zDb to
98147 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
98148 assert( pC->iDb>=0 );
98149 assert( pOp->p4.pTab!=0 );
98150 zDb = db->aDb[pC->iDb].zDbSName;
98151 pTab = pOp->p4.pTab;
98152 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
98153 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98161 /* Invoke the pre-update-hook if required. */
98162 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
98163 if( db->xPreUpdateCallback && pTab ){
98166 || (aMem[pOp->p3].flags & MEM_Int)
98170 zDb, pTab, pC->movetoTarget,
98171 pOp->p3, -1
98178 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
98183 if( p->pFrame==0 ){
98184 if( pC->isEphemeral==0
98185 && (pOp->p5 & OPFLAG_AUXDELETE)==0
98186 && (pC->wrFlag & OPFLAG_FORDELETE)==0
98190 if( pOp->p2 & OPFLAG_NCHANGE ){
98191 nExtraDelete--;
98196 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
98197 pC->cacheStatus = CACHE_STALE;
98199 pC->seekResult = 0;
98202 /* Invoke the update-hook if required. */
98204 p->nChange++;
98205 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
98206 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
98207 pC->movetoTarget);
98208 assert( pC->iDb>=0 );
98222 sqlite3VdbeSetChanges(db, p->nChange);
98223 p->nChange = 0;
98247 pC = p->apCsr[pOp->p1];
98249 assert( pOp->p4type==P4_INT32 );
98250 pIn3 = &aMem[pOp->p3];
98251 nKeyCol = pOp->p4.i;
98267 ** a register that is the source for a pseudo-table cursor created using
98268 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
98275 pOut = &aMem[pOp->p2];
98276 pC = p->apCsr[pOp->p1];
98279 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
98280 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98282 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
98299 ** of a real table, not a pseudo-table.
98303 ** register will be invalidated as soon as the cursor moves - including
98321 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98322 pC = p->apCsr[pOp->p1];
98324 assert( pC->eCurType==CURTYPE_BTREE );
98326 assert( pC->nullRow==0 );
98327 assert( pC->uc.pCursor!=0 );
98328 pCrsr = pC->uc.pCursor;
98338 assert( pC->deferredMoveto==0 );
98342 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
98348 if( !pOp->p3 ) Deephemeralize(pOut);
98350 REGISTER_TRACE(pOp->p2, pOut);
98371 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98372 pC = p->apCsr[pOp->p1];
98374 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
98375 if( pC->nullRow ){
98376 pOut->flags = MEM_Null;
98378 }else if( pC->deferredMoveto ){
98379 v = pC->movetoTarget;
98381 }else if( pC->eCurType==CURTYPE_VTAB ){
98382 assert( pC->uc.pVCur!=0 );
98383 pVtab = pC->uc.pVCur->pVtab;
98384 pModule = pVtab->pModule;
98385 assert( pModule->xRowid );
98386 rc = pModule->xRowid(pC->uc.pVCur, &v);
98391 assert( pC->eCurType==CURTYPE_BTREE );
98392 assert( pC->uc.pCursor!=0 );
98395 if( pC->nullRow ){
98396 pOut->flags = MEM_Null;
98399 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
98401 pOut->u.i = v;
98412 ** pseudo-cursor that always returns NULL for every column.
98417 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98418 pC = p->apCsr[pOp->p1];
98421 ** pseudo-cursor that always gives null rows. */
98422 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
98424 pC->seekResult = 0;
98425 pC->isTable = 1;
98426 pC->noReuse = 1;
98427 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
98429 pC->nullRow = 1;
98430 pC->cacheStatus = CACHE_STALE;
98431 if( pC->eCurType==CURTYPE_BTREE ){
98432 assert( pC->uc.pCursor!=0 );
98433 sqlite3BtreeClearCursor(pC->uc.pCursor);
98436 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
98469 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98470 pC = p->apCsr[pOp->p1];
98472 assert( pC->eCurType==CURTYPE_BTREE );
98473 pCrsr = pC->uc.pCursor;
98477 pC->seekOp = pOp->opcode;
98479 if( pOp->opcode==OP_SeekEnd ){
98480 assert( pOp->p2==0 );
98481 pC->seekResult = -1;
98487 pC->nullRow = (u8)res;
98488 pC->deferredMoveto = 0;
98489 pC->cacheStatus = CACHE_STALE;
98491 if( pOp->p2>0 ){
98509 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98510 pC = p->apCsr[pOp->p1];
98512 pCrsr = pC->uc.pCursor;
98518 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
98551 sqlite3_search_count--;
98553 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
98577 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98578 assert( pOp->p5==0 );
98579 assert( pOp->p2>=0 && pOp->p2<p->nOp );
98581 pC = p->apCsr[pOp->p1];
98583 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
98586 pC->seekOp = OP_Rewind;
98591 assert( pC->eCurType==CURTYPE_BTREE );
98592 pCrsr = pC->uc.pCursor;
98595 pC->deferredMoveto = 0;
98596 pC->cacheStatus = CACHE_STALE;
98599 pC->nullRow = (u8)res;
98600 if( pOp->p2>0 ){
98618 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
98627 ** number P5-1 in the prepared statement is incremented.
98643 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
98652 ** number P5-1 in the prepared statement is incremented.
98664 pC = p->apCsr[pOp->p1];
98670 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98671 assert( pOp->p5==0
98672 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
98673 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
98674 pC = p->apCsr[pOp->p1];
98676 assert( pC->deferredMoveto==0 );
98677 assert( pC->eCurType==CURTYPE_BTREE );
98678 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
98679 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
98680 || pC->seekOp==OP_NullRow);
98681 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
98685 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98686 assert( pOp->p5==0
98687 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
98688 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
98689 pC = p->apCsr[pOp->p1];
98691 assert( pC->deferredMoveto==0 );
98692 assert( pC->eCurType==CURTYPE_BTREE );
98693 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
98694 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
98695 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
98696 || pC->seekOp==OP_IfNoHope);
98697 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
98700 pC->cacheStatus = CACHE_STALE;
98703 pC->nullRow = 0;
98704 p->aCounter[pOp->p5]++;
98712 pC->nullRow = 1;
98728 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
98748 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98749 pC = p->apCsr[pOp->p1];
98753 pIn2 = &aMem[pOp->p2];
98754 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
98755 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
98756 assert( pC->eCurType==CURTYPE_BTREE );
98757 assert( pC->isTable==0 );
98760 x.nKey = pIn2->n;
98761 x.pKey = pIn2->z;
98762 x.aMem = aMem + pOp->p3;
98763 x.nMem = (u16)pOp->p4.i;
98764 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
98765 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
98766 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
98768 assert( pC->deferredMoveto==0 );
98769 pC->cacheStatus = CACHE_STALE;
98784 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98785 pC = p->apCsr[pOp->p1];
98789 pIn2 = &aMem[pOp->p2];
98790 assert( pIn2->flags & MEM_Blob );
98791 assert( pC->isTable==0 );
98812 ** this (self-correcting and non-critical) error if in writable_schema mode.
98820 assert( pOp->p3>0 );
98821 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
98822 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98823 pC = p->apCsr[pOp->p1];
98825 assert( pC->eCurType==CURTYPE_BTREE );
98827 pCrsr = pC->uc.pCursor;
98829 r.pKeyInfo = pC->pKeyInfo;
98830 r.nField = (u16)pOp->p3;
98832 r.aMem = &aMem[pOp->p2];
98838 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
98842 assert( pC->deferredMoveto==0 );
98843 pC->cacheStatus = CACHE_STALE;
98844 pC->seekResult = 0;
98861 ** is non-zero, then reading column a(i)-1 from cursor P3 is
98882 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98883 pC = p->apCsr[pOp->p1];
98885 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
98886 assert( pC->uc.pCursor!=0 );
98887 assert( pC->isTable==0 || IsNullCursor(pC) );
98888 assert( pC->deferredMoveto==0 );
98889 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
98900 if( !pC->nullRow ){
98902 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
98906 if( pOp->opcode==OP_DeferredSeek ){
98907 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
98908 pTabCur = p->apCsr[pOp->p3];
98910 assert( pTabCur->eCurType==CURTYPE_BTREE );
98911 assert( pTabCur->uc.pCursor!=0 );
98912 assert( pTabCur->isTable );
98913 pTabCur->nullRow = 0;
98914 pTabCur->movetoTarget = rowid;
98915 pTabCur->deferredMoveto = 1;
98916 pTabCur->cacheStatus = CACHE_STALE;
98917 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
98918 assert( !pTabCur->isEphemeral );
98919 pTabCur->ub.aAltMap = pOp->p4.ai;
98920 assert( !pC->isEphemeral );
98921 pTabCur->pAltCursor = pC;
98924 pOut->u.i = rowid;
98927 assert( pOp->opcode==OP_IdxRowid );
98928 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
98937 ** already occurred, this instruction is a no-op.
98942 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98943 pC = p->apCsr[pOp->p1];
98944 if( pC->deferredMoveto ){
99003 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99004 pC = p->apCsr[pOp->p1];
99006 assert( pC->isOrdered );
99007 assert( pC->eCurType==CURTYPE_BTREE );
99008 assert( pC->uc.pCursor!=0);
99009 assert( pC->deferredMoveto==0 );
99010 assert( pOp->p4type==P4_INT32 );
99011 r.pKeyInfo = pC->pKeyInfo;
99012 r.nField = (u16)pOp->p4.i;
99013 if( pOp->opcode<OP_IdxLT ){
99014 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
99015 r.default_rc = -1;
99017 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
99020 r.aMem = &aMem[pOp->p3];
99026 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
99037 assert( pC->eCurType==CURTYPE_BTREE );
99038 pCur = pC->uc.pCursor;
99056 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
99057 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
99058 res = -res;
99060 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
99081 ** value of the root page that moved - its value before the move occurred -
99092 ** and non-autovacuum modes.
99101 assert( p->readOnly==0 );
99102 assert( pOp->p1>1 );
99104 pOut->flags = MEM_Null;
99105 if( db->nVdbeRead > db->nVDestroy+1 ){
99107 p->errorAction = OE_Abort;
99110 iDb = pOp->p3;
99111 assert( DbMaskTest(p->btreeMask, iDb) );
99113 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
99114 pOut->flags = MEM_Int;
99115 pOut->u.i = iMoved;
99119 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
99139 ** If the P3 value is non-zero, then the row change count is incremented
99151 assert( p->readOnly==0 );
99152 assert( DbMaskTest(p->btreeMask, pOp->p2) );
99153 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
99154 if( pOp->p3 ){
99155 p->nChange += nChange;
99156 if( pOp->p3>0 ){
99157 assert( memIsValid(&aMem[pOp->p3]) );
99158 memAboutToChange(p, &aMem[pOp->p3]);
99159 aMem[pOp->p3].u.i += nChange;
99177 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99178 pC = p->apCsr[pOp->p1];
99181 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
99183 assert( pC->eCurType==CURTYPE_BTREE );
99184 assert( pC->isEphemeral );
99185 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
99194 ** Allocate a new b-tree in the main database file if P1==0 or in the
99198 ** The root page number of the new b-tree is stored in register P2.
99207 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
99208 assert( pOp->p1>=0 && pOp->p1<db->nDb );
99209 assert( DbMaskTest(p->btreeMask, pOp->p1) );
99210 assert( p->readOnly==0 );
99211 pDb = &db->aDb[pOp->p1];
99212 assert( pDb->pBt!=0 );
99213 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
99215 pOut->u.i = pgno;
99233 db->nSqlExec++;
99236 xAuth = db->xAuth;
99238 mTrace = db->mTrace;
99239 if( pOp->p1 ){
99241 db->xAuth = 0;
99243 db->mTrace = 0;
99245 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
99246 db->nSqlExec--;
99248 db->xAuth = xAuth;
99250 db->mTrace = mTrace;
99267 ** then runs the new virtual machine. It is thus a re-entrant opcode.
99280 for(iDb=0; iDb<db->nDb; iDb++){
99281 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
99285 iDb = pOp->p1;
99286 assert( iDb>=0 && iDb<db->nDb );
99288 || db->mallocFailed
99289 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
99292 if( pOp->p4.z==0 ){
99293 sqlite3SchemaClear(db->aDb[iDb].pSchema);
99294 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
99295 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
99296 db->mDbFlags |= DBFLAG_SchemaChange;
99297 p->expired = 0;
99304 initData.pzErrMsg = &p->zErrMsg;
99306 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
99309 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
99313 assert( db->init.busy==0 );
99314 db->init.busy = 1;
99317 assert( !db->mallocFailed );
99321 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
99327 db->init.busy = 0;
99348 assert( pOp->p1>=0 && pOp->p1<db->nDb );
99349 rc = sqlite3AnalysisLoad(db, pOp->p1);
99357 ** Remove the internal (in-memory) data structures that describe
99365 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
99371 ** Remove the internal (in-memory) data structures that describe
99379 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
99385 ** Remove the internal (in-memory) data structures that describe
99393 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
99425 assert( p->bIsReader );
99426 nRoot = pOp->p2;
99427 aRoot = pOp->p4.ai;
99430 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
99431 pnErr = &aMem[pOp->p3];
99432 assert( (pnErr->flags & MEM_Int)!=0 );
99433 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
99434 pIn1 = &aMem[pOp->p1];
99435 assert( pOp->p5<db->nDb );
99436 assert( DbMaskTest(p->btreeMask, pOp->p5) );
99437 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
99438 (int)pnErr->u.i+1, &nErr, &z);
99446 pnErr->u.i -= nErr-1;
99447 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
99464 pIn1 = &aMem[pOp->p1];
99465 pIn2 = &aMem[pOp->p2];
99466 assert( (pIn2->flags & MEM_Int)!=0 );
99467 if( (pIn1->flags & MEM_Blob)==0 ){
99471 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
99486 pIn1 = &aMem[pOp->p1];
99487 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
99488 if( (pIn1->flags & MEM_Blob)==0
99489 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
99498 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
99506 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
99515 ** must have P4==0, the final set must have P4==-1, and for all other sets
99520 ** (b) when P4==-1 there is no need to insert the value, as it will
99530 pIn1 = &aMem[pOp->p1];
99531 pIn3 = &aMem[pOp->p3];
99532 iSet = pOp->p4.i;
99533 assert( pIn3->flags&MEM_Int );
99538 if( (pIn1->flags & MEM_Blob)==0 ){
99542 assert( pOp->p4type==P4_INT32 );
99543 assert( iSet==-1 || iSet>=0 );
99545 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
99550 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
99563 ** cell in an array of values used as arguments to the sub-program. P2
99564 ** contains the address to jump to if the sub-program throws an IGNORE
99567 ** memory required by the sub-vdbe at runtime.
99571 ** If P5 is non-zero, then recursive program invocation is enabled.
99574 int nMem; /* Number of memory registers for sub-program */
99575 int nByte; /* Bytes of runtime space required for sub-program */
99580 SubProgram *pProgram; /* Sub-program to execute */
99583 pProgram = pOp->p4.pProgram;
99584 pRt = &aMem[pOp->p3];
99585 assert( pProgram->nOp>0 );
99588 ** disabled for backwards compatibility (p5 is set if this sub-program
99598 if( pOp->p5 ){
99599 t = pProgram->token;
99600 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
99604 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
99614 if( (pRt->flags&MEM_Blob)==0 ){
99620 nMem = pProgram->nMem + pProgram->nCsr;
99622 if( pProgram->nCsr==0 ) nMem++;
99625 + pProgram->nCsr * sizeof(VdbeCursor*)
99626 + (pProgram->nOp + 7)/8;
99632 pRt->flags = MEM_Blob|MEM_Dyn;
99633 pRt->z = (char*)pFrame;
99634 pRt->n = nByte;
99635 pRt->xDel = sqlite3VdbeFrameMemDel;
99637 pFrame->v = p;
99638 pFrame->nChildMem = nMem;
99639 pFrame->nChildCsr = pProgram->nCsr;
99640 pFrame->pc = (int)(pOp - aOp);
99641 pFrame->aMem = p->aMem;
99642 pFrame->nMem = p->nMem;
99643 pFrame->apCsr = p->apCsr;
99644 pFrame->nCursor = p->nCursor;
99645 pFrame->aOp = p->aOp;
99646 pFrame->nOp = p->nOp;
99647 pFrame->token = pProgram->token;
99649 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
99652 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
99654 pMem->flags = MEM_Undefined;
99655 pMem->db = db;
99658 pFrame = (VdbeFrame*)pRt->z;
99659 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
99660 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
99661 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
99662 assert( pProgram->nCsr==pFrame->nChildCsr );
99663 assert( (int)(pOp - aOp)==pFrame->pc );
99666 p->nFrame++;
99667 pFrame->pParent = p->pFrame;
99668 pFrame->lastRowid = db->lastRowid;
99669 pFrame->nChange = p->nChange;
99670 pFrame->nDbChange = p->db->nChange;
99671 assert( pFrame->pAuxData==0 );
99672 pFrame->pAuxData = p->pAuxData;
99673 p->pAuxData = 0;
99674 p->nChange = 0;
99675 p->pFrame = pFrame;
99676 p->aMem = aMem = VdbeFrameMem(pFrame);
99677 p->nMem = pFrame->nChildMem;
99678 p->nCursor = (u16)pFrame->nChildCsr;
99679 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
99680 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
99681 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
99682 p->aOp = aOp = pProgram->aOp;
99683 p->nOp = pProgram->nOp;
99689 for(i=0; i<p->nMem; i++){
99690 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
99695 pOp = &aOp[-1];
99701 ** This opcode is only ever present in sub-programs called via the
99715 pFrame = p->pFrame;
99716 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
99728 ** If P1 is non-zero, the database constraint counter is incremented
99733 if( db->flags & SQLITE_DeferFKs ){
99734 db->nDeferredImmCons += pOp->p2;
99735 }else if( pOp->p1 ){
99736 db->nDeferredCons += pOp->p2;
99738 p->nFkConstraint += pOp->p2;
99746 ** This opcode tests if a foreign key constraint-counter is currently zero.
99750 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
99752 ** zero, the jump is taken if the statement constraint-counter is zero
99756 if( pOp->p1 ){
99757 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
99758 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
99760 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
99761 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
99773 ** within a sub-program). Set the value of register P1 to the maximum of
99781 if( p->pFrame ){
99782 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
99783 pIn1 = &pFrame->aMem[pOp->p1];
99785 pIn1 = &aMem[pOp->p1];
99789 pIn2 = &aMem[pOp->p2];
99791 if( pIn1->u.i<pIn2->u.i){
99792 pIn1->u.i = pIn2->u.i;
99799 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
99809 pIn1 = &aMem[pOp->p1];
99810 assert( pIn1->flags&MEM_Int );
99811 VdbeBranchTaken( pIn1->u.i>0, 2);
99812 if( pIn1->u.i>0 ){
99813 pIn1->u.i -= pOp->p3;
99820 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
99833 ** and r[P2] is set to -1.
99839 pIn1 = &aMem[pOp->p1];
99840 pIn3 = &aMem[pOp->p3];
99842 assert( pIn1->flags & MEM_Int );
99843 assert( pIn3->flags & MEM_Int );
99844 x = pIn1->u.i;
99845 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
99853 pOut->u.i = -1;
99855 pOut->u.i = x;
99861 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
99865 ** If it is non-zero (negative or positive) and then also jump to P2.
99869 pIn1 = &aMem[pOp->p1];
99870 assert( pIn1->flags&MEM_Int );
99871 VdbeBranchTaken(pIn1->u.i<0, 2);
99872 if( pIn1->u.i ){
99873 if( pIn1->u.i>0 ) pIn1->u.i--;
99880 ** Synopsis: if (--r[P1])==0 goto P2
99886 pIn1 = &aMem[pOp->p1];
99887 assert( pIn1->flags&MEM_Int );
99888 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
99889 VdbeBranchTaken(pIn1->u.i==0, 2);
99890 if( pIn1->u.i==0 ) goto jump_to_p2;
99939 assert( pOp->p4type==P4_FUNCDEF );
99940 n = pOp->p5;
99941 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
99942 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
99943 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
99945 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
99947 pCtx->pMem = 0;
99948 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
99949 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
99950 pCtx->pFunc = pOp->p4.pFunc;
99951 pCtx->iOp = (int)(pOp - aOp);
99952 pCtx->pVdbe = p;
99953 pCtx->skipFlag = 0;
99954 pCtx->isError = 0;
99955 pCtx->enc = encoding;
99956 pCtx->argc = n;
99957 pOp->p4type = P4_FUNCCTX;
99958 pOp->p4.pCtx = pCtx;
99961 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
99963 pOp->opcode = OP_AggStep1;
99972 assert( pOp->p4type==P4_FUNCCTX );
99973 pCtx = pOp->p4.pCtx;
99974 pMem = &aMem[pOp->p3];
99977 if( pOp->p1 ){
99980 assert( pMem->uTemp==0x1122e0e3 );
99983 pMem->uTemp = 0x1122e0e3;
99991 if( pCtx->pMem != pMem ){
99992 pCtx->pMem = pMem;
99993 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
99997 for(i=0; i<pCtx->argc; i++){
99998 assert( memIsValid(pCtx->argv[i]) );
99999 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
100003 pMem->n++;
100004 assert( pCtx->pOut->flags==MEM_Null );
100005 assert( pCtx->isError==0 );
100006 assert( pCtx->skipFlag==0 );
100008 if( pOp->p1 ){
100009 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
100012 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
100014 if( pCtx->isError ){
100015 if( pCtx->isError>0 ){
100016 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
100017 rc = pCtx->isError;
100019 if( pCtx->skipFlag ){
100020 assert( pOp[-1].opcode==OP_CollSeq );
100021 i = pOp[-1].p1;
100023 pCtx->skipFlag = 0;
100025 sqlite3VdbeMemRelease(pCtx->pOut);
100026 pCtx->pOut->flags = MEM_Null;
100027 pCtx->isError = 0;
100030 assert( pCtx->pOut->flags==MEM_Null );
100031 assert( pCtx->skipFlag==0 );
100064 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100065 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
100066 pMem = &aMem[pOp->p1];
100067 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
100069 if( pOp->p3 ){
100070 memAboutToChange(p, &aMem[pOp->p3]);
100071 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
100072 pMem = &aMem[pOp->p3];
100076 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
100085 REGISTER_TRACE((int)(pMem-aMem), pMem);
100092 ** Checkpoint database P1. This is a no-op if P1 is not currently in
100099 ** mem[P3+2] are initialized to -1.
100106 assert( p->readOnly==0 );
100108 aRes[1] = aRes[2] = -1;
100109 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
100110 || pOp->p2==SQLITE_CHECKPOINT_FULL
100111 || pOp->p2==SQLITE_CHECKPOINT_RESTART
100112 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
100114 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
100120 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
100137 ** Write a string containing the final journal-mode to register P2.
100149 eNew = pOp->p3;
100158 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100159 assert( p->readOnly==0 );
100161 pBt = db->aDb[pOp->p1].pBt;
100176 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
100184 if( !db->autoCommit || db->nVdbeRead>1 ){
100195 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
100223 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
100224 pOut->z = (char *)sqlite3JournalModename(eNew);
100225 pOut->n = sqlite3Strlen30(pOut->z);
100226 pOut->enc = SQLITE_UTF8;
100244 assert( p->readOnly==0 );
100245 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
100246 pOp->p2 ? &aMem[pOp->p2] : 0);
100262 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100263 assert( DbMaskTest(p->btreeMask, pOp->p1) );
100264 assert( p->readOnly==0 );
100265 pBt = db->aDb[pOp->p1].pBt;
100284 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
100294 assert( pOp->p2==0 || pOp->p2==1 );
100295 if( !pOp->p1 ){
100296 sqlite3ExpirePreparedStatements(db, pOp->p2);
100298 p->expired = pOp->p2+1;
100310 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100311 pC = p->apCsr[pOp->p1];
100313 assert( pC->eCurType==CURTYPE_BTREE );
100314 sqlite3BtreeCursorPin(pC->uc.pCursor);
100325 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100326 pC = p->apCsr[pOp->p1];
100328 assert( pC->eCurType==CURTYPE_BTREE );
100329 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
100338 ** the shared-cache feature is enabled.
100344 ** P2 contains the root-page of the table to lock.
100350 u8 isWriteLock = (u8)pOp->p3;
100351 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
100352 int p1 = pOp->p1;
100353 assert( p1>=0 && p1<db->nDb );
100354 assert( DbMaskTest(p->btreeMask, p1) );
100356 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
100359 const char *z = pOp->p4.z;
100381 pVTab = pOp->p4.pVtab;
100383 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
100403 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
100404 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
100405 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
100408 assert( zTab || db->mallocFailed );
100410 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
100425 db->nVDestroy++;
100426 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
100427 db->nVDestroy--;
100428 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
100447 assert( p->bIsReader );
100450 pVtab = pOp->p4.pVtab->pVtab;
100451 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
100455 pModule = pVtab->pModule;
100456 rc = pModule->xOpen(pVtab, &pVCur);
100461 pVCur->pVtab = pVtab;
100464 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
100466 pCur->uc.pVCur = pVCur;
100467 pVtab->nRef++;
100469 assert( db->mallocFailed );
100470 pModule->xClose(pVCur);
100493 pOut = &aMem[pOp->p2];
100495 assert( pOp->p4type==P4_TABLEREF );
100496 pTab = pOp->p4.pTab;
100498 assert( pTab->nTabRef>0 );
100500 if( pTab->u.vtab.p==0 ) break;
100501 pVtab = pTab->u.vtab.p->pVtab;
100503 pModule = pVtab->pModule;
100505 assert( pModule->iVersion>=4 );
100506 assert( pModule->xIntegrity!=0 );
100507 sqlite3VtabLock(pTab->u.vtab.p);
100508 assert( pOp->p1>=0 && pOp->p1<db->nDb );
100509 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
100510 pOp->p3, &zErr);
100511 sqlite3VtabUnlock(pTab->u.vtab.p);
100517 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free);
100538 pC = p->apCsr[pOp->p1];
100541 pRhs->pCsr = pC->uc.pCursor;
100542 pRhs->pOut = &aMem[pOp->p3];
100544 pOut->flags = MEM_Null;
100584 pQuery = &aMem[pOp->p3];
100586 pCur = p->apCsr[pOp->p1];
100588 REGISTER_TRACE(pOp->p3, pQuery);
100590 assert( pCur->eCurType==CURTYPE_VTAB );
100591 pVCur = pCur->uc.pVCur;
100592 pVtab = pVCur->pVtab;
100593 pModule = pVtab->pModule;
100596 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
100597 nArg = (int)pArgc->u.i;
100598 iQuery = (int)pQuery->u.i;
100601 apArg = p->apArg;
100605 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
100608 res = pModule->xEof(pVCur);
100609 pCur->nullRow = 0;
100620 ** Store in register P3 the value of the P2-th column of
100621 ** the current row of the virtual-table of cursor P1.
100637 VdbeCursor *pCur = p->apCsr[pOp->p1];
100639 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100640 pDest = &aMem[pOp->p3];
100642 if( pCur->nullRow ){
100646 assert( pCur->eCurType==CURTYPE_VTAB );
100647 pVtab = pCur->uc.pVCur->pVtab;
100648 pModule = pVtab->pModule;
100649 assert( pModule->xColumn );
100653 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
100654 if( pOp->p5 & OPFLAG_NOCHNG ){
100656 pDest->flags = MEM_Null|MEM_Zero;
100657 pDest->u.nZero = 0;
100661 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
100668 REGISTER_TRACE(pOp->p3, pDest);
100689 pCur = p->apCsr[pOp->p1];
100691 assert( pCur->eCurType==CURTYPE_VTAB );
100692 if( pCur->nullRow ){
100695 pVtab = pCur->uc.pVCur->pVtab;
100696 pModule = pVtab->pModule;
100697 assert( pModule->xNext );
100705 rc = pModule->xNext(pCur->uc.pVCur);
100708 res = pModule->xEof(pCur->uc.pVCur);
100730 isLegacy = (db->flags & SQLITE_LegacyAlter);
100731 db->flags |= SQLITE_LegacyAlter;
100732 pVtab = pOp->p4.pVtab->pVtab;
100733 pName = &aMem[pOp->p1];
100734 assert( pVtab->pModule->xRename );
100736 assert( p->readOnly==0 );
100737 REGISTER_TRACE(pOp->p1, pName);
100738 assert( pName->flags & MEM_Str );
100739 testcase( pName->enc==SQLITE_UTF8 );
100740 testcase( pName->enc==SQLITE_UTF16BE );
100741 testcase( pName->enc==SQLITE_UTF16LE );
100744 rc = pVtab->pModule->xRename(pVtab, pName->z);
100745 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
100747 p->expired = 0;
100760 ** invocation. The value in register (P3+P2-1) corresponds to the
100790 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
100791 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
100793 assert( p->readOnly==0 );
100794 if( db->mallocFailed ) goto no_mem;
100796 pVtab = pOp->p4.pVtab->pVtab;
100797 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
100801 pModule = pVtab->pModule;
100802 nArg = pOp->p2;
100803 assert( pOp->p4type==P4_VTAB );
100804 if( ALWAYS(pModule->xUpdate) ){
100805 u8 vtabOnConflict = db->vtabOnConflict;
100806 apArg = p->apArg;
100807 pX = &aMem[pOp->p3];
100814 db->vtabOnConflict = pOp->p5;
100815 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
100816 db->vtabOnConflict = vtabOnConflict;
100818 if( rc==SQLITE_OK && pOp->p1 ){
100819 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
100820 db->lastRowid = rowid;
100822 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
100823 if( pOp->p5==OE_Ignore ){
100826 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
100829 p->nChange++;
100844 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
100864 pBt = db->aDb[pOp->p1].pBt;
100866 if( pOp->p3 ){
100868 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
100870 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
100885 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
100904 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
100913 ** purely non-deterministic. Some built-in date/time functions can be
100914 ** either deterministic of non-deterministic, depending on their arguments.
100915 ** When those function are used in a non-deterministic way, they will check
100926 assert( pOp->p4type==P4_FUNCCTX );
100927 pCtx = pOp->p4.pCtx;
100933 pOut = &aMem[pOp->p3];
100934 if( pCtx->pOut != pOut ){
100935 pCtx->pVdbe = p;
100936 pCtx->pOut = pOut;
100937 pCtx->enc = encoding;
100938 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
100940 assert( pCtx->pVdbe==p );
100944 for(i=0; i<pCtx->argc; i++){
100945 assert( memIsValid(pCtx->argv[i]) );
100946 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
100950 assert( pCtx->isError==0 );
100951 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
100954 if( pCtx->isError ){
100955 if( pCtx->isError>0 ){
100957 rc = pCtx->isError;
100959 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
100960 pCtx->isError = 0;
100964 assert( (pOut->flags&MEM_Str)==0
100965 || pOut->enc==encoding
100966 || db->mallocFailed );
100969 REGISTER_TRACE(pOp->p3, pOut);
100980 pIn1 = &aMem[pOp->p1];
100981 pIn1->flags &= ~MEM_Subtype;
100994 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
100995 pIn1 = &aMem[pOp->p1];
100996 assert( pIn1->flags & MEM_Blob );
100997 assert( pIn1->n>0 );
101000 if( db->flags&SQLITE_VdbeTrace ){
101002 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101005 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101008 h %= (pIn1->n*8);
101009 pIn1->z[h/8] |= 1<<(h&7);
101025 ** false positive - if the jump is taken when it should fall through.
101030 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
101031 pIn1 = &aMem[pOp->p1];
101032 assert( (pIn1->flags & MEM_Blob)!=0 );
101033 assert( pIn1->n >= 1 );
101036 if( db->flags&SQLITE_VdbeTrace ){
101038 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
101041 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
101044 h %= (pIn1->n*8);
101045 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
101047 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
101050 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
101070 ** the UTF-8 string contained in P4 is emitted on the trace callback.
101089 ** The "--" string is broken up to prevent false-positives with srcck1.c.
101092 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
101094 ** using the X argument when X begins with "--" and invoking
101097 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
101100 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
101103 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
101104 && p->minWriteFileFormat!=254 /* tag-20220401a */
101105 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
101108 if( db->mTrace & SQLITE_TRACE_LEGACY ){
101110 db->trace.xLegacy(db->pTraceArg, z);
101114 if( db->nVdbeExec>1 ){
101115 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
101116 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
101119 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
101123 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
101126 for(j=0; j<db->nDb; j++){
101127 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
101128 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
101133 if( (db->flags & SQLITE_SqlTrace)!=0
101134 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
101136 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
101140 assert( pOp->p2>0 );
101141 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
101142 if( pOp->opcode==OP_Trace ) break;
101143 for(i=1; i<p->nOp; i++){
101144 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
101146 pOp->p1 = 0;
101148 pOp->p1++;
101149 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
101159 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
101164 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101165 assert( pOp->p4type==P4_EXPR );
101166 pC = p->apCsr[pOp->p1];
101168 assert( pC->eCurType==CURTYPE_BTREE );
101169 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
101170 pOp->p4.pExpr, aMem);
101227 assert( pOp->p1>0 );
101228 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
101229 pMem = &aMem[pOp->p1];
101230 constMask = pOp->p3;
101231 for(i=0; i<pOp->p2; i++, pMem++){
101233 pMem->pScopyFrom = 0;
101234 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
101250 ** the same as a no-op. This opcodesnever appears in a real VM program.
101253 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
101260 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
101282 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
101285 if( db->flags & SQLITE_VdbeTrace ){
101286 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
101289 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
101292 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
101309 if( db->mallocFailed ){
101316 if( db->flags & SQLITE_VdbeTrace ){
101317 const char *zTrace = p->zSql;
101324 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
101327 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
101330 p->rc = rc;
101334 (int)(pOp - aOp), p->zSql, p->zErrMsg);
101335 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
101337 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
101338 db->flags |= SQLITE_CorruptRdOnly;
101342 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
101362 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
101363 nProgressLimit += db->nProgressOps;
101364 if( db->xProgress(db->pProgressArg) ){
101371 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
101372 if( DbMaskNonZero(p->lockMask) ){
101376 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
101400 assert( AtomicLoad(&db->u1.isInterrupted) );
101446 ** the b-tree cursor associated with blob handle p to point to row iRow.
101457 ** If an error does occur, then the b-tree cursor is closed. All subsequent
101464 Vdbe *v = (Vdbe *)p->pStmt;
101469 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
101475 if( v->pc>4 ){
101476 v->pc = 4;
101477 assert( v->aOp[v->pc].opcode==OP_NotExists );
101480 rc = sqlite3_step(p->pStmt);
101483 VdbeCursor *pC = v->apCsr[0];
101486 assert( pC->eCurType==CURTYPE_BTREE );
101487 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
101488 testcase( pC->nHdrParsed==p->iCol );
101489 testcase( pC->nHdrParsed==p->iCol+1 );
101491 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
101495 sqlite3_finalize(p->pStmt);
101496 p->pStmt = 0;
101498 p->iOffset = pC->aType[p->iCol + pC->nField];
101499 p->nByte = sqlite3VdbeSerialTypeLen(type);
101500 p->pCsr = pC->uc.pCursor;
101501 sqlite3BtreeIncrblobCursor(p->pCsr);
101507 }else if( p->pStmt ){
101508 rc = sqlite3_finalize(p->pStmt);
101509 p->pStmt = 0;
101511 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
101514 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
101534 int wrFlag, /* True -> read/write access, false -> read-only */
101538 int iCol; /* Index of zColumn in row-record */
101558 sqlite3_mutex_enter(db->mutex);
101593 pBlob->pTab = pTab;
101594 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
101597 for(iCol=0; iCol<pTab->nCol; iCol++) {
101598 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
101602 if( iCol==pTab->nCol ){
101617 if( db->flags&SQLITE_ForeignKeys ){
101624 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
101626 for(j=0; j<pFKey->nCol; j++){
101627 if( pFKey->aCol[j].iFrom==iCol ){
101634 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
101636 for(j=0; j<pIdx->nKeyCol; j++){
101638 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
101652 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
101653 assert( pBlob->pStmt || db->mallocFailed );
101654 if( pBlob->pStmt ){
101658 ** of writing code to use the b-tree layer directly is that the
101663 ** Code external to the Vdbe then "borrows" the b-tree cursor and
101668 ** which closes the b-tree cursor and (possibly) commits the
101681 Vdbe *v = (Vdbe *)pBlob->pStmt;
101682 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
101686 pTab->pSchema->schema_cookie,
101687 pTab->pSchema->iGeneration);
101689 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
101695 if( db->mallocFailed==0 ){
101702 aOp[0].p2 = pTab->tnum;
101704 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
101706 if( db->mallocFailed==0 ){
101710 ** parameter of the other to pTab->tnum. */
101712 aOp[1].p2 = pTab->tnum;
101723 aOp[1].p4.i = pTab->nCol+1;
101724 aOp[3].p2 = pTab->nCol;
101733 pBlob->iCol = iCol;
101734 pBlob->db = db;
101736 if( db->mallocFailed ){
101745 if( rc==SQLITE_OK && db->mallocFailed==0 ){
101748 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
101755 sqlite3_mutex_leave(db->mutex);
101769 sqlite3_stmt *pStmt = p->pStmt;
101770 db = p->db;
101771 sqlite3_mutex_enter(db->mutex);
101773 sqlite3_mutex_leave(db->mutex);
101797 db = p->db;
101798 sqlite3_mutex_enter(db->mutex);
101799 v = (Vdbe*)p->pStmt;
101801 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
101805 /* If there is no statement handle, then the blob-handle has
101811 ** returned, clean-up the statement handle.
101813 assert( db == v->db );
101814 sqlite3BtreeEnterCursor(p->pCsr);
101817 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
101818 /* If a pre-update hook is registered and this is a write cursor,
101821 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
101828 ** using the incremental-blob API, this works. For the sessions module
101832 iKey = sqlite3BtreeIntegerKey(p->pCsr);
101833 assert( v->apCsr[0]!=0 );
101834 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
101836 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
101841 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
101842 sqlite3BtreeLeaveCursor(p->pCsr);
101845 p->pStmt = 0;
101847 v->rc = rc;
101852 sqlite3_mutex_leave(db->mutex);
101878 return (p && p->pStmt) ? p->nByte : 0;
101897 db = p->db;
101898 sqlite3_mutex_enter(db->mutex);
101900 if( p->pStmt==0 ){
101901 /* If there is no statement handle, then the blob-handle has
101907 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
101917 assert( rc==SQLITE_OK || p->pStmt==0 );
101918 sqlite3_mutex_leave(db->mutex);
101927 ** 2011-07-09
101942 ** The VdbeSorter object implements a multi-threaded external merge sort
101946 ** Here is the (internal, non-API) interface between this module and the
101999 ** an in-memory merge sort. In this case, no temporary files are required
102006 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
102007 ** of PMAs may be created by merging existing PMAs together - for example
102008 ** merging two or more level-0 PMAs together creates a level-1 PMA.
102012 ** page-cache of the main database. Specifically, the threshold is set to
102016 ** If the sorter is running in single-threaded mode, then all PMAs generated
102018 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
102025 ** The sorter is running in multi-threaded mode if (a) the library was built
102026 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
102035 ** sorter is running in single-threaded mode, then these PMAs are merged
102040 ** Or, if running in multi-threaded mode, then a background thread is
102050 ** Rewind() is called, then a hierarchy of incremental-merges is used.
102056 ** If running in multi-threaded mode and there are more than
102069 ** characteristics of the sorter in multi-threaded mode.
102076 ** Hard-coded maximum amount of data to accumulate in memory before flushing
102089 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
102091 typedef struct SorterList SorterList; /* In-memory list of records */
102104 ** An in-memory list of objects to be sorted.
102113 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
102136 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
102150 ** aReadr[0] -> Banana
102151 ** aReadr[1] -> Feijoa
102152 ** aReadr[2] -> Elderberry
102153 ** aReadr[3] -> Currant
102154 ** aReadr[4] -> Grapefruit
102155 ** aReadr[5] -> Apple
102156 ** aReadr[6] -> Durian
102157 ** aReadr[7] -> EOF
102166 ** aReadr[5] -> Eggplant
102171 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
102194 ** single-threaded operation, there is exactly one instance of this object
102195 ** and for multi-threaded operation there are two or more instances.
102206 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
102224 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
102228 SorterFile file; /* Temp file for level-0 PMAs */
102252 SorterList list; /* List of in-memory records */
102295 ** There are two types of IncrMerger object - single (bUseThread==0) and
102296 ** multi-threaded (bUseThread==1).
102298 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
102313 ** A single-threaded IncrMerger does not open any temporary files of its
102315 ** at offset iStartOff of file pTask->file2. And instead of using a
102317 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
102335 ** size. But I/O is more efficient if it occurs in page-sized blocks where
102337 ** the PMA so that aligned, page-size blocks are written.
102340 int eFWErr; /* Non-zero if in an error state */
102354 ** by this module. If using a separate allocation for each in-memory record
102362 ** has finished passing records to the sorter, or when the in-memory buffer
102395 sqlite3_free(pReadr->aAlloc);
102396 sqlite3_free(pReadr->aBuffer);
102397 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
102398 vdbeIncrFree(pReadr->pIncr);
102419 if( p->aMap ){
102420 *ppOut = &p->aMap[p->iReadOff];
102421 p->iReadOff += nByte;
102425 assert( p->aBuffer );
102428 ** p->nBuffer bytes of data from the file into it. Or, if there are less
102429 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
102430 iBuf = p->iReadOff % p->nBuffer;
102436 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
102437 nRead = p->nBuffer;
102439 nRead = (int)(p->iEof - p->iReadOff);
102444 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
102448 nAvail = p->nBuffer - iBuf;
102451 /* The requested data is available in the in-memory buffer. In this
102454 *ppOut = &p->aBuffer[iBuf];
102455 p->iReadOff += nByte;
102457 /* The requested data is not all available in the in-memory buffer.
102458 ** In this case, allocate space at p->aAlloc[] to copy the requested
102459 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
102462 /* Extend the p->aAlloc[] allocation if required. */
102463 if( p->nAlloc<nByte ){
102465 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
102467 aNew = sqlite3Realloc(p->aAlloc, nNew);
102469 p->nAlloc = nNew;
102470 p->aAlloc = aNew;
102474 ** p->aAlloc[]. */
102475 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
102476 p->iReadOff += nAvail;
102477 nRem = nByte - nAvail;
102479 /* The following loop copies up to p->nBuffer bytes per iteration into
102480 ** the p->aAlloc[] buffer. */
102487 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
102490 assert( aNext!=p->aAlloc );
102491 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
102492 nRem -= nCopy;
102495 *ppOut = p->aAlloc;
102508 if( p->aMap ){
102509 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
102511 iBuf = p->iReadOff % p->nBuffer;
102512 if( iBuf && (p->nBuffer-iBuf)>=9 ){
102513 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
102540 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
102541 sqlite3_file *pFd = pFile->pFd;
102542 if( pFd->pMethods->iVersion>=3 ){
102543 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
102563 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
102566 if( pReadr->aMap ){
102567 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
102568 pReadr->aMap = 0;
102570 pReadr->iReadOff = iOff;
102571 pReadr->iEof = pFile->iEof;
102572 pReadr->pFd = pFile->pFd;
102574 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
102575 if( rc==SQLITE_OK && pReadr->aMap==0 ){
102576 int pgsz = pTask->pSorter->pgsz;
102577 int iBuf = pReadr->iReadOff % pgsz;
102578 if( pReadr->aBuffer==0 ){
102579 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
102580 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
102581 pReadr->nBuffer = pgsz;
102584 int nRead = pgsz - iBuf;
102585 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
102586 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
102589 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
102607 if( pReadr->iReadOff>=pReadr->iEof ){
102608 IncrMerger *pIncr = pReadr->pIncr;
102612 if( rc==SQLITE_OK && pIncr->bEof==0 ){
102614 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
102632 pReadr->nKey = (int)nRec;
102633 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
102642 ** starting at offset iStart and ending at offset iEof-1. This function
102658 assert( pFile->iEof>iStart );
102659 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
102660 assert( pReadr->aBuffer==0 );
102661 assert( pReadr->aMap==0 );
102667 pReadr->iEof = pReadr->iReadOff + nByte;
102684 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102688 UnpackedRecord *r2 = pTask->pUnpacked;
102690 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
102698 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
102702 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
102703 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
102706 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
102711 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102715 UnpackedRecord *r2 = pTask->pUnpacked;
102717 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
102730 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102745 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
102747 res = n1 - n2;
102751 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
102757 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
102758 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
102759 res = res * -1;
102772 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
102794 if( (res = v1[i] - v2[i])!=0 ){
102796 res = v1[0] & 0x80 ? -1 : +1;
102802 res = s1 - s2;
102807 res = -1;
102809 res = s1 - s2;
102814 if( *v1 & 0x80 ) res = -1;
102821 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
102826 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
102827 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
102828 res = res * -1;
102837 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
102840 ** is non-zero and the sorter is able to guarantee a stable sort, nField
102844 ** of the records being sorted. So if the sort is stable, there is never
102848 ** The sorter can guarantee a stable sort when running in single-threaded
102849 ** mode, but not in multi-threaded mode.
102861 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
102862 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
102876 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
102884 nWorker = SORTER_MAX_MERGE_COUNT-1;
102888 assert( pCsr->pKeyInfo );
102889 assert( !pCsr->isEphemeral );
102890 assert( pCsr->eCurType==CURTYPE_SORTER );
102891 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
102895 pCsr->uc.pSorter = pSorter;
102899 Btree *pBt = db->aDb[0].pBt;
102900 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
102901 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
102902 pKeyInfo->db = 0;
102904 pKeyInfo->nKeyField = nField;
102907 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
102909 pSorter->nTask = nWorker + 1;
102910 pSorter->iPrev = (u8)(nWorker - 1);
102911 pSorter->bUseThreads = (pSorter->nTask>1);
102912 pSorter->db = db;
102913 for(i=0; i<pSorter->nTask; i++){
102914 SortSubtask *pTask = &pSorter->aTask[i];
102915 pTask->pSorter = pSorter;
102921 pSorter->mnPmaSize = szPma * pgsz;
102923 mxCache = db->aDb[0].pSchema->cache_size;
102925 /* A negative cache-size value C indicates that the cache is abs(C)
102927 mxCache = mxCache * -1024;
102932 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
102937 assert( pSorter->iMemory==0 );
102938 pSorter->nMemory = pgsz;
102939 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
102940 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
102944 if( pKeyInfo->nAllField<13
102945 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
102946 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
102948 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
102963 pNext = p->u.pNext;
102973 sqlite3DbFree(db, pTask->pUnpacked);
102975 /* pTask->list.aMemory can only be non-zero if it was handed memory
102977 if( pTask->list.aMemory ){
102978 sqlite3_free(pTask->list.aMemory);
102982 assert( pTask->list.aMemory==0 );
102983 vdbeSorterRecordFree(0, pTask->list.pList);
102985 if( pTask->file.pFd ){
102986 sqlite3OsCloseFree(pTask->file.pFd);
102988 if( pTask->file2.pFd ){
102989 sqlite3OsCloseFree(pTask->file2.pFd);
102997 int iTask = (pTask - pTask->pSorter->aTask);
102998 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103012 int iTask = (pTask - pTask->pSorter->aTask);
103013 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103023 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
103036 ** Join thread pTask->thread.
103040 if( pTask->pThread ){
103042 int bDone = pTask->bDone;
103046 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
103049 assert( pTask->bDone==1 );
103050 pTask->bDone = 0;
103051 pTask->pThread = 0;
103064 assert( pTask->pThread==0 && pTask->bDone==0 );
103065 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
103070 ** level-0 PMAs.
103079 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
103082 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
103083 for(i=pSorter->nTask-1; i>=0; i--){
103084 SortSubtask *pTask = &pSorter->aTask[i];
103114 pNew->nTree = N;
103115 pNew->pTask = 0;
103116 pNew->aReadr = (PmaReader*)&pNew[1];
103117 pNew->aTree = (int*)&pNew->aReadr[N];
103128 for(i=0; i<pMerger->nTree; i++){
103129 vdbePmaReaderClear(&pMerger->aReadr[i]);
103142 if( pIncr->bUseThread ){
103143 vdbeSorterJoinThread(pIncr->pTask);
103144 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
103145 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
103148 vdbeMergeEngineFree(pIncr->pMerger);
103159 assert( pSorter->bUseThreads || pSorter->pReader==0 );
103161 if( pSorter->pReader ){
103162 vdbePmaReaderClear(pSorter->pReader);
103163 sqlite3DbFree(db, pSorter->pReader);
103164 pSorter->pReader = 0;
103167 vdbeMergeEngineFree(pSorter->pMerger);
103168 pSorter->pMerger = 0;
103169 for(i=0; i<pSorter->nTask; i++){
103170 SortSubtask *pTask = &pSorter->aTask[i];
103172 pTask->pSorter = pSorter;
103174 if( pSorter->list.aMemory==0 ){
103175 vdbeSorterRecordFree(0, pSorter->list.pList);
103177 pSorter->list.pList = 0;
103178 pSorter->list.szPMA = 0;
103179 pSorter->bUsePMA = 0;
103180 pSorter->iMemory = 0;
103181 pSorter->mxKeysize = 0;
103182 sqlite3DbFree(db, pSorter->pUnpacked);
103183 pSorter->pUnpacked = 0;
103191 assert( pCsr->eCurType==CURTYPE_SORTER );
103192 pSorter = pCsr->uc.pSorter;
103195 sqlite3_free(pSorter->list.aMemory);
103197 pCsr->uc.pSorter = 0;
103203 ** The first argument is a file-handle open on a temporary file. The file
103212 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
103226 ** Allocate space for a file-handle and open a temporary file. If successful,
103227 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
103237 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
103254 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
103258 if( pTask->pUnpacked==0 ){
103259 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
103260 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
103261 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
103262 pTask->pUnpacked->errCode = 0;
103283 res = pTask->xCompare(
103284 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
103289 pp = &p1->u.pNext;
103290 p1 = p1->u.pNext;
103297 pp = &p2->u.pNext;
103298 p2 = p2->u.pNext;
103314 if( p->typeMask==SORTER_TYPE_INTEGER ){
103316 }else if( p->typeMask==SORTER_TYPE_TEXT ){
103323 ** Sort the linked list of records headed at pTask->pList. Return
103336 p = pList->pList;
103337 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
103342 if( pList->aMemory ){
103343 if( (u8*)p==pList->aMemory ){
103346 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
103347 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
103350 pNext = p->u.pNext;
103353 p->u.pNext = 0;
103367 pList->pList = p;
103369 assert( pTask->pUnpacked->errCode==SQLITE_OK
103370 || pTask->pUnpacked->errCode==SQLITE_NOMEM
103372 return pTask->pUnpacked->errCode;
103376 ** Initialize a PMA-writer object.
103385 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
103386 if( !p->aBuffer ){
103387 p->eFWErr = SQLITE_NOMEM_BKPT;
103389 p->iBufEnd = p->iBufStart = (iStart % nBuf);
103390 p->iWriteOff = iStart - p->iBufStart;
103391 p->nBuffer = nBuf;
103392 p->pFd = pFd;
103402 while( nRem>0 && p->eFWErr==0 ){
103404 if( nCopy>(p->nBuffer - p->iBufEnd) ){
103405 nCopy = p->nBuffer - p->iBufEnd;
103408 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
103409 p->iBufEnd += nCopy;
103410 if( p->iBufEnd==p->nBuffer ){
103411 p->eFWErr = sqlite3OsWrite(p->pFd,
103412 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
103413 p->iWriteOff + p->iBufStart
103415 p->iBufStart = p->iBufEnd = 0;
103416 p->iWriteOff += p->nBuffer;
103418 assert( p->iBufEnd<p->nBuffer );
103420 nRem -= nCopy;
103425 ** Flush any buffered data to disk and clean up the PMA-writer object.
103426 ** The results of using the PMA-writer after this call are undefined.
103435 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
103436 p->eFWErr = sqlite3OsWrite(p->pFd,
103437 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
103438 p->iWriteOff + p->iBufStart
103441 *piEof = (p->iWriteOff + p->iBufEnd);
103442 sqlite3_free(p->aBuffer);
103443 rc = p->eFWErr;
103460 ** Write the current contents of in-memory linked-list pList to a level-0
103461 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
103469 ** * One or more records packed end-to-end in order of ascending keys.
103474 sqlite3 *db = pTask->pSorter->db;
103479 /* Set iSz to the expected size of file pTask->file after writing the PMA.
103481 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
103486 assert( pList->szPMA>0 );
103489 if( pTask->file.pFd==0 ){
103490 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
103491 assert( rc!=SQLITE_OK || pTask->file.pFd );
103492 assert( pTask->file.iEof==0 );
103493 assert( pTask->nPMA==0 );
103498 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
103510 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
103511 pTask->file.iEof);
103512 pTask->nPMA++;
103513 vdbePmaWriteVarint(&writer, pList->szPMA);
103514 for(p=pList->pList; p; p=pNext){
103515 pNext = p->u.pNext;
103516 vdbePmaWriteVarint(&writer, p->nVal);
103517 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
103518 if( pList->aMemory==0 ) sqlite3_free(p);
103520 pList->pList = p;
103521 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
103525 assert( rc!=SQLITE_OK || pList->pList==0 );
103526 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
103542 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
103543 SortSubtask *pTask = pMerger->pTask;
103546 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
103557 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
103558 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
103560 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
103563 if( pReadr1->pFd==0 ){
103565 }else if( pReadr2->pFd==0 ){
103566 iRes = -1;
103568 iRes = pTask->xCompare(pTask, &bCached,
103569 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
103575 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
103580 ** was actually called above, then pTask->pUnpacked now contains
103589 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
103590 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
103593 if( pReadr1->pFd ) bCached = 0;
103594 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
103595 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
103598 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
103601 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
103606 ** The main routine for background threads that write level-0 PMAs.
103611 assert( pTask->bDone==0 );
103612 rc = vdbeSorterListToPMA(pTask, &pTask->list);
103613 pTask->bDone = 1;
103624 pSorter->bUsePMA = 1;
103625 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
103630 int nWorker = (pSorter->nTask-1);
103634 pSorter->bUsePMA = 1;
103636 /* Select a sub-task to sort and flush the current list of in-memory
103637 ** records to disk. If the sorter is running in multi-threaded mode,
103638 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
103639 ** the background thread from a sub-tasks previous turn is still running,
103640 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
103641 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
103642 ** sub-tasks are preferred as they use background threads - the final
103643 ** sub-task uses the main thread. */
103645 int iTest = (pSorter->iPrev + i + 1) % nWorker;
103646 pTask = &pSorter->aTask[iTest];
103647 if( pTask->bDone ){
103650 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
103656 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
103663 assert( pTask->pThread==0 && pTask->bDone==0 );
103664 assert( pTask->list.pList==0 );
103665 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
103667 aMem = pTask->list.aMemory;
103669 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
103670 pTask->list = pSorter->list;
103671 pSorter->list.pList = 0;
103672 pSorter->list.szPMA = 0;
103674 pSorter->list.aMemory = aMem;
103675 pSorter->nMemory = sqlite3MallocSize(aMem);
103676 }else if( pSorter->list.aMemory ){
103677 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
103678 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
103704 assert( pCsr->eCurType==CURTYPE_SORTER );
103705 pSorter = pCsr->uc.pSorter;
103706 getVarint32NR((const u8*)&pVal->z[1], t);
103708 pSorter->typeMask &= SORTER_TYPE_INTEGER;
103710 pSorter->typeMask &= SORTER_TYPE_TEXT;
103712 pSorter->typeMask = 0;
103720 ** If using the single large allocation mode (pSorter->aMemory!=0), then
103727 ** * The total memory allocated for the in-memory list is greater
103728 ** than (page-size * cache-size), or
103730 ** * The total memory allocated for the in-memory list is greater
103731 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
103733 nReq = pVal->n + sizeof(SorterRecord);
103734 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
103735 if( pSorter->mxPmaSize ){
103736 if( pSorter->list.aMemory ){
103737 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
103740 (pSorter->list.szPMA > pSorter->mxPmaSize)
103741 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
103746 pSorter->list.szPMA = 0;
103747 pSorter->iMemory = 0;
103748 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
103752 pSorter->list.szPMA += nPMA;
103753 if( nPMA>pSorter->mxKeysize ){
103754 pSorter->mxKeysize = nPMA;
103757 if( pSorter->list.aMemory ){
103758 int nMin = pSorter->iMemory + nReq;
103760 if( nMin>pSorter->nMemory ){
103762 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
103763 int iListOff = -1;
103764 if( pSorter->list.pList ){
103765 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
103768 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
103770 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
103773 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
103775 pSorter->list.aMemory = aNew;
103776 pSorter->nMemory = nNew;
103779 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
103780 pSorter->iMemory += ROUND8(nReq);
103781 if( pSorter->list.pList ){
103782 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
103789 pNew->u.pNext = pSorter->list.pList;
103792 memcpy(SRVAL(pNew), pVal->z, pVal->n);
103793 pNew->nVal = pVal->n;
103794 pSorter->list.pList = pNew;
103800 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
103802 ** except that the number-of-bytes varint is omitted from the start.
103807 i64 iStart = pIncr->iStartOff;
103808 SorterFile *pOut = &pIncr->aFile[1];
103809 SortSubtask *pTask = pIncr->pTask;
103810 MergeEngine *pMerger = pIncr->pMerger;
103812 assert( pIncr->bEof==0 );
103816 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
103819 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
103820 int nKey = pReader->nKey;
103825 if( pReader->pFd==0 ) break;
103826 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
103830 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
103831 assert( pIncr->pMerger->pTask==pTask );
103832 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
103835 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
103844 ** multi-threaded IncrMerger objects.
103849 pIncr->pTask->bDone = 1;
103858 assert( pIncr->bUseThread );
103859 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
103869 ** For single-threaded objects, this is accomplished by literally reading
103870 ** keys from pIncr->pMerger and repopulating aFile[0].
103872 ** For multi-threaded objects, all that is required is to wait until the
103884 if( pIncr->bUseThread ){
103885 rc = vdbeSorterJoinThread(pIncr->pTask);
103888 SorterFile f0 = pIncr->aFile[0];
103889 pIncr->aFile[0] = pIncr->aFile[1];
103890 pIncr->aFile[1] = f0;
103894 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
103895 pIncr->bEof = 1;
103904 pIncr->aFile[0] = pIncr->aFile[1];
103905 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
103906 pIncr->bEof = 1;
103928 pIncr->pMerger = pMerger;
103929 pIncr->pTask = pTask;
103930 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
103931 pTask->file2.iEof += pIncr->mxSz;
103942 ** Set the "use-threads" flag on object pIncr.
103945 pIncr->bUseThread = 1;
103946 pIncr->pTask->file2.iEof -= pIncr->mxSz;
103953 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
103959 int iOut /* Store the result in pMerger->aTree[iOut] */
103967 assert( iOut<pMerger->nTree && iOut>0 );
103969 if( iOut>=(pMerger->nTree/2) ){
103970 i1 = (iOut - pMerger->nTree/2) * 2;
103973 i1 = pMerger->aTree[iOut*2];
103974 i2 = pMerger->aTree[iOut*2+1];
103977 p1 = &pMerger->aReadr[i1];
103978 p2 = &pMerger->aReadr[i2];
103980 if( p1->pFd==0 ){
103982 }else if( p2->pFd==0 ){
103985 SortSubtask *pTask = pMerger->pTask;
103988 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
103989 res = pTask->xCompare(
103990 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
103999 pMerger->aTree[iOut] = iRes;
104006 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
104052 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
104056 assert( pMerger->pTask==0 );
104057 pMerger->pTask = pTask;
104059 nTree = pMerger->nTree;
104064 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
104067 ** on this PmaReader before any of the multi-threaded PmaReaders takes
104068 ** better advantage of multi-processor hardware. */
104069 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
104071 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
104076 for(i=pMerger->nTree-1; i>0; i--){
104079 return pTask->pUnpacked->errCode;
104084 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
104086 ** object at (pReadr->pIncr).
104089 ** in the sub-tree headed by pReadr are also initialized. Data is then
104094 ** to be a multi-threaded PmaReader and this function is being called in a
104095 ** background thread. In this case all PmaReaders in the sub-tree are
104103 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
104104 ** this entire function is being run by thread (pTask->thread), that will
104108 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
104109 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
104117 IncrMerger *pIncr = pReadr->pIncr;
104118 SortSubtask *pTask = pIncr->pTask;
104119 sqlite3 *db = pTask->pSorter->db;
104121 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
104124 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
104126 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
104127 ** requires two temp files to itself, whereas a single-threaded object
104128 ** only requires a region of pTask->file2. */
104130 int mxSz = pIncr->mxSz;
104132 if( pIncr->bUseThread ){
104133 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
104135 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
104139 /*if( !pIncr->bUseThread )*/{
104140 if( pTask->file2.pFd==0 ){
104141 assert( pTask->file2.iEof>0 );
104142 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
104143 pTask->file2.iEof = 0;
104146 pIncr->aFile[1].pFd = pTask->file2.pFd;
104147 pIncr->iStartOff = pTask->file2.iEof;
104148 pTask->file2.iEof += mxSz;
104154 if( rc==SQLITE_OK && pIncr->bUseThread ){
104156 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
104158 ** pIncr->pTask->thread.
104187 pReader->pIncr->pTask->bDone = 1;
104193 ** If the PmaReader passed as the first argument is not an incremental-reader
104194 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
104198 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
104204 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
104208 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
104209 if( pIncr->bUseThread ){
104211 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
104222 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
104223 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
104228 ** first PMA to read from pTask->file. Assuming no error occurs, it is
104236 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
104237 MergeEngine **ppOut /* OUT: New merge-engine */
104249 PmaReader *pReadr = &pNew->aReadr[i];
104250 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
104251 iOff = pReadr->iEof;
104268 ** nPMA<=16 -> TreeDepth() == 0
104269 ** nPMA<=256 -> TreeDepth() == 1
104270 ** nPMA<=65536 -> TreeDepth() == 2
104283 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
104311 PmaReader *pReadr = &p->aReadr[iIter];
104313 if( pReadr->pIncr==0 ){
104318 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
104322 p = pReadr->pIncr->pMerger;
104328 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
104337 ** that has already written two or more level-0 PMAs to one or more temp
104355 /* If the sorter uses more than one task, then create the top-level
104357 ** one PmaReader per sub-task. */
104358 assert( pSorter->bUseThreads || pSorter->nTask==1 );
104359 if( pSorter->nTask>1 ){
104360 pMain = vdbeMergeEngineNew(pSorter->nTask);
104365 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
104366 SortSubtask *pTask = &pSorter->aTask[iTask];
104367 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
104368 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
104370 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
104373 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
104374 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
104380 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
104381 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
104382 int nReader; /* Number of level-0 PMAs to merge */
104384 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
104395 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
104420 ** (for multi-threaded sorters) so that it can be used to iterate through
104427 SortSubtask *pTask0 = &pSorter->aTask[0];
104430 sqlite3 *db = pTask0->pSorter->db;
104433 for(i=0; i<pSorter->nTask; i++){
104434 pSorter->aTask[i].xCompare = xCompare;
104441 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
104442 if( pSorter->bUseThreads ){
104445 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
104449 pSorter->pReader = pReadr;
104453 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
104455 vdbeIncrMergerSetThreads(pReadr->pIncr);
104456 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
104458 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
104460 assert( pIncr->pTask!=pLast );
104463 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
104468 ** b) If it is using task (nTask-1), it is configured to run
104469 ** in single-threaded mode. This is important, as the
104473 PmaReader *p = &pMain->aReadr[iTask];
104474 assert( p->pIncr==0 || (
104475 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
104476 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
104490 pSorter->pMerger = pMain;
104511 assert( pCsr->eCurType==CURTYPE_SORTER );
104512 pSorter = pCsr->uc.pSorter;
104517 ** from the in-memory list. */
104518 if( pSorter->bUsePMA==0 ){
104519 if( pSorter->list.pList ){
104521 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
104528 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
104532 assert( pSorter->list.pList );
104542 assert( pSorter->pReader==0 );
104563 assert( pCsr->eCurType==CURTYPE_SORTER );
104564 pSorter = pCsr->uc.pSorter;
104565 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
104566 if( pSorter->bUsePMA ){
104567 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
104568 assert( pSorter->bUseThreads==0 || pSorter->pReader );
104569 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
104571 if( pSorter->bUseThreads ){
104572 rc = vdbePmaReaderNext(pSorter->pReader);
104573 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
104576 /*if( !pSorter->bUseThreads )*/ {
104578 assert( pSorter->pMerger!=0 );
104579 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
104580 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
104584 SorterRecord *pFree = pSorter->list.pList;
104585 pSorter->list.pList = pFree->u.pNext;
104586 pFree->u.pNext = 0;
104587 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
104588 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
104602 if( pSorter->bUsePMA ){
104605 if( pSorter->bUseThreads ){
104606 pReader = pSorter->pReader;
104609 /*if( !pSorter->bUseThreads )*/{
104610 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
104612 *pnKey = pReader->nKey;
104613 pKey = pReader->aKey;
104615 *pnKey = pSorter->list.pList->nVal;
104616 pKey = SRVAL(pSorter->list.pList);
104628 assert( pCsr->eCurType==CURTYPE_SORTER );
104629 pSorter = pCsr->uc.pSorter;
104634 pOut->n = nKey;
104636 memcpy(pOut->z, pKey, nKey);
104669 assert( pCsr->eCurType==CURTYPE_SORTER );
104670 pSorter = pCsr->uc.pSorter;
104671 r2 = pSorter->pUnpacked;
104672 pKeyInfo = pCsr->pKeyInfo;
104674 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
104676 r2->nField = nKeyCol;
104678 assert( r2->nField==nKeyCol );
104683 if( r2->aMem[i].flags & MEM_Null ){
104684 *pRes = -1;
104689 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
104696 ** 2020-03-23
104707 ** This file implements virtual-tables for examining the bytecode content
104714 /* An instance of the bytecode() table-valued function.
104718 sqlite3_vtab base; /* Base class - must be first */
104727 sqlite3_vtab_cursor base; /* Base class - must be first */
104742 ** Create a new bytecode() table-valued function.
104791 pNew->db = db;
104792 pNew->bTablesUsed = isTabUsed*2;
104815 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
104816 *ppCursor = &pCur->base;
104824 sqlite3_free(pCur->zP4);
104825 pCur->zP4 = 0;
104826 sqlite3VdbeMemRelease(&pCur->sub);
104827 sqlite3VdbeMemSetNull(&pCur->sub);
104828 if( pCur->needFinalize ){
104829 sqlite3_finalize(pCur->pStmt);
104831 pCur->pStmt = 0;
104832 pCur->needFinalize = 0;
104833 pCur->zType = 0;
104834 pCur->zSchema = 0;
104835 pCur->zName = 0;
104854 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
104856 if( pCur->zP4 ){
104857 sqlite3_free(pCur->zP4);
104858 pCur->zP4 = 0;
104860 if( pCur->zName ){
104861 pCur->zName = 0;
104862 pCur->zType = 0;
104863 pCur->zSchema = 0;
104866 (Vdbe*)pCur->pStmt,
104867 pCur->showSubprograms ? &pCur->sub : 0,
104868 pTab->bTablesUsed,
104869 &pCur->iRowid,
104870 &pCur->iAddr,
104871 &pCur->aOp);
104873 sqlite3VdbeMemSetNull(&pCur->sub);
104874 pCur->aOp = 0;
104885 return pCur->aOp==0;
104898 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
104899 Op *pOp = pCur->aOp + pCur->iAddr;
104900 if( pVTab->bTablesUsed ){
104904 if( i<=2 && pCur->zType==0 ){
104907 int iDb = pOp->p3;
104908 Pgno iRoot = (Pgno)pOp->p2;
104909 sqlite3 *db = pVTab->db;
104910 pSchema = db->aDb[iDb].pSchema;
104911 pCur->zSchema = db->aDb[iDb].zDbSName;
104912 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
104914 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
104915 pCur->zName = pTab->zName;
104916 pCur->zType = "table";
104920 if( pCur->zName==0 ){
104921 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
104923 if( pIdx->tnum==iRoot ){
104924 pCur->zName = pIdx->zName;
104925 pCur->zType = "index";
104935 sqlite3_result_int(ctx, pCur->iAddr);
104938 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
104939 -1, SQLITE_STATIC);
104942 sqlite3_result_int(ctx, pOp->p1);
104945 sqlite3_result_int(ctx, pOp->p2);
104948 sqlite3_result_int(ctx, pOp->p3);
104952 if( pCur->zP4==0 ){
104953 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
104956 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
104959 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
104960 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
104965 sqlite3_result_int(ctx, pOp->p5);
104968 Op *aOp = pCur->aOp;
104970 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
104971 if( pCur->iRowid==pCur->iAddr+1 ){
104974 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
104983 sqlite3_result_int(ctx, pOp->nExec);
104986 sqlite3_result_int(ctx, pOp->nCycle);
104996 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
104999 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
105002 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
105005 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
105017 *pRowid = pCur->iRowid;
105033 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
105038 pCur->iRowid = 0;
105039 pCur->iAddr = 0;
105040 pCur->showSubprograms = idxNum==0;
105047 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
105048 pCur->needFinalize = 1;
105051 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
105053 if( pCur->pStmt==0 ){
105054 pVTab->base.zErrMsg = sqlite3_mprintf(
105056 pVTab->bTablesUsed ? "tables_used" : "bytecode"
105078 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
105079 pIdxInfo->estimatedCost = (double)100;
105080 pIdxInfo->estimatedRows = 100;
105081 pIdxInfo->idxNum = 0;
105082 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
105083 if( p->usable==0 ) continue;
105084 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
105086 pIdxInfo->aConstraintUsage[i].omit = 1;
105087 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
105089 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
105090 pIdxInfo->aConstraintUsage[i].omit = 1;
105091 pIdxInfo->idxNum = 1;
105156 ** This file contains code use to implement an in-memory rollback journal.
105157 ** The in-memory rollback journal is used to journal transactions for
105160 ** Update: The in-memory journal is also used to temporarily cache
105161 ** smaller journals that are not critical for power-loss recovery.
105179 ** The zChunk array is always at least 8 bytes in size - usually much more.
105196 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
105208 ** This structure is a subclass of sqlite3_file. Each open memory-journal
105213 int nChunkSize; /* In-memory chunk-size */
105216 FileChunk *pFirst; /* Head of in-memory chunk-list */
105226 ** Read data from the in-memory journal file. This is the implementation
105241 if( (iAmt+iOfst)>p->endpoint.iOffset ){
105244 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
105245 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
105247 for(pChunk=p->pFirst;
105248 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
105249 pChunk=pChunk->pNext
105251 iOff += p->nChunkSize;
105254 pChunk = p->readpoint.pChunk;
105258 iChunkOffset = (int)(iOfst%p->nChunkSize);
105260 int iSpace = p->nChunkSize - iChunkOffset;
105261 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
105262 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
105264 nRead -= iSpace;
105266 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
105267 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
105268 p->readpoint.pChunk = pChunk;
105280 pNext = pIter->pNext;
105299 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
105301 nChunk = copy.endpoint.iOffset - iOff;
105303 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
105308 /* No error has occurred. Free the in-memory buffers. */
105314 ** the original before returning. This way, SQLite uses the in-memory
105315 ** journal data to roll back changes made to the internal page-cache
105342 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
105352 /* An in-memory journal file should only ever be appended to. Random
105354 ** the in-memory journal is being used by a connection using the
105355 ** atomic-write optimization. In this case the first 28 bytes of the
105357 assert( iOfst<=p->endpoint.iOffset );
105358 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
105361 if( iOfst==0 && p->pFirst ){
105362 assert( p->nChunkSize>iAmt );
105363 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
105366 FileChunk *pChunk = p->endpoint.pChunk;
105367 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
105368 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
105373 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
105377 pNew->pNext = 0;
105379 assert( p->pFirst );
105380 pChunk->pNext = pNew;
105382 assert( !p->pFirst );
105383 p->pFirst = pNew;
105385 pChunk = p->endpoint.pChunk = pNew;
105389 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
105391 nWrite -= iSpace;
105392 p->endpoint.iOffset += iSpace;
105401 ** Truncate the in-memory file.
105405 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
105406 if( size<p->endpoint.iOffset ){
105409 memjrnlFreeChunks(p->pFirst);
105410 p->pFirst = 0;
105412 i64 iOff = p->nChunkSize;
105413 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
105414 iOff += p->nChunkSize;
105417 memjrnlFreeChunks(pIter->pNext);
105418 pIter->pNext = 0;
105422 p->endpoint.pChunk = pIter;
105423 p->endpoint.iOffset = size;
105424 p->readpoint.pChunk = 0;
105425 p->readpoint.iOffset = 0;
105435 memjrnlFreeChunks(p->pFirst);
105443 ** syncing an in-memory journal is a no-op.
105455 *pSize = (sqlite_int64) p->endpoint.iOffset;
105490 ** all content is always stored in main-memory. Finally, if nSpill is a
105491 ** positive value, then the journal file is initially created in-memory
105507 /* Zero the file-handle object. If nSpill was passed zero, initialize
105510 ** made on the journal file-handle. */
105517 p->nChunkSize = nSpill;
105519 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
105520 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
105523 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
105524 p->nSpill = nSpill;
105525 p->flags = flags;
105526 p->zJournal = zName;
105527 p->pVfs = pVfs;
105532 ** Open an in-memory journal file.
105535 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
105542 ** in-memory-only journal file (i.e. is one that was opened with a +ve
105549 if( pJfd->pMethods==&MemJournalMethods && (
105551 p->nSpill>0
105556 NEVER(p->nSpill>0)
105559 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
105569 ** The file-handle passed as the only argument is open on a journal file.
105574 return p->pMethods==&MemJournalMethods;
105579 ** pVfs to create the underlying on-disk files.
105582 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
105613 for(pWin=pList; pWin; pWin=pWin->pNextWin){
105615 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
105617 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
105619 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
105621 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
105623 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
105645 ** return from the top-level walk call.
105655 rc = pWalker->xExprCallback(pWalker, pExpr);
105658 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
105659 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
105662 if( pExpr->pRight ){
105664 pExpr = pExpr->pRight;
105668 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
105670 if( pExpr->x.pList ){
105671 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
105675 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
105696 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
105697 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
105704 ** This is a no-op callback for Walker->xSelectCallback2. If this
105705 ** callback is set, then the Select->pWinDefn list is traversed.
105710 /* No-op */
105720 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
105721 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
105722 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
105723 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
105724 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
105725 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
105727 if( p->pWinDefn ){
105729 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
105730 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
105732 || pWalker->xSelectCallback2==sqlite3SelectPopWith
105737 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
105757 pSrc = p->pSrc;
105759 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
105760 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
105763 if( pItem->fg.isTabFunc
105764 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
105776 ** on the compound select chain, p->pPrior.
105781 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
105788 ** is a no-op returning WRC_Continue.
105793 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
105795 rc = pWalker->xSelectCallback(pWalker, p);
105802 if( pWalker->xSelectCallback2 ){
105803 pWalker->xSelectCallback2(pWalker, p);
105805 p = p->pPrior;
105815 pWalker->walkerDepth++;
105820 pWalker->walkerDepth--;
105825 ** No-op routine for the parse-tree walker.
105839 ** No-op routine for the parse-tree walker for SELECT statements.
105879 ** is a helper function - a callback for the tree walker.
105884 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
105898 ** Turn the pExpr expression into an alias for the iCol-th column of the
105919 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
105923 Expr *pOrig; /* The iCol-th column of the result set */
105927 assert( iCol>=0 && iCol<pEList->nExpr );
105928 pOrig = pEList->a[iCol].pExpr;
105931 if( pExpr->pAggInfo ) return;
105932 db = pParse->db;
105934 if( db->mallocFailed ){
105940 if( pExpr->op==TK_COLLATE ){
105942 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
105948 if( ALWAYS(pExpr->y.pWin!=0) ){
105949 pExpr->y.pWin->pOwner = pExpr;
105959 ** and mark the expression-list item by setting ExprList.a[].fg.eEName
105962 ** Check to see if the zSpan/eEName of the expression-list item passed to this
105969 ** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
105982 int eEName = pItem->fg.eEName;
105987 zSpan = pItem->zEName;
106007 ** Return TRUE if the double-quoted string mis-feature should be supported.
106010 if( db->init.busy ) return 1; /* Always support for legacy schemas */
106011 if( pTopNC->ncFlags & NC_IsDDL ){
106013 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
106016 return (db->flags & SQLITE_DqsDDL)!=0;
106019 return (db->flags & SQLITE_DqsDML)!=0;
106024 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
106031 n = pExpr->iColumn;
106033 pExTab = pExpr->y.pTab;
106035 assert( n < pExTab->nCol );
106036 if( (pExTab->tabFlags & TF_HasGenerated)!=0
106037 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
106039 testcase( pExTab->nCol==BMS-1 );
106040 testcase( pExTab->nCol==BMS );
106041 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
106043 testcase( n==BMS-1 );
106045 if( n>=BMS ) n = BMS-1;
106062 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
106064 pNew->iTable = pMatch->iCursor;
106065 pNew->iColumn = iColumn;
106066 pNew->y.pTab = pMatch->pTab;
106067 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
106074 ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
106079 Schema *pSchema /* non-NULL if a database qualifier is present */
106083 assert( pTab->tnum==1 );
106085 zLegacy = pTab->zName;
106105 ** pExpr->iDb Set the index in db->aDb[] of the database X
106107 ** pExpr->iTable Set to the cursor number for the table obtained
106109 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
106111 ** pExpr->iColumn Set to the column number within the table.
106112 ** pExpr->op Set to TK_COLUMN.
106113 ** pExpr->pLeft Any expression this points to is deleted
106114 ** pExpr->pRight Any expression this points to is deleted.
106138 sqlite3 *db = pParse->db; /* The database connection */
106143 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
106153 /* Initialize the node to no-match */
106154 pExpr->iTable = -1;
106162 testcase( pNC->ncFlags & NC_PartIdx );
106163 testcase( pNC->ncFlags & NC_IsCheck );
106164 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
106171 for(i=0; i<db->nDb; i++){
106172 assert( db->aDb[i].zDbSName );
106173 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
106174 pSchema = db->aDb[i].pSchema;
106178 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
106181 pSchema = db->aDb[0].pSchema;
106182 zDb = db->aDb[0].zDbSName;
106187 /* Start at the inner-most context and move outward until a match is found */
106191 SrcList *pSrcList = pNC->pSrcList;
106194 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
106196 pTab = pItem->pTab;
106197 assert( pTab!=0 && pTab->zName!=0 );
106198 assert( pTab->nCol>0 || pParse->nErr );
106199 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
106200 if( pItem->fg.isNestedFrom ){
106205 ** This pItem -------------^
106208 assert( pItem->pSelect!=0 );
106209 pEList = pItem->pSelect->pEList;
106211 assert( pEList->nExpr==pTab->nCol );
106212 for(j=0; j<pEList->nExpr; j++){
106214 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
106219 if( pItem->fg.isUsing==0
106220 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
106228 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
106229 /* An INNER or LEFT JOIN. Use the left-most table */
106232 if( (pItem->fg.jointype & JT_LEFT)==0 ){
106233 /* A RIGHT JOIN. Use the right-most table */
106239 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
106251 pExpr->iColumn = j;
106252 pEList->a[j].fg.bUsed = 1;
106254 /* rowid cannot be part of a USING clause - assert() this. */
106255 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 );
106256 if( pEList->a[j].fg.bUsingTerm ) break;
106263 if( pTab->pSchema!=pSchema ) continue;
106266 if( pItem->zAlias!=0 ){
106267 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
106270 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
106271 if( pTab->tnum!=1 ) continue;
106275 if( IN_RENAME_OBJECT && pItem->zAlias ){
106276 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
106280 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
106281 if( pCol->hName==hCol
106282 && sqlite3StrICmp(pCol->zCnName, zCol)==0
106285 if( pItem->fg.isUsing==0
106286 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
106294 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
106295 /* An INNER or LEFT JOIN. Use the left-most table */
106298 if( (pItem->fg.jointype & JT_LEFT)==0 ){
106299 /* A RIGHT JOIN. Use the right-most table */
106305 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
106310 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
106311 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
106312 if( pItem->fg.isNestedFrom ){
106327 ** non-VIEW candidate plus multiple VIEW candidates. In other
106328 ** words non-VIEW candidate terms take precedence over VIEWs.
106333 && ALWAYS(pMatch->pTab!=0)
106334 && (pMatch->pTab->tabFlags & TF_Ephemeral)!=0
106335 && (pTab->tabFlags & TF_Ephemeral)==0)
106343 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
106345 ** always be a non-VIEW
106353 pExpr->iTable = pMatch->iCursor;
106355 pExpr->y.pTab = pMatch->pTab;
106356 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
106359 pSchema = pExpr->y.pTab->pSchema;
106372 if( pParse->pTriggerTab!=0 ){
106373 int op = pParse->eTriggerOp;
106375 if( pParse->bReturning ){
106376 if( (pNC->ncFlags & NC_UBaseReg)!=0
106378 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
106380 pExpr->iTable = op!=TK_DELETE;
106381 pTab = pParse->pTriggerTab;
106384 pExpr->iTable = 1;
106385 pTab = pParse->pTriggerTab;
106387 pExpr->iTable = 0;
106388 pTab = pParse->pTriggerTab;
106393 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
106394 Upsert *pUpsert = pNC->uNC.pUpsert;
106396 pTab = pUpsert->pUpsertSrc->a[0].pTab;
106397 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
106405 pSchema = pTab->pSchema;
106407 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
106408 if( pCol->hName==hCol
106409 && sqlite3StrICmp(pCol->zCnName, zCol)==0
106411 if( iCol==pTab->iPKey ){
106412 iCol = -1;
106417 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
106418 /* IMP: R-51414-32910 */
106419 iCol = -1;
106421 if( iCol<pTab->nCol ){
106425 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
106426 testcase( iCol==(-1) );
106429 pExpr->iColumn = iCol;
106430 pExpr->y.pTab = pTab;
106433 pExpr->iTable = pNC->uNC.pUpsert->regData +
106441 pExpr->y.pTab = pTab;
106442 if( pParse->bReturning ){
106444 pExpr->op2 = TK_COLUMN;
106445 pExpr->iColumn = iCol;
106446 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
106449 pExpr->iColumn = (i16)iCol;
106453 pExpr->affExpr = SQLITE_AFF_INTEGER;
106454 }else if( pExpr->iTable==0 ){
106457 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
106461 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
106477 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
106479 && ALWAYS(VisibleRowid(pMatch->pTab) || pMatch->fg.isNestedFrom)
106482 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
106483 pExpr->affExpr = SQLITE_AFF_INTEGER;
106488 ** might refer to an result-set alias. This happens, for example, when
106498 ** The ability to use an output result-set column in the WHERE, GROUP BY,
106505 && (pNC->ncFlags & NC_UEList)!=0
106508 pEList = pNC->uNC.pEList;
106510 for(j=0; j<pEList->nExpr; j++){
106511 char *zAs = pEList->a[j].zEName;
106512 if( pEList->a[j].fg.eEName==ENAME_NAME
106516 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
106517 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
106518 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
106519 pOrig = pEList->a[j].pExpr;
106520 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
106525 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
106550 pNC = pNC->pNext;
106557 ** supplied) and the value of Z is enclosed in double-quotes, then
106562 ** Because no reference was made to outer contexts, the pNC->nRef
106566 assert( pExpr->op==TK_ID );
106570 /* If a double-quoted identifier does not match any known column name,
106574 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
106585 "double-quoted string literal: \"%w\"", zCol);
106587 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
106589 pExpr->op = TK_STRING;
106590 memset(&pExpr->y, 0, sizeof(pExpr->y));
106610 if( pFJMatch->nExpr==cnt-1 ){
106614 sqlite3ExprDelete(db, pExpr->pLeft);
106615 pExpr->pLeft = 0;
106616 sqlite3ExprDelete(db, pExpr->pRight);
106617 pExpr->pRight = 0;
106619 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
106620 pExpr->op = TK_FUNCTION;
106621 pExpr->u.zToken = "coalesce";
106622 pExpr->x.pList = pFJMatch;
106638 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
106639 pParse->checkSchema = 1;
106640 pTopNC->nNcErr++;
106647 sqlite3ExprDelete(db, pExpr->pLeft);
106648 pExpr->pLeft = 0;
106649 sqlite3ExprDelete(db, pExpr->pRight);
106650 pExpr->pRight = 0;
106668 if( pExpr->iColumn>=0 && cnt==1 && pMatch!=0 ){
106669 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
106672 pExpr->op = eNewExprOp;
106677 if( pParse->db->xAuth
106678 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
106680 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
106687 pTopNC->nRef++;
106689 pTopNC = pTopNC->pNext;
106704 SrcItem *pItem = &pSrc->a[iSrc];
106707 pTab = p->y.pTab = pItem->pTab;
106708 p->iTable = pItem->iCursor;
106709 if( p->y.pTab->iPKey==iCol ){
106710 p->iColumn = -1;
106712 p->iColumn = (ynVar)iCol;
106713 if( (pTab->tabFlags & TF_HasGenerated)!=0
106714 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
106716 testcase( pTab->nCol==63 );
106717 testcase( pTab->nCol==64 );
106718 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
106721 testcase( iCol==BMS-1 );
106722 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
106731 ** pNC->ncFlags values determined by validMask.
106753 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
106755 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
106758 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
106761 if( pExpr ) pExpr->op = TK_NULL;
106762 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
106766 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
106770 ** Return 1024 times this value. Or return -1 if p is not a floating point
106774 double r = -1.0;
106775 if( p->op!=TK_FLOAT ) return -1;
106777 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
106779 if( r>1.0 ) return -1;
106798 pNC = pWalker->u.pNC;
106800 pParse = pNC->pParse;
106801 assert( pParse==pWalker->pParse );
106804 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
106805 SrcList *pSrcList = pNC->pSrcList;
106807 for(i=0; i<pNC->pSrcList->nSrc; i++){
106808 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
106812 switch( pExpr->op ){
106820 SrcList *pSrcList = pNC->pSrcList;
106822 assert( pSrcList && pSrcList->nSrc>=1 );
106823 pItem = pSrcList->a;
106824 pExpr->op = TK_COLUMN;
106826 pExpr->y.pTab = pItem->pTab;
106827 pExpr->iTable = pItem->iCursor;
106828 pExpr->iColumn--;
106829 pExpr->affExpr = SQLITE_AFF_INTEGER;
106835 ** "expr IS NOT NULL" --> "TRUE"
106836 ** "expr IS NULL" --> "FALSE"
106841 ** If this optimization occurs, also restore the NameContext ref-counts
106852 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
106853 anRef[i] = p->nRef;
106855 sqlite3WalkExpr(pWalker, pExpr->pLeft);
106856 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
106859 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
106860 pExpr->flags |= EP_IntValue;
106861 pExpr->op = TK_INTEGER;
106863 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
106864 p->nRef = anRef[i];
106866 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
106867 pExpr->pLeft = 0;
106877 ** be one call to lookupName(). Then the compiler will in-line
106887 if( pExpr->op==TK_ID ){
106891 zColumn = pExpr->u.zToken;
106893 Expr *pLeft = pExpr->pLeft;
106894 testcase( pNC->ncFlags & NC_IdxExpr );
106895 testcase( pNC->ncFlags & NC_GenCol );
106898 pRight = pExpr->pRight;
106899 if( pRight->op==TK_ID ){
106902 assert( pRight->op==TK_DOT );
106904 zDb = pLeft->u.zToken;
106905 pLeft = pRight->pLeft;
106906 pRight = pRight->pRight;
106909 zTable = pLeft->u.zToken;
106910 zColumn = pRight->u.zToken;
106914 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
106923 ExprList *pList = pExpr->x.pList; /* The argument list */
106924 int n = pList ? pList->nExpr : 0; /* Number of arguments */
106930 u8 enc = ENC(pParse->db); /* The database encoding */
106931 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
106933 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
106936 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER );
106937 zId = pExpr->u.zToken;
106938 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
106940 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
106947 is_agg = pDef->xFinalize!=0;
106948 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
106951 pExpr->iTable = exprProbability(pList->a[1].pExpr);
106952 if( pExpr->iTable<0 ){
106956 pNC->nNcErr++;
106959 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
106961 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
106962 ** short-hand for likelihood(X,0.0625).
106963 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
106965 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
106968 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
106973 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
106978 pNC->nNcErr++;
106980 pExpr->op = TK_NULL;
106985 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
106992 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
106993 /* Clearly non-deterministic functions like random(), but also
106999 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
107003 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
107004 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
107006 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
107007 && pParse->nested==0
107008 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
107010 /* Internal-use-only functions are disallowed unless the
107012 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
107017 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
107026 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
107027 || (pDef->xValue==0 && pDef->xInverse==0)
107028 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
107030 if( pDef && pDef->xValue==0 && pWin ){
107034 pNC->nNcErr++;
107036 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
107037 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
107038 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
107041 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
107047 pNC->nNcErr++;
107051 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
107053 pNC->nNcErr++;
107057 else if( no_such_func && pParse->db->init.busy==0
107059 && pParse->explain==0
107063 pNC->nNcErr++;
107067 pNC->nNcErr++;
107072 "FILTER may not be used with non-aggregate %#T()",
107075 pNC->nNcErr++;
107078 else if( is_agg==0 && pExpr->pLeft ){
107080 pNC->nNcErr++;
107087 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
107089 pNC->ncFlags &= ~NC_AllowAgg;
107100 if( pExpr->pLeft ){
107101 assert( pExpr->pLeft->op==TK_ORDER );
107102 assert( ExprUseXList(pExpr->pLeft) );
107103 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
107107 Select *pSel = pNC->pWinSelect;
107108 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
107110 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
107111 if( pParse->db->mallocFailed ) break;
107113 sqlite3WalkExprList(pWalker, pWin->pPartition);
107114 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
107115 sqlite3WalkExpr(pWalker, pWin->pFilter);
107117 pNC->ncFlags |= NC_HasWin;
107122 pExpr->op = TK_AGG_FUNCTION;
107123 pExpr->op2 = 0;
107126 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
107131 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
107133 pExpr->op2++;
107134 pNC2 = pNC2->pNext;
107140 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
107141 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
107142 pNC2->ncFlags |= NC_HasAgg
107143 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
107147 pNC->ncFlags |= savedAllowFlags;
107149 /* FIX ME: Compute pExpr->affinity based on the expected return
107156 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
107159 testcase( pExpr->op==TK_IN );
107161 int nRef = pNC->nRef;
107162 testcase( pNC->ncFlags & NC_IsCheck );
107163 testcase( pNC->ncFlags & NC_PartIdx );
107164 testcase( pNC->ncFlags & NC_IdxExpr );
107165 testcase( pNC->ncFlags & NC_GenCol );
107166 if( pNC->ncFlags & NC_SelfRef ){
107169 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
107171 assert( pNC->nRef>=nRef );
107172 if( nRef!=pNC->nRef ){
107175 pNC->ncFlags |= NC_Subquery;
107180 testcase( pNC->ncFlags & NC_IsCheck );
107181 testcase( pNC->ncFlags & NC_PartIdx );
107182 testcase( pNC->ncFlags & NC_IdxExpr );
107183 testcase( pNC->ncFlags & NC_GenCol );
107190 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
107194 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
107197 if( pRight->op==TK_TRUEFALSE ){
107198 pExpr->op2 = pExpr->op;
107199 pExpr->op = TK_TRUTH;
107213 if( pParse->db->mallocFailed ) break;
107214 assert( pExpr->pLeft!=0 );
107215 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
107216 if( pExpr->op==TK_BETWEEN ){
107218 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
107220 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
107223 assert( pExpr->pRight!=0 );
107224 nRight = sqlite3ExprVectorSize(pExpr->pRight);
107227 testcase( pExpr->op==TK_EQ );
107228 testcase( pExpr->op==TK_NE );
107229 testcase( pExpr->op==TK_LT );
107230 testcase( pExpr->op==TK_LE );
107231 testcase( pExpr->op==TK_GT );
107232 testcase( pExpr->op==TK_GE );
107233 testcase( pExpr->op==TK_IS );
107234 testcase( pExpr->op==TK_ISNOT );
107235 testcase( pExpr->op==TK_BETWEEN );
107237 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107242 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
107243 return pParse->nErr ? WRC_Abort : WRC_Continue;
107250 ** to the AS-name of one of the terms of the expression list. If it is,
107267 if( pE->op==TK_ID ){
107270 zCol = pE->u.zToken;
107271 for(i=0; i<pEList->nExpr; i++){
107272 if( pEList->a[i].fg.eEName==ENAME_NAME
107273 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
107291 ** Attempt to match pE against result set columns in the left-most
107293 ** as an indication to the caller that it should sort by the i-th column.
107294 ** The left-most column is 1. In other words, the value returned is the
107298 ** If there is no match, return 0. Return -1 if an error occurs.
107310 u8 savedSuppErr; /* Saved value of db->suppressErr */
107313 pEList = pSelect->pEList;
107319 nc.pSrcList = pSelect->pSrc;
107323 db = pParse->db;
107324 savedSuppErr = db->suppressErr;
107325 db->suppressErr = 1;
107327 db->suppressErr = savedSuppErr;
107331 ** in the result set. Return an 1-based index of the matching
107332 ** result-set entry.
107334 for(i=0; i<pEList->nExpr; i++){
107335 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
107345 ** Generate an ORDER BY or GROUP BY term out-of-range error.
107350 int i, /* The index (1-based) of the term out of range */
107355 "%r %s BY term out of range - should be "
107357 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
107369 ** beginning with the left-most SELECT and working toward the right.
107385 pOrderBy = pSelect->pOrderBy;
107387 db = pParse->db;
107388 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
107392 for(i=0; i<pOrderBy->nExpr; i++){
107393 pOrderBy->a[i].fg.done = 0;
107395 pSelect->pNext = 0;
107396 while( pSelect->pPrior ){
107397 pSelect->pPrior->pNext = pSelect;
107398 pSelect = pSelect->pPrior;
107403 pEList = pSelect->pEList;
107405 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
107406 int iCol = -1;
107408 if( pItem->fg.done ) continue;
107409 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
107412 if( iCol<=0 || iCol>pEList->nExpr ){
107413 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
107431 if( !db->mallocFailed ){
107447 pNew->flags |= EP_IntValue;
107448 pNew->u.iValue = iCol;
107449 if( pItem->pExpr==pE ){
107450 pItem->pExpr = pNew;
107452 Expr *pParent = pItem->pExpr;
107453 assert( pParent->op==TK_COLLATE );
107454 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
107455 assert( pParent->pLeft==pE );
107456 pParent->pLeft = pNew;
107459 pItem->u.x.iOrderByCol = (u16)iCol;
107461 pItem->fg.done = 1;
107466 pSelect = pSelect->pNext;
107468 for(i=0; i<pOrderBy->nExpr; i++){
107469 if( pOrderBy->a[i].fg.done==0 ){
107486 ** return non-zero. Return zero if no errors are seen.
107495 sqlite3 *db = pParse->db;
107499 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
107500 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
107504 pEList = pSelect->pEList;
107506 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
107507 if( pItem->u.x.iOrderByCol ){
107508 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
107509 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
107512 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
107525 Window *pWin = pExpr->y.pWin;
107536 if( pSelect->pWin ){
107554 ** If the order-by term is an integer I between 1 and N (where N is the
107556 ** in the resolution is a copy of the I-th result-set expression. If
107557 ** the order-by term is an identifier that corresponds to the AS-name of
107558 ** a result-set expression, then the term resolves to a copy of the
107559 ** result-set expression. Otherwise, the expression is resolved in
107560 ** the usual way - using sqlite3ResolveExprNames().
107579 nResult = pSelect->pEList->nExpr;
107580 pParse = pNC->pParse;
107581 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
107582 Expr *pE = pItem->pExpr;
107586 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
107588 /* If an AS-name match is found, mark this ORDER BY column as being
107589 ** a copy of the iCol-th result-set column. The subsequent call to
107591 ** copy of the iCol-th result-set expression. */
107592 pItem->u.x.iOrderByCol = (u16)iCol;
107599 ** order-by term to a copy of the result-set expression */
107604 pItem->u.x.iOrderByCol = (u16)iCol;
107609 pItem->u.x.iOrderByCol = 0;
107613 for(j=0; j<pSelect->pEList->nExpr; j++){
107614 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
107619 pItem->u.x.iOrderByCol = j+1;
107637 Select *pLeftmost; /* Left-most of SELECT of a compound */
107642 if( p->selFlags & SF_Resolved ){
107645 pOuterNC = pWalker->u.pNC;
107646 pParse = pWalker->pParse;
107647 db = pParse->db;
107657 if( (p->selFlags & SF_Expanded)==0 ){
107659 return pParse->nErr ? WRC_Abort : WRC_Prune;
107662 isCompound = p->pPrior!=0;
107666 assert( (p->selFlags & SF_Expanded)!=0 );
107667 assert( (p->selFlags & SF_Resolved)==0 );
107668 p->selFlags |= SF_Resolved;
107676 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
107682 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
107683 ** as if it were part of the sub-query, not the parent. This block
107684 ** moves the pOrderBy down to the sub-query. It will be moved back
107686 if( p->selFlags & SF_Converted ){
107687 Select *pSub = p->pSrc->a[0].pSelect;
107688 assert( p->pSrc->nSrc==1 && p->pOrderBy );
107689 assert( pSub->pPrior && pSub->pOrderBy==0 );
107690 pSub->pOrderBy = p->pOrderBy;
107691 p->pOrderBy = 0;
107696 for(i=0; i<p->pSrc->nSrc; i++){
107697 SrcItem *pItem = &p->pSrc->a[i];
107698 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
107699 int nRef = pOuterNC ? pOuterNC->nRef : 0;
107700 const char *zSavedContext = pParse->zAuthContext;
107702 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
107703 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
107704 pParse->zAuthContext = zSavedContext;
107705 if( pParse->nErr ) return WRC_Abort;
107706 assert( db->mallocFailed==0 );
107709 ** expressions in the sub-select were resolved, the sub-select
107715 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
107716 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
107721 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
107722 ** resolve the result-set expression list.
107725 sNC.pSrcList = p->pSrc;
107729 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
107732 /* If there are no aggregate functions in the result-set, and no GROUP BY
107735 assert( (p->selFlags & SF_Aggregate)==0 );
107736 pGroupBy = p->pGroupBy;
107740 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
107745 /* Add the output column list to the name-context before parsing the
107751 ** re-evaluated for each reference to it.
107754 sNC.uNC.pEList = p->pEList;
107756 if( p->pHaving ){
107757 if( (p->selFlags & SF_Aggregate)==0 ){
107758 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
107761 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
107763 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
107765 /* Resolve names in table-valued-function arguments */
107766 for(i=0; i<p->pSrc->nSrc; i++){
107767 SrcItem *pItem = &p->pSrc->a[i];
107768 if( pItem->fg.isTabFunc
107769 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
107778 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
107779 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
107780 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
107795 ** the sub-query back to the parent query. At this point each term
107799 if( p->selFlags & SF_Converted ){
107800 Select *pSub = p->pSrc->a[0].pSelect;
107801 p->pOrderBy = pSub->pOrderBy;
107802 pSub->pOrderBy = 0;
107807 ** below, after all of the result-sets for all of the elements of
107810 ** If there is an ORDER BY clause on a term of a compound-select other
107811 ** than the right-most term, then that is a syntax error. But the error
107815 if( p->pOrderBy!=0
107816 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
107817 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
107821 if( db->mallocFailed ){
107832 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
107835 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
107836 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
107846 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
107847 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
107853 p = p->pPrior;
107869 ** table columns and result-set columns. At the same time, do error
107877 ** the symbolic name assigned to an ATTACH-ed database.
107888 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
107892 ** To resolve result-set references, look for expression nodes of the
107893 ** form Z (with no X and Y prefix) where the Z matches the right-hand
107894 ** size of an AS clause in the result-set of a SELECT. The Z expression
107895 ** is replaced by a copy of the left-hand side of the result-set expression.
107896 ** Table-name and function resolution occurs on the substituted expression
107923 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107924 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107925 w.pParse = pNC->pParse;
107927 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
107931 w.pParse->nHeight += pExpr->nHeight;
107932 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
107939 w.pParse->nHeight -= pExpr->nHeight;
107943 testcase( pNC->ncFlags & NC_HasAgg );
107944 testcase( pNC->ncFlags & NC_HasWin );
107945 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
107946 pNC->ncFlags |= savedHasAgg;
107947 return pNC->nNcErr>0 || w.pParse->nErr>0;
107963 w.pParse = pNC->pParse;
107968 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107969 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107970 for(i=0; i<pList->nExpr; i++){
107971 Expr *pExpr = pList->a[i].pExpr;
107974 w.pParse->nHeight += pExpr->nHeight;
107975 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
107981 w.pParse->nHeight -= pExpr->nHeight;
107985 testcase( pNC->ncFlags & NC_HasAgg );
107986 testcase( pNC->ncFlags & NC_HasWin );
107987 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
107988 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
107989 savedHasAgg |= pNC->ncFlags &
107991 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
107993 if( w.pParse->nErr>0 ) return WRC_Abort;
107995 pNC->ncFlags |= savedHasAgg;
108001 ** descendants of the SELECT, including compounds off of p->pPrior,
108032 ** ------------
108040 ** nodes of the expression is set to -1 and the Expr.iColumn value is
108052 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
108053 NameContext sNC; /* Name context for pParse->pNewTable */
108063 sSrc.a[0].zName = pTab->zName;
108065 sSrc.a[0].iCursor = -1;
108066 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
108067 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
108106 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
108107 return pTab->aCol[iCol].affinity;
108114 ** or a sub-select with a column as the return value, then the
108128 op = pExpr->op;
108129 while( 1 /* exit-by-break */ ){
108130 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
108132 assert( pExpr->y.pTab!=0 );
108133 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
108137 assert( pExpr->x.pSelect!=0 );
108138 assert( pExpr->x.pSelect->pEList!=0 );
108139 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
108140 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
108145 return sqlite3AffinityType(pExpr->u.zToken, 0);
108149 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
108150 assert( pExpr->iColumn < pExpr->iTable );
108151 assert( pExpr->iColumn >= 0 );
108152 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
108154 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
108159 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
108162 assert( pExpr->op==TK_COLLATE
108163 || pExpr->op==TK_IF_NULL_ROW
108164 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
108165 pExpr = pExpr->pLeft;
108166 op = pExpr->op;
108169 if( op!=TK_REGISTER || (op = pExpr->op2)==TK_REGISTER ) break;
108171 return pExpr->affExpr;
108186 switch( pExpr->op ){
108190 pExpr = pExpr->pLeft;
108225 ExprList *pList = pExpr->x.pList;
108227 assert( pList->nExpr > 0);
108228 for(ii=1; ii<pList->nExpr; ii+=2){
108229 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
108231 if( pList->nExpr % 2 ){
108232 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
108249 ** If a memory allocation error occurs, that fact is recorded in pParse->db
108258 if( pCollName->n>0 ){
108259 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
108261 pNew->pLeft = pExpr;
108262 pNew->flags |= EP_Collate|EP_Skip;
108284 assert( pExpr->op==TK_COLLATE );
108285 pExpr = pExpr->pLeft;
108299 assert( pExpr->x.pList->nExpr>0 );
108300 assert( pExpr->op==TK_FUNCTION );
108301 pExpr = pExpr->x.pList->a[0].pExpr;
108302 }else if( pExpr->op==TK_COLLATE ){
108303 pExpr = pExpr->pLeft;
108326 sqlite3 *db = pParse->db;
108330 int op = p->op;
108331 if( op==TK_REGISTER ) op = p->op2;
108332 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
108337 assert( p->y.pTab!=0 );
108338 if( (j = p->iColumn)>=0 ){
108339 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
108345 p = p->pLeft;
108350 p = p->x.pList->a[0].pExpr;
108355 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
108358 if( p->flags & EP_Collate ){
108359 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
108360 p = p->pLeft;
108362 Expr *pNext = p->pRight;
108364 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
108365 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
108367 for(i=0; i<p->x.pList->nExpr; i++){
108368 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
108369 pNext = p->x.pList->a[i].pExpr;
108398 if( p==0 ) p = pParse->db->pDfltColl;
108409 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
108441 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
108442 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
108443 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
108444 assert( pExpr->pLeft );
108445 aff = sqlite3ExprAffinity(pExpr->pLeft);
108446 if( pExpr->pRight ){
108447 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
108449 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
108506 if( pLeft->flags & EP_Collate ){
108508 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
108529 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
108531 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
108552 if( pParse->nErr ) return 0;
108559 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
108561 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
108581 ** is a sub-select, return the number of columns in the sub-select. For
108585 u8 op = pExpr->op;
108586 if( op==TK_REGISTER ) op = pExpr->op2;
108589 return pExpr->x.pList->nExpr;
108592 return pExpr->x.pSelect->pEList->nExpr;
108599 ** Return a pointer to a subexpression of pVector that is the i-th
108609 ** just the expression for the i-th term of the result set, and may
108614 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
108616 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
108617 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
108619 return pVector->x.pSelect->pEList->a[i].pExpr;
108622 return pVector->x.pList->a[i].pExpr;
108631 ** the iField-th column of the vector expression pVector.
108651 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
108656 if( pVector->op==TK_SELECT ){
108664 ** pLeft->iTable: First in an array of register holding result, or 0
108677 pRet->iTable = nField;
108678 pRet->iColumn = iField;
108679 pRet->pLeft = pVector;
108682 if( pVector->op==TK_VECTOR ){
108685 ppVector = &pVector->x.pList->a[iField].pExpr;
108693 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
108701 ** sub-select returns more than one column, the first in an array
108709 if( pExpr->op==TK_SELECT ){
108717 ** Argument pVector points to a vector expression - either a TK_VECTOR
108725 ** containing the results of the sub-select.
108742 u8 op = pVector->op;
108746 return pVector->iTable+iField;
108750 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
108755 *ppExpr = pVector->x.pList->a[iField].pExpr;
108768 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
108769 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
108770 ** otherwise: op==pExpr->op and p5==0
108779 Vdbe *v = pParse->pVdbe;
108780 Expr *pLeft = pExpr->pLeft;
108781 Expr *pRight = pExpr->pRight;
108792 if( pParse->nErr ) return;
108797 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
108798 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
108799 || pExpr->op==TK_LT || pExpr->op==TK_GT
108800 || pExpr->op==TK_LE || pExpr->op==TK_GE
108802 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
108803 || (pExpr->op==TK_ISNOT && op==TK_NE) );
108804 assert( p5==0 || pExpr->op!=op );
108805 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
108833 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
108843 if( i==nLeft-1 ){
108851 if( i==nLeft-2 ) opx = op;
108869 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
108890 if( p->nHeight>*pnHeight ){
108891 *pnHeight = p->nHeight;
108898 for(i=0; i<p->nExpr; i++){
108899 heightOfExpr(p->a[i].pExpr, pnHeight);
108905 for(p=pSelect; p; p=p->pPrior){
108906 heightOfExpr(p->pWhere, pnHeight);
108907 heightOfExpr(p->pHaving, pnHeight);
108908 heightOfExpr(p->pLimit, pnHeight);
108909 heightOfExprList(p->pEList, pnHeight);
108910 heightOfExprList(p->pGroupBy, pnHeight);
108911 heightOfExprList(p->pOrderBy, pnHeight);
108926 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
108927 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
108928 nHeight = p->pRight->nHeight;
108931 heightOfSelect(p->x.pSelect, &nHeight);
108932 }else if( p->x.pList ){
108933 heightOfExprList(p->x.pList, &nHeight);
108934 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
108936 p->nHeight = nHeight + 1;
108948 if( pParse->nErr ) return;
108950 sqlite3ExprCheckHeight(pParse, p->nHeight);
108968 if( pParse->nErr ) return;
108969 if( p && ExprUseXList(p) && p->x.pList ){
108970 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
108982 pExpr->w.iOfst = iOfst;
108996 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
109000 ** can be translated into a 32-bit integer, then the token is not
109017 if( op!=TK_INTEGER || pToken->z==0
109018 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
109019 nExtra = pToken->n+1;
109026 pNew->op = (u8)op;
109027 pNew->iAgg = -1;
109030 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
109031 pNew->u.iValue = iValue;
109033 pNew->u.zToken = (char*)&pNew[1];
109034 assert( pToken->z!=0 || pToken->n==0 );
109035 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
109036 pNew->u.zToken[pToken->n] = 0;
109037 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
109043 pNew->nHeight = 1;
109050 ** Allocate a new expression node from a zero-terminated token that has
109077 assert( db->mallocFailed );
109082 assert( pRoot->x.pSelect==0 );
109084 pRoot->pRight = pRight;
109085 pRoot->flags |= EP_Propagate & pRight->flags;
109087 pRoot->nHeight = pRight->nHeight+1;
109089 pRoot->nHeight = 1;
109093 pRoot->pLeft = pLeft;
109094 pRoot->flags |= EP_Propagate & pLeft->flags;
109096 if( pLeft->nHeight>=pRoot->nHeight ){
109097 pRoot->nHeight = pLeft->nHeight+1;
109108 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
109118 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
109121 p->op = op & 0xff;
109122 p->iAgg = -1;
109123 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
109124 sqlite3ExprCheckHeight(pParse, p->nHeight);
109126 sqlite3ExprDelete(pParse->db, pLeft);
109127 sqlite3ExprDelete(pParse->db, pRight);
109138 pExpr->x.pSelect = pSelect;
109142 assert( pParse->db->mallocFailed );
109143 sqlite3SelectDelete(pParse->db, pSelect);
109170 for(ii=0; ii<pEList->nExpr; ii++){
109172 Expr *pExpr = pEList->a[ii].pExpr;
109174 if( pExpr->op==TK_VECTOR ){
109176 nExprElem = pExpr->x.pList->nExpr;
109181 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
109187 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
109188 pExpr->x.pList = 0;
109191 pSel->op = TK_ALL;
109192 pSel->pPrior = pRet;
109198 if( pRet && pRet->pPrior ){
109199 pRet->selFlags |= SF_MultiValue;
109201 sqlite3ExprListDelete(pParse->db, pEList);
109214 sqlite3 *db = pParse->db;
109220 u32 f = pLeft->flags | pRight->flags;
109244 sqlite3 *db = pParse->db;
109252 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
109254 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
109255 && !pParse->nested
109259 pNew->x.pList = pList;
109269 ** the arguments of a non-aggregate function.
109273 "ORDER BY may not be used with non-aggregate %#T()", p
109293 sqlite3 *db = pParse->db;
109295 assert( db->mallocFailed );
109299 assert( db->mallocFailed );
109303 assert( pExpr->op==TK_FUNCTION );
109304 assert( pExpr->pLeft==0 );
109306 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){
109307 /* Ignore ORDER BY on zero-argument aggregates */
109324 pOB->x.pList = pOrderBy;
109326 pExpr->pLeft = pOB;
109334 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
109336 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
109337 ** top-level SQL
109347 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
109349 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
109350 || (pParse->db->flags & SQLITE_TrustedSchema)==0
109381 sqlite3 *db = pParse->db;
109387 z = pExpr->u.zToken;
109394 x = (ynVar)(++pParse->nVar);
109402 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
109403 i = z[1]-'0'; /* The common case of ?N for a single digit N */
109406 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
109410 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
109411 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
109412 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
109414 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
109415 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
109419 if( x>pParse->nVar ){
109420 pParse->nVar = (int)x;
109422 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
109430 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
109432 x = (ynVar)(++pParse->nVar);
109437 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
109440 pExpr->iColumn = x;
109441 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
109443 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
109453 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
109455 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
109456 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
109459 assert( p->pLeft==0 );
109460 assert( p->pRight==0 );
109461 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
109462 assert( !ExprUseXList(p) || p->x.pList==0 );
109467 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
109468 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
109469 if( p->pRight ){
109471 sqlite3ExprDeleteNN(db, p->pRight);
109474 sqlite3SelectDelete(db, p->x.pSelect);
109476 sqlite3ExprListDelete(db, p->x.pList);
109479 sqlite3WindowDelete(db, p->y.pWin);
109498 }else if( p->pOn ){
109499 sqlite3ExprDeleteNN(db, p->pOn);
109500 }else if( p->pUsing ){
109501 sqlite3IdListDelete(db, p->pUsing);
109513 ** pExpr to the pParse->pConstExpr list with a register number of 0.
109529 sqlite3ExprDeleteNN(pParse->db, p);
109555 ** The dupedExprStructSize() function returns two values OR-ed together:
109568 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
109589 if( p->pLeft || p->x.pList ){
109592 assert( p->pRight==0 );
109606 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
109607 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
109627 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
109628 if( p->pRight ) nByte += dupedExprSize(p->pRight);
109636 ** compression while doing sqlite3ExprDup(). The top-level Expr does the
109652 ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
109664 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
109673 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
109675 sEdupBuf.zEnd = pEdupBuf->zEnd;
109684 }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
109685 nToken = sqlite3Strlen30NN(p->u.zToken)+1;
109706 ** by the copy of the p->u.zToken string (if any).
109711 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
109712 nToken = sqlite3Strlen30(p->u.zToken) + 1;
109718 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken );
109723 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=
109727 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
109733 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
109734 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
109735 pNew->flags |= staticFlag;
109741 /* Copy the p->u.zToken string, if any. */
109744 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
109745 memcpy(zToken, p->u.zToken, nToken);
109750 if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){
109752 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
109754 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
109756 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
109757 p->op!=TK_ORDER ? dupFlags : 0);
109762 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
109767 /* Fill in pNew->pLeft and pNew->pRight. */
109769 if( p->op==TK_SELECT_COLUMN ){
109770 pNew->pLeft = p->pLeft;
109771 assert( p->pRight==0
109772 || p->pRight==p->pLeft
109773 || ExprHasProperty(p->pLeft, EP_Subquery) );
109775 pNew->pLeft = p->pLeft ?
109776 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0;
109778 pNew->pRight = p->pRight ?
109779 exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0;
109781 if( p->op==TK_SELECT_COLUMN ){
109782 pNew->pLeft = p->pLeft;
109783 assert( p->pRight==0
109784 || p->pRight==p->pLeft
109785 || ExprHasProperty(p->pLeft, EP_Subquery) );
109787 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
109789 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
109801 ** and the db->mallocFailed flag set.
109807 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
109811 pRet->nCte = p->nCte;
109812 for(i=0; i<p->nCte; i++){
109813 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
109814 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
109815 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
109816 pRet->a[i].eM10d = p->a[i].eM10d;
109831 ** objects found there, assembling them onto the linked list at Select->pWin.
109834 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
109835 Select *pSelect = pWalker->u.pSelect;
109836 Window *pWin = pExpr->y.pWin;
109839 assert( pWin->ppThis==0 );
109845 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
109874 ** part of the in-memory representation of the database schema.
109891 pNew->nExpr = p->nExpr;
109892 pNew->nAlloc = p->nAlloc;
109893 pItem = pNew->a;
109894 pOldItem = p->a;
109895 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
109896 Expr *pOldExpr = pOldItem->pExpr;
109898 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
109900 && pOldExpr->op==TK_SELECT_COLUMN
109901 && (pNewExpr = pItem->pExpr)!=0
109903 if( pNewExpr->pRight ){
109904 pPriorSelectColOld = pOldExpr->pRight;
109905 pPriorSelectColNew = pNewExpr->pRight;
109906 pNewExpr->pLeft = pNewExpr->pRight;
109908 if( pOldExpr->pLeft!=pPriorSelectColOld ){
109909 pPriorSelectColOld = pOldExpr->pLeft;
109911 pNewExpr->pRight = pPriorSelectColNew;
109913 pNewExpr->pLeft = pPriorSelectColNew;
109916 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
109917 pItem->fg = pOldItem->fg;
109918 pItem->fg.done = 0;
109919 pItem->u = pOldItem->u;
109938 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
109941 pNew->nSrc = pNew->nAlloc = p->nSrc;
109942 for(i=0; i<p->nSrc; i++){
109943 SrcItem *pNewItem = &pNew->a[i];
109944 const SrcItem *pOldItem = &p->a[i];
109946 pNewItem->pSchema = pOldItem->pSchema;
109947 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
109948 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
109949 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
109950 pNewItem->fg = pOldItem->fg;
109951 pNewItem->iCursor = pOldItem->iCursor;
109952 pNewItem->addrFillSub = pOldItem->addrFillSub;
109953 pNewItem->regReturn = pOldItem->regReturn;
109954 if( pNewItem->fg.isIndexedBy ){
109955 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
109957 pNewItem->u2 = pOldItem->u2;
109958 if( pNewItem->fg.isCte ){
109959 pNewItem->u2.pCteUse->nUse++;
109961 if( pNewItem->fg.isTabFunc ){
109962 pNewItem->u1.pFuncArg =
109963 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
109965 pTab = pNewItem->pTab = pOldItem->pTab;
109967 pTab->nTabRef++;
109969 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
109970 if( pOldItem->fg.isUsing ){
109971 assert( pNewItem->fg.isUsing );
109972 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
109974 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
109976 pNewItem->colUsed = pOldItem->colUsed;
109985 assert( p->eU4!=EU4_EXPR );
109986 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
109988 pNew->nId = p->nId;
109989 pNew->eU4 = p->eU4;
109990 for(i=0; i<p->nId; i++){
109991 struct IdList_item *pNewItem = &pNew->a[i];
109992 const struct IdList_item *pOldItem = &p->a[i];
109993 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
109994 pNewItem->u4 = pOldItem->u4;
110005 for(p=pDup; p; p=p->pPrior){
110008 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
110009 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
110010 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
110011 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
110012 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
110013 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
110014 pNew->op = p->op;
110015 pNew->pNext = pNext;
110016 pNew->pPrior = 0;
110017 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
110018 pNew->iLimit = 0;
110019 pNew->iOffset = 0;
110020 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
110021 pNew->addrOpenEphm[0] = -1;
110022 pNew->addrOpenEphm[1] = -1;
110023 pNew->nSelectRow = p->nSelectRow;
110024 pNew->pWith = sqlite3WithDup(db, p->pWith);
110026 pNew->pWin = 0;
110027 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
110028 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
110030 pNew->selId = p->selId;
110031 if( db->mallocFailed ){
110035 pNew->pNext = 0;
110040 pp = &pNew->pPrior;
110062 ** NULL is returned. If non-NULL is returned, then it is guaranteed
110073 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
110078 pList->nAlloc = 4;
110079 pList->nExpr = 1;
110080 pItem = &pList->a[0];
110082 pItem->pExpr = pExpr;
110092 pList->nAlloc *= 2;
110094 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
110102 pItem = &pList->a[pList->nExpr++];
110104 pItem->pExpr = pExpr;
110114 return sqlite3ExprListAppendNew(pParse->db,pExpr);
110116 if( pList->nAlloc<pList->nExpr+1 ){
110117 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
110119 pItem = &pList->a[pList->nExpr++];
110121 pItem->pExpr = pExpr;
110142 sqlite3 *db = pParse->db;
110145 int iFirst = pList ? pList->nExpr : 0;
110156 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
110158 pColumns->nId, n);
110162 for(i=0; i<pColumns->nId; i++){
110163 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
110164 assert( pSubExpr!=0 || db->mallocFailed );
110168 assert( pList->nExpr==iFirst+i+1 );
110169 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
110170 pColumns->a[i].zName = 0;
110174 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
110175 Expr *pFirst = pList->a[iFirst].pExpr;
110177 assert( pFirst->op==TK_SELECT_COLUMN );
110181 pFirst->pRight = pExpr;
110186 pFirst->iTable = pColumns->nId;
110201 assert( p->nExpr>0 );
110213 pItem = &p->a[p->nExpr-1];
110214 assert( pItem->fg.bNulls==0 );
110218 pItem->fg.sortFlags = (u8)iSortOrder;
110221 pItem->fg.bNulls = 1;
110223 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
110233 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
110242 assert( pList!=0 || pParse->db->mallocFailed!=0 );
110243 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
110246 assert( pList->nExpr>0 );
110247 pItem = &pList->a[pList->nExpr-1];
110248 assert( pItem->zEName==0 );
110249 assert( pItem->fg.eEName==ENAME_NAME );
110250 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
110252 /* If dequote==0, then pName->z does not point to part of a DDL
110254 ** to the token-map. */
110255 sqlite3Dequote(pItem->zEName);
110257 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
110268 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
110277 sqlite3 *db = pParse->db;
110278 assert( pList!=0 || db->mallocFailed!=0 );
110280 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
110281 assert( pList->nExpr>0 );
110282 if( pItem->zEName==0 ){
110283 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
110284 pItem->fg.eEName = ENAME_SPAN;
110298 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
110299 testcase( pEList && pEList->nExpr==mx );
110300 testcase( pEList && pEList->nExpr==mx+1 );
110301 if( pEList && pEList->nExpr>mx ){
110310 int i = pList->nExpr;
110311 struct ExprList_item *pItem = pList->a;
110312 assert( pList->nExpr>0 );
110315 sqlite3ExprDelete(db, pItem->pExpr);
110316 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
110318 }while( --i>0 );
110326 ** Return the bitwise-OR of all Expr.flags fields in the given
110333 for(i=0; i<pList->nExpr; i++){
110334 Expr *pExpr = pList->a[i].pExpr;
110336 m |= pExpr->flags;
110342 ** This is a SELECT-node callback for the expression walker that
110344 ** pWalker->eCode to zero and abort.
110350 pWalker->eCode = 0;
110371 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
110376 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
110378 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
110380 pExpr->op = TK_TRUEFALSE;
110393 assert( pExpr->op==TK_TRUEFALSE );
110395 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
110396 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
110397 return pExpr->u.zToken[4]==0;
110415 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
110416 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
110417 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
110419 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
110421 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
110436 ** sqlite3ExprIsConstant() pWalker->eCode==1
110437 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
110438 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
110439 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
110456 /* If pWalker->eCode is 2 then any term of the expression that comes from
110459 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
110460 pWalker->eCode = 0;
110464 switch( pExpr->op ){
110466 ** and either pWalker->eCode==4 or 5 or the function has the
110469 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
110472 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
110475 pWalker->eCode = 0;
110488 testcase( pExpr->op==TK_ID );
110489 testcase( pExpr->op==TK_COLUMN );
110490 testcase( pExpr->op==TK_AGG_FUNCTION );
110491 testcase( pExpr->op==TK_AGG_COLUMN );
110492 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
110495 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
110502 testcase( pExpr->op==TK_REGISTER );
110503 testcase( pExpr->op==TK_IF_NULL_ROW );
110504 testcase( pExpr->op==TK_DOT );
110505 pWalker->eCode = 0;
110508 if( pWalker->eCode==5 ){
110512 pExpr->op = TK_NULL;
110513 }else if( pWalker->eCode==4 ){
110516 pWalker->eCode = 0;
110521 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
110522 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
110540 ** Walk an expression tree. Return non-zero if the expression is constant
110543 ** For the purposes of this function, a double-quoted string (ex: "abc")
110544 ** is considered a variable but a single-quoted string (ex: 'abc') is
110552 ** Walk an expression tree. Return non-zero if
110561 ** can be added to the pParse->pConstExpr list and evaluated once when
110569 ** Walk an expression tree. Return non-zero if the expression is constant
110571 ** expression must not refer to any non-deterministic function nor any
110580 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
110582 ** sources anywhere else in the query. Return true (non-zero) if pExpr
110589 ** To be an single-source constraint, the following must be true:
110591 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
110593 ** (2) pExpr cannot use subqueries or non-deterministic functions.
110613 ** Without this restriction, accepting pExpr as a single-table
110617 ** on push-down.
110624 const SrcItem *pSrc = &pSrcList->a[iSrc];
110625 if( pSrc->fg.jointype & JT_LTORJ ){
110628 if( pSrc->fg.jointype & JT_LEFT ){
110630 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
110635 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
110639 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
110640 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
110647 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
110655 ExprList *pGroupBy = pWalker->u.pGroupBy;
110660 for(i=0; i<pGroupBy->nExpr; i++){
110661 Expr *p = pGroupBy->a[i].pExpr;
110662 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
110663 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
110670 /* Check if pExpr is a sub-select. If so, consider it variable. */
110672 pWalker->eCode = 0;
110680 ** Walk the expression tree passed as the first argument. Return non-zero
110688 ** assumes that no other collating sequence will have a finer-grained
110711 ** in a CREATE TABLE statement. Return non-zero if the expression is
110712 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
110726 ** For the purposes of this function, a double-quoted string (ex: "abc")
110727 ** is considered a variable but a single-quoted string (ex: 'abc') is
110755 ** to fit in a 32-bit integer, return 1 and put the value of the integer
110757 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
110763 /* If an expression is an integer literal that fits in a signed 32-bit
110765 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
110766 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
110768 if( p->flags & EP_IntValue ){
110769 *pValue = p->u.iValue;
110772 switch( p->op ){
110774 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
110779 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
110781 *pValue = -v;
110808 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
110809 p = p->pLeft;
110812 op = p->op;
110813 if( op==TK_REGISTER ) op = p->op2;
110823 || p->y.pTab==0 /* Reference to column of index on expression */
110825 || (p->iColumn==XN_ROWID && IsView(p->y.pTab))
110827 || (p->iColumn>=0
110828 && p->y.pTab->aCol!=0 /* Possible due to prior error */
110829 && p->y.pTab->aCol[p->iColumn].notNull==0);
110849 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
110850 if( p->op==TK_UMINUS ) unaryMinus = 1;
110851 p = p->pLeft;
110853 op = p->op;
110854 if( op==TK_REGISTER ) op = p->op2;
110869 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
110870 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
110879 ** Return TRUE if the given string is a row-id column name.
110890 ** pTab. An alias is usable if there is not an explicit user-defined column
110899 for(iCol=0; iCol<pTab->nCol; iCol++){
110900 if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break;
110902 if( iCol==pTab->nCol ){
110925 p = pX->x.pSelect;
110926 if( p->pPrior ) return 0; /* Not a compound SELECT */
110927 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
110928 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
110929 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
110932 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
110933 if( p->pLimit ) return 0; /* Has no LIMIT clause */
110934 if( p->pWhere ) return 0; /* Has no WHERE clause */
110935 pSrc = p->pSrc;
110937 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
110938 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
110939 pTab = pSrc->a[0].pTab;
110943 pEList = p->pEList;
110946 for(i=0; i<pEList->nExpr; i++){
110947 Expr *pRes = pEList->a[i].pExpr;
110948 if( pRes->op!=TK_COLUMN ) return 0;
110949 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
110957 ** Generate code that checks the left-most column of index table iCur to see if
110959 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
110977 ** right-hand side. Return TRUE if that list is constant.
110983 pLHS = pIn->pLeft;
110984 pIn->pLeft = 0;
110986 pIn->pLeft = pLHS;
110996 ** The job of this routine is to find or create a b-tree object that can
111000 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
111003 ** The returned value of this function indicates the b-tree type, as follows:
111005 ** IN_INDEX_ROWID - The cursor was opened on a database table.
111006 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
111007 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
111008 ** IN_INDEX_EPH - The cursor was opened on a specially created and
111010 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
111013 ** An existing b-tree might be used if the RHS expression pX is a simple
111020 ** pX->iTable made to point to the ephemeral table instead of an
111023 ** will be set on pX and the pX->y.sub fields will be set to show where
111028 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
111032 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
111033 ** through the set members) then the b-tree must not contain duplicates.
111035 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
111038 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
111039 ** for fast set membership tests) then an ephemeral table must
111041 ** index can be found with the specified <columns> as its left-most.
111045 ** routine might decide that creating an ephemeral b-tree for membership
111050 ** When the b-tree is being used for membership tests, the calling function
111059 ** the value in that register will be NULL if the b-tree contains one or more
111060 ** NULL values, and it will be some non-NULL value if the b-tree contains no
111089 assert( pX->op==TK_IN );
111091 iTab = pParse->nTab++;
111100 ExprList *pEList = pX->x.pSelect->pEList;
111101 for(i=0; i<pEList->nExpr; i++){
111102 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
111104 if( i==pEList->nExpr ){
111112 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
111113 sqlite3 *db = pParse->db; /* Database connection */
111116 ExprList *pEList = p->pEList;
111117 int nExpr = pEList->nExpr;
111119 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
111120 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
111121 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
111122 pTab = p->pSrc->a[0].pTab;
111125 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
111128 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
111131 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
111139 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
111151 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
111152 int iCol = pEList->a[i].pExpr->iColumn;
111174 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
111177 if( pIdx->nColumn<nExpr ) continue;
111178 if( pIdx->pPartIdxWhere!=0 ) continue;
111179 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
111181 testcase( pIdx->nColumn==BMS-2 );
111182 testcase( pIdx->nColumn==BMS-1 );
111183 if( pIdx->nColumn>=BMS-1 ) continue;
111185 if( pIdx->nKeyCol>nExpr
111186 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
111194 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
111195 Expr *pRhs = pEList->a[i].pExpr;
111200 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
111201 assert( pIdx->azColl[j] );
111202 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
111214 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
111215 if( colUsed==(MASKBIT(nExpr)-1) ){
111219 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
111220 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
111222 VdbeComment((v, "%s", pIdx->zName));
111224 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
111228 i64 mask = (1<<nExpr)-1;
111232 *prRhsHasNull = ++pParse->nMem;
111254 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
111256 pParse->nTab--; /* Back out the allocation of the unused cursor */
111257 iTab = -1; /* Cursor is not allocated */
111262 /* Could not find an existing table or index to use as the RHS b-tree.
111265 u32 savedNQueryLoop = pParse->nQueryLoop;
111269 pParse->nQueryLoop = 0;
111271 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
111273 assert( pX->op==TK_IN );
111278 pParse->nQueryLoop = savedNQueryLoop;
111283 n = sqlite3ExprVectorSize(pX->pLeft);
111294 ** function allocates and returns a nul-terminated string containing
111301 Expr *pLeft = pExpr->pLeft;
111303 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
111306 assert( pExpr->op==TK_IN );
111307 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
111314 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
111330 ** "sub-select returns N columns - expected M"
111333 if( pParse->nErr==0 ){
111334 const char *zFmt = "sub-select returns %d columns - expected %d";
111342 ** it is not permitted. If pExpr is a sub-select vector, this routine
111345 ** "sub-select returns N columns - expected 1"
111354 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
111368 ** x IN (4,5,11) -- IN operator with list on right-hand side
111369 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
111373 ** table is computed, the cursor number is also stored in pExpr->iTable,
111397 v = pParse->pVdbe;
111403 ** * The right-hand side is a correlated subquery
111404 ** * The right-hand side is an expression list containing variables
111410 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
111419 pExpr->x.pSelect->selId));
111422 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
111423 pExpr->y.sub.iAddr);
111424 assert( iTab!=pExpr->iTable );
111425 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
111434 pExpr->y.sub.regReturn = ++pParse->nMem;
111435 pExpr->y.sub.iAddr =
111436 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
111442 pLeft = pExpr->pLeft;
111448 pExpr->iTable = iTab;
111449 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
111452 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
111457 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
111465 Select *pSelect = pExpr->x.pSelect;
111466 ExprList *pEList = pSelect->pEList;
111469 addrOnce?"":"CORRELATED ", pSelect->selId
111473 if( ALWAYS(pEList->nExpr==nVal) ){
111480 pSelect->iLimit = 0;
111481 testcase( pSelect->selFlags & SF_Distinct );
111483 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
111484 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
111485 sqlite3SelectDelete(pParse->db, pCopy);
111486 sqlite3DbFree(pParse->db, dest.zAffSdst);
111493 assert( pEList->nExpr>0 );
111497 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
111498 pParse, p, pEList->a[i].pExpr
111502 }else if( ALWAYS(pExpr->x.pList!=0) ){
111512 ExprList *pList = pExpr->x.pList;
111523 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
111529 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
111530 Expr *pE2 = pItem->pExpr;
111534 ** this code only executes once. Because for a non-constant
111538 sqlite3VdbeChangeToNoop(v, addrOnce-1);
111560 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
111561 || pParse->nErr );
111562 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
111563 pExpr->y.sub.iAddr, 1);
111574 ** (SELECT a FROM b) -- subquery
111575 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
111579 ** Return the register that holds the result. For a multi-column SELECT,
111581 ** return value is the register of the left-most result column.
111596 Vdbe *v = pParse->pVdbe;
111598 if( pParse->nErr ) return 0;
111599 testcase( pExpr->op==TK_EXISTS );
111600 testcase( pExpr->op==TK_SELECT );
111601 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
111603 pSel = pExpr->x.pSelect;
111608 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
111610 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
111611 pExpr->y.sub.iAddr);
111612 return pExpr->iTable;
111619 pExpr->y.sub.regReturn = ++pParse->nMem;
111620 pExpr->y.sub.iAddr =
111621 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
111626 ** * The right-hand side is a correlated subquery
111627 ** * The right-hand side is an expression list containing variables
111648 addrOnce?"":"CORRELATED ", pSel->selId));
111649 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
111650 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
111651 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
111652 pParse->nMem += nReg;
111653 if( pExpr->op==TK_SELECT ){
111657 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
111664 if( pSel->pLimit ){
111665 /* The subquery already has a limit. If the pre-existing limit is X
111667 sqlite3 *db = pParse->db;
111670 pLimit->affExpr = SQLITE_AFF_NUMERIC;
111672 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
111674 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
111675 pSel->pLimit->pLeft = pLimit;
111677 /* If there is no pre-existing limit add a limit of 1 */
111678 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
111679 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
111681 pSel->iLimit = 0;
111683 pExpr->op2 = pExpr->op;
111684 pExpr->op = TK_ERROR;
111687 pExpr->iTable = rReg = dest.iSDParm;
111692 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
111696 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
111697 || pParse->nErr );
111698 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
111699 pExpr->y.sub.iAddr, 1);
111709 ** sub-select on the RHS of the IN() operator has the same number of
111711 ** a sub-query, that the LHS is a vector of size 1.
111714 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
111715 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
111716 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
111717 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
111721 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
111735 ** The left-hand side (LHS) is a scalar or vector expression. The
111736 ** right-hand side (RHS) is an array of zero or more scalar values, or a
111751 ** See the separate in-operator.md documentation file in the canonical
111775 int addrTop; /* Top of the step-6 loop */
111777 u8 okConstFactor = pParse->okConstFactor;
111780 pLeft = pExpr->pLeft;
111783 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
111785 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
111787 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
111793 v = pParse->pVdbe;
111801 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
111806 ** nVector-1. */
111826 assert( pParse->okConstFactor==okConstFactor );
111827 pParse->okConstFactor = 0;
111829 pParse->okConstFactor = okConstFactor;
111846 ** This is step (1) in the in-operator.md optimized algorithm.
111856 pList = pExpr->x.pList;
111857 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
111862 for(ii=0; ii<pList->nExpr; ii++){
111863 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
111864 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
111868 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
111872 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
111873 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
111874 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
111875 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
111906 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
111907 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
111914 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
111919 /* In this case, the RHS is the ROWID of table b-tree and so we also
111920 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
111938 /* Step 4. If the RHS is known to be non-NULL and we did not find
111998 sqlite3DbFree(pParse->db, aiMap);
111999 sqlite3DbFree(pParse->db, zAff);
112006 ** value described by z[0..n-1] into register iMem.
112008 ** The z[] string will probably not be zero-terminated. But the
112017 if( negateFlag ) value = -value;
112026 ** text z[0..n-1] into register iMem.
112028 ** Expr.u.zToken is always UTF8 and zero-terminated.
112031 Vdbe *v = pParse->pVdbe;
112032 if( pExpr->flags & EP_IntValue ){
112033 int i = pExpr->u.iValue;
112035 if( negFlag ) i = -i;
112040 const char *z = pExpr->u.zToken;
112045 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
112050 negFlag?"-":"",pExpr);
112058 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
112066 ** appropriate for the iIdxCol-th column of index pIdx.
112075 i16 iTabCol = pIdx->aiColumn[iIdxCol];
112077 assert( pIdx->aColExpr );
112078 assert( pIdx->aColExpr->nExpr>iIdxCol );
112079 pParse->iSelfTab = iTabCur + 1;
112080 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
112081 pParse->iSelfTab = 0;
112083 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
112100 Vdbe *v = pParse->pVdbe;
112101 int nErr = pParse->nErr;
112103 assert( pParse->iSelfTab!=0 );
112104 if( pParse->iSelfTab>0 ){
112105 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
112110 if( pCol->affinity>=SQLITE_AFF_TEXT ){
112111 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
112114 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
112119 ** Generate code to extract the value of the iCol-th column of a table.
112132 if( iCol<0 || iCol==pTab->iPKey ){
112134 VdbeComment((v, "%s.rowid", pTab->zName));
112142 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
112144 if( pCol->colFlags & COLFLAG_BUSY ){
112146 pCol->zCnName);
112148 int savedSelfTab = pParse->iSelfTab;
112149 pCol->colFlags |= COLFLAG_BUSY;
112150 pParse->iSelfTab = iTabCur+1;
112152 pParse->iSelfTab = savedSelfTab;
112153 pCol->colFlags &= ~COLFLAG_BUSY;
112172 ** Generate code that will extract the iColumn-th column from
112186 assert( pParse->pVdbe!=0 );
112189 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
112191 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
112192 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
112193 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG);
112199 ** Generate code to move content from registers iFrom...iFrom+nReg-1
112200 ** over to iTo..iTo+nReg-1.
112203 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
112214 p->op2 = p->op;
112215 p->op = TK_REGISTER;
112216 p->iTable = iReg;
112237 if( p->op==TK_SELECT ){
112245 iResult = pParse->nMem+1;
112246 pParse->nMem += nResult;
112249 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
112257 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
112261 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
112268 ** in-line rather than by using the usual callbacks.
112277 Vdbe *v = pParse->pVdbe;
112280 nFarg = pFarg->nExpr;
112281 assert( nFarg>0 ); /* All in-line functions have at least one argument */
112284 /* Attempt a direct implementation of the built-in COALESCE() and
112286 ** arguments past the first non-NULL argument.
112291 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
112295 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
112310 Expr *pArg = pFarg->a[0].pExpr;
112311 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
112312 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
112320 /* The UNLIKELY() function is a no-op. The result is the value
112324 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
112329 ** Test-only SQL functions that are only usable if enabled
112337 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
112346 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
112355 pA1 = pFarg->a[1].pExpr;
112356 if( pA1->op==TK_COLUMN ){
112358 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
112375 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
112379 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
112388 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
112400 for(p=pParse->pIdxEpr; p; p=p->pIENext){
112402 int iDataCur = p->iDataCur;
112404 if( pParse->iSelfTab ){
112405 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
112406 iDataCur = -1;
112408 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
112409 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
112411 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
112412 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
112413 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
112419 v = pParse->pVdbe;
112421 if( p->bMaybeNullRow ){
112426 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
112428 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
112429 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
112431 p = pParse->pIdxEpr;
112432 pParse->pIdxEpr = 0;
112434 pParse->pIdxEpr = p;
112437 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
112438 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
112442 return -1; /* Not found */
112458 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
112459 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
112460 Vdbe *v = pParse->pVdbe;
112464 if( p->bMaybeNullRow ){
112465 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur);
112467 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
112468 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0,
112469 (const char*)&p->aff, 1);
112493 Vdbe *v = pParse->pVdbe; /* The VM under construction */
112496 int regFree1 = 0; /* If non-zero free this temporary register */
112497 int regFree2 = 0; /* If non-zero free this temporary register */
112502 assert( target>0 && target<=pParse->nMem );
112508 }else if( pParse->pIdxEpr!=0
112515 op = pExpr->op;
112520 AggInfo *pAggInfo = pExpr->pAggInfo;
112523 assert( pExpr->iAgg>=0 );
112524 if( pExpr->iAgg>=pAggInfo->nColumn ){
112530 ** tag-20230325-2 */
112536 pCol = &pAggInfo->aCol[pExpr->iAgg];
112537 if( !pAggInfo->directMode ){
112538 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
112539 }else if( pAggInfo->useSortingIdx ){
112540 Table *pTab = pCol->pTab;
112541 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
112542 pCol->iSorterColumn, target);
112545 }else if( pCol->iColumn<0 ){
112546 VdbeComment((v,"%s.rowid",pTab->zName));
112549 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
112550 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
112555 }else if( pExpr->y.pTab==0 ){
112558 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
112565 int iTab = pExpr->iTable;
112569 ** constraints, and that constant is coded by the pExpr->pLeft
112575 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
112577 assert( pExpr->y.pTab!=0 );
112578 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
112584 &zAff[(aff-'B')*2], P4_STATIC);
112589 if( pParse->iSelfTab<0 ){
112593 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
112599 int iCol = pExpr->iColumn;
112601 pTab = pExpr->y.pTab;
112604 assert( iCol<pTab->nCol );
112606 return -1-pParse->iSelfTab;
112608 pCol = pTab->aCol + iCol;
112610 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
112612 if( pCol->colFlags & COLFLAG_GENERATED ){
112613 if( pCol->colFlags & COLFLAG_BUSY ){
112615 pCol->zCnName);
112618 pCol->colFlags |= COLFLAG_BUSY;
112619 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
112622 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
112626 if( pCol->affinity==SQLITE_AFF_REAL ){
112636 iTab = pParse->iSelfTab - 1;
112639 else if( pParse->pIdxPartExpr
112645 assert( pExpr->y.pTab!=0 );
112646 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
112647 pExpr->iColumn, iTab, target,
112648 pExpr->op2);
112662 codeReal(v, pExpr->u.zToken, 0, target);
112668 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
112676 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
112686 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
112687 assert( pExpr->u.zToken[1]=='\'' );
112688 z = &pExpr->u.zToken[2];
112689 n = sqlite3Strlen30(z) - 1;
112698 assert( pExpr->u.zToken!=0 );
112699 assert( pExpr->u.zToken[0]!=0 );
112700 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
112701 if( pExpr->u.zToken[1]!=0 ){
112702 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
112703 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
112704 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
112710 return pExpr->iTable;
112715 sqlite3ExprCode(pParse, pExpr->pLeft, target);
112719 sqlite3AffinityType(pExpr->u.zToken, 0));
112727 /* fall-through */
112734 Expr *pLeft = pExpr->pLeft;
112739 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
112741 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
112783 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
112784 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
112791 Expr *pLeft = pExpr->pLeft;
112793 if( pLeft->op==TK_INTEGER ){
112797 }else if( pLeft->op==TK_FLOAT ){
112799 codeReal(v, pLeft->u.zToken, 1, target);
112808 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
112818 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
112826 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
112828 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
112829 bNormal = pExpr->op2==TK_IS;
112841 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
112851 AggInfo *pInfo = pExpr->pAggInfo;
112853 || NEVER(pExpr->iAgg<0)
112854 || NEVER(pExpr->iAgg>=pInfo->nFunc)
112859 return AggInfoFuncReg(pInfo, pExpr->iAgg);
112870 sqlite3 *db = pParse->db; /* The database connection */
112876 return pExpr->y.pWin->regResult;
112883 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
112887 pFarg = pExpr->x.pList;
112888 nFarg = pFarg ? pFarg->nExpr : 0;
112890 zId = pExpr->u.zToken;
112893 if( pDef==0 && pParse->explain ){
112897 if( pDef==0 || pDef->xFinalize!=0 ){
112901 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){
112902 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
112903 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
112905 SQLITE_PTR_TO_INT(pDef->pUserData), target);
112906 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
112911 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
112915 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
112916 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
112921 r1 = pParse->nMem+1;
112922 pParse->nMem += nFarg;
112932 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
112935 assert( pFarg->a[0].pExpr!=0 );
112936 exprOp = pFarg->a[0].pExpr->op;
112942 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
112943 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
112944 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
112945 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG;
112967 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
112969 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
112972 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
112973 if( !pColl ) pColl = db->pDfltColl;
112977 pDef, pExpr->op2);
112993 if( pParse->db->mallocFailed ){
112997 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
113007 Expr *pLeft = pExpr->pLeft;
113008 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
113009 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
113010 pLeft->op2 = pParse->withinRJSubrtn;
113012 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
113014 if( pExpr->iTable!=n ){
113016 pExpr->iTable, n);
113018 return pLeft->iTable + pExpr->iColumn;
113041 ** X is stored in pExpr->pLeft.
113042 ** Y is stored in pExpr->pList->a[0].pExpr.
113043 ** Z is stored in pExpr->pList->a[1].pExpr.
113051 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
113052 ** "SOFT-COLLATE" that is added to constraints that are pushed down
113053 ** from outer queries into sub-queries by the push-down optimization.
113056 assert( pExpr->pLeft );
113057 sqlite3ExprCode(pParse, pExpr->pLeft, target);
113061 pExpr = pExpr->pLeft;
113062 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
113067 pExpr = pExpr->pLeft;
113068 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
113073 ** to a column in the new.* or old.* pseudo-tables available to
113075 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
113076 ** is set to the column of the pseudo-table to read, or to -1 to
113081 ** to reference another column of the old.* pseudo-table, where
113083 ** set to (n+1), where n is the number of columns in each pseudo-table.
113084 ** For a reference to any other column in the new.* pseudo-table, p1
113093 ** p1==0 -> old.rowid p1==3 -> new.rowid
113094 ** p1==1 -> old.a p1==4 -> new.a
113095 ** p1==2 -> old.b p1==5 -> new.b
113102 pTab = pExpr->y.pTab;
113103 iCol = pExpr->iColumn;
113104 p1 = pExpr->iTable * (pTab->nCol+1) + 1
113107 assert( pExpr->iTable==0 || pExpr->iTable==1 );
113108 assert( iCol>=-1 && iCol<pTab->nCol );
113109 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
113110 assert( p1>=0 && p1<(pTab->nCol*2+2) );
113114 (pExpr->iTable ? "new" : "old"),
113115 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
113122 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
113124 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
113137 ** that derive from the right-hand table of a LEFT JOIN. The
113138 ** Expr.iTable value is the table number for the right-hand table.
113144 u8 okConstFactor = pParse->okConstFactor;
113145 AggInfo *pAggInfo = pExpr->pAggInfo;
113147 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
113148 if( !pAggInfo->directMode ){
113149 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
113152 if( pExpr->pAggInfo->useSortingIdx ){
113153 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
113154 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
113160 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target);
113168 pParse->okConstFactor = 0; /* note (1) above */
113169 sqlite3ExprCode(pParse, pExpr->pLeft, target);
113171 pParse->okConstFactor = okConstFactor;
113187 ** X (if it exists) is in pExpr->pLeft.
113188 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
113191 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
113208 sqlite3 *db = pParse->db;
113210 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
113211 assert(pExpr->x.pList->nExpr > 0);
113212 pEList = pExpr->x.pList;
113213 aListelem = pEList->a;
113214 nExpr = pEList->nExpr;
113216 if( (pX = pExpr->pLeft)!=0 ){
113218 if( db->mallocFailed ){
113222 testcase( pX->op==TK_COLUMN );
113230 ** The value in regFree1 might get SCopy-ed into the file result.
113235 for(i=0; i<nExpr-1; i=i+2){
113243 testcase( pTest->op==TK_COLUMN );
113245 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
113251 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
113262 assert( pExpr->affExpr==OE_Rollback
113263 || pExpr->affExpr==OE_Abort
113264 || pExpr->affExpr==OE_Fail
113265 || pExpr->affExpr==OE_Ignore
113267 if( !pParse->pTriggerTab && !pParse->nested ){
113269 "RAISE() may only be used within a trigger-program");
113272 if( pExpr->affExpr==OE_Abort ){
113276 if( pExpr->affExpr==OE_Ignore ){
113278 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
113282 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
113283 pExpr->affExpr, pExpr->u.zToken, 0, 0);
113320 p = pParse->pConstExpr;
113324 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
113325 if( pItem->fg.reusable
113326 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
113328 return pItem->u.iConstExprReg;
113332 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
113334 Vdbe *v = pParse->pVdbe;
113338 pParse->okConstFactor = 0;
113339 if( !pParse->db->mallocFailed ){
113340 if( regDest<0 ) regDest = ++pParse->nMem;
113343 pParse->okConstFactor = 1;
113344 sqlite3ExprDelete(pParse->db, pExpr);
113349 struct ExprList_item *pItem = &p->a[p->nExpr-1];
113350 pItem->fg.reusable = regDest<0;
113351 if( regDest<0 ) regDest = ++pParse->nMem;
113352 pItem->u.iConstExprReg = regDest;
113354 pParse->pConstExpr = p;
113377 && pExpr->op!=TK_REGISTER
113381 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
113404 assert( target>0 && target<=pParse->nMem );
113405 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
113406 if( pParse->pVdbe==0 ) return;
113413 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
113419 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
113429 sqlite3 *db = pParse->db;
113431 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
113442 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
113454 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
113479 Vdbe *v = pParse->pVdbe;
113482 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
113483 n = pList->nExpr;
113485 for(pItem=pList->a, i=0; i<n; i++, pItem++){
113486 Expr *pExpr = pItem->pExpr;
113488 if( pItem->fg.bSorterRef ){
113489 i--;
113490 n--;
113493 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
113495 i--;
113496 n--;
113498 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
113509 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
113510 && pOp->p1+pOp->p3+1==inReg
113511 && pOp->p2+pOp->p3+1==target+i
113512 && pOp->p5==0 /* The do-not-merge flag must be clear */
113514 pOp->p3++;
113556 sqlite3 *db = pParse->db;
113563 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
113564 if( db->mallocFailed==0 ){
113570 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
113573 compRight.pRight = pExpr->x.pList->a[1].pExpr;
113583 pDel->flags |= EP_OuterON;
113617 Vdbe *v = pParse->pVdbe;
113627 op = pExpr->op;
113637 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
113639 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
113643 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
113644 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
113650 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
113657 isNot = pExpr->op2==TK_ISNOT;
113658 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
113662 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
113665 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
113683 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
113685 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113686 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
113687 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
113707 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113735 /* No-op */
113760 Vdbe *v = pParse->pVdbe;
113771 /* The value of pExpr->op and op are related as follows:
113773 ** pExpr->op op
113774 ** --------- ----------
113784 ** For other values of pExpr->op, op is undefined and unused.
113789 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
113793 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
113794 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
113795 assert( pExpr->op!=TK_NE || op==OP_Eq );
113796 assert( pExpr->op!=TK_EQ || op==OP_Ne );
113797 assert( pExpr->op!=TK_LT || op==OP_Ge );
113798 assert( pExpr->op!=TK_LE || op==OP_Gt );
113799 assert( pExpr->op!=TK_GT || op==OP_Le );
113800 assert( pExpr->op!=TK_GE || op==OP_Lt );
113802 switch( pExpr->op ){
113808 }else if( pExpr->op==TK_AND ){
113810 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
113811 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
113815 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
113817 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
113824 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
113831 isNot = pExpr->op2==TK_ISNOT;
113832 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
113837 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
113842 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
113849 testcase( pExpr->op==TK_IS );
113850 testcase( pExpr->op==TK_ISNOT );
113851 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
113860 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
113862 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113863 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
113864 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
113882 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
113912 /* no-op */
113933 sqlite3 *db = pParse->db;
113935 if( db->mallocFailed==0 ){
113945 ** If pExpr is a simple SQL value - an integer, real, string, blob
113946 ** or NULL value - then the VDBE currently being prepared is configured
113947 ** to re-prepare each time a new value is bound to variable pVar.
113950 ** same as that currently bound to variable pVar, non-zero is returned.
113963 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
113965 iVar = pVar->iColumn;
113966 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
113967 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
113970 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
113985 ** other than the top-level COLLATE operator.
113987 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
113999 ** this routine is used, it does not hurt to get an extra 2 - that
114004 ** pParse->pReprepare can be matched against literals in pB. The
114005 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
114020 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
114023 combinedFlags = pA->flags | pB->flags;
114025 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
114030 if( pA->op!=pB->op || pA->op==TK_RAISE ){
114031 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
114034 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
114037 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
114038 && pB->iTable<0 && pA->iTable==iTab
114047 if( pA->u.zToken ){
114048 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
114049 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
114051 assert( pA->op==pB->op );
114056 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
114061 }else if( pA->op==TK_NULL ){
114063 }else if( pA->op==TK_COLLATE ){
114064 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
114066 if( pB->u.zToken!=0
114067 && pA->op!=TK_COLUMN
114068 && pA->op!=TK_AGG_COLUMN
114069 && strcmp(pA->u.zToken,pB->u.zToken)!=0
114074 if( (pA->flags & (EP_Distinct|EP_Commuted))
114075 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
114079 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
114080 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
114081 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
114082 if( pA->op!=TK_STRING
114083 && pA->op!=TK_TRUEFALSE
114086 if( pA->iColumn!=pB->iColumn ) return 2;
114087 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
114088 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
114101 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
114104 ** This routine might return non-zero for equivalent ExprLists. The
114110 ** always differs from a non-NULL pointer.
114116 if( pA->nExpr!=pB->nExpr ) return 1;
114117 for(i=0; i<pA->nExpr; i++){
114119 Expr *pExprA = pA->a[i].pExpr;
114120 Expr *pExprB = pB->a[i].pExpr;
114121 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
114128 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
114139 ** Return non-zero if Expr p can only be true if pNN is not NULL.
114141 ** Or if seenNot is true, return non-zero if Expr p can only be
114142 ** non-NULL if pNN is not NULL
114149 int seenNot /* Return true only if p can be any non-NULL value */
114154 return pNN->op!=TK_NULL;
114156 switch( p->op ){
114159 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
114160 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
114165 pList = p->x.pList;
114167 assert( pList->nExpr==2 );
114169 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
114170 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
114174 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
114194 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
114201 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
114205 if( p->op2!=TK_IS ) return 0;
114206 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
114210 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
114233 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
114250 if( pE2->op==TK_OR
114251 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
114252 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
114256 if( pE2->op==TK_NOTNULL
114257 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
114265 ** set pWalker->eCode to one only if *both* of the input expressions
114266 ** separately have the implies-not-null-row property.
114269 if( pWalker->eCode==0 ){
114271 if( pWalker->eCode ){
114272 pWalker->eCode = 0;
114280 ** If the expression node requires that the table at pWalker->iCur
114281 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
114283 ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
114288 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
114289 ** (never setting pWalker->eCode) is a harmless missed optimization.
114292 testcase( pExpr->op==TK_AGG_COLUMN );
114293 testcase( pExpr->op==TK_AGG_FUNCTION );
114295 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){
114296 /* If iCur is used in an inner-join ON clause to the left of a
114297 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
114299 ** To keep things simple, any use of iCur from any inner-join is
114303 switch( pExpr->op ){
114312 testcase( pExpr->op==TK_ISNOT );
114313 testcase( pExpr->op==TK_ISNULL );
114314 testcase( pExpr->op==TK_NOTNULL );
114315 testcase( pExpr->op==TK_IS );
114316 testcase( pExpr->op==TK_VECTOR );
114317 testcase( pExpr->op==TK_FUNCTION );
114318 testcase( pExpr->op==TK_TRUTH );
114319 testcase( pExpr->op==TK_CASE );
114323 if( pWalker->u.iCur==pExpr->iTable ){
114324 pWalker->eCode = 1;
114331 /* Both sides of an AND or OR must separately imply non-null-row.
114335 ** If only one of x or y is non-null-row, then the overall expression
114338 testcase( pExpr->op==TK_OR );
114339 testcase( pExpr->op==TK_AND );
114340 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
114346 ** the left-hand side of the IN is NULL then the IN itself will be
114348 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){
114349 sqlite3WalkExpr(pWalker, pExpr->pLeft);
114354 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
114355 ** both y and z must be non-null row */
114357 assert( pExpr->x.pList->nExpr==2 );
114358 sqlite3WalkExpr(pWalker, pExpr->pLeft);
114359 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
114360 pExpr->x.pList->a[1].pExpr);
114372 Expr *pLeft = pExpr->pLeft;
114373 Expr *pRight = pExpr->pRight;
114374 testcase( pExpr->op==TK_EQ );
114375 testcase( pExpr->op==TK_NE );
114376 testcase( pExpr->op==TK_LT );
114377 testcase( pExpr->op==TK_LE );
114378 testcase( pExpr->op==TK_GT );
114379 testcase( pExpr->op==TK_GE );
114382 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
114383 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
114384 if( (pLeft->op==TK_COLUMN
114385 && ALWAYS(pLeft->y.pTab!=0)
114386 && IsVirtual(pLeft->y.pTab))
114387 || (pRight->op==TK_COLUMN
114388 && ALWAYS(pRight->y.pTab!=0)
114389 && IsVirtual(pRight->y.pTab))
114401 ** Return true (non-zero) if expression p can only be true if at least
114402 ** one column of table iTab is non-null. In other words, return true
114419 ** be non-NULL, then the LEFT JOIN can be safely converted into an
114426 if( p->op==TK_NOTNULL ){
114427 p = p->pLeft;
114429 while( p->op==TK_AND ){
114430 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
114431 p = p->pRight;
114458 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
114459 ** pWalker->u.pIdxCover->pIdx.
114462 if( pExpr->op==TK_COLUMN
114463 && pExpr->iTable==pWalker->u.pIdxCover->iCur
114464 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
114466 pWalker->eCode = 1;
114518 struct RefSrcList *p = pWalker->u.pRefSrcList;
114519 SrcList *pSrc = pSelect->pSrc;
114522 if( pSrc->nSrc==0 ) return WRC_Continue;
114523 j = p->nExclude;
114524 p->nExclude += pSrc->nSrc;
114525 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
114527 p->nExclude = 0;
114530 p->aiExclude = piNew;
114532 for(i=0; i<pSrc->nSrc; i++, j++){
114533 p->aiExclude[j] = pSrc->a[i].iCursor;
114538 struct RefSrcList *p = pWalker->u.pRefSrcList;
114539 SrcList *pSrc = pSelect->pSrc;
114540 if( p->nExclude ){
114541 assert( p->nExclude>=pSrc->nSrc );
114542 p->nExclude -= pSrc->nSrc;
114548 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
114551 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
114555 if( pExpr->op==TK_COLUMN
114556 || pExpr->op==TK_AGG_COLUMN
114559 struct RefSrcList *p = pWalker->u.pRefSrcList;
114560 SrcList *pSrc = p->pRef;
114561 int nSrc = pSrc ? pSrc->nSrc : 0;
114563 if( pExpr->iTable==pSrc->a[i].iCursor ){
114564 pWalker->eCode |= 1;
114568 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
114569 if( i>=p->nExclude ){
114570 pWalker->eCode |= 2;
114585 ** -1 pExpr only references no tables at all, or it only
114594 assert( pParse->db!=0 );
114601 x.db = pParse->db;
114603 assert( pExpr->op==TK_AGG_FUNCTION );
114605 sqlite3WalkExprList(&w, pExpr->x.pList);
114606 if( pExpr->pLeft ){
114607 assert( pExpr->pLeft->op==TK_ORDER );
114608 assert( ExprUseXList(pExpr->pLeft) );
114609 assert( pExpr->pLeft->x.pList!=0 );
114610 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
114614 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
114617 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
114623 return -1;
114640 && pExpr->pAggInfo!=0
114642 AggInfo *pAggInfo = pExpr->pAggInfo;
114643 int iAgg = pExpr->iAgg;
114644 Parse *pParse = pWalker->pParse;
114645 sqlite3 *db = pParse->db;
114647 if( pExpr->op!=TK_AGG_FUNCTION ){
114648 if( iAgg<pAggInfo->nColumn
114649 && pAggInfo->aCol[iAgg].pCExpr==pExpr
114653 pAggInfo->aCol[iAgg].pCExpr = pExpr;
114658 assert( pExpr->op==TK_AGG_FUNCTION );
114659 if( ALWAYS(iAgg<pAggInfo->nFunc)
114660 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
114664 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
114679 pWalker->pParse = pParse;
114680 pWalker->xExprCallback = agginfoPersistExprCb;
114681 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
114685 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
114690 pInfo->aCol = sqlite3ArrayAllocate(
114692 pInfo->aCol,
114693 sizeof(pInfo->aCol[0]),
114694 &pInfo->nColumn,
114701 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
114706 pInfo->aFunc = sqlite3ArrayAllocate(
114708 pInfo->aFunc,
114709 sizeof(pInfo->aFunc[0]),
114710 &pInfo->nFunc,
114720 ** If no prior entry is found, create a new one and return -1. The
114721 ** new column will have an index of pAggInfo->nColumn-1.
114731 assert( pAggInfo->iFirstReg==0 );
114732 pCol = pAggInfo->aCol;
114733 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
114734 if( pCol->pCExpr==pExpr ) return;
114735 if( pCol->iTable==pExpr->iTable
114736 && pCol->iColumn==pExpr->iColumn
114737 && pExpr->op!=TK_IF_NULL_ROW
114742 k = addAggInfoColumn(pParse->db, pAggInfo);
114745 assert( pParse->db->mallocFailed );
114748 pCol = &pAggInfo->aCol[k];
114750 pCol->pTab = pExpr->y.pTab;
114751 pCol->iTable = pExpr->iTable;
114752 pCol->iColumn = pExpr->iColumn;
114753 pCol->iSorterColumn = -1;
114754 pCol->pCExpr = pExpr;
114755 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
114757 ExprList *pGB = pAggInfo->pGroupBy;
114758 struct ExprList_item *pTerm = pGB->a;
114759 n = pGB->nExpr;
114761 Expr *pE = pTerm->pExpr;
114762 if( pE->op==TK_COLUMN
114763 && pE->iTable==pExpr->iTable
114764 && pE->iColumn==pExpr->iColumn
114766 pCol->iSorterColumn = j;
114771 if( pCol->iSorterColumn<0 ){
114772 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
114776 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
114777 pExpr->pAggInfo = pAggInfo;
114778 if( pExpr->op==TK_COLUMN ){
114779 pExpr->op = TK_AGG_COLUMN;
114781 pExpr->iAgg = (i16)k;
114791 NameContext *pNC = pWalker->u.pNC;
114792 Parse *pParse = pNC->pParse;
114793 SrcList *pSrcList = pNC->pSrcList;
114794 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
114796 assert( pNC->ncFlags & NC_UAggInfo );
114797 assert( pAggInfo->iFirstReg==0 );
114798 switch( pExpr->op ){
114802 assert( pParse->iSelfTab==0 );
114803 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
114804 if( pParse->pIdxEpr==0 ) break;
114805 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
114806 int iDataCur = pIEpr->iDataCur;
114808 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
114812 for(i=0; i<pSrcList->nSrc; i++){
114813 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
114815 if( i>=pSrcList->nSrc ) break;
114816 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
114817 if( pParse->nErr ){ return WRC_Abort; }
114825 tmp.iTable = pIEpr->iIdxCur;
114826 tmp.iColumn = pIEpr->iIdxCol;
114828 if( pParse->nErr ){ return WRC_Abort; }
114829 assert( pAggInfo->aCol!=0 );
114830 assert( tmp.iAgg<pAggInfo->nColumn );
114831 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
114832 pExpr->pAggInfo = pAggInfo;
114833 pExpr->iAgg = tmp.iAgg;
114839 testcase( pExpr->op==TK_AGG_COLUMN );
114840 testcase( pExpr->op==TK_COLUMN );
114841 testcase( pExpr->op==TK_IF_NULL_ROW );
114845 SrcItem *pItem = pSrcList->a;
114846 for(i=0; i<pSrcList->nSrc; i++, pItem++){
114848 if( pExpr->iTable==pItem->iCursor ){
114851 } /* endif pExpr->iTable==pItem->iCursor */
114857 if( (pNC->ncFlags & NC_InAggFunc)==0
114858 && pWalker->walkerDepth==pExpr->op2
114863 struct AggInfo_func *pItem = pAggInfo->aFunc;
114864 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
114865 if( pItem->pFExpr==pExpr ) break;
114866 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
114870 if( i>=pAggInfo->nFunc ){
114871 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
114873 u8 enc = ENC(pParse->db);
114874 i = addAggInfoFunc(pParse->db, pAggInfo);
114878 pItem = &pAggInfo->aFunc[i];
114879 pItem->pFExpr = pExpr;
114881 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
114882 pItem->pFunc = sqlite3FindFunction(pParse->db,
114883 pExpr->u.zToken, nArg, enc, 0);
114884 assert( pItem->bOBUnique==0 );
114885 if( pExpr->pLeft
114886 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0
114892 assert( pExpr->pLeft->op==TK_ORDER );
114893 assert( ExprUseXList(pExpr->pLeft) );
114894 pItem->iOBTab = pParse->nTab++;
114895 pOBList = pExpr->pLeft->x.pList;
114896 assert( pOBList->nExpr>0 );
114897 assert( pItem->bOBUnique==0 );
114898 if( pOBList->nExpr==1
114900 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
114901 pExpr->x.pList->a[0].pExpr,0)==0
114903 pItem->bOBPayload = 0;
114904 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct);
114906 pItem->bOBPayload = 1;
114909 pItem->iOBTab = -1;
114911 if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){
114912 pItem->iDistinct = pParse->nTab++;
114914 pItem->iDistinct = -1;
114918 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
114922 pExpr->iAgg = (i16)i;
114923 pExpr->pAggInfo = pAggInfo;
114935 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
114950 assert( pNC->pSrcList!=0 );
114964 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
114965 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
114974 if( pParse->nTempReg==0 ){
114975 return ++pParse->nMem;
114977 return pParse->aTempReg[--pParse->nTempReg];
114987 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
114988 pParse->aTempReg[pParse->nTempReg++] = iReg;
114999 i = pParse->iRangeReg;
115000 n = pParse->nRangeReg;
115002 pParse->iRangeReg += nReg;
115003 pParse->nRangeReg -= nReg;
115005 i = pParse->nMem+1;
115006 pParse->nMem += nReg;
115016 if( nReg>pParse->nRangeReg ){
115017 pParse->nRangeReg = nReg;
115018 pParse->iRangeReg = iReg;
115025 ** Always invoke this procedure after coding a subroutine or co-routine
115027 ** the sub/co-routine does not use registers in common with the code that
115028 ** invokes the sub/co-routine.
115031 pParse->nTempReg = 0;
115032 pParse->nRangeReg = 0;
115040 if( pParse->nMem<iReg ) pParse->nMem = iReg;
115051 const ExprList *pList = pParse->pConstExpr;
115054 for(i=0; i<pList->nExpr; i++){
115055 if( pList->a[i].u.iConstExprReg>=iMin ){
115056 iMin = pList->a[i].u.iConstExprReg + 1;
115060 pParse->nTempReg = 0;
115061 pParse->nRangeReg = 0;
115074 if( pParse->nRangeReg>0
115075 && pParse->iRangeReg+pParse->nRangeReg > iFirst
115076 && pParse->iRangeReg <= iLast
115080 for(i=0; i<pParse->nTempReg; i++){
115081 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
115085 if( pParse->pConstExpr ){
115086 ExprList *pList = pParse->pConstExpr;
115087 for(i=0; i<pList->nExpr; i++){
115088 int iReg = pList->a[i].u.iConstExprReg;
115125 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
115130 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
115132 || (pTab->tabFlags & TF_Eponymous)!=0
115133 || ( (pTab->tabFlags & TF_Shadow)!=0
115134 && sqlite3ReadOnlyShadowTables(pParse->db)
115138 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
115158 pParse->colNamesSet = 1;
115182 ** Generate VM code to replace any double-quoted strings (but not double-quoted
115184 ** database zDb with their single-quoted equivalents. If argument bTemp is
115210 Vdbe *v = pParse->pVdbe;
115213 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
115214 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
115230 char *zName = 0; /* NULL-terminated version of pName */
115231 sqlite3 *db = pParse->db; /* Database connection */
115232 int nTabName; /* Number of UTF-8 characters in zTabName */
115235 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
115237 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
115238 assert( pSrc->nSrc==1 );
115239 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
115241 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
115243 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
115244 zDb = db->aDb[iDb].zDbSName;
115274 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
115281 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
115292 if( pVTab->pVtab->pModule->xRename==0 ){
115308 /* figure out how many UTF-8 characters are in zName */
115309 zTabName = pTab->zName;
115310 nTabName = sqlite3Utf8CharLen(zTabName, -1);
115347 zDb, zName, pTab->zName);
115368 ** of any resources used by the v-table implementation (including other
115373 int i = ++pParse->nMem;
115408 ** The Table structure pParse->pNewTable was extended to include
115412 Table *pNew; /* Copy of pParse->pNewTable */
115417 char *zCol; /* Null-terminated column definition */
115424 db = pParse->db;
115425 assert( db->pParse==pParse );
115426 if( pParse->nErr ) return;
115427 assert( db->mallocFailed==0 );
115428 pNew = pParse->pNewTable;
115432 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
115433 zDb = db->aDb[iDb].zDbSName;
115434 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
115435 pCol = &pNew->aCol[pNew->nCol-1];
115442 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
115452 if( pCol->colFlags & COLFLAG_PRIMKEY ){
115456 if( pNew->pIndex ){
115461 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
115466 assert( pDflt==0 || pDflt->op==TK_SPAN );
115467 if( pDflt && pDflt->pLeft->op==TK_NULL ){
115471 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
115473 "Cannot add a REFERENCES column with non-NULL default value");
115475 if( pCol->notNull && !pDflt ){
115490 assert( db->mallocFailed == 1 );
115495 "Cannot add a column with non-constant default");
115499 }else if( pCol->colFlags & COLFLAG_STORED ){
115505 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
115507 char *zEnd = &zCol[pColDef->n-1];
115509 *zEnd-- = '\0';
115520 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
115535 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
115545 if( pNew->pCheck!=0
115546 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
115547 || (pTab->tabFlags & TF_Strict)!=0
115552 " WHEN quick_check GLOB 'non-* value in*'"
115559 " OR quick_check GLOB 'non-* value in*'",
115567 ** This function is called by the parser after the table-name in
115568 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
115569 ** pSrc is the full-name of the table being altered.
115587 sqlite3 *db = pParse->db;
115590 assert( pParse->pNewTable==0 );
115592 if( db->mallocFailed ) goto exit_begin_add_column;
115593 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
115614 assert( pTab->u.tab.addColOffset>0 );
115615 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115626 pParse->pNewTable = pNew;
115627 pNew->nTabRef = 1;
115628 pNew->nCol = pTab->nCol;
115629 assert( pNew->nCol>0 );
115630 nAlloc = (((pNew->nCol-1)/8)*8)+8;
115631 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
115632 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
115633 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
115634 if( !pNew->aCol || !pNew->zName ){
115635 assert( db->mallocFailed );
115638 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
115639 for(i=0; i<pNew->nCol; i++){
115640 Column *pCol = &pNew->aCol[i];
115641 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
115642 pCol->hName = sqlite3StrIHash(pCol->zCnName);
115645 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
115646 pNew->pSchema = db->aDb[iDb].pSchema;
115647 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
115648 assert( pNew->nTabRef==1 );
115659 ** it loads an error message into pParse and returns non-zero.
115679 zType, pTab->zName
115696 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
115700 sqlite3 *db = pParse->db; /* Database connection */
115710 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
115718 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
115720 zDb = db->aDb[iSchema].zDbSName;
115724 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
115733 for(iCol=0; iCol<pTab->nCol; iCol++){
115734 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
115736 if( iCol==pTab->nCol ){
115741 /* Ensure the schema contains no double-quoted strings */
115752 assert( pNew->n>0 );
115753 bQuote = sqlite3Isquote(pNew->z[0]);
115760 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
115761 pTab->zName
115768 zDb, pTab->zName, iCol, zNew, bQuote
115823 ** rename-token list.
115825 ** 2. Dereferences each pointer in the rename-token list.
115828 ** address-sanitizer or similar. If any of these pointers no longer
115829 ** point to valid objects, an exception is raised by the memory-checking
115843 assert( pParse==pParse->db->pParse );
115844 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
115845 if( pParse->nErr==0 ){
115848 for(p=pParse->pRename; p; p=p->pNext){
115849 if( p->p ){
115850 assert( p->p!=pPtr );
115851 i += *(u8*)(p->p) | 1;
115867 ** in pParse->pRename.
115879 assert( pPtr || pParse->db->mallocFailed );
115881 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
115882 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
115884 pNew->p = pPtr;
115885 pNew->t = *pToken;
115886 pNew->pNext = pParse->pRename;
115887 pParse->pRename = pNew;
115902 for(p=pParse->pRename; p; p=p->pNext){
115903 if( p->p==pFrom ){
115904 p->p = pTo;
115914 Parse *pParse = pWalker->pParse;
115917 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
115927 With *pWith = pSelect->pWith;
115929 Parse *pParse = pWalker->pParse;
115932 assert( pWith->nCte>0 );
115933 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
115934 /* Push a copy of the With object onto the with-stack. We use a copy
115936 ** and SF_Resolved) below. And the parser code that uses the with-stack
115939 pCopy = sqlite3WithDup(pParse->db, pWith);
115942 for(i=0; i<pWith->nCte; i++){
115943 Select *p = pWith->a[i].pSelect;
115948 if( sNC.pParse->db->mallocFailed ) return;
115950 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
115952 if( pCopy && pParse->pWith==pCopy ){
115953 pParse->pWith = pCopy->pOuter;
115967 for(ii=0; ii<pIdList->nId; ii++){
115968 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
115976 Parse *pParse = pWalker->pParse;
115978 if( pParse->nErr ) return WRC_Abort;
115979 testcase( p->selFlags & SF_View );
115980 testcase( p->selFlags & SF_CopyCte );
115981 if( p->selFlags & (SF_View|SF_CopyCte) ){
115984 if( ALWAYS(p->pEList) ){
115985 ExprList *pList = p->pEList;
115986 for(i=0; i<pList->nExpr; i++){
115987 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
115988 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
115992 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
115993 SrcList *pSrc = p->pSrc;
115994 for(i=0; i<pSrc->nSrc; i++){
115995 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
115996 if( pSrc->a[i].fg.isUsing==0 ){
115997 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
115999 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
116012 u8 eMode = pParse->eParseMode;
116018 pParse->eParseMode = PARSE_MODE_UNMAP;
116020 pParse->eParseMode = eMode;
116024 ** Remove all nodes that are part of expression-list pEList from the
116035 for(i=0; i<pEList->nExpr; i++){
116036 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
116037 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
116050 pNext = p->pNext;
116073 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
116074 if( (*pp)->p==pPtr ){
116077 *pp = pToken->pNext;
116078 pToken->pNext = pCtx->pList;
116079 pCtx->pList = pToken;
116080 pCtx->nList++;
116091 ** descend into sub-select statements.
116094 if( p->selFlags & (SF_View|SF_CopyCte) ){
116095 testcase( p->selFlags & SF_View );
116096 testcase( p->selFlags & SF_CopyCte );
116110 ** constructed in RenameCtx object at pWalker->u.pRename.
116113 RenameCtx *p = pWalker->u.pRename;
116114 if( pExpr->op==TK_TRIGGER
116115 && pExpr->iColumn==p->iCol
116116 && pWalker->pParse->pTriggerTab==p->pTab
116118 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
116119 }else if( pExpr->op==TK_COLUMN
116120 && pExpr->iColumn==p->iCol
116122 && p->pTab==pExpr->y.pTab
116124 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
116139 RenameToken *pBest = pCtx->pList;
116143 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
116144 if( pToken->t.z>pBest->t.z ) pBest = pToken;
116146 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
116147 *pp = pBest->pNext;
116154 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
116156 ** sub-routine is currently stored in pParse->zErrMsg. This function
116170 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
116172 pParse->zErrMsg
116174 sqlite3_result_error(pCtx, zErr, -1);
116175 sqlite3DbFree(pParse->db, zErr);
116179 ** For each name in the the expression-list pEList (i.e. each
116180 ** pEList->a[i].zName) that matches the string in zOld, extract the
116181 ** corresponding rename-token from Parse object pParse and add it
116192 for(i=0; i<pEList->nExpr; i++){
116193 const char *zName = pEList->a[i].zEName;
116194 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
116205 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
116206 ** that matches the string in zOld, extract the corresponding rename-token
116217 for(i=0; i<pIdList->nId; i++){
116218 const char *zName = pIdList->a[i].zName;
116247 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
116248 p->eParseMode = PARSE_MODE_RENAME;
116249 p->db = db;
116250 p->nQueryLoop = 1;
116252 if( db->mallocFailed ) rc = SQLITE_NOMEM;
116254 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
116265 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
116266 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
116271 db->init.iDb = 0;
116311 nQuot = sqlite3Strlen30(zQuot)-1;
116315 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
116324 /* At this point pRename->pList contains a list of RenameToken objects
116326 ** with the new column name, or with single-quoted versions of themselves.
116331 while( pRename->pList ){
116338 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
116344 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
116347 /* Dequote the double-quoted token. Then requote it again, this time
116350 ** add another space after the new, single-quoted version of the
116353 memcpy(zBuf1, pBest->t.z, pBest->t.n);
116354 zBuf1[pBest->t.n] = 0;
116357 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
116363 iOff = pBest->t.z - zSql;
116364 if( pBest->t.n!=nReplace ){
116365 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
116366 nOut - (iOff + pBest->t.n)
116368 nOut += nReplace - pBest->t.n;
116375 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
116386 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
116391 for(i=0; i<pEList->nExpr; i++){
116392 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
116393 pEList->a[i].fg.eEName = val;
116399 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
116405 sqlite3 *db = pParse->db;
116406 Trigger *pNew = pParse->pNewTrigger;
116413 assert( pNew->pTabSchema );
116414 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
116415 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
116417 pParse->eTriggerOp = pNew->op;
116420 if( ALWAYS(pParse->pTriggerTab) ){
116421 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
116425 if( rc==SQLITE_OK && pNew->pWhen ){
116426 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
116429 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
116430 if( pStep->pSelect ){
116431 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
116432 if( pParse->nErr ) rc = pParse->rc;
116434 if( rc==SQLITE_OK && pStep->zTarget ){
116438 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
116441 pStep->pExprList = 0;
116445 /* pStep->pExprList contains an expression-list used for an UPDATE
116449 ** pStep->pExprList to ENAME_SPAN (from their current value of
116453 renameSetENames(pStep->pExprList, ENAME_SPAN);
116455 renameSetENames(pStep->pExprList, ENAME_NAME);
116456 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
116457 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
116458 assert( pSrc==pSel->pSrc );
116459 if( pStep->pExprList ) pSel->pEList = 0;
116460 pSel->pSrc = 0;
116463 if( pStep->pFrom ){
116465 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
116466 SrcItem *p = &pStep->pFrom->a[i];
116467 if( p->pSelect ){
116468 sqlite3SelectPrep(pParse, p->pSelect, 0);
116473 if( db->mallocFailed ){
116477 if( rc==SQLITE_OK && pStep->pWhere ){
116478 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
116481 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
116483 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
116484 if( pStep->pUpsert && rc==SQLITE_OK ){
116485 Upsert *pUpsert = pStep->pUpsert;
116486 pUpsert->pUpsertSrc = pSrc;
116489 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
116491 ExprList *pUpsertSet = pUpsert->pUpsertSet;
116495 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
116498 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
116520 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
116523 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
116524 sqlite3WalkSelect(pWalker, pStep->pSelect);
116525 sqlite3WalkExpr(pWalker, pStep->pWhere);
116526 sqlite3WalkExprList(pWalker, pStep->pExprList);
116527 if( pStep->pUpsert ){
116528 Upsert *pUpsert = pStep->pUpsert;
116529 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
116530 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
116531 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
116532 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
116534 if( pStep->pFrom ){
116536 for(i=0; i<pStep->pFrom->nSrc; i++){
116537 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
116548 sqlite3 *db = pParse->db;
116550 if( pParse->pVdbe ){
116551 sqlite3VdbeFinalize(pParse->pVdbe);
116553 sqlite3DeleteTable(db, pParse->pNewTable);
116554 while( (pIdx = pParse->pNewIndex)!=0 ){
116555 pParse->pNewIndex = pIdx->pNext;
116558 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
116559 sqlite3DbFree(db, pParse->zErrMsg);
116560 renameTokenFree(db, pParse->pRename);
116576 ** 7. bQuote: Non-zero if the new column name should be quoted.
116580 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
116610 sqlite3_xauth xAuth = db->xAuth;
116620 if( pTab==0 || iCol>=pTab->nCol ){
116624 zOld = pTab->aCol[iCol].zCnName;
116626 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
116629 db->xAuth = 0;
116644 Select *pSelect = sParse.pNewTable->u.view.pSelect;
116645 pSelect->selFlags &= ~SF_View;
116648 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
116655 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
116659 if( iCol<sParse.pNewTable->nCol ){
116661 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
116665 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
116667 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
116668 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
116669 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
116671 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
116672 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
116675 for(i=0; i<sParse.pNewTable->nCol; i++){
116677 &sParse.pNewTable->aCol[i]);
116684 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
116685 for(i=0; i<pFKey->nCol; i++){
116686 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
116687 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
116689 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
116690 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
116692 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
116698 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
116699 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
116706 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
116707 if( pStep->zTarget ){
116708 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
116710 if( pStep->pUpsert ){
116711 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
116714 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
116715 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
116723 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
116747 db->xAuth = xAuth;
116756 RenameCtx *p = pWalker->u.pRename;
116757 if( pExpr->op==TK_COLUMN
116759 && p->pTab==pExpr->y.pTab
116761 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
116771 RenameCtx *p = pWalker->u.pRename;
116772 SrcList *pSrc = pSelect->pSrc;
116773 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
116774 testcase( pSelect->selFlags & SF_View );
116775 testcase( pSelect->selFlags & SF_CopyCte );
116779 assert( pWalker->pParse->db->mallocFailed );
116782 for(i=0; i<pSrc->nSrc; i++){
116783 SrcItem *pItem = &pSrc->a[i];
116784 if( pItem->pTab==p->pTab ){
116785 renameTokenFind(pWalker->pParse, p, pItem->zName);
116811 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
116834 sqlite3_xauth xAuth = db->xAuth;
116835 db->xAuth = 0;
116851 int isLegacy = (db->flags & SQLITE_LegacyAlter);
116857 Select *pSelect = pTab->u.view.pSelect;
116862 assert( pSelect->selFlags & SF_View );
116863 pSelect->selFlags &= ~SF_View;
116864 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
116868 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
116874 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
116879 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
116880 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
116881 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
116890 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
116893 sqlite3WalkExprList(&sWalker, pTab->pCheck);
116895 renameTokenFind(&sParse, &sCtx, pTab->zName);
116901 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
116903 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
116911 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
116912 && sCtx.pTab->pSchema==pTrigger->pTabSchema
116914 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
116921 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
116922 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
116923 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
116925 if( pStep->pFrom ){
116927 for(i=0; i<pStep->pFrom->nSrc; i++){
116928 SrcItem *pItem = &pStep->pFrom->a[i];
116929 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
116930 renameTokenFind(&sParse, &sCtx, pItem->zName);
116958 db->xAuth = xAuth;
116966 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
116967 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
116975 ** double-quotes use single quotes instead.
117009 sqlite3_xauth xAuth = db->xAuth;
117010 db->xAuth = 0;
117035 Select *pSelect = sParse.pNewTable->u.view.pSelect;
117036 pSelect->selFlags &= ~SF_View;
117039 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
117045 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
117047 for(i=0; i<sParse.pNewTable->nCol; i++){
117050 &sParse.pNewTable->aCol[i]));
117055 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
117056 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
117082 db->xAuth = xAuth;
117121 int isLegacy = (db->flags & SQLITE_LegacyAlter);
117126 sqlite3_xauth xAuth = db->xAuth;
117127 db->xAuth = 0;
117135 int flags = db->flags;
117136 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
117138 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
117144 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
117153 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
117171 db->xAuth = xAuth;
117180 ** argv[0]: An integer - the index of the schema containing the table
117182 ** argv[2]: An integer - the index of the column to remove.
117196 const char *zDb = db->aDb[iSchema].zDbSName;
117205 sqlite3_xauth xAuth = db->xAuth;
117206 db->xAuth = 0;
117213 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
117219 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
117220 if( iCol<pTab->nCol-1 ){
117222 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
117223 zEnd = (const char*)pEnd->t.z;
117226 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
117227 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
117230 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
117231 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
117237 db->xAuth = xAuth;
117253 sqlite3 *db = pParse->db; /* Database handle */
117258 int iCol; /* Index of column zCol in pTab->aCol[] */
117261 assert( pParse->pNewTable==0 );
117263 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
117264 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
117275 assert( db->mallocFailed );
117286 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
117288 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
117295 if( pTab->nCol<=1 ){
117301 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117303 zDb = db->aDb[iDb].zDbSName;
117306 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
117316 , zDb, iDb, iCol, pTab->zName
117324 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
117330 int nField = 0; /* Number of non-virtual columns after drop */
117333 iCur = pParse->nTab++;
117336 reg = ++pParse->nMem;
117339 pParse->nMem += pTab->nCol;
117342 pParse->nMem += pPk->nColumn;
117343 for(i=0; i<pPk->nKeyCol; i++){
117346 nField = pPk->nKeyCol;
117348 regRec = ++pParse->nMem;
117349 for(i=0; i<pTab->nCol; i++){
117350 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
117355 if( iPos<pPk->nKeyCol ) continue;
117356 regOut = reg+1+iPos-(iPos>iColPos);
117360 if( i==pTab->iPKey ){
117370 pParse->nMem++;
117376 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
117392 ** Register built-in functions used to help implement ALTER TABLE
117409 ** 2005-07-08
117459 ** columns. The N-th integer (for N>1) is the average number of rows in
117460 ** the index which have the same value for the first N-1 columns. For
117461 ** a K-column index, there will be K+1 integers in the stat column. If
117485 ** inclusive are samples of the left-most key value in the index taken at
117492 ** For i between 0 and S-1. Conceptually, the index space is divided into
117519 ** of entries in the index whose left-most column exactly matches
117520 ** the left-most column of the sample. The second integer in nEq
117526 ** left-most column is less than the left-most column of the sample.
117527 ** The K-th integer in the nLt entry is the number of index entries
117542 ** looks at the left-most column of the index. The sqlite_stat3.sample
117543 ** column contains the actual value of the left-most column instead
117564 ** appropriate compile-time options are provided.
117593 sqlite3 *db = pParse->db;
117607 pDb = &db->aDb[iDb];
117616 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
117619 ** side-effect of the CREATE TABLE statement is to leave the rootpage
117620 ** of the new table in register pParse->regRoot. This is important
117623 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
117625 aRoot[i] = (u32)pParse->regRoot;
117632 aRoot[i] = pStat->tnum;
117637 pDb->zDbSName, zTab, zWhereType, zWhere
117640 }else if( db->xPreUpdateCallback ){
117641 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
117667 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
117692 int nLimit; /* Analysis row-scan limit */
117695 u8 nSkipAhead; /* Number of times of skip-ahead */
117700 u32 iPrn; /* Pseudo-random number used for sampling */
117715 if( p->nRowid ){
117716 sqlite3DbFree(db, p->u.aRowid);
117717 p->nRowid = 0;
117727 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
117728 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
117729 if( p->u.aRowid ){
117730 p->nRowid = n;
117731 memcpy(p->u.aRowid, pData, n);
117733 p->nRowid = 0;
117743 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
117744 p->nRowid = 0;
117745 p->u.iRowid = iRowid;
117755 pTo->isPSample = pFrom->isPSample;
117756 pTo->iCol = pFrom->iCol;
117757 pTo->iHash = pFrom->iHash;
117758 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
117759 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
117760 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
117761 if( pFrom->nRowid ){
117762 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
117764 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
117775 if( p->mxSample ){
117777 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
117778 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
117779 sampleClear(p->db, &p->current);
117782 sqlite3DbFree(p->db, p);
117791 ** L: A limit on the number of rows to scan, or 0 for no-limit
117849 p->db = db;
117850 p->nEst = sqlite3_value_int64(argv[2]);
117851 p->nRow = 0;
117852 p->nLimit = sqlite3_value_int64(argv[3]);
117853 p->nCol = nCol;
117854 p->nKeyCol = nKeyCol;
117855 p->nSkipAhead = 0;
117856 p->current.anDLt = (tRowcnt*)&p[1];
117857 p->current.anEq = &p->current.anDLt[nColUp];
117860 p->mxSample = p->nLimit==0 ? mxSample : 0;
117863 int i; /* Used to iterate through p->aSample[] */
117865 p->iGet = -1;
117866 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
117867 p->current.anLt = &p->current.anEq[nColUp];
117868 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
117871 p->a = (struct StatSample*)&p->current.anLt[nColUp];
117872 p->aBest = &p->a[mxSample];
117873 pSpace = (u8*)(&p->a[mxSample+nCol]);
117875 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
117876 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
117877 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
117879 assert( (pSpace - (u8*)p)==n );
117882 p->aBest[i].iCol = i;
117907 ** pNew and pOld are both candidate non-periodic samples selected for
117908 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
117915 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
117922 int nCol = pAccum->nCol;
117924 assert( pNew->iCol==pOld->iCol );
117925 for(i=pNew->iCol+1; i<nCol; i++){
117926 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
117927 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
117929 if( pNew->iHash>pOld->iHash ) return 1;
117939 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
117946 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
117947 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
117949 assert( pOld->isPSample==0 && pNew->isPSample==0 );
117950 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
117954 if( pNew->iCol<pOld->iCol ) return 1;
117955 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
117961 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
117962 ** remove the least desirable sample from p->a[] to make room.
117974 if( nEqZero>p->nMaxEqZero ){
117975 p->nMaxEqZero = nEqZero;
117977 if( pNew->isPSample==0 ){
117979 assert( pNew->anEq[pNew->iCol]>0 );
117986 for(i=p->nSample-1; i>=0; i--){
117987 StatSample *pOld = &p->a[i];
117988 if( pOld->anEq[pNew->iCol]==0 ){
117989 if( pOld->isPSample ) return;
117990 assert( pOld->iCol>pNew->iCol );
117998 pUpgrade->iCol = pNew->iCol;
117999 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
118005 if( p->nSample>=p->mxSample ){
118006 StatSample *pMin = &p->a[p->iMin];
118007 tRowcnt *anEq = pMin->anEq;
118008 tRowcnt *anLt = pMin->anLt;
118009 tRowcnt *anDLt = pMin->anDLt;
118010 sampleClear(p->db, pMin);
118011 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
118012 pSample = &p->a[p->nSample-1];
118013 pSample->nRowid = 0;
118014 pSample->anEq = anEq;
118015 pSample->anDLt = anDLt;
118016 pSample->anLt = anLt;
118017 p->nSample = p->mxSample-1;
118020 /* The "rows less-than" for the rowid column must be greater than that
118021 ** for the last sample in the p->a[] array. Otherwise, the samples would
118023 assert( p->nSample==0
118024 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
118027 pSample = &p->a[p->nSample];
118029 p->nSample++;
118032 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
118035 if( p->nSample>=p->mxSample ){
118036 int iMin = -1;
118037 for(i=0; i<p->mxSample; i++){
118038 if( p->a[i].isPSample ) continue;
118039 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
118044 p->iMin = iMin;
118052 ** p->current contains a sample that reflects the previous row of the
118061 for(i=(p->nCol-2); i>=iChng; i--){
118062 StatSample *pBest = &p->aBest[i];
118063 pBest->anEq[i] = p->current.anEq[i];
118064 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
118070 ** p->nMaxEqZero or greater set to zero. */
118071 for(i=p->nSample-1; i>=0; i--){
118073 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
118077 if( iChng<p->nMaxEqZero ){
118078 for(i=p->nSample-1; i>=0; i--){
118080 for(j=iChng; j<p->nCol; j++){
118081 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
118084 p->nMaxEqZero = iChng;
118094 ** C Index of left-most column to differ from previous row
118104 ** if it wants the byte-code to do special processing.
118121 assert( p->nCol>0 );
118122 assert( iChng<p->nCol );
118124 if( p->nRow==0 ){
118126 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
118130 if( p->mxSample ) samplePushPrevious(p, iChng);
118136 p->current.anEq[i]++;
118138 for(i=iChng; i<p->nCol; i++){
118139 p->current.anDLt[i]++;
118141 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
118143 p->current.anEq[i] = 1;
118147 p->nRow++;
118149 if( p->mxSample ){
118152 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
118154 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
118157 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
118159 nLt = p->current.anLt[p->nCol-1];
118161 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
118162 p->current.isPSample = 1;
118163 p->current.iCol = 0;
118164 sampleInsert(p, &p->current, p->nCol-1);
118165 p->current.isPSample = 0;
118169 for(i=0; i<(p->nCol-1); i++){
118170 p->current.iCol = i;
118171 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
118172 sampleCopy(p, &p->aBest[i], &p->current);
118177 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
118178 p->nSkipAhead++;
118179 sqlite3_result_int(context, p->current.anDLt[0]>0);
118217 ** a one-parameter function, stat_get(P), that always returns the
118234 assert( eCall==STAT_GET_STAT1 || p->mxSample );
118259 ** I = (K+D-1)/D
118269 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
118271 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
118272 for(i=0; i<p->nKeyCol; i++){
118273 u64 nDistinct = p->current.anDLt[i] + 1;
118274 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
118275 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
118277 assert( p->current.anEq[i] );
118283 if( p->iGet<0 ){
118285 p->iGet = 0;
118287 if( p->iGet<p->nSample ){
118288 StatSample *pS = p->a + p->iGet;
118289 if( pS->nRowid==0 ){
118290 sqlite3_result_int64(context, pS->u.iRowid);
118292 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
118301 assert( p->iGet<p->nSample );
118303 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
118304 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
118306 aCnt = p->a[p->iGet].anDLt;
118307 p->iGet++;
118311 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
118312 for(i=0; i<p->nCol; i++){
118315 if( sStat.nChar ) sStat.nChar--;
118337 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
118350 ** of the k-th column of the pIdx index.
118358 assert( k>=0 && k<pIdx->nColumn );
118359 i = pIdx->aiColumn[k];
118361 VdbeComment((v,"%s.rowid",pIdx->zName));
118363 assert( pIdx->bHasExpr );
118364 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
118366 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
118385 sqlite3 *db = pParse->db; /* Database handle */
118391 int jZeroRows = -1; /* Jump from here if number of rows is zero */
118405 int doOnce = 1; /* Flag for a one-time computation */
118421 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
118426 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118430 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
118431 db->aDb[iDb].zDbSName ) ){
118437 if( db->xPreUpdateCallback ){
118440 pStat1->zName = (char*)&pStat1[1];
118441 memcpy(pStat1->zName, "sqlite_stat1", 13);
118442 pStat1->nCol = 3;
118443 pStat1->iPKey = -1;
118444 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
118448 /* Establish a read-lock on the table at the shared-cache level.
118449 ** Open a read-only cursor on the table. Also allocate a cursor number
118452 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
118455 pParse->nTab = MAX(pParse->nTab, iTab);
118457 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
118459 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
118467 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
118469 nCol = pIdx->nKeyCol;
118470 zIdxName = pTab->zName;
118471 nColTest = nCol - 1;
118473 nCol = pIdx->nColumn;
118474 zIdxName = pIdx->zName;
118475 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
118480 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
118483 ** Pseudo-code for loop that calls stat_push():
118520 /* Open a read-only cursor on the index being analyzed. */
118521 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
118522 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
118524 VdbeComment((v, "%s", pIdx->zName));
118535 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
118549 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
118582 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
118583 /* For a single-column UNIQUE index, once we have found a non-NULL
118590 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
118610 sqlite3VdbeJumpHere(v, addrNextRow-1);
118633 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
118635 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
118636 for(j=0; j<pPk->nKeyCol; j++){
118637 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
118638 assert( k>=0 && k<pIdx->nColumn );
118642 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
118643 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
118651 if( db->nAnalysisLimit ){
118673 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
118679 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
118695 for(pX=pTab->pIndex; pX; pX=pX->pNext){
118698 nColX = pX->nKeyCol;
118700 nColX = pX->nColumn;
118711 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
118715 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
118736 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
118750 VdbeComment((v, "%s", pTab->zName));
118760 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
118782 sqlite3 *db = pParse->db;
118783 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
118790 iStatCur = pParse->nTab;
118791 pParse->nTab += 3;
118793 iMem = pParse->nMem+1;
118794 iTab = pParse->nTab;
118796 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
118818 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
118819 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
118821 iStatCur = pParse->nTab;
118822 pParse->nTab += 3;
118824 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
118826 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
118828 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
118836 ** ANALYZE -- 1
118837 ** ANALYZE <database> -- 2
118838 ** ANALYZE ?<database>.?<tablename> -- 3
118845 sqlite3 *db = pParse->db;
118856 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
118864 for(i=0; i<db->nDb; i++){
118868 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
118875 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
118879 analyzeTable(pParse, pIdx->pTable, pIdx);
118887 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
118903 ** The first argument points to a nul-terminated string containing a
118927 v = v*10 + c - '0';
118946 pIndex->bUnordered = 0;
118947 pIndex->noSkipScan = 0;
118950 pIndex->bUnordered = 1;
118951 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
118954 pIndex->szIdxRow = sqlite3LogEst(sz);
118956 pIndex->noSkipScan = 1;
118959 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
118960 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
118975 ** argv[2] = results of analysis - on integer for each column
118992 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
119001 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
119007 int nCol = pIndex->nKeyCol+1;
119012 if( pIndex->aiRowEst==0 ){
119013 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
119014 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
119016 aiRowEst = pIndex->aiRowEst;
119018 pIndex->bUnordered = 0;
119019 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
119020 pIndex->hasStat1 = 1;
119021 if( pIndex->pPartIdxWhere==0 ){
119022 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
119023 pTable->tabFlags |= TF_HasStat1;
119027 fakeIdx.szIdxRow = pTable->szTabRow;
119031 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
119032 pTable->szTabRow = fakeIdx.szIdxRow;
119033 pTable->tabFlags |= TF_HasStat1;
119047 if( pIdx->aSample ){
119049 for(j=0; j<pIdx->nSample; j++){
119050 IndexSample *p = &pIdx->aSample[j];
119051 sqlite3DbFree(db, p->p);
119053 sqlite3DbFree(db, pIdx->aSample);
119055 if( db->pnBytesFreed==0 ){
119056 pIdx->nSample = 0;
119057 pIdx->aSample = 0;
119067 ** Populate the pIdx->aAvgEq[] array based on the samples currently
119068 ** stored in pIdx->aSample[].
119072 IndexSample *aSample = pIdx->aSample;
119073 IndexSample *pFinal = &aSample[pIdx->nSample-1];
119076 if( pIdx->nSampleCol>1 ){
119081 nCol = pIdx->nSampleCol-1;
119082 pIdx->aAvgEq[nCol] = 1;
119085 int nSample = pIdx->nSample;
119093 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
119094 nRow = pFinal->anLt[iCol];
119095 nDist100 = (i64)100 * pFinal->anDLt[iCol];
119096 nSample--;
119098 nRow = pIdx->aiRowEst[0];
119099 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
119101 pIdx->nRowEst0 = nRow;
119108 if( i==(pIdx->nSample-1)
119117 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
119120 pIdx->aAvgEq[iCol] = avgEq;
119164 IndexSample *pSample; /* A slot in pIdx->aSample[] */
119166 assert( db->lookaside.bDisable );
119171 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
119189 assert( pIdx==0 || pIdx->nSample==0 );
119191 if( pIdx->aSample!=0 ){
119195 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
119196 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
119197 nIdxCol = pIdx->nKeyCol;
119199 nIdxCol = pIdx->nColumn;
119201 pIdx->nSampleCol = nIdxCol;
119202 pIdx->mxSample = nSample;
119207 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
119208 if( pIdx->aSample==0 ){
119212 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
119213 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
119214 pIdx->pTable->tabFlags |= TF_HasStat4;
119216 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
119217 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
119218 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
119220 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
119229 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
119242 if( pIdx->nSample>=pIdx->mxSample ){
119249 nCol = pIdx->nSampleCol;
119254 pSample = &pIdx->aSample[pIdx->nSample];
119255 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
119256 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
119257 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
119266 pSample->n = sqlite3_column_bytes(pStmt, 4);
119267 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
119268 if( pSample->p==0 ){
119272 if( pSample->n ){
119273 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
119275 pIdx->nSample++;
119290 assert( db->lookaside.bDisable );
119321 ** If an OOM error occurs, this function always sets db->mallocFailed.
119330 Schema *pSchema = db->aDb[iDb].pSchema;
119333 assert( iDb>=0 && iDb<db->nDb );
119334 assert( db->aDb[iDb].pBt!=0 );
119338 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
119340 pTab->tabFlags &= ~TF_HasStat1;
119342 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
119344 pIdx->hasStat1 = 0;
119347 pIdx->aSample = 0;
119353 sInfo.zDatabase = db->aDb[iDb].zDbSName;
119369 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
119371 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
119381 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
119383 sqlite3_free(pIdx->aiRowEst);
119384 pIdx->aiRowEst = 0;
119437 if( pExpr->op!=TK_ID ){
119440 pExpr->op = TK_STRING;
119452 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
119458 ** An SQL user-function registered to do the work of an ATTACH statement. The
119468 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
119469 ** new database, close the database on db->init.iDb and reopen it as an
119497 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
119504 ** from sqlite3_deserialize() to close database db->init.iDb and
119516 pNew = &db->aDb[db->init.iDb];
119517 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
119518 pNew->pBt = pNewBt;
119519 pNew->pSchema = pNewSchema;
119535 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
119536 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
119537 db->aLimit[SQLITE_LIMIT_ATTACHED]
119541 for(i=0; i<db->nDb; i++){
119549 /* Allocate the new entry in the db->aDb[] array and initialize the schema
119552 if( db->aDb==db->aDbStatic ){
119553 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
119555 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
119557 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
119560 db->aDb = aNew;
119561 pNew = &db->aDb[db->nDb];
119568 flags = db->openFlags;
119569 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
119572 sqlite3_result_error(context, zErr, -1);
119578 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
119579 db->nDb++;
119580 pNew->zDbSName = sqlite3DbStrDup(db, zName);
119582 db->noSharedCache = 0;
119588 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
119589 if( !pNew->pSchema ){
119591 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
119596 sqlite3BtreeEnter(pNew->pBt);
119597 pPager = sqlite3BtreePager(pNew->pBt);
119598 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
119599 sqlite3BtreeSecureDelete(pNew->pBt,
119600 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
119602 sqlite3BtreeSetPagerFlags(pNew->pBt,
119603 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
119605 sqlite3BtreeLeave(pNew->pBt);
119607 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
119608 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
119615 ** remove the entry from the db->aDb[] array. i.e. put everything back the
119620 db->init.iDb = 0;
119621 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
119632 if( newAuth<db->auth.authLevel ){
119639 int iDb = db->nDb - 1;
119641 if( db->aDb[iDb].pBt ){
119642 sqlite3BtreeClose(db->aDb[iDb].pBt);
119643 db->aDb[iDb].pBt = 0;
119644 db->aDb[iDb].pSchema = 0;
119647 db->nDb = iDb;
119664 sqlite3_result_error(context, zErrDyn, -1);
119671 ** An SQL user-function registered to do the work of an DETACH statement. The
119693 for(i=0; i<db->nDb; i++){
119694 pDb = &db->aDb[i];
119695 if( pDb->pBt==0 ) continue;
119699 if( i>=db->nDb ){
119707 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
119708 || sqlite3BtreeIsInBackup(pDb->pBt)
119716 assert( db->aDb[1].pSchema );
119717 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
119720 if( pTrig->pTabSchema==pDb->pSchema ){
119721 pTrig->pTabSchema = pTrig->pSchema;
119726 sqlite3BtreeClose(pDb->pBt);
119727 pDb->pBt = 0;
119728 pDb->pSchema = 0;
119733 sqlite3_result_error(context, zErr, -1);
119752 sqlite3* db = pParse->db;
119757 if( pParse->nErr ) goto attach_end;
119772 if( pAuthArg->op==TK_STRING ){
119774 zAuthArg = pAuthArg->u.zToken;
119792 assert( v || db->mallocFailed );
119794 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
119795 pFunc->nArg, pFunc, 0);
119854 DbFixer *pFix = p->u.pFix;
119855 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
119856 if( pExpr->op==TK_VARIABLE ){
119857 if( pFix->pParse->db->init.busy ){
119858 pExpr->op = TK_NULL;
119860 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
119871 DbFixer *pFix = p->u.pFix;
119874 sqlite3 *db = pFix->pParse->db;
119875 int iDb = sqlite3FindDbName(db, pFix->zDb);
119876 SrcList *pList = pSelect->pSrc;
119879 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
119880 if( pFix->bTemp==0 ){
119881 if( pItem->zDatabase ){
119882 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
119883 sqlite3ErrorMsg(pFix->pParse,
119885 pFix->zType, pFix->pName, pItem->zDatabase);
119888 sqlite3DbFree(db, pItem->zDatabase);
119889 pItem->zDatabase = 0;
119890 pItem->fg.notCte = 1;
119892 pItem->pSchema = pFix->pSchema;
119893 pItem->fg.fromDDL = 1;
119896 if( pList->a[i].fg.isUsing==0
119897 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
119903 if( pSelect->pWith ){
119904 for(i=0; i<pSelect->pWith->nCte; i++){
119905 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
119924 sqlite3 *db = pParse->db;
119925 assert( db->nDb>iDb );
119926 pFix->pParse = pParse;
119927 pFix->zDb = db->aDb[iDb].zDbSName;
119928 pFix->pSchema = db->aDb[iDb].pSchema;
119929 pFix->zType = zType;
119930 pFix->pName = pName;
119931 pFix->bTemp = (iDb==1);
119932 pFix->w.pParse = pParse;
119933 pFix->w.xExprCallback = fixExprCb;
119934 pFix->w.xSelectCallback = fixSelectCb;
119935 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
119936 pFix->w.walkerDepth = 0;
119937 pFix->w.eCode = 0;
119938 pFix->w.u.pFix = pFix;
119952 ** pParse->zErrMsg and these routines return non-zero. If everything
119964 res = sqlite3WalkSelect(&pFix->w, &s);
119973 return sqlite3WalkSelect(&pFix->w, pSelect);
119979 return sqlite3WalkExpr(&pFix->w, pExpr);
119989 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
119990 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
119991 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
119992 || sqlite3FixSrcList(pFix, pStep->pFrom)
119999 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
120000 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
120001 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
120002 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
120003 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
120010 pStep = pStep->pNext;
120033 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
120080 ** means that the SQL statement will never-run - the sqlite3_exec() call
120096 sqlite3_mutex_enter(db->mutex);
120097 db->xAuth = (sqlite3_xauth)xAuth;
120098 db->pAuthArg = pArg;
120099 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
120100 sqlite3_mutex_leave(db->mutex);
120105 ** Write an error message into pParse->zErrMsg that explains that the
120106 ** user-supplied authorization function returned an illegal value.
120110 pParse->rc = SQLITE_ERROR;
120128 sqlite3 *db = pParse->db; /* Database handle */
120129 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
120132 if( db->init.busy ) return SQLITE_OK;
120133 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
120135 ,db->auth.zAuthUser
120140 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
120142 pParse->rc = SQLITE_AUTH;
120166 int iSrc; /* Index in pTabList->a[] of table being read */
120170 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
120172 assert( pParse->db->xAuth!=0 );
120173 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
120180 if( pExpr->op==TK_TRIGGER ){
120181 pTab = pParse->pTriggerTab;
120184 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
120185 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
120186 pTab = pTabList->a[iSrc].pTab;
120191 iCol = pExpr->iColumn;
120195 assert( iCol<pTab->nCol );
120196 zCol = pTab->aCol[iCol].zCnName;
120197 }else if( pTab->iPKey>=0 ){
120198 assert( pTab->iPKey<pTab->nCol );
120199 zCol = pTab->aCol[pTab->iPKey].zCnName;
120203 assert( iDb>=0 && iDb<pParse->db->nDb );
120204 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
120205 pExpr->op = TK_NULL;
120222 sqlite3 *db = pParse->db;
120228 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
120229 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
120233 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
120234 ** callback are either NULL pointers or zero-terminated strings that
120242 testcase( pParse->zAuthContext==0 );
120244 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
120246 ,db->auth.zAuthUser
120251 pParse->rc = SQLITE_AUTH;
120262 ** popped. Or if pParse==0, this routine is a no-op.
120270 pContext->pParse = pParse;
120271 pContext->zAuthContext = pParse->zAuthContext;
120272 pParse->zAuthContext = zContext;
120280 if( pContext->pParse ){
120281 pContext->pParse->zAuthContext = pContext->zAuthContext;
120282 pContext->pParse = 0;
120329 ** Record the fact that we want to lock a table at run-time.
120352 for(i=0; i<pToplevel->nTableLock; i++){
120353 p = &pToplevel->aTableLock[i];
120354 if( p->iDb==iDb && p->iTab==iTab ){
120355 p->isWriteLock = (p->isWriteLock || isWriteLock);
120360 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
120361 pToplevel->aTableLock =
120362 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
120363 if( pToplevel->aTableLock ){
120364 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
120365 p->iDb = iDb;
120366 p->iTab = iTab;
120367 p->isWriteLock = isWriteLock;
120368 p->zLockName = zName;
120370 pToplevel->nTableLock = 0;
120371 sqlite3OomFault(pToplevel->db);
120382 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
120392 Vdbe *pVdbe = pParse->pVdbe;
120395 for(i=0; i<pParse->nTableLock; i++){
120396 TableLock *p = &pParse->aTableLock[i];
120397 int p1 = p->iDb;
120398 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
120399 p->zLockName, P4_STATIC);
120407 ** Return TRUE if the given yDbMask object is empty - if it contains no
120434 assert( pParse->pToplevel==0 );
120435 db = pParse->db;
120436 assert( db->pParse==pParse );
120437 if( pParse->nested ) return;
120438 if( pParse->nErr ){
120439 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
120442 assert( db->mallocFailed==0 );
120447 v = pParse->pVdbe;
120449 if( db->init.busy ){
120450 pParse->rc = SQLITE_DONE;
120454 if( v==0 ) pParse->rc = SQLITE_ERROR;
120456 assert( !pParse->isMultiWrite
120457 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
120459 if( pParse->bReturning ){
120460 Returning *pReturning = pParse->u1.pReturning;
120464 if( pReturning->nRetCol ){
120467 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
120469 reg = pReturning->iRetReg;
120470 for(i=0; i<pReturning->nRetCol; i++){
120471 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
120474 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
120482 if( pParse->nTableLock>0 && db->init.busy==0 ){
120484 if( db->auth.authLevel<UAUTH_User ){
120486 pParse->rc = SQLITE_AUTH_USER;
120498 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
120500 assert( db->nDb>0 );
120504 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
120506 pSchema = db->aDb[iDb].pSchema;
120510 DbMaskTest(pParse->writeMask,iDb), /* P2 */
120511 pSchema->schema_cookie, /* P3 */
120512 pSchema->iGeneration /* P4 */
120514 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
120516 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
120517 }while( ++iDb<db->nDb );
120519 for(i=0; i<pParse->nVtabLock; i++){
120520 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
120523 pParse->nVtabLock = 0;
120528 ** obtain the required table-locks. This is a no-op unless the
120529 ** shared-cache feature is enabled.
120531 if( pParse->nTableLock ) codeTableLocks(pParse);
120536 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
120540 if( pParse->pConstExpr ){
120541 ExprList *pEL = pParse->pConstExpr;
120542 pParse->okConstFactor = 0;
120543 for(i=0; i<pEL->nExpr; i++){
120544 assert( pEL->a[i].u.iConstExprReg>0 );
120545 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
120549 if( pParse->bReturning ){
120550 Returning *pRet = pParse->u1.pReturning;
120551 if( pRet->nRetCol ){
120552 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
120562 assert( v!=0 || pParse->nErr );
120563 assert( db->mallocFailed==0 || pParse->nErr );
120564 if( pParse->nErr==0 ){
120567 assert( pParse->pAinc==0 || pParse->nTab>0 );
120569 pParse->rc = SQLITE_DONE;
120571 pParse->rc = SQLITE_ERROR;
120584 ** * Built-in SQL functions always take precedence over application-defined
120586 ** built-in function.
120591 sqlite3 *db = pParse->db;
120592 u32 savedDbFlags = db->mDbFlags;
120595 if( pParse->nErr ) return;
120596 if( pParse->eParseMode ) return;
120597 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
120605 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
120606 pParse->nErr++;
120609 pParse->nested++;
120612 db->mDbFlags |= DBFLAG_PreferBuiltin;
120614 db->mDbFlags = savedDbFlags;
120617 pParse->nested--;
120631 ** Locate the in-memory structure that describes a particular database
120651 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
120656 for(i=0; i<db->nDb; i++){
120657 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
120659 if( i>=db->nDb ){
120668 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
120675 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
120680 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
120687 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
120690 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
120693 for(i=2; i<db->nDb; i++){
120695 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
120700 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
120702 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
120711 ** Locate the in-memory structure that describes a particular database
120714 ** error message in pParse->zErrMsg.
120717 ** routine leaves an error message in pParse->zErrMsg where
120727 sqlite3 *db = pParse->db;
120731 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
120743 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
120744 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
120749 testcase( pMod->pEpoTab==0 );
120750 return pMod->pEpoTab;
120755 pParse->checkSchema = 1;
120756 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
120768 assert( HasRowid(p) || p->iPKey<0 );
120779 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
120780 ** non-NULL if it is part of a view or trigger program definition. See
120789 assert( p->pSchema==0 || p->zDatabase==0 );
120790 if( p->pSchema ){
120791 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
120792 zDb = pParse->db->aDb[iDb].zDbSName;
120794 zDb = p->zDatabase;
120796 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
120816 ** Locate the in-memory structure that describes
120832 for(i=OMIT_TEMPDB; i<db->nDb; i++){
120834 Schema *pSchema = db->aDb[j].pSchema;
120838 p = sqlite3HashFind(&pSchema->idxHash, zName);
120851 sqlite3ExprDelete(db, p->pPartIdxWhere);
120852 sqlite3ExprListDelete(db, p->aColExpr);
120853 sqlite3DbFree(db, p->zColAff);
120854 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
120856 sqlite3_free(p->aiRowEst);
120872 pHash = &db->aDb[iDb].pSchema->idxHash;
120875 if( pIndex->pTable->pIndex==pIndex ){
120876 pIndex->pTable->pIndex = pIndex->pNext;
120881 p = pIndex->pTable->pIndex;
120882 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
120883 if( ALWAYS(p && p->pNext==pIndex) ){
120884 p->pNext = pIndex->pNext;
120889 db->mDbFlags |= DBFLAG_SchemaChange;
120893 ** Look through the list of open database files in db->aDb[] and if
120895 ** db->aDb[] structure to a smaller size, if possible.
120902 for(i=j=2; i<db->nDb; i++){
120903 struct Db *pDb = &db->aDb[i];
120904 if( pDb->pBt==0 ){
120905 sqlite3DbFree(db, pDb->zDbSName);
120906 pDb->zDbSName = 0;
120910 db->aDb[j] = db->aDb[i];
120914 db->nDb = j;
120915 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
120916 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
120917 sqlite3DbFree(db, db->aDb);
120918 db->aDb = db->aDbStatic;
120924 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
120929 assert( iDb<db->nDb );
120935 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
120938 if( db->nSchemaLock==0 ){
120939 for(i=0; i<db->nDb; i++){
120941 sqlite3SchemaClear(db->aDb[i].pSchema);
120954 for(i=0; i<db->nDb; i++){
120955 Db *pDb = &db->aDb[i];
120956 if( pDb->pSchema ){
120957 if( db->nSchemaLock==0 ){
120958 sqlite3SchemaClear(pDb->pSchema);
120964 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
120967 if( db->nSchemaLock==0 ){
120976 db->mDbFlags &= ~DBFLAG_SchemaChange;
120992 pList = pTab->u.tab.pDfltList;
120993 if( pCol->iDflt==0
120995 || NEVER(pList->nExpr<pCol->iDflt)
120997 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
120998 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
121000 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
121001 pList->a[pCol->iDflt-1].pExpr = pExpr;
121011 if( pCol->iDflt==0 ) return 0;
121013 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
121014 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
121015 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
121030 n = sqlite3Strlen30(pCol->zCnName) + 1;
121031 if( pCol->colFlags & COLFLAG_HASTYPE ){
121032 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
121035 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
121037 pCol->zCnName = zNew;
121038 memcpy(pCol->zCnName + n, zColl, nColl);
121039 pCol->colFlags |= COLFLAG_HASCOLL;
121048 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
121049 z = pCol->zCnName;
121051 if( pCol->colFlags & COLFLAG_HASTYPE ){
121066 if( (pCol = pTable->aCol)!=0 ){
121067 for(i=0; i<pTable->nCol; i++, pCol++){
121068 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
121069 sqlite3DbFree(db, pCol->zCnName);
121071 sqlite3DbNNFreeNN(db, pTable->aCol);
121073 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
121075 if( db->pnBytesFreed==0 ){
121076 pTable->aCol = 0;
121077 pTable->nCol = 0;
121079 pTable->u.tab.pDfltList = 0;
121097 ** db parameter can be used with db->pnBytesFreed to measure the memory
121113 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
121119 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
121120 pNext = pIndex->pNext;
121121 assert( pIndex->pSchema==pTable->pSchema
121122 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
121123 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
121124 char *zName = pIndex->zName;
121126 &pIndex->pSchema->idxHash, zName, 0
121128 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
121144 sqlite3SelectDelete(db, pTable->u.view.pSelect);
121150 sqlite3DbFree(db, pTable->zName);
121151 sqlite3DbFree(db, pTable->zColAff);
121152 sqlite3ExprListDelete(db, pTable->pCheck);
121162 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
121176 assert( iDb>=0 && iDb<db->nDb );
121179 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
121180 pDb = &db->aDb[iDb];
121181 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
121183 db->mDbFlags |= DBFLAG_SchemaChange;
121202 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
121218 if( p->nTab==0 ){
121219 p->nTab = 1;
121224 ** Parameter zName points to a nul-terminated buffer containing the name
121226 ** function returns the index of the named database in db->aDb[], or
121227 ** -1 if the named db cannot be found.
121230 int i = -1; /* Database number */
121233 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
121234 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
121246 ** index of the named database in db->aDb[], or -1 if the named db
121281 sqlite3 *db = pParse->db;
121284 if( pName2->n>0 ){
121285 if( db->init.busy ) {
121287 return -1;
121293 return -1;
121296 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
121297 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
121298 iDb = db->init.iDb;
121308 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
121309 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
121311 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
121313 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
121315 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
121319 ** This routine is used to check if the UTF-8 string zName is a legal
121335 sqlite3 *db = pParse->db;
121337 || db->init.imposterTable
121343 if( db->init.busy ){
121344 if( sqlite3_stricmp(zType, db->init.azInit[0])
121345 || sqlite3_stricmp(zName, db->init.azInit[1])
121346 || sqlite3_stricmp(zTblName, db->init.azInit[2])
121352 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
121369 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
121376 ** find the (first) offset of that column in index pIdx. Or return -1
121381 for(i=0; i<pIdx->nColumn; i++){
121382 if( iCol==pIdx->aiColumn[i] ) return i;
121384 return -1;
121397 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
121400 if( pTab->tabFlags & TF_HasVirtual ){
121403 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
121415 ** the N-th virtual column (zero-based) then the storage number is
121416 ** the number of non-virtual columns in the table plus N.
121432 ** -- 0 1 2 3 4 5 6 7 8
121443 ** this routine is a no-op macro. If the pTab does not have any virtual
121444 ** columns, then this routine is no-op that always return iCol. If iCol
121450 assert( iCol<pTab->nCol );
121451 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
121453 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
121455 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
121457 return pTab->nNVCol + i - n;
121471 ** is a read-only no-op.
121474 int iReg = ++pParse->nMem;
121492 ** The new table record is initialized and put in pParse->pNewTable.
121509 sqlite3 *db = pParse->db;
121514 if( db->init.busy && db->init.newTnum==1 ){
121516 iDb = db->init.iDb;
121523 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
121535 pParse->sNameToken = *pName;
121540 if( db->init.iDb==1 ) isTemp = 1;
121551 char *zDb = db->aDb[iDb].zDbSName;
121570 char *zDb = db->aDb[iDb].zDbSName;
121580 assert( !db->init.busy || CORRUPT_DB );
121594 assert( db->mallocFailed );
121595 pParse->rc = SQLITE_NOMEM_BKPT;
121596 pParse->nErr++;
121599 pTable->zName = zName;
121600 pTable->iPKey = -1;
121601 pTable->pSchema = db->aDb[iDb].pSchema;
121602 pTable->nTabRef = 1;
121604 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
121606 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
121608 assert( pParse->pNewTable==0 );
121609 pParse->pNewTable = pTable;
121619 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
121636 reg1 = pParse->regRowid = ++pParse->nMem;
121637 reg2 = pParse->regRoot = ++pParse->nMem;
121638 reg3 = ++pParse->nMem;
121642 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
121648 /* This just creates a place-holder record in the sqlite_schema table.
121652 ** The rowid for the new entry is left in register pParse->regRowid.
121653 ** The root page number of the new table is left in reg pParse->regRoot.
121663 assert( !pParse->bReturning );
121664 pParse->u1.addrCrTab =
121675 /* Normal (non-error) return. */
121680 pParse->checkSchema = 1;
121690 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
121691 pCol->colFlags |= COLFLAG_HIDDEN;
121692 if( pTab ) pTab->tabFlags |= TF_HasHidden;
121693 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
121694 pTab->tabFlags |= TF_OOOHidden;
121704 pHash = &(db->aDb[1].pSchema->trigHash);
121705 sqlite3HashInsert(pHash, pRet->zName, 0);
121706 sqlite3ExprListDelete(db, pRet->pReturnEL);
121729 sqlite3 *db = pParse->db;
121730 if( pParse->pNewTrigger ){
121733 assert( pParse->bReturning==0 || pParse->ifNotExists );
121735 pParse->bReturning = 1;
121741 pParse->u1.pReturning = pRet;
121742 pRet->pParse = pParse;
121743 pRet->pReturnEL = pList;
121746 testcase( pParse->earlyCleanup );
121747 if( db->mallocFailed ) return;
121748 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
121750 pRet->retTrig.zName = pRet->zName;
121751 pRet->retTrig.op = TK_RETURNING;
121752 pRet->retTrig.tr_tm = TRIGGER_AFTER;
121753 pRet->retTrig.bReturning = 1;
121754 pRet->retTrig.pSchema = db->aDb[1].pSchema;
121755 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
121756 pRet->retTrig.step_list = &pRet->retTStep;
121757 pRet->retTStep.op = TK_RETURNING;
121758 pRet->retTStep.pTrig = &pRet->retTrig;
121759 pRet->retTStep.pExprList = pList;
121760 pHash = &(db->aDb[1].pSchema->trigHash);
121761 assert( sqlite3HashFind(pHash, pRet->zName)==0
121762 || pParse->nErr || pParse->ifNotExists );
121763 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
121764 ==&pRet->retTrig ){
121783 sqlite3 *db = pParse->db;
121790 if( (p = pParse->pNewTable)==0 ) return;
121791 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
121792 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
121802 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
121804 sType.n -= 6;
121805 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
121807 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
121809 sType.n -= 9;
121810 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
121839 for(i=0; i<p->nCol; i++){
121840 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
121846 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
121851 p->aCol = aNew;
121852 pCol = &p->aCol[p->nCol];
121853 memset(pCol, 0, sizeof(p->aCol[0]));
121854 pCol->zCnName = z;
121855 pCol->hName = hName;
121861 pCol->affinity = affinity;
121862 pCol->eCType = eType;
121863 pCol->szEst = szEst;
121867 pCol->colFlags |= COLFLAG_SORTERREF;
121876 pCol->affinity = sqlite3AffinityType(zType, pCol);
121877 pCol->colFlags |= COLFLAG_HASTYPE;
121879 p->nCol++;
121880 p->nNVCol++;
121881 pParse->constraintName.n = 0;
121893 p = pParse->pNewTable;
121894 if( p==0 || NEVER(p->nCol<1) ) return;
121895 pCol = &p->aCol[p->nCol-1];
121896 pCol->notNull = (u8)onError;
121897 p->tabFlags |= TF_HasNotNull;
121901 if( pCol->colFlags & COLFLAG_UNIQUE ){
121903 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
121904 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
121905 if( pIdx->aiColumn[0]==p->nCol-1 ){
121906 pIdx->uniqNotNull = 1;
121916 ** This routine does a case-independent search of zType for the
121924 ** --------------------------------
121982 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
121989 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
121994 pCol->colFlags |= COLFLAG_SORTERREF;
121999 pCol->szEst = v;
122022 sqlite3 *db = pParse->db;
122023 p = pParse->pNewTable;
122025 int isInit = db->init.busy && db->init.iDb!=1;
122026 pCol = &(p->aCol[p->nCol-1]);
122029 pCol->zCnName);
122031 }else if( pCol->colFlags & COLFLAG_GENERATED ){
122032 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
122033 testcase( pCol->colFlags & COLFLAG_STORED );
122074 if( p->op==TK_STRING ){
122075 p->op = TK_ID;
122076 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
122077 p->pLeft->op = TK_ID;
122085 pCol->colFlags |= COLFLAG_PRIMKEY;
122087 if( pCol->colFlags & COLFLAG_GENERATED ){
122088 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
122089 testcase( pCol->colFlags & COLFLAG_STORED );
122108 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
122121 Table *pTab = pParse->pNewTable;
122123 int iCol = -1, i;
122126 if( pTab->tabFlags & TF_HasPrimaryKey ){
122128 "table \"%s\" has more than one primary key", pTab->zName);
122131 pTab->tabFlags |= TF_HasPrimaryKey;
122133 iCol = pTab->nCol - 1;
122134 pCol = &pTab->aCol[iCol];
122138 nTerm = pList->nExpr;
122140 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
122143 if( pCExpr->op==TK_ID ){
122146 zCName = pCExpr->u.zToken;
122147 for(iCol=0; iCol<pTab->nCol; iCol++){
122148 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
122149 pCol = &pTab->aCol[iCol];
122159 && pCol->eCType==COLTYPE_INTEGER
122163 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
122164 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
122166 pTab->iPKey = iCol;
122167 pTab->keyConf = (u8)onError;
122169 pTab->tabFlags |= autoInc*TF_Autoincrement;
122170 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
122184 sqlite3ExprListDelete(pParse->db, pList);
122198 Table *pTab = pParse->pNewTable;
122199 sqlite3 *db = pParse->db;
122201 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
122203 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
122204 if( pParse->constraintName.n ){
122205 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
122209 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
122211 t.n = (int)(zEnd - t.z);
122212 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
122217 sqlite3ExprDelete(pParse->db, pCheckExpr);
122231 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
122232 i = p->nCol-1;
122233 db = pParse->db;
122239 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
122245 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
122246 assert( pIdx->nKeyCol==1 );
122247 if( pIdx->aiColumn[0]==i ){
122248 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
122261 Table *pTab = pParse->pNewTable;
122267 pCol = &(pTab->aCol[pTab->nCol-1]);
122272 if( pCol->iDflt>0 ) goto generated_error;
122274 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
122275 /* no-op */
122276 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
122282 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
122283 pCol->colFlags |= eType;
122286 pTab->tabFlags |= eType;
122287 if( pCol->colFlags & COLFLAG_PRIMKEY ){
122290 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
122297 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
122304 pCol->zCnName);
122306 sqlite3ExprDelete(pParse->db, pExpr);
122309 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
122311 sqlite3ExprDelete(pParse->db, pExpr);
122325 ** This plan is not completely bullet-proof. It is possible for
122331 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
122332 ** the schema-version whenever the schema changes.
122335 sqlite3 *db = pParse->db;
122336 Vdbe *v = pParse->pVdbe;
122339 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
122362 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
122369 ** it is quoted using double-quotes.
122405 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
122406 n += identLength(pCol->zCnName) + 5;
122408 n += identLength(p->zName);
122418 n += 35 + 6*p->nCol;
122426 identPut(zStmt, &k, p->zName);
122428 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
122440 sqlite3_snprintf(n-k, &zStmt[k], zSep);
122443 identPut(zStmt, &k, pCol->zCnName);
122444 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
122445 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
122446 testcase( pCol->affinity==SQLITE_AFF_BLOB );
122447 testcase( pCol->affinity==SQLITE_AFF_TEXT );
122448 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
122449 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
122450 testcase( pCol->affinity==SQLITE_AFF_REAL );
122451 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
122453 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
122455 assert( pCol->affinity==SQLITE_AFF_BLOB
122456 || pCol->affinity==SQLITE_AFF_FLEXNUM
122457 || pCol->affinity==sqlite3AffinityType(zType, 0) );
122462 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
122473 if( pIdx->nColumn>=N ) return SQLITE_OK;
122474 assert( pIdx->isResized==0 );
122478 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
122479 pIdx->azColl = (const char**)zExtra;
122481 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
122482 pIdx->aiRowLogEst = (LogEst*)zExtra;
122484 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
122485 pIdx->aiColumn = (i16*)zExtra;
122487 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
122488 pIdx->aSortOrder = (u8*)zExtra;
122489 pIdx->nColumn = N;
122490 pIdx->isResized = 1;
122501 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
122502 wTable += pTabCol->szEst;
122504 if( pTab->iPKey<0 ) wTable++;
122505 pTab->szTabRow = sqlite3LogEst(wTable*4);
122514 const Column *aCol = pIdx->pTable->aCol;
122515 for(i=0; i<pIdx->nColumn; i++){
122516 i16 x = pIdx->aiColumn[i];
122517 assert( x<pIdx->pTable->nCol );
122520 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
122528 while( nCol-- > 0 ){
122538 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
122551 assert( nKey<=pIdx->nColumn );
122552 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
122553 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
122554 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
122555 assert( pPk->pTable==pIdx->pTable );
122557 j = pPk->aiColumn[iCol];
122560 assert( pIdx->aiColumn[i]>=0 || j>=0 );
122561 if( pIdx->aiColumn[i]==j
122562 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
122575 ** high-order bit of colNotIdxed is always 1. All unindexed columns
122578 ** 2019-10-24: For the purpose of this computation, virtual columns are
122586 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
122592 Table *pTab = pIdx->pTable;
122593 for(j=pIdx->nColumn-1; j>=0; j--){
122594 int x = pIdx->aiColumn[j];
122595 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
122596 testcase( x==BMS-1 );
122597 testcase( x==BMS-2 );
122598 if( x<BMS-1 ) m |= MASKBIT(x);
122601 pIdx->colNotIdxed = ~m;
122602 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
122635 sqlite3 *db = pParse->db;
122636 Vdbe *v = pParse->pVdbe;
122640 if( !db->init.imposterTable ){
122641 for(i=0; i<pTab->nCol; i++){
122642 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
122643 && (pTab->aCol[i].notNull==OE_None)
122645 pTab->aCol[i].notNull = OE_Abort;
122648 pTab->tabFlags |= TF_HasNotNull;
122654 assert( !pParse->bReturning );
122655 if( pParse->u1.addrCrTab ){
122657 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
122663 if( pTab->iPKey>=0 ){
122666 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
122670 pTab->tabFlags &= ~TF_WithoutRowid;
122674 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
122676 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
122677 assert( pParse->pNewTable==pTab );
122678 pTab->iPKey = -1;
122679 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
122681 if( pParse->nErr ){
122682 pTab->tabFlags &= ~TF_WithoutRowid;
122685 assert( db->mallocFailed==0 );
122687 assert( pPk->nKeyCol==1 );
122697 for(i=j=1; i<pPk->nKeyCol; i++){
122699 pPk->nColumn--;
122701 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
122702 pPk->azColl[j] = pPk->azColl[i];
122703 pPk->aSortOrder[j] = pPk->aSortOrder[i];
122704 pPk->aiColumn[j++] = pPk->aiColumn[i];
122707 pPk->nKeyCol = j;
122710 pPk->isCovering = 1;
122711 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
122712 nPk = pPk->nColumn = pPk->nKeyCol;
122718 if( v && pPk->tnum>0 ){
122719 assert( db->init.busy==0 );
122720 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
122724 pPk->tnum = pTab->tnum;
122726 /* Update the in-memory representation of all UNIQUE indices by converting
122729 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122733 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
122734 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
122740 pIdx->nColumn = pIdx->nKeyCol;
122743 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
122744 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
122745 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
122746 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
122747 pIdx->aiColumn[j] = pPk->aiColumn[i];
122748 pIdx->azColl[j] = pPk->azColl[i];
122749 if( pPk->aSortOrder[i] ){
122751 pIdx->bAscKeyBug = 1;
122756 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
122757 assert( pIdx->nColumn>=j );
122763 for(i=0; i<pTab->nCol; i++){
122764 if( !hasColumn(pPk->aiColumn, nPk, i)
122765 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
122768 for(i=0, j=nPk; i<pTab->nCol; i++){
122769 if( !hasColumn(pPk->aiColumn, j, i)
122770 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
122772 assert( j<pPk->nColumn );
122773 pPk->aiColumn[j] = i;
122774 pPk->azColl[j] = sqlite3StrBINARY;
122778 assert( pPk->nColumn==j );
122779 assert( pTab->nNVCol<=j );
122794 nName = sqlite3Strlen30(pTab->zName);
122795 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
122797 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
122799 if( pMod->pModule->iVersion<3 ) return 0;
122800 if( pMod->pModule->xShadowName==0 ) return 0;
122801 return pMod->pModule->xShadowName(zName+nName+1);
122813 int nName; /* Length of pTab->zName */
122818 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
122820 if( NEVER(pMod->pModule==0) ) return;
122821 if( pMod->pModule->iVersion<3 ) return;
122822 if( pMod->pModule->xShadowName==0 ) return;
122823 assert( pTab->zName!=0 );
122824 nName = sqlite3Strlen30(pTab->zName);
122825 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
122827 assert( pOther->zName!=0 );
122829 if( pOther->tabFlags & TF_Shadow ) continue;
122830 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
122831 && pOther->zName[nName]=='_'
122832 && pMod->pModule->xShadowName(pOther->zName+nName+1)
122834 pOther->tabFlags |= TF_Shadow;
122886 #define markExprListImmutable(X) /* no-op */
122899 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
122918 sqlite3 *db = pParse->db; /* The database connection */
122925 p = pParse->pNewTable;
122928 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
122929 p->tabFlags |= TF_Shadow;
122932 /* If the db->init.busy is 1 it means we are reading the SQL off the
122935 ** for the table from the db->init.newTnum field. (The page number
122939 ** table itself. So mark it read-only.
122941 if( db->init.busy ){
122942 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
122946 p->tnum = db->init.newTnum;
122947 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
122961 p->tabFlags |= TF_Strict;
122962 for(ii=0; ii<p->nCol; ii++){
122963 Column *pCol = &p->aCol[ii];
122964 if( pCol->eCType==COLTYPE_CUSTOM ){
122965 if( pCol->colFlags & COLFLAG_HASTYPE ){
122968 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
122972 p->zName, pCol->zCnName);
122975 }else if( pCol->eCType==COLTYPE_ANY ){
122976 pCol->affinity = SQLITE_AFF_BLOB;
122978 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
122979 && p->iPKey!=ii
122980 && pCol->notNull == OE_None
122982 pCol->notNull = OE_Abort;
122983 p->tabFlags |= TF_HasNotNull;
122988 assert( (p->tabFlags & TF_HasPrimaryKey)==0
122989 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
122990 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
122991 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
122995 if( (p->tabFlags & TF_Autoincrement) ){
123000 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
123001 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
123004 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
123007 iDb = sqlite3SchemaToIndex(db, p->pSchema);
123012 if( p->pCheck ){
123013 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
123014 if( pParse->nErr ){
123017 sqlite3ExprListDelete(db, p->pCheck);
123018 p->pCheck = 0;
123020 markExprListImmutable(p->pCheck);
123025 if( p->tabFlags & TF_HasGenerated ){
123027 testcase( p->tabFlags & TF_HasVirtual );
123028 testcase( p->tabFlags & TF_HasStored );
123029 for(ii=0; ii<p->nCol; ii++){
123030 u32 colFlags = p->aCol[ii].colFlags;
123032 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
123042 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
123050 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
123058 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
123068 if( !db->init.busy ){
123096 ** statement to populate the new table. The root-page number for the
123097 ** new table is in register pParse->regRoot.
123103 ** A shared-cache write-lock is not required to write to the new table,
123104 ** as a schema-lock must have already been obtained to create it. Since
123105 ** a schema-lock excludes all other database users, the write-lock would
123110 int regYield; /* Register holding co-routine entry-point */
123111 int addrTop; /* Top of the co-routine */
123118 pParse->rc = SQLITE_ERROR;
123119 pParse->nErr++;
123122 regYield = ++pParse->nMem;
123123 regRec = ++pParse->nMem;
123124 regRowid = ++pParse->nMem;
123125 assert(pParse->nTab==1);
123127 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
123129 pParse->nTab = 2;
123132 if( pParse->nErr ) return;
123135 assert( p->aCol==0 );
123136 p->nCol = p->nNVCol = pSelTab->nCol;
123137 p->aCol = pSelTab->aCol;
123138 pSelTab->nCol = 0;
123139 pSelTab->aCol = 0;
123143 if( pParse->nErr ) return;
123145 sqlite3VdbeJumpHere(v, addrTop - 1);
123161 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
123162 n = (int)(pEnd2->z - pParse->sNameToken.z);
123163 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
123165 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
123177 db->aDb[iDb].zDbSName,
123179 p->zName,
123180 p->zName,
123181 pParse->regRoot,
123183 pParse->regRowid
123192 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
123193 Db *pDb = &db->aDb[iDb];
123195 if( pDb->pSchema->pSeqTab==0 ){
123198 pDb->zDbSName
123206 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
123210 if( p->tabFlags & TF_HasGenerated ){
123213 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
123217 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
123220 /* Add the table to the in-memory representation of the database.
123222 if( db->init.busy ){
123224 Schema *pSchema = p->pSchema;
123226 assert( HasRowid(p) || p->iPKey<0 );
123227 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
123233 pParse->pNewTable = 0;
123234 db->mDbFlags |= DBFLAG_SchemaChange;
123239 assert( !pParse->nested );
123241 if( strcmp(p->zName, "sqlite_sequence")==0 ){
123243 p->pSchema->pSeqTab = p;
123251 if( pCons->z==0 ){
123254 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
123280 sqlite3 *db = pParse->db;
123282 if( pParse->nVar>0 ){
123287 p = pParse->pNewTable;
123288 if( p==0 || pParse->nErr ) goto create_view_fail;
123293 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
123297 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
123299 p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */
123303 iDb = sqlite3SchemaToIndex(db, p->pSchema);
123309 ** allocated rather than point to the input string - which means that
123312 pSelect->selFlags |= SF_View;
123314 p->u.view.pSelect = pSelect;
123317 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
123319 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
123320 p->eTabType = TABTYP_VIEW;
123321 if( db->mallocFailed ) goto create_view_fail;
123326 sEnd = pParse->sLastToken;
123332 n = (int)(sEnd.z - pBegin->z);
123334 z = pBegin->z;
123335 while( sqlite3Isspace(z[n-1]) ){ n--; }
123336 sEnd.z = &z[n-1];
123356 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
123362 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
123374 db->nSchemaLock++;
123376 db->nSchemaLock--;
123386 assert( pTable->nCol<=0 );
123403 if( pTable->nCol<0 ){
123404 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
123407 assert( pTable->nCol>=0 );
123417 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
123419 u8 eParseMode = pParse->eParseMode;
123420 int nTab = pParse->nTab;
123421 int nSelect = pParse->nSelect;
123422 pParse->eParseMode = PARSE_MODE_NORMAL;
123423 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
123424 pTable->nCol = -1;
123427 xAuth = db->xAuth;
123428 db->xAuth = 0;
123430 db->xAuth = xAuth;
123434 pParse->nTab = nTab;
123435 pParse->nSelect = nSelect;
123437 pTable->nCol = 0;
123439 }else if( pTable->pCheck ){
123442 ** arglist which is stored in pTable->pCheck. The pCheck field
123446 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
123447 &pTable->nCol, &pTable->aCol);
123448 if( pParse->nErr==0
123449 && pTable->nCol==pSel->pEList->nExpr
123451 assert( db->mallocFailed==0 );
123458 assert( pTable->aCol==0 );
123459 pTable->nCol = pSelTab->nCol;
123460 pTable->aCol = pSelTab->aCol;
123461 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
123462 pSelTab->nCol = 0;
123463 pSelTab->aCol = 0;
123464 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
123466 pTable->nNVCol = pTable->nCol;
123470 pParse->eParseMode = eParseMode;
123474 pTable->pSchema->schemaFlags |= DB_UnresetViews;
123475 if( db->mallocFailed ){
123483 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
123496 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
123511 ** root-page of a table or index in database iDb has changed from iFrom
123532 pDb = &db->aDb[iDb];
123533 pHash = &pDb->pSchema->tblHash;
123536 if( pTab->tnum==iFrom ){
123537 pTab->tnum = iTo;
123540 pHash = &pDb->pSchema->idxHash;
123543 if( pIdx->tnum==iFrom ){
123544 pIdx->tnum = iTo;
123551 ** Write code to erase the table with root-page iTable from database iDb.
123553 ** if a root-page of another table is moved by the btree-layer whilst
123554 ** erasing iTable (this can happen with an auto-vacuum database).
123564 ** is non-zero, then it is the root page number of a table moved to
123575 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
123583 ** in case a root-page belonging to another table is moved by the btree layer
123584 ** is also added (this can happen with an auto-vacuum database).
123587 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
123589 ** table and index root-pages in order, starting with the numerically
123590 ** largest root-page number. This guarantees that none of the root-pages
123598 ** and root page 5 happened to be the largest root-page number in the
123601 ** a free-list page.
123603 Pgno iTab = pTab->tnum;
123613 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123614 Pgno iIdx = pIdx->tnum;
123615 assert( pIdx->pSchema==pTab->pSchema );
123623 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
123624 assert( iDb>=0 && iDb<pParse->db->nDb );
123642 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
123646 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
123660 sqlite3 *db = pParse->db;
123662 Db *pDb = &db->aDb[iDb];
123680 assert( pTrigger->pSchema==pTab->pSchema ||
123681 pTrigger->pSchema==db->aDb[1].pSchema );
123683 pTrigger = pTrigger->pNext;
123690 ** move as a result of the drop (can happen in auto-vacuum mode).
123692 if( pTab->tabFlags & TF_Autoincrement ){
123695 pDb->zDbSName, pTab->zName
123710 pDb->zDbSName, pTab->zName);
123719 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
123722 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
123728 ** Return TRUE if shadow tables should be read-only in the current
123733 if( (db->flags & SQLITE_Defensive)!=0
123734 && db->pVtabCtx==0
123735 && db->nVdbeExec==0
123748 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
123749 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
123750 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
123753 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
123756 if( pTab->tabFlags & TF_Eponymous ){
123769 sqlite3 *db = pParse->db;
123772 if( db->mallocFailed ){
123775 assert( pParse->nErr==0 );
123776 assert( pName->nSrc==1 );
123778 if( noErr ) db->suppressErr++;
123780 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
123781 if( noErr ) db->suppressErr--;
123785 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
123790 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123791 assert( iDb>=0 && iDb<db->nDb );
123803 const char *zDb = db->aDb[iDb].zDbSName;
123817 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
123826 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
123829 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
123835 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
123844 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
123848 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
123860 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
123881 ** under construction in the pParse->pNewTable field.
123893 sqlite3 *db = pParse->db;
123897 Table *p = pParse->pNewTable;
123906 int iCol = p->nCol-1;
123908 if( pToCol && pToCol->nExpr!=1 ){
123911 p->aCol[iCol].zCnName, pTo);
123915 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
123921 nCol = pFromCol->nExpr;
123923 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
123925 for(i=0; i<pToCol->nExpr; i++){
123926 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
123933 pFKey->pFrom = p;
123935 pFKey->pNextFrom = p->u.tab.pFKey;
123936 z = (char*)&pFKey->aCol[nCol];
123937 pFKey->zTo = z;
123941 memcpy(z, pTo->z, pTo->n);
123942 z[pTo->n] = 0;
123944 z += pTo->n+1;
123945 pFKey->nCol = nCol;
123947 pFKey->aCol[0].iFrom = p->nCol-1;
123951 for(j=0; j<p->nCol; j++){
123952 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
123953 pFKey->aCol[i].iFrom = j;
123957 if( j>=p->nCol ){
123960 pFromCol->a[i].zEName);
123964 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
123970 int n = sqlite3Strlen30(pToCol->a[i].zEName);
123971 pFKey->aCol[i].zCol = z;
123973 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
123975 memcpy(z, pToCol->a[i].zEName, n);
123980 pFKey->isDeferred = 0;
123981 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
123982 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
123984 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
123985 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
123986 pFKey->zTo, (void *)pFKey
123993 assert( pNextTo->pPrevTo==0 );
123994 pFKey->pNextTo = pNextTo;
123995 pNextTo->pPrevTo = pFKey;
124001 p->u.tab.pFKey = pFKey;
124022 if( (pTab = pParse->pNewTable)==0 ) return;
124024 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
124025 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
124026 pFKey->isDeferred = (u8)isDeferred;
124039 ** the root page number of the index is taken from pIndex->tnum.
124042 Table *pTab = pIndex->pTable; /* The table that is indexed */
124043 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
124044 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
124053 sqlite3 *db = pParse->db; /* The database connection */
124054 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
124057 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
124058 db->aDb[iDb].zDbSName ) ){
124063 /* Require a write-lock on the table to perform this operation */
124064 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
124071 tnum = pIndex->tnum;
124074 assert( pKey!=0 || pParse->nErr );
124077 iSorter = pParse->nTab++;
124078 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
124104 pIndex->nKeyCol); VdbeCoverage(v);
124119 if( !pIndex->bAscKeyBug ){
124144 ** of 8-byte aligned space after the Index object and return a
124164 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
124165 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
124166 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
124167 p->aSortOrder = (u8*)pExtra;
124168 p->nColumn = nCol;
124169 p->nKeyCol = nCol - 1;
124178 ** pParse and return non-zero. Otherwise, return zero.
124183 for(i=0; i<pList->nExpr; i++){
124184 if( pList->a[i].fg.bNulls ){
124185 u8 sf = pList->a[i].fg.sortFlags;
124200 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
124201 ** as the table to be indexed. pParse->pNewTable is a table that is
124205 ** is a primary key or unique-constraint on the most recent column added
124212 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
124228 sqlite3 *db = pParse->db;
124238 assert( db->pParse==pParse );
124239 if( pParse->nErr ){
124242 assert( db->mallocFailed==0 );
124258 /* Use the two-part index name to determine the database
124265 assert( pName && pName->z );
124272 if( !db->init.busy ){
124274 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
124286 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
124287 assert( db->mallocFailed==0 || pTab==0 );
124289 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
124291 "cannot create a TEMP index on non-TEMP table \"%s\"",
124292 pTab->zName);
124299 pTab = pParse->pNewTable;
124301 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124303 pDb = &db->aDb[iDb];
124306 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
124307 && db->init.busy==0
124310 && sqlite3UserAuthTable(pTab->zName)==0
124313 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
124345 assert( pName->z!=0 );
124346 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
124350 if( !db->init.busy ){
124351 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
124356 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
124360 assert( !db->init.busy );
124370 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
124371 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
124388 const char *zDb = pDb->zDbSName;
124394 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
124406 Column *pCol = &pTab->aCol[pTab->nCol-1];
124407 pCol->colFlags |= COLFLAG_UNIQUE;
124408 sqlite3TokenInit(&prevCol, pCol->zCnName);
124412 assert( pList->nExpr==1 );
124416 if( pParse->nErr ) goto exit_create_index;
124422 for(i=0; i<pList->nExpr; i++){
124423 Expr *pExpr = pList->a[i].pExpr;
124425 if( pExpr->op==TK_COLLATE ){
124427 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
124435 nExtraCol = pPk ? pPk->nKeyCol : 1;
124436 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
124437 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
124439 if( db->mallocFailed ){
124442 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
124443 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
124444 pIndex->zName = zExtra;
124446 memcpy(pIndex->zName, zName, nName+1);
124447 pIndex->pTable = pTab;
124448 pIndex->onError = (u8)onError;
124449 pIndex->uniqNotNull = onError!=OE_None;
124450 pIndex->idxType = idxType;
124451 pIndex->pSchema = db->aDb[iDb].pSchema;
124452 pIndex->nKeyCol = pList->nExpr;
124455 pIndex->pPartIdxWhere = pPIWhere;
124462 if( pDb->pSchema->file_format>=4 ){
124463 sortOrderMask = -1; /* Honor DESC */
124471 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
124477 pListItem = pList->a;
124479 pIndex->aColExpr = pList;
124482 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
124483 Expr *pCExpr; /* The i-th index expression */
124484 int requestedSortOrder; /* ASC or DESC on the i-th expression */
124487 sqlite3StringToId(pListItem->pExpr);
124488 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
124489 if( pParse->nErr ) goto exit_create_index;
124490 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
124491 if( pCExpr->op!=TK_COLUMN ){
124492 if( pTab==pParse->pNewTable ){
124497 if( pIndex->aColExpr==0 ){
124498 pIndex->aColExpr = pList;
124502 pIndex->aiColumn[i] = XN_EXPR;
124503 pIndex->uniqNotNull = 0;
124504 pIndex->bHasExpr = 1;
124506 j = pCExpr->iColumn;
124509 j = pTab->iPKey;
124511 if( pTab->aCol[j].notNull==0 ){
124512 pIndex->uniqNotNull = 0;
124514 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
124515 pIndex->bHasVCol = 1;
124516 pIndex->bHasExpr = 1;
124519 pIndex->aiColumn[i] = (i16)j;
124522 if( pListItem->pExpr->op==TK_COLLATE ){
124524 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
124525 zColl = pListItem->pExpr->u.zToken;
124531 nExtra -= nColl;
124533 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
124536 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
124539 pIndex->azColl[i] = zColl;
124540 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
124541 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
124549 for(j=0; j<pPk->nKeyCol; j++){
124550 int x = pPk->aiColumn[j];
124552 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
124553 pIndex->nColumn--;
124555 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
124556 pIndex->aiColumn[i] = x;
124557 pIndex->azColl[i] = pPk->azColl[j];
124558 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
124562 assert( i==pIndex->nColumn );
124564 pIndex->aiColumn[i] = XN_ROWID;
124565 pIndex->azColl[i] = sqlite3StrBINARY;
124568 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
124573 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
124575 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
124576 pIndex->isCovering = 1;
124577 for(j=0; j<pTab->nCol; j++){
124578 if( j==pTab->iPKey ) continue;
124580 pIndex->isCovering = 0;
124585 if( pTab==pParse->pNewTable ){
124608 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124611 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
124614 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
124615 for(k=0; k<pIdx->nKeyCol; k++){
124618 assert( pIdx->aiColumn[k]>=0 );
124619 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
124620 z1 = pIdx->azColl[k];
124621 z2 = pIndex->azColl[k];
124624 if( k==pIdx->nKeyCol ){
124625 if( pIdx->onError!=pIndex->onError ){
124633 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
124637 if( pIdx->onError==OE_Default ){
124638 pIdx->onError = pIndex->onError;
124641 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
124643 pIndex->pNext = pParse->pNewIndex;
124644 pParse->pNewIndex = pIndex;
124655 ** in-memory database structures.
124657 assert( pParse->nErr==0 );
124658 if( db->init.busy ){
124661 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
124663 pIndex->tnum = db->init.newTnum;
124666 pParse->rc = SQLITE_CORRUPT_BKPT;
124670 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
124671 pIndex->zName, pIndex);
124677 db->mDbFlags |= DBFLAG_SchemaChange;
124696 int iMem = ++pParse->nMem;
124709 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
124717 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
124718 if( pName->z[n-1]==';' ) n--;
124721 onError==OE_None ? "" : " UNIQUE", n, pName->z);
124732 db->aDb[iDb].zDbSName,
124733 pIndex->zName,
124734 pTab->zName,
124741 ** to invalidate all pre-compiled statements.
124747 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
124751 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
124754 if( db->init.busy || pTblName==0 ){
124755 pIndex->pNext = pTab->pIndex;
124756 pTab->pIndex = pIndex;
124760 assert( pParse->pNewIndex==0 );
124761 pParse->pNewIndex = pIndex;
124775 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
124777 if( pThis->onError!=OE_Replace ) continue;
124778 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
124780 pThis->pNext = pNext->pNext;
124781 pNext->pNext = pThis;
124782 ppFrom = &pNext->pNext;
124790 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
124791 assert( pThis->onError!=OE_Replace
124792 || pThis->pNext==0
124793 || pThis->pNext->onError==OE_Replace );
124804 ** Fill the Index.aiRowEst[] array with default information - information
124814 ** aiRowEst[N]<=aiRowEst[N-1]
124824 LogEst *a = pIdx->aiRowLogEst;
124826 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
124830 assert( !pIdx->hasStat1 );
124836 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
124842 x = pIdx->pTable->nRowLogEst;
124845 pIdx->pTable->nRowLogEst = x = 99;
124847 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
124853 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
124858 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
124868 sqlite3 *db = pParse->db;
124871 if( db->mallocFailed ){
124874 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
124875 assert( pName->nSrc==1 );
124879 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
124882 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
124884 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
124887 pParse->checkSchema = 1;
124890 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
124895 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
124899 Table *pTab = pIndex->pTable;
124900 const char *zDb = db->aDb[iDb].zDbSName;
124906 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
124918 db->aDb[iDb].zDbSName, pIndex->zName
124920 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
124922 destroyRootPage(pParse, pIndex->tnum, iDb);
124923 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
124936 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
124944 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
124956 if( (n & (n-1))==0 ){
124960 *pIdx = -1;
124978 sqlite3 *db = pParse->db;
124986 sizeof(IdList) + pList->nId*sizeof(pList->a));
124993 i = pList->nId++;
124994 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
124995 if( IN_RENAME_OBJECT && pList->a[i].zName ){
124996 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
125008 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
125009 for(i=0; i<pList->nId; i++){
125010 sqlite3DbFree(db, pList->a[i].zName);
125016 ** Return the index in pList of the identifier named zId. Return -1
125022 for(i=0; i<pList->nId; i++){
125023 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
125025 return -1;
125034 ** appropriate for small and memory-limited applications.
125063 int nExtra, /* Number of new slots to add to pSrc->a[] */
125064 int iStart /* Index in pSrc->a[] of first new slot */
125072 assert( iStart<=pSrc->nSrc );
125075 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
125077 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
125078 sqlite3 *db = pParse->db;
125080 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
125087 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
125089 assert( db->mallocFailed );
125093 pSrc->nAlloc = nAlloc;
125098 for(i=pSrc->nSrc-1; i>=iStart; i--){
125099 pSrc->a[i+nExtra] = pSrc->a[i];
125101 pSrc->nSrc += nExtra;
125104 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
125106 pSrc->a[i].iCursor = -1;
125159 assert( pParse->db!=0 );
125160 db = pParse->db;
125162 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
125164 pList->nAlloc = 1;
125165 pList->nSrc = 1;
125166 memset(&pList->a[0], 0, sizeof(pList->a[0]));
125167 pList->a[0].iCursor = -1;
125169 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
125177 pItem = &pList->a[pList->nSrc-1];
125178 if( pDatabase && pDatabase->z==0 ){
125182 pItem->zName = sqlite3NameFromToken(db, pDatabase);
125183 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
125185 pItem->zName = sqlite3NameFromToken(db, pTable);
125186 pItem->zDatabase = 0;
125197 assert( pList || pParse->db->mallocFailed );
125199 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
125200 if( pItem->iCursor>=0 ) continue;
125201 pItem->iCursor = pParse->nTab++;
125202 if( pItem->pSelect ){
125203 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
125217 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
125218 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
125219 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
125220 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
125221 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
125222 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
125223 sqlite3DeleteTable(db, pItem->pTab);
125224 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
125225 if( pItem->fg.isUsing ){
125226 sqlite3IdListDelete(db, pItem->u3.pUsing);
125227 }else if( pItem->u3.pOn ){
125228 sqlite3ExprDelete(db, pItem->u3.pOn);
125240 ** pDatabase is NULL if the database name qualifier is missing - the
125255 Token *pAlias, /* The right-hand side of the AS subexpression */
125260 sqlite3 *db = pParse->db;
125261 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
125263 (pOnUsing->pOn ? "ON" : "USING")
125271 assert( p->nSrc>0 );
125272 pItem = &p->a[p->nSrc-1];
125274 assert( pItem->zName==0 || pDatabase!=0 );
125275 if( IN_RENAME_OBJECT && pItem->zName ){
125276 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
125277 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
125280 if( pAlias->n ){
125281 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
125284 pItem->pSelect = pSubquery;
125285 if( pSubquery->selFlags & SF_NestedFrom ){
125286 pItem->fg.isNestedFrom = 1;
125289 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
125290 assert( pItem->fg.isUsing==0 );
125292 pItem->u3.pOn = 0;
125293 }else if( pOnUsing->pUsing ){
125294 pItem->fg.isUsing = 1;
125295 pItem->u3.pUsing = pOnUsing->pUsing;
125297 pItem->u3.pOn = pOnUsing->pOn;
125310 ** element of the source-list passed as the second argument.
125314 if( p && pIndexedBy->n>0 ){
125316 assert( p->nSrc>0 );
125317 pItem = &p->a[p->nSrc-1];
125318 assert( pItem->fg.notIndexed==0 );
125319 assert( pItem->fg.isIndexedBy==0 );
125320 assert( pItem->fg.isTabFunc==0 );
125321 if( pIndexedBy->n==1 && !pIndexedBy->z ){
125324 pItem->fg.notIndexed = 1;
125326 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
125327 pItem->fg.isIndexedBy = 1;
125328 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
125339 assert( p1 && p1->nSrc==1 );
125341 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
125343 sqlite3SrcListDelete(pParse->db, p2);
125346 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
125347 sqlite3DbFree(pParse->db, p2);
125348 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
125356 ** table-valued-function.
125360 SrcItem *pItem = &p->a[p->nSrc-1];
125361 assert( pItem->fg.notIndexed==0 );
125362 assert( pItem->fg.isIndexedBy==0 );
125363 assert( pItem->fg.isTabFunc==0 );
125364 pItem->u1.pFuncArg = pList;
125365 pItem->fg.isTabFunc = 1;
125367 sqlite3ExprListDelete(pParse->db, pList);
125383 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
125388 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
125395 if( p && p->nSrc>1 ){
125396 int i = p->nSrc-1;
125399 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
125400 }while( (--i)>0 );
125401 p->a[0].fg.jointype = 0;
125406 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
125407 i--;
125410 p->a[i].fg.jointype |= JT_LTORJ;
125411 }while( (--i)>=0 );
125425 db = pParse->db;
125433 for(i=0; i<db->nDb; i++){
125435 Btree *pBt = db->aDb[i].pBt;
125460 assert( pParse->db!=0 );
125478 char *zName = sqlite3NameFromToken(pParse->db, pName);
125486 sqlite3DbFree(pParse->db, zName);
125498 sqlite3 *db = pParse->db;
125499 if( db->aDb[1].pBt==0 && !pParse->explain ){
125509 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
125513 pParse->rc = rc;
125516 db->aDb[1].pBt = pBt;
125517 assert( db->aDb[1].pSchema );
125518 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
125529 ** will occur at the end of the top-level VDBE and will be generated
125533 assert( iDb>=0 && iDb<pToplevel->db->nDb );
125534 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
125536 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
125537 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
125538 DbMaskSet(pToplevel->cookieMask, iDb);
125554 sqlite3 *db = pParse->db;
125556 for(i=0; i<db->nDb; i++){
125557 Db *pDb = &db->aDb[i];
125558 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
125580 DbMaskSet(pToplevel->writeMask, iDb);
125581 pToplevel->isMultiWrite |= setStatement;
125593 pToplevel->isMultiWrite = 1;
125614 pToplevel->mayAbort = 1;
125631 assert( pParse->pVdbe!=0 );
125633 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
125652 Table *pTab = pIdx->pTable;
125654 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
125655 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
125656 if( pIdx->aColExpr ){
125657 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
125659 for(j=0; j<pIdx->nKeyCol; j++){
125661 assert( pIdx->aiColumn[j]>=0 );
125662 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
125664 sqlite3_str_appendall(&errMsg, pTab->zName);
125678 ** Code an OP_Halt due to non-unique rowid.
125683 Table *pTab /* The table with the non-unique rowid */
125687 if( pTab->iPKey>=0 ){
125688 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
125689 pTab->aCol[pTab->iPKey].zCnName);
125692 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
125707 for(i=0; i<pIndex->nColumn; i++){
125708 const char *z = pIndex->azColl[i];
125709 assert( z!=0 || pIndex->aiColumn[i]<0 );
125710 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
125727 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
125729 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
125731 sqlite3RefillIndex(pParse, pIndex, -1);
125747 sqlite3 *db = pParse->db; /* The database connection */
125752 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
125754 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
125765 ** REINDEX -- 1
125766 ** REINDEX <collation> -- 2
125767 ** REINDEX ?<database>.?<tablename> -- 3
125768 ** REINDEX ?<database>.?<indexname> -- 4
125783 sqlite3 *db = pParse->db; /* The database connection */
125795 }else if( NEVER(pName2==0) || pName2->z==0 ){
125797 assert( pName1->z );
125798 zColl = sqlite3NameFromToken(pParse->db, pName1);
125812 zDb = db->aDb[iDb].zDbSName;
125823 sqlite3RefillIndex(pParse, pIndex, -1);
125838 int nCol = pIdx->nColumn;
125839 int nKey = pIdx->nKeyCol;
125841 if( pParse->nErr ) return 0;
125842 if( pIdx->uniqNotNull ){
125843 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
125845 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
125850 const char *zColl = pIdx->azColl[i];
125851 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
125853 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
125854 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
125856 if( pParse->nErr ){
125857 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
125858 if( pIdx->bNoQuery==0 ){
125863 ** the missing index using the collation-needed callback. For
125866 pIdx->bNoQuery = 1;
125867 pParse->rc = SQLITE_ERROR_RETRY;
125882 Token *pName, /* Name of the common-table */
125888 sqlite3 *db = pParse->db;
125891 assert( pNew!=0 || db->mallocFailed );
125893 if( db->mallocFailed ){
125897 pNew->pSelect = pQuery;
125898 pNew->pCols = pArglist;
125899 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
125900 pNew->eM10d = eM10d;
125911 sqlite3ExprListDelete(db, pCte->pCols);
125912 sqlite3SelectDelete(db, pCte->pSelect);
125913 sqlite3DbFree(db, pCte->zName);
125936 sqlite3 *db = pParse->db;
125946 zName = pCte->zName;
125949 for(i=0; i<pWith->nCte; i++){
125950 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
125957 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
125962 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
125964 if( db->mallocFailed ){
125968 pNew->a[pNew->nCte++] = *pCte;
125981 for(i=0; i<pWith->nCte; i++){
125982 cteClear(db, &pWith->a[i]);
126014 assert( !db->xCollNeeded || !db->xCollNeeded16 );
126015 if( db->xCollNeeded ){
126018 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
126022 if( db->xCollNeeded16 ){
126025 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
126028 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
126039 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
126044 char *z = pColl->zName;
126049 if( pColl2->xCmp!=0 ){
126051 pColl->xDel = 0; /* Do not copy the destructor */
126070 if( pColl && pColl->xCmp==0 ){
126071 const char *zName = pColl->zName;
126072 sqlite3 *db = pParse->db;
126091 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
126103 pColl = sqlite3HashFind(&db->aCollSeq, zName);
126117 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
126135 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
126160 if( pColl ) pColl += enc-1;
126162 pColl = db->pDfltColl;
126173 db->enc = enc;
126174 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
126177 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
126202 sqlite3 *db = pParse->db;
126208 if( !p || !p->xCmp ){
126215 if( p && !p->xCmp && synthCollSeq(db, p) ){
126218 assert( !p || p->xCmp );
126221 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
126247 sqlite3 *db = pParse->db;
126249 u8 initbusy = db->init.busy;
126253 if( !initbusy && (!pColl || !pColl->xCmp) ){
126266 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
126267 ** is also -1. In other words, we are searching for a function that
126270 ** If nArg is -2 that means that we are searching for any function
126280 ** 4: UTF8/16 conversion required - argument count matches exactly
126281 ** 5: UTF16 byte order conversion required - argument count matches exactly
126284 ** If nArg==(-2) then any function with a non-null xSFunc is
126286 ** a non-match.
126291 int nArg, /* Desired number of arguments. (-1)==any */
126295 assert( p->nArg>=-1 );
126298 if( p->nArg!=nArg ){
126299 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
126300 if( p->nArg>=0 ) return 0;
126305 if( p->nArg==nArg ){
126312 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
126314 }else if( (enc & p->funcFlags & 2)!=0 ){
126330 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
126331 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
126332 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
126355 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
126356 aDef[i].pNext = pOther->pNext;
126357 pOther->pNext = &aDef[i];
126370 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
126378 ** If nArg is -2, then the first valid function found is returned. A
126379 ** function is valid if xSFunc is non-zero. The nArg==(-2)
126381 ** of arguments. If nArg is -2, then createFlag must be 0.
126389 const char *zName, /* Name of the function. zero-terminated */
126390 int nArg, /* Number of arguments. -1 means any number */
126400 assert( nArg>=(-2) );
126401 assert( nArg>=(-1) || createFlag==0 );
126404 /* First search for a match amongst the application-defined functions.
126406 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
126413 p = p->pNext;
126416 /* If no match is found, search the built-in functions.
126418 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
126419 ** functions even if a prior app-defined function was found. And give
126420 ** priority to built-in functions.
126425 ** new function. But the FuncDefs for built-in functions are read-only.
126426 ** So we must not search for built-ins when creating a new function.
126428 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
126438 p = p->pNext;
126450 pBest->zName = (const char*)&pBest[1];
126451 pBest->nArg = (u16)nArg;
126452 pBest->funcFlags = enc;
126454 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
126455 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
126461 pBest->pNext = pOther;
126465 if( pBest && (pBest->xSFunc || createFlag) ){
126487 temp1 = pSchema->tblHash;
126488 temp2 = pSchema->trigHash;
126489 sqlite3HashInit(&pSchema->trigHash);
126490 sqlite3HashClear(&pSchema->idxHash);
126495 sqlite3HashInit(&pSchema->tblHash);
126501 sqlite3HashClear(&pSchema->fkeyHash);
126502 pSchema->pSeqTab = 0;
126503 if( pSchema->schemaFlags & DB_SchemaLoaded ){
126504 pSchema->iGeneration++;
126506 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
126522 }else if ( 0==p->file_format ){
126523 sqlite3HashInit(&p->tblHash);
126524 sqlite3HashInit(&p->idxHash);
126525 sqlite3HashInit(&p->trigHash);
126526 sqlite3HashInit(&p->fkeyHash);
126527 p->enc = SQLITE_UTF8;
126560 ** pSrc->a[0].pTab Pointer to the Table object
126561 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
126565 SrcItem *pItem = pSrc->a;
126567 assert( pItem && pSrc->nSrc>=1 );
126569 if( pItem->pTab ) sqlite3DeleteTable(pParse->db, pItem->pTab);
126570 pItem->pTab = pTab;
126571 pItem->fg.notCte = 1;
126573 pTab->nTabRef++;
126574 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
126581 /* Generate byte-code that will report the number of rows modified
126591 /* Return true if table pTab is read-only.
126593 ** A table is read-only if any of the following are true:
126608 ** is for a top-level SQL statement.
126611 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
126618 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
126621 if( pParse->pToplevel!=0
126622 && pTab->u.vtab.p->eVtabRisk >
126623 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
126626 pTab->zName);
126635 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
126636 db = pParse->db;
126637 if( (pTab->tabFlags & TF_Readonly)!=0 ){
126638 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
126640 assert( pTab->tabFlags & TF_Shadow );
126647 ** If pTab is not writable -> generate an error message and return 1.
126648 ** If pTab is writable but other errors have occurred -> return 1.
126649 ** If pTab is writable and no prior errors -> return 0;
126653 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
126658 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
126660 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
126685 sqlite3 *db = pParse->db;
126686 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
126690 assert( pFrom->nSrc==1 );
126691 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
126692 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
126693 assert( pFrom->a[0].fg.isUsing==0 );
126694 assert( pFrom->a[0].u3.pOn==0 );
126715 SrcList *pSrc, /* the FROM clause -- which tables to scan */
126721 sqlite3 *db = pParse->db;
126733 sqlite3ExprDelete(pParse->db, pWhere);
126734 sqlite3ExprListDelete(pParse->db, pOrderBy);
126754 pTab = pSrc->a[0].pTab;
126763 assert( pPk->nKeyCol>=1 );
126764 if( pPk->nKeyCol==1 ){
126766 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol );
126767 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
126772 for(i=0; i<pPk->nKeyCol; i++){
126774 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol );
126775 p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
126780 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
126787 pSrc->a[0].pTab = 0;
126789 pSrc->a[0].pTab = pTab;
126790 if( pSrc->a[0].fg.isIndexedBy ){
126791 assert( pSrc->a[0].fg.isCte==0 );
126792 pSrc->a[0].u2.pIBIndex = 0;
126793 pSrc->a[0].fg.isIndexedBy = 0;
126794 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
126795 }else if( pSrc->a[0].fg.isCte ){
126796 pSrc->a[0].u2.pCteUse->nUse++;
126863 db = pParse->db;
126864 assert( db->pParse==pParse );
126865 if( pParse->nErr ){
126868 assert( db->mallocFailed==0 );
126869 assert( pTabList->nSrc==1 );
126898 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
126922 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126923 assert( iDb<db->nDb );
126924 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
126925 db->aDb[iDb].zDbSName);
126934 assert( pTabList->nSrc==1 );
126935 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
126936 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
126937 pParse->nTab++;
126943 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
126952 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
126981 if( (db->flags & SQLITE_CountRows)!=0
126982 && !pParse->nested
126983 && !pParse->pTriggerTab
126984 && !pParse->bReturning
126986 memCnt = ++pParse->nMem;
126997 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
127007 && db->xPreUpdateCallback==0
127011 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
127013 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
127014 pTab->zName, P4_STATIC);
127016 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127017 assert( pIdx->pSchema==pTab->pSchema );
127019 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
127021 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
127034 iRowSet = ++pParse->nMem;
127041 nPk = pPk->nKeyCol;
127042 iPk = pParse->nMem+1;
127043 pParse->nMem += nPk;
127044 iEphCur = pParse->nTab++;
127053 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
127054 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
127055 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
127076 assert( pPk->aiColumn[i]>=0 );
127078 pPk->aiColumn[i], iPk+i);
127082 iKey = ++pParse->nMem;
127083 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
127087 /* For ONEPASS, no need to store the rowid/primary-key. There is only
127098 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
127099 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
127105 iKey = ++pParse->nMem;
127108 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
127138 /* Set up a loop over the rowids/primary-keys that were found in the
127139 ** where-clause loop above.
127143 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
127172 pParse->isMultiWrite = 0;
127180 int count = (pParse->nested==0); /* True to count changes */
127185 /* End of the loop over all rowids/primary-keys. */
127196 } /* End non-truncate path */
127202 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
127248 ** cursor number iIdxCur+i for the i-th index.
127284 u8 count, /* If non-zero, increment the row change counter */
127289 Vdbe *v = pParse->pVdbe; /* Vdbe */
127323 iOld = pParse->nMem+1;
127324 pParse->nMem += (1 + pTab->nCol);
127326 /* Populate the OLD.* pseudo-table register array. These values will be
127329 for(iCol=0; iCol<pTab->nCol; iCol++){
127357 iIdxNoSeek = -1;
127370 ** If variable 'count' is non-zero, then this OP_Delete instruction should
127371 ** invoke the update-hook. The pre-update-hook, on the other hand should
127373 ** the update-hook is not invoked for rows removed by REPLACE, but the
127374 ** pre-update-hook is.
127380 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
127424 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
127425 ** index is the 0-th index.)
127439 int r1 = -1; /* Register holding an index key */
127446 v = pParse->pVdbe;
127448 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
127453 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
127457 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
127505 Vdbe *v = pParse->pVdbe;
127511 if( pIdx->pPartIdxWhere ){
127513 pParse->iSelfTab = iDataCur + 1;
127514 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
127516 pParse->iSelfTab = 0;
127517 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
127523 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
127525 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
127528 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
127529 && pPrior->aiColumn[j]!=XN_EXPR
127535 if( pIdx->aiColumn[j]>=0 ){
127553 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
127559 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
127576 ** This file contains the C-language implementations for many of the SQL
127593 assert( context->pVdbe!=0 );
127594 pOp = &context->pVdbe->aOp[context->iOp-1];
127595 assert( pOp->opcode==OP_CollSeq );
127596 assert( pOp->p4type==P4_COLLSEQ );
127597 return pOp->p4.pColl;
127605 assert( context->isError<=0 );
127606 context->isError = -1;
127607 context->skipFlag = 1;
127611 ** Implementation of the non-aggregate min() and max() functions
127624 mask = sqlite3_user_data(context)==0 ? 0 : -1;
127627 assert( mask==-1 || mask==0 );
127649 int i = sqlite3_value_type(argv[0]) - 1;
127657 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
127661 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
127706 sqlite3_result_int(context, (int)(z-z0));
127733 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2;
127755 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
127766 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
127768 ** equivalent positive 64-bit two complement value. */
127769 sqlite3_result_error(context, "integer overflow", -1);
127772 iVal = -iVal;
127778 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
127785 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
127789 if( rVal<0 ) rVal = -rVal;
127856 nHaystack--;
127887 x.nArg = argc-1;
127890 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
127903 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
127904 ** of x. If x is text, then we actually count UTF-8 characters.
127948 ** as substr(X,1,N) - it returns the first N characters of X. This
127949 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
127950 ** from 2009-02-02 for compatibility of applications that exploited the
127957 p2 = -p2;
127961 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
127971 p1--;
127973 p2--;
127976 p1 -= p2;
127986 p1--;
127988 for(z2=z; *z2 && p2; p2--){
127991 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
127995 p2 = len-p1;
128019 /* If Y==0 and X will fit in a 64-bit int,
128023 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
128026 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
128051 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
128052 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
128053 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
128115 #define noopFunc versionFunc /* Substitute function - never called */
128130 ** (or -9223372036854775808) since when you do abs() of that
128135 ** therefore be no less than -9223372036854775807.
128137 r = -(r & LARGEST_INT64);
128177 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
128186 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
128211 /* IMP: R-11217-42568 This function is a wrapper around the
128217 ** A structure defining how to do GLOB-style comparisons.
128229 ** macro for fast reading of the next character in the common case where
128240 /* The correct SQL-92 behavior is for the LIKE operator to ignore
128255 ** Compare two UTF-8 strings for equality where the first string is
128275 ** range of characters can be specified using '-'. Example:
128276 ** "[a-z]" matches any single lower-case letter. To match a '-', make
128299 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
128300 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
128301 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
128318 if( pInfo->matchSet==0 ){
128324 assert( matchOther<0x80 ); /* '[' is a single-byte character */
128326 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
128339 ** For a case-insensitive search, set variable cx to be the same as
128372 if( pInfo->matchSet==0 ){
128392 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
128423 ** non-zero if there is no match.
128436 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
128437 ** a miss - like strcmp().
128461 ** the built-in LIKE operator. The first argument to the function is the
128499 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
128500 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
128501 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
128502 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
128506 /* The escape character string must consist of a single UTF-8 character.
128511 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
128513 "ESCAPE expression must be a single character", -1);
128517 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
128520 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
128521 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
128524 escape = pInfo->matchSet;
128564 /* IMP: R-48699-48617 This function is an SQL wrapper around the
128565 ** sqlite3_libversion() C-interface. */
128566 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
128580 /* IMP: R-24470-31136 This function is an SQL wrapper around the
128582 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
128588 ** its side-effects.
128614 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
128638 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
128642 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
128646 /* Array for converting from half-bytes (nybbles) into ASCII hex
128661 assert( pStr!=0 && pStr->nChar==0 );
128671 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
128688 if( pStr->accError==0 ){
128689 char *zText = pStr->zText;
128699 pStr->nChar = nBlob*2 + 3;
128721 ** Strings are surrounded by single-quotes with escapes on interior quotes
128731 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
128742 ** The unicode() function. Return the integer unicode code-point value
128795 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
128823 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
128829 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
128909 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
128919 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
128932 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
128971 || sqlite3_context_db_handle(context)->mallocFailed );
128991 loopLimit = nStr - nPattern;
128998 nOut += nRep - nPattern;
128999 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
129000 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
129001 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
129007 if( (cntExpand&(cntExpand-1))==0 ){
129012 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
129022 i += nPattern-1;
129025 assert( j+nStr-i+1<=nOut );
129026 memcpy(&zOut[j], &zStr[i], nStr-i);
129027 j += nStr - i;
129082 aLen[nChar] = (unsigned)(z - azChar[nChar]);
129097 nIn -= len;
129105 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
129108 nIn -= len;
129121 ** Return a string value that is the concatenation of all non-null
129137 n += (argc-1)*nSep;
129165 ** concatentation of all non-null arguments.
129179 ** argument. Use the first argument (which must be non-NULL) as the
129190 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
129198 ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
129199 ** When the "sqlite3" command-line shell is built using this functionality,
129201 ** involving application-defined functions to be examined in a generic
129209 /* no-op */
129217 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
129218 ** is only available if the SQLITE_SOUNDEX compile-time option is used
129225 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
129270 /* IMP: R-64894-50321 The string "?000" is returned if the argument
129279 ** A function that loads a shared-library extension then returns NULL.
129290 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
129291 sqlite3_result_error(context, "not authorized", -1);
129301 sqlite3_result_error(context, zErrMsg, -1);
129315 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
129318 u8 approx; /* True if any non-integer value was input to the sum */
129323 ** Do one step of the Kahan-Babushka-Neumaier summation.
129334 volatile double s = pSum->rSum;
129337 pSum->rErr += (s - t) + r;
129339 pSum->rErr += (r - t) + s;
129341 pSum->rSum = t;
129348 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
129351 iBig = iVal - iSm;
129360 ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
129366 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
129368 p->rSum = (double)(iVal - iSm);
129369 p->rErr = (double)iSm;
129371 p->rSum = (double)iVal;
129372 p->rErr = 0.0;
129394 p->cnt++;
129395 if( p->approx==0 ){
129397 kahanBabuskaNeumaierInit(p, p->iSum);
129398 p->approx = 1;
129401 i64 x = p->iSum;
129403 p->iSum = x;
129405 p->ovrfl = 1;
129406 kahanBabuskaNeumaierInit(p, p->iSum);
129407 p->approx = 1;
129415 p->ovrfl = 0;
129429 /* p is always non-NULL because sumStep() will have been called first
129432 assert( p->cnt>0 );
129433 p->cnt--;
129434 if( !p->approx ){
129435 p->iSum -= sqlite3_value_int64(argv[0]);
129439 kahanBabuskaNeumaierStepInt64(p, -iVal);
129445 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
129455 if( p && p->cnt>0 ){
129456 if( p->approx ){
129457 if( p->ovrfl ){
129458 sqlite3_result_error(context,"integer overflow",-1);
129459 }else if( !sqlite3IsOverflow(p->rErr) ){
129460 sqlite3_result_double(context, p->rSum+p->rErr);
129462 sqlite3_result_double(context, p->rSum);
129465 sqlite3_result_int64(context, p->iSum);
129472 if( p && p->cnt>0 ){
129474 if( p->approx ){
129475 r = p->rSum;
129476 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
129478 r = (double)(p->iSum);
129480 sqlite3_result_double(context, r/(double)p->cnt);
129488 if( p->approx ){
129489 r = p->rSum;
129490 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
129492 r = (double)(p->iSum);
129517 p->n++;
129524 ** expressed as a 32-bit integer. */
129525 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
129526 || p->n==sqlite3_aggregate_count(context) );
129532 sqlite3_result_int64(context, p ? p->n : 0);
129538 /* p is always non-NULL since countStep() will have been called first */
129540 p->n--;
129542 p->bInverse = 1;
129566 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
129567 }else if( pBest->flags ){
129574 ** sqlite3_user_data() function returns (void *)-1. For min() it
129587 pBest->db = sqlite3_context_db_handle(context);
129595 if( pRes->flags ){
129627 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
129629 ** (Hence, its slots in use number nAccum-1 between method calls.)
129650 int firstTerm = pGCC->str.mxAlloc==0;
129651 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
129654 sqlite3_str_appendchar(&pGCC->str, 1, ',');
129658 pGCC->nFirstSepLength = 1;
129665 sqlite3_str_append(&pGCC->str, zSep, nSep);
129671 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
129672 int *pnsl = pGCC->pnSepLengths;
129675 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
129677 int i = 0, nA = pGCC->nAccum-1;
129678 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
129681 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
129684 if( ALWAYS(pGCC->nAccum>0) ){
129685 pnsl[pGCC->nAccum-1] = nSep;
129687 pGCC->pnSepLengths = pnsl;
129689 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
129696 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
129698 pGCC->nAccum += 1;
129702 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
129717 /* pGCC is always non-NULL since groupConcatStep() will have always
129725 pGCC->nAccum -= 1;
129726 if( pGCC->pnSepLengths!=0 ){
129727 assert(pGCC->nAccum >= 0);
129728 if( pGCC->nAccum>0 ){
129729 nVS += *pGCC->pnSepLengths;
129730 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
129731 (pGCC->nAccum-1)*sizeof(int));
129734 /* If removing single accumulated string, harmlessly over-do. */
129735 nVS += pGCC->nFirstSepLength;
129737 if( nVS>=(int)pGCC->str.nChar ){
129738 pGCC->str.nChar = 0;
129740 pGCC->str.nChar -= nVS;
129741 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
129743 if( pGCC->str.nChar==0 ){
129744 pGCC->str.mxAlloc = 0;
129745 sqlite3_free(pGCC->pnSepLengths);
129746 pGCC->pnSepLengths = 0;
129757 sqlite3ResultStrAccum(context, &pGCC->str);
129759 sqlite3_free(pGCC->pnSepLengths);
129768 StrAccum *pAccum = &pGCC->str;
129769 if( pAccum->accError==SQLITE_TOOBIG ){
129771 }else if( pAccum->accError==SQLITE_NOMEM ){
129775 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
129784 ** This routine does per-connection function registration. Most
129785 ** of the built-in functions above are part of the global function set.
129797 ** Re-register the built-in LIKE functions. The caseSensitive
129817 pDef->funcFlags |= flags;
129818 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
129827 ** LIKE-style function then return FALSE.
129843 assert( pExpr->op==TK_FUNCTION );
129845 if( !pExpr->x.pList ){
129848 nExpr = pExpr->x.pList->nExpr;
129850 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
129854 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
129862 memcpy(aWc, pDef->pUserData, 3);
129870 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
129872 if( pEscape->op!=TK_STRING ) return 0;
129874 zEscape = pEscape->u.zToken;
129881 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
129897 /* Extra math functions that require linking with -lm
129955 ** ln(X) - natural logarithm
129956 ** log(X) - log X base 10
129957 ** log10(X) - log X base 10
129958 ** log(B,X) - log X base B
130012 ** Implementation of 1-argument SQL math functions:
130014 ** exp(X) - Compute e to the X-th power
130034 ** Implementation of 2-argument SQL math functions:
130036 ** power(X,Y) - Compute X to the Y-th power
130059 ** Implementation of 0-argument pi() function.
130088 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
130118 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
130124 ** to the global function hash table. This occurs at start-time (as
130135 ** FuncDef.pHash elements at start-time. The elements of this array
130136 ** are read-only after initialization is complete.
130175 FUNCTION(min, -1, 0, 1, minmaxFunc ),
130179 FUNCTION(max, -1, 1, 1, minmaxFunc ),
130188 FUNCTION(printf, -1, 0, 0, printfFunc ),
130189 FUNCTION(format, -1, 0, 0, printfFunc ),
130191 FUNCTION(char, -1, 0, 0, charFunc ),
130205 FUNCTION(concat, -1, 0, 0, concatFunc ),
130207 FUNCTION(concat_ws, -1, 0, 0, concatwsFunc ),
130251 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
130292 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
130303 #if 0 /* Enable to print out how the built-in functions are hashed */
130308 printf("FUNC-HASH %02d:", i);
130309 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
130310 int n = sqlite3Strlen30(p->zName);
130311 int h = p->zName[0] + n;
130312 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
130313 printf(" %s(%d)", p->zName, h);
130343 ** --------------------------
130425 ** ---------------
130427 ** Before coding an UPDATE or DELETE row operation, the code-generator
130432 ** accessed). No information is required by the code-generator before
130436 ** sqlite3FkRequired() - Test to see if FK processing is required.
130437 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
130441 ** --------------------------------------
130443 ** sqlite3FkCheck() - Check for foreign key violations.
130444 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
130445 ** sqlite3FkDelete() - Delete an FKey structure.
130450 ** -----------------------
130480 ** constraint to the parent table column stored in the left-most column
130482 ** child table column that corresponds to the second left-most column of
130501 ** then non-zero is returned, and a "foreign key mismatch" error loaded
130502 ** into pParse. If an OOM error occurs, non-zero is returned and the
130503 ** pParse->db->mallocFailed flag is set.
130514 int nCol = pFKey->nCol; /* Number of columns in parent key */
130515 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
130522 /* If this is a non-composite (single column) foreign key, check if it
130528 ** Non-composite foreign keys do not require the aiCol array.
130538 if( pParent->iPKey>=0 ){
130540 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
130546 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
130551 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
130552 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
130564 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
130569 /* If zKey is non-NULL, then this foreign key was declared to
130575 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
130584 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
130586 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
130588 zIdxCol = pParent->aCol[iCol].zCnName;
130590 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
130591 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
130603 if( !pParse->disableTriggers ){
130605 "foreign key mismatch - \"%w\" referencing \"%w\"",
130606 pFKey->pFrom->zName, pFKey->zTo);
130608 sqlite3DbFree(pParse->db, aiCol);
130620 ** affected - once to "delete" the old row, and then again to "insert" the
130630 ** --------------------------------------------------------------------------
130655 int iCur = pParse->nTab - 1; /* Cursor number to use */
130659 (!pFKey->isDeferred
130660 && !(pParse->db->flags & SQLITE_DeferFKs)
130661 && !pParse->pToplevel
130662 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
130672 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
130675 for(i=0; i<pFKey->nCol; i++){
130676 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
130693 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
130698 ** to increment the constraint-counter (i.e. this is an INSERT operation),
130700 ** increment the constraint-counter. */
130701 if( pTab==pFKey->pFrom && nIncr==1 ){
130709 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
130713 int nCol = pFKey->nCol;
130716 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
130720 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
130725 ** to increment the constraint-counter (i.e. this is an INSERT operation),
130727 ** increment the constraint-counter.
130729 ** If any of the parent-key values are NULL, then the row cannot match
130731 ** of the parent-key values are NULL (at this point it is known that
130734 if( pTab==pFKey->pFrom && nIncr==1 ){
130737 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
130740 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
130741 pIdx->aiColumn[i]);
130742 assert( pIdx->aiColumn[i]>=0 );
130743 assert( aiCol[i]!=pTab->iPKey );
130744 if( pIdx->aiColumn[i]==pTab->iPKey ){
130755 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
130762 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
130763 && !pParse->pToplevel
130764 && !pParse->isMultiWrite
130774 if( nIncr>0 && pFKey->isDeferred==0 ){
130777 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
130802 sqlite3 *db = pParse->db;
130806 if( iCol>=0 && iCol!=pTab->iPKey ){
130807 pCol = &pTab->aCol[iCol];
130808 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
130809 pExpr->affExpr = pCol->affinity;
130811 if( zColl==0 ) zColl = db->pDfltColl->zName;
130814 pExpr->iTable = regBase;
130815 pExpr->affExpr = SQLITE_AFF_INTEGER;
130834 pExpr->y.pTab = pTab;
130835 pExpr->iTable = iCursor;
130836 pExpr->iColumn = iCol;
130845 ** code for an SQL UPDATE operation, this function may be called twice -
130848 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
130857 ** --------------------------------------------------------------------------
130879 sqlite3 *db = pParse->db; /* Database handle */
130887 assert( pIdx==0 || pIdx->pTable==pTab );
130888 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
130889 assert( pIdx!=0 || pFKey->nCol==1 );
130893 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
130899 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
130905 for(i=0; i<pFKey->nCol; i++){
130912 iCol = pIdx ? pIdx->aiColumn[i] : -1;
130914 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
130916 zCol = pFKey->pFrom->aCol[iCol].zCnName;
130936 if( pTab==pFKey->pFrom && nIncr>0 ){
130941 pLeft = exprTableRegister(pParse, pTab, regData, -1);
130942 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
130947 for(i=0; i<pIdx->nKeyCol; i++){
130948 i16 iCol = pIdx->aiColumn[i];
130951 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
130969 if( pParse->nErr==0 ){
130971 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
130999 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
131005 ** and all of its sub-components.
131007 ** The Trigger structure or any of its sub-components may be allocated from
131012 TriggerStep *pStep = p->step_list;
131013 sqlite3ExprDelete(dbMem, pStep->pWhere);
131014 sqlite3ExprListDelete(dbMem, pStep->pExprList);
131015 sqlite3SelectDelete(dbMem, pStep->pSelect);
131016 sqlite3ExprDelete(dbMem, p->pWhen);
131028 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
131033 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
131034 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
131035 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
131058 sqlite3 *db = pParse->db;
131059 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
131072 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
131073 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
131080 pParse->disableTriggers = 1;
131082 pParse->disableTriggers = 0;
131093 if( (db->flags & SQLITE_DeferFKs)==0 ){
131114 ** is set to -1). If the rowid column is modified by the UPDATE statement
131115 ** the bChngRowid argument is non-zero.
131127 for(i=0; i<p->nCol; i++){
131128 int iChildKey = p->aCol[i].iFrom;
131130 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
131141 ** is set to -1). If the rowid column is modified by the UPDATE statement
131142 ** the bChngRowid argument is non-zero.
131154 for(i=0; i<p->nCol; i++){
131155 char *zKey = p->aCol[i].zCol;
131157 for(iKey=0; iKey<pTab->nCol; iKey++){
131158 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
131159 Column *pCol = &pTab->aCol[iKey];
131161 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
131162 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
131178 if( pTop->pTriggerPrg ){
131179 Trigger *p = pTop->pTriggerPrg->pTrigger;
131180 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
131181 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
131183 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
131196 ** first register in an array of (pTab->nCol+1) registers containing the
131202 ** first register of an array of (pTab->nCol+1) registers containing the new
131218 sqlite3 *db = pParse->db; /* Database handle */
131222 int isIgnoreErrors = pParse->disableTriggers;
131224 /* Exactly one of regOld and regNew should be non-zero. */
131227 /* If foreign-keys are disabled, this function is a no-op. */
131228 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
131231 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
131232 zDb = db->aDb[iDb].zDbSName;
131236 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
131246 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
131256 if( pParse->disableTriggers ){
131257 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
131259 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
131263 if( !isIgnoreErrors || db->mallocFailed ) return;
131270 ** FK counter for each row of the current table with non-NULL keys.
131273 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
131274 for(i=0; i<pFKey->nCol; i++){
131276 iFromCol = pFKey->aCol[i].iFrom;
131277 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
131280 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
131284 assert( pFKey->nCol==1 || (aiFree && pIdx) );
131289 iCol = pFKey->aCol[0].iFrom;
131292 for(i=0; i<pFKey->nCol; i++){
131293 if( aiCol[i]==pTab->iPKey ){
131294 aiCol[i] = -1;
131296 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
131301 if( db->xAuth ){
131303 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
131304 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
131310 /* Take a shared-cache advisory read-lock on the parent table. Allocate
131313 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
131314 pParse->nTab++;
131320 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
131339 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
131348 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
131349 && !pParse->pToplevel && !pParse->isMultiWrite
131358 if( !isIgnoreErrors || db->mallocFailed ) return;
131361 assert( aiCol || pFKey->nCol==1 );
131367 SrcItem *pItem = pSrc->a;
131368 pItem->pTab = pFKey->pFrom;
131369 pItem->zName = pFKey->pFrom->zName;
131370 pItem->pTab->nTabRef++;
131371 pItem->iCursor = pParse->nTab++;
131374 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
131377 int eAction = pFKey->aAction[aChange!=0];
131378 if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None;
131384 ** So do not set the "may-abort" flag in this case.
131387 ** may-abort flag will eventually be set on this statement anyway
131398 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
131402 pItem->zName = 0;
131420 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
131423 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
131424 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
131426 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
131430 for(i=0; i<pIdx->nKeyCol; i++){
131431 assert( pIdx->aiColumn[i]>=0 );
131432 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
131447 ** entry in the aChange[] array is set to -1. If the column is modified,
131452 ** non-zero. If there is no foreign key related processing, this function
131468 int *aChange, /* Non-NULL for UPDATE operations */
131473 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
131478 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
131485 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
131487 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
131493 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
131495 if( (pParse->db->flags & SQLITE_FkNoAction)==0
131496 && p->aAction[1]!=OE_None
131510 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
131518 ** require no special handling by the triggers sub-system, code for them is
131541 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
131543 sqlite3 *db = pParse->db; /* Database handle */
131548 action = pFKey->aAction[iAction];
131549 if( (db->flags & SQLITE_FkNoAction) ) action = OE_None;
131550 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
131553 pTrigger = pFKey->apTrigger[iAction];
131559 int *aiCol = 0; /* child table cols -> parent key cols */
131568 assert( aiCol || pFKey->nCol==1 );
131570 for(i=0; i<pFKey->nCol; i++){
131578 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
131580 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
131581 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
131583 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
131584 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
131622 Column *pCol = pFKey->pFrom->aCol + iFromCol;
131624 if( pCol->colFlags & COLFLAG_GENERATED ){
131625 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
131626 testcase( pCol->colFlags & COLFLAG_STORED );
131629 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
131645 zFrom = pFKey->pFrom->zName;
131649 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
131655 pRaise->affExpr = OE_Abort;
131659 assert( pSrc->nSrc==1 );
131660 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
131661 pSrc->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
131678 nFrom + 1 /* Space for pStep->zTarget */
131681 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
131682 pStep->zTarget = (char *)&pStep[1];
131683 memcpy((char *)pStep->zTarget, zFrom, nFrom);
131685 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
131686 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
131687 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
131690 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
131694 /* Re-enable the lookaside buffer, if it was disabled earlier. */
131701 if( db->mallocFailed==1 ){
131710 pStep->op = TK_SELECT;
131714 pStep->op = TK_DELETE;
131719 pStep->op = TK_UPDATE;
131721 pStep->pTrig = pTrigger;
131722 pTrigger->pSchema = pTab->pSchema;
131723 pTrigger->pTabSchema = pTab->pSchema;
131724 pFKey->apTrigger[iAction] = pTrigger;
131725 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
131738 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
131743 /* If foreign-key support is enabled, iterate through all FKs that
131746 ** trigger sub-program. */
131747 if( pParse->db->flags&SQLITE_ForeignKeys ){
131749 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
131769 FKey *pNext; /* Copy of pFKey->pNextFrom */
131773 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
131774 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
131777 if( db->pnBytesFreed==0 ){
131778 if( pFKey->pPrevTo ){
131779 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
131781 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
131782 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
131784 if( pFKey->pNextTo ){
131785 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
131789 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
131792 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
131796 fkTriggerDelete(db, pFKey->apTrigger[0]);
131797 fkTriggerDelete(db, pFKey->apTrigger[1]);
131800 pNext = pFKey->pNextFrom;
131842 assert( pParse->pVdbe!=0 );
131843 v = pParse->pVdbe;
131845 if( !pParse->db->noSharedCache ){
131846 sqlite3TableLock(pParse, iDb, pTab->tnum,
131847 (opcode==OP_OpenWrite)?1:0, pTab->zName);
131850 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
131851 VdbeComment((v, "%s", pTab->zName));
131855 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
131856 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
131858 VdbeComment((v, "%s", pTab->zName));
131868 ** ------------------------------
131892 Table *pTab = pIdx->pTable;
131893 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
131894 if( !pIdx->zColAff ){
131898 for(n=0; n<pIdx->nColumn; n++){
131899 i16 x = pIdx->aiColumn[n];
131902 aff = pTab->aCol[x].affinity;
131907 assert( pIdx->bHasExpr );
131908 assert( pIdx->aColExpr!=0 );
131909 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
131913 pIdx->zColAff[n] = aff;
131915 pIdx->zColAff[n] = 0;
131916 return pIdx->zColAff;
131919 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
131920 return pIdx->zColAff;
131931 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
131934 for(i=j=0; i<pTab->nCol; i++){
131935 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
131936 zColAff[j++] = pTab->aCol[i].affinity;
131940 zColAff[j--] = 0;
131950 ** For ordinary (legacy, non-strict) tables:
131951 ** -----------------------------------------
131957 ** which were then optimized out) then this routine becomes a no-op.
131967 ** --------- ---------------
131975 ** ------------------
131989 if( pTab->tabFlags & TF_Strict ){
131998 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
131999 pPrev->opcode = OP_TypeCheck;
132000 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
132003 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
132008 zColAff = pTab->zColAff;
132015 pTab->zColAff = zColAff;
132023 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
132024 || sqlite3VdbeDb(v)->mallocFailed );
132025 sqlite3VdbeChangeP4(v, -1, zColAff, i);
132031 ** Return non-zero if the table pTab in database iDb or any of its indices
132041 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
132047 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
132049 Pgno tnum = pOp->p2;
132050 if( tnum==pTab->tnum ){
132053 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
132054 if( tnum==pIndex->tnum ){
132060 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
132061 assert( pOp->p4.pVtab!=0 );
132062 assert( pOp->p4type==P4_VTAB );
132074 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
132075 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
132076 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
132100 assert( pTab->tabFlags & TF_HasGenerated );
132101 testcase( pTab->tabFlags & TF_HasVirtual );
132102 testcase( pTab->tabFlags & TF_HasStored );
132107 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
132108 if( (pTab->tabFlags & TF_HasStored)!=0 ){
132109 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
132110 if( pOp->opcode==OP_Affinity ){
132112 ** columns. '@' is the no-op affinity and those columns have not
132115 char *zP4 = pOp->p4.z;
132117 assert( pOp->p4type==P4_DYNAMIC );
132119 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
132122 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
132127 }else if( pOp->opcode==OP_TypeCheck ){
132131 pOp->p3 = 1;
132136 ** this is a two-pass algorithm. On the first pass, mark all generated
132139 for(i=0; i<pTab->nCol; i++){
132140 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
132141 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
132142 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
132143 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
132152 /* On the second pass, compute the value of each NOT-AVAILABLE column.
132157 pParse->iSelfTab = -iRegStore;
132161 for(i=0; i<pTab->nCol; i++){
132162 Column *pCol = pTab->aCol + i;
132163 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
132165 pCol->colFlags |= COLFLAG_BUSY;
132168 pCol->colFlags &= ~COLFLAG_BUSY;
132174 assert( pCol->colFlags & COLFLAG_GENERATED );
132177 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
132182 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
132184 pParse->iSelfTab = 0;
132219 assert( pParse->db->aDb[iDb].pSchema!=0 );
132220 if( (pTab->tabFlags & TF_Autoincrement)!=0
132221 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
132225 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
132229 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
132233 || pSeqTab->nCol!=2
132235 pParse->nErr++;
132236 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
132240 pInfo = pToplevel->pAinc;
132241 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
132243 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
132245 testcase( pParse->earlyCleanup );
132246 if( pParse->db->mallocFailed ) return 0;
132247 pInfo->pNext = pToplevel->pAinc;
132248 pToplevel->pAinc = pInfo;
132249 pInfo->pTab = pTab;
132250 pInfo->iDb = iDb;
132251 pToplevel->nMem++; /* Register to hold name of table */
132252 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
132253 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
132255 memId = pInfo->regCtr;
132266 sqlite3 *db = pParse->db; /* The database connection */
132269 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
132271 /* This routine is never called during trigger-generation. It is
132272 ** only called from the top-level */
132273 assert( pParse->pTriggerTab==0 );
132277 for(p = pParse->pAinc; p; p = p->pNext){
132294 pDb = &db->aDb[p->iDb];
132295 memId = p->regCtr;
132296 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
132297 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
132298 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
132304 aOp[3].p1 = memId-1;
132313 if( pParse->nTab==0 ) pParse->nTab = 1;
132327 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
132340 Vdbe *v = pParse->pVdbe;
132341 sqlite3 *db = pParse->db;
132344 for(p = pParse->pAinc; p; p = p->pNext){
132354 Db *pDb = &db->aDb[p->iDb];
132356 int memId = p->regCtr;
132359 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
132362 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
132367 aOp[2].p1 = memId-1;
132376 if( pParse->pAinc ) autoIncrementEnd(pParse);
132381 ** above are all no-ops
132405 ** then a list of all (non-hidden) columns for the table is substituted.
132410 ** first two forms shown above. A VALUES clause is really just short-hand
132416 ** insert with data coming from a single-row VALUES clause, the code executes
132417 ** once straight down through. Pseudo-code follows (we call this
132429 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
132453 ** X <- A
132461 ** end-coroutine X
132474 ** X <- A
132482 ** end co-routine R
132511 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
132540 db = pParse->db;
132541 assert( db->pParse==pParse );
132542 if( pParse->nErr ){
132545 assert( db->mallocFailed==0 );
132552 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
132553 pList = pSelect->pEList;
132554 pSelect->pEList = 0;
132561 assert( pTabList->nSrc==1 );
132566 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132567 assert( iDb<db->nDb );
132568 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
132569 db->aDb[iDb].zDbSName) ){
132594 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
132600 ** ViewGetColumnNames() is a no-op if pTab is not a view.
132606 /* Cannot insert into a read-only table.
132616 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
132625 ** very fast and which reduce fragmentation of indices.
132648 regRowid = regIns = pParse->nMem+1;
132649 pParse->nMem += pTab->nCol + 1;
132652 pParse->nMem++;
132665 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
132666 ** loop, if ipkColumn==(-1), that means that integer primary key
132675 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
132677 assert( pColumn->eU4!=EU4_EXPR );
132678 pColumn->eU4 = EU4_IDX;
132679 for(i=0; i<pColumn->nId; i++){
132680 pColumn->a[i].u4.idx = -1;
132682 for(i=0; i<pColumn->nId; i++){
132683 for(j=0; j<pTab->nCol; j++){
132684 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
132685 pColumn->a[i].u4.idx = j;
132687 if( j==pTab->iPKey ){
132691 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
132694 pTab->aCol[j].zCnName);
132701 if( j>=pTab->nCol ){
132702 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
132707 pTabList->a, pColumn->a[i].zName);
132708 pParse->checkSchema = 1;
132716 ** is coming from a SELECT statement, then generate a co-routine that
132718 ** co-routine is the common header to the 3rd and 4th templates.
132721 /* Data is coming from a SELECT or from a multi-row VALUES clause.
132722 ** Generate a co-routine to run the SELECT. */
132723 int regYield; /* Register holding co-routine entry-point */
132724 int addrTop; /* Top of the co-routine */
132727 regYield = ++pParse->nMem;
132732 dest.nSdst = pTab->nCol;
132735 assert( db->pParse==pParse );
132736 if( rc || pParse->nErr ) goto insert_cleanup;
132737 assert( db->mallocFailed==0 );
132739 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
132740 assert( pSelect->pEList );
132741 nColumn = pSelect->pEList->nExpr;
132771 srcTab = pParse->nTab++;
132786 ** single-row VALUES clause
132791 srcTab = -1;
132794 nColumn = pList->nExpr;
132808 ipkColumn = pTab->iPKey;
132810 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
132811 testcase( pTab->tabFlags & TF_HasVirtual );
132812 testcase( pTab->tabFlags & TF_HasStored );
132813 for(i=ipkColumn-1; i>=0; i--){
132814 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
132815 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
132816 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
132817 ipkColumn--;
132829 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
132830 for(i=0; i<pTab->nCol; i++){
132831 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
132834 if( nColumn!=(pTab->nCol-nHidden) ){
132837 pTabList->a, pTab->nCol-nHidden, nColumn);
132841 if( pColumn!=0 && nColumn!=pColumn->nId ){
132842 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
132848 if( (db->flags & SQLITE_CountRows)!=0
132849 && !pParse->nested
132850 && !pParse->pTriggerTab
132851 && !pParse->bReturning
132853 regRowCount = ++pParse->nMem;
132860 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
132866 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
132868 aRegIdx[i] = ++pParse->nMem;
132869 pParse->nMem += pIdx->nColumn;
132871 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
132878 pTab->zName);
132885 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
132888 pTabList->a[0].iCursor = iDataCur;
132891 pNx->pUpsertSrc = pTabList;
132892 pNx->regData = regData;
132893 pNx->iDataCur = iDataCur;
132894 pNx->iIdxCur = iIdxCur;
132895 if( pNx->pUpsertTarget ){
132900 pNx = pNx->pNextUpsert;
132928 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
132932 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
132934 ** the value does not get overwritten by a NULL at tag-20191021-002. */
132948 for(i=0; i<pTab->nCol; i++, iRegStore++){
132952 if( i==pTab->iPKey ){
132953 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
132956 ** NULL - we cannot optimize further by skipping the column completely */
132960 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
132966 iRegStore--;
132970 ** triggers, the slots used for stored columns will be OP_Copy-ed
132981 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
132987 assert( pColumn->eU4==EU4_IDX );
132988 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
132989 if( j>=pColumn->nId ){
132993 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
133001 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
133005 k = i - nHidden;
133015 Expr *pX = pList->a[k].pExpr;
133029 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
133035 ** not happened yet) so we substitute a rowid of -1
133038 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
133046 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
133049 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
133055 assert( pTab->nNVCol>0 || pParse->nErr>0 );
133056 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
133063 if( pTab->tabFlags & TF_HasGenerated ){
133064 testcase( pTab->tabFlags & TF_HasVirtual );
133065 testcase( pTab->tabFlags & TF_HasStored );
133081 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
133083 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
133096 /* Rowid already initialized at tag-20191021-001 */
133098 Expr *pIpk = pList->a[ipkColumn].pExpr;
133099 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
133103 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
133134 if( pTab->tabFlags & TF_HasGenerated ){
133146 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
133157 if( db->flags & SQLITE_ForeignKeys ){
133175 }else if( pParse->bReturning ){
133177 ** constant value -1, in case one or more of the returned expressions
133179 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
133192 pTab, regData-2-pTab->nCol, onError, endOfLoop);
133209 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
133210 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
133224 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
133260 ** Meanings of bits in of pWalker->eCode for
133267 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
133272 if( pExpr->op==TK_COLUMN ){
133273 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
133274 if( pExpr->iColumn>=0 ){
133275 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
133276 pWalker->eCode |= CKCNSTRNT_COLUMN;
133279 pWalker->eCode |= CKCNSTRNT_ROWID;
133286 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
133295 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
133296 ** The operation of this routine is the same - return true if an only if
133324 ** the indexes of a table in the order provided in the Table->pIndex list.
133325 ** However, sometimes (rarely - when there is an upsert) it wants to visit
133359 assert( pIter->i==0 );
133360 if( pIter->eType ){
133361 *pIx = pIter->u.ax.aIdx[0].ix;
133362 return pIter->u.ax.aIdx[0].p;
133365 return pIter->u.lx.pIdx;
133371 if( pIter->eType ){
133372 int i = ++pIter->i;
133373 if( i>=pIter->u.ax.nIdx ){
133377 *pIx = pIter->u.ax.aIdx[i].ix;
133378 return pIter->u.ax.aIdx[i].p;
133381 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
133382 return pIter->u.lx.pIdx;
133392 ** pTab->nCol+1 registers in this range. The first register (the one
133419 ** at pTab->pIndex.
133421 ** (2019-05-07) The generated code also creates a new record for the
133423 ** register identified by aRegIdx[nIdx] - in other words in the first
133434 ** for the first index in the pTab->pIndex list. Cursors for other indices
133435 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
133443 ** --------------- ---------- ----------------------------------------
133473 ** Or if overrideError==OE_Default, then the pParse->onError parameter
133474 ** is used. Or if pParse->onError==OE_Default then the onError value
133485 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
133519 db = pParse->db;
133520 v = pParse->pVdbe;
133523 nCol = pTab->nCol;
133534 nPkField = pPk->nKeyCol;
133543 if( pTab->tabFlags & TF_HasNotNull ){
133550 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
133551 int isGenerated; /* non-zero if column is generated */
133552 onError = pCol->notNull;
133554 if( i==pTab->iPKey ){
133557 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
133573 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
133575 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
133576 testcase( pCol->colFlags & COLFLAG_STORED );
133577 testcase( pCol->colFlags & COLFLAG_GENERATED );
133593 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
133605 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
133606 pCol->zCnName);
133607 testcase( zMsg==0 && db->mallocFailed==0 );
133632 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
133640 } /* end of 2-pass loop */
133641 } /* end if( has-not-null-constraints ) */
133646 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133647 ExprList *pCheck = pTab->pCheck;
133648 pParse->iSelfTab = -(regNewData+1);
133650 for(i=0; i<pCheck->nExpr; i++){
133653 Expr *pExpr = pCheck->a[i].pExpr;
133668 if( !db->mallocFailed ){
133675 char *zName = pCheck->a[i].zEName;
133676 assert( zName!=0 || pParse->db->mallocFailed );
133677 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
133684 pParse->iSelfTab = 0;
133701 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
133719 sIdxIter.u.lx.pIdx = pTab->pIndex;
133721 if( pUpsert->pUpsertTarget==0 ){
133722 /* There is just on ON CONFLICT clause and it has no constraint-target */
133723 assert( pUpsert->pNextUpsert==0 );
133724 if( pUpsert->isDoUpdate==0 ){
133725 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
133733 }else if( pTab->pIndex!=0 ){
133741 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
133750 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
133751 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
133752 if( pTerm->pUpsertTarget==0 ) break;
133753 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
133755 pIdx = pTab->pIndex;
133756 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
133757 pIdx = pIdx->pNext;
133766 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
133796 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
133802 if( db->flags&SQLITE_RecTriggers ){
133812 regTrigCnt = ++pParse->nMem;
133827 onError = pTab->keyConf;
133838 if( pUpsertClause->isDoUpdate==0 ){
133859 && pTab->pIndex /* There exist other constraints */
133898 ** recursive-triggers flag is set, call GenerateRowDelete() to
133900 ** the triggers and remove both the table and index b-tree entries.
133902 ** Otherwise, if there are no triggers or the recursive-triggers
133904 ** GenerateRowIndexDelete(). This removes the index b-tree entries
133905 ** only. The table b-tree entry will be replaced by the new entry
133922 regNewData, 1, 0, OE_Replace, 1, -1);
133928 /* This OP_Delete opcode fires the pre-update-hook only. It does
133929 ** not modify the b-tree. It is more efficient to let the coming
133935 if( pTab->pIndex ){
133937 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
133960 sqlite3VdbeJumpHere(v, ipkTop-1);
133993 VdbeNoopComment((v, "prep index %s", pIdx->zName));
133998 if( pIdx->pPartIdxWhere ){
134000 pParse->iSelfTab = -(regNewData+1);
134001 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
134003 pParse->iSelfTab = 0;
134010 for(i=0; i<pIdx->nColumn; i++){
134011 int iField = pIdx->aiColumn[i];
134014 pParse->iSelfTab = -(regNewData+1);
134015 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
134016 pParse->iSelfTab = 0;
134017 VdbeComment((v, "%s column %d", pIdx->zName, i));
134018 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
134026 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
134029 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
134030 VdbeComment((v, "for %s", pIdx->zName));
134032 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
134033 sqlite3SetMakeRecordP5(v, pIdx->pTable);
134036 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
134048 onError = pIdx->onError;
134061 if( pUpsertClause->isDoUpdate==0 ){
134076 ** must be explicitly deleted in order to ensure any pre-update hook
134080 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
134083 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
134085 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
134086 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
134097 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
134105 ** is different from old-rowid */
134114 ** store it in registers regR..regR+nPk-1 */
134116 for(i=0; i<pPk->nKeyCol; i++){
134117 assert( pPk->aiColumn[i]>=0 );
134118 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
134120 VdbeComment((v, "%s.%s", pTab->zName,
134121 pTab->aCol[pPk->aiColumn[i]].zCnName));
134132 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
134136 for(i=0; i<pPk->nKeyCol; i++){
134137 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
134138 x = pPk->aiColumn[i];
134140 if( i==(pPk->nKeyCol-1) ){
134184 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
134185 assert( nConflictCk>0 || db->mallocFailed );
134213 if( pIdx->pPartIdxWhere ){
134216 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
134220 ** the constraint-ok jump destination to be the address of
134241 nConflictCk--;
134296 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
134310 ** to be the number of columns in table pTab that must not be NULL-trimmed.
134312 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
134318 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
134319 if( pTab->pSchema->file_format<2 ) return;
134321 for(i=pTab->nCol-1; i>0; i--){
134322 if( pTab->aCol[i].iDflt!=0 ) break;
134323 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
134332 ** PK index. This function adds code to invoke the pre-update hook,
134342 Vdbe *v = pParse->pVdbe;
134345 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
134385 v = pParse->pVdbe;
134388 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
134390 assert( pIdx->onError!=OE_Replace
134391 || pIdx->pNext==0
134392 || pIdx->pNext->onError==OE_Replace );
134394 if( pIdx->pPartIdxWhere ){
134408 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
134412 if( pParse->nested ){
134425 if( !pParse->nested ){
134441 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
134447 ** pTab->pIndex list.
134449 ** If pTab is a virtual table, then this routine is a no-op and the
134473 /* This routine is a no-op for virtual tables. Leave the output
134476 *piDataCur = *piIdxCur = -999;
134479 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
134480 v = pParse->pVdbe;
134482 if( iBase<0 ) iBase = pParse->nTab;
134487 }else if( pParse->db->noSharedCache==0 ){
134488 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
134491 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
134493 assert( pIdx->pSchema==pTab->pSchema );
134499 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
134502 VdbeComment((v, "%s", pIdx->zName));
134505 if( iBase>pParse->nTab ) pParse->nTab = iBase;
134514 ** purposes only - to make sure the transfer optimization really
134536 assert( pDest->pTable!=pSrc->pTable );
134537 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
134540 if( pDest->onError!=pSrc->onError ){
134543 for(i=0; i<pSrc->nKeyCol; i++){
134544 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
134547 if( pSrc->aiColumn[i]==XN_EXPR ){
134548 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
134549 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
134550 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
134554 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
134557 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
134561 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
134579 ** There are lots of rules for determining compatibility - see comments
134584 ** is empty - a factor that can only be determined at run-time. In that
134601 sqlite3 *db = pParse->db;
134605 SrcItem *pItem; /* An element of pSelect->pSrc */
134618 if( pParse->pWith || pSelect->pWith ){
134630 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
134633 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
134634 if( pSelect->pSrc->nSrc!=1 ){
134637 if( pSelect->pSrc->a[0].pSelect ){
134640 if( pSelect->pWhere ){
134643 if( pSelect->pOrderBy ){
134648 if( pSelect->pGroupBy ){
134651 if( pSelect->pLimit ){
134654 if( pSelect->pPrior ){
134657 if( pSelect->selFlags & SF_Distinct ){
134660 pEList = pSelect->pEList;
134662 if( pEList->nExpr!=1 ){
134665 assert( pEList->a[0].pExpr );
134666 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
134674 pItem = pSelect->pSrc->a;
134679 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
134689 if( pDest->nCol!=pSrc->nCol ){
134692 if( pDest->iPKey!=pSrc->iPKey ){
134695 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
134696 return 0; /* Cannot feed from a non-strict into a strict table */
134698 for(i=0; i<pDest->nCol; i++){
134699 Column *pDestCol = &pDest->aCol[i];
134700 Column *pSrcCol = &pSrc->aCol[i];
134702 if( (db->mDbFlags & DBFLAG_Vacuum)==0
134703 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
134725 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
134726 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
134727 return 0; /* Both columns have the same generated-column type */
134733 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
134736 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
134737 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
134738 testcase( pDestCol->colFlags & COLFLAG_STORED );
134743 if( pDestCol->affinity!=pSrcCol->affinity ){
134750 if( pDestCol->notNull && !pSrcCol->notNull ){
134754 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
134757 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
134759 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
134762 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
134763 pSrcExpr->u.zToken)!=0)
134769 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
134773 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
134779 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
134784 return 0; /* Corrupt schema - two indexes on the same btree */
134788 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
134801 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
134805 if( (db->flags & SQLITE_CountRows)!=0 ){
134816 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
134819 iSrc = pParse->nTab++;
134820 iDest = pParse->nTab++;
134827 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
134828 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
134857 if( pDest->iPKey>=0 ){
134859 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
134867 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
134871 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
134874 if( db->mDbFlags & DBFLAG_Vacuum ){
134881 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
134890 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
134891 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
134899 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
134900 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
134902 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
134904 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
134908 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
134910 VdbeComment((v, "%s", pSrcIdx->zName));
134911 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
134914 VdbeComment((v, "%s", pDestIdx->zName));
134916 if( db->mDbFlags & DBFLAG_Vacuum ){
134921 ** order. In this case, instead of seeking within the b-tree as part
134923 ** OP_IdxInsert to seek to the point within the b-tree where each key
134931 for(i=0; i<pSrcIdx->nColumn; i++){
134932 const char *zColl = pSrcIdx->azColl[i];
134935 if( i==pSrcIdx->nColumn ){
134940 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
134945 if( (db->mDbFlags & DBFLAG_Vacuum)==0
135021 sqlite3_mutex_enter(db->mutex);
135028 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
135034 /* this happens for a comment or white-space */
135047 && db->flags&SQLITE_NullCallback)) ){
135074 /* EVIDENCE-OF: R-38229-40159 If the callback function to
135075 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
135113 assert( (rc&db->errMask)==rc );
135114 sqlite3_mutex_leave(db->mutex);
135525 ** (part of the main SQLite library - not an extension) so that
135532 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
135534 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
135536 #define sqlite3_bind_blob sqlite3_api->bind_blob
135537 #define sqlite3_bind_double sqlite3_api->bind_double
135538 #define sqlite3_bind_int sqlite3_api->bind_int
135539 #define sqlite3_bind_int64 sqlite3_api->bind_int64
135540 #define sqlite3_bind_null sqlite3_api->bind_null
135541 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
135542 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
135543 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
135544 #define sqlite3_bind_text sqlite3_api->bind_text
135545 #define sqlite3_bind_text16 sqlite3_api->bind_text16
135546 #define sqlite3_bind_value sqlite3_api->bind_value
135547 #define sqlite3_busy_handler sqlite3_api->busy_handler
135548 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
135549 #define sqlite3_changes sqlite3_api->changes
135550 #define sqlite3_close sqlite3_api->close
135551 #define sqlite3_collation_needed sqlite3_api->collation_needed
135552 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
135553 #define sqlite3_column_blob sqlite3_api->column_blob
135554 #define sqlite3_column_bytes sqlite3_api->column_bytes
135555 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
135556 #define sqlite3_column_count sqlite3_api->column_count
135557 #define sqlite3_column_database_name sqlite3_api->column_database_name
135558 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
135559 #define sqlite3_column_decltype sqlite3_api->column_decltype
135560 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
135561 #define sqlite3_column_double sqlite3_api->column_double
135562 #define sqlite3_column_int sqlite3_api->column_int
135563 #define sqlite3_column_int64 sqlite3_api->column_int64
135564 #define sqlite3_column_name sqlite3_api->column_name
135565 #define sqlite3_column_name16 sqlite3_api->column_name16
135566 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
135567 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
135568 #define sqlite3_column_table_name sqlite3_api->column_table_name
135569 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
135570 #define sqlite3_column_text sqlite3_api->column_text
135571 #define sqlite3_column_text16 sqlite3_api->column_text16
135572 #define sqlite3_column_type sqlite3_api->column_type
135573 #define sqlite3_column_value sqlite3_api->column_value
135574 #define sqlite3_commit_hook sqlite3_api->commit_hook
135575 #define sqlite3_complete sqlite3_api->complete
135576 #define sqlite3_complete16 sqlite3_api->complete16
135577 #define sqlite3_create_collation sqlite3_api->create_collation
135578 #define sqlite3_create_collation16 sqlite3_api->create_collation16
135579 #define sqlite3_create_function sqlite3_api->create_function
135580 #define sqlite3_create_function16 sqlite3_api->create_function16
135581 #define sqlite3_create_module sqlite3_api->create_module
135582 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
135583 #define sqlite3_data_count sqlite3_api->data_count
135584 #define sqlite3_db_handle sqlite3_api->db_handle
135585 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
135586 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
135587 #define sqlite3_errcode sqlite3_api->errcode
135588 #define sqlite3_errmsg sqlite3_api->errmsg
135589 #define sqlite3_errmsg16 sqlite3_api->errmsg16
135590 #define sqlite3_exec sqlite3_api->exec
135592 #define sqlite3_expired sqlite3_api->expired
135594 #define sqlite3_finalize sqlite3_api->finalize
135595 #define sqlite3_free sqlite3_api->free
135596 #define sqlite3_free_table sqlite3_api->free_table
135597 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
135598 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
135599 #define sqlite3_get_table sqlite3_api->get_table
135601 #define sqlite3_global_recover sqlite3_api->global_recover
135603 #define sqlite3_interrupt sqlite3_api->interruptx
135604 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
135605 #define sqlite3_libversion sqlite3_api->libversion
135606 #define sqlite3_libversion_number sqlite3_api->libversion_number
135607 #define sqlite3_malloc sqlite3_api->malloc
135608 #define sqlite3_mprintf sqlite3_api->mprintf
135609 #define sqlite3_open sqlite3_api->open
135610 #define sqlite3_open16 sqlite3_api->open16
135611 #define sqlite3_prepare sqlite3_api->prepare
135612 #define sqlite3_prepare16 sqlite3_api->prepare16
135613 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
135614 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
135615 #define sqlite3_profile sqlite3_api->profile
135616 #define sqlite3_progress_handler sqlite3_api->progress_handler
135617 #define sqlite3_realloc sqlite3_api->realloc
135618 #define sqlite3_reset sqlite3_api->reset
135619 #define sqlite3_result_blob sqlite3_api->result_blob
135620 #define sqlite3_result_double sqlite3_api->result_double
135621 #define sqlite3_result_error sqlite3_api->result_error
135622 #define sqlite3_result_error16 sqlite3_api->result_error16
135623 #define sqlite3_result_int sqlite3_api->result_int
135624 #define sqlite3_result_int64 sqlite3_api->result_int64
135625 #define sqlite3_result_null sqlite3_api->result_null
135626 #define sqlite3_result_text sqlite3_api->result_text
135627 #define sqlite3_result_text16 sqlite3_api->result_text16
135628 #define sqlite3_result_text16be sqlite3_api->result_text16be
135629 #define sqlite3_result_text16le sqlite3_api->result_text16le
135630 #define sqlite3_result_value sqlite3_api->result_value
135631 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
135632 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
135633 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
135634 #define sqlite3_snprintf sqlite3_api->xsnprintf
135635 #define sqlite3_step sqlite3_api->step
135636 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
135637 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
135638 #define sqlite3_total_changes sqlite3_api->total_changes
135639 #define sqlite3_trace sqlite3_api->trace
135641 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
135643 #define sqlite3_update_hook sqlite3_api->update_hook
135644 #define sqlite3_user_data sqlite3_api->user_data
135645 #define sqlite3_value_blob sqlite3_api->value_blob
135646 #define sqlite3_value_bytes sqlite3_api->value_bytes
135647 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
135648 #define sqlite3_value_double sqlite3_api->value_double
135649 #define sqlite3_value_int sqlite3_api->value_int
135650 #define sqlite3_value_int64 sqlite3_api->value_int64
135651 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
135652 #define sqlite3_value_text sqlite3_api->value_text
135653 #define sqlite3_value_text16 sqlite3_api->value_text16
135654 #define sqlite3_value_text16be sqlite3_api->value_text16be
135655 #define sqlite3_value_text16le sqlite3_api->value_text16le
135656 #define sqlite3_value_type sqlite3_api->value_type
135657 #define sqlite3_vmprintf sqlite3_api->vmprintf
135658 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
135659 #define sqlite3_overload_function sqlite3_api->overload_function
135660 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
135661 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
135662 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
135663 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
135664 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
135665 #define sqlite3_blob_close sqlite3_api->blob_close
135666 #define sqlite3_blob_open sqlite3_api->blob_open
135667 #define sqlite3_blob_read sqlite3_api->blob_read
135668 #define sqlite3_blob_write sqlite3_api->blob_write
135669 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
135670 #define sqlite3_file_control sqlite3_api->file_control
135671 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
135672 #define sqlite3_memory_used sqlite3_api->memory_used
135673 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
135674 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
135675 #define sqlite3_mutex_free sqlite3_api->mutex_free
135676 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
135677 #define sqlite3_mutex_try sqlite3_api->mutex_try
135678 #define sqlite3_open_v2 sqlite3_api->open_v2
135679 #define sqlite3_release_memory sqlite3_api->release_memory
135680 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
135681 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
135682 #define sqlite3_sleep sqlite3_api->sleep
135683 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
135684 #define sqlite3_vfs_find sqlite3_api->vfs_find
135685 #define sqlite3_vfs_register sqlite3_api->vfs_register
135686 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
135687 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
135688 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
135689 #define sqlite3_result_error_code sqlite3_api->result_error_code
135690 #define sqlite3_test_control sqlite3_api->test_control
135691 #define sqlite3_randomness sqlite3_api->randomness
135692 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
135693 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
135694 #define sqlite3_limit sqlite3_api->limit
135695 #define sqlite3_next_stmt sqlite3_api->next_stmt
135696 #define sqlite3_sql sqlite3_api->sql
135697 #define sqlite3_status sqlite3_api->status
135698 #define sqlite3_backup_finish sqlite3_api->backup_finish
135699 #define sqlite3_backup_init sqlite3_api->backup_init
135700 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
135701 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
135702 #define sqlite3_backup_step sqlite3_api->backup_step
135703 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
135704 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
135705 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
135706 #define sqlite3_db_config sqlite3_api->db_config
135707 #define sqlite3_db_mutex sqlite3_api->db_mutex
135708 #define sqlite3_db_status sqlite3_api->db_status
135709 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
135710 #define sqlite3_log sqlite3_api->log
135711 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
135712 #define sqlite3_sourceid sqlite3_api->sourceid
135713 #define sqlite3_stmt_status sqlite3_api->stmt_status
135714 #define sqlite3_strnicmp sqlite3_api->strnicmp
135715 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
135716 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
135717 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
135718 #define sqlite3_wal_hook sqlite3_api->wal_hook
135719 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
135720 #define sqlite3_vtab_config sqlite3_api->vtab_config
135721 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
135723 #define sqlite3_close_v2 sqlite3_api->close_v2
135724 #define sqlite3_db_filename sqlite3_api->db_filename
135725 #define sqlite3_db_readonly sqlite3_api->db_readonly
135726 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
135727 #define sqlite3_errstr sqlite3_api->errstr
135728 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
135729 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
135730 #define sqlite3_stricmp sqlite3_api->stricmp
135731 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
135732 #define sqlite3_uri_int64 sqlite3_api->uri_int64
135733 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
135734 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
135735 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
135737 #define sqlite3_auto_extension sqlite3_api->auto_extension
135738 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
135739 #define sqlite3_bind_text64 sqlite3_api->bind_text64
135740 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
135741 #define sqlite3_load_extension sqlite3_api->load_extension
135742 #define sqlite3_malloc64 sqlite3_api->malloc64
135743 #define sqlite3_msize sqlite3_api->msize
135744 #define sqlite3_realloc64 sqlite3_api->realloc64
135745 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
135746 #define sqlite3_result_blob64 sqlite3_api->result_blob64
135747 #define sqlite3_result_text64 sqlite3_api->result_text64
135748 #define sqlite3_strglob sqlite3_api->strglob
135750 #define sqlite3_value_dup sqlite3_api->value_dup
135751 #define sqlite3_value_free sqlite3_api->value_free
135752 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
135753 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
135755 #define sqlite3_value_subtype sqlite3_api->value_subtype
135756 #define sqlite3_result_subtype sqlite3_api->result_subtype
135758 #define sqlite3_status64 sqlite3_api->status64
135759 #define sqlite3_strlike sqlite3_api->strlike
135760 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
135762 #define sqlite3_system_errno sqlite3_api->system_errno
135764 #define sqlite3_trace_v2 sqlite3_api->trace_v2
135765 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
135767 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
135769 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
135770 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
135771 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
135772 #define sqlite3_result_pointer sqlite3_api->result_pointer
135773 #define sqlite3_value_pointer sqlite3_api->value_pointer
135775 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
135776 #define sqlite3_value_nochange sqlite3_api->value_nochange
135777 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
135779 #define sqlite3_keyword_count sqlite3_api->keyword_count
135780 #define sqlite3_keyword_name sqlite3_api->keyword_name
135781 #define sqlite3_keyword_check sqlite3_api->keyword_check
135782 #define sqlite3_str_new sqlite3_api->str_new
135783 #define sqlite3_str_finish sqlite3_api->str_finish
135784 #define sqlite3_str_appendf sqlite3_api->str_appendf
135785 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
135786 #define sqlite3_str_append sqlite3_api->str_append
135787 #define sqlite3_str_appendall sqlite3_api->str_appendall
135788 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
135789 #define sqlite3_str_reset sqlite3_api->str_reset
135790 #define sqlite3_str_errcode sqlite3_api->str_errcode
135791 #define sqlite3_str_length sqlite3_api->str_length
135792 #define sqlite3_str_value sqlite3_api->str_value
135794 #define sqlite3_create_window_function sqlite3_api->create_window_function
135796 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
135798 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
135799 #define sqlite3_value_frombind sqlite3_api->value_frombind
135801 #define sqlite3_drop_modules sqlite3_api->drop_modules
135803 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
135804 #define sqlite3_uri_key sqlite3_api->uri_key
135805 #define sqlite3_filename_database sqlite3_api->filename_database
135806 #define sqlite3_filename_journal sqlite3_api->filename_journal
135807 #define sqlite3_filename_wal sqlite3_api->filename_wal
135809 #define sqlite3_create_filename sqlite3_api->create_filename
135810 #define sqlite3_free_filename sqlite3_api->free_filename
135811 #define sqlite3_database_file_object sqlite3_api->database_file_object
135813 #define sqlite3_txn_state sqlite3_api->txn_state
135815 #define sqlite3_changes64 sqlite3_api->changes64
135816 #define sqlite3_total_changes64 sqlite3_api->total_changes64
135818 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
135820 #define sqlite3_error_offset sqlite3_api->error_offset
135821 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
135822 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
135823 #define sqlite3_vtab_in sqlite3_api->vtab_in
135824 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
135825 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
135828 #define sqlite3_deserialize sqlite3_api->deserialize
135829 #define sqlite3_serialize sqlite3_api->serialize
135831 #define sqlite3_db_name sqlite3_api->db_name
135833 #define sqlite3_value_encoding sqlite3_api->value_encoding
135835 #define sqlite3_is_interrupted sqlite3_api->is_interrupted
135837 #define sqlite3_stmt_explain sqlite3_api->stmt_explain
135839 #define sqlite3_get_clientdata sqlite3_api->get_clientdata
135840 #define sqlite3_set_clientdata sqlite3_api->set_clientdata
135853 # define SQLITE_EXTENSION_INIT1 /*no-op*/
135855 # define SQLITE_EXTENSION_INIT3 /*no-op*/
136391 sqlite3_vfs *pVfs = db->pVfs;
136423 if( (db->flags & SQLITE_LoadExtension)==0 ){
136432 /* tag-20210611-1. Some dlopen() implementations will segfault if given
136437 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
136480 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
136500 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
136519 /* Append the new shared library handle to the db->aExtension array. */
136520 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
136524 if( db->nExtension>0 ){
136525 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
136527 sqlite3DbFree(db, db->aExtension);
136528 db->aExtension = aHandle;
136530 db->aExtension[db->nExtension++] = handle;
136541 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
136553 sqlite3_mutex_enter(db->mutex);
136556 sqlite3_mutex_leave(db->mutex);
136566 assert( sqlite3_mutex_held(db->mutex) );
136567 for(i=0; i<db->nExtension; i++){
136568 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
136570 sqlite3DbFree(db, db->aExtension);
136581 sqlite3_mutex_enter(db->mutex);
136583 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
136585 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
136587 sqlite3_mutex_leave(db->mutex);
136608 ** we have to locate the state vector at run-time. In the more common
136671 ** routine is a no-op.
136689 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
136691 wsdAutoext.nExt--;
136857 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
136861 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
136863 /* Names of columns for pragmas that return multi-column result
136864 ** or that return single-column results where the name of the
136931 /* Definitions of all built-in pragmas */
137522 ** Interpret the given string as an auto-vacuum mode value.
137540 ** backed temporary databases, 2 for the Red-Black tree in memory database
137541 ** and 0 to use the compile-time default.
137545 return z[0] - '0';
137562 sqlite3 *db = pParse->db;
137563 if( db->aDb[1].pBt!=0 ){
137564 if( !db->autoCommit
137565 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
137571 sqlite3BtreeClose(db->aDb[1].pBt);
137572 db->aDb[1].pBt = 0;
137587 sqlite3 *db = pParse->db;
137588 if( db->temp_store==ts ) return SQLITE_OK;
137592 db->temp_store = (u8)ts;
137604 u8 n = pPragma->nPragCName;
137607 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
137610 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
137644 if( db->autoCommit ){
137645 Db *pDb = db->aDb;
137646 int n = db->nDb;
137652 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
137653 while( (n--) > 0 ){
137654 if( pDb->pBt ){
137655 sqlite3BtreeSetPagerFlags(pDb->pBt,
137656 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
137663 # define setAllPagerFlags(X) /* no-op */
137668 ** Return a human-readable name for a constraint resolution action.
137689 ** journal-mode name.
137716 upr = ArraySize(aPragmaName)-1;
137722 upr = mid - 1;
137737 int isBuiltin, /* True if this is a built-in function */
137748 for(; p; p=p->pNext){
137757 if( p->xSFunc==0 ) continue;
137758 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
137763 if( p->xValue!=0 ){
137765 }else if( p->xFinalize!=0 ){
137771 p->zName, isBuiltin,
137772 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
137773 p->nArg,
137774 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
137783 ** Generate code to output a single-column result row with a value of the
137816 int minusFlag /* True if a '-' sign preceded <value> */
137818 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
137819 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
137825 sqlite3 *db = pParse->db; /* The database connection */
137832 pParse->nMem = 2;
137838 pDb = &db->aDb[iDb];
137850 zRight = sqlite3MPrintf(db, "-%T", pValue);
137856 zDb = pId2->n>0 ? pDb->zDbSName : 0;
137861 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
137863 ** handled the pragma and generate a no-op prepared statement.
137865 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
137870 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
137880 db->busyHandler.nBusy = 0;
137894 pParse->nErr++;
137895 pParse->rc = rc;
137902 /* IMP: R-43042-22504 No error messages are generated if an
137908 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
137913 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
137914 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
137920 switch( pPragma->ePragTyp ){
137955 pParse->nMem += 2;
137967 pDb->pSchema->cache_size = size;
137968 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
137985 Btree *pBt = pDb->pBt;
137991 /* Malloc may fail when setting the page-size, as there is an internal
137994 db->nextPagesize = sqlite3Atoi(zRight);
137995 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
138004 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
138011 Btree *pBt = pDb->pBt;
138012 int b = -1;
138015 if( sqlite3_stricmp(zRight, "fast")==0 ){
138021 if( pId2->n==0 && b>=0 ){
138023 for(ii=0; ii<db->nDb; ii++){
138024 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
138053 iReg = ++pParse->nMem;
138077 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
138080 ** the locking-mode of the main database).
138082 eMode = db->dfltLockMode;
138085 if( pId2->n==0 ){
138087 ** of the PRAGMA command. In this case the locking-mode must be
138095 assert(pDb==&db->aDb[0]);
138096 for(ii=2; ii<db->nDb; ii++){
138097 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
138100 db->dfltLockMode = (u8)eMode;
138102 pPager = sqlite3BtreePager(pDb->pBt);
138139 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
138140 /* Do not allow journal-mode "OFF" in defensive since the database
138145 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
138148 pId2->n = 1;
138150 for(ii=db->nDb-1; ii>=0; ii--){
138151 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
138167 Pager *pPager = sqlite3BtreePager(pDb->pBt);
138168 i64 iLimit = -2;
138171 if( iLimit<-1 ) iLimit = -1;
138184 ** Get or set the value of the database 'auto-vacuum' parameter.
138189 Btree *pBt = pDb->pBt;
138196 db->nextAutovac = (u8)eAuto;
138198 ** incr-vacuum flags. This is required in case this connection
138200 ** as an auto-vacuum capable db.
138207 ** that this really is an auto-vacuum capable database.
138226 aOp[4].p3 = eAuto - 1;
138249 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
138265 ** number of pages is adjusted so that the cache uses -N kibibytes
138271 returnSingleInt(v, pDb->pSchema->cache_size);
138274 pDb->pSchema->cache_size = size;
138275 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
138292 ** number of pages is adjusted so that the cache uses -N kibibytes
138306 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
138307 sqlite3BtreeSetSpillSize(pDb->pBt,0));
138311 sqlite3BtreeSetSpillSize(pDb->pBt, size);
138314 db->flags |= SQLITE_CacheSpill;
138316 db->flags &= ~(u64)SQLITE_CacheSpill;
138345 if( pId2->n==0 ) db->szMmap = sz;
138346 for(ii=db->nDb-1; ii>=0; ii--){
138347 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
138348 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
138352 sz = -1;
138361 pParse->nErr++;
138362 pParse->rc = rc;
138375 ** Note that it is possible for the library compile-time options to
138380 returnSingleInt(v, db->temp_store);
138405 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
138413 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
138414 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
138452 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
138483 Pager *pPager = sqlite3BtreePager(pDb->pBt);
138490 Pager *pPager = sqlite3BtreePager(pDb->pBt);
138520 returnSingleInt(v, pDb->safety_level-1);
138522 if( !db->autoCommit ){
138528 pDb->safety_level = iLevel;
138529 pDb->bSyncSet = 1;
138541 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
138543 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
138544 if( db->autoCommit==0 ){
138546 ** in auto-commit mode. */
138550 if( db->auth.authLevel==UAUTH_User ){
138551 /* Do not allow non-admin users to modify the schema arbitrarily */
138558 || (db->flags & SQLITE_Defensive)==0
138560 db->flags |= mask;
138563 db->flags &= ~mask;
138564 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
138568 /* IMP: R-60817-01178 If the argument is "RESET" then schema
138575 /* Many of the flag-pragmas modify the code generated by the SQL
138598 ** pk: Non-zero for PK fields.
138609 pParse->nMem = 7;
138611 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
138614 if( pCol->colFlags & COLFLAG_NOINSERT ){
138615 if( pPragma->iArg==0 ){
138619 if( pCol->colFlags & COLFLAG_VIRTUAL ){
138621 }else if( pCol->colFlags & COLFLAG_STORED ){
138623 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
138627 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
138632 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
138635 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
138638 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
138639 i-nHidden,
138640 pCol->zCnName,
138642 pCol->notNull ? 1 : 0,
138643 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
138666 pParse->nMem = 6;
138668 for(ii=0; ii<db->nDb; ii++){
138672 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
138679 pHash = &db->aDb[ii].pSchema->tblHash;
138681 while( initNCol-- ){
138686 if( pTab->nCol==0 ){
138687 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
138690 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
138694 if( db->mallocFailed ){
138695 sqlite3ErrorMsg(db->pParse, "out of memory");
138696 db->pParse->rc = SQLITE_NOMEM_BKPT;
138698 pHash = &db->aDb[ii].pSchema->tblHash;
138707 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
138712 }else if( pTab->tabFlags & TF_Shadow ){
138718 db->aDb[ii].zDbSName,
138719 sqlite3PreferredTableName(pTab->zName),
138721 pTab->nCol,
138722 (pTab->tabFlags & TF_WithoutRowid)!=0,
138723 (pTab->tabFlags & TF_Strict)!=0
138734 pParse->nMem = 5;
138736 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
138739 sqlite3PreferredTableName(pTab->zName),
138741 pTab->szTabRow,
138742 pTab->nRowLogEst,
138743 pTab->tabFlags);
138744 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138746 pIdx->zName,
138747 pIdx->szIdxRow,
138748 pIdx->aiRowLogEst[0],
138749 pIdx->hasStat1);
138771 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
138774 if( pPragma->iArg ){
138776 mx = pIdx->nColumn;
138777 pParse->nMem = 6;
138780 mx = pIdx->nKeyCol;
138781 pParse->nMem = 3;
138783 pTab = pIdx->pTable;
138785 assert( pParse->nMem<=pPragma->nPragCName );
138787 i16 cnum = pIdx->aiColumn[i];
138789 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
138790 if( pPragma->iArg ){
138792 pIdx->aSortOrder[i],
138793 pIdx->azColl[i],
138794 i<pIdx->nKeyCol);
138796 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
138808 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
138809 pParse->nMem = 5;
138811 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
138815 pIdx->zName,
138817 azOrigin[pIdx->idxType],
138818 pIdx->pPartIdxWhere!=0);
138826 pParse->nMem = 3;
138827 for(i=0; i<db->nDb; i++){
138828 if( db->aDb[i].pBt==0 ) continue;
138829 assert( db->aDb[i].zDbSName!=0 );
138832 db->aDb[i].zDbSName,
138833 sqlite3BtreeGetFilename(db->aDb[i].pBt));
138841 pParse->nMem = 2;
138842 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
138844 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
138854 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
138855 pParse->nMem = 6;
138857 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
138858 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
138862 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
138864 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
138873 pParse->nMem = 1;
138874 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
138876 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
138899 pFK = pTab->u.tab.pFKey;
138901 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
138903 pParse->nMem = 8;
138907 for(j=0; j<pFK->nCol; j++){
138911 pFK->zTo,
138912 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
138913 pFK->aCol[j].zCol,
138914 actionName(pFK->aAction[1]), /* ON UPDATE */
138915 actionName(pFK->aAction[0]), /* ON DELETE */
138919 pFK = pFK->pNextFrom;
138944 regResult = pParse->nMem+1;
138945 pParse->nMem += 4;
138946 regRow = ++pParse->nMem;
138947 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
138956 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
138957 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
138958 zDb = db->aDb[iDb].zDbSName;
138960 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
138961 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
138963 sqlite3VdbeLoadString(v, regResult, pTab->zName);
138965 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
138966 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
138969 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
138975 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
138983 assert( pParse->nErr>0 || pFK==0 );
138985 if( pParse->nTab<i ) pParse->nTab = i;
138988 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
138989 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
138994 assert( x==0 || db->mallocFailed );
139002 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
139003 for(j=0; j<pFK->nCol; j++){
139004 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
139012 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
139013 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
139014 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
139020 assert( pFK->nCol==1 || db->mallocFailed );
139029 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
139068 ** without the overhead of cross-checking indexes. Quick_check
139096 ** to -1 here, to indicate that the VDBE should verify the integrity
139099 assert( iDb==0 || pId2->z );
139100 if( pId2->z==0 ) iDb = -1;
139103 pParse->nMem = 6;
139114 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
139117 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
139120 for(i=0; i<db->nDb; i++){
139131 pParse->okConstFactor = 0; /* tag-20230327-1 */
139133 /* Do an integrity check of the B-Tree
139139 pTbls = &db->aDb[i].pSchema->tblHash;
139146 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
139159 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
139160 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139161 aRoot[++cnt] = pIdx->tnum;
139170 /* Do the b-tree integrity checks */
139175 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
139189 int r1 = -1;
139192 int mxCol; /* Maximum non-virtual column number */
139201 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
139202 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
139207 ** reg[8+i] counts the number of entries in the i-th index
139210 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
139213 assert( pParse->nMem>=8+j );
139218 /* Fetch the right-most column from the table. This will cause
139225 mxCol = -1;
139226 for(j=0; j<pTab->nCol; j++){
139227 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
139229 if( mxCol==pTab->iPKey ) mxCol--;
139232 ** PK index column-count, so there is no need to account for them
139234 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
139246 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
139251 pTab->zName);
139256 for(j=0; j<pPk->nKeyCol; j++){
139264 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
139265 ** (3) Datatype for TEXT columns in non-STRICT tables must be
139267 ** (4) Datatype for numeric columns in non-STRICT tables must not
139270 bStrict = (pTab->tabFlags & TF_Strict)!=0;
139271 for(j=0; j<pTab->nCol; j++){
139273 Column *pCol = pTab->aCol + j; /* The column to be checked */
139279 if( j==pTab->iPKey ) continue;
139281 doTypeCheck = pCol->eCType>COLTYPE_ANY;
139283 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
139285 if( pCol->notNull==0 && !doTypeCheck ) continue;
139289 if( pCol->colFlags & COLFLAG_VIRTUAL ){
139291 p1 = -1;
139294 if( pCol->iDflt ){
139297 pCol->affinity, &pDfltValue);
139315 if( pCol->notNull ){
139333 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
139334 pCol->zCnName);
139345 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
139355 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
139356 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
139358 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
139359 sqlite3StdType[pCol->eCType-1],
139360 pTab->zName, pTab->aCol[j].zCnName);
139362 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
139363 /* (3) Datatype for TEXT columns in non-STRICT tables must be
139369 pTab->zName, pTab->aCol[j].zCnName);
139371 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
139372 /* (4) Datatype for numeric columns in non-STRICT tables must not
139381 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
139385 pTab->zName, pTab->aCol[j].zCnName);
139393 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
139394 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
139395 if( db->mallocFailed==0 ){
139400 pParse->iSelfTab = iDataCur + 1;
139401 for(k=pCheck->nExpr-1; k>0; k--){
139402 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
139404 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
139407 pParse->iSelfTab = 0;
139409 pTab->zName);
139418 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
139429 pIdx->nColumn); VdbeCoverage(v);
139434 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
139446 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
139449 "rowid not at end-of-record for row ");
139452 sqlite3VdbeGoto(v, jmp5-1);
139456 /* Any indexed columns with non-BINARY collations must still hold
139459 for(kk=0; kk<pIdx->nKeyCol; kk++){
139460 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
139471 sqlite3VdbeGoto(v, jmp5-1);
139481 for(kk=0; kk<pIdx->nKeyCol; kk++){
139482 int iCol = pIdx->aiColumn[kk];
139483 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
139484 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
139492 pIdx->nKeyCol); VdbeCoverage(v);
139493 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
139502 sqlite3VdbeJumpHere(v, loopTop-1);
139505 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
139510 sqlite3VdbeLoadString(v, 4, pIdx->zName);
139516 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
139532 if( pTab->nCol<=0 ){
139533 const char *zMod = pTab->u.vtab.azArg[0];
139534 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
139537 if( pTab->u.vtab.p==0 ) continue;
139538 pVTab = pTab->u.vtab.p->pVtab;
139540 if( NEVER(pVTab->pModule==0) ) continue;
139541 if( pVTab->pModule->iVersion<4 ) continue;
139542 if( pVTab->pModule->xIntegrity==0 ) continue;
139544 pTab->nTabRef++;
139569 aOp[0].p2 = 1-mxErr;
139575 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
139584 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
139589 ** The second form of this pragma is a no-op if the main database file
139610 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
139611 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
139612 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
139615 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
139625 returnSingleText(v, encnames[ENC(pParse->db)].zName);
139632 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
139633 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
139634 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
139635 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
139641 if( !pEnc->zName ){
139666 ** the value of the schema-version and user-version, respectively. Both
139667 ** the schema-version and the user-version are 32-bit signed integers
139670 ** The schema-cookie is usually only manipulated internally by SQLite. It
139677 ** the schema-version is potentially dangerous and may lead to program
139680 ** The user-version is not used internally by SQLite. It may be used by
139684 int iCookie = pPragma->iArg; /* Which cookie to read or write */
139686 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
139701 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
139703 ** mode. Change the OP_SetCookie opcode into a no-op. */
139730 ** Return the names of all compile-time options used in this build,
139736 pParse->nMem = 1;
139753 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
139764 pParse->nMem = 3;
139783 db->xWalCallback==sqlite3WalDefaultHook ?
139784 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
139792 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
139834 ** ever added that should be off by default, those off-by-default
139844 ** (2) The query planner used sqlite_stat1-style statistics for one or
139872 iTabCur = pParse->nTab++;
139873 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
139876 pSchema = db->aDb[iDb].pSchema;
139877 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
139883 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
139886 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
139887 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139888 if( !pIdx->hasStat1 ){
139900 db->aDb[iDb].zDbSName, pTab->zName);
139924 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
139928 returnSingleInt(v, db->busyTimeout);
139936 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
139938 ** specified and is a non-negative integer.
139939 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
139941 ** sqlite3_soft_heap_limit64(-1) C-language function.
139948 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
139959 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
139966 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
139969 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
139988 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
140002 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
140005 db->nAnalysisLimit = (int)(N&0x7fffffff);
140007 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
140020 pParse->nMem = 2;
140021 for(i=0; i<db->nDb; i++){
140025 if( db->aDb[i].zDbSName==0 ) continue;
140026 pBt = db->aDb[i].pBt;
140029 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
140033 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
140041 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
140050 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
140055 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
140106 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
140111 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
140115 if( pPragma->mPragFlg & PragFlg_Result1 ){
140119 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
140125 assert( strlen(zBuf) < sizeof(zBuf)-1 );
140133 pTab->pName = pPragma;
140134 pTab->db = db;
140135 pTab->iHidden = i;
140136 pTab->nHidden = j;
140168 pIdxInfo->estimatedCost = (double)1;
140169 if( pTab->nHidden==0 ){ return SQLITE_OK; }
140170 pConstraint = pIdxInfo->aConstraint;
140173 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
140174 if( pConstraint->usable==0 ) continue;
140175 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
140176 if( pConstraint->iColumn < pTab->iHidden ) continue;
140177 j = pConstraint->iColumn - pTab->iHidden;
140182 pIdxInfo->estimatedCost = (double)2147483647;
140183 pIdxInfo->estimatedRows = 2147483647;
140186 j = seen[0]-1;
140187 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
140188 pIdxInfo->aConstraintUsage[j].omit = 1;
140190 pIdxInfo->estimatedCost = (double)1000;
140191 pIdxInfo->estimatedRows = 1000;
140194 pIdxInfo->estimatedCost = (double)20;
140195 pIdxInfo->estimatedRows = 20;
140196 j = seen[1]-1;
140197 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
140198 pIdxInfo->aConstraintUsage[j].omit = 1;
140208 pCsr->base.pVtab = pVtab;
140209 *ppCursor = &pCsr->base;
140216 sqlite3_finalize(pCsr->pPragma);
140217 pCsr->pPragma = 0;
140218 for(i=0; i<ArraySize(pCsr->azArg); i++){
140219 sqlite3_free(pCsr->azArg[i]);
140220 pCsr->azArg[i] = 0;
140238 pCsr->iRowid++;
140239 assert( pCsr->pPragma );
140240 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
140241 rc = sqlite3_finalize(pCsr->pPragma);
140242 pCsr->pPragma = 0;
140257 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
140266 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
140269 assert( j<ArraySize(pCsr->azArg) );
140270 assert( pCsr->azArg[j]==0 );
140272 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
140273 if( pCsr->azArg[j]==0 ){
140278 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
140280 if( pCsr->azArg[1] ){
140281 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
140283 sqlite3_str_appendall(&acc, pTab->pName->zName);
140284 if( pCsr->azArg[0] ){
140285 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
140289 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
140292 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
140303 return (pCsr->pPragma==0);
140315 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
140316 if( i<pTab->iHidden ){
140317 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
140319 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
140329 *p = pCsr->iRowid;
140336 0, /* xCreate - create a table */
140337 pragmaVtabConnect, /* xConnect - connect to an existing table */
140338 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
140339 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
140340 0, /* xDestroy - Drop a table */
140341 pragmaVtabOpen, /* xOpen - open a cursor */
140342 pragmaVtabClose, /* xClose - close a cursor */
140343 pragmaVtabFilter, /* xFilter - configure scan constraints */
140344 pragmaVtabNext, /* xNext - advance a cursor */
140346 pragmaVtabColumn, /* xColumn - read data */
140347 pragmaVtabRowid, /* xRowid - read data */
140348 0, /* xUpdate - write data */
140349 0, /* xBegin - begin transaction */
140350 0, /* xSync - sync transaction */
140351 0, /* xCommit - commit transaction */
140352 0, /* xRollback - rollback transaction */
140353 0, /* xFindFunction - function overloading */
140354 0, /* xRename - rename the table */
140372 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
140373 assert( sqlite3HashFind(&db->aModule, zName)==0 );
140409 sqlite3 *db = pData->db;
140410 if( db->mallocFailed ){
140411 pData->rc = SQLITE_NOMEM_BKPT;
140412 }else if( pData->pzErrMsg[0]!=0 ){
140414 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
140420 *pData->pzErrMsg = sqlite3MPrintf(db,
140422 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
140425 pData->rc = SQLITE_ERROR;
140426 }else if( db->flags & SQLITE_WriteSchema ){
140427 pData->rc = SQLITE_CORRUPT_BKPT;
140432 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
140433 *pData->pzErrMsg = z;
140434 pData->rc = SQLITE_CORRUPT_BKPT;
140445 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
140446 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
140454 const char *zSql, /* UTF-8 encoded SQL statement. */
140479 sqlite3 *db = pData->db;
140480 int iDb = pData->iDb;
140484 assert( sqlite3_mutex_held(db->mutex) );
140485 db->mDbFlags |= DBFLAG_EncodingFixed;
140487 pData->nInitRow++;
140488 if( db->mallocFailed ){
140493 assert( iDb>=0 && iDb<db->nDb );
140500 ** But because db->init.busy is set to 1, no VDBE code is generated
140510 u8 saved_iDb = db->init.iDb;
140514 assert( db->init.busy );
140515 db->init.iDb = iDb;
140516 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
140517 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
140523 db->init.orphanTrigger = 0;
140524 db->init.azInit = (const char**)argv;
140526 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
140527 rc = db->errCode;
140529 db->init.iDb = saved_iDb;
140530 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
140532 if( db->init.orphanTrigger ){
140535 if( rc > pData->rc ) pData->rc = rc;
140543 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
140555 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
140559 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
140560 || pIndex->tnum<2
140561 || pIndex->tnum>pData->mxPage
140592 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
140594 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
140595 assert( iDb>=0 && iDb<db->nDb );
140596 assert( db->aDb[iDb].pSchema );
140597 assert( sqlite3_mutex_held(db->mutex) );
140598 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
140600 db->init.busy = 1;
140602 /* Construct the in-memory representation schema tables (sqlite_schema or
140606 ** the schema table as read-only. */
140622 db->mDbFlags &= mask;
140630 pDb = &db->aDb[iDb];
140631 if( pDb->pBt==0 ){
140638 /* If there is not already a read-only (or read-write) transaction opened
140639 ** on the b-tree database, open one now. If a transaction is opened, it
140641 sqlite3BtreeEnter(pDb->pBt);
140642 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
140643 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
140658 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
140669 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
140671 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
140674 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
140676 /* If opening a non-empty database, check the text encoding. For the
140681 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
140682 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
140686 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
140691 if( db->nVdbeActive>0 && encoding!=ENC(db)
140692 && (db->mDbFlags & DBFLAG_Vacuum)==0
140701 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
140709 pDb->pSchema->enc = ENC(db);
140711 if( pDb->pSchema->cache_size==0 ){
140713 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
140715 pDb->pSchema->cache_size = size;
140717 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
140719 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
140725 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
140728 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
140729 if( pDb->pSchema->file_format==0 ){
140730 pDb->pSchema->file_format = 1;
140732 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
140743 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
140744 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
140749 assert( db->init.busy );
140750 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
140755 db->aDb[iDb].zDbSName, zSchemaTabName);
140759 xAuth = db->xAuth;
140760 db->xAuth = 0;
140764 db->xAuth = xAuth;
140775 assert( pDb == &(db->aDb[iDb]) );
140776 if( db->mallocFailed ){
140779 pDb = &db->aDb[iDb];
140781 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
140802 sqlite3BtreeCommit(pDb->pBt);
140804 sqlite3BtreeLeave(pDb->pBt);
140813 db->init.busy = 0;
140818 ** Initialize all database files - the main database file, the file
140828 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
140830 assert( sqlite3_mutex_held(db->mutex) );
140831 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
140832 assert( db->init.busy==0 );
140834 assert( db->nDb>0 );
140841 for(i=db->nDb-1; i>0; i--){
140842 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
140855 ** This routine is a no-op if the database schema is already initialized.
140860 sqlite3 *db = pParse->db;
140861 assert( sqlite3_mutex_held(db->mutex) );
140862 if( !db->init.busy ){
140863 rc = sqlite3Init(db, &pParse->zErrMsg);
140865 pParse->rc = rc;
140866 pParse->nErr++;
140867 }else if( db->noSharedCache ){
140868 db->mDbFlags |= DBFLAG_SchemaKnownOk;
140877 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
140878 ** make no changes to pParse->rc.
140881 sqlite3 *db = pParse->db;
140886 assert( pParse->checkSchema );
140887 assert( sqlite3_mutex_held(db->mutex) );
140888 for(iDb=0; iDb<db->nDb; iDb++){
140890 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
140893 /* If there is not already a read-only (or read-write) transaction opened
140894 ** on the b-tree database, open one now. If a transaction is opened, it
140895 ** will be closed immediately after reading the meta-value. */
140900 pParse->rc = SQLITE_NOMEM;
140907 ** value stored as part of the in-memory schema representation,
140911 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
140912 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
140925 ** which database file in db->aDb[] the schema refers to.
140931 int i = -32768;
140933 /* If pSchema is NULL, then return -32768. This happens when code in
140935 ** created by a sub-select). In this case the return value of this
140938 ** We return -32768 instead of the more usual -1 simply because using
140939 ** -32768 as the incorrect index into db->aDb[] is much
140940 ** more likely to cause a segfault than -1 (of course there are assert()
140942 ** -32768 will still fit into a 16-bit signed integer.
140944 assert( sqlite3_mutex_held(db->mutex) );
140947 assert( i<db->nDb );
140948 if( db->aDb[i].pSchema==pSchema ){
140952 assert( i>=0 && i<db->nDb );
140961 sqlite3 *db = pParse->db;
140963 assert( db->pParse==pParse );
140964 assert( pParse->nested==0 );
140966 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
140968 while( pParse->pCleanup ){
140969 ParseCleanup *pCleanup = pParse->pCleanup;
140970 pParse->pCleanup = pCleanup->pNext;
140971 pCleanup->xCleanup(db, pCleanup->pPtr);
140974 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
140975 if( pParse->pConstExpr ){
140976 sqlite3ExprListDelete(db, pParse->pConstExpr);
140978 assert( db->lookaside.bDisable >= pParse->disableLookaside );
140979 db->lookaside.bDisable -= pParse->disableLookaside;
140980 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
140981 assert( pParse->db->pParse==pParse );
140982 db->pParse = pParse->pOuterParse;
140993 ** common cleanups, we save a single NULL-pointer comparison in
140998 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
141000 ** use-after-free errors following an OOM. The preferred way to do this is
141003 ** testcase( pParse->earlyCleanup );
141018 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
141020 pCleanup->pNext = pParse->pCleanup;
141021 pParse->pCleanup = pCleanup;
141022 pCleanup->pPtr = pPtr;
141023 pCleanup->xCleanup = xCleanup;
141025 xCleanup(pParse->db, pPtr);
141028 pParse->earlyCleanup = 1;
141046 assert( db->pParse!=pParse );
141047 pParse->pOuterParse = db->pParse;
141048 db->pParse = pParse;
141049 pParse->db = db;
141050 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
141062 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
141066 const char *zSql, /* UTF-8 encoded SQL statement. */
141080 sParse.pOuterParse = db->pParse;
141081 db->pParse = &sParse;
141090 if( db->mallocFailed ){
141092 db->errCode = rc = SQLITE_NOMEM;
141095 assert( sqlite3_mutex_held(db->mutex) );
141097 /* For a long-term use prepared statement avoid the use of
141108 ** some other database connection is holding a write-lock, which in
141129 if( !db->noSharedCache ){
141130 for(i=0; i<db->nDb; i++) {
141131 Btree *pBt = db->aDb[i].pBt;
141136 const char *zDb = db->aDb[i].zDbSName;
141138 testcase( db->flags & SQLITE_ReadUncommit );
141146 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
141149 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
141151 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
141162 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
141176 if( db->init.busy==0 ){
141177 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
141179 if( db->mallocFailed ){
141184 if( sParse.checkSchema && db->init.busy==0 ){
141209 sParse.pTriggerPrg = pT->pNext;
141220 const char *zSql, /* UTF-8 encoded SQL statement. */
141237 sqlite3_mutex_enter(db->mutex);
141245 if( rc==SQLITE_OK || db->mallocFailed ) break;
141247 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
141250 assert( (rc&db->errMask)==rc );
141251 db->busyHandler.nBusy = 0;
141252 sqlite3_mutex_leave(db->mutex);
141272 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
141276 assert( sqlite3_mutex_held(db->mutex) );
141278 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
141306 const char *zSql, /* UTF-8 encoded SQL statement. */
141318 const char *zSql, /* UTF-8 encoded SQL statement. */
141324 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
141336 const char *zSql, /* UTF-8 encoded SQL statement. */
141343 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
141360 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
141364 const void *zSql, /* UTF-16 encoded SQL statement. */
141370 /* This function currently works by first transforming the UTF-16
141371 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
141391 sqlite3_mutex_enter(db->mutex);
141394 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
141399 ** equivalent pointer into the UTF-16 string by counting the unicode
141401 ** the same number of characters into the UTF-16 string.
141403 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
141408 sqlite3_mutex_leave(db->mutex);
141422 const void *zSql, /* UTF-16 encoded SQL statement. */
141434 const void *zSql, /* UTF-16 encoded SQL statement. */
141446 const void *zSql, /* UTF-16 encoded SQL statement. */
141497 ** The aDefer[] array is used by the sorter-references optimization. For
141507 ** When the sorter-reference optimization is used, there is one entry in the
141516 int regReturn; /* Register holding block-output return address */
141517 int labelBkOut; /* Start label for the block-output subroutine */
141548 Select *pPrior = p->pPrior;
141549 sqlite3ExprListDelete(db, p->pEList);
141550 sqlite3SrcListDelete(db, p->pSrc);
141551 sqlite3ExprDelete(db, p->pWhere);
141552 sqlite3ExprListDelete(db, p->pGroupBy);
141553 sqlite3ExprDelete(db, p->pHaving);
141554 sqlite3ExprListDelete(db, p->pOrderBy);
141555 sqlite3ExprDelete(db, p->pLimit);
141556 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
141558 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
141559 sqlite3WindowListDelete(db, p->pWinDefn);
141561 while( p->pWin ){
141562 assert( p->pWin->ppThis==&p->pWin );
141563 sqlite3WindowUnlinkFromSelect(p->pWin);
141576 pDest->eDest = (u8)eDest;
141577 pDest->iSDParm = iParm;
141578 pDest->iSDParm2 = 0;
141579 pDest->zAffSdst = 0;
141580 pDest->iSdst = 0;
141581 pDest->nSdst = 0;
141592 SrcList *pSrc, /* the FROM clause -- which tables to scan */
141602 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
141604 assert( pParse->db->mallocFailed );
141609 sqlite3Expr(pParse->db,TK_ASTERISK,0));
141611 pNew->pEList = pEList;
141612 pNew->op = TK_SELECT;
141613 pNew->selFlags = selFlags;
141614 pNew->iLimit = 0;
141615 pNew->iOffset = 0;
141616 pNew->selId = ++pParse->nSelect;
141617 pNew->addrOpenEphm[0] = -1;
141618 pNew->addrOpenEphm[1] = -1;
141619 pNew->nSelectRow = 0;
141620 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
141621 pNew->pSrc = pSrc;
141622 pNew->pWhere = pWhere;
141623 pNew->pGroupBy = pGroupBy;
141624 pNew->pHaving = pHaving;
141625 pNew->pOrderBy = pOrderBy;
141626 pNew->pPrior = 0;
141627 pNew->pNext = 0;
141628 pNew->pLimit = pLimit;
141629 pNew->pWith = 0;
141631 pNew->pWin = 0;
141632 pNew->pWinDefn = 0;
141634 if( pParse->db->mallocFailed ) {
141635 clearSelect(pParse->db, pNew, pNew!=&standin);
141638 assert( pNew->pSrc!=0 || pParse->nErr>0 );
141652 ** Return a pointer to the right-most SELECT statement in a compound.
141655 while( p->pNext ) p = p->pNext;
141680 ** ------- ----- ----- ------------
141681 ** CROSS - - JT_CROSS
141682 ** INNER - - JT_INNER
141683 ** LEFT - - JT_LEFT|JT_OUTER
141684 ** LEFT OUTER - JT_LEFT|JT_OUTER
141685 ** RIGHT - - JT_RIGHT|JT_OUTER
141686 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
141687 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
141688 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
141689 ** NATURAL INNER - JT_NATURAL|JT_INNER
141690 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
141692 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
141694 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
141698 ** of other non-standard and in many cases nonsensical join types.
141703 ** INNER CROSS JOIN -> same as JOIN
141704 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
141705 ** OUTER LEFT JOIN -> same as LEFT JOIN
141706 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
141707 ** LEFT RIGHT JOIN -> same as FULL JOIN
141708 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
141709 ** CROSS CROSS CROSS JOIN -> same as JOIN
141748 if( p->n==aKeyword[j].nChar
141749 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
141777 ** Return the index of a column in a table. Return -1 if the column
141784 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
141785 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
141787 return -1;
141795 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
141796 if( pItem->fg.isNestedFrom ){
141798 assert( pItem->pSelect!=0 );
141799 pResults = pItem->pSelect->pEList;
141801 assert( iCol>=0 && iCol<pResults->nExpr );
141802 pResults->a[iCol].fg.bUsed = 1;
141808 ** table that has a column named zCol. The search is left-to-right.
141818 int iStart, /* First member of pSrc->a[] to check */
141819 int iEnd, /* Last member of pSrc->a[] to check */
141821 int *piTab, /* Write index of pSrc->a[] here */
141822 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
141828 assert( iEnd<pSrc->nSrc );
141833 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
141835 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
141838 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
141880 p->w.iJoin = iTable;
141881 if( p->op==TK_FUNCTION ){
141883 if( p->x.pList ){
141885 for(i=0; i<p->x.pList->nExpr; i++){
141886 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
141890 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
141891 p = p->pRight;
141911 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
141915 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
141918 if( p->op==TK_FUNCTION ){
141920 assert( p->pLeft==0 );
141921 if( p->x.pList ){
141923 for(i=0; i<p->x.pList->nExpr; i++){
141924 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
141928 unsetJoinExpr(p->pLeft, iTable, nullable);
141929 p = p->pRight;
141946 ** The left most table is the first entry in Select.pSrc. The right-most
141960 pSrc = p->pSrc;
141961 pLeft = &pSrc->a[0];
141963 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
141964 Table *pRightTab = pRight->pTab;
141967 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
141968 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
141973 if( pRight->fg.jointype & JT_NATURAL ){
141975 if( pRight->fg.isUsing || pRight->u3.pOn ){
141980 for(j=0; j<pRightTab->nCol; j++){
141983 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
141984 zName = pRightTab->aCol[j].zCnName;
141988 assert( pUsing->nId>0 );
141989 assert( pUsing->a[pUsing->nId-1].zName==0 );
141990 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
141995 pRight->fg.isUsing = 1;
141996 pRight->fg.isSynthUsing = 1;
141997 pRight->u3.pUsing = pUsing;
141999 if( pParse->nErr ) return 1;
142009 if( pRight->fg.isUsing ){
142010 IdList *pList = pRight->u3.pUsing;
142011 sqlite3 *db = pParse->db;
142013 for(j=0; j<pList->nId; j++){
142022 zName = pList->a[j].zName;
142026 pRight->fg.isSynthUsing)==0
142028 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
142033 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
142034 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
142037 ** contains the zName column, then this branch is a no-op.
142044 ** non-USING references to zName on the left of an INNER or LEFT
142051 pRight->fg.isSynthUsing)!=0 ){
142052 if( pSrc->a[iLeft].fg.isUsing==0
142053 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
142061 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
142076 pEq->w.iJoin = pE2->iTable;
142078 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
142085 else if( pRight->u3.pOn ){
142086 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
142087 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
142088 pRight->u3.pOn = 0;
142089 pRight->fg.isOn = 1;
142118 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
142119 0, pInfo->ecelFlags);
142121 if( pInfo->pExtra ){
142122 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
142123 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
142141 int nOBSat = pSort->nOBSat;
142142 Vdbe *v = pParse->pVdbe;
142143 int regOut = ++pParse->nMem;
142144 if( pSort->pDeferredRowLoad ){
142145 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
142147 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
142153 ** through regData+nData-1 onto the sorter.
142164 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
142165 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
142166 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
142170 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
142193 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
142198 regBase = regData - nPrefixReg;
142200 regBase = pParse->nMem + 1;
142201 pParse->nMem += nBase;
142203 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
142204 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
142205 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
142206 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
142209 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
142223 regPrevKey = pParse->nMem+1;
142224 pParse->nMem += pSort->nOBSat;
142225 nKey = nExpr - pSort->nOBSat + bSeq;
142229 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
142232 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
142233 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
142234 if( pParse->db->mallocFailed ) return;
142235 pOp->p2 = nKey + nData;
142236 pKI = pOp->p4.pKeyInfo;
142237 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
142238 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
142239 testcase( pKI->nAllField > pKI->nKeyField+2 );
142240 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
142241 pKI->nAllField-pKI->nKeyField-1);
142245 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
142246 pSort->regReturn = ++pParse->nMem;
142247 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
142248 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
142250 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
142254 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
142268 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
142273 int iCsr = pSort->iECursor;
142278 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
142285 if( pSort->sortFlags & SORTFLAG_UseSorter ){
142290 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
142291 regBase+nOBSat, nBase-nOBSat);
142294 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
142297 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
142367 int nResultCol = pEList->nExpr;
142368 Vdbe *v = pParse->pVdbe;
142377 iRet = regPrev = pParse->nMem+1;
142378 pParse->nMem += nResultCol;
142382 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
142383 if( i<nResultCol-1 ){
142390 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
142393 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
142394 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
142429 ** No adjustments necessary. This function is a no-op.
142448 if( pParse->nErr==0
142451 Vdbe *v = pParse->pVdbe;
142453 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
142462 pOp->opcode = OP_Null;
142463 pOp->p1 = 1;
142464 pOp->p2 = iVal;
142471 ** This function is called as part of inner-loop generation for a SELECT
142473 ** determines the expressions, if any, that the sorter-reference
142474 ** optimization should be used for. The sorter-reference optimization
142487 ** for which the sorter-reference optimization should be enabled.
142488 ** Additionally, the pSort->aDefer[] array is populated with entries
142503 for(i=0; i<pEList->nExpr; i++){
142504 struct ExprList_item *pItem = &pEList->a[i];
142505 if( pItem->u.x.iOrderByCol==0 ){
142506 Expr *pExpr = pItem->pExpr;
142508 if( pExpr->op==TK_COLUMN
142509 && pExpr->iColumn>=0
142511 && (pTab = pExpr->y.pTab)!=0
142513 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
142517 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
142520 if( nDefer==ArraySize(pSort->aDefer) ){
142528 nKey = pPk->nKeyCol;
142533 pNew->iTable = pExpr->iTable;
142535 pNew->y.pTab = pExpr->y.pTab;
142536 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
142540 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
142541 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
142542 pSort->aDefer[nDefer].nKey = nKey;
142546 pItem->fg.bSorterRef = 1;
142550 pSort->nDefer = (u8)nDefer;
142559 ** If srcTab is negative, then the p->pEList expressions
142561 ** zero or more, then data is pulled from srcTab and p->pEList is used only
142567 int srcTab, /* Pull data from this table if non-negative */
142574 Vdbe *v = pParse->pVdbe;
142577 int eDest = pDest->eDest; /* How to dispose of results */
142578 int iParm = pDest->iSDParm; /* First argument to disposal method */
142586 ** values for any expressions that are also part of the sort-key are omitted
142592 assert( p->pEList!=0 );
142593 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
142594 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
142597 codeOffset(v, p->iOffset, iContinue);
142602 nResultCol = p->pEList->nExpr;
142604 if( pDest->iSdst==0 ){
142606 nPrefixReg = pSort->pOrderBy->nExpr;
142607 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
142608 pParse->nMem += nPrefixReg;
142610 pDest->iSdst = pParse->nMem+1;
142611 pParse->nMem += nResultCol;
142612 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
142614 ** on the right-hand side of an INSERT contains more result columns than
142618 pParse->nMem += nResultCol;
142620 pDest->nSdst = nResultCol;
142621 regOrig = regResult = pDest->iSdst;
142625 VdbeComment((v, "%s", p->pEList->a[i].zEName));
142642 /* For each expression in p->pEList that is a copy of an expression in
142643 ** the ORDER BY clause (pSort->pOrderBy), set the associated
142645 ** expression within the sort-key that pushOntoSorter() will generate.
142646 ** This allows the p->pEList field to be omitted from the sorted record,
142650 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
142652 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
142653 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
142657 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
142658 if( pExtra && pParse->db->mallocFailed==0 ){
142664 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
142665 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
142666 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
142667 pParse->nMem += pExtra->nExpr;
142673 pEList = p->pEList;
142674 for(i=0; i<pEList->nExpr; i++){
142675 if( pEList->a[i].u.x.iOrderByCol>0
142677 || pEList->a[i].fg.bSorterRef
142680 nResultCol--;
142699 if( pExtra ) nResultCol += pExtra->nExpr;
142701 if( p->iLimit
142707 pSort->pDeferredRowLoad = &sRowLoadInfo;
142719 int eType = pDistinct->eTnctType;
142720 int iTab = pDistinct->tabTnct;
142721 assert( nResultCol==p->pEList->nExpr );
142722 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
142723 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
142725 codeOffset(v, p->iOffset, iContinue);
142766 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
142769 ** This does not affect operation in any way - it just allows MakeRecord
142771 if( eDest==SRT_Table && pDest->iSDParm2 ){
142808 int i2 = pDest->iSDParm2;
142817 regResult+(i2<0), nResultCol-(i2<0), r1);
142842 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
142844 r1, pDest->zAffSdst, nResultCol);
142866 assert( nResultCol<=pDest->nSdst );
142870 assert( nResultCol==pDest->nSdst );
142878 case SRT_Coroutine: /* Send data to a co-routine */
142886 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
142895 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
142896 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
142897 ** pSO->nExpr columns, then make sure all keys are unique by adding a
142906 pSO = pDest->pOrderBy;
142908 nKey = pSO->nExpr;
142927 regResult + pSO->a[i].u.x.iOrderByCol - 1,
142945 ** user-defined functions that have side effects. We do not care
142959 if( pSort==0 && p->iLimit ){
142960 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
142969 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
142972 p->aSortFlags = (u8*)&p->aColl[N+X];
142973 p->nKeyField = (u16)N;
142974 p->nAllField = (u16)(N+X);
142975 p->enc = ENC(db);
142976 p->db = db;
142977 p->nRef = 1;
142990 assert( p->db!=0 );
142991 assert( p->nRef>0 );
142992 p->nRef--;
142993 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
143002 assert( p->nRef>0 );
143003 p->nRef++;
143015 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
143041 sqlite3 *db = pParse->db;
143044 nExpr = pList->nExpr;
143045 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
143048 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
143049 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
143050 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
143073 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
143076 ** "USE TEMP B-TREE FOR xxx"
143082 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
143086 ** Assign expression b to lvalue a. A second, no-op, version of this macro
143095 /* No-op versions of the explainXXX() functions and macros. */
143102 ** If the inner loop was generated using a non-null pOrderBy argument,
143114 Vdbe *v = pParse->pVdbe; /* The prepared statement */
143115 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
143120 ExprList *pOrderBy = pSort->pOrderBy;
143121 int eDest = pDest->eDest;
143122 int iParm = pDest->iSDParm;
143131 struct ExprList_item *aOutEx = p->pEList->a;
143137 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat>0?"RIGHT PART OF ":"")
143139 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
143140 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
143144 if( pSort->labelBkOut ){
143145 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
143147 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
143151 /* Open any cursors needed for sorter-reference expressions */
143152 for(i=0; i<pSort->nDefer; i++){
143153 Table *pTab = pSort->aDefer[i].pTab;
143154 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
143155 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
143156 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
143160 iTab = pSort->iECursor;
143162 if( eDest==SRT_Mem && p->iOffset ){
143163 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
143166 regRow = pDest->iSdst;
143176 nKey = pOrderBy->nExpr - pSort->nOBSat;
143177 if( pSort->sortFlags & SORTFLAG_UseSorter ){
143178 int regSortOut = ++pParse->nMem;
143179 iSortTab = pParse->nTab++;
143180 if( pSort->labelBkOut ){
143188 assert( p->iLimit==0 && p->iOffset==0 );
143193 codeOffset(v, p->iOffset, addrContinue);
143196 if( p->iOffset>0 ){
143197 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
143200 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
143207 if( pSort->nDefer ){
143211 for(i=0; i<pSort->nDefer; i++){
143212 int iCsr = pSort->aDefer[i].iCsr;
143213 Table *pTab = pSort->aDefer[i].pTab;
143214 int nKey = pSort->aDefer[i].nKey;
143224 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
143237 for(i=nColumn-1; i>=0; i--){
143246 iRead = aOutEx[i].u.x.iOrderByCol-1;
143248 iRead = iCol--;
143254 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
143266 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
143268 pDest->zAffSdst, nColumn);
143278 int i2 = pDest->iSDParm2;
143280 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
143293 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
143295 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
143311 if( pSort->sortFlags & SORTFLAG_UseSorter ){
143316 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
143317 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
143329 ** result-set expression in all of the following SELECT statements is
143340 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
143367 assert( pNC->pSrcList!=0 );
143368 switch( pExpr->op ){
143376 int iCol = pExpr->iColumn; /* Index of column in pTab */
143378 SrcList *pTabList = pNC->pSrcList;
143379 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
143380 if( j<pTabList->nSrc ){
143381 pTab = pTabList->a[j].pTab;
143382 pS = pTabList->a[j].pSelect;
143384 pNC = pNC->pNext;
143399 ** sub-select. In this case, set the column type to NULL, even
143409 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
143411 /* The "table" is actually a sub-select or a view in the FROM clause
143413 ** data for the result-set column of the sub-select.
143415 if( iCol<pS->pEList->nExpr
143419 ** rowid of the sub-select or view. This expression is legal (see
143420 ** test case misc2.2.2) - it always evaluates to NULL.
143423 Expr *p = pS->pEList->a[iCol].pExpr;
143424 sNC.pSrcList = pS->pSrc;
143426 sNC.pParse = pNC->pParse;
143433 if( iCol<0 ) iCol = pTab->iPKey;
143434 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
143439 zOrigCol = pTab->aCol[iCol].zCnName;
143440 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
143442 zOrigTab = pTab->zName;
143443 if( pNC->pParse && pTab->pSchema ){
143444 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
143445 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
143448 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
143452 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
143460 /* The expression is a sub-select. Return the declaration type and
143468 pS = pExpr->x.pSelect;
143469 p = pS->pEList->a[0].pExpr;
143470 sNC.pSrcList = pS->pSrc;
143472 sNC.pParse = pNC->pParse;
143500 Vdbe *v = pParse->pVdbe;
143506 for(i=0; i<pEList->nExpr; i++){
143507 Expr *p = pEList->a[i].pExpr;
143515 /* The vdbe must make its own copy of the column-type and other
143565 Vdbe *v = pParse->pVdbe;
143570 sqlite3 *db = pParse->db;
143574 if( pParse->colNamesSet ) return;
143575 /* Column names are determined by the left-most term of a compound select */
143576 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
143578 pTabList = pSelect->pSrc;
143579 pEList = pSelect->pEList;
143582 pParse->colNamesSet = 1;
143583 fullName = (db->flags & SQLITE_FullColNames)!=0;
143584 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
143585 sqlite3VdbeSetNumCols(v, pEList->nExpr);
143586 for(i=0; i<pEList->nExpr; i++){
143587 Expr *p = pEList->a[i].pExpr;
143590 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
143591 assert( p->op!=TK_COLUMN
143592 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
143593 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
143595 char *zName = pEList->a[i].zEName;
143597 }else if( srcName && p->op==TK_COLUMN ){
143599 int iCol = p->iColumn;
143600 pTab = p->y.pTab;
143602 if( iCol<0 ) iCol = pTab->iPKey;
143603 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
143607 zCol = pTab->aCol[iCol].zCnName;
143611 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
143617 const char *z = pEList->a[i].zEName;
143653 sqlite3 *db = pParse->db; /* Database connection */
143665 nCol = pEList->nExpr;
143677 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
143678 struct ExprList_item *pX = &pEList->a[i];
143682 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
143685 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
143686 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
143687 pColExpr = pColExpr->pRight;
143690 if( pColExpr->op==TK_COLUMN
143692 && ALWAYS( pColExpr->y.pTab!=0 )
143695 int iCol = pColExpr->iColumn;
143696 pTab = pColExpr->y.pTab;
143697 if( iCol<0 ) iCol = pTab->iPKey;
143698 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
143699 }else if( pColExpr->op==TK_ID ){
143701 zName = pColExpr->u.zToken;
143704 assert( zName==pX->zEName ); /* pointer comparison intended */
143718 if( pCollide->fg.bUsingTerm ){
143719 pCol->colFlags |= COLFLAG_NOEXPAND;
143723 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
143732 pCol->zCnName = zName;
143733 pCol->hName = sqlite3StrIHash(zName);
143734 if( pX->fg.bNoExpand ){
143735 pCol->colFlags |= COLFLAG_NOEXPAND;
143743 if( pParse->nErr ){
143750 return pParse->rc;
143772 sqlite3 *db = pParse->db;
143781 testcase( (pSelect->selFlags & SF_Resolved)==0 );
143782 assert( (pSelect->selFlags & SF_Resolved)!=0 || IN_RENAME_OBJECT );
143783 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
143785 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
143786 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
143787 a = pSelect->pEList->a;
143789 sNC.pSrcList = pSelect->pSrc;
143790 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
143793 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
143795 /* pCol->szEst = ... // Column size est for SELECT tables never used */
143796 pCol->affinity = sqlite3ExprAffinity(p);
143797 if( pCol->affinity<=SQLITE_AFF_NONE ){
143798 pCol->affinity = aff;
143800 if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
143803 for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
143804 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
143806 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
143807 pCol->affinity = SQLITE_AFF_BLOB;
143809 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
143810 pCol->affinity = SQLITE_AFF_BLOB;
143812 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
143813 pCol->affinity = SQLITE_AFF_FLEXNUM;
143817 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
143818 if( pCol->affinity==SQLITE_AFF_NUMERIC
143819 || pCol->affinity==SQLITE_AFF_FLEXNUM
143825 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
143834 n = sqlite3Strlen30(pCol->zCnName);
143835 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
143836 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
143837 if( pCol->zCnName ){
143838 memcpy(&pCol->zCnName[n+1], zType, m+1);
143839 pCol->colFlags |= COLFLAG_HASTYPE;
143844 assert( pTab->pIndex==0 );
143845 sqlite3ColumnSetColl(db, pCol, pColl->zName);
143848 pTab->szTabRow = 1; /* Any non-zero value works */
143857 sqlite3 *db = pParse->db;
143860 savedFlags = db->flags;
143861 db->flags &= ~(u64)SQLITE_FullColNames;
143862 db->flags |= SQLITE_ShortColNames;
143864 db->flags = savedFlags;
143865 if( pParse->nErr ) return 0;
143866 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
143871 pTab->nTabRef = 1;
143872 pTab->zName = 0;
143873 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
143874 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
143876 pTab->iPKey = -1;
143877 if( db->mallocFailed ){
143889 if( pParse->pVdbe ){
143890 return pParse->pVdbe;
143892 if( pParse->pToplevel==0
143893 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
143895 pParse->okConstFactor = 1;
143903 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
143911 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
143919 ** Only if pLimit->pLeft!=0 do the limit registers get
143929 Expr *pLimit = p->pLimit;
143931 if( p->iLimit ) return;
143934 ** "LIMIT -1" always shows all rows. There is some
143940 assert( pLimit->op==TK_LIMIT );
143941 assert( pLimit->pLeft!=0 );
143942 p->iLimit = iLimit = ++pParse->nMem;
143945 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
143950 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
143951 p->nSelectRow = sqlite3LogEst((u64)n);
143952 p->selFlags |= SF_FixedLimit;
143955 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
143960 if( pLimit->pRight ){
143961 p->iOffset = iOffset = ++pParse->nMem;
143962 pParse->nMem++; /* Allocate an extra register for limit+offset */
143963 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
143974 ** Return the appropriate collating sequence for the iCol-th column of
143975 ** the result set for the compound-select statement "p". Return NULL if
143979 ** left-most term of the select that has a collating sequence.
143983 if( p->pPrior ){
143984 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
143989 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
143992 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
143993 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
144008 ExprList *pOrderBy = p->pOrderBy;
144009 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
144010 sqlite3 *db = pParse->db;
144015 struct ExprList_item *pItem = &pOrderBy->a[i];
144016 Expr *pTerm = pItem->pExpr;
144019 if( pTerm->flags & EP_Collate ){
144022 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
144023 if( pColl==0 ) pColl = db->pDfltColl;
144024 pOrderBy->a[i].pExpr =
144025 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
144028 pRet->aColl[i] = pColl;
144029 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
144041 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
144043 ** p->pPrior p
144046 ** There is exactly one reference to the recursive-table in the FROM clause
144047 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
144049 ** The setup-query runs once to generate an initial set of rows that go
144053 ** recursive-table for a recursive-query run. The output of the recursive-query
144078 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
144079 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
144080 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
144082 Select *pFirstRec; /* Left-most recursive term */
144098 if( p->pWin ){
144109 p->nSelectRow = 320; /* 4 billion rows */
144111 pLimit = p->pLimit;
144112 regLimit = p->iLimit;
144113 regOffset = p->iOffset;
144114 p->pLimit = 0;
144115 p->iLimit = p->iOffset = 0;
144116 pOrderBy = p->pOrderBy;
144119 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
144120 if( pSrc->a[i].fg.isRecursive ){
144121 iCurrent = pSrc->a[i].iCursor;
144129 iQueue = pParse->nTab++;
144130 if( p->op==TK_UNION ){
144132 iDistinct = pParse->nTab++;
144139 regCurrent = ++pParse->nMem;
144143 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
144151 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
144152 p->selFlags |= SF_UsesEphemeral;
144156 p->pOrderBy = 0;
144162 ** iDistinct table. pFirstRec is left pointing to the left-most
144165 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
144166 if( pFirstRec->selFlags & SF_Aggregate ){
144170 pFirstRec->op = TK_ALL;
144171 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
144174 /* Store the results of the setup-query in Queue. */
144175 pSetup = pFirstRec->pPrior;
144176 pSetup->pNext = 0;
144179 pSetup->pNext = p;
144188 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
144206 ** the value for the recursive-table. Store the results in the Queue.
144208 pFirstRec->pPrior = 0;
144211 assert( pFirstRec->pPrior==0 );
144212 pFirstRec->pPrior = pSetup;
144219 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
144220 p->pOrderBy = pOrderBy;
144221 p->pLimit = pLimit;
144229 Select *p, /* The right-most of SELECTs to be coded */
144234 ** Handle the special case of a compound-select that originates from a
144247 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
144251 Select *p, /* The right-most of SELECTs to be coded */
144256 int bShowAll = p->pLimit==0;
144257 assert( p->selFlags & SF_MultiValue );
144259 assert( p->selFlags & SF_Values );
144260 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
144261 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
144263 if( p->pWin ) return -1;
144265 if( p->pPrior==0 ) break;
144266 assert( p->pPrior->pNext==p );
144267 p = p->pPrior;
144273 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
144275 p->nSelectRow = nRow;
144276 p = p->pNext;
144287 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
144296 ** "p" points to the right-most of the two queries. the query on the
144297 ** left is p->pPrior. The left query could also be a compound query
144303 ** Example 1: Consider a three-way compound SQL statement.
144311 ** `-----> SELECT b FROM t2
144313 ** `------> SELECT a FROM t1
144317 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
144324 Select *p, /* The right-most of SELECTs to be coded */
144335 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
144337 assert( p && p->pPrior ); /* Calling function guarantees this much */
144338 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
144339 assert( p->selFlags & SF_Compound );
144340 db = pParse->db;
144341 pPrior = p->pPrior;
144343 assert( pPrior->pOrderBy==0 );
144344 assert( pPrior->pLimit==0 );
144352 assert( p->pEList );
144353 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
144357 /* Special handling for a compound-select that originates as a VALUES clause.
144359 if( p->selFlags & SF_MultiValue ){
144368 assert( p->pEList && pPrior->pEList );
144369 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
144372 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
144379 if( p->pOrderBy ){
144384 if( pPrior->pPrior==0 ){
144386 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
144392 switch( p->op ){
144396 assert( !pPrior->pLimit );
144397 pPrior->iLimit = p->iLimit;
144398 pPrior->iOffset = p->iOffset;
144399 pPrior->pLimit = p->pLimit;
144402 pPrior->pLimit = 0;
144406 p->pPrior = 0;
144407 p->iLimit = pPrior->iLimit;
144408 p->iOffset = pPrior->iOffset;
144409 if( p->iLimit ){
144410 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
144412 if( p->iOffset ){
144414 p->iLimit, p->iOffset+1, p->iOffset);
144421 pDelete = p->pPrior;
144422 p->pPrior = pPrior;
144423 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
144424 if( p->pLimit
144425 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
144426 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
144428 p->nSelectRow = sqlite3LogEst((u64)nLimit);
144440 Expr *pLimit; /* Saved values of p->nLimit */
144444 testcase( p->op==TK_EXCEPT );
144445 testcase( p->op==TK_UNION );
144451 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
144457 unionTab = pParse->nTab++;
144458 assert( p->pOrderBy==0 );
144460 assert( p->addrOpenEphm[0] == -1 );
144461 p->addrOpenEphm[0] = addr;
144462 findRightmost(p)->selFlags |= SF_UsesEphemeral;
144463 assert( p->pEList );
144469 assert( !pPrior->pOrderBy );
144479 if( p->op==TK_EXCEPT ){
144482 assert( p->op==TK_UNION );
144485 p->pPrior = 0;
144486 pLimit = p->pLimit;
144487 p->pLimit = 0;
144489 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
144490 sqlite3SelectOpName(p->op)));
144494 assert( p->pOrderBy==0 );
144495 pDelete = p->pPrior;
144496 p->pPrior = pPrior;
144497 p->pOrderBy = 0;
144498 if( p->op==TK_UNION ){
144499 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
144501 sqlite3ExprDelete(db, p->pLimit);
144502 p->pLimit = pLimit;
144503 p->iLimit = 0;
144504 p->iOffset = 0;
144510 assert( p->pEList || db->mallocFailed );
144511 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
144527 default: assert( p->op==TK_INTERSECT ); {
144539 tab1 = pParse->nTab++;
144540 tab2 = pParse->nTab++;
144541 assert( p->pOrderBy==0 );
144544 assert( p->addrOpenEphm[0] == -1 );
144545 p->addrOpenEphm[0] = addr;
144546 findRightmost(p)->selFlags |= SF_UsesEphemeral;
144547 assert( p->pEList );
144561 assert( p->addrOpenEphm[1] == -1 );
144562 p->addrOpenEphm[1] = addr;
144563 p->pPrior = 0;
144564 pLimit = p->pLimit;
144565 p->pLimit = 0;
144567 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
144568 sqlite3SelectOpName(p->op)));
144572 pDelete = p->pPrior;
144573 p->pPrior = pPrior;
144574 if( p->nSelectRow>pPrior->nSelectRow ){
144575 p->nSelectRow = pPrior->nSelectRow;
144577 sqlite3ExprDelete(db, p->pLimit);
144578 p->pLimit = pLimit;
144584 assert( p->pEList );
144606 if( p->pNext==0 ){
144611 if( pParse->nErr ) goto multi_select_end;
144617 ** This section is run by the right-most SELECT statement only.
144618 ** SELECT statements to the left always skip this part. The right-most
144622 if( p->selFlags & SF_UsesEphemeral ){
144626 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
144629 assert( p->pNext==0 );
144630 assert( p->pEList!=0 );
144631 nCol = p->pEList->nExpr;
144637 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
144640 *apColl = db->pDfltColl;
144644 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
144646 int addr = pLoop->addrOpenEphm[i];
144650 assert( pLoop->addrOpenEphm[1]<0 );
144656 pLoop->addrOpenEphm[i] = -1;
144663 pDest->iSdst = dest.iSdst;
144664 pDest->nSdst = dest.nSdst;
144679 if( p->selFlags & SF_Values ){
144684 sqlite3SelectOpName(p->op));
144692 ** The data to be output is contained in pIn->iSdst. There are
144693 ** pIn->nSdst columns to be output. pDest is where the output should
144705 ** If the LIMIT found in p->iLimit is reached, jump immediately to
144718 Vdbe *v = pParse->pVdbe;
144730 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
144734 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
144737 if( pParse->db->mallocFailed ) return 0;
144741 codeOffset(v, p->iOffset, iContinue);
144743 assert( pDest->eDest!=SRT_Exists );
144744 assert( pDest->eDest!=SRT_Table );
144745 switch( pDest->eDest ){
144751 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
144752 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
144753 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
144765 testcase( pIn->nSdst>1 );
144767 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
144768 r1, pDest->zAffSdst, pIn->nSdst);
144769 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
144770 pIn->iSdst, pIn->nSdst);
144778 ** if it is the RHS of a row-value IN operator.
144781 testcase( pIn->nSdst>1 );
144782 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
144789 ** starting at pDest->iSdst. Then the co-routine yields.
144792 if( pDest->iSdst==0 ){
144793 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
144794 pDest->nSdst = pIn->nSdst;
144796 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
144797 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
144810 assert( pDest->eDest==SRT_Output );
144811 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
144818 if( p->iLimit ){
144819 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
144840 ** co-routines. Then run the co-routines in parallel and merge the results
144867 ** ------------- ----------------- -------------- -----------------
144918 Select *p, /* The right-most of SELECTs to be coded */
144923 Select *pSplit; /* Left-most SELECT in the right-hand group */
144928 int regAddrA; /* Address register for select-A coroutine */
144929 int regAddrB; /* Address register for select-B coroutine */
144930 int addrSelectA; /* Address of the select-A coroutine */
144931 int addrSelectB; /* Address of the select-B coroutine */
144932 int regOutA; /* Address register for the output-A subroutine */
144933 int regOutB; /* Address register for the output-B subroutine */
144934 int addrOutA; /* Address of the output-A subroutine */
144935 int addrOutB = 0; /* Address of the output-B subroutine */
144936 int addrEofA; /* Address of the select-A-exhausted subroutine */
144938 int addrEofB; /* Address of the select-B-exhausted subroutine */
144942 int regLimitA; /* Limit register for select-A */
144943 int regLimitB; /* Limit register for select-A */
144945 int savedLimit; /* Saved value of p->iLimit */
144946 int savedOffset; /* Saved value of p->iOffset */
144958 assert( p->pOrderBy!=0 );
144960 db = pParse->db;
144961 v = pParse->pVdbe;
144969 op = p->op;
144970 assert( p->pPrior->pOrderBy==0 );
144971 pOrderBy = p->pOrderBy;
144973 nOrderBy = pOrderBy->nExpr;
144980 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
144982 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
144984 assert( pItem->u.x.iOrderByCol>0 );
144985 if( pItem->u.x.iOrderByCol==i ) break;
144990 pNew->flags |= EP_IntValue;
144991 pNew->u.iValue = i;
144992 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
144993 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
145009 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
145011 assert( pItem->u.x.iOrderByCol>0 );
145012 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
145013 aPermute[i] = pItem->u.x.iOrderByCol - 1;
145027 int nExpr = p->pEList->nExpr;
145028 assert( nOrderBy>=nExpr || db->mallocFailed );
145029 regPrev = pParse->nMem+1;
145030 pParse->nMem += nExpr+1;
145036 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
145037 pKeyDup->aSortFlags[i] = 0;
145048 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
145050 assert( pSplit->pPrior->pNext==pSplit );
145057 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
145059 pPrior = pSplit->pPrior;
145061 pSplit->pPrior = 0;
145062 pPrior->pNext = 0;
145063 assert( p->pOrderBy == pOrderBy );
145064 assert( pOrderBy!=0 || db->mallocFailed );
145065 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
145066 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
145067 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
145071 if( p->iLimit && op==TK_ALL ){
145072 regLimitA = ++pParse->nMem;
145073 regLimitB = ++pParse->nMem;
145074 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
145080 sqlite3ExprDelete(db, p->pLimit);
145081 p->pLimit = 0;
145083 regAddrA = ++pParse->nMem;
145084 regAddrB = ++pParse->nMem;
145085 regOutA = ++pParse->nMem;
145086 regOutB = ++pParse->nMem;
145090 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
145093 ** left of the compound operator - the "A" select.
145098 pPrior->iLimit = regLimitA;
145105 ** the right - the "B" select
145110 savedLimit = p->iLimit;
145111 savedOffset = p->iOffset;
145112 p->iLimit = regLimitB;
145113 p->iOffset = 0;
145116 p->iLimit = savedLimit;
145117 p->iOffset = savedOffset;
145145 VdbeNoopComment((v, "eof-A subroutine"));
145150 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
145158 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
145160 VdbeNoopComment((v, "eof-B subroutine"));
145168 VdbeNoopComment((v, "A-lt-B subroutine"));
145181 VdbeNoopComment((v, "A-eq-B subroutine"));
145189 VdbeNoopComment((v, "A-gt-B subroutine"));
145218 if( pSplit->pPrior ){
145220 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
145222 pSplit->pPrior = pPrior;
145223 pPrior->pNext = pSplit;
145224 sqlite3ExprListDelete(db, pPrior->pOrderBy);
145225 pPrior->pOrderBy = 0;
145230 return pParse->nErr!=0;
145245 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
145268 ** when processing a non-matched row of the left.
145285 ** a column in table number iTable with a copy of the iColumn-th
145302 && pExpr->w.iJoin==pSubst->iTable
145305 pExpr->w.iJoin = pSubst->iNewTable;
145307 if( pExpr->op==TK_COLUMN
145308 && pExpr->iTable==pSubst->iTable
145312 if( pExpr->iColumn<0 ){
145313 pExpr->op = TK_NULL;
145321 iColumn = pExpr->iColumn;
145323 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
145324 assert( pExpr->pRight==0 );
145325 pCopy = pSubst->pEList->a[iColumn].pExpr;
145327 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
145329 sqlite3 *db = pSubst->pParse->db;
145330 if( pSubst->isOuterJoin
145331 && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable)
145336 ifNullRow.iTable = pSubst->iNewTable;
145337 ifNullRow.iColumn = -99;
145343 if( db->mallocFailed ){
145347 if( pSubst->isOuterJoin ){
145351 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
145352 pExpr->flags & (EP_OuterON|EP_InnerON));
145356 if( pExpr->op==TK_TRUEFALSE ){
145357 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
145358 pExpr->op = TK_INTEGER;
145363 ** just as it did when it was a column of a view or sub-query. */
145365 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
145366 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
145367 pSubst->pCList->a[iColumn].pExpr
145369 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
145370 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
145371 (pColl ? pColl->zName : "BINARY")
145379 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
145380 pExpr->iTable = pSubst->iNewTable;
145382 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
145383 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
145385 substSelect(pSubst, pExpr->x.pSelect, 1);
145387 substExprList(pSubst, pExpr->x.pList);
145391 Window *pWin = pExpr->y.pWin;
145392 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
145393 substExprList(pSubst, pWin->pPartition);
145394 substExprList(pSubst, pWin->pOrderBy);
145406 for(i=0; i<pList->nExpr; i++){
145407 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
145413 int doPrior /* Do substitutes on p->pPrior too */
145420 substExprList(pSubst, p->pEList);
145421 substExprList(pSubst, p->pGroupBy);
145422 substExprList(pSubst, p->pOrderBy);
145423 p->pHaving = substExpr(pSubst, p->pHaving);
145424 p->pWhere = substExpr(pSubst, p->pWhere);
145425 pSrc = p->pSrc;
145427 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
145428 substSelect(pSubst, pItem->pSelect, 1);
145429 if( pItem->fg.isTabFunc ){
145430 substExprList(pSubst, pItem->u1.pFuncArg);
145433 }while( doPrior && (p = p->pPrior)!=0 );
145443 ** pSrcItem->colUsed mask.
145447 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
145448 pItem = pWalker->u.pSrcItem;
145449 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
145450 if( pExpr->iColumn<0 ) return WRC_Continue;
145451 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
145459 if( NEVER(pSrcItem->pTab==0) ) return;
145464 pSrcItem->colUsed = 0;
145480 ** If pSrc contains any sub-selects, call this routine recursively
145481 ** on the FROM clause of each such sub-select, with iExcept set to -1.
145491 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
145494 assert( pItem->iCursor < aCsrMap[0] );
145495 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
145496 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
145498 pItem->iCursor = aCsrMap[pItem->iCursor+1];
145499 for(p=pItem->pSelect; p; p=p->pPrior){
145500 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
145510 int *aCsrMap = pWalker->u.aiCol;
145522 int op = pExpr->op;
145524 renumberCursorDoMapping(pWalker, &pExpr->iTable);
145527 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
145535 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
145557 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
145572 while( pSel->pPrior ){
145573 pSel = pSel->pPrior;
145575 return pSel->pEList;
145579 ** Return true if any of the result-set columns in the compound query
145586 assert( p->pEList!=0 );
145587 assert( p->pPrior!=0 );
145588 pList = p->pEList;
145589 for(ii=0; ii<pList->nExpr; ii++){
145592 assert( pList->a[ii].pExpr!=0 );
145593 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
145594 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
145595 assert( pSub1->pEList!=0 );
145596 assert( pSub1->pEList->nExpr>ii );
145597 assert( pSub1->pEList->a[ii].pExpr!=0 );
145598 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
145642 ** other than the one FROM-clause subquery that is a candidate
145644 ** from 2015-02-09.)
145658 ** sub-queries that were excluded from this optimization. Restriction
145673 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
145674 ** accidentally carried the comment forward until 2014-09-15. Original
145711 ** The parent and sub-query may contain WHERE clauses. Subject to
145718 ** Also, each component of the sub-query must return the same number
145721 ** such (illegal) sub-query is flattened. The caller will detect the
145724 ** (18) If the sub-query is a compound select, then all terms of the
145731 ** (20) If the sub-query is a compound select, then it must not use
145742 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
145748 ** The subquery may not be an aggregate that uses the built-in min() or
145771 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
145774 ** If flattening is not attempted, this routine is a no-op and returns 0.
145783 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
145786 const char *zSavedAuthContext = pParse->zAuthContext;
145789 Select *pSub1; /* Pointer to the rightmost select in sub-query */
145793 int iNewParent = -1;/* Replacement table for iParent */
145798 sqlite3 *db = pParse->db;
145805 assert( p->pPrior==0 );
145807 pSrc = p->pSrc;
145808 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
145809 pSubitem = &pSrc->a[iFrom];
145810 iParent = pSubitem->iCursor;
145811 pSub = pSubitem->pSelect;
145815 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
145818 pSubSrc = pSub->pSrc;
145822 ** because they could be computed at compile-time. But when LIMIT and OFFSET
145825 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
145826 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
145827 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
145830 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
145831 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
145832 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
145835 if( p->pOrderBy && pSub->pOrderBy ){
145838 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
145839 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
145840 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
145843 if( pSub->selFlags & (SF_Recursive) ){
145862 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
145863 if( pSubSrc->nSrc>1 /* (3a) */
145864 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
145865 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
145866 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
145873 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
145874 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
145879 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes );
145881 /* Restriction (17): If the sub-query is a compound SELECT, then it must
145886 if( pSub->pPrior ){
145888 if( pSub->pOrderBy ){
145891 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
145894 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
145895 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
145896 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
145897 assert( pSub->pSrc!=0 );
145898 assert( (pSub->selFlags & SF_Recursive)==0 );
145899 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
145900 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
145901 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
145902 || pSub1->pSrc->nSrc<1 /* (17c) */
145904 || pSub1->pWin /* (17e) */
145909 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
145911 ** omitted on left-hand tables of the right join that is being
145915 testcase( pSub1->pSrc->nSrc>1 );
145919 if( p->pOrderBy ){
145920 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
145921 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
145926 if( (p->selFlags & SF_Recursive) ) return 0;
145931 if( pSrc->nSrc>1 ){
145932 if( pParse->nSelect>500 ) return 0;
145934 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
145935 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
145941 pSub->selId, pSub, iFrom));
145944 pParse->zAuthContext = pSubitem->zName;
145947 pParse->zAuthContext = zSavedAuthContext;
145950 pSub1 = pSubitem->pSelect;
145951 sqlite3DbFree(db, pSubitem->zDatabase);
145952 sqlite3DbFree(db, pSubitem->zName);
145953 sqlite3DbFree(db, pSubitem->zAlias);
145954 pSubitem->zDatabase = 0;
145955 pSubitem->zName = 0;
145956 pSubitem->zAlias = 0;
145957 pSubitem->pSelect = 0;
145958 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
145960 /* If the sub-query is a compound SELECT statement, then (by restrictions
145964 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
145967 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
145968 ** OFFSET clauses and joins them to the left-hand-side of the original
145970 ** select statements in the compound sub-query.
145991 ** We call this the "compound-subquery flattening".
145993 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
145995 ExprList *pOrderBy = p->pOrderBy;
145996 Expr *pLimit = p->pLimit;
145997 Select *pPrior = p->pPrior;
145998 Table *pItemTab = pSubitem->pTab;
145999 pSubitem->pTab = 0;
146000 p->pOrderBy = 0;
146001 p->pPrior = 0;
146002 p->pLimit = 0;
146004 p->pLimit = pLimit;
146005 p->pOrderBy = pOrderBy;
146006 p->op = TK_ALL;
146007 pSubitem->pTab = pItemTab;
146009 p->pPrior = pPrior;
146011 pNew->selId = ++pParse->nSelect;
146012 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
146015 pNew->pPrior = pPrior;
146016 if( pPrior ) pPrior->pNext = pNew;
146017 pNew->pNext = p;
146018 p->pPrior = pNew;
146019 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
146020 " creates %u as peer\n",pNew->selId));
146022 assert( pSubitem->pSelect==0 );
146025 if( db->mallocFailed ){
146026 pSubitem->pSelect = pSub1;
146035 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
146037 if( ALWAYS(pSubitem->pTab!=0) ){
146038 Table *pTabToDel = pSubitem->pTab;
146039 if( pTabToDel->nTabRef==1 ){
146044 testcase( pToplevel->earlyCleanup );
146046 pTabToDel->nTabRef--;
146048 pSubitem->pTab = 0;
146051 /* The following loop runs once for each term in a compound-subquery
146053 ** of flattening - a flattening other than a compound-subquery flattening -
146065 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
146068 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
146070 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
146071 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
146072 pSrc = pParent->pSrc; /* FROM clause of the outer query */
146075 jointype = pSubitem->fg.jointype; /* First time through the loop */
146094 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
146096 pParent->pSrc = pSrc;
146103 SrcItem *pItem = &pSrc->a[i+iFrom];
146104 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
146105 assert( pItem->fg.isTabFunc==0 );
146106 *pItem = pSubSrc->a[i];
146107 pItem->fg.jointype |= ltorj;
146108 iNewParent = pSubSrc->a[i].iCursor;
146109 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
146111 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
146112 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
146126 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
146127 /* At this point, any non-zero iOrderByCol values indicate that the
146134 ** function attempts to flatten a compound sub-query into pParent
146135 ** (the only way this can happen is if the compound sub-query is
146136 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
146137 ExprList *pOrderBy = pSub->pOrderBy;
146138 for(i=0; i<pOrderBy->nExpr; i++){
146139 pOrderBy->a[i].u.x.iOrderByCol = 0;
146141 assert( pParent->pOrderBy==0 );
146142 pParent->pOrderBy = pOrderBy;
146143 pSub->pOrderBy = 0;
146145 pWhere = pSub->pWhere;
146146 pSub->pWhere = 0;
146151 if( pParent->pWhere ){
146152 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
146154 pParent->pWhere = pWhere;
146157 if( db->mallocFailed==0 ){
146163 x.pEList = pSub->pEList;
146170 pParent->selFlags |= pSub->selFlags & SF_Compound;
146171 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
146179 if( pSub->pLimit ){
146180 pParent->pLimit = pSub->pLimit;
146181 pSub->pLimit = 0;
146187 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
146215 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
146239 assert( pColumn->op==TK_COLUMN );
146244 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
146248 /* 2018-10-25 ticket [cf5ed20f]
146250 for(i=0; i<pConst->nConst; i++){
146251 const Expr *pE2 = pConst->apExpr[i*2];
146252 assert( pE2->op==TK_COLUMN );
146253 if( pE2->iTable==pColumn->iTable
146254 && pE2->iColumn==pColumn->iColumn
146260 pConst->bHasAffBlob = 1;
146263 pConst->nConst++;
146264 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
146265 pConst->nConst*2*sizeof(Expr*));
146266 if( pConst->apExpr==0 ){
146267 pConst->nConst = 0;
146269 pConst->apExpr[pConst->nConst*2-2] = pColumn;
146270 pConst->apExpr[pConst->nConst*2-1] = pValue;
146277 ** is part of the AND-connected terms of the expression. For each term
146283 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
146288 if( pExpr->op==TK_AND ){
146289 findConstInWhere(pConst, pExpr->pRight);
146290 findConstInWhere(pConst, pExpr->pLeft);
146293 if( pExpr->op!=TK_EQ ) return;
146294 pRight = pExpr->pRight;
146295 pLeft = pExpr->pLeft;
146298 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
146301 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
146310 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
146312 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
146321 if( pConst->pOomFault[0] ) return WRC_Prune;
146322 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
146323 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
146329 for(i=0; i<pConst->nConst; i++){
146330 Expr *pColumn = pConst->apExpr[i*2];
146332 if( pColumn->iTable!=pExpr->iTable ) continue;
146333 if( pColumn->iColumn!=pExpr->iColumn ) continue;
146338 pConst->nChng++;
146341 assert( pExpr->pLeft==0 );
146342 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
146343 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
146352 ** any substitutions based on the contents of pWalker->u.pConst should
146358 ** one of the columns in pWalker->u.pConst, or
146363 ** pWalker->u.pConst.
146366 WhereConst *pConst = pWalker->u.pConst;
146371 if( pConst->bHasAffBlob ){
146372 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
146373 || pExpr->op==TK_IS
146375 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
146376 if( pConst->pOomFault[0] ) return WRC_Prune;
146377 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
146378 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
146382 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
146386 ** The WHERE-clause constant propagation optimization.
146389 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
146421 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
146444 x.pOomFault = &pParse->db->mallocFailed;
146450 if( ALWAYS(p->pSrc!=0)
146451 && p->pSrc->nSrc>0
146452 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
146461 findConstInWhere(&x, p->pWhere);
146470 sqlite3WalkExpr(&w, p->pWhere);
146471 sqlite3DbFree(x.pParse->db, x.apExpr);
146482 ** push WHERE clause expression pExpr down to FROM clause sub-query
146489 ** BY clause of all window function used by the sub-query. It is safe
146495 ** * the sub-query uses only one distinct window frame, and
146499 assert( pSubq->pWin->pPartition );
146500 assert( (pSubq->selFlags & SF_MultiPart)==0 );
146501 assert( pSubq->pPrior==0 );
146502 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
146512 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
146516 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
146524 ** (1) (** This restriction was removed on 2017-09-29. We used to
146542 ** of a LEFT JOIN where iCursor is not the right-hand table of that
146561 ** (6b) The inner query is a compound and uses window-functions.
146565 ** all window-functions used by the sub-query. It is safe to
146567 ** window over which any window-function is calculated.
146587 ** Without this restriction, the push-down optimization might move
146592 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
146600 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
146613 pSrc = &pSrcList->a[iSrc];
146615 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){
146618 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){
146622 if( pSubq->pPrior ){
146625 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
146626 u8 op = pSel->op;
146633 if( pSel->pWin ) return 0; /* restriction (6b) */
146639 ** non-BINARY collating sequence. */
146640 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
146642 const ExprList *pList = pSel->pEList;
146644 for(ii=0; ii<pList->nExpr; ii++){
146645 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
146654 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
146665 for(pX=pSubq; pX; pX=pX->pPrior){
146666 assert( (pX->selFlags & (SF_Recursive))==0 );
146671 if( pSubq->pLimit!=0 ){
146674 while( pWhere->op==TK_AND ){
146675 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
146676 pWhere = pWhere->pLeft;
146681 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */
146685 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
146690 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT)!=0 ){
146699 || pWhere->w.iJoin!=iCursor)
146704 && pWhere->w.iJoin!=iCursor
146711 if( ViewCanHaveRowid && (pWhere->op==TK_ISNULL || pWhere->op==TK_NOTNULL) ){
146712 Expr *pLeft = pWhere->pLeft;
146714 && pLeft->op==TK_COLUMN
146715 && pLeft->iColumn < 0
146724 pSubq->selFlags |= SF_PushDown;
146727 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
146728 unsetJoinExpr(pNew, -1, 1);
146730 x.iTable = pSrc->iCursor;
146731 x.iNewTable = pSrc->iCursor;
146733 x.pEList = pSubq->pEList;
146737 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
146738 /* Restriction 6c has prevented push-down in this case */
146739 sqlite3ExprDelete(pParse->db, pNew);
146740 nChng--;
146744 if( pSubq->selFlags & SF_Aggregate ){
146745 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
146747 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
146749 pSubq = pSubq->pPrior;
146757 ** Check to see if a subquery contains result-set columns that are
146758 ** never used. If it does, change the value of those result-set columns
146773 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
146776 assert( pItem->pTab!=0 );
146777 pTab = pItem->pTab;
146778 assert( pItem->pSelect!=0 );
146779 pSub = pItem->pSelect;
146780 assert( pSub->pEList->nExpr==pTab->nCol );
146781 for(pX=pSub; pX; pX=pX->pPrior){
146782 if( (pX->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){
146783 testcase( pX->selFlags & SF_Distinct );
146784 testcase( pX->selFlags & SF_Aggregate );
146787 if( pX->pPrior && pX->op!=TK_ALL ){
146793 if( pX->pWin ){
146800 colUsed = pItem->colUsed;
146801 if( pSub->pOrderBy ){
146802 ExprList *pList = pSub->pOrderBy;
146803 for(j=0; j<pList->nExpr; j++){
146804 u16 iCol = pList->a[j].u.x.iOrderByCol;
146806 iCol--;
146807 colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
146811 nCol = pTab->nCol;
146813 Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT;
146815 for(pX=pSub; pX; pX=pX->pPrior) {
146816 Expr *pY = pX->pEList->a[j].pExpr;
146817 if( pY->op==TK_NULL ) continue;
146818 pY->op = TK_NULL;
146820 pX->selFlags |= SF_PushDown;
146852 assert( pFunc->op==TK_AGG_FUNCTION );
146855 pEList = pFunc->x.pList;
146857 || pEList->nExpr!=1
146864 zFunc = pFunc->u.zToken;
146867 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
146877 assert( pOrderBy!=0 || db->mallocFailed );
146878 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
146884 ** The second argument is the associated aggregate-info object. This
146889 ** where table is a database table, not a sub-select or view. If the query
146903 assert( !p->pGroupBy );
146905 if( p->pWhere
146906 || p->pEList->nExpr!=1
146907 || p->pSrc->nSrc!=1
146908 || p->pSrc->a[0].pSelect
146909 || pAggInfo->nFunc!=1
146910 || p->pHaving
146914 pTab = p->pSrc->a[0].pTab;
146918 pExpr = p->pEList->a[0].pExpr;
146920 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
146921 if( pExpr->pAggInfo!=pAggInfo ) return 0;
146922 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
146923 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
146932 ** If the source-list item passed as an argument was augmented with an
146936 ** pFrom->pIndex and return SQLITE_OK.
146939 Table *pTab = pFrom->pTab;
146940 char *zIndexedBy = pFrom->u1.zIndexedBy;
146943 assert( pFrom->fg.isIndexedBy!=0 );
146945 for(pIdx=pTab->pIndex;
146946 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
146947 pIdx=pIdx->pNext
146951 pParse->checkSchema = 1;
146954 assert( pFrom->fg.isCte==0 );
146955 pFrom->u2.pIBIndex = pIdx;
146990 if( p->pPrior==0 ) return WRC_Continue;
146991 if( p->pOrderBy==0 ) return WRC_Continue;
146992 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
146994 a = p->pOrderBy->a;
146996 /* If iOrderByCol is already non-zero, then it has already been matched
146998 ** SELECT is rewritten for window-functions processing and then passed
147003 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
147004 if( a[i].pExpr->flags & EP_Collate ) break;
147010 pParse = pWalker->pParse;
147011 db = pParse->db;
147018 p->pSrc = pNewSrc;
147019 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
147020 p->op = TK_SELECT;
147021 p->pWhere = 0;
147022 pNew->pGroupBy = 0;
147023 pNew->pHaving = 0;
147024 pNew->pOrderBy = 0;
147025 p->pPrior = 0;
147026 p->pNext = 0;
147027 p->pWith = 0;
147029 p->pWinDefn = 0;
147031 p->selFlags &= ~SF_Compound;
147032 assert( (p->selFlags & SF_Converted)==0 );
147033 p->selFlags |= SF_Converted;
147034 assert( pNew->pPrior!=0 );
147035 pNew->pPrior->pNext = pNew;
147036 pNew->pLimit = 0;
147041 ** Check to see if the FROM clause term pFrom has table-valued function
147043 ** non-zero, since pFrom is not allowed to be a table-valued function.
147046 if( pFrom->fg.isTabFunc ){
147047 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
147057 ** FROM clause element pItem is really a common-table-expression (CTE)
147061 ** If a non-NULL value is returned, set *ppContext to point to the With
147069 const char *zName = pItem->zName;
147071 assert( pItem->zDatabase==0 );
147073 for(p=pWith; p; p=p->pOuter){
147075 for(i=0; i<p->nCte; i++){
147076 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
147078 return &p->a[i];
147081 if( p->bView ) break;
147087 ** with the inner-most WITH clause being at the top of the stack.
147111 if( pParse->nErr==0 ){
147112 assert( pParse->pWith!=pWith );
147113 pWith->pOuter = pParse->pWith;
147114 pParse->pWith = pWith;
147123 ** pParse->pWith linked list). And if currently processing a CTE
147127 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
147142 assert( pFrom->pTab==0 );
147143 if( pParse->pWith==0 ){
147147 if( pParse->nErr ){
147148 /* Prior errors might have left pParse->pWith in a goofy state, so
147152 if( pFrom->zDatabase!=0 ){
147157 if( pFrom->fg.notCte ){
147165 pCte = searchWith(pParse->pWith, pFrom, &pWith);
147167 sqlite3 *db = pParse->db;
147171 Select *pLeft; /* Left-most SELECT statement */
147172 Select *pRecTerm; /* Left-most recursive term */
147174 With *pSavedWith; /* Initial value of pParse->pWith */
147175 int iRecTab = -1; /* Cursor for recursive table */
147178 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
147180 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
147182 if( pCte->zCteErr ){
147183 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
147188 assert( pFrom->pTab==0 );
147191 pCteUse = pCte->pUse;
147193 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
147200 pCteUse->eM10d = pCte->eM10d;
147202 pFrom->pTab = pTab;
147203 pTab->nTabRef = 1;
147204 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
147205 pTab->iPKey = -1;
147206 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
147207 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
147208 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
147209 if( db->mallocFailed ) return 2;
147210 pFrom->pSelect->selFlags |= SF_CopyCte;
147211 assert( pFrom->pSelect );
147212 if( pFrom->fg.isIndexedBy ){
147213 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
147216 pFrom->fg.isCte = 1;
147217 pFrom->u2.pCteUse = pCteUse;
147218 pCteUse->nUse++;
147221 pRecTerm = pSel = pFrom->pSelect;
147222 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
147223 while( bMayRecursive && pRecTerm->op==pSel->op ){
147225 SrcList *pSrc = pRecTerm->pSrc;
147226 assert( pRecTerm->pPrior!=0 );
147227 for(i=0; i<pSrc->nSrc; i++){
147228 SrcItem *pItem = &pSrc->a[i];
147229 if( pItem->zDatabase==0
147230 && pItem->zName!=0
147231 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
147233 pItem->pTab = pTab;
147234 pTab->nTabRef++;
147235 pItem->fg.isRecursive = 1;
147236 if( pRecTerm->selFlags & SF_Recursive ){
147238 "multiple references to recursive table: %s", pCte->zName
147242 pRecTerm->selFlags |= SF_Recursive;
147243 if( iRecTab<0 ) iRecTab = pParse->nTab++;
147244 pItem->iCursor = iRecTab;
147247 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
147248 pRecTerm = pRecTerm->pPrior;
147251 pCte->zCteErr = "circular reference: %s";
147252 pSavedWith = pParse->pWith;
147253 pParse->pWith = pWith;
147254 if( pSel->selFlags & SF_Recursive ){
147257 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
147258 assert( pRecTerm->pNext!=0 );
147259 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
147260 assert( pRecTerm->pWith==0 );
147261 pRecTerm->pWith = pSel->pWith;
147263 pRecTerm->pWith = 0;
147265 pParse->pWith = pSavedWith;
147270 pParse->pWith = pSavedWith;
147274 pParse->pWith = pWith;
147276 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
147277 pEList = pLeft->pEList;
147278 if( pCte->pCols ){
147279 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
147281 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
147283 pParse->pWith = pSavedWith;
147286 pEList = pCte->pCols;
147289 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
147291 if( pSel->selFlags & SF_Recursive ){
147292 pCte->zCteErr = "multiple recursive references: %s";
147294 pCte->zCteErr = "recursive reference in a subquery: %s";
147298 pCte->zCteErr = 0;
147299 pParse->pWith = pSavedWith;
147316 Parse *pParse = pWalker->pParse;
147317 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
147318 With *pWith = findRightmost(p)->pWith;
147320 assert( pParse->pWith==pWith || pParse->nErr );
147321 pParse->pWith = pWith->pOuter;
147329 ** sub-query in the FROM clause of a SELECT statement. This function
147335 Select *pSel = pFrom->pSelect;
147339 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
147341 pTab->nTabRef = 1;
147342 if( pFrom->zAlias ){
147343 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
147345 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
147347 while( pSel->pPrior ){ pSel = pSel->pPrior; }
147348 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
147349 pTab->iPKey = -1;
147350 pTab->eTabType = TABTYP_VIEW;
147351 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
147353 /* The usual case - do not allow ROWID on a subquery */
147354 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
147357 pTab->tabFlags |= TF_Ephemeral | sqlite3Config.mNoVisibleRowid;
147359 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
147378 N--;
147380 if( pBase->fg.isUsing==0 ) continue;
147381 if( NEVER(pBase->u3.pUsing==0) ) continue;
147382 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
147395 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
147397 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
147413 Parse *pParse = pWalker->pParse;
147418 sqlite3 *db = pParse->db;
147420 u16 selFlags = p->selFlags;
147423 p->selFlags |= SF_Expanded;
147424 if( db->mallocFailed ){
147427 assert( p->pSrc!=0 );
147431 if( pWalker->eCode ){
147433 p->selId = ++pParse->nSelect;
147435 pTabList = p->pSrc;
147436 pEList = p->pEList;
147437 if( pParse->pWith && (p->selFlags & SF_View) ){
147438 if( p->pWith==0 ){
147439 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
147440 if( p->pWith==0 ){
147444 p->pWith->bView = 1;
147446 sqlite3WithPush(pParse, p->pWith, 0);
147457 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147459 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
147460 if( pFrom->pTab ) continue;
147461 assert( pFrom->fg.isRecursive==0 );
147462 if( pFrom->zName==0 ){
147464 Select *pSel = pFrom->pSelect;
147465 /* A sub-query in the FROM clause of a SELECT */
147467 assert( pFrom->pTab==0 );
147474 pTab = pFrom->pTab;
147479 assert( pFrom->pTab==0 );
147480 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
147482 if( pTab->nTabRef>=0xffff ){
147484 pTab->zName);
147485 pFrom->pTab = 0;
147488 pTab->nTabRef++;
147495 u8 eCodeOrig = pWalker->eCode;
147497 assert( pFrom->pSelect==0 );
147499 if( (db->flags & SQLITE_EnableView)==0
147500 && pTab->pSchema!=db->aDb[1].pSchema
147503 pTab->zName);
147505 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
147509 && pFrom->fg.fromDDL
147510 && ALWAYS(pTab->u.vtab.p!=0)
147511 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
147514 pTab->zName);
147518 nCol = pTab->nCol;
147519 pTab->nCol = -1;
147520 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
147521 sqlite3WalkSelect(pWalker, pFrom->pSelect);
147522 pWalker->eCode = eCodeOrig;
147523 pTab->nCol = nCol;
147529 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
147536 assert( db->mallocFailed==0 || pParse->nErr!=0 );
147537 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
147552 for(k=0; k<pEList->nExpr; k++){
147553 pE = pEList->a[k].pExpr;
147554 if( pE->op==TK_ASTERISK ) break;
147555 assert( pE->op!=TK_DOT || pE->pRight!=0 );
147556 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
147557 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
147558 elistFlags |= pE->flags;
147560 if( k<pEList->nExpr ){
147566 struct ExprList_item *a = pEList->a;
147568 int flags = pParse->db->flags;
147572 for(k=0; k<pEList->nExpr; k++){
147574 elistFlags |= pE->flags;
147575 pRight = pE->pRight;
147576 assert( pE->op!=TK_DOT || pRight!=0 );
147577 if( pE->op!=TK_ASTERISK
147578 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
147584 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
147585 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
147595 if( pE->op==TK_DOT ){
147597 assert( pE->pLeft!=0 );
147598 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
147599 zTName = pE->pLeft->u.zToken;
147600 assert( ExprUseWOfst(pE->pLeft) );
147601 iErrOfst = pE->pRight->w.iOfst;
147604 iErrOfst = pE->w.iOfst;
147606 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147608 Table *pTab = pFrom->pTab; /* Table for this data source */
147609 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
147615 if( (zTabName = pFrom->zAlias)==0 ){
147616 zTabName = pTab->zName;
147618 if( db->mallocFailed ) break;
147619 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
147620 if( pFrom->fg.isNestedFrom ){
147621 assert( pFrom->pSelect!=0 );
147622 pNestedFrom = pFrom->pSelect->pEList;
147624 assert( pNestedFrom->nExpr==pTab->nCol );
147631 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147632 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
147634 if( i+1<pTabList->nSrc
147640 for(ii=0; ii<pUsing->nId; ii++){
147641 const char *zUName = pUsing->a[ii].zName;
147646 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
147647 assert( pX->zEName==0 );
147648 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
147649 pX->fg.eEName = ENAME_TAB;
147650 pX->fg.bUsingTerm = 1;
147657 nAdd = pTab->nCol;
147663 if( j==pTab->nCol ){
147667 zName = pTab->aCol[j].zCnName;
147669 /* If pTab is actually an SF_NestedFrom sub-select, do not
147671 if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID ){
147677 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0
147683 ** result-set list unless the SELECT has the SF_IncludeHidden
147686 if( (p->selFlags & SF_IncludeHidden)==0
147687 && IsHiddenColumn(&pTab->aCol[j])
147691 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
147702 if( pFrom->fg.isUsing
147703 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
147711 if( (pTabList->nSrc>1
147712 && ( (pFrom->fg.jointype & JT_LTORJ)==0
147714 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
147722 if( IN_RENAME_OBJECT && pE->pLeft ){
147723 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
147737 pX = &pNew->a[pNew->nExpr-1];
147738 assert( pX->zEName==0 );
147740 if( pNestedFrom && (!ViewCanHaveRowid || j<pNestedFrom->nExpr) ){
147741 assert( j<pNestedFrom->nExpr );
147742 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
147743 testcase( pX->zEName==0 );
147745 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
147747 testcase( pX->zEName==0 );
147749 pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID : ENAME_TAB);
147750 if( (pFrom->fg.isUsing
147751 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
147753 || (j<pTab->nCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND))
147755 pX->fg.bNoExpand = 1;
147758 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
147759 pX->fg.eEName = ENAME_NAME;
147761 pX->zEName = sqlite3DbStrDup(db, zName);
147762 pX->fg.eEName = ENAME_NAME;
147776 p->pEList = pNew;
147778 if( p->pEList ){
147779 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
147784 p->selFlags |= SF_ComplexResult;
147789 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
147816 ** The calling function can detect the problem by looking at pParse->nErr
147817 ** and/or pParse->db->mallocFailed.
147823 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
147840 ** For each FROM-clause subquery, add Column.zType, Column.zColl, and
147855 if( p->selFlags & SF_HasTypeInfo ) return;
147856 p->selFlags |= SF_HasTypeInfo;
147857 pParse = pWalker->pParse;
147858 testcase( (p->selFlags & SF_Resolved)==0 );
147859 assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
147860 pTabList = p->pSrc;
147861 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147862 Table *pTab = pFrom->pTab;
147864 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
147865 /* A sub-query in the FROM clause of a SELECT */
147866 Select *pSel = pFrom->pSelect;
147878 ** the Table structures of all FROM-clause subqueries in a
147899 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
147900 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
147912 assert( p!=0 || pParse->db->mallocFailed );
147913 assert( pParse->db->pParse==pParse );
147914 if( pParse->db->mallocFailed ) return;
147915 if( p->selFlags & SF_HasTypeInfo ) return;
147917 if( pParse->nErr ) return;
147919 if( pParse->nErr ) return;
147929 for(ii=0; ii<pAggInfo->nColumn; ii++){
147930 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
147932 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
147934 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
147935 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
147936 pCol->iSorterColumn,
147937 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
147938 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
147940 for(ii=0; ii<pAggInfo->nFunc; ii++){
147941 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
147942 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
147943 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
147949 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
147971 assert( pAggInfo->iFirstReg==0 );
147972 pNC->ncFlags |= NC_InAggFunc;
147973 for(i=0; i<pAggInfo->nFunc; i++){
147974 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
147975 assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION );
147977 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
147978 if( pExpr->pLeft ){
147979 assert( pExpr->pLeft->op==TK_ORDER );
147980 assert( ExprUseXList(pExpr->pLeft) );
147981 sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList);
147986 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
147990 pNC->ncFlags &= ~NC_InAggFunc;
148004 NameContext *pNC /* Name context used to resolve agg-func args */
148006 assert( pAggInfo->iFirstReg==0 );
148008 assert( pSelect->pGroupBy!=0 );
148009 pAggInfo->nColumn = pAggInfo->nAccumulator;
148010 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
148011 int mx = pSelect->pGroupBy->nExpr - 1;
148013 for(j=0; j<pAggInfo->nColumn; j++){
148014 k = pAggInfo->aCol[j].iSorterColumn;
148017 pAggInfo->nSortingColumn = mx+1;
148026 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
148027 printf("data-cursor=%d index={%d,%d}\n",
148028 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
148029 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
148046 if( pExpr->pAggInfo==0 ) return WRC_Continue;
148047 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
148048 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
148049 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
148050 pAggInfo = pExpr->pAggInfo;
148051 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue;
148052 assert( pExpr->iAgg>=0 );
148053 pCol = &pAggInfo->aCol[pExpr->iAgg];
148054 pExpr->op = TK_AGG_COLUMN;
148055 pExpr->iTable = pCol->iTable;
148056 pExpr->iColumn = pCol->iColumn;
148062 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
148071 for(i=0; i<pAggInfo->nFunc; i++){
148072 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
148079 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
148080 ** register in this block is stored in pAggInfo->iFirstReg.
148096 assert( pAggInfo->iFirstReg==0 );
148097 pAggInfo->iFirstReg = pParse->nMem + 1;
148098 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
148110 Vdbe *v = pParse->pVdbe;
148113 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
148114 assert( pAggInfo->iFirstReg>0 );
148115 assert( pParse->db->pParse==pParse );
148116 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
148118 if( pParse->nErr ) return;
148119 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
148120 pAggInfo->iFirstReg+nReg-1);
148121 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
148122 if( pFunc->iDistinct>=0 ){
148123 Expr *pE = pFunc->pFExpr;
148125 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
148128 pFunc->iDistinct = -1;
148130 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
148131 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
148132 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
148133 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
148134 pFunc->pFunc->zName));
148137 if( pFunc->iOBTab>=0 ){
148141 assert( pFunc->pFExpr->pLeft!=0 );
148142 assert( pFunc->pFExpr->pLeft->op==TK_ORDER );
148143 assert( ExprUseXList(pFunc->pFExpr->pLeft) );
148144 pOBList = pFunc->pFExpr->pLeft->x.pList;
148145 if( !pFunc->bOBUnique ){
148148 if( pFunc->bOBPayload ){
148150 assert( ExprUseXList(pFunc->pFExpr) );
148151 nExtra += pFunc->pFExpr->x.pList->nExpr;
148154 if( !pFunc->bOBUnique && pParse->nErr==0 ){
148155 pKeyInfo->nKeyField++;
148158 pFunc->iOBTab, pOBList->nExpr+nExtra, 0,
148160 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)",
148161 pFunc->pFunc->zName));
148171 Vdbe *v = pParse->pVdbe;
148174 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
148176 assert( ExprUseXList(pF->pFExpr) );
148177 pList = pF->pFExpr->x.pList;
148178 if( pF->iOBTab>=0 ){
148180 ** all content was stored in emphermal table pF->iOBTab. Extract that
148189 nArg = pList->nExpr;
148192 if( pF->bOBPayload==0 ){
148195 assert( pF->pFExpr->pLeft!=0 );
148196 assert( ExprUseXList(pF->pFExpr->pLeft) );
148197 assert( pF->pFExpr->pLeft->x.pList!=0 );
148198 nKey = pF->pFExpr->pLeft->x.pList->nExpr;
148199 if( ALWAYS(!pF->bOBUnique) ) nKey++;
148201 iTop = sqlite3VdbeAddOp1(v, OP_Rewind, pF->iOBTab); VdbeCoverage(v);
148202 for(j=nArg-1; j>=0; j--){
148203 sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, nKey+j, regAgg+j);
148206 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
148208 sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v);
148213 pList ? pList->nExpr : 0);
148214 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
148222 ** If regAcc is non-zero and there are no min() or max() aggregates
148223 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
148240 Vdbe *v = pParse->pVdbe;
148247 assert( pAggInfo->iFirstReg>0 );
148248 if( pParse->nErr ) return;
148249 pAggInfo->directMode = 1;
148250 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
148257 assert( ExprUseXList(pF->pFExpr) );
148258 assert( !IsWindowFunc(pF->pFExpr) );
148259 pList = pF->pFExpr->x.pList;
148260 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
148261 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
148262 if( pAggInfo->nAccumulator
148263 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
148269 if( regHit==0 ) regHit = ++pParse->nMem;
148282 if( pF->iOBTab>=0 ){
148288 nArg = pList->nExpr;
148290 assert( pF->pFExpr->pLeft!=0 );
148291 assert( pF->pFExpr->pLeft->op==TK_ORDER );
148292 assert( ExprUseXList(pF->pFExpr->pLeft) );
148293 pOBList = pF->pFExpr->pLeft->x.pList;
148295 assert( pOBList->nExpr>0 );
148296 regAggSz = pOBList->nExpr;
148297 if( !pF->bOBUnique ){
148300 if( pF->bOBPayload ){
148307 jj = pOBList->nExpr;
148308 if( !pF->bOBUnique ){
148309 sqlite3VdbeAddOp2(v, OP_Sequence, pF->iOBTab, regAgg+jj);
148312 if( pF->bOBPayload ){
148317 nArg = pList->nExpr;
148325 if( pF->iDistinct>=0 && pList ){
148329 pF->iDistinct = codeDistinct(pParse, eDistinctType,
148330 pF->iDistinct, addrNext, pList, regDistinct);
148332 if( pF->iOBTab>=0 ){
148334 sqlite3VdbeAddOp3(v, OP_MakeRecord, regAgg, regAggSz-1,
148335 regAgg+regAggSz-1);
148336 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pF->iOBTab, regAgg+regAggSz-1,
148337 regAgg, regAggSz-1);
148341 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
148345 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
148346 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
148347 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
148350 pColl = pParse->db->pDfltColl;
148352 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
148357 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
148365 if( regHit==0 && pAggInfo->nAccumulator ){
148371 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
148372 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
148375 pAggInfo->directMode = 0;
148391 if( pParse->explain==2 ){
148394 pTab->zName,
148396 bCover ? pIdx->zName : ""
148411 ** sub-expression matches the criteria for being moved to the WHERE
148412 ** clause. If so, add it to the WHERE clause and replace the sub-expression
148416 if( pExpr->op!=TK_AND ){
148417 Select *pS = pWalker->u.pSelect;
148419 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
148425 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
148427 && pExpr->pAggInfo==0
148429 sqlite3 *db = pWalker->pParse->db;
148432 Expr *pWhere = pS->pWhere;
148434 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
148435 pS->pWhere = pNew;
148436 pWalker->eCode = 1;
148465 sqlite3WalkExpr(&sWalker, p->pHaving);
148475 ** Check to see if the pThis entry of pTabList is a self-join of another view.
148476 ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
148479 ** If pThis is a self-join, then return the SrcItem for the first other
148480 ** instance of that view found. If pThis is not a self-join then return 0.
148483 SrcList *pTabList, /* Search for self-joins in this FROM clause */
148485 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
148488 assert( pThis->pSelect!=0 );
148489 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
148492 pItem = &pTabList->a[iFirst++];
148493 if( pItem->pSelect==0 ) continue;
148494 if( pItem->fg.viaCoroutine ) continue;
148495 if( pItem->zName==0 ) continue;
148496 assert( pItem->pTab!=0 );
148497 assert( pThis->pTab!=0 );
148498 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
148499 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
148500 pS1 = pItem->pSelect;
148501 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
148506 if( pItem->pSelect->selFlags & SF_PushDown ){
148520 sqlite3DbFree(db, p->aCol);
148521 sqlite3DbFree(db, p->aFunc);
148549 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
148550 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
148551 if( p->pWhere ) return 0;
148552 if( p->pHaving ) return 0;
148553 if( p->pGroupBy ) return 0;
148554 if( p->pOrderBy ) return 0;
148555 pExpr = p->pEList->a[0].pExpr;
148556 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
148558 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
148560 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
148561 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
148563 pSub = p->pSrc->a[0].pSelect;
148565 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
148566 if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */
148568 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
148569 if( pSub->pWhere ) return 0; /* No WHERE clause */
148570 if( pSub->pLimit ) return 0; /* No LIMIT clause */
148571 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
148572 assert( pSub->pHaving==0 ); /* Due to the previous */
148573 pSub = pSub->pPrior; /* Repeat over compound */
148578 db = pParse->db;
148581 pSub = p->pSrc->a[0].pSelect;
148582 p->pSrc->a[0].pSelect = 0;
148583 sqlite3SrcListDelete(db, p->pSrc);
148584 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
148587 pPrior = pSub->pPrior;
148588 pSub->pPrior = 0;
148589 pSub->pNext = 0;
148590 pSub->selFlags |= SF_Aggregate;
148591 pSub->selFlags &= ~SF_Compound;
148592 pSub->nSelectRow = 0;
148593 sqlite3ExprListDelete(db, pSub->pEList);
148595 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
148605 p->pEList->a[0].pExpr = pExpr;
148606 p->selFlags &= ~SF_Aggregate;
148610 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
148618 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
148624 for(i=0; i<pSrc->nSrc; i++){
148625 SrcItem *p1 = &pSrc->a[i];
148627 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
148630 if( p1->pSelect
148631 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
148632 && sameSrcAlias(p0, p1->pSelect->pSrc)
148641 ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
148642 ** be implemented as a co-routine. The i-th entry is guaranteed to be
148645 ** The subquery is implemented as a co-routine if all of the following are
148652 ** (b) The subquery is the left-most term and a CROSS JOIN or similar
148655 ** (i) The subquery is the left-most subquery in the FROM clause
148666 ** (5) The subquery is not self-joined
148674 SrcItem *pItem = &pTabList->a[i];
148675 if( pItem->fg.isCte ){
148676 const CteUse *pCteUse = pItem->u2.pCteUse;
148677 if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */
148678 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */
148680 if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */
148681 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */
148682 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
148686 if( pTabList->nSrc==1 ) return 1; /* (1a) */
148687 if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */
148688 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
148691 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
148692 while( 1 /*exit-by-break*/ ){
148693 if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */
148695 i--;
148696 pItem--;
148697 if( pItem->pSelect!=0 ) return 0; /* (1c-i) */
148710 ** pParse->zErrMsg.
148738 db = pParse->db;
148739 assert( pParse==db->pParse );
148741 if( p==0 || pParse->nErr ){
148744 assert( db->mallocFailed==0 );
148747 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
148757 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
148758 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
148759 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
148760 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
148762 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
148763 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
148764 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
148766 if( p->pOrderBy ){
148770 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
148775 p->pOrderBy);
148776 testcase( pParse->earlyCleanup );
148777 p->pOrderBy = 0;
148779 p->selFlags &= ~SF_Distinct;
148780 p->selFlags |= SF_NoopOrderBy;
148783 if( pParse->nErr ){
148786 assert( db->mallocFailed==0 );
148787 assert( p->pEList!=0 );
148797 ** In this case, it is an error if the target object (pSrc->a[0]) name
148798 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
148804 if( p->selFlags & SF_UFSrcCheck ){
148805 SrcItem *p0 = &p->pSrc->a[0];
148806 if( sameSrcAlias(p0, p->pSrc) ){
148809 p0->zAlias ? p0->zAlias : p0->pTab->zName
148815 ** and leaving this flag set can cause errors if a compound sub-query
148816 ** in p->pSrc is flattened into this query and this function called
148818 p->selFlags &= ~SF_UFSrcCheck;
148821 if( pDest->eDest==SRT_Output ){
148827 assert( pParse->nErr );
148831 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
148837 pTabList = p->pSrc;
148838 isAgg = (p->selFlags & SF_Aggregate)!=0;
148840 sSort.pOrderBy = p->pOrderBy;
148846 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
148847 SrcItem *pItem = &pTabList->a[i];
148848 Select *pSub = pItem->pSelect;
148849 Table *pTab = pItem->pTab;
148858 ** LEFT JOIN -> JOIN
148859 ** RIGHT JOIN -> JOIN
148860 ** FULL JOIN -> RIGHT JOIN
148862 ** If terms of the i-th table are used in the WHERE clause in such a
148863 ** way that the i-th table cannot be the NULL row of a join, then
148867 if( (pItem->fg.jointype & (JT_LEFT|JT_LTORJ))!=0
148868 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor,
148869 pItem->fg.jointype & JT_LTORJ)
148872 if( pItem->fg.jointype & JT_LEFT ){
148873 if( pItem->fg.jointype & JT_RIGHT ){
148875 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
148876 pItem->fg.jointype &= ~JT_LEFT;
148879 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148880 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
148881 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
148884 if( pItem->fg.jointype & JT_LTORJ ){
148885 for(j=i+1; j<pTabList->nSrc; j++){
148886 SrcItem *pI2 = &pTabList->a[j];
148887 if( pI2->fg.jointype & JT_RIGHT ){
148888 if( pI2->fg.jointype & JT_LEFT ){
148890 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
148891 pI2->fg.jointype &= ~JT_RIGHT;
148894 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148895 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
148896 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
148900 for(j=pTabList->nSrc-1; j>=0; j--){
148901 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148902 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148912 if( pTab->nCol!=pSub->pEList->nExpr ){
148914 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
148922 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){
148930 ** will be implemented as a co-routine and there is no advantage to
148933 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
148934 assert( pSub->pGroupBy==0 );
148936 /* If a FROM-clause subquery has an ORDER BY clause that is not
148945 ** (2) The subquery was added to help with window-function
148949 ** the built-in count(), min(), or max().
148958 if( pSub->pOrderBy!=0
148959 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
148960 && pSub->pLimit==0 /* Condition (1) */
148961 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
148962 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
148966 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
148969 pSub->pOrderBy);
148970 pSub->pOrderBy = 0;
148976 ** it will be implemented as a co-routine, then do not flatten. This
148991 if( pSub->pOrderBy!=0
148993 && (p->selFlags & SF_ComplexResult)!=0
148994 && (pTabList->nSrc==1
148995 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
149001 if( pParse->nErr ) goto select_end;
149003 i = -1;
149005 pTabList = p->pSrc;
149006 if( db->mallocFailed ) goto select_end;
149008 sSort.pOrderBy = p->pOrderBy;
149017 if( p->pPrior ){
149020 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
149025 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
149030 /* Do the WHERE-clause constant propagation optimization if this is
149031 ** a join. No need to speed time on this operation for non-join queries
149035 if( p->pWhere!=0
149036 && p->pWhere->op==TK_AND
149053 if( db->mallocFailed ) goto select_end;
149054 pTabList = p->pSrc;
149059 ** (2) Generate code for all sub-queries
149061 for(i=0; i<pTabList->nSrc; i++){
149062 SrcItem *pItem = &pTabList->a[i];
149075 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
149076 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
149083 ** assume the column name is non-NULL and segfault. The use of an empty
149086 if( pItem->colUsed==0 && pItem->zName!=0 ){
149087 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
149091 /* Generate code for all sub-queries in the FROM clause
149093 pSub = pItem->pSelect;
149097 assert( pItem->addrFillSub==0 );
149102 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
149106 pParse->nHeight += sqlite3SelectExprHeight(p);
149108 /* Make copies of constant WHERE-clause terms in the outer query down
149112 && (pItem->fg.isCte==0
149113 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
149114 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
149119 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
149123 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
149125 TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
149138 pSub->selId));
149144 zSavedAuthContext = pParse->zAuthContext;
149145 pParse->zAuthContext = pItem->zName;
149149 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
149150 /* Implement a co-routine that will return a single row of the result
149155 pItem->regReturn = ++pParse->nMem;
149156 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
149158 pItem->addrFillSub = addrTop;
149159 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
149160 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
149162 pItem->pTab->nRowLogEst = pSub->nSelectRow;
149163 pItem->fg.viaCoroutine = 1;
149164 pItem->regResult = dest.iSdst;
149165 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
149166 sqlite3VdbeJumpHere(v, addrTop-1);
149168 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
149171 ** the make the pItem->iCursor be a copy of the ephemeral table that
149173 CteUse *pCteUse = pItem->u2.pCteUse;
149174 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
149175 if( pItem->iCursor!=pCteUse->iCur ){
149176 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
149179 pSub->nSelectRow = pCteUse->nRowEst;
149183 if( pPrior->addrFillSub ){
149184 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
149186 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
149187 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
149198 pItem->regReturn = ++pParse->nMem;
149200 pItem->addrFillSub = topAddr+1;
149201 pItem->fg.isMaterialized = 1;
149202 if( pItem->fg.isCorrelated==0 ){
149211 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
149215 pItem->pTab->nRowLogEst = pSub->nSelectRow;
149217 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
149219 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
149222 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
149223 CteUse *pCteUse = pItem->u2.pCteUse;
149224 pCteUse->addrM9e = pItem->addrFillSub;
149225 pCteUse->regRtn = pItem->regReturn;
149226 pCteUse->iCur = pItem->iCursor;
149227 pCteUse->nRowEst = pSub->nSelectRow;
149230 if( db->mallocFailed ) goto select_end;
149231 pParse->nHeight -= sqlite3SelectExprHeight(p);
149232 pParse->zAuthContext = zSavedAuthContext;
149238 pEList = p->pEList;
149239 pWhere = p->pWhere;
149240 pGroupBy = p->pGroupBy;
149241 pHaving = p->pHaving;
149242 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
149246 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
149252 ** if the select-list is the same as the ORDER BY list, then this query
149261 ** The second form is preferred as a single index (or temp-table) may be
149263 ** written the query must use a temp-table for at least one of the ORDER
149264 ** BY and DISTINCT, and an index or separate temp-table for the other.
149266 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
149267 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
149269 && p->pWin==0
149272 p->selFlags &= ~SF_Distinct;
149273 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
149274 p->selFlags |= SF_Aggregate;
149275 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
149291 ** being unused if the data can be extracted in pre-sorted order.
149300 pParse, sSort.pOrderBy, 0, pEList->nExpr);
149301 sSort.iECursor = pParse->nTab++;
149304 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
149308 sSort.addrSortIndex = -1;
149313 if( pDest->eDest==SRT_EphemTab ){
149314 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
149315 if( p->selFlags & SF_NestedFrom ){
149316 /* Delete or NULL-out result columns that will never be used */
149318 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
149319 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
149320 sqlite3DbFree(db, pEList->a[ii].zEName);
149321 pEList->nExpr--;
149323 for(ii=0; ii<pEList->nExpr; ii++){
149324 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
149332 if( (p->selFlags & SF_FixedLimit)==0 ){
149333 p->nSelectRow = 320; /* 4 billion rows */
149335 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
149336 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
149343 if( p->selFlags & SF_Distinct ){
149344 sDistinct.tabTnct = pParse->nTab++;
149347 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
149358 | (p->selFlags & SF_FixedLimit);
149360 Window *pWin = p->pWin; /* Main window object (or NULL) */
149371 p->pEList, p, wctrlFlags, p->nSelectRow);
149373 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
149374 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
149382 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
149396 assert( p->pEList==pEList );
149402 int regGosub = ++pParse->nMem;
149408 VdbeNoopComment((v, "inner-loop subroutine"));
149410 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
149413 VdbeComment((v, "end inner-loop subroutine"));
149419 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
149451 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
149452 pItem->u.x.iAlias = 0;
149454 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
149455 pItem->u.x.iAlias = 0;
149458 if( p->nSelectRow>66 ) p->nSelectRow = 66;
149463 ** in the correct order. It also may not - the GROUP BY might use a
149468 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
149471 ** ASC or DESC order - only that each group is returned contiguously.
149475 for(ii=0; ii<pGroupBy->nExpr; ii++){
149477 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
149478 pGroupBy->a[ii].fg.sortFlags = sortFlags;
149480 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
149486 p->nSelectRow = 0;
149500 testcase( pParse->earlyCleanup );
149502 if( db->mallocFailed ){
149505 pAggInfo->selId = p->selId;
149507 pAggInfo->pSelect = p;
149514 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
149515 pAggInfo->pGroupBy = pGroupBy;
149520 assert( pWhere==p->pWhere );
149521 assert( pHaving==p->pHaving );
149522 assert( pGroupBy==p->pGroupBy );
149524 pWhere = p->pWhere;
149528 pAggInfo->nAccumulator = pAggInfo->nColumn;
149529 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
149530 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
149535 if( db->mallocFailed ) goto select_end;
149554 int addr1; /* A-vs-B comparison jump */
149566 if( pAggInfo->nFunc==1
149567 && pAggInfo->aFunc[0].iDistinct>=0
149568 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
149569 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
149570 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
149572 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
149584 pAggInfo->sortingIdx = pParse->nTab++;
149586 0, pAggInfo->nColumn);
149588 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
149593 iUseFlag = ++pParse->nMem;
149594 iAbortFlag = ++pParse->nMem;
149595 regOutputRow = ++pParse->nMem;
149597 regReset = ++pParse->nMem;
149599 iAMem = pParse->nMem + 1;
149600 pParse->nMem += pGroupBy->nExpr;
149601 iBMem = pParse->nMem + 1;
149602 pParse->nMem += pGroupBy->nExpr;
149605 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
149622 if( pParse->pIdxEpr ){
149628 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
149648 ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s",
149649 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
149654 nGroupBy = pGroupBy->nExpr;
149657 for(i=0; i<pAggInfo->nColumn; i++){
149658 if( pAggInfo->aCol[i].iSorterColumn>=j ){
149666 pAggInfo->directMode = 1;
149667 for(i=0; i<pAggInfo->nColumn; i++){
149668 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
149669 if( pCol->iSorterColumn>=j ){
149670 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
149674 pAggInfo->directMode = 0;
149678 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
149679 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
149684 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
149688 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
149690 pAggInfo->useSortingIdx = 1;
149691 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
149692 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
149695 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
149700 if( pParse->pIdxEpr ){
149716 ** This is an optimization - the correct answer should result regardless.
149733 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
149736 for(j=0; j<pGroupBy->nExpr; j++){
149740 pAggInfo->directMode = 1;
149741 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
149744 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
149758 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
149777 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
149797 ** is less than or equal to zero, the subroutine is a no-op. If
149814 selectInnerLoop(pParse, p, -1, &sSort,
149820 /* Generate a subroutine that will reset the group-by accumulator
149829 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
149830 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
149849 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
149850 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
149854 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
149857 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
149861 ** (2011-04-15) Do not do a full scan of an unordered index.
149863 ** (2013-10-03) Do not count the entries in a partial index.
149869 if( !p->pSrc->a[0].fg.notIndexed ){
149870 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149871 if( pIdx->bUnordered==0
149872 && pIdx->szIdxRow<pTab->szTabRow
149873 && pIdx->pPartIdxWhere==0
149874 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
149881 iRoot = pBest->tnum;
149885 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
149888 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
149909 if( pAggInfo->nAccumulator ){
149910 for(i=0; i<pAggInfo->nFunc; i++){
149911 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
149914 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
149918 if( i==pAggInfo->nFunc ){
149919 regAcc = ++pParse->nMem;
149922 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
149923 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
149924 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
149933 assert( p->pGroupBy==0 );
149942 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
149954 struct AggInfo_func *pF = pAggInfo->aFunc;
149956 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
149971 selectInnerLoop(pParse, p, -1, 0, 0,
149986 assert( p->pEList==pEList );
149987 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
149996 rc = (pParse->nErr>0);
150002 assert( db->mallocFailed==0 || db->mallocFailed==1 );
150003 assert( db->mallocFailed==0 || pParse->nErr!=0 );
150006 if( pAggInfo && !db->mallocFailed ){
150007 for(i=0; i<pAggInfo->nColumn; i++){
150008 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
150010 assert( pExpr->pAggInfo==pAggInfo );
150011 assert( pExpr->iAgg==i );
150013 for(i=0; i<pAggInfo->nFunc; i++){
150014 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
150016 assert( pExpr->pAggInfo==pAggInfo );
150017 assert( pExpr->iAgg==i );
150077 int need; /* Slots needed in p->azResult[] */
150081 /* Make sure there is enough space in p->azResult to hold everything
150084 if( p->nRow==0 && argv!=0 ){
150089 if( p->nData + need > p->nAlloc ){
150091 p->nAlloc = p->nAlloc*2 + need;
150092 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
150094 p->azResult = azNew;
150100 if( p->nRow==0 ){
150101 p->nColumn = nCol;
150105 p->azResult[p->nData++] = z;
150107 }else if( (int)p->nColumn!=nCol ){
150108 sqlite3_free(p->zErrMsg);
150109 p->zErrMsg = sqlite3_mprintf(
150112 p->rc = SQLITE_ERROR;
150128 p->azResult[p->nData++] = z;
150130 p->nRow++;
150135 p->rc = SQLITE_NOMEM_BKPT;
150175 db->errCode = SQLITE_NOMEM;
150191 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
150204 db->errCode = SQLITE_NOMEM;
150223 azResult--;
150256 pTriggerStep = pTriggerStep->pNext;
150258 sqlite3ExprDelete(db, pTmp->pWhere);
150259 sqlite3ExprListDelete(db, pTmp->pExprList);
150260 sqlite3SelectDelete(db, pTmp->pSelect);
150261 sqlite3IdListDelete(db, pTmp->pIdList);
150262 sqlite3UpsertDelete(db, pTmp->pUpsert);
150263 sqlite3SrcListDelete(db, pTmp->pFrom);
150264 sqlite3DbFree(db, pTmp->zSpan);
150275 ** are already attached to pTab->pTrigger. But there might be additional
150277 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
150282 ** pTab as well as the triggers lised in pTab->pTrigger.
150289 assert( pParse->disableTriggers==0 );
150290 pTmpSchema = pParse->db->aDb[1].pSchema;
150291 p = sqliteHashFirst(&pTmpSchema->trigHash);
150292 pList = pTab->pTrigger;
150295 if( pTrig->pTabSchema==pTab->pSchema
150296 && pTrig->table
150297 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
150298 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
150300 pTrig->pNext = pList;
150302 }else if( pTrig->op==TK_RETURNING ){
150304 assert( pParse->db->pVtabCtx==0 );
150306 assert( pParse->bReturning );
150307 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
150308 pTrig->table = pTab->zName;
150309 pTrig->pTabSchema = pTab->pSchema;
150310 pTrig->pNext = pList;
150318 printf("Triggers for %s:", pTab->zName);
150319 for(pX=pList; pX; pX=pX->pNext){
150320 printf(" %s", pX->zName);
150333 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
150352 sqlite3 *db = pParse->db; /* The database connection */
150357 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
150363 if( pName2->n>0 ){
150376 if( !pTableName || db->mallocFailed ){
150380 /* A long-standing parser bug is that this syntax was allowed:
150388 if( db->init.busy && iDb!=1 ){
150389 sqlite3DbFree(db, pTableName->a[0].zDatabase);
150390 pTableName->a[0].zDatabase = 0;
150399 if( db->init.busy==0 && pName2->n==0 && pTab
150400 && pTab->pSchema==db->aDb[1].pSchema ){
150405 if( db->mallocFailed ) goto trigger_cleanup;
150406 assert( pTableName->nSrc==1 );
150420 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
150429 assert( db->mallocFailed );
150432 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
150437 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
150441 assert( !db->init.busy );
150443 VVA_ONLY( pParse->ifNotExists = 1; )
150450 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
150460 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
150465 " trigger on table: %S", pTableName->a);
150471 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
150473 const char *zDb = db->aDb[iTabDb].zDbSName;
150474 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
150476 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
150497 pTrigger->zName = zName;
150499 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
150500 pTrigger->pSchema = db->aDb[iDb].pSchema;
150501 pTrigger->pTabSchema = pTab->pSchema;
150502 pTrigger->op = (u8)op;
150503 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
150505 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
150506 pTrigger->pWhen = pWhen;
150509 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
150511 pTrigger->pColumns = pColumns;
150513 assert( pParse->pNewTrigger==0 );
150514 pParse->pNewTrigger = pTrigger;
150521 if( !pParse->pNewTrigger ){
150524 assert( pParse->pNewTrigger==pTrigger );
150529 if( db->init.iDb==1 ){
150532 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
150536 ** "orphaned trigger" - a trigger whose associated table is missing.
150538 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
150540 db->init.orphanTrigger = 1;
150554 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
150556 sqlite3 *db = pParse->db; /* The database */
150561 pParse->pNewTrigger = 0;
150562 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
150563 zName = pTrig->zName;
150564 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
150565 pTrig->step_list = pStepList;
150567 pStepList->pTrig = pTrig;
150568 pStepList = pStepList->pNext;
150570 sqlite3TokenInit(&nameToken, pTrig->zName);
150572 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
150573 || sqlite3FixExpr(&sFix, pTrig->pWhen)
150580 assert( !db->init.busy );
150581 pParse->pNewTrigger = pTrig;
150589 if( !db->init.busy ){
150594 ** are read-only, and the trigger makes a change to a shadow table,
150595 ** then raise an error - do not allow the trigger to be created. */
150598 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
150599 if( pStep->zTarget!=0
150600 && sqlite3ShadowTableName(db, pStep->zTarget)
150604 pTrig->zName, pStep->zTarget);
150614 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
150619 db->aDb[iDb].zDbSName, zName,
150620 pTrig->table, z);
150627 if( db->init.busy ){
150629 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
150635 }else if( pLink->pSchema==pLink->pTabSchema ){
150637 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
150639 pLink->pNext = pTab->pTrigger;
150640 pTab->pTrigger = pLink;
150646 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
150679 pTriggerStep->op = TK_SELECT;
150680 pTriggerStep->pSelect = pSelect;
150681 pTriggerStep->orconf = OE_Default;
150682 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
150690 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
150699 sqlite3 *db = pParse->db;
150702 if( pParse->nErr ) return 0;
150703 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
150706 memcpy(z, pName->z, pName->n);
150708 pTriggerStep->zTarget = z;
150709 pTriggerStep->op = op;
150710 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
150712 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
150735 sqlite3 *db = pParse->db;
150738 assert(pSelect != 0 || db->mallocFailed);
150743 pTriggerStep->pSelect = pSelect;
150746 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
150748 pTriggerStep->pIdList = pColumn;
150749 pTriggerStep->pUpsert = pUpsert;
150750 pTriggerStep->orconf = orconf;
150752 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
150773 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
150780 sqlite3 *db = pParse->db;
150786 pTriggerStep->pExprList = pEList;
150787 pTriggerStep->pWhere = pWhere;
150788 pTriggerStep->pFrom = pFrom;
150793 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
150794 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
150795 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
150797 pTriggerStep->orconf = orconf;
150817 sqlite3 *db = pParse->db;
150823 pTriggerStep->pWhere = pWhere;
150826 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
150828 pTriggerStep->orconf = OE_Default;
150838 if( pTrigger==0 || pTrigger->bReturning ) return;
150839 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
150840 sqlite3DbFree(db, pTrigger->zName);
150841 sqlite3DbFree(db, pTrigger->table);
150842 sqlite3ExprDelete(db, pTrigger->pWhen);
150843 sqlite3IdListDelete(db, pTrigger->pColumns);
150860 sqlite3 *db = pParse->db;
150862 if( db->mallocFailed ) goto drop_trigger_cleanup;
150867 assert( pName->nSrc==1 );
150868 zDb = pName->a[0].zDatabase;
150869 zName = pName->a[0].zName;
150871 for(i=OMIT_TEMPDB; i<db->nDb; i++){
150875 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
150880 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
150884 pParse->checkSchema = 1;
150898 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
150908 sqlite3 *db = pParse->db;
150911 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
150912 assert( iDb>=0 && iDb<db->nDb );
150914 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
150918 const char *zDb = db->aDb[iDb].zDbSName;
150921 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
150933 db->aDb[iDb].zDbSName, pTrigger->zName
150936 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
150948 pHash = &(db->aDb[iDb].pSchema->trigHash);
150951 if( pTrigger->pSchema==pTrigger->pTabSchema ){
150955 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
150957 *pp = (*pp)->pNext;
150964 db->mDbFlags |= DBFLAG_SchemaChange;
150980 for(e=0; e<pEList->nExpr; e++){
150981 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
150990 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
150991 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
151014 || (pList->bReturning && pList->pNext==0) );
151017 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
151018 && pTab->pTrigger!=0
151023 if( pList==pTab->pTrigger ){
151027 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
151028 p->pNext = 0;
151032 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
151033 mask |= p->tr_tm;
151034 }else if( p->op==TK_RETURNING ){
151038 p->op = op;
151045 p->tr_tm = TRIGGER_BEFORE;
151047 p->tr_tm = TRIGGER_AFTER;
151049 mask |= p->tr_tm;
151050 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
151053 mask |= p->tr_tm;
151055 p = p->pNext;
151072 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
151073 || pParse->disableTriggers
151082 ** Convert the pStep->zTarget string into a SrcList and return a pointer
151095 sqlite3 *db = pParse->db;
151097 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
151099 assert( pSrc==0 || pSrc->nSrc==1 );
151102 Schema *pSchema = pStep->pTrig->pSchema;
151103 pSrc->a[0].zName = zName;
151104 if( pSchema!=db->aDb[1].pSchema ){
151105 pSrc->a[0].pSchema = pSchema;
151107 if( pStep->pFrom ){
151108 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
151109 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
151135 if( pTerm->op==TK_ASTERISK ) return 1;
151136 if( pTerm->op!=TK_DOT ) return 0;
151137 assert( pTerm->pRight!=0 );
151138 assert( pTerm->pLeft!=0 );
151139 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
151156 sqlite3 *db = pParse->db;
151159 for(i=0; i<pList->nExpr; i++){
151160 Expr *pOldExpr = pList->a[i].pExpr;
151164 for(jj=0; jj<pTab->nCol; jj++){
151166 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
151167 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
151169 if( !db->mallocFailed ){
151170 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
151171 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
151172 pItem->fg.eEName = ENAME_NAME;
151178 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
151179 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
151180 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
151181 pItem->fg.eEName = pList->a[i].fg.eEName;
151191 ** is generated in-line.
151199 Vdbe *v = pParse->pVdbe;
151200 sqlite3 *db = pParse->db;
151207 if( !pParse->bReturning ){
151212 assert( db->pParse==pParse );
151213 pReturning = pParse->u1.pReturning;
151214 if( pTrigger != &(pReturning->retTrig) ){
151220 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
151224 sFrom.a[0].iCursor = -1;
151226 if( pParse->nErr==0 ){
151227 assert( db->mallocFailed==0 );
151231 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
151232 if( pParse->nErr==0 ){
151235 if( pReturning->nRetCol==0 ){
151236 pReturning->nRetCol = pNew->nExpr;
151237 pReturning->iRetCur = pParse->nTab++;
151242 pParse->eTriggerOp = pTrigger->op;
151243 pParse->pTriggerTab = pTab;
151245 && ALWAYS(!db->mallocFailed)
151248 int nCol = pNew->nExpr;
151249 int reg = pParse->nMem+1;
151250 pParse->nMem += nCol+2;
151251 pReturning->iRetReg = reg;
151253 Expr *pCol = pNew->a[i].pExpr;
151254 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
151261 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
151262 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
151266 pParse->eTriggerOp = 0;
151267 pParse->pTriggerTab = 0;
151282 Vdbe *v = pParse->pVdbe;
151283 sqlite3 *db = pParse->db;
151285 assert( pParse->pTriggerTab && pParse->pToplevel );
151288 for(pStep=pStepList; pStep; pStep=pStep->pNext){
151299 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
151300 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
151302 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
151303 assert( pParse->okConstFactor==0 );
151306 if( pStep->zSpan ){
151308 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
151313 switch( pStep->op ){
151317 sqlite3ExprListDup(db, pStep->pExprList, 0),
151318 sqlite3ExprDup(db, pStep->pWhere, 0),
151319 pParse->eOrconf, 0, 0, 0
151327 sqlite3SelectDup(db, pStep->pSelect, 0),
151328 sqlite3IdListDup(db, pStep->pIdList),
151329 pParse->eOrconf,
151330 sqlite3UpsertDup(db, pStep->pUpsert)
151338 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
151343 default: assert( pStep->op==TK_SELECT ); {
151345 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
151376 ** Parse context structure pFrom has just been used to create a sub-vdbe
151381 assert( pFrom->zErrMsg==0 || pFrom->nErr );
151382 assert( pTo->zErrMsg==0 || pTo->nErr );
151383 if( pTo->nErr==0 ){
151384 pTo->zErrMsg = pFrom->zErrMsg;
151385 pTo->nErr = pFrom->nErr;
151386 pTo->rc = pFrom->rc;
151388 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
151393 ** Create and populate a new TriggerPrg object with a sub-program
151403 sqlite3 *db = pParse->db; /* Database handle */
151407 NameContext sNC; /* Name context for sub-vdbe */
151408 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
151410 Parse sSubParse; /* Parse context for sub-vdbe */
151412 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
151413 assert( pTop->pVdbe );
151417 ** list of the top-level Parse object sooner rather than later. */
151420 pPrg->pNext = pTop->pTriggerPrg;
151421 pTop->pTriggerPrg = pPrg;
151422 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
151424 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
151425 pPrg->pTrigger = pTrigger;
151426 pPrg->orconf = orconf;
151427 pPrg->aColmask[0] = 0xffffffff;
151428 pPrg->aColmask[1] = 0xffffffff;
151431 ** trigger sub-program. */
151437 sSubParse.zAuthContext = pTrigger->zName;
151438 sSubParse.eTriggerOp = pTrigger->op;
151439 sSubParse.nQueryLoop = pParse->nQueryLoop;
151440 sSubParse.prepFlags = pParse->prepFlags;
151445 pTrigger->zName, onErrorText(orconf),
151446 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
151447 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
151448 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
151449 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
151450 pTab->zName
151453 if( pTrigger->zName ){
151454 sqlite3VdbeChangeP4(v, -1,
151455 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
151461 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
151463 if( pTrigger->pWhen ){
151464 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
151465 if( db->mallocFailed==0
151474 /* Code the trigger program into the sub-vdbe. */
151475 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
151477 /* Insert an OP_Halt at the end of the sub-program. */
151482 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
151485 if( pParse->nErr==0 ){
151486 assert( db->mallocFailed==0 );
151487 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
151489 pProgram->nMem = sSubParse.nMem;
151490 pProgram->nCsr = sSubParse.nTab;
151491 pProgram->token = (void *)pTrigger;
151492 pPrg->aColmask[0] = sSubParse.oldmask;
151493 pPrg->aColmask[1] = sSubParse.newmask;
151505 ** Return a pointer to a TriggerPrg object containing the sub-program for
151519 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
151525 for(pPrg=pRoot->pTriggerPrg;
151526 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
151527 pPrg=pPrg->pNext
151533 pParse->db->errByteOffset = -1;
151556 assert( pPrg || pParse->nErr );
151559 ** is a pointer to the sub-vdbe containing the trigger program. */
151561 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
151563 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
151564 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
151566 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
151568 /* Set the P5 operand of the OP_Program instruction to non-zero if
151570 ** invocation is disallowed if (a) the sub-program is really a trigger,
151585 ** operation on pTab, this function is a no-op.
151590 ** (a copy of pTab->nCol), then registers are populated as follows:
151593 ** ------------------------------------------------------
151595 ** reg+1 OLD.* value of left-most column of pTab
151597 ** reg+N OLD.* value of right-most column of pTab
151599 ** reg+N+2 NEW.* value of left-most column of pTab
151601 ** reg+N+N+1 NEW.* value of right-most column of pTab
151634 for(p=pTrigger; p; p=p->pNext){
151639 assert( p->pSchema!=0 );
151640 assert( p->pTabSchema!=0 );
151641 assert( p->pSchema==p->pTabSchema
151642 || p->pSchema==pParse->db->aDb[1].pSchema );
151649 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
151650 && p->tr_tm==tr_tm
151651 && checkColumnOverlap(p->pColumns, pChanges)
151653 if( !p->bReturning ){
151663 ** Triggers may access values stored in the old.* or new.* pseudo-table.
151664 ** This function returns a 32-bit bitmask indicating which columns of the
151669 ** Bit 0 of the returned mask is set if the left-most column of the
151704 for(p=pTrigger; p; p=p->pNext){
151705 if( p->op==op
151706 && (tr_tm&p->tr_tm)
151707 && checkColumnOverlap(p->pColumns,pChanges)
151709 if( p->bReturning ){
151715 mask |= pPrg->aColmask[isNew];
151760 ** i-th column of table pTab. This routine sets the P4 parameter of the
151766 ** command. If the latter, then the row-records in the table btree on disk
151769 ** If the former, then all row-records are guaranteed to include a value
151783 ** If column as REAL affinity and the table is an ordinary b-tree table
151791 assert( pTab->nCol>i );
151792 pCol = &pTab->aCol[i];
151793 if( pCol->iDflt ){
151797 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
151798 assert( i<pTab->nCol );
151801 pCol->affinity, &pValue);
151807 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
151816 ** and false if not. This is an optimization. False-positives are a
151817 ** performance degradation, but false-negatives can result in a corrupt
151820 ** aXRef[j] will be non-negative if column j of the original table is
151830 i16 iIdxCol = pIdx->aiColumn[iCol];
151836 assert( pIdx->aColExpr!=0 );
151837 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
151838 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
151846 ** to be unchanged. This is an optimization. False-positives are a
151847 ** performance degradation, but false-negatives can result in a corrupt
151850 ** aXRef[j] will be non-negative if column j of the original table is
151859 if( pIdx->pPartIdxWhere==0 ) return 0;
151860 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
151868 ** table in the source-list (pSrc->a[0]).
151872 if( pRet ) pRet->iColumn = iCol+1;
151880 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
151888 ** SELECT <other-columns>, pChanges FROM pTabList
151890 ** GROUP BY <other-columns>
151896 ** the <other-columns> in the query above are is determined by the type
151897 ** of table pTabList->a[0].pTab.
151900 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
151904 ** If the table is actually a view, then <other-columns> are all columns of
151908 ** If the table is a virtual or ordinary intkey table, then <other-columns>
151911 ** rowid value in <other-columns> is used as the integer key, and the
151931 sqlite3 *db = pParse->db;
151932 Table *pTab = pTabList->a[0].pTab;
151952 assert( pTabList->nSrc>1 );
151954 assert( pSrc->a[0].fg.notCte );
151955 pSrc->a[0].iCursor = -1;
151956 pSrc->a[0].pTab->nTabRef--;
151957 pSrc->a[0].pTab = 0;
151960 for(i=0; i<pPk->nKeyCol; i++){
151961 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
151971 for(i=0; i<pTab->nCol; i++){
151984 assert( pChanges!=0 || pParse->db->mallocFailed );
151986 for(i=0; i<pChanges->nExpr; i++){
151988 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
151996 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
151998 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
152036 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
152037 ** an expression for the i-th column of the table.
152038 ** aXRef[i]==-1 if the i-th column is not changed. */
152044 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
152046 NameContext sNC; /* The name-context to resolve expressions in */
152068 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
152080 db = pParse->db;
152081 assert( db->pParse==pParse );
152082 if( pParse->nErr ){
152085 assert( db->mallocFailed==0 );
152091 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
152113 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
152119 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
152122 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
152147 iBaseCur = iDataCur = pParse->nTab++;
152150 testcase( pPk!=0 && pPk!=pTab->pIndex );
152151 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
152153 iDataCur = pParse->nTab;
152155 pParse->nTab++;
152159 iDataCur = pUpsert->iDataCur;
152160 iIdxCur = pUpsert->iIdxCur;
152161 pParse->nTab = iBaseCur;
152163 pTabList->a[0].iCursor = iDataCur;
152168 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
152170 aRegIdx = aXRef+pTab->nCol;
152174 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
152176 /* Initialize the name-context */
152194 for(i=0; i<pChanges->nExpr; i++){
152195 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
152198 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
152201 for(j=0; j<pTab->nCol; j++){
152202 if( pTab->aCol[j].hName==hCol
152203 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
152205 if( j==pTab->iPKey ){
152207 pRowidExpr = pChanges->a[i].pExpr;
152209 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
152213 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
152214 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
152215 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
152218 pTab->aCol[j].zCnName);
152226 if( j>=pTab->nCol ){
152227 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
152228 j = -1;
152230 pRowidExpr = pChanges->a[i].pExpr;
152233 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
152234 pParse->checkSchema = 1;
152241 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
152242 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
152243 db->aDb[iDb].zDbSName);
152247 aXRef[j] = -1;
152261 ** is non-negative, so the value of aXRef[] for generated columns can be
152262 ** set to any non-negative number. We use 99999 so that the value is
152265 if( pTab->tabFlags & TF_HasGenerated ){
152267 testcase( pTab->tabFlags & TF_HasVirtual );
152268 testcase( pTab->tabFlags & TF_HasStored );
152271 for(i=0; i<pTab->nCol; i++){
152273 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
152275 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
152291 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
152300 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
152305 reg = ++pParse->nMem;
152306 pParse->nMem += pIdx->nColumn;
152309 for(i=0; i<pIdx->nKeyCol; i++){
152311 reg = ++pParse->nMem;
152312 pParse->nMem += pIdx->nColumn;
152313 if( onError==OE_Default && pIdx->onError==OE_Replace ){
152323 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
152330 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
152339 ** two-pass update algorithm. */
152340 assert( aRegIdx[nAllIdx]==pParse->nMem );
152342 regOldRowid = regNewRowid = ++pParse->nMem;
152344 regOld = pParse->nMem + 1;
152345 pParse->nMem += pTab->nCol;
152348 regNewRowid = ++pParse->nMem;
152350 regNew = pParse->nMem + 1;
152351 pParse->nMem += pTab->nCol;
152356 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
152393 if( (db->flags&SQLITE_CountRows)!=0
152394 && !pParse->pTriggerTab
152395 && !pParse->nested
152396 && !pParse->bReturning
152399 regRowCount = ++pParse->nMem;
152405 iEph = pParse->nTab++;
152409 nPk = pPk ? pPk->nKeyCol : 0;
152410 iPk = pParse->nMem+1;
152411 pParse->nMem += nPk;
152412 pParse->nMem += nChangeFrom;
152413 regKey = ++pParse->nMem;
152415 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
152416 iEph = pParse->nTab++;
152417 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
152422 pKeyInfo->nAllField = nEphCol;
152456 ** Do not consider a single-pass strategy for a multi-row update if
152466 if( !pParse->nested
152478 /* A one-pass strategy that might update more than one row may not
152494 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
152504 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
152508 aRegIdx[nAllIdx] = ++pParse->nMem;
152520 assert( pPk->aiColumn[i]>=0 );
152522 pPk->aiColumn[i], iPk+i);
152548 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
152549 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
152552 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
152630 /* Compute the old pre-UPDATE content of the row being changed, if that
152637 for(i=0; i<pTab->nCol; i++){
152638 u32 colFlags = pTab->aCol[i].colFlags;
152671 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
152672 if( i==pTab->iPKey ){
152674 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
152675 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
152680 int nOff = (isView ? pTab->nCol : nPk);
152684 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
152702 if( pTab->tabFlags & TF_HasGenerated ){
152703 testcase( pTab->tabFlags & TF_HasVirtual );
152704 testcase( pTab->tabFlags & TF_HasStored );
152718 /* The row-trigger may have deleted the row being updated. In this
152720 ** required. This behavior - what happens when the row being updated
152721 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
152732 /* After-BEFORE-trigger-reload-loop:
152738 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
152741 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
152742 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
152743 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
152744 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
152749 if( pTab->tabFlags & TF_HasGenerated ){
152750 testcase( pTab->tabFlags & TF_HasVirtual );
152751 testcase( pTab->tabFlags & TF_HasStored );
152783 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
152796 ** to invoke the pre-update hook.
152799 ** pre-update hook. If the caller invokes preupdate_new(), the returned
152813 if( !pParse->nested ){
152856 /* Nothing to do at end-of-loop for a single-pass */
152870 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
152908 ** There are two possible strategies - the default and the special
152936 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
152939 sqlite3 *db = pParse->db; /* Database connection */
152942 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
152946 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
152955 ephemTab = pParse->nTab++;
152957 regArg = pParse->nMem + 1;
152958 pParse->nMem += nArg;
152959 if( pSrc->nSrc>1 ){
152973 assert( pPk->nKeyCol==1 );
152974 iPk = pPk->aiColumn[0];
152976 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
152983 for(i=0; i<pTab->nCol; i++){
152986 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
152990 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG;
152999 regRec = ++pParse->nMem;
153000 regRowid = ++pParse->nMem;
153009 for(i=0; i<pTab->nCol; i++){
153010 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
153012 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
153030 assert( pPk->nKeyCol==1 );
153031 iPk = pPk->aiColumn[0];
153042 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
153053 ** accept no-change records with serial_type 10 */
153064 if( pSrc->nSrc==1 ){
153097 ** 2018-04-12
153118 Upsert *pNext = p->pNextUpsert;
153119 sqlite3ExprListDelete(db, p->pUpsertTarget);
153120 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
153121 sqlite3ExprListDelete(db, p->pUpsertSet);
153122 sqlite3ExprDelete(db, p->pUpsertWhere);
153123 sqlite3DbFree(db, p->pToFree);
153139 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
153140 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
153141 sqlite3ExprListDup(db, p->pUpsertSet, 0),
153142 sqlite3ExprDup(db, p->pUpsertWhere, 0),
153143 sqlite3UpsertDup(db, p->pNextUpsert)
153168 pNew->pUpsertTarget = pTarget;
153169 pNew->pUpsertTargetWhere = pTargetWhere;
153170 pNew->pUpsertSet = pSet;
153171 pNew->pUpsertWhere = pWhere;
153172 pNew->isDoUpdate = pSet!=0;
153173 pNew->pNextUpsert = pNext;
153180 ** symbols in the conflict-target.
153195 ExprList *pTarget; /* The conflict-target clause */
153196 Expr *pTerm; /* One term of the conflict-target clause */
153201 assert( pTabList->nSrc==1 );
153202 assert( pTabList->a[0].pTab!=0 );
153204 assert( pUpsert->pUpsertTarget!=0 );
153206 /* Resolve all symbolic names in the conflict-target clause, which
153207 ** includes both the list of columns and the optional partial-index
153213 for(; pUpsert && pUpsert->pUpsertTarget;
153214 pUpsert=pUpsert->pNextUpsert, nClause++){
153215 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
153217 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
153221 pTab = pTabList->a[0].pTab;
153222 pTarget = pUpsert->pUpsertTarget;
153223 iCursor = pTabList->a[0].iCursor;
153225 && pTarget->nExpr==1
153226 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
153227 && pTerm->iColumn==XN_ROWID
153229 /* The conflict-target is the rowid of the primary table */
153230 assert( pUpsert->pUpsertIdx==0 );
153238 ** prior to comparing against the conflict-target expression.
153244 sCol[1].iTable = pTabList->a[0].iCursor;
153247 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153250 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
153251 if( pIdx->pPartIdxWhere ){
153252 if( pUpsert->pUpsertTargetWhere==0 ) continue;
153253 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
153254 pIdx->pPartIdxWhere, iCursor)!=0 ){
153258 nn = pIdx->nKeyCol;
153261 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
153262 if( pIdx->aiColumn[ii]==XN_EXPR ){
153263 assert( pIdx->aColExpr!=0 );
153264 assert( pIdx->aColExpr->nExpr>ii );
153265 assert( pIdx->bHasExpr );
153266 pExpr = pIdx->aColExpr->a[ii].pExpr;
153267 if( pExpr->op!=TK_COLLATE ){
153273 sCol[1].iColumn = pIdx->aiColumn[ii];
153277 if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
153291 pUpsert->pUpsertIdx = pIdx;
153295 ** after multi-CONFLICT upsert was added, and so we silently ignore
153298 pUpsert->isDup = 1;
153302 if( pUpsert->pUpsertIdx==0 ){
153304 if( nClause==0 && pUpsert->pNextUpsert==0 ){
153325 pNext = pUpsert->pNextUpsert;
153326 while( 1 /*exit-by-return*/ ){
153328 if( pNext->pUpsertTarget==0 ) return 1;
153329 if( pNext->pUpsertIdx==0 ) return 1;
153330 if( !pNext->isDup ) return 0;
153331 pNext = pNext->pNextUpsert;
153345 && pUpsert->pUpsertTarget!=0
153346 && pUpsert->pUpsertIdx!=pIdx
153348 pUpsert = pUpsert->pNextUpsert;
153357 ** In this case parameter iCur is a cursor open on the table b-tree that
153363 Parse *pParse, /* The parsing and code-generating context */
153369 Vdbe *v = pParse->pVdbe;
153370 sqlite3 *db = pParse->db;
153378 iDataCur = pUpsert->iDataCur;
153390 int nPk = pPk->nKeyCol;
153391 int iPk = pParse->nMem+1;
153392 pParse->nMem += nPk;
153395 assert( pPk->aiColumn[i]>=0 );
153396 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
153398 VdbeComment((v, "%s.%s", pIdx->zName,
153399 pTab->aCol[pPk->aiColumn[i]].zCnName));
153410 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
153412 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
153414 for(i=0; i<pTab->nCol; i++){
153415 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
153416 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
153419 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
153420 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
153464 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
153536 if( pParse->nErr ) goto build_vacuum_end;
153545 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
153546 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
153549 iDb = sqlite3FindDb(pParse->db, pNm);
153556 iIntoReg = ++pParse->nMem;
153563 sqlite3ExprDelete(pParse->db, pInto);
153579 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
153580 u64 saved_flags; /* Saved value of db->flags */
153581 i64 saved_nChange; /* Saved value of db->nChange */
153582 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
153583 u32 saved_openFlags; /* Saved value of db->openFlags */
153593 if( !db->autoCommit ){
153595 return SQLITE_ERROR; /* IMP: R-12218-18073 */
153597 if( db->nVdbeActive>1 ){
153598 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
153599 return SQLITE_ERROR; /* IMP: R-15610-35227 */
153601 saved_openFlags = db->openFlags;
153604 sqlite3SetString(pzErrMsg, db, "non-text filename");
153608 db->openFlags &= ~SQLITE_OPEN_READONLY;
153609 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
153615 ** restored before returning. Then set the writable-schema flag, and
153617 saved_flags = db->flags;
153618 saved_mDbFlags = db->mDbFlags;
153619 saved_nChange = db->nChange;
153620 saved_nTotalChange = db->nTotalChange;
153621 saved_mTrace = db->mTrace;
153622 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
153623 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
153624 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
153626 db->mTrace = 0;
153628 zDbMain = db->aDb[iDb].zDbSName;
153629 pMain = db->aDb[iDb].pBt;
153638 ** An optimization would be to use a non-journaled pager.
153646 nDb = db->nDb;
153648 db->openFlags = saved_openFlags;
153650 assert( (db->nDb-1)==nDb );
153651 pDb = &db->aDb[nDb];
153652 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
153653 pTemp = pDb->pBt;
153657 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
153662 db->mDbFlags |= DBFLAG_VacuumInto;
153664 /* For a VACUUM INTO, the pager-flags are set to the same values as
153667 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
153671 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
153677 ** to ensure that we do not try to change the page-size on a WAL database.
153689 db->nextPagesize = 0;
153693 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
153694 || NEVER(db->mallocFailed)
153701 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
153708 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
153722 db->init.iDb = 0;
153735 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
153736 db->mDbFlags &= ~DBFLAG_Vacuum;
153755 ** both transactions are closed by this block - the main database
153809 /* Restore the original value of db->flags */
153810 db->init.iDb = 0;
153811 db->mDbFlags = saved_mDbFlags;
153812 db->flags = saved_flags;
153813 db->nChange = saved_nChange;
153814 db->nTotalChange = saved_nTotalChange;
153815 db->mTrace = saved_mTrace;
153816 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
153825 db->autoCommit = 1;
153828 sqlite3BtreeClose(pDb->pBt);
153829 pDb->pBt = 0;
153830 pDb->pSchema = 0;
153833 /* This both clears the schemas and reduces the size of the db->aDb[]
153904 pMod->zName = zCopy;
153905 pMod->pModule = pModule;
153906 pMod->pAux = pAux;
153907 pMod->xDestroy = xDestroy;
153908 pMod->pEpoTab = 0;
153909 pMod->nRefModule = 1;
153911 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
153939 sqlite3_mutex_enter(db->mutex);
153943 sqlite3_mutex_leave(db->mutex);
153949 ** External API function used to create a new virtual-table module.
153964 ** External API function used to create a new virtual-table module.
153980 ** External API to drop all virtual-table modules, except those named
153988 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
153993 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
153996 createModule(db, pMod->zName, 0, 0, 0);
154006 assert( pMod->nRefModule>0 );
154007 pMod->nRefModule--;
154008 if( pMod->nRefModule==0 ){
154009 if( pMod->xDestroy ){
154010 pMod->xDestroy(pMod->pAux);
154012 assert( pMod->pEpoTab==0 );
154026 pVTab->nRef++;
154031 ** pTab is a pointer to a Table structure representing a virtual-table.
154033 ** this virtual-table, if one has been created, or NULL otherwise.
154038 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
154043 ** Decrement the ref-count on a virtual table object. When the ref-count
154047 sqlite3 *db = pVTab->db;
154050 assert( pVTab->nRef>0 );
154051 assert( db->eOpenState==SQLITE_STATE_OPEN
154052 || db->eOpenState==SQLITE_STATE_ZOMBIE );
154054 pVTab->nRef--;
154055 if( pVTab->nRef==0 ){
154056 sqlite3_vtab *p = pVTab->pVtab;
154058 p->pModule->xDisconnect(p);
154060 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
154067 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
154070 ** connection db is left in the p->u.vtab.p list.
154077 pVTable = p->u.vtab.p;
154078 p->u.vtab.p = 0;
154084 ** database connection that may have an entry in the p->u.vtab.p list.
154086 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
154089 sqlite3 *db2 = pVTable->db;
154090 VTable *pNext = pVTable->pNext;
154094 p->u.vtab.p = pRet;
154095 pRet->pNext = 0;
154097 pVTable->pNext = db2->pDisconnect;
154098 db2->pDisconnect = pVTable;
154110 ** list in p->pVTab. It also decrements the VTable ref count. This is
154113 ** be being used by other shared-cache connections).
154120 assert( sqlite3_mutex_held(db->mutex) );
154122 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
154123 if( (*ppVTab)->db==db ){
154125 *ppVTab = pVTab->pNext;
154137 ** shared b-tree databases opened using connection db are held by the
154147 ** or, if the virtual table is stored in a non-sharable database, then
154151 ** by multiple threads. It is thread-safe.
154154 VTable *p = db->pDisconnect;
154157 assert( sqlite3_mutex_held(db->mutex) );
154160 db->pDisconnect = 0;
154163 VTable *pNext = p->pNext;
154171 ** Clear any and all virtual-table information from the Table record.
154175 ** Since it is a virtual-table, the Table structure contains a pointer
154187 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
154188 if( p->u.vtab.azArg ){
154190 for(i=0; i<p->u.vtab.nArg; i++){
154191 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
154193 sqlite3DbFree(db, p->u.vtab.azArg);
154198 ** Add a new module argument to pTable->u.vtab.azArg[].
154199 ** The string is not copied - the pointer is stored. The
154206 sqlite3 *db = pParse->db;
154209 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
154210 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
154211 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
154213 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
154217 int i = pTable->u.vtab.nArg++;
154220 pTable->u.vtab.azArg = azModuleArg;
154240 pTable = pParse->pNewTable;
154242 assert( 0==pTable->pIndex );
154243 pTable->eTabType = TABTYP_VTAB;
154245 db = pParse->db;
154247 assert( pTable->u.vtab.nArg==0 );
154250 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
154251 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
154252 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
154254 pParse->sNameToken.n = (int)(
154255 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
154264 if( pTable->u.vtab.azArg ){
154265 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
154267 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
154268 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
154275 ** in pParse->zArg[] and appends it to the list of arguments on the
154276 ** virtual table currently under construction in pParse->pTable.
154279 if( pParse->sArg.z && pParse->pNewTable ){
154280 const char *z = (const char*)pParse->sArg.z;
154281 int n = pParse->sArg.n;
154282 sqlite3 *db = pParse->db;
154283 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
154292 Table *pTab = pParse->pNewTable; /* The table being constructed */
154293 sqlite3 *db = pParse->db; /* The database connection */
154298 pParse->sArg.z = 0;
154299 if( pTab->u.vtab.nArg<1 ) return;
154307 if( !db->init.busy ){
154318 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
154320 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
154326 ** The VM register number pParse->regRowid holds the rowid of an
154330 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
154335 db->aDb[iDb].zDbSName,
154336 pTab->zName,
154337 pTab->zName,
154339 pParse->regRowid
154345 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
154349 iReg = ++pParse->nMem;
154350 sqlite3VdbeLoadString(v, iReg, pTab->zName);
154353 /* If we are rereading the sqlite_schema table create the in-memory
154356 Schema *pSchema = pTab->pSchema;
154357 const char *zName = pTab->zName;
154360 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
154366 pParse->pNewTable = 0;
154376 pParse->sArg.z = 0;
154377 pParse->sArg.n = 0;
154385 Token *pArg = &pParse->sArg;
154386 if( pArg->z==0 ){
154387 pArg->z = p->z;
154388 pArg->n = p->n;
154390 assert(pArg->z <= p->z);
154391 pArg->n = (int)(&p->z[p->n] - pArg->z);
154411 int nArg = pTab->u.vtab.nArg;
154418 azArg = (const char *const*)pTab->u.vtab.azArg;
154420 /* Check that the virtual-table is not already being initialized */
154421 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
154422 if( pCtx->pTab==pTab ){
154424 "vtable constructor called recursively: %s", pTab->zName
154430 zModuleName = sqlite3DbStrDup(db, pTab->zName);
154441 pVTable->db = db;
154442 pVTable->pMod = pMod;
154443 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
154445 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
154446 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
154449 assert( &db->pVtabCtx );
154453 sCtx.pPrior = db->pVtabCtx;
154455 db->pVtabCtx = &sCtx;
154456 pTab->nTabRef++;
154457 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
154459 db->pVtabCtx = sCtx.pPrior;
154471 }else if( ALWAYS(pVTable->pVtab) ){
154474 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
154475 pVTable->pVtab->pModule = pMod->pModule;
154476 pMod->nRefModule++;
154477 pVTable->nRef = 1;
154480 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
154487 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
154491 pVTable->pNext = pTab->u.vtab.p;
154492 pTab->u.vtab.p = pVTable;
154494 for(iCol=0; iCol<pTab->nCol; iCol++){
154495 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
154501 && (i==0 || zType[i-1]==' ')
154514 assert(zType[i-1]==' ');
154515 zType[i-1] = '\0';
154517 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
154518 pTab->tabFlags |= TF_HasHidden;
154521 pTab->tabFlags |= oooHidden;
154536 ** This call is a no-op if table pTab is not a virtual table.
154539 sqlite3 *db = pParse->db;
154551 zMod = pTab->u.vtab.azArg[0];
154552 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
154555 const char *zModule = pTab->u.vtab.azArg[0];
154560 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
154563 pParse->rc = rc;
154571 ** Grow the db->aVTrans[] array so that there is room for at least one
154572 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
154578 if( (db->nVTrans%ARRAY_INCR)==0 ){
154581 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
154582 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
154586 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
154587 db->aVTrans = aVTrans;
154599 db->aVTrans[db->nVTrans++] = pVTab;
154617 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
154618 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
154621 zMod = pTab->u.vtab.azArg[0];
154622 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
154628 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
154632 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
154664 sqlite3_mutex_enter(db->mutex);
154665 pCtx = db->pVtabCtx;
154666 if( !pCtx || pCtx->bDeclared ){
154668 sqlite3_mutex_leave(db->mutex);
154671 pTab = pCtx->pTab;
154678 ** schema. Nevertheless, defend against that (turn off db->init.busy)
154680 assert( db->init.busy==0 );
154681 initBusy = db->init.busy;
154682 db->init.busy = 0;
154686 && ALWAYS(!db->mallocFailed)
154690 if( !pTab->aCol ){
154693 pTab->aCol = pNew->aCol;
154694 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
154695 pTab->nNVCol = pTab->nCol = pNew->nCol;
154696 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
154697 pNew->nCol = 0;
154698 pNew->aCol = 0;
154699 assert( pTab->pIndex==0 );
154702 && pCtx->pVTable->pMod->pModule->xUpdate!=0
154703 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
154705 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
154706 ** or else must have a single-column PRIMARY KEY */
154709 pIdx = pNew->pIndex;
154711 assert( pIdx->pNext==0 );
154712 pTab->pIndex = pIdx;
154713 pNew->pIndex = 0;
154714 pIdx->pTable = pTab;
154717 pCtx->bDeclared = 1;
154731 db->init.busy = initBusy;
154735 sqlite3_mutex_leave(db->mutex);
154744 ** This call is a no-op if zTab is not a virtual table.
154750 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
154753 && ALWAYS(pTab->u.vtab.p!=0)
154757 for(p=pTab->u.vtab.p; p; p=p->pNext){
154758 assert( p->pVtab );
154759 if( p->pVtab->nRef>0 ){
154764 xDestroy = p->pMod->pModule->xDestroy;
154765 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
154767 pTab->nTabRef++;
154768 rc = xDestroy(p->pVtab);
154771 assert( pTab->u.vtab.p==p && p->pNext==0 );
154772 p->pVtab = 0;
154773 pTab->u.vtab.p = 0;
154792 if( db->aVTrans ){
154793 VTable **aVTrans = db->aVTrans;
154794 db->aVTrans = 0;
154795 for(i=0; i<db->nVTrans; i++){
154797 sqlite3_vtab *p = pVTab->pVtab;
154800 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
154803 pVTab->iSavepoint = 0;
154807 db->nVTrans = 0;
154816 ** If an error message is available, leave it in p->zErrMsg.
154821 VTable **aVTrans = db->aVTrans;
154823 db->aVTrans = 0;
154824 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
154826 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
154827 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
154832 db->aVTrans = aVTrans;
154866 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
154877 pModule = pVTab->pVtab->pModule;
154879 if( pModule->xBegin ){
154883 for(i=0; i<db->nVTrans; i++){
154884 if( db->aVTrans[i]==pVTab ){
154893 rc = pModule->xBegin(pVTab->pVtab);
154895 int iSvpt = db->nStatement + db->nSavepoint;
154897 if( iSvpt && pModule->xSavepoint ){
154898 pVTab->iSavepoint = iSvpt;
154899 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
154926 assert( iSavepoint>=-1 );
154927 if( db->aVTrans ){
154929 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
154930 VTable *pVTab = db->aVTrans[i];
154931 const sqlite3_module *pMod = pVTab->pMod->pModule;
154932 if( pVTab->pVtab && pMod->iVersion>=2 ){
154937 xMethod = pMod->xSavepoint;
154938 pVTab->iSavepoint = iSavepoint+1;
154941 xMethod = pMod->xRollbackTo;
154944 xMethod = pMod->xRelease;
154947 if( xMethod && pVTab->iSavepoint>iSavepoint ){
154948 u64 savedFlags = (db->flags & SQLITE_Defensive);
154949 db->flags &= ~(u64)SQLITE_Defensive;
154950 rc = xMethod(pVTab->pVtab, iSavepoint);
154951 db->flags |= savedFlags;
154989 if( pExpr->op!=TK_COLUMN ) return pDef;
154991 pTab = pExpr->y.pTab;
154994 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
154996 assert( pVtab->pModule!=0 );
154997 pMod = (sqlite3_module *)pVtab->pModule;
154998 if( pMod->xFindFunction==0 ) return pDef;
155004 ** with an all lower-case function name. Continue in this tradition to
155010 for(i=0; pDef->zName[i]; i++){
155011 unsigned char x = (unsigned char)pDef->zName[i];
155016 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
155024 + sqlite3Strlen30(pDef->zName) + 1);
155029 pNew->zName = (const char*)&pNew[1];
155030 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
155031 pNew->xSFunc = xSFunc;
155032 pNew->pUserData = pArg;
155033 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
155038 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
155041 ** is a no-op.
155049 for(i=0; i<pToplevel->nVtabLock; i++){
155050 if( pTab==pToplevel->apVtabLock[i] ) return;
155052 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
155053 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
155055 pToplevel->apVtabLock = apVtabLock;
155056 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
155058 sqlite3OomFault(pToplevel->db);
155065 ** exist. Return non-zero if either the eponymous virtual table instance
155072 ** instances always exist. They cannot be DROP-ed.
155078 const sqlite3_module *pModule = pMod->pModule;
155082 sqlite3 *db = pParse->db;
155083 if( pMod->pEpoTab ) return 1;
155084 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
155087 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
155088 if( pTab->zName==0 ){
155092 pMod->pEpoTab = pTab;
155093 pTab->nTabRef = 1;
155094 pTab->eTabType = TABTYP_VTAB;
155095 pTab->pSchema = db->aDb[0].pSchema;
155096 assert( pTab->u.vtab.nArg==0 );
155097 pTab->iPKey = -1;
155098 pTab->tabFlags |= TF_Eponymous;
155099 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
155101 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
155102 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
155116 Table *pTab = pMod->pEpoTab;
155121 pTab->tabFlags |= TF_Ephemeral;
155123 pMod->pEpoTab = 0;
155143 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
155144 return (int)aMap[db->vtabOnConflict-1];
155160 sqlite3_mutex_enter(db->mutex);
155161 p = db->pVtabCtx;
155165 assert( p->pTab==0 || IsVirtual(p->pTab) );
155169 p->pVTable->bConstraint = (u8)va_arg(ap, int);
155173 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
155177 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
155181 p->pVTable->bAllSchemas = 1;
155193 sqlite3_mutex_leave(db->mutex);
155202 ** 2015-06-06
155215 ** This file was split off from where.c on 2015-06-06 in order to reduce the
155224 ** 2013-11-12
155293 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
155301 int addrSkip; /* Jump here for next iteration of skip-scan */
155305 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
155306 int addrBignull; /* Jump here for next part of big-null scan */
155316 union { /* Information that depends on pWLoop->wsFlags */
155322 int iBase; /* Base register of multi-key index record */
155326 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
155340 ** prevent a query solution - which is an error) and many terms of the
155342 ** potential way of implementing that FROM-clause term, together with
155358 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
155401 ** correspond to the subquery(s) of OR-clause processing. Only the
155417 ** vector, not a scalar, and because dependencies are many-to-one, not
155418 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
155434 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
155447 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
155478 ** spread out over the non-negative integers. For example, the cursor
155497 int iParent; /* Disable pWC->a[iParent] when this term disabled */
155507 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
155520 #define TERM_OK 0x0040 /* Used during OR-clause processing */
155526 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
155533 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
155544 int k; /* Resume scanning at this->pWC->a[this->k] */
155550 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
155572 int nBase; /* Number of terms through the last non-Virtual */
155610 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
155611 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
155618 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
155619 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
155658 ** routine. The limit is high enough that is should not impact real-world
155696 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
155705 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
155719 ** Private interfaces - callable only by other where.c routines.
155771 int iLevel, /* Which level of pWInfo->a[] should be coded */
155799 ** OR-ed combination of these values can be used when searching for
155811 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
155812 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
155813 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
155814 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
155818 #define WO_OR 0x0200 /* Two or more OR-connected terms */
155819 #define WO_AND 0x0400 /* Two or more AND-connected terms */
155822 #define WO_ROWVAL 0x2000 /* A row-value term */
155825 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
155840 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
155848 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
155853 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
155855 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
155858 /* 0x02000000 -- available for reuse */
155859 #define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
155869 ** Return the name of the i-th column of the pIdx index.
155872 i = pIdx->aiColumn[i];
155875 return pIdx->pTable->aCol[i].zCnName;
155890 int iTerm, /* Zero-based index of first term. */
155891 int bAnd, /* Non-zero to append " AND " */
155931 Index *pIndex = pLoop->u.btree.pIndex;
155932 u16 nEq = pLoop->u.btree.nEq;
155933 u16 nSkip = pLoop->nSkip;
155936 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
155945 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
155946 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
155949 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
155950 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
155956 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
155958 ** was defined at compile-time. If it is not a no-op, a single OP_Explain
155972 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
155975 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
155976 Vdbe *v = pParse->pVdbe; /* VM being constructed */
155977 sqlite3 *db = pParse->db; /* Database handle */
155985 pLoop = pLevel->pWLoop;
155986 flags = pLoop->wsFlags;
155990 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
156000 assert( pLoop->u.btree.pIndex!=0 );
156001 pIdx = pLoop->u.btree.pIndex;
156003 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
156018 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
156024 const Table *pTab = pItem->pTab;
156025 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
156047 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
156050 if( pItem->fg.jointype & JT_LEFT ){
156051 sqlite3_str_appendf(&str, " LEFT-JOIN");
156054 if( pLoop->nOut>=10 ){
156056 sqlite3LogEstToInt(pLoop->nOut));
156064 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
156074 ** required and this routine is a no-op.
156085 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
156086 Vdbe *v = pParse->pVdbe; /* VM being constructed */
156087 sqlite3 *db = pParse->db; /* Database handle */
156097 pLoop = pLevel->pWLoop;
156098 if( pLoop->wsFlags & WHERE_IPK ){
156099 const Table *pTab = pItem->pTab;
156100 if( pTab->iPKey>=0 ){
156101 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
156106 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
156107 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
156108 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
156115 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
156117 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
156140 WhereLoop *pLoop = pLvl->pWLoop;
156141 int wsFlags = pLoop->wsFlags;
156144 if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
156145 zObj = pLoop->u.btree.pIndex->zName;
156147 zObj = pSrclist->a[pLvl->iFrom].zName;
156148 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
156151 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
156156 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
156159 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
156162 int addr = pSrclist->a[pLvl->iFrom].addrFillSub;
156163 VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1);
156164 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
156165 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
156166 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
156213 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
156218 while( (pTerm->wtFlags & TERM_CODED)==0
156219 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
156220 && (pLevel->notReady & pTerm->prereqAll)==0
156222 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
156223 pTerm->wtFlags |= TERM_LIKECOND;
156225 pTerm->wtFlags |= TERM_CODED;
156229 sqlite3DebugPrintf("DISABLE-");
156230 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
156233 if( pTerm->iParent<0 ) break;
156234 pTerm = &pTerm->pWC->a[pTerm->iParent];
156236 pTerm->nChild--;
156237 if( pTerm->nChild!=0 ) break;
156247 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
156254 Vdbe *v = pParse->pVdbe;
156256 assert( pParse->db->mallocFailed );
156266 n--;
156270 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
156271 n--;
156342 sqlite3 *db = pParse->db;
156346 if( db->mallocFailed==0 ){
156347 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
156355 pOrigRhs = pSelect->pEList;
156356 assert( pNew->pLeft!=0 );
156357 assert( ExprUseXList(pNew->pLeft) );
156358 if( pSelect==pNew->x.pSelect ){
156359 pOrigLhs = pNew->pLeft->x.pList;
156361 for(i=iEq; i<pLoop->nLTerm; i++){
156362 if( pLoop->aLTerm[i]->pExpr==pX ){
156364 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
156365 iField = pLoop->aLTerm[i]->u.x.iField - 1;
156366 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
156367 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
156368 pOrigRhs->a[iField].pExpr = 0;
156370 assert( pOrigLhs->a[iField].pExpr!=0 );
156371 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
156372 pOrigLhs->a[iField].pExpr = 0;
156379 pNew->pLeft->x.pList = pLhs;
156381 pSelect->pEList = pRhs;
156382 if( pLhs && pLhs->nExpr==1 ){
156386 Expr *p = pLhs->a[0].pExpr;
156387 pLhs->a[0].pExpr = 0;
156388 sqlite3ExprDelete(db, pNew->pLeft);
156389 pNew->pLeft = p;
156391 if( pSelect->pOrderBy ){
156393 ** iOrderByCol variables. These are set to non-zero when an
156395 ** result-set. Since the result-set of the SELECT statement may
156399 ExprList *pOrderBy = pSelect->pOrderBy;
156400 for(i=0; i<pOrderBy->nExpr; i++){
156401 pOrderBy->a[i].u.x.iOrderByCol = 0;
156429 ** straight-line code. For constraints of the form X IN (...)
156437 int bRev, /* True for reverse-order IN operations */
156440 Expr *pX = pTerm->pExpr;
156441 Vdbe *v = pParse->pVdbe;
156444 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
156446 if( pX->op==TK_EQ || pX->op==TK_IS ){
156447 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
156448 }else if( pX->op==TK_ISNULL ){
156456 WhereLoop *pLoop = pLevel->pWLoop;
156461 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
156462 && pLoop->u.btree.pIndex!=0
156463 && pLoop->u.btree.pIndex->aSortOrder[iEq]
156469 assert( pX->op==TK_IN );
156473 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
156478 for(i=iEq;i<pLoop->nLTerm; i++){
156479 assert( pLoop->aLTerm[i]!=0 );
156480 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
156484 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
156487 Expr *pExpr = pTerm->pExpr;
156488 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
156489 sqlite3 *db = pParse->db;
156491 if( !db->mallocFailed ){
156492 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
156494 pExpr->iTable = iTab;
156498 int n = sqlite3ExprVectorSize(pX->pLeft);
156499 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
156513 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
156514 pLoop->wsFlags |= WHERE_IN_ABLE;
156515 if( pLevel->u.in.nIn==0 ){
156516 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
156518 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
156519 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
156522 i = pLevel->u.in.nIn;
156523 pLevel->u.in.nIn += nEq;
156524 pLevel->u.in.aInLoop =
156525 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
156526 pLevel->u.in.aInLoop,
156527 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
156528 pIn = pLevel->u.in.aInLoop;
156532 for(i=iEq;i<pLoop->nLTerm; i++){
156533 if( pLoop->aLTerm[i]->pExpr==pX ){
156534 int iOut = iReg + i - iEq;
156536 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
156539 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
156543 pIn->iCur = iTab;
156544 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
156546 pIn->iBase = iReg - i;
156547 pIn->nPrefix = i;
156549 pIn->nPrefix = 0;
156552 pIn->eEndLoopOp = OP_Noop;
156558 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
156559 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
156561 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
156563 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
156566 pLevel->u.in.nIn = 0;
156568 sqlite3DbFree(pParse->db, aiMap);
156579 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
156581 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
156582 || (pTerm->eOperator & WO_EQUIV)==0
156603 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
156604 ** The only thing it does is allocate the pLevel->iMem memory cell and
156643 u16 nSkip; /* Number of left-most columns to skip */
156644 Vdbe *v = pParse->pVdbe; /* The vm under construction */
156654 pLoop = pLevel->pWLoop;
156655 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
156656 nEq = pLoop->u.btree.nEq;
156657 nSkip = pLoop->nSkip;
156658 pIdx = pLoop->u.btree.pIndex;
156663 regBase = pParse->nMem + 1;
156665 pParse->nMem += nReg;
156667 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
156668 assert( zAff!=0 || pParse->db->mallocFailed );
156671 int iIdxCur = pLevel->iIdxCur;
156672 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
156676 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
156678 assert( pLevel->addrSkip==0 );
156679 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
156686 testcase( pIdx->aiColumn[j]==XN_EXPR );
156696 pTerm = pLoop->aLTerm[j];
156700 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
156701 testcase( pTerm->wtFlags & TERM_VIRTUAL );
156711 if( pTerm->eOperator & WO_IN ){
156712 if( pTerm->pExpr->flags & EP_xIsSelect ){
156719 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
156720 Expr *pRight = pTerm->pExpr->pRight;
156721 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
156722 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
156725 if( pParse->nErr==0 ){
156726 assert( pParse->db->mallocFailed==0 );
156756 ** becomes a no-op.
156763 if( pTerm->wtFlags & TERM_LIKEOPT ){
156765 assert( pLevel->iLikeRepCntr>0 );
156768 assert( pOp->opcode==OP_String8
156769 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
156770 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
156771 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
156794 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
156797 struct CCurHint *pHint = pWalker->u.pCCurHint;
156798 assert( pHint->pIdx!=0 );
156799 if( pExpr->op==TK_COLUMN
156800 && pExpr->iTable==pHint->iTabCur
156801 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
156803 pWalker->eCode = 1;
156810 ** should be included in the cursor-hint for a table that is on the rhs
156811 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
156824 if( pExpr->op==TK_IS
156825 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
156826 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
156828 pWalker->eCode = 1;
156829 }else if( pExpr->op==TK_FUNCTION ){
156832 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
156833 pWalker->eCode = 1;
156861 struct CCurHint *pHint = pWalker->u.pCCurHint;
156862 if( pExpr->op==TK_COLUMN ){
156863 if( pExpr->iTable!=pHint->iTabCur ){
156864 reg = ++pWalker->pParse->nMem; /* Register for column value */
156865 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
156866 pExpr->op = TK_REGISTER;
156867 pExpr->iTable = reg;
156868 }else if( pHint->pIdx!=0 ){
156869 pExpr->iTable = pHint->iIdxCur;
156870 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
156871 assert( pExpr->iColumn>=0 );
156873 }else if( pExpr->pAggInfo ){
156875 reg = ++pWalker->pParse->nMem; /* Register for column value */
156876 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
156877 pExpr->op = TK_REGISTER;
156878 pExpr->iTable = reg;
156879 }else if( pExpr->op==TK_TRUEFALSE ){
156880 /* Do not walk disabled expressions. tag-20230504-1 */
156893 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
156895 Parse *pParse = pWInfo->pParse;
156896 sqlite3 *db = pParse->db;
156897 Vdbe *v = pParse->pVdbe;
156899 WhereLoop *pLoop = pLevel->pWLoop;
156908 iCur = pLevel->iTabCur;
156909 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
156911 sHint.iIdxCur = pLevel->iIdxCur;
156912 sHint.pIdx = pLoop->u.btree.pIndex;
156916 pWC = &pWInfo->sWC;
156917 for(i=0; i<pWC->nBase; i++){
156918 pTerm = &pWC->a[i];
156919 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
156920 if( pTerm->prereqAll & pLevel->notReady ) continue;
156924 ** from the cursor-hint.
156944 if( pTabItem->fg.jointype & JT_LEFT ){
156945 Expr *pExpr = pTerm->pExpr;
156947 || pExpr->w.iJoin!=pTabItem->iCursor
156951 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
156955 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
156958 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
156961 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
156962 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
156963 if( j<pLoop->nLTerm ) continue;
156966 /* No subqueries or non-deterministic functions allowed */
156967 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
156974 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
156979 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
156983 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
156990 # define codeCursorHint(A,B,C,D) /* No-op */
156994 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
157005 ** However, if the scan currently being coded is a branch of an OR-loop and
157022 int iCur, /* Cursor for IPK b-tree */
157025 Parse *pParse = pWInfo->pParse; /* Parse context */
157026 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
157029 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
157031 pWInfo->bDeferredSeek = 1;
157033 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
157034 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
157037 Table *pTab = pIdx->pTable;
157038 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
157040 ai[0] = pTab->nCol;
157041 for(i=0; i<pIdx->nColumn-1; i++){
157043 assert( pIdx->aiColumn[i]<pTab->nCol );
157044 x1 = pIdx->aiColumn[i];
157049 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
157068 Vdbe *v = pParse->pVdbe;
157070 assert( p->op==TK_SELECT );
157072 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
157079 pList = p->x.pList;
157080 assert( nReg<=pList->nExpr );
157082 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
157086 assert( nReg==1 || pParse->nErr );
157105 while( pTruth->op==TK_AND ){
157106 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
157107 pTruth = pTruth->pRight;
157109 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157111 if( pTerm->wtFlags & TERM_CODED ) continue;
157112 pExpr = pTerm->pExpr;
157114 pTerm->wtFlags |= TERM_CODED;
157124 ** inner-loop Bloom filters, then evaluate those filters now, before the
157130 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
157137 int iLevel, /* Which level of pWInfo->a[] should be coded */
157141 while( ++iLevel < pWInfo->nLevel ){
157142 WhereLevel *pLevel = &pWInfo->a[iLevel];
157143 WhereLoop *pLoop = pLevel->pWLoop;
157144 if( pLevel->regFilter==0 ) continue;
157145 if( pLevel->pWLoop->nSkip ) continue;
157146 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
157147 ** vvvvv--' pLevel->regFilter if this were true. */
157148 if( NEVER(pLoop->prereq & notReady) ) continue;
157149 assert( pLevel->addrBrk==0 );
157150 pLevel->addrBrk = addrNxt;
157151 if( pLoop->wsFlags & WHERE_IPK ){
157152 WhereTerm *pTerm = pLoop->aLTerm[0];
157155 assert( pTerm->pExpr!=0 );
157156 testcase( pTerm->wtFlags & TERM_VIRTUAL );
157159 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
157160 VdbeCoverage(pParse->pVdbe);
157161 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
157163 VdbeCoverage(pParse->pVdbe);
157165 u16 nEq = pLoop->u.btree.nEq;
157169 assert( pLoop->wsFlags & WHERE_INDEXED );
157170 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
157173 sqlite3DbFree(pParse->db, zStartAff);
157174 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
157176 VdbeCoverage(pParse->pVdbe);
157178 pLevel->regFilter = 0;
157179 pLevel->addrBrk = 0;
157184 ** Generate code for the start of the iLevel-th loop in the WHERE clause
157191 int iLevel, /* Which level of pWInfo->a[] should be coded */
157212 pWC = &pWInfo->sWC;
157213 db = pParse->db;
157214 pLoop = pLevel->pWLoop;
157215 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
157216 iCur = pTabItem->iCursor;
157217 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
157218 bRev = (pWInfo->revMask>>iLevel)&1;
157219 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
157223 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
157231 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
157233 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
157248 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
157249 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
157255 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
157256 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
157258 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
157259 pLevel->iLeftJoin = ++pParse->nMem;
157260 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
157261 VdbeComment((v, "init LEFT JOIN no-match flag"));
157266 for(j=iLevel; j>0; j--){
157267 if( pWInfo->a[j].iLeftJoin ) break;
157268 if( pWInfo->a[j].pRJ ) break;
157270 addrHalt = pWInfo->a[j].addrBrk;
157272 /* Special case of a FROM clause subquery implemented as a co-routine */
157273 if( pTabItem->fg.viaCoroutine ){
157274 int regYield = pTabItem->regReturn;
157275 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
157276 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
157278 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
157279 pLevel->op = OP_Goto;
157283 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
157284 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
157289 int nConstraint = pLoop->nLTerm;
157292 addrNotFound = pLevel->addrBrk;
157295 pTerm = pLoop->aLTerm[j];
157297 if( pTerm->eOperator & WO_IN ){
157298 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
157299 int iTab = pParse->nTab++;
157300 int iCache = ++pParse->nMem;
157301 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
157305 addrNotFound = pLevel->addrNxt;
157308 Expr *pRight = pTerm->pExpr->pRight;
157310 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
157311 && pLoop->u.vtab.bOmitOffset
157313 assert( pTerm->eOperator==WO_AUX );
157314 assert( pWInfo->pSelect!=0 );
157315 assert( pWInfo->pSelect->iOffset>0 );
157316 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
157321 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
157324 pLoop->u.vtab.idxStr,
157325 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
157327 pLoop->u.vtab.needFree = 0;
157329 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
157330 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
157331 pLevel->p1 = iCur;
157332 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
157333 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
157334 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
157337 pTerm = pLoop->aLTerm[j];
157338 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
157342 if( (pTerm->eOperator & WO_IN)!=0
157343 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
157344 && !db->mallocFailed
157349 int iIn; /* IN loop corresponding to the j-th constraint */
157356 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
157357 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
157358 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
157359 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
157361 testcase( pOp->opcode==OP_Rowid );
157362 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
157371 if( !db->mallocFailed ){
157372 int iFld = pTerm->u.x.iField;
157373 Expr *pLeft = pTerm->pExpr->pLeft;
157376 assert( pLeft->op==TK_VECTOR );
157378 assert( iFld<=pLeft->x.pList->nExpr );
157379 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
157381 pCompare->pLeft = pLeft;
157383 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
157385 pRight->iTable = iReg+j+2;
157387 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
157390 pCompare->pLeft = 0;
157398 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
157406 if( (pLoop->wsFlags & WHERE_IPK)!=0
157407 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
157414 assert( pLoop->u.btree.nEq==1 );
157415 pTerm = pLoop->aLTerm[0];
157417 assert( pTerm->pExpr!=0 );
157418 testcase( pTerm->wtFlags & TERM_VIRTUAL );
157419 iReleaseReg = ++pParse->nMem;
157422 addrNxt = pLevel->addrNxt;
157423 if( pLevel->regFilter ){
157426 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
157433 pLevel->op = OP_Noop;
157434 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
157435 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
157446 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
157447 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
157473 assert( (pStart->wtFlags & TERM_VNULL)==0 );
157474 testcase( pStart->wtFlags & TERM_VIRTUAL );
157475 pX = pStart->pExpr;
157477 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
157478 if( sqlite3ExprIsVector(pX->pRight) ){
157480 codeExprOrVector(pParse, pX->pRight, r1, 1);
157481 testcase( pX->op==TK_GT );
157482 testcase( pX->op==TK_GE );
157483 testcase( pX->op==TK_LT );
157484 testcase( pX->op==TK_LE );
157485 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
157486 assert( pX->op!=TK_GT || op==OP_SeekGE );
157487 assert( pX->op!=TK_GE || op==OP_SeekGE );
157488 assert( pX->op!=TK_LT || op==OP_SeekLE );
157489 assert( pX->op!=TK_LE || op==OP_SeekLE );
157491 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
157493 op = aMoveOp[(pX->op - TK_GT)];
157497 VdbeCoverageIf(v, pX->op==TK_GT);
157498 VdbeCoverageIf(v, pX->op==TK_LE);
157499 VdbeCoverageIf(v, pX->op==TK_LT);
157500 VdbeCoverageIf(v, pX->op==TK_GE);
157509 pX = pEnd->pExpr;
157511 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
157512 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
157513 testcase( pEnd->wtFlags & TERM_VIRTUAL );
157514 memEndValue = ++pParse->nMem;
157515 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
157516 if( 0==sqlite3ExprIsVector(pX->pRight)
157517 && (pX->op==TK_LT || pX->op==TK_GT)
157523 if( 0==sqlite3ExprIsVector(pX->pRight) ){
157528 pLevel->op = bRev ? OP_Prev : OP_Next;
157529 pLevel->p1 = iCur;
157530 pLevel->p2 = start;
157531 assert( pLevel->p5==0 );
157533 iRowidReg = ++pParse->nMem;
157542 }else if( pLoop->wsFlags & WHERE_INDEXED ){
157547 ** left-most columns of the index. It may also contain
157550 ** the right-most column can be an inequality - the rest must
157590 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
157591 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
157592 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
157608 int regBignull = 0; /* big-null flag register */
157611 pIdx = pLoop->u.btree.pIndex;
157612 iIdxCur = pLevel->iIdxCur;
157613 assert( nEq>=pLoop->nSkip );
157619 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
157620 pRangeStart = pLoop->aLTerm[j++];
157621 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
157623 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
157624 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
157626 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
157627 pRangeEnd = pLoop->aLTerm[j++];
157628 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
157630 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
157632 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
157633 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
157634 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
157636 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
157640 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
157642 pLevel->iLikeRepCntr <<=1;
157643 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
157647 j = pIdx->aiColumn[nEq];
157648 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
157653 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
157656 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
157659 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
157662 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
157663 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
157667 testcase( pLoop->nSkip>0 );
157670 pLevel->regBignull = regBignull = ++pParse->nMem;
157671 if( pLevel->iLeftJoin ){
157674 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
157681 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
157687 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
157703 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
157705 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
157706 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
157707 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
157708 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
157709 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
157710 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
157716 Expr *pRight = pRangeStart->pExpr->pRight;
157719 if( (pRangeStart->wtFlags & TERM_VNULL)==0
157729 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
157746 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
157747 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
157748 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
157754 VdbeComment((v, "NULL-scan pass ctr"));
157756 if( pLevel->regFilter ){
157757 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
157765 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
157776 (pIdx->aiRowLogEst[0]+9)/10);
157801 nConstraint-startEq);
157815 assert( pLevel->p2==0 );
157817 Expr *pRight = pRangeEnd->pExpr->pRight;
157821 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
157831 assert( pParse->db->mallocFailed );
157834 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
157852 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
157857 /* Except, skip the end-of-range check while doing the NULL-scan */
157859 VdbeComment((v, "If NULL-scan 2nd pass"));
157871 /* During a NULL-scan, check to see if we have reached the end of
157877 VdbeComment((v, "If NULL-scan 1st pass"));
157888 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
157893 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
157894 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
157897 }else if( HasRowid(pIdx->pTable) ){
157900 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
157901 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
157902 for(j=0; j<pPk->nKeyCol; j++){
157903 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
157907 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
157910 if( pLevel->iLeftJoin==0 ){
157915 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
157918 if( pIdx->pPartIdxWhere ){
157919 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
157922 testcase( pIdx->pPartIdxWhere );
157924 ** The OR-optimization doesn't work for the right hand table of
157926 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
157930 if( pLoop->wsFlags & WHERE_ONEROW ){
157931 pLevel->op = OP_Noop;
157933 pLevel->op = OP_Prev;
157935 pLevel->op = OP_Next;
157937 pLevel->p1 = iIdxCur;
157938 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
157939 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
157940 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
157942 assert( pLevel->p5==0 );
157948 if( pLoop->wsFlags & WHERE_MULTI_OR ){
157987 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
157992 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
157993 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
157995 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
157997 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
158005 Table *pTab = pTabItem->pTab;
158007 pTerm = pLoop->aLTerm[0];
158009 assert( pTerm->eOperator & WO_OR );
158010 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
158011 pOrWc = &pTerm->u.pOrInfo->wc;
158012 pLevel->op = OP_Return;
158013 pLevel->p1 = regReturn;
158019 if( pWInfo->nLevel>1 ){
158022 nNotReady = pWInfo->nLevel - iLevel - 1;
158024 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
158026 pOrTab->nAlloc = (u8)(nNotReady + 1);
158027 pOrTab->nSrc = pOrTab->nAlloc;
158028 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
158029 origSrc = pWInfo->pTabList->a;
158031 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
158034 pOrTab = pWInfo->pTabList;
158045 ** correct response for the end-of-loop code (the OP_Return) is to
158049 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
158051 regRowset = ++pParse->nMem;
158055 regRowset = pParse->nTab++;
158056 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
158059 regRowid = ++pParse->nMem;
158069 ** the "interesting" terms of z - terms that did not originate in the
158077 ** 2022-02-04: Do not push down slices of a row-value comparison.
158079 ** the initialization of the right-hand operand of the vector comparison
158083 ** 2022-03-03: Do not push down expressions that involve subqueries.
158084 ** The subquery might get coded as a subroutine. Any table-references
158085 ** in the subquery might be resolved to index-references for the index on
158088 ** index-references will not work. tag-20220303a
158091 if( pWC->nTerm>1 ){
158093 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
158094 Expr *pExpr = pWC->a[iTerm].pExpr;
158095 if( &pWC->a[iTerm] == pTerm ) continue;
158096 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
158097 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
158098 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
158099 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
158102 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
158103 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
158111 ** prevents sqlite3PExpr() from applying the AND short-circuit
158119 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
158121 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
158122 for(ii=0; ii<pOrWc->nTerm; ii++){
158123 WhereTerm *pOrTerm = &pOrWc->a[ii];
158124 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
158125 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
158126 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
158133 if( db->mallocFailed ){
158138 pAndExpr->pLeft = pOrExpr;
158143 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
158146 assert( pSubWInfo || pParse->nErr );
158150 pParse, pOrTab, &pSubWInfo->a[0], 0
158152 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
158154 /* This is the sub-WHERE clause body. First skip over
158155 ** duplicate rows from prior sub-WHERE clauses, and record the
158157 ** row will be skipped in subsequent sub-WHERE clauses.
158159 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
158160 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
158162 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
158168 int nPk = pPk->nKeyCol;
158175 int iCol = pPk->aiColumn[iPk];
158187 ** the temp table. And if iSet is -1, assume that there is no
158210 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
158213 /* The pSubWInfo->untestedTerms flag means that this OR term
158218 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
158220 /* If all of the OR-connected terms are optimized using the same
158226 ** uses an index, and this is either the first OR-connected term
158232 pSubLoop = pSubWInfo->a[0].pWLoop;
158233 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
158234 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
158235 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
158236 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
158238 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
158239 pCov = pSubLoop->u.btree.pIndex;
158244 pWInfo->bDeferredSeek = 1;
158255 assert( pLevel->pWLoop==pLoop );
158256 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
158257 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
158258 pLevel->u.pCoveringIdx = pCov;
158259 if( pCov ) pLevel->iIdxCur = iCovCur;
158261 pAndExpr->pLeft = 0;
158265 sqlite3VdbeGoto(v, pLevel->addrBrk);
158270 ** loop. The byte-code formatter will use that P2 value as a hint to
158272 ** See tag-20220407a in vdbe.c and shell.c */
158273 assert( pLevel->op==OP_Return );
158274 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
158276 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
158288 if( pTabItem->fg.isRecursive ){
158290 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
158291 pLevel->op = OP_Noop;
158294 pLevel->op = aStep[bRev];
158295 pLevel->p1 = iCur;
158296 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
158299 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
158304 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
158316 ** sub-queries.
158324 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
158327 testcase( pTerm->wtFlags & TERM_VIRTUAL );
158328 testcase( pTerm->wtFlags & TERM_CODED );
158329 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
158330 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
158331 testcase( pWInfo->untestedTerms==0
158332 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
158333 pWInfo->untestedTerms = 1;
158336 pE = pTerm->pExpr;
158338 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
158341 ** join processing. tag-20220513a */
158343 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
158347 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
158348 if( m & pLevel->notReady ){
158354 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
158358 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
158363 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
158372 u32 x = pLevel->iLikeRepCntr;
158383 pWC->nTerm-j, pTerm, iLoop));
158387 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
158392 pTerm->wtFlags |= TERM_CODED;
158405 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
158408 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
158409 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
158410 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
158411 if( pTerm->leftCursor!=iCur ) continue;
158412 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
158413 pE = pTerm->pExpr;
158417 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
158421 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
158422 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158423 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
158426 if( pAlt->wtFlags & (TERM_CODED) ) continue;
158427 if( (pAlt->eOperator & WO_IN)
158428 && ExprUseXSelect(pAlt->pExpr)
158429 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
158433 testcase( pAlt->eOperator & WO_EQ );
158434 testcase( pAlt->eOperator & WO_IS );
158435 testcase( pAlt->eOperator & WO_IN );
158437 sEAlt = *pAlt->pExpr;
158438 sEAlt.pLeft = pE->pLeft;
158440 pAlt->wtFlags |= TERM_CODED;
158446 if( pLevel->pRJ ){
158451 WhereRightJoin *pRJ = pLevel->pRJ;
158453 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
158458 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
158461 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
158466 nPk = pPk->nKeyCol;
158469 int iCol = pPk->aiColumn[iPk];
158473 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
158475 VdbeComment((v, "match against %s", pTab->zName));
158477 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
158478 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
158487 if( pLevel->iLeftJoin ){
158488 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
158489 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
158491 if( pLevel->pRJ==0 ){
158496 if( pLevel->pRJ ){
158499 ** be in-line with the rest of the code. But at the end, a separate
158503 WhereRightJoin *pRJ = pLevel->pRJ;
158504 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
158505 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
158506 assert( pParse->withinRJSubrtn < 255 );
158507 pParse->withinRJSubrtn++;
158512 ** appropriate WHERE clause constraint checks. tag-20220513a.
158515 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
158516 testcase( pTerm->wtFlags & TERM_VIRTUAL );
158517 testcase( pTerm->wtFlags & TERM_CODED );
158518 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
158519 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
158520 assert( pWInfo->untestedTerms );
158523 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
158524 assert( pTerm->pExpr );
158525 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
158526 pTerm->wtFlags |= TERM_CODED;
158532 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
158538 iLevel, (u64)pLevel->notReady);
158541 return pLevel->notReady;
158545 ** Generate the code for the loop that finds all non-matched terms
158553 Parse *pParse = pWInfo->pParse;
158554 Vdbe *v = pParse->pVdbe;
158555 WhereRightJoin *pRJ = pLevel->pRJ;
158557 WhereClause *pWC = &pWInfo->sWC;
158559 WhereLoop *pLoop = pLevel->pWLoop;
158560 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
158565 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
158566 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
158567 pRJ->regReturn);
158570 mAll |= pWInfo->a[k].pWLoop->maskSelf;
158571 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
158572 iIdxCur = pWInfo->a[k].iIdxCur;
158577 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
158578 mAll |= pLoop->maskSelf;
158579 for(k=0; k<pWC->nTerm; k++){
158580 WhereTerm *pTerm = &pWC->a[k];
158581 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
158582 && pTerm->eOperator!=WO_ROWVAL
158586 if( pTerm->prereqAll & ~mAll ) continue;
158587 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
158589 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
158596 assert( pParse->withinRJSubrtn < 100 );
158597 pParse->withinRJSubrtn++;
158601 int iCur = pLevel->iTabCur;
158602 int r = ++pParse->nMem;
158606 Table *pTab = pTabItem->pTab;
158608 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
158613 nPk = pPk->nKeyCol;
158614 pParse->nMem += nPk - 1;
158616 int iCol = pPk->aiColumn[iPk];
158620 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
158622 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
158625 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
158628 sqlite3ExprDelete(pParse->db, pSubWhere);
158630 assert( pParse->withinRJSubrtn>0 );
158631 pParse->withinRJSubrtn--;
158637 ** 2015-06-08
158664 sqlite3WhereClauseClear(&p->wc);
158672 sqlite3WhereClauseClear(&p->wc);
158679 ** The index in pWC->a[] of the new WhereTerm is returned on success.
158682 ** the db->mallocFailed flag so that higher-level functions can detect it.
158684 ** This routine will increase the size of the pWC->a[] array as necessary.
158693 ** the pWC->a[] array.
158699 if( pWC->nTerm>=pWC->nSlot ){
158700 WhereTerm *pOld = pWC->a;
158701 sqlite3 *db = pWC->pWInfo->pParse->db;
158702 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
158703 if( pWC->a==0 ){
158707 pWC->a = pOld;
158710 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
158711 pWC->nSlot = pWC->nSlot*2;
158713 pTerm = &pWC->a[idx = pWC->nTerm++];
158714 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
158716 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
158718 pTerm->truthProb = 1;
158720 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
158721 pTerm->wtFlags = wtFlags;
158722 pTerm->pWC = pWC;
158723 pTerm->iParent = -1;
158724 memset(&pTerm->eOperator, 0,
158725 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
158747 if( pExpr->pLeft->op==TK_VECTOR
158748 || pExpr->pRight->op==TK_VECTOR
158749 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
158750 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
158752 pExpr->flags ^= EP_Commuted;
158754 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
158755 if( pExpr->op>=TK_GT ){
158760 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
158761 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
158779 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
158780 c = (u16)(WO_EQ<<(op-TK_EQ));
158818 int cnt; /* Number of non-wildcard prefix characters */
158820 sqlite3 *db = pParse->db; /* Database connection */
158832 pList = pExpr->x.pList;
158833 pLeft = pList->a[1].pExpr;
158835 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
158836 op = pRight->op;
158837 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
158838 Vdbe *pReprepare = pParse->pReprepare;
158839 int iCol = pRight->iColumn;
158844 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
158845 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
158848 z = (u8*)pRight->u.zToken;
158860 ** with a wildcard and if (2) the non-wildcard prefix does not end with
158867 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && 255!=(u8)z[cnt-1] ){
158879 zNew = pPrefix->u.zToken;
158895 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
158896 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
158897 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
158898 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
158899 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
158901 if( pLeft->op!=TK_COLUMN
158904 && ALWAYS(pLeft->y.pTab)
158905 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
158911 if( iTo==1 && zNew[0]=='-' ){
158914 zNew[iTo-1]++;
158916 zNew[iTo-1]--;
158931 Vdbe *v = pParse->pVdbe;
158932 sqlite3VdbeSetVarmask(v, pRight->iColumn);
158934 if( *pisComplete && pRight->u.zToken[1] ){
158943 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
158965 ** ----------------------- ---------------------------------
158993 if( pExpr->op==TK_FUNCTION ){
159008 pList = pExpr->x.pList;
159009 if( pList==0 || pList->nExpr!=2 ){
159013 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
159015 ** the left-hand side operand in their in-fix form.
159020 pCol = pList->a[1].pExpr;
159021 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
159025 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
159027 *ppRight = pList->a[0].pExpr;
159040 ** Historically, xFindFunction expected to see lower-case function
159044 pCol = pList->a[0].pExpr;
159045 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
159046 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
159052 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
159054 assert( pVtab->pModule!=0 );
159056 pMod = (sqlite3_module *)pVtab->pModule;
159057 if( pMod->xFindFunction!=0 ){
159058 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
159061 *ppRight = pList->a[1].pExpr;
159067 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
159069 Expr *pLeft = pExpr->pLeft;
159070 Expr *pRight = pExpr->pRight;
159071 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
159075 assert( pRight==0 || pRight->op!=TK_COLUMN
159076 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
159083 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
159084 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
159085 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
159098 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
159099 pDerived->w.iJoin = pBase->w.iJoin;
159107 pWC->a[iChild].iParent = iParent;
159108 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
159109 pWC->a[iParent].nChild++;
159113 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
159118 if( pTerm->eOperator!=WO_AND ){
159121 if( N<pTerm->u.pAndInfo->wc.nTerm ){
159122 return &pTerm->u.pAndInfo->wc.a[N];
159129 ** two subterms are in disjunction - they are OR-ed together.
159139 ** x<y OR x=y --> x<=y
159140 ** x=y OR x=y --> x=y
159141 ** x<=y OR x<y --> x<=y
159145 ** x<y OR x>y --> x!=y
159153 u16 eOp = pOne->eOperator | pTwo->eOperator;
159159 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
159160 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
159161 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
159164 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
159165 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
159166 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
159167 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
159169 if( (eOp & (eOp-1))!=0 ){
159177 db = pWC->pWInfo->pParse->db;
159178 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
159180 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
159181 pNew->op = op;
159188 ** Analyze a term that consists of two or more OR-connected
159201 ** The term being analyzed must have two or more of OR-connected subterms.
159202 ** A single subterm might be a set of AND-connected sub-subterms.
159240 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
159277 int idxTerm /* Index of the OR-term to be analyzed */
159279 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
159280 Parse *pParse = pWInfo->pParse; /* Parser context */
159281 sqlite3 *db = pParse->db; /* Database connection */
159282 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
159283 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
159286 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
159296 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
159297 assert( pExpr->op==TK_OR );
159298 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
159300 pTerm->wtFlags |= TERM_ORINFO;
159301 pOrWc = &pOrInfo->wc;
159302 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
159306 if( db->mallocFailed ) return;
159307 assert( pOrWc->nTerm>=2 );
159314 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
159315 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
159317 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
159325 pOrTerm->u.pAndInfo = pAndInfo;
159326 pOrTerm->wtFlags |= TERM_ANDINFO;
159327 pOrTerm->eOperator = WO_AND;
159328 pOrTerm->leftCursor = -1;
159329 pAndWC = &pAndInfo->wc;
159330 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
159331 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
159332 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
159334 pAndWC->pOuter = pWC;
159335 if( !db->mallocFailed ){
159336 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
159337 assert( pAndTerm->pExpr );
159338 if( allowedOp(pAndTerm->pExpr->op)
159339 || pAndTerm->eOperator==WO_AUX
159341 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
159347 }else if( pOrTerm->wtFlags & TERM_COPIED ){
159352 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
159353 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
159354 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
159355 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
159358 if( (pOrTerm->eOperator & WO_EQ)==0 ){
159370 pOrInfo->indexable = indexable;
159371 pTerm->eOperator = WO_OR;
159372 pTerm->leftCursor = -1;
159374 pWC->hasOr = 1;
159377 /* For a two-way OR, attempt to implementation case 2.
159379 if( indexable && pOrWc->nTerm==2 ){
159382 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
159385 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
159396 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
159399 ** something other than == on a column in the single table. The 1-bit
159403 ** sure the same column is used on all terms. The 2-bit case is when
159414 int iColumn = -1; /* Column index on lhs of IN operator */
159415 int iCursor = -1; /* Table cursor common to all terms */
159426 pOrTerm = pOrWc->a;
159427 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
159428 assert( pOrTerm->eOperator & WO_EQ );
159429 pOrTerm->wtFlags &= ~TERM_OK;
159430 if( pOrTerm->leftCursor==iCursor ){
159431 /* This is the 2-bit case and we are on the second iteration and
159436 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
159437 pOrTerm->leftCursor))==0 ){
159442 testcase( pOrTerm->wtFlags & TERM_COPIED );
159443 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
159444 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
159447 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
159448 iColumn = pOrTerm->u.x.leftColumn;
159449 iCursor = pOrTerm->leftCursor;
159450 pLeft = pOrTerm->pExpr->pLeft;
159458 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
159466 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
159467 assert( pOrTerm->eOperator & WO_EQ );
159468 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
159469 if( pOrTerm->leftCursor!=iCursor ){
159470 pOrTerm->wtFlags &= ~TERM_OK;
159471 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
159472 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
159477 /* If the right-hand side is also a column, then the affinities
159481 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
159482 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
159486 pOrTerm->wtFlags |= TERM_OK;
159502 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
159503 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
159504 assert( pOrTerm->eOperator & WO_EQ );
159505 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
159506 assert( pOrTerm->leftCursor==iCursor );
159507 assert( pOrTerm->u.x.leftColumn==iColumn );
159508 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
159509 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
159510 pLeft = pOrTerm->pExpr->pLeft;
159519 pNew->x.pList = pList;
159523 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
159551 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
159552 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
159554 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
159555 aff2 = sqlite3ExprAffinity(pExpr->pRight);
159563 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
159574 SrcList *pSrc = pS->pSrc;
159575 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
159576 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
159577 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
159578 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
159579 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
159582 for(i=0; i<pSrc->nSrc; i++){
159583 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
159584 if( pSrc->a[i].fg.isUsing==0 ){
159585 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
159587 if( pSrc->a[i].fg.isTabFunc ){
159588 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
159592 pS = pS->pPrior;
159603 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
159614 int j /* Start looking with the j-th pFrom entry */
159620 iCur = pFrom->a[j].iCursor;
159621 for(pIdx=pFrom->a[j].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159622 if( pIdx->aColExpr==0 ) continue;
159623 for(i=0; i<pIdx->nKeyCol; i++){
159624 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
159625 assert( pIdx->bHasExpr );
159626 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
159627 && pExpr->op!=TK_STRING
159635 }while( ++j < pFrom->nSrc );
159652 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
159654 pExpr = pExpr->x.pList->a[0].pExpr;
159657 if( pExpr->op==TK_COLUMN ){
159658 aiCurCol[0] = pExpr->iTable;
159659 aiCurCol[1] = pExpr->iColumn;
159663 for(i=0; i<pFrom->nSrc; i++){
159665 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159666 if( pIdx->aColExpr ){
159698 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
159702 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
159708 int op; /* Top-level operator. pExpr->op */
159709 Parse *pParse = pWInfo->pParse; /* Parsing context */
159710 sqlite3 *db = pParse->db; /* Database connection */
159714 if( db->mallocFailed ){
159717 assert( pWC->nTerm > idxTerm );
159718 pTerm = &pWC->a[idxTerm];
159719 pMaskSet = &pWInfo->sMaskSet;
159720 pExpr = pTerm->pExpr;
159722 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
159723 pMaskSet->bVarSelect = 0;
159724 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
159725 op = pExpr->op;
159727 assert( pExpr->pRight==0 );
159730 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
159732 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
159734 prereqAll = prereqLeft | pTerm->prereqRight;
159736 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
159737 if( pExpr->pLeft==0
159739 || pExpr->x.pList!=0
159743 prereqAll = prereqLeft | pTerm->prereqRight;
159746 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
159757 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
159760 extraRight = x-1; /* ON clause terms may not be used with an index
159773 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
159780 pTerm->prereqAll = prereqAll;
159781 pTerm->leftCursor = -1;
159782 pTerm->iParent = -1;
159783 pTerm->eOperator = 0;
159786 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
159787 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
159788 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
159790 if( pTerm->u.x.iField>0 ){
159792 assert( pLeft->op==TK_VECTOR );
159794 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
159798 pTerm->leftCursor = aiCurCol[0];
159799 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
159800 pTerm->u.x.leftColumn = aiCurCol[1];
159801 pTerm->eOperator = operatorMask(op) & opMask;
159803 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
159810 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
159811 assert( pTerm->u.x.iField==0 );
159812 if( pTerm->leftCursor>=0 ){
159815 if( db->mallocFailed ){
159821 pNew = &pWC->a[idxNew];
159823 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
159824 pTerm = &pWC->a[idxTerm];
159825 pTerm->wtFlags |= TERM_COPIED;
159828 pTerm->eOperator |= WO_EQUIV;
159835 pNew->wtFlags |= exprCommute(pParse, pDup);
159836 pNew->leftCursor = aiCurCol[0];
159837 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
159838 pNew->u.x.leftColumn = aiCurCol[1];
159840 pNew->prereqRight = prereqLeft | extraRight;
159841 pNew->prereqAll = prereqAll;
159842 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
159849 pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */
159850 pExpr->u.zToken = "false";
159852 pTerm->prereqAll = 0;
159853 pTerm->eOperator = 0;
159873 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
159878 pList = pExpr->x.pList;
159880 assert( pList->nExpr==2 );
159885 sqlite3ExprDup(db, pExpr->pLeft, 0),
159886 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
159891 pTerm = &pWC->a[idxTerm];
159901 else if( pExpr->op==TK_OR ){
159902 assert( pWC->op==TK_AND );
159904 pTerm = &pWC->a[idxTerm];
159913 else if( pExpr->op==TK_NOTNULL ){
159914 if( pExpr->pLeft->op==TK_COLUMN
159915 && pExpr->pLeft->iColumn>=0
159919 Expr *pLeft = pExpr->pLeft;
159930 pNewTerm = &pWC->a[idxNew];
159931 pNewTerm->prereqRight = 0;
159932 pNewTerm->leftCursor = pLeft->iTable;
159933 pNewTerm->u.x.leftColumn = pLeft->iColumn;
159934 pNewTerm->eOperator = WO_GT;
159936 pTerm = &pWC->a[idxTerm];
159937 pTerm->wtFlags |= TERM_COPIED;
159938 pNewTerm->prereqAll = pTerm->prereqAll;
159954 ** for LIKE) then the lower-bound is made all uppercase and the upper-
159958 else if( pExpr->op==TK_FUNCTION
159959 && pWC->op==TK_AND
159963 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
159972 pLeft = pExpr->x.pList->a[1].pExpr;
159978 /* Convert the lower bound to upper-case and the upper bound to
159979 ** lower-case (upper-case is less than lower-case in ASCII) so that
159982 if( noCase && !pParse->db->mallocFailed ){
159985 pTerm->wtFlags |= TERM_LIKE;
159986 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
159987 pStr1->u.zToken[i] = sqlite3Toupper(c);
159988 pStr2->u.zToken[i] = sqlite3Tolower(c);
159992 if( !db->mallocFailed ){
159994 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
160003 if( c=='A'-1 ) isComplete = 0;
160025 pTerm = &pWC->a[idxTerm];
160033 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
160034 ** new terms for each component comparison - "a = ?" and "b = ?". The
160039 ** is not a sub-select.
160041 ** tag-20220128a
160043 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
160044 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
160045 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
160046 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
160047 || (pExpr->pRight->flags & EP_xIsSelect)==0)
160048 && pWC->op==TK_AND
160054 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
160055 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
160057 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
160062 pTerm = &pWC->a[idxTerm];
160063 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
160064 pTerm->eOperator = WO_ROWVAL;
160067 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
160076 else if( pExpr->op==TK_IN
160077 && pTerm->u.x.iField==0
160078 && pExpr->pLeft->op==TK_VECTOR
160080 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values))
160082 && pExpr->x.pSelect->pWin==0
160084 && pWC->op==TK_AND
160087 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
160090 pWC->a[idxNew].u.x.iField = i+1;
160106 else if( pWC->op==TK_AND ){
160109 while( res-- > 0 ){
160122 pNewExpr->w.iJoin = pExpr->w.iJoin;
160126 pNewTerm = &pWC->a[idxNew];
160127 pNewTerm->prereqRight = prereqExpr;
160128 pNewTerm->leftCursor = pLeft->iTable;
160129 pNewTerm->u.x.leftColumn = pLeft->iColumn;
160130 pNewTerm->eOperator = WO_AUX;
160131 pNewTerm->eMatchOp = eOp2;
160133 pTerm = &pWC->a[idxTerm];
160134 pTerm->wtFlags |= TERM_COPIED;
160135 pNewTerm->prereqAll = pTerm->prereqAll;
160145 testcase( pTerm!=&pWC->a[idxTerm] );
160146 pTerm = &pWC->a[idxTerm];
160147 pTerm->prereqRight |= extraRight;
160174 pWC->op = op;
160177 if( pE2->op!=op ){
160180 sqlite3WhereSplit(pWC, pE2->pLeft, op);
160181 sqlite3WhereSplit(pWC, pE2->pRight, op);
160188 ** where-clause passed as the first argument. The value for the term
160203 Parse *pParse = pWC->pWInfo->pParse;
160204 sqlite3 *db = pParse->db;
160212 pVal->u.iValue = iVal;
160217 pVal->iTable = iReg;
160224 pTerm = &pWC->a[idx];
160225 pTerm->leftCursor = iCsr;
160226 pTerm->eOperator = WO_AUX;
160227 pTerm->eMatchOp = eMatchOp;
160250 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
160251 if( p->pGroupBy==0
160252 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
160253 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
160255 ExprList *pOrderBy = p->pOrderBy;
160256 int iCsr = p->pSrc->a[0].iCursor;
160260 for(ii=0; ii<pWC->nTerm; ii++){
160261 if( pWC->a[ii].wtFlags & TERM_CODED ){
160263 ** other, subsequent terms. It can be ignored. See tag-20220128a */
160264 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
160265 assert( pWC->a[ii].eOperator==WO_ROWVAL );
160268 if( pWC->a[ii].nChild ){
160270 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
160275 if( pWC->a[ii].leftCursor!=iCsr ) return;
160280 for(ii=0; ii<pOrderBy->nExpr; ii++){
160281 Expr *pExpr = pOrderBy->a[ii].pExpr;
160282 if( pExpr->op!=TK_COLUMN ) return;
160283 if( pExpr->iTable!=iCsr ) return;
160284 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
160288 /* All conditions are met. Add the terms to the where-clause object. */
160289 assert( p->pLimit->op==TK_LIMIT );
160290 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
160292 if( p->iOffset>0 ){
160293 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
160306 pWC->pWInfo = pWInfo;
160307 pWC->hasOr = 0;
160308 pWC->pOuter = 0;
160309 pWC->nTerm = 0;
160310 pWC->nBase = 0;
160311 pWC->nSlot = ArraySize(pWC->aStatic);
160312 pWC->a = pWC->aStatic;
160321 sqlite3 *db = pWC->pWInfo->pParse->db;
160322 assert( pWC->nTerm>=pWC->nBase );
160323 if( pWC->nTerm>0 ){
160324 WhereTerm *a = pWC->a;
160325 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
160328 /* Verify that every term past pWC->nBase is virtual */
160329 for(i=pWC->nBase; i<pWC->nTerm; i++){
160330 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
160334 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
160335 if( a->wtFlags & TERM_DYNAMIC ){
160336 sqlite3ExprDelete(db, a->pExpr);
160338 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
160339 if( a->wtFlags & TERM_ORINFO ){
160340 assert( (a->wtFlags & TERM_ANDINFO)==0 );
160341 whereOrInfoDelete(db, a->u.pOrInfo);
160343 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
160344 whereAndInfoDelete(db, a->u.pAndInfo);
160359 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
160364 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
160369 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
160375 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
160382 ** "no-inline" function to avoid the stack push overhead in the
160390 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
160391 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
160392 if( p->pRight ){
160393 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
160394 assert( p->x.pList==0 );
160396 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
160397 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
160398 }else if( p->x.pList ){
160399 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
160402 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
160403 assert( p->y.pWin!=0 );
160404 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
160405 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
160406 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
160412 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
160413 return sqlite3WhereGetMask(pMaskSet, p->iTable);
160415 assert( p->op!=TK_IF_NULL_ROW );
160427 for(i=0; i<pList->nExpr; i++){
160428 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
160448 for(i=pWC->nTerm-1; i>=0; i--){
160454 ** For table-valued-functions, transform the function arguments into
160470 if( pItem->fg.isTabFunc==0 ) return;
160471 pTab = pItem->pTab;
160473 pArgs = pItem->u1.pFuncArg;
160475 for(j=k=0; j<pArgs->nExpr; j++){
160478 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
160479 if( k>=pTab->nCol ){
160480 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
160481 pTab->zName, j);
160484 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
160486 pColRef->iTable = pItem->iCursor;
160487 pColRef->iColumn = k++;
160489 pColRef->y.pTab = pTab;
160490 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
160492 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
160494 if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){
160495 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
160496 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
160499 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
160502 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
160558 return pWInfo->nRowOut;
160566 return pWInfo->eDistinct;
160578 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
160582 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
160584 ** inner-most loop did not fit within the sorter, then we can skip all
160587 ** loop - the loop immediately outside the inner-most.
160594 ** be the continuation for the second-inner-most loop. If the ORDER BY
160596 ** be the continuation for the inner-most loop.
160599 ** inner-most loop, in the sense that a correct answer will result.
160606 if( !pWInfo->bOrderedInnerLoop ){
160608 ** continuation of the inner-most loop. */
160609 return pWInfo->iContinue;
160611 pInner = &pWInfo->a[pWInfo->nLevel-1];
160612 assert( pInner->addrNxt!=0 );
160613 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
160618 ** the aggregate-step call to min() or max(), check to see if any
160630 if( !pWInfo->bOrderedInnerLoop ) return;
160631 if( pWInfo->nOBSat==0 ) return;
160632 for(i=pWInfo->nLevel-1; i>=0; i--){
160633 pInner = &pWInfo->a[i];
160634 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
160635 sqlite3VdbeGoto(v, pInner->addrNxt);
160639 sqlite3VdbeGoto(v, pWInfo->iBreak);
160647 assert( pWInfo->iContinue!=0 );
160648 return pWInfo->iContinue;
160656 return pWInfo->iBreak;
160663 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
160670 ** Either value may be -1, indicating that cursor is not used.
160673 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
160677 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
160679 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
160681 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
160685 return pWInfo->eOnePass;
160693 return pWInfo->bDeferredSeek;
160700 pDest->n = pSrc->n;
160701 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
160714 LogEst rRun, /* Run-cost of the new entry */
160719 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
160720 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
160723 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
160727 if( pSet->n<N_OR_COST ){
160728 p = &pSet->a[pSet->n++];
160729 p->nOut = nOut;
160731 p = pSet->a;
160732 for(i=1; i<pSet->n; i++){
160733 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
160735 if( p->rRun<=rRun ) return 0;
160738 p->prereq = prereq;
160739 p->rRun = rRun;
160740 if( p->nOut>nOut ) p->nOut = nOut;
160750 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
160751 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
160752 assert( iCursor>=-1 );
160753 if( pMaskSet->ix[0]==iCursor ){
160756 for(i=1; i<pMaskSet->n; i++){
160757 if( pMaskSet->ix[i]==iCursor ){
160768 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
160770 pBlock->pNext = pWInfo->pMemToFree;
160771 pBlock->sz = nByte;
160772 pWInfo->pMemToFree = pBlock;
160781 pOldBlk--;
160782 assert( pOldBlk->sz<nByte );
160783 memcpy(pNew, pOld, pOldBlk->sz);
160793 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
160797 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
160798 pMaskSet->ix[pMaskSet->n++] = iCursor;
160802 ** If the right-hand branch of the expression is a TK_COLUMN, then return
160803 ** a pointer to the right-hand branch. Otherwise, return NULL.
160806 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
160807 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
160820 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
160822 WhereClause *pWC; /* Shorthand for pScan->pWC */
160824 int k = pScan->k; /* Where to start scanning */
160826 assert( pScan->iEquiv<=pScan->nEquiv );
160827 pWC = pScan->pWC;
160829 iColumn = pScan->aiColumn[pScan->iEquiv-1];
160830 iCur = pScan->aiCur[pScan->iEquiv-1];
160834 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
160835 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
160836 if( pTerm->leftCursor==iCur
160837 && pTerm->u.x.leftColumn==iColumn
160839 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
160840 pScan->pIdxExpr,iCur)==0)
160841 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
160843 if( (pTerm->eOperator & WO_EQUIV)!=0
160844 && pScan->nEquiv<ArraySize(pScan->aiCur)
160845 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
160848 for(j=0; j<pScan->nEquiv; j++){
160849 if( pScan->aiCur[j]==pX->iTable
160850 && pScan->aiColumn[j]==pX->iColumn ){
160854 if( j==pScan->nEquiv ){
160855 pScan->aiCur[j] = pX->iTable;
160856 pScan->aiColumn[j] = pX->iColumn;
160857 pScan->nEquiv++;
160860 if( (pTerm->eOperator & pScan->opMask)!=0 ){
160862 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
160864 Parse *pParse = pWC->pWInfo->pParse;
160865 pX = pTerm->pExpr;
160866 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
160869 assert(pX->pLeft);
160871 if( pColl==0 ) pColl = pParse->db->pDfltColl;
160872 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
160876 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
160877 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
160878 && pX->op==TK_COLUMN
160879 && pX->iTable==pScan->aiCur[0]
160880 && pX->iColumn==pScan->aiColumn[0]
160882 testcase( pTerm->eOperator & WO_IS );
160885 pScan->pWC = pWC;
160886 pScan->k = k+1;
160890 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
160891 pTerm, pScan->nEquiv);
160892 for(ii=0; ii<pScan->nEquiv; ii++){
160894 pScan->aiCur[ii], pScan->aiColumn[ii]);
160903 pWC = pWC->pOuter;
160906 if( pScan->iEquiv>=pScan->nEquiv ) break;
160907 pWC = pScan->pOrigWC;
160909 pScan->iEquiv++;
160916 ** It is factored out into a separate tail-recursion subroutine so that
160917 ** the normal whereScanInit() routine, which is a high-runner, does not
160921 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
160952 pScan->pOrigWC = pWC;
160953 pScan->pWC = pWC;
160954 pScan->pIdxExpr = 0;
160955 pScan->idxaff = 0;
160956 pScan->zCollName = 0;
160957 pScan->opMask = opMask;
160958 pScan->k = 0;
160959 pScan->aiCur[0] = iCur;
160960 pScan->nEquiv = 1;
160961 pScan->iEquiv = 1;
160964 iColumn = pIdx->aiColumn[j];
160965 if( iColumn==pIdx->pTable->iPKey ){
160968 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
160969 pScan->zCollName = pIdx->azColl[j];
160971 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
160972 pScan->zCollName = pIdx->azColl[j];
160973 pScan->aiColumn[0] = XN_EXPR;
160979 pScan->aiColumn[0] = iColumn;
160990 ** Search for terms matching the iColumn-th column of pIdx
160991 ** rather than the iColumn-th column of table iCur.
160995 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
161002 ** then try for the one with no dependencies on <expr> - in other words where
161005 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
161023 if( (p->prereqRight & notReady)==0 ){
161024 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
161025 testcase( p->eOperator & WO_IS );
161036 ** This function searches pList for an entry that matches the iCol-th column
161039 ** If such an expression is found, its index in pList->a[] is returned. If
161040 ** no expression is found, -1 is returned.
161050 const char *zColl = pIdx->azColl[iCol];
161052 for(i=0; i<pList->nExpr; i++){
161053 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
161055 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
161056 && p->iColumn==pIdx->aiColumn[iCol]
161057 && p->iTable==iBase
161059 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
161060 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
161066 return -1;
161070 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
161075 assert( iCol>=0 && iCol<pIdx->nColumn );
161076 j = pIdx->aiColumn[iCol];
161078 return pIdx->pTable->aCol[j].notNull;
161079 }else if( j==(-1) ){
161082 assert( j==(-2) );
161089 ** Return true if the DISTINCT expression-list passed as the third argument
161093 ** DISTINCT list are collectively unique and individually non-null.
161106 /* If there is more than one table or sub-select in the FROM clause of
161109 if( pTabList->nSrc!=1 ) return 0;
161110 iBase = pTabList->a[0].iCursor;
161111 pTab = pTabList->a[0].pTab;
161114 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
161115 ** current SELECT is a correlated sub-query.
161117 for(i=0; i<pDistinct->nExpr; i++){
161118 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
161120 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
161121 if( p->iTable==iBase && p->iColumn<0 ) return 1;
161132 ** comparison and select-list expressions must match those of the index.
161137 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
161139 if( pIdx->pPartIdxWhere ) continue;
161140 for(i=0; i<pIdx->nKeyCol; i++){
161146 if( i==pIdx->nKeyCol ){
161160 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
161167 ** opcodes into OP_Copy when the table is being accessed via co-routine
161180 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
161182 Vdbe *v = pParse->pVdbe;
161185 if( pParse->db->mallocFailed ) return;
161187 if( pOp->p1!=iTabCur ) continue;
161188 if( pOp->opcode==OP_Column ){
161189 pOp->opcode = OP_Copy;
161190 pOp->p1 = pOp->p2 + iRegister;
161191 pOp->p2 = pOp->p3;
161192 pOp->p3 = 0;
161193 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
161194 }else if( pOp->opcode==OP_Rowid ){
161195 pOp->opcode = OP_Sequence;
161196 pOp->p1 = iAutoidxCur;
161199 pOp->opcode = OP_Null;
161200 pOp->p3 = 0;
161211 ** are no-ops.
161217 for(i=0; i<p->nConstraint; i++){
161221 p->aConstraint[i].iColumn,
161222 p->aConstraint[i].iTermOffset,
161223 p->aConstraint[i].op,
161224 p->aConstraint[i].usable,
161227 for(i=0; i<p->nOrderBy; i++){
161230 p->aOrderBy[i].iColumn,
161231 p->aOrderBy[i].desc);
161237 for(i=0; i<p->nConstraint; i++){
161240 p->aConstraintUsage[i].argvIndex,
161241 p->aConstraintUsage[i].omit);
161243 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
161244 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
161245 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
161246 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
161247 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
161265 ** not-NULL condition on the left table of the RIGHT JOIN.
161271 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
161272 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
161273 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
161274 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
161275 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
161276 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
161277 || pTerm->pExpr->w.iJoin != pSrc->iCursor
161281 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
161282 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
161303 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
161304 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
161305 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
161306 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
161311 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
161312 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161313 if( pTerm->u.x.leftColumn<0 ) return 0;
161314 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
161315 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
161316 testcase( pTerm->pExpr->op==TK_IS );
161342 if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){
161343 Table *pTab = pIdx->pTable;
161347 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
161348 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
161349 assert( pIdx->nColumn>1 );
161350 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID );
161351 for(ii=0; ii<(pIdx->nColumn-1); ii++){
161353 int iCol = pIdx->aiColumn[ii];
161355 zName = pTab->aCol[iCol].zCnName;
161361 sqlite3OomFault(pParse->db);
161387 WhereTerm *pWCEnd; /* End of pWC->a[] */
161396 int mxBitCol; /* Maximum column in pSrc->colUsed */
161416 v = pParse->pVdbe;
161423 pTabList = pWC->pWInfo->pTabList;
161424 pSrc = &pTabList->a[pLevel->iFrom];
161425 pTable = pSrc->pTab;
161426 pWCEnd = &pWC->a[pWC->nTerm];
161427 pLoop = pLevel->pWLoop;
161429 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
161430 Expr *pExpr = pTerm->pExpr;
161434 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
161435 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom)
161438 sqlite3ExprDup(pParse->db, pExpr, 0));
161443 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161444 iCol = pTerm->u.x.leftColumn;
161445 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
161447 testcase( iCol==BMS-1 );
161450 "automatic index on %s(%s)", pTable->zName,
161451 pTable->aCol[iCol].zCnName);
161455 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
161458 pLoop->aLTerm[nKeyCol++] = pTerm;
161463 assert( nKeyCol>0 || pParse->db->mallocFailed );
161464 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
161465 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
161479 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
161481 mxBitCol = MIN(BMS-1,pTable->nCol);
161482 testcase( pTable->nCol==BMS-1 );
161483 testcase( pTable->nCol==BMS-2 );
161487 if( pSrc->colUsed & MASKBIT(BMS-1) ){
161488 nKeyCol += pTable->nCol - BMS + 1;
161492 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
161494 pLoop->u.btree.pIndex = pIdx;
161495 pIdx->zName = "auto-index";
161496 pIdx->pTable = pTable;
161499 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
161503 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161504 iCol = pTerm->u.x.leftColumn;
161505 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
161506 testcase( iCol==BMS-1 );
161509 Expr *pX = pTerm->pExpr;
161511 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
161513 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
161514 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
161516 if( ALWAYS(pX->pLeft!=0)
161517 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT
161529 assert( (u32)n==pLoop->u.btree.nEq );
161535 pIdx->aiColumn[n] = i;
161536 pIdx->azColl[n] = sqlite3StrBINARY;
161540 if( pSrc->colUsed & MASKBIT(BMS-1) ){
161541 for(i=BMS-1; i<pTable->nCol; i++){
161542 pIdx->aiColumn[n] = i;
161543 pIdx->azColl[n] = sqlite3StrBINARY;
161548 pIdx->aiColumn[n] = XN_ROWID;
161549 pIdx->azColl[n] = sqlite3StrBINARY;
161553 assert( pLevel->iIdxCur>=0 );
161554 pLevel->iIdxCur = pParse->nTab++;
161555 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
161557 VdbeComment((v, "for %s", pTable->zName));
161558 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){
161559 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
161560 pLevel->regFilter = ++pParse->nMem;
161561 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
161565 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] );
161566 if( pSrc->fg.viaCoroutine ){
161567 int regYield = pSrc->regReturn;
161569 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSrc->addrFillSub);
161572 VdbeComment((v, "next row of %s", pSrc->pTab->zName));
161574 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
161579 pLoop->wsFlags |= WHERE_PARTIALIDX;
161583 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
161585 if( pLevel->regFilter ){
161586 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
161587 regBase, pLoop->u.btree.nEq);
161590 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
161593 if( pSrc->fg.viaCoroutine ){
161595 testcase( pParse->db->mallocFailed );
161596 assert( pLevel->iIdxCur>0 );
161597 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
161598 pSrc->regResult, pLevel->iIdxCur);
161600 pSrc->fg.viaCoroutine = 0;
161602 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
161610 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
161613 sqlite3ExprDelete(pParse->db, pPartial);
161630 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
161639 int iLevel, /* Index in pWInfo->a[] that is pLevel */
161648 Parse *pParse = pWInfo->pParse; /* Parsing context */
161649 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
161650 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
161655 saved_pIdxEpr = pParse->pIdxEpr;
161656 saved_pIdxPartExpr = pParse->pIdxPartExpr;
161657 pParse->pIdxEpr = 0;
161658 pParse->pIdxPartExpr = 0;
161662 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
161663 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 );
161674 iCur = pLevel->iTabCur;
161675 pLevel->regFilter = ++pParse->nMem;
161678 ** to zero-filled blob of at least 80K bits, but maybe more if the
161680 ** measure the size of the table at run-time using OP_Count with
161685 pTabList = pWInfo->pTabList;
161686 iSrc = pLevel->iFrom;
161687 pItem = &pTabList->a[iSrc];
161689 pTab = pItem->pTab;
161691 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
161697 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
161700 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
161701 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
161702 Expr *pExpr = pTerm->pExpr;
161703 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
161706 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
161709 if( pLoop->wsFlags & WHERE_IPK ){
161712 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
161715 Index *pIdx = pLoop->u.btree.pIndex;
161716 int n = pLoop->u.btree.nEq;
161720 assert( pIdx->pTable==pItem->pTab );
161723 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
161727 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
161730 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
161731 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
161732 while( ++iLevel < pWInfo->nLevel ){
161734 pLevel = &pWInfo->a[iLevel];
161735 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
161736 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
161737 pLoop = pLevel->pWLoop;
161739 if( pLoop->prereq & notReady ) continue;
161740 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
161743 /* This is a candidate for bloom-filter pull-down (early evaluation).
161750 }while( iLevel < pWInfo->nLevel );
161752 pParse->pIdxEpr = saved_pIdxEpr;
161753 pParse->pIdxPartExpr = saved_pIdxPartExpr;
161772 Parse *pParse = pWInfo->pParse;
161783 ExprList *pOrderBy = pWInfo->pOrderBy;
161786 pTab = pSrc->pTab;
161794 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
161795 pTerm->wtFlags &= ~TERM_OK;
161796 if( pTerm->leftCursor != pSrc->iCursor ) continue;
161797 if( pTerm->prereqRight & mUnusable ) continue;
161798 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
161799 testcase( pTerm->eOperator & WO_IN );
161800 testcase( pTerm->eOperator & WO_ISNULL );
161801 testcase( pTerm->eOperator & WO_IS );
161802 testcase( pTerm->eOperator & WO_ALL );
161803 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
161804 if( pTerm->wtFlags & TERM_VNULL ) continue;
161806 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
161807 assert( pTerm->u.x.leftColumn>=XN_ROWID );
161808 assert( pTerm->u.x.leftColumn<pTab->nCol );
161809 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
161815 pTerm->wtFlags |= TERM_OK;
161824 int n = pOrderBy->nExpr;
161826 Expr *pExpr = pOrderBy->a[i].pExpr;
161835 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
161837 /* First case - a direct column references without a COLLATE operator */
161838 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
161839 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
161843 /* 2nd case - a column reference with a COLLATE operator. Only match
161845 if( pExpr->op==TK_COLLATE
161846 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
161847 && pE2->iTable==pSrc->iCursor
161851 assert( pExpr->u.zToken!=0 );
161852 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
161853 pExpr->iColumn = pE2->iColumn;
161854 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
161855 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
161857 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
161865 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
161866 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
161867 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
161875 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
161884 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
161887 pIdxInfo->aConstraint = pIdxCons;
161888 pIdxInfo->aOrderBy = pIdxOrderBy;
161889 pIdxInfo->aConstraintUsage = pUsage;
161890 pHidden->pWC = pWC;
161891 pHidden->pParse = pParse;
161892 pHidden->eDistinct = eDistinct;
161893 pHidden->mIn = 0;
161894 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
161896 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
161897 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
161899 op = pTerm->eOperator & WO_ALL;
161901 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
161902 pHidden->mIn |= SMASKBIT32(j);
161907 pIdxCons[j].op = pTerm->eMatchOp;
161924 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
161927 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
161939 pIdxInfo->nConstraint = j;
161941 Expr *pExpr = pOrderBy->a[i].pExpr;
161943 assert( pExpr->op==TK_COLUMN
161944 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
161945 && pExpr->iColumn==pExpr->pLeft->iColumn) );
161946 pIdxOrderBy[j].iColumn = pExpr->iColumn;
161947 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
161950 pIdxInfo->nOrderBy = j;
161965 assert( pHidden->pParse!=0 );
161966 assert( pHidden->pParse->db==db );
161967 for(i=0; i<pIdxInfo->nConstraint; i++){
161968 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
161969 pHidden->aRhs[i] = 0;
161987 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
161991 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
161995 pParse->db->nSchemaLock++;
161996 rc = pVtab->pModule->xBestIndex(pVtab, p);
161997 pParse->db->nSchemaLock--;
162002 sqlite3OomFault(pParse->db);
162003 }else if( !pVtab->zErrMsg ){
162006 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
162009 if( pTab->u.vtab.p->bAllSchemas ){
162012 sqlite3_free(pVtab->zErrMsg);
162013 pVtab->zErrMsg = 0;
162028 ** into the aSample[] array - it is an index into a virtual set of samples
162039 IndexSample *aSample = pIdx->aSample;
162053 assert( pIdx->nSample>0 );
162054 assert( pRec->nField>0 );
162101 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
162102 nField = pIdx->nKeyCol;
162104 nField = pIdx->nColumn;
162106 nField = MIN(pRec->nField, nField);
162108 iSample = pIdx->nSample * nField;
162120 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
162126 pRec->nField = n;
162129 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
162132 iLower = aSample[iSamp].anLt[n-1];
162134 res = -1;
162137 iCol = n-1;
162146 if( pParse->db->mallocFailed==0 ){
162149 assert( i<pIdx->nSample );
162150 assert( iCol==nField-1 );
162151 pRec->nField = nField;
162153 || pParse->db->mallocFailed
162156 /* Unless i==pIdx->nSample, indicating that pRec is larger than
162159 assert( i<=pIdx->nSample && i>=0 );
162160 pRec->nField = iCol+1;
162161 assert( i==pIdx->nSample
162163 || pParse->db->mallocFailed );
162168 ** If (i>0), then pRec must also be greater than sample (i-1). */
162170 pRec->nField = iCol;
162172 || pParse->db->mallocFailed || CORRUPT_DB );
162175 pRec->nField = nField;
162176 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
162177 || pParse->db->mallocFailed || CORRUPT_DB );
162185 assert( iCol==nField-1 );
162190 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
162193 if( i>=pIdx->nSample ){
162194 iUpper = pIdx->nRowEst0;
162202 iGap = iUpper - iLower;
162210 aStat[1] = pIdx->aAvgEq[nField-1];
162213 /* Restore the pRec->nField value before returning. */
162214 pRec->nField = nField;
162233 if( pTerm->truthProb<=0 ){
162234 nRet += pTerm->truthProb;
162235 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
162236 nRet -= 20; assert( 20==sqlite3LogEst(4) );
162248 assert( iCol>=0 && iCol<pIdx->nColumn );
162249 if( !pIdx->zColAff ){
162252 assert( pIdx->zColAff[iCol]!=0 );
162253 return pIdx->zColAff[iCol];
162261 ** range-scan on a skip-scan index. For example:
162266 ** Value pLoop->nOut is currently set to the estimated number of rows
162277 ** N is the total number of samples, the pLoop->nOut value is adjusted
162280 ** nOut = nOut * ( min(U - L, 1) / N )
162301 Index *p = pLoop->u.btree.pIndex;
162302 int nEq = pLoop->u.btree.nEq;
162303 sqlite3 *db = pParse->db;
162304 int nLower = -1;
162305 int nUpper = p->nSample+1;
162314 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
162316 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
162320 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
162321 nUpper = p2 ? 0 : p->nSample;
162327 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
162328 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
162338 nDiff = (nUpper - nLower);
162347 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
162348 pLoop->nOut -= nAdjust;
162350 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
162351 nLower, nUpper, nAdjust*-1, pLoop->nOut));
162381 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
162389 ** left-most column of the index). Or, if the query is:
162414 int nOut = pLoop->nOut;
162418 Index *p = pLoop->u.btree.pIndex;
162419 int nEq = pLoop->u.btree.nEq;
162421 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
162422 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
162424 if( nEq==pBuilder->nRecValid ){
162425 UnpackedRecord *pRec = pBuilder->pRec;
162427 int nBtm = pLoop->u.btree.nBtm;
162428 int nTop = pLoop->u.btree.nTop;
162433 ** key-prefix formed by the nEq values matched against the nEq left-most
162447 ** The number of rows between the two bounds is then just iUpper-iLower.
162451 int iLwrIdx = -2; /* aSample[] for the lower bound */
162452 int iUprIdx = -1; /* aSample[] for the upper bound */
162455 testcase( pRec->nField!=pBuilder->nRecValid );
162456 pRec->nField = pBuilder->nRecValid;
162461 iUpper = p->nRowEst0;
162463 /* Note: this call could be optimized away - since the same values must
162470 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
162471 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
162472 assert( p->aSortOrder!=0 );
162473 if( p->aSortOrder[nEq] ){
162482 Expr *pExpr = pLower->pExpr->pRight;
162489 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
162491 nOut--;
162499 Expr *pExpr = pUpper->pExpr->pRight;
162506 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
162508 nOut--;
162513 pBuilder->pRec = pRec;
162516 nNew = sqlite3LogEst(iUpper - iLower);
162521 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
162542 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 );
162547 ** has an application-defined likelihood(), assume the range is
162548 ** reduced by an additional 75%. This means that, by default, an open-ended
162552 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
162553 nNew -= 20;
162556 nOut -= (pLower!=0) + (pUpper!=0);
162560 if( pLoop->nOut>nOut ){
162562 pLoop->nOut, nOut));
162565 pLoop->nOut = (LogEst)nOut;
162573 ** the histogram data. This only works when x is the left-most
162580 ** non-zero.
162593 Index *p = pBuilder->pNew->u.btree.pIndex;
162594 int nEq = pBuilder->pNew->u.btree.nEq;
162595 UnpackedRecord *pRec = pBuilder->pRec;
162601 assert( nEq<=p->nColumn );
162602 assert( p->aSample!=0 );
162603 assert( p->nSample>0 );
162604 assert( pBuilder->nRecValid<nEq );
162608 if( pBuilder->nRecValid<(nEq-1) ){
162614 if( nEq>=p->nColumn ){
162619 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
162620 pBuilder->pRec = pRec;
162623 pBuilder->nRecValid = nEq;
162627 p->zName, nEq-1, (int)a[1]));
162637 ** an IN constraint where the right-hand side of the IN operator
162644 ** non-zero.
162657 Index *p = pBuilder->pNew->u.btree.pIndex;
162658 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
162659 int nRecValid = pBuilder->nRecValid;
162665 assert( p->aSample!=0 );
162666 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
162668 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
162670 pBuilder->nRecValid = nRecValid;
162678 assert( pBuilder->nRecValid==nRecValid );
162690 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
162695 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
162696 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
162697 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
162698 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
162699 if( pTerm->eOperator & WO_SINGLE ){
162700 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
162702 pTerm->leftCursor, pTerm->u.x.leftColumn);
162703 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
162705 pTerm->u.pOrInfo->indexable);
162707 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
162710 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
162711 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
162715 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
162716 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
162718 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
162719 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
162721 if( pTerm->iParent>=0 ){
162722 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
162725 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
162736 for(i=0; i<pWC->nTerm; i++){
162737 sqlite3WhereTermPrint(&pWC->a[i], i);
162747 WhereInfo *pWInfo = pWC->pWInfo;
162748 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
162749 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
162750 Table *pTab = pItem->pTab;
162751 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
162752 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
162753 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
162755 pItem->zAlias ? pItem->zAlias : pTab->zName);
162756 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
162758 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
162760 int i = sqlite3Strlen30(zName) - 1;
162761 while( zName[i]!='_' ) i--;
162764 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
162770 if( p->u.vtab.idxStr ){
162772 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
162774 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
162776 sqlite3DebugPrintf(" %-19s", z);
162779 if( p->wsFlags & WHERE_SKIPSCAN ){
162780 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
162782 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
162784 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
162785 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
162787 for(i=0; i<p->nLTerm; i++){
162788 sqlite3WhereTermPrint(p->aLTerm[i], i);
162799 p->aLTerm = p->aLTermSpace;
162800 p->nLTerm = 0;
162801 p->nLSlot = ArraySize(p->aLTermSpace);
162802 p->wsFlags = 0;
162809 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
162810 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
162811 sqlite3_free(p->u.vtab.idxStr);
162812 p->u.vtab.needFree = 0;
162813 p->u.vtab.idxStr = 0;
162814 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
162815 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
162816 sqlite3DbFreeNN(db, p->u.btree.pIndex);
162817 p->u.btree.pIndex = 0;
162827 if( p->aLTerm!=p->aLTermSpace ){
162828 sqlite3DbFreeNN(db, p->aLTerm);
162829 p->aLTerm = p->aLTermSpace;
162830 p->nLSlot = ArraySize(p->aLTermSpace);
162833 p->nLTerm = 0;
162834 p->wsFlags = 0;
162838 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
162842 if( p->nLSlot>=n ) return SQLITE_OK;
162844 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
162846 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
162847 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
162848 p->aLTerm = paNew;
162849 p->nLSlot = n;
162858 if( pFrom->nLTerm > pTo->nLSlot
162859 && whereLoopResize(db, pTo, pFrom->nLTerm)
162865 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
162866 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
162867 pFrom->u.vtab.needFree = 0;
162868 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
162869 pFrom->u.btree.pIndex = 0;
162889 sqlite3WhereClauseClear(&pWInfo->sWC);
162890 while( pWInfo->pLoops ){
162891 WhereLoop *p = pWInfo->pLoops;
162892 pWInfo->pLoops = p->pNextLoop;
162895 while( pWInfo->pMemToFree ){
162896 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
162897 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
162898 pWInfo->pMemToFree = pNext;
162917 ** was added because if X uses skip-scan less than Y it still might
162920 ** than a non-covering index even if it is a proper subset.
162927 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
162930 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
162931 if( pY->nSkip > pX->nSkip ) return 0;
162932 for(i=pX->nLTerm-1; i>=0; i--){
162933 if( pX->aLTerm[i]==0 ) continue;
162934 for(j=pY->nLTerm-1; j>=0; j--){
162935 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
162939 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
162940 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
162961 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
162962 for(; p; p=p->pNextLoop){
162963 if( p->iTab!=pTemplate->iTab ) continue;
162964 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
162969 pTemplate->rRun, pTemplate->nOut,
162970 MIN(p->rRun, pTemplate->rRun),
162971 MIN(p->nOut - 1, pTemplate->nOut)));
162972 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
162973 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
162978 pTemplate->rRun, pTemplate->nOut,
162979 MAX(p->rRun, pTemplate->rRun),
162980 MAX(p->nOut + 1, pTemplate->nOut)));
162981 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
162982 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
163006 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
163007 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
163016 assert( p->rSetup==0 || pTemplate->rSetup==0
163017 || p->rSetup==pTemplate->rSetup );
163021 ** rSetup. Call this SETUP-INVARIANT */
163022 assert( p->rSetup>=pTemplate->rSetup );
163024 /* Any loop using an application-defined index (or PRIMARY KEY or
163026 ** than an automatic index. Unless it is a skip-scan. */
163027 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
163028 && (pTemplate->nSkip)==0
163029 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
163030 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
163031 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
163041 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
163042 && p->rSetup<=pTemplate->rSetup /* (2a) */
163043 && p->rRun<=pTemplate->rRun /* (2b) */
163044 && p->nOut<=pTemplate->nOut /* (2c) */
163054 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
163055 && p->rRun>=pTemplate->rRun /* (2a) */
163056 && p->nOut>=pTemplate->nOut /* (2b) */
163058 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
163074 ** If pBuilder->pOrSet is not NULL then we care about only the
163076 ** information is gathered in the pBuilder->pOrSet object. This special
163079 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
163091 WhereInfo *pWInfo = pBuilder->pWInfo;
163092 sqlite3 *db = pWInfo->pParse->db;
163096 if( pBuilder->iPlanLimit==0 ){
163098 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
163101 pBuilder->iPlanLimit--;
163103 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
163105 /* If pBuilder->pOrSet is defined, then only keep track of the costs
163108 if( pBuilder->pOrSet!=0 ){
163109 if( pTemplate->nLTerm ){
163111 u16 n = pBuilder->pOrSet->n;
163114 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
163115 pTemplate->nOut);
163118 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
163119 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
163128 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
163136 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
163152 sqlite3WhereLoopPrint(p, pBuilder->pWC);
163157 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
163165 p->pNextLoop = 0;
163170 WhereLoop **ppTail = &p->pNextLoop;
163177 *ppTail = pToDel->pNextLoop;
163181 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
163188 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
163189 Index *pIndex = p->u.btree.pIndex;
163190 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
163191 p->u.btree.pIndex = 0;
163210 ** TODO --> Perhaps this is something that could be improved by better
163214 ** value corresponds to -1 in LogEst notation, so this means decrement
163221 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
163222 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
163232 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
163234 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
163236 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
163237 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
163239 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
163240 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
163241 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
163242 for(j=pLoop->nLTerm-1; j>=0; j--){
163243 pX = pLoop->aLTerm[j];
163246 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
163249 sqlite3ProgressCheck(pWC->pWInfo->pParse);
163250 if( pLoop->maskSelf==pTerm->prereqAll ){
163254 ** "self-culling".
163256 ** 2022-03-24: Self-culling only applies if either the extra terms
163257 ** are straight comparison operators that are non-true with NULL
163260 if( (pTerm->eOperator & 0x3f)!=0
163261 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
163264 pLoop->wsFlags |= WHERE_SELFCULL;
163267 if( pTerm->truthProb<=0 ){
163270 pLoop->nOut += pTerm->truthProb;
163274 pLoop->nOut--;
163275 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
163276 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
163278 Expr *pRight = pTerm->pExpr->pRight;
163280 testcase( pTerm->pExpr->op==TK_IS );
163281 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
163287 pTerm->wtFlags |= TERM_HEURTRUTH;
163294 if( pLoop->nOut > nRow-iReduce ){
163295 pLoop->nOut = nRow - iReduce;
163323 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
163326 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
163335 assert( ExprUseXList(pTerm->pExpr->pLeft) );
163336 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
163337 pRhs = pTerm->pExpr->pRight;
163339 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
163341 pRhs = pRhs->x.pList->a[i].pExpr;
163348 if( pLhs->op!=TK_COLUMN
163349 || pLhs->iTable!=iCur
163350 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
163351 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
163356 testcase( pLhs->iColumn==XN_ROWID );
163358 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
163363 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
163370 ** compiled with -DSQLITE_ENABLE_COSTMULT
163379 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
163382 ** When this function is called, pBuilder->pNew->nOut contains the
163387 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
163396 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
163397 Parse *pParse = pWInfo->pParse; /* Parsing context */
163398 sqlite3 *db = pParse->db; /* Database connection malloc context */
163403 Bitmask saved_prereq; /* Original value of pNew->prereq */
163404 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
163405 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
163406 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
163407 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
163408 u16 saved_nSkip; /* Original value of pNew->nSkip */
163409 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
163410 LogEst saved_nOut; /* Original value of pNew->nOut */
163416 pNew = pBuilder->pNew;
163417 assert( db->mallocFailed==0 || pParse->nErr>0 );
163418 if( pParse->nErr ){
163419 return pParse->rc;
163422 pProbe->pTable->zName,pProbe->zName,
163423 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
163425 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
163426 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
163427 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
163430 assert( pNew->u.btree.nBtm==0 );
163433 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
163435 assert( pNew->u.btree.nEq<pProbe->nColumn );
163436 assert( pNew->u.btree.nEq<pProbe->nKeyCol
163437 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
163439 saved_nEq = pNew->u.btree.nEq;
163440 saved_nBtm = pNew->u.btree.nBtm;
163441 saved_nTop = pNew->u.btree.nTop;
163442 saved_nSkip = pNew->nSkip;
163443 saved_nLTerm = pNew->nLTerm;
163444 saved_wsFlags = pNew->wsFlags;
163445 saved_prereq = pNew->prereq;
163446 saved_nOut = pNew->nOut;
163447 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
163449 pNew->rSetup = 0;
163450 rSize = pProbe->aiRowLogEst[0];
163453 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
163458 int nRecValid = pBuilder->nRecValid;
163460 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
163465 if( pTerm->prereqRight & pNew->maskSelf ) continue;
163469 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
163471 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
163476 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
163477 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
163479 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
163481 pNew->wsFlags = saved_wsFlags;
163482 pNew->u.btree.nEq = saved_nEq;
163483 pNew->u.btree.nBtm = saved_nBtm;
163484 pNew->u.btree.nTop = saved_nTop;
163485 pNew->nLTerm = saved_nLTerm;
163486 if( pNew->nLTerm>=pNew->nLSlot
163487 && whereLoopResize(db, pNew, pNew->nLTerm+1)
163489 break; /* OOM while trying to enlarge the pNew->aLTerm array */
163491 pNew->aLTerm[pNew->nLTerm++] = pTerm;
163492 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
163495 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
163496 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
163497 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
163501 Expr *pExpr = pTerm->pExpr;
163512 for(i=0; i<pNew->nLTerm-1; i++){
163513 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
163515 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
163517 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
163519 if( pProbe->hasStat1 && rLogSize>=10 ){
163526 ** the left-most index column, M==N.
163536 ** with the index, as using an index has better worst-case behavior.
163541 M = pProbe->aiRowLogEst[saved_nEq];
163543 /* TUNING v----- 10 to bias toward indexed IN */
163544 x = M + logK + 10 - (nIn + rLogSize);
163553 " nInMul=%d) prefers skip-scan\n",
163555 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
163564 pNew->wsFlags |= WHERE_COLUMN_IN;
163566 int iCol = pProbe->aiColumn[saved_nEq];
163567 pNew->wsFlags |= WHERE_COLUMN_EQ;
163568 assert( saved_nEq==pNew->u.btree.nEq );
163570 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
163572 if( iCol==XN_ROWID || pProbe->uniqNotNull
163573 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
163575 pNew->wsFlags |= WHERE_ONEROW;
163577 pNew->wsFlags |= WHERE_UNQ_WANTED;
163580 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
163582 pNew->wsFlags |= WHERE_COLUMN_NULL;
163585 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
163590 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
163591 pNew->u.btree.nBtm = nVecLen;
163594 if( pTerm->wtFlags & TERM_LIKEOPT ){
163598 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
163599 assert( pTop->wtFlags & TERM_LIKEOPT );
163600 assert( pTop->eOperator==WO_LT );
163601 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
163602 pNew->aLTerm[pNew->nLTerm++] = pTop;
163603 pNew->wsFlags |= WHERE_TOP_LIMIT;
163604 pNew->u.btree.nTop = 1;
163610 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
163611 pNew->u.btree.nTop = nVecLen;
163613 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
163614 pNew->aLTerm[pNew->nLTerm-2] : 0;
163618 /* At this point pNew->nOut is set to the number of rows expected to
163622 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
163623 assert( pNew->nOut==saved_nOut );
163624 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
163629 int nEq = ++pNew->u.btree.nEq;
163632 assert( pNew->nOut==saved_nOut );
163633 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
163636 pNew->nOut += pTerm->truthProb;
163637 pNew->nOut -= nIn;
163642 && pProbe->nSample
163643 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
163644 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
163647 Expr *pExpr = pTerm->pExpr;
163652 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
163654 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
163659 pNew->nOut = sqlite3LogEst(nOut);
163663 ** See tag-202002240-1 */
163664 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
163673 pTerm->wtFlags |= TERM_HIGHTRUTH;
163674 if( pTerm->wtFlags & TERM_HEURTRUTH ){
163678 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
163681 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
163682 pNew->nOut -= nIn;
163688 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
163693 pNew->nOut += 10;
163700 ** it to pNew->rRun, which is currently set to the cost of the index
163701 ** seek only. Then, if this is a non-covering index, add the cost of
163703 assert( pSrc->pTab->szTabRow>0 );
163704 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
163705 /* The pProbe->szIdxRow is low for an IPK table since the interior
163707 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
163708 ** under-estimate the scanning cost. */
163709 rCostIdx = pNew->nOut + 16;
163711 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
163713 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
163714 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
163715 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
163717 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
163719 nOutUnadjusted = pNew->nOut;
163720 pNew->rRun += nInMul + nIn;
163721 pNew->nOut += nInMul + nIn;
163722 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
163725 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
163726 pNew->nOut = saved_nOut;
163728 pNew->nOut = nOutUnadjusted;
163731 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
163732 && pNew->u.btree.nEq<pProbe->nColumn
163733 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
163734 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
163736 if( pNew->u.btree.nEq>3 ){
163741 pNew->nOut = saved_nOut;
163743 pBuilder->nRecValid = nRecValid;
163746 pNew->prereq = saved_prereq;
163747 pNew->u.btree.nEq = saved_nEq;
163748 pNew->u.btree.nBtm = saved_nBtm;
163749 pNew->u.btree.nTop = saved_nTop;
163750 pNew->nSkip = saved_nSkip;
163751 pNew->wsFlags = saved_wsFlags;
163752 pNew->nOut = saved_nOut;
163753 pNew->nLTerm = saved_nLTerm;
163755 /* Consider using a skip-scan if there are no WHERE clause constraints
163756 ** available for the left-most terms of the index, and if the average
163757 ** number of repeats in the left-most terms is at least 18.
163767 && saved_nEq+1<pProbe->nKeyCol
163768 && saved_nEq==pNew->nLTerm
163769 && pProbe->noSkipScan==0
163770 && pProbe->hasStat1!=0
163772 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
163773 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
163776 pNew->u.btree.nEq++;
163777 pNew->nSkip++;
163778 pNew->aLTerm[pNew->nLTerm++] = 0;
163779 pNew->wsFlags |= WHERE_SKIPSCAN;
163780 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
163781 pNew->nOut -= nIter;
163782 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
163783 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
163786 pNew->nOut = saved_nOut;
163787 pNew->u.btree.nEq = saved_nEq;
163788 pNew->nSkip = saved_nSkip;
163789 pNew->wsFlags = saved_wsFlags;
163793 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
163814 if( pIndex->bUnordered ) return 0;
163815 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
163816 for(ii=0; ii<pOB->nExpr; ii++){
163817 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
163819 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
163820 if( pExpr->iColumn<0 ) return 1;
163821 for(jj=0; jj<pIndex->nKeyCol; jj++){
163822 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
163824 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
163825 for(jj=0; jj<pIndex->nKeyCol; jj++){
163826 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
163827 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
163850 pParse = pWC->pWInfo->pParse;
163851 while( pWhere->op==TK_AND ){
163852 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
163853 pWhere = pWhere->pRight;
163855 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
163856 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
163858 pExpr = pTerm->pExpr;
163859 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
163862 && (pTerm->wtFlags & TERM_VNULL)==0
163880 for(i=0; i<pIdx->nColumn; i++){
163881 if( pIdx->aiColumn[i]==XN_EXPR
163882 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
163902 ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
163904 ** If the Expr node references the table with cursor pCk->iTabCur, then
163905 ** make sure that column is covered by the index pCk->pIdx. We know that
163906 ** all columns less than 63 (really BMS-1) are covered, so we don't need
163909 ** If the index does not cover the column, then set pWalk->eCode to
163910 ** non-zero and return WRC_Abort to stop the search.
163915 ** If pCk->pIdx contains indexed expressions and one of those expressions
163925 pCk = pWalk->u.pCovIdxCk;
163926 pIdx = pCk->pIdx;
163927 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
163928 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
163929 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
163930 pIdx = pWalk->u.pCovIdxCk->pIdx;
163931 aiColumn = pIdx->aiColumn;
163932 nColumn = pIdx->nColumn;
163934 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
163936 pCk->bUnidx = 1;
163938 }else if( pIdx->bHasExpr
163939 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
163940 pCk->bExpr = 1;
163948 ** pIdx is an index that covers all of the low-number columns used by
163949 ** pWInfo->pSelect (columns from 0 through 62) or an index that has
163978 if( pWInfo->pSelect==0 ){
163983 if( pIdx->bHasExpr==0 ){
163984 for(i=0; i<pIdx->nColumn; i++){
163985 if( pIdx->aiColumn[i]>=BMS-1 ) break;
163987 if( i>=pIdx->nColumn ){
164002 sqlite3WalkSelect(&w, pWInfo->pSelect);
164015 ** free the Parse->pIdxEpr list when the Parse object is destroyed.
164021 *pp = p->pIENext;
164022 sqlite3ExprDelete(db, p->pExpr);
164028 ** This function is called for a partial index - one with a WHERE clause - in
164037 ** The "a" in the select-list may be replaced by <expr>, iff:
164062 assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 );
164065 if( pPart->op==TK_AND ){
164066 wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem);
164067 pPart = pPart->pLeft;
164070 if( (pPart->op==TK_EQ || pPart->op==TK_IS) ){
164071 Expr *pLeft = pPart->pLeft;
164072 Expr *pRight = pPart->pRight;
164075 if( pLeft->op!=TK_COLUMN ) return;
164078 if( pLeft->iColumn<0 ) return;
164079 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
164082 sqlite3 *db = pParse->db;
164085 int bNullRow = (pItem->fg.jointype&(JT_LEFT|JT_LTORJ))!=0;
164086 p->pExpr = sqlite3ExprDup(db, pRight, 0);
164087 p->iDataCur = pItem->iCursor;
164088 p->iIdxCur = iIdxCur;
164089 p->iIdxCol = pLeft->iColumn;
164090 p->bMaybeNullRow = bNullRow;
164091 p->pIENext = pParse->pIdxPartExpr;
164092 p->aff = aff;
164093 pParse->pIdxPartExpr = p;
164094 if( p->pIENext==0 ){
164095 void *pArg = (void*)&pParse->pIdxPartExpr;
164099 }else if( pLeft->iColumn<(BMS-1) ){
164100 *pMask &= ~((Bitmask)1 << pLeft->iColumn);
164109 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
164110 ** a b-tree table, not a virtual table.
164112 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
164117 ** cost = nRow * 3.0 // full-table scan
164119 ** cost = nRow * (K+3.0) // scan of non-covering index
164126 ** the index b-tree:
164129 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
164133 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
164138 ** log(nRow) factor is omitted from a non-covering index scan in order to
164139 ** bias the scoring in favor of using an index, since the worst-case
164140 ** performance of using an index is far better than the worst-case performance
164151 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
164162 pNew = pBuilder->pNew;
164163 pWInfo = pBuilder->pWInfo;
164164 pTabList = pWInfo->pTabList;
164165 pSrc = pTabList->a + pNew->iTab;
164166 pTab = pSrc->pTab;
164167 pWC = pBuilder->pWC;
164168 assert( !IsVirtual(pSrc->pTab) );
164170 if( pSrc->fg.isIndexedBy ){
164171 assert( pSrc->fg.isCte==0 );
164173 pProbe = pSrc->u2.pIBIndex;
164175 pProbe = pTab->pIndex;
164191 aiRowEstPk[0] = pTab->nRowLogEst;
164193 pFirst = pSrc->pTab->pIndex;
164194 if( pSrc->fg.notIndexed==0 ){
164201 rSize = pTab->nRowLogEst;
164205 if( !pBuilder->pOrSet /* Not part of an OR optimization */
164206 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
164207 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
164208 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
164209 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
164211 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
164212 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
164213 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
164215 /* Generate auto-index WhereLoops */
164218 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
164220 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
164221 if( pTerm->prereqRight & pNew->maskSelf ) continue;
164223 pNew->u.btree.nEq = 1;
164224 pNew->nSkip = 0;
164225 pNew->u.btree.pIndex = 0;
164226 pNew->nLTerm = 1;
164227 pNew->aLTerm[0] = pTerm;
164228 /* TUNING: One-time cost for computing the automatic index is
164231 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
164236 pNew->rSetup = rLogSize + rSize;
164237 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
164238 pNew->rSetup += 28;
164240 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
164243 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
164244 if( pNew->rSetup<0 ) pNew->rSetup = 0;
164249 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
164250 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
164251 pNew->wsFlags = WHERE_AUTO_INDEX;
164252 pNew->prereq = mPrereq | pTerm->prereqRight;
164262 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
164264 if( pProbe->pPartIdxWhere!=0
164265 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
164266 pProbe->pPartIdxWhere)
164268 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
164271 if( pProbe->bNoQuery ) continue;
164272 rSize = pProbe->aiRowLogEst[0];
164273 pNew->u.btree.nEq = 0;
164274 pNew->u.btree.nBtm = 0;
164275 pNew->u.btree.nTop = 0;
164276 pNew->nSkip = 0;
164277 pNew->nLTerm = 0;
164278 pNew->iSortIdx = 0;
164279 pNew->rSetup = 0;
164280 pNew->prereq = mPrereq;
164281 pNew->nOut = rSize;
164282 pNew->u.btree.pIndex = pProbe;
164283 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
164286 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
164287 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
164289 pNew->wsFlags = WHERE_IPK;
164292 pNew->iSortIdx = b ? iSortIdx : 0;
164295 ** since index lookups have better worst-case performance if our
164306 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
164308 pNew->rRun = rSize + 16;
164310 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
164313 pNew->nOut = rSize;
164317 if( pProbe->isCovering ){
164319 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
164321 m = pSrc->colUsed & pProbe->colNotIdxed;
164322 if( pProbe->pPartIdxWhere ){
164324 pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0
164327 pNew->wsFlags = WHERE_INDEXED;
164328 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
164329 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
164332 ("-> %s is not a covering index"
164333 " according to whereIsCoveringIndex()\n", pProbe->zName));
164337 pNew->wsFlags |= isCov;
164340 ("-> %s is a covering expression index"
164341 " according to whereIsCoveringIndex()\n", pProbe->zName));
164345 ("-> %s might be a covering expression index"
164346 " according to whereIsCoveringIndex()\n", pProbe->zName));
164351 ("-> %s a covering index according to bitmasks\n",
164352 pProbe->zName, m==0 ? "is" : "is not"));
164353 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
164360 || pProbe->pPartIdxWhere!=0
164361 || pSrc->fg.isIndexedBy
164363 && pProbe->bUnordered==0
164364 && (pProbe->szIdxRow<pTab->szTabRow)
164365 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
164367 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
164370 pNew->iSortIdx = b ? iSortIdx : 0;
164375 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
164377 /* If this is a non-covering index scan, add in the cost of
164384 int iCur = pSrc->iCursor;
164385 WhereClause *pWC2 = &pWInfo->sWC;
164386 for(ii=0; ii<pWC2->nTerm; ii++){
164387 WhereTerm *pTerm = &pWC2->a[ii];
164388 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
164393 if( pTerm->truthProb<=0 ){
164394 nLookup += pTerm->truthProb;
164396 nLookup--;
164397 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
164401 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
164403 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
164405 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
164406 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
164408 ** positioned to the correct row during the right-join no-match
164413 pNew->nOut = rSize;
164418 pBuilder->bldFlags1 = 0;
164420 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
164421 /* If a non-unique index is used, or if a prefix of the key for
164422 ** unique index is used (making the index functionally non-unique)
164425 pTab->tabFlags |= TF_StatsUsed;
164428 sqlite3Stat4ProbeFree(pBuilder->pRec);
164429 pBuilder->nRecValid = 0;
164430 pBuilder->pRec = 0;
164442 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
164443 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
164444 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
164449 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
164477 WhereClause *pWC = pBuilder->pWC;
164480 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
164484 WhereLoop *pNew = pBuilder->pNew;
164485 Parse *pParse = pBuilder->pWInfo->pParse;
164486 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
164487 int nConstraint = pIdxInfo->nConstraint;
164491 pNew->prereq = mPrereq;
164495 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
164497 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
164498 pIdxCons->usable = 0;
164499 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
164500 && (pTerm->eOperator & mExclude)==0
164503 pIdxCons->usable = 1;
164509 assert( pIdxInfo->needToFreeIdxStr==0 );
164510 pIdxInfo->idxStr = 0;
164511 pIdxInfo->idxNum = 0;
164512 pIdxInfo->orderByConsumed = 0;
164513 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
164514 pIdxInfo->estimatedRows = 25;
164515 pIdxInfo->idxFlags = 0;
164516 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
164517 pHidden->mHandleIn = 0;
164520 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
164527 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
164533 mxTerm = -1;
164534 assert( pNew->nLSlot>=nConstraint );
164535 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
164536 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
164537 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
164540 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
164542 int j = pIdxCons->iTermOffset;
164545 || j>=pWC->nTerm
164546 || pNew->aLTerm[iTerm]!=0
164547 || pIdxCons->usable==0
164549 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
164550 testcase( pIdxInfo->needToFreeIdxStr );
164553 testcase( iTerm==nConstraint-1 );
164555 testcase( j==pWC->nTerm-1 );
164556 pTerm = &pWC->a[j];
164557 pNew->prereq |= pTerm->prereqRight;
164558 assert( iTerm<pNew->nLSlot );
164559 pNew->aLTerm[iTerm] = pTerm;
164566 pNew->u.vtab.omitMask |= 1<<iTerm;
164570 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
164571 pNew->u.vtab.bOmitOffset = 1;
164574 if( SMASKBIT32(i) & pHidden->mHandleIn ){
164575 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
164576 }else if( (pTerm->eOperator & WO_IN)!=0 ){
164582 pIdxInfo->orderByConsumed = 0;
164583 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
164594 if( pIdxInfo->needToFreeIdxStr ){
164595 sqlite3_free(pIdxInfo->idxStr);
164596 pIdxInfo->idxStr = 0;
164597 pIdxInfo->needToFreeIdxStr = 0;
164605 pNew->nLTerm = mxTerm+1;
164607 if( pNew->aLTerm[i]==0 ){
164608 /* The non-zero argvIdx values must be contiguous. Raise an
164610 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
164611 testcase( pIdxInfo->needToFreeIdxStr );
164615 assert( pNew->nLTerm<=pNew->nLSlot );
164616 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
164617 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
164618 pIdxInfo->needToFreeIdxStr = 0;
164619 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
164620 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
164621 pIdxInfo->nOrderBy : 0);
164622 pNew->rSetup = 0;
164623 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
164624 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
164628 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
164629 pNew->wsFlags |= WHERE_ONEROW;
164631 pNew->wsFlags &= ~WHERE_ONEROW;
164634 if( pNew->u.vtab.needFree ){
164635 sqlite3_free(pNew->u.vtab.idxStr);
164636 pNew->u.vtab.needFree = 0;
164640 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
164663 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
164665 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
164666 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
164667 if( pX->pLeft ){
164668 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
164670 zRet = (pC ? pC->zName : sqlite3StrBINARY);
164683 if( m & pHidden->mIn ){
164685 pHidden->mHandleIn &= ~m;
164687 pHidden->mHandleIn |= m;
164698 ** on the right-hand-side of constraint iCons.
164708 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
164709 rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */
164711 if( pH->aRhs[iCons]==0 ){
164712 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
164714 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
164715 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
164719 pVal = pH->aRhs[iCons];
164723 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
164724 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
164735 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
164736 return pHidden->eDistinct;
164742 ** prepared is read-only, then just start read transactions on all
164746 ** This is used by the (built-in) sqlite_dbpage virtual table.
164749 int nDb = pParse->db->nDb;
164754 if( DbMaskNonZero(pParse->writeMask) ){
164763 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
164784 ** mUnusable should always be configured as "not-usable" for xBestIndex.
164805 pWInfo = pBuilder->pWInfo;
164806 pParse = pWInfo->pParse;
164807 pWC = pBuilder->pWC;
164808 pNew = pBuilder->pNew;
164809 pSrc = &pWInfo->pTabList->a[pNew->iTab];
164810 assert( IsVirtual(pSrc->pTab) );
164813 pNew->rSetup = 0;
164814 pNew->wsFlags = WHERE_VIRTUALTABLE;
164815 pNew->nLTerm = 0;
164816 pNew->u.vtab.needFree = 0;
164817 nConstraint = p->nConstraint;
164818 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
164819 freeIndexInfo(pParse->db, p);
164824 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
164841 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
164854 mBestNoIn = pNew->prereq & ~mPrereq;
164869 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
164880 if( pNew->prereq==mPrereq ){
164906 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
164907 freeIndexInfo(pParse->db, p);
164908 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
164922 WhereInfo *pWInfo = pBuilder->pWInfo;
164933 pWC = pBuilder->pWC;
164934 pWCEnd = pWC->a + pWC->nTerm;
164935 pNew = pBuilder->pNew;
164937 pItem = pWInfo->pTabList->a + pNew->iTab;
164938 iCur = pItem->iCursor;
164940 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
164941 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
164943 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
164944 if( (pTerm->eOperator & WO_OR)!=0
164945 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
164947 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
164948 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
164956 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
164957 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
164958 if( (pOrTerm->eOperator & WO_AND)!=0 ){
164959 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
164960 }else if( pOrTerm->leftCursor==iCur ){
164961 tempWC.pWInfo = pWC->pWInfo;
164973 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
164974 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
164980 if( IsVirtual(pItem->pTab) ){
165011 pNew->nLTerm = 1;
165012 pNew->aLTerm[0] = pTerm;
165013 pNew->wsFlags = WHERE_MULTI_OR;
165014 pNew->rSetup = 0;
165015 pNew->iSortIdx = 0;
165016 memset(&pNew->u, 0, sizeof(pNew->u));
165019 ** of all sub-scans required by the OR-scan. However, due to rounding
165020 ** errors, it may be that the cost of the OR-scan is equal to its
165021 ** most expensive sub-scan. Add the smallest possible penalty
165028 ** the planner may elect to "OR" together a full-table scan and an
165030 pNew->rRun = sSum.a[i].rRun + 1;
165031 pNew->nOut = sSum.a[i].nOut;
165032 pNew->prereq = sSum.a[i].prereq;
165035 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
165045 WhereInfo *pWInfo = pBuilder->pWInfo;
165049 SrcList *pTabList = pWInfo->pTabList;
165051 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
165052 sqlite3 *db = pWInfo->pParse->db;
165060 pNew = pBuilder->pNew;
165063 assert( pNew->nLTerm==0 );
165064 assert( pNew->wsFlags==0 );
165065 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
165066 assert( pNew->aLTerm!=0 );
165068 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
165069 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
165071 pNew->iTab = iTab;
165072 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
165073 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
165075 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
165083 ** prevent FROM-clause terms from moving from the right side of
165087 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
165089 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
165094 if( IsVirtual(pItem->pTab) ){
165097 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
165098 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
165107 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
165110 mPrior |= pNew->maskSelf;
165111 if( rc || db->mallocFailed ){
165141 ** pOrderBy terms must be matched in strict left-to-right order.
165148 u16 nLoop, /* Number of entries in pPath->aLoop[] */
165149 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
165155 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
165171 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
165174 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
165178 ** We say the WhereLoop is "one-row" if it generates no more than one
165179 ** row of output. A WhereLoop is one-row if all of the following are true:
165182 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
165183 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
165185 ** We say the WhereLoop is "order-distinct" if the set of columns from
165187 ** row of the WhereLoop. Every one-row WhereLoop is automatically
165188 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
165189 ** is not order-distinct. To be order-distinct is not quite the same as being
165191 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
165192 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
165196 ** automatically order-distinct.
165202 nOrderBy = pOrderBy->nExpr;
165203 testcase( nOrderBy==BMS-1 );
165204 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
165206 obDone = MASKBIT(nOrderBy)-1;
165214 if( iLoop>0 ) ready |= pLoop->maskSelf;
165216 pLoop = pPath->aLoop[iLoop];
165221 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
165222 if( pLoop->u.vtab.isOrdered
165229 pLoop->u.btree.nDistinctCol = 0;
165231 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
165240 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
165242 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
165243 if( pOBExpr->iTable!=iCur ) continue;
165244 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
165247 if( pTerm->eOperator==WO_IN ){
165253 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
165254 if( j>=pLoop->nLTerm ) continue;
165256 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
165257 Parse *pParse = pWInfo->pParse;
165258 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
165259 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
165261 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
165264 testcase( pTerm->pExpr->op==TK_IS );
165269 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
165270 if( pLoop->wsFlags & WHERE_IPK ){
165274 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
165277 nKeyCol = pIndex->nKeyCol;
165278 nColumn = pIndex->nColumn;
165279 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
165280 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
165281 || !HasRowid(pIndex->pTable));
165282 /* All relevant terms of the index must also be non-NULL in order
165285 ** made at tag-20210426-1 below */
165287 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
165298 assert( j>=pLoop->u.btree.nEq
165299 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
165301 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
165302 u16 eOp = pLoop->aLTerm[j]->eOperator;
165307 ** the loop need to be marked as not order-distinct because it can
165326 ** j<pLoop->u.btree.nEq constraint above. Any equality other
165329 Expr *pX = pLoop->aLTerm[j]->pExpr;
165330 for(i=j+1; i<pLoop->u.btree.nEq; i++){
165331 if( pLoop->aLTerm[i]->pExpr==pX ){
165332 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
165341 ** (revIdx) for the j-th column of the index.
165344 iColumn = pIndex->aiColumn[j];
165345 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
165346 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
165353 ** WhereLoop is not well-ordered. tag-20210426-1
165357 && j>=pLoop->u.btree.nEq
165358 && pIndex->pTable->aCol[iColumn].notNull==0
165367 /* Find the ORDER BY term that corresponds to the j-th column
165373 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
165379 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
165380 if( pOBExpr->iTable!=iCur ) continue;
165381 if( pOBExpr->iColumn!=iColumn ) continue;
165383 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
165389 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
165390 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
165393 pLoop->u.btree.nDistinctCol = j+1;
165403 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
165408 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
165413 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
165414 if( j==pLoop->u.btree.nEq ){
165415 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
165439 } /* end-if not one-row */
165443 orderDistinctMask |= pLoop->maskSelf;
165448 p = pOrderBy->a[i].pExpr;
165449 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
165456 } /* End the loop over all WhereLoops from outer-most down to inner-most */
165459 for(i=nOrderBy-1; i>0; i--){
165460 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
165465 return -1;
165472 ** BY clause - and so any order that groups rows as required satisfies the
165489 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
165490 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
165493 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
165494 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
165495 return pWInfo->sorted;
165503 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
165504 if( pLast ) zName[i++] = pLast->cId;
165526 ** Or, if the order-by clause has X terms but only the last Y
165527 ** terms are out of order, then block-sorting will reduce the
165543 assert( pWInfo->pSelect!=0 );
165544 assert( pWInfo->pSelect->pEList!=0 );
165546 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
165550 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
165558 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
165563 if( pWInfo->iLimit<nRow ){
165564 nRow = pWInfo->iLimit;
165566 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
165569 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
165576 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
165578 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
165608 pParse = pWInfo->pParse;
165609 nLoop = pWInfo->nLevel;
165611 ** For 2-way joins, the 5 best paths are followed.
165614 assert( nLoop<=pWInfo->pTabList->nSrc );
165615 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
165616 nRowEst, pParse->nQueryLoop));
165623 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
165626 nOrderBy = pWInfo->pOrderBy->nExpr;
165632 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
165638 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
165639 pFrom->aLoop = pX;
165644 ** is either zero - meaning it has not yet been initialized - or the
165659 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
165667 ** -1, indicating that the result set may or may not be ordered,
165669 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
165678 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
165684 Bitmask revMask; /* Mask of rev-order loops for (..) */
165686 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
165687 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
165688 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
165699 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
165700 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
165701 nOut = pFrom->nRow + pWLoop->nOut;
165702 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
165703 isOrdered = pFrom->isOrdered;
165707 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
165710 revMask = pFrom->revLoop;
165725 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
165726 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
165730 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
165734 ** mxChoice best-so-far paths.
165736 ** First look for an existing path among best-so-far paths
165740 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
165741 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
165742 ** of legal values for isOrdered, -1..64.
165745 if( pTo->maskLoop==maskNew
165746 && ((pTo->isOrdered^isOrdered)&0x80)==0
165748 testcase( jj==nTo-1 );
165753 /* None of the existing best-so-far paths match the candidate. */
165758 ** paths currently in the best-so-far buffer. So discard
165762 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
165770 ** needs to be added to the set of best-so-far paths. */
165781 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
165787 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
165793 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
165795 if( pTo->rCost<rCost
165796 || (pTo->rCost==rCost
165797 && (pTo->nRow<nOut
165798 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
165805 "Skip %s cost=%-3d,%3d,%3d order=%c",
165808 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
165809 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
165810 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
165814 testcase( pTo->rCost==rCost );
165817 testcase( pTo->rCost==rCost+1 );
165823 "Update %s cost=%-3d,%3d,%3d order=%c",
165826 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
165827 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
165828 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
165833 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
165834 pTo->revLoop = revMask;
165835 pTo->nRow = nOut;
165836 pTo->rCost = rCost;
165837 pTo->rUnsorted = rUnsorted;
165838 pTo->isOrdered = isOrdered;
165839 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
165840 pTo->aLoop[iLoop] = pWLoop;
165846 if( pTo->rCost>mxCost
165847 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
165849 mxCost = pTo->rCost;
165850 mxUnsorted = pTo->rUnsorted;
165860 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
165862 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
165863 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
165864 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
165865 if( pTo->isOrdered>0 ){
165866 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
165883 sqlite3StackFreeNN(pParse->db, pSpace);
165890 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
165892 assert( pWInfo->nLevel==nLoop );
165895 WhereLevel *pLevel = pWInfo->a + iLoop;
165896 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
165897 pLevel->iFrom = pWLoop->iTab;
165898 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
165900 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
165901 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
165902 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
165906 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
165907 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
165908 if( rc==pWInfo->pResultSet->nExpr ){
165909 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
165912 pWInfo->bOrderedInnerLoop = 0;
165913 if( pWInfo->pOrderBy ){
165914 pWInfo->nOBSat = pFrom->isOrdered;
165915 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
165916 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
165917 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
165919 if( pWInfo->pSelect->pOrderBy
165920 && pWInfo->nOBSat > pWInfo->pSelect->pOrderBy->nExpr ){
165921 pWInfo->nOBSat = pWInfo->pSelect->pOrderBy->nExpr;
165924 pWInfo->revMask = pFrom->revLoop;
165925 if( pWInfo->nOBSat<=0 ){
165926 pWInfo->nOBSat = 0;
165928 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
165933 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
165934 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
165937 if( rc==pWInfo->pOrderBy->nExpr ){
165938 pWInfo->bOrderedInnerLoop = 1;
165939 pWInfo->revMask = m;
165944 && pWInfo->nOBSat==1
165945 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
165947 pWInfo->bOrderedInnerLoop = 1;
165950 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
165951 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
165954 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
165955 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
165957 assert( pWInfo->sorted==0 );
165958 if( nOrder==pWInfo->pOrderBy->nExpr ){
165959 pWInfo->sorted = 1;
165960 pWInfo->revMask = revMask;
165966 pWInfo->nRowOut = pFrom->nRow;
165969 sqlite3StackFreeNN(pParse->db, pSpace);
165977 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
165980 ** Return non-zero on success, if this query can be handled by this
165981 ** no-frills query planner. Return zero if this query needs the
165982 ** general-purpose query planner.
165996 pWInfo = pBuilder->pWInfo;
165997 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
165998 assert( pWInfo->pTabList->nSrc>=1 );
165999 pItem = pWInfo->pTabList->a;
166000 pTab = pItem->pTab;
166002 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
166003 testcase( pItem->fg.isIndexedBy );
166004 testcase( pItem->fg.notIndexed );
166007 iCur = pItem->iCursor;
166008 pWC = &pWInfo->sWC;
166009 pLoop = pBuilder->pNew;
166010 pLoop->wsFlags = 0;
166011 pLoop->nSkip = 0;
166012 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
166013 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
166015 testcase( pTerm->eOperator & WO_IS );
166016 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
166017 pLoop->aLTerm[0] = pTerm;
166018 pLoop->nLTerm = 1;
166019 pLoop->u.btree.nEq = 1;
166021 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
166023 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
166025 assert( pLoop->aLTermSpace==pLoop->aLTerm );
166027 || pIdx->pPartIdxWhere!=0
166028 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
166030 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
166031 for(j=0; j<pIdx->nKeyCol; j++){
166033 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
166035 testcase( pTerm->eOperator & WO_IS );
166036 pLoop->aLTerm[j] = pTerm;
166038 if( j!=pIdx->nKeyCol ) continue;
166039 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
166040 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
166041 pLoop->wsFlags |= WHERE_IDX_ONLY;
166043 pLoop->nLTerm = j;
166044 pLoop->u.btree.nEq = j;
166045 pLoop->u.btree.pIndex = pIdx;
166047 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
166051 if( pLoop->wsFlags ){
166052 pLoop->nOut = (LogEst)1;
166053 pWInfo->a[0].pWLoop = pLoop;
166054 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
166055 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
166056 pWInfo->a[0].iTabCur = iCur;
166057 pWInfo->nRowOut = 1;
166058 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
166059 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
166060 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
166062 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
166064 pLoop->cId = '0';
166080 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
166081 pWalker->eCode = 0;
166088 ** Return true if the expression contains no non-deterministic SQL
166089 ** functions. Do not consider non-deterministic SQL functions that are
166090 ** part of sub-select statements.
166113 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
166114 p->cId = zLabel[i%(sizeof(zLabel)-1)];
166134 ** 5) The table must not have an inner-join ON or USING clause if there is
166138 ** the right-most table of a subquery that was flattened into the
166139 ** main query and that subquery was the right-hand operand of an
166169 assert( pWInfo->nLevel>=2 );
166170 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
166174 assert( pWInfo->pResultSet!=0 );
166175 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
166177 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
166178 if( pWInfo->pOrderBy ){
166179 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
166181 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
166182 for(i=pWInfo->nLevel-1; i>=1; i--){
166186 pLoop = pWInfo->a[i].pWLoop;
166187 pItem = &pWInfo->pTabList->a[pLoop->iTab];
166188 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
166189 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
166190 && (pLoop->wsFlags & WHERE_ONEROW)==0
166194 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
166195 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
166196 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
166197 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
166198 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
166199 || pTerm->pExpr->w.iJoin!=pItem->iCursor
166205 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
166206 && pTerm->pExpr->w.iJoin==pItem->iCursor
166212 WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
166213 notReady &= ~pLoop->maskSelf;
166214 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
166215 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
166216 pTerm->wtFlags |= TERM_CODED;
166219 if( i!=pWInfo->nLevel-1 ){
166220 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
166221 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
166223 pWInfo->nLevel--;
166224 assert( pWInfo->nLevel>0 );
166238 ** (3) Bloom-filter processing is not disabled. (Checked by the
166253 assert( pWInfo->nLevel>=2 );
166254 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
166255 for(i=0; i<pWInfo->nLevel; i++){
166256 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
166258 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
166259 Table *pTab = pItem->pTab;
166260 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
166261 pTab->tabFlags |= TF_StatsUsed;
166263 && (pLoop->wsFlags & reqFlags)==reqFlags
166264 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
166265 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
166267 if( nSearch > pTab->nRowLogEst ){
166268 testcase( pItem->fg.jointype & JT_LEFT );
166269 pLoop->wsFlags |= WHERE_BLOOMFILTER;
166270 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
166272 "-> use Bloom-filter on loop %c because there are ~%.1e "
166274 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
166275 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
166278 nSearch += pLoop->nOut;
166288 ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
166294 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
166295 Index *pIdx, /* The index-on-expression that contains the expressions */
166302 assert( pIdx->bHasExpr );
166303 pTab = pIdx->pTable;
166304 for(i=0; i<pIdx->nColumn; i++){
166306 int j = pIdx->aiColumn[i];
166309 pExpr = pIdx->aColExpr->a[i].pExpr;
166310 testcase( pTabItem->fg.jointype & JT_LEFT );
166311 testcase( pTabItem->fg.jointype & JT_RIGHT );
166312 testcase( pTabItem->fg.jointype & JT_LTORJ );
166313 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
166314 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
166315 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
166321 if( pExpr->op==TK_FUNCTION ){
166327 sqlite3 *db = pParse->db;
166329 n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166330 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166331 if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166335 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166337 p->pIENext = pParse->pIdxEpr;
166340 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
166344 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
166345 p->iDataCur = pTabItem->iCursor;
166346 p->iIdxCur = iIdxCur;
166347 p->iIdxCol = i;
166348 p->bMaybeNullRow = bMaybeNullRow;
166349 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
166350 p->aff = pIdx->zColAff[i];
166353 p->zIdxName = pIdx->zName;
166355 pParse->pIdxEpr = p;
166356 if( p->pIENext==0 ){
166357 void *pArg = (void*)&pParse->pIdxEpr;
166364 ** Set the reverse-scan order mask to one for all tables in the query
166373 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
166374 SrcItem *pItem = &pWInfo->pTabList->a[ii];
166375 if( !pItem->fg.isCte
166376 || pItem->u2.pCteUse->eM10d!=M10d_Yes
166377 || NEVER(pItem->pSelect==0)
166378 || pItem->pSelect->pOrderBy==0
166380 pWInfo->revMask |= MASKBIT(ii);
166404 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
166408 ** end |-- by sqlite3WhereEnd()
166415 ** scanning through all values on the right-hand side of the IN.
166418 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
166428 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
166438 ** inner loops (or around the "..." if the test occurs within the inner-
166487 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
166491 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
166508 db = pParse->db;
166512 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
166513 if( pOrderBy && pOrderBy->nExpr>=BMS ){
166521 testcase( pTabList->nSrc==BMS );
166522 if( pTabList->nSrc>BMS ){
166532 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
166537 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
166538 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
166541 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
166543 if( db->mallocFailed ){
166548 pWInfo->pParse = pParse;
166549 pWInfo->pTabList = pTabList;
166550 pWInfo->pOrderBy = pOrderBy;
166552 pWInfo->pWhere = pWhere;
166554 pWInfo->pResultSet = pResultSet;
166555 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
166556 pWInfo->nLevel = nTabList;
166557 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
166558 pWInfo->wctrlFlags = wctrlFlags;
166559 pWInfo->iLimit = iAuxArg;
166560 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
166561 pWInfo->pSelect = pSelect;
166562 memset(&pWInfo->nOBSat, 0,
166563 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
166564 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
166565 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
166566 pMaskSet = &pWInfo->sMaskSet;
166567 pMaskSet->n = 0;
166568 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
166570 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
166572 sWLB.pWC = &pWInfo->sWC;
166577 sWLB.pNew->cId = '*';
166583 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
166584 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
166589 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
166593 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
166599 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
166602 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
166606 ** Note that bitmasks are created for all pTabList->nSrc tables in
166608 ** equal to pTabList->nSrc but might be shortened to 1 if the
166613 createMask(pMaskSet, pTabList->a[ii].iCursor);
166614 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
166615 }while( (++ii)<pTabList->nSrc );
166619 for(ii=0; ii<pTabList->nSrc; ii++){
166620 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
166629 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
166630 if( pSelect && pSelect->pLimit ){
166631 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
166633 if( pParse->nErr ) goto whereBeginError;
166635 /* The False-WHERE-Term-Bypass optimization:
166644 ** right-hand side of a LEFT or FULL JOIN.
166647 ** (4) If the expression contains non-deterministic functions
166648 ** that are not within a sub-select. This is not required
166652 ** WHERE random()>0; -- eval random() once per row
166653 ** WHERE (SELECT random())>0; -- eval random() just once overall
166661 for(ii=0; ii<sWLB.pWC->nBase; ii++){
166662 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
166664 if( pT->wtFlags & TERM_VIRTUAL ) continue;
166665 pX = pT->pExpr;
166667 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) );
166668 if( pT->prereqAll==0 /* Conditions (1) and (2) */
166671 && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 )
166673 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL);
166674 pT->wtFlags |= TERM_CODED;
166683 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
166684 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
166686 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
166689 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
166690 pWInfo->pOrderBy = pResultSet;
166713 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
166734 while( pWInfo->pLoops ){
166735 WhereLoop *p = pWInfo->pLoops;
166736 pWInfo->pLoops = p->pNextLoop;
166746 if( db->mallocFailed ) goto whereBeginError;
166747 if( pWInfo->pOrderBy ){
166748 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
166749 if( db->mallocFailed ) goto whereBeginError;
166753 ** the output size by a factor of 8 (LogEst -30).
166755 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){
166757 pWInfo->nRowOut, pWInfo->nRowOut-30));
166758 pWInfo->nRowOut -= 30;
166762 assert( pWInfo->pTabList!=0 );
166763 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
166766 if( pParse->nErr ){
166769 assert( db->mallocFailed==0 );
166772 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
166773 if( pWInfo->nOBSat>0 ){
166774 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
166776 switch( pWInfo->eDistinct ){
166791 for(ii=0; ii<pWInfo->nLevel; ii++){
166792 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
166800 ** This query optimization is factored out into a separate "no-inline"
166803 ** some C-compiler optimizers from in-lining the
166805 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
166808 if( pWInfo->nLevel>=2
166814 nTabList = pWInfo->nLevel;
166821 if( pWInfo->nLevel>=2
166829 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
166834 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
166837 ** to use a one-pass algorithm, determine if this is appropriate.
166839 ** A one-pass approach can be used if the caller has requested one
166843 ** * the caller has indicated that a one-pass approach can be used
166852 ** use a one-pass approach, and this is not set accurately for scans
166855 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
166857 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
166859 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
166862 && !IsVirtual(pTabList->a[0].pTab)
166866 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
166867 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
166871 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
166879 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
166884 pTabItem = &pTabList->a[pLevel->iFrom];
166885 pTab = pTabItem->pTab;
166886 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
166887 pLoop = pLevel->pWLoop;
166888 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
166892 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
166894 int iCur = pTabItem->iCursor;
166900 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
166902 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
166905 if( pWInfo->eOnePass!=ONEPASS_OFF ){
166907 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
166909 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
166910 assert( pTabItem->iCursor==pLevel->iTabCur );
166911 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
166912 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
166913 if( pWInfo->eOnePass==ONEPASS_OFF
166914 && pTab->nCol<BMS
166915 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
166916 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
166921 Bitmask b = pTabItem->colUsed;
166924 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
166925 assert( n<=pTab->nCol );
166928 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid)==0 ){
166936 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
166937 (const u8*)&pTabItem->colUsed, P4_INT64);
166940 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
166942 if( pLoop->wsFlags & WHERE_INDEXED ){
166943 Index *pIx = pLoop->u.btree.pIndex;
166947 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
166951 /* This is one term of an OR-optimization using the PRIMARY KEY of a
166953 iIndexCur = pLevel->iTabCur;
166955 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
166956 Index *pJ = pTabItem->pTab->pIndex;
166961 pJ = pJ->pNext;
166964 pWInfo->aiCurOnePass[1] = iIndexCur;
166969 iIndexCur = pParse->nTab++;
166970 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
166973 if( pIx->pPartIdxWhere && (pTabItem->fg.jointype & JT_RIGHT)==0 ){
166975 pParse, pIx, pIx->pPartIdxWhere, 0, iIndexCur, pTabItem
166979 pLevel->iIdxCur = iIndexCur;
166981 assert( pIx->pSchema==pTab->pSchema );
166984 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
166986 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
166987 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
166988 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
166989 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
166990 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
166991 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
166995 VdbeComment((v, "%s", pIx->zName));
167000 for(ii=0; ii<pIx->nColumn; ii++){
167001 jj = pIx->aiColumn[ii];
167004 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
167014 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
167015 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
167017 WhereRightJoin *pRJ = pLevel->pRJ;
167018 pRJ->iMatch = pParse->nTab++;
167019 pRJ->regBloom = ++pParse->nMem;
167020 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
167021 pRJ->regReturn = ++pParse->nMem;
167022 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
167023 assert( pTab==pTabItem->pTab );
167026 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
167027 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
167029 pInfo->aColl[0] = 0;
167030 pInfo->aSortFlags[0] = 0;
167035 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
167038 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
167042 pWInfo->nOBSat = 0;
167043 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
167046 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
167047 if( db->mallocFailed ) goto whereBeginError;
167057 if( pParse->nErr ) goto whereBeginError;
167058 pLevel = &pWInfo->a[ii];
167059 wsFlags = pLevel->pWLoop->wsFlags;
167060 pSrc = &pTabList->a[pLevel->iFrom];
167061 if( pSrc->fg.isMaterialized ){
167062 if( pSrc->fg.isCorrelated ){
167063 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
167066 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
167070 assert( pTabList == pWInfo->pTabList );
167074 constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel);
167079 if( db->mallocFailed ) goto whereBeginError;
167084 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
167086 pWInfo->iContinue = pLevel->addrCont;
167093 VdbeModuleComment((v, "Begin WHERE-core"));
167094 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
167100 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
167113 # define OpcodeRewriteTrace(D,K,P) /* no-op */
167121 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
167133 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
167134 if( pOp->p1!=iCur ) continue;
167135 if( pOp->opcode==OP_Close ) return 0;
167136 if( pOp->opcode==OP_OpenRead ) return 1;
167137 if( pOp->opcode==OP_OpenWrite ) return 1;
167138 if( pOp->opcode==OP_OpenDup ) return 1;
167139 if( pOp->opcode==OP_OpenAutoindex ) return 1;
167140 if( pOp->opcode==OP_OpenEphemeral ) return 1;
167151 Parse *pParse = pWInfo->pParse;
167152 Vdbe *v = pParse->pVdbe;
167156 SrcList *pTabList = pWInfo->pTabList;
167157 sqlite3 *db = pParse->db;
167163 VdbeModuleComment((v, "End WHERE-core"));
167164 for(i=pWInfo->nLevel-1; i>=0; i--){
167166 pLevel = &pWInfo->a[i];
167167 if( pLevel->pRJ ){
167170 WhereRightJoin *pRJ = pLevel->pRJ;
167171 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
167172 pLevel->addrCont = 0;
167173 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
167174 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
167178 pLoop = pLevel->pWLoop;
167179 if( pLevel->op!=OP_Noop ){
167184 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
167185 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
167186 && (pLoop->wsFlags & WHERE_INDEXED)!=0
167187 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
167188 && (n = pLoop->u.btree.nDistinctCol)>0
167189 && pIdx->aiRowLogEst[n]>=36
167191 int r1 = pParse->nMem+1;
167194 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
167196 pParse->nMem += n+1;
167197 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
167198 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
167201 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
167205 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
167206 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
167207 sqlite3VdbeChangeP5(v, pLevel->p5);
167209 VdbeCoverageIf(v, pLevel->op==OP_Next);
167210 VdbeCoverageIf(v, pLevel->op==OP_Prev);
167211 VdbeCoverageIf(v, pLevel->op==OP_VNext);
167212 if( pLevel->regBignull ){
167213 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
167214 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
167220 }else if( pLevel->addrCont ){
167221 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
167223 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
167226 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
167227 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
167228 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
167229 || pParse->db->mallocFailed );
167230 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
167231 if( pIn->eEndLoopOp!=OP_Noop ){
167232 if( pIn->nPrefix ){
167234 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
167235 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
167236 if( pLevel->iLeftJoin ){
167237 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
167242 ** return the null-row. So, if the cursor is not open yet,
167245 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
167250 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
167252 pIn->iBase, pIn->nPrefix);
167258 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
167261 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
167263 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
167264 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
167266 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
167269 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
167270 if( pLevel->pRJ ){
167271 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
167274 if( pLevel->addrSkip ){
167275 sqlite3VdbeGoto(v, pLevel->addrSkip);
167276 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
167277 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
167278 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
167281 if( pLevel->addrLikeRep ){
167282 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
167283 pLevel->addrLikeRep);
167287 if( pLevel->iLeftJoin ){
167288 int ws = pLoop->wsFlags;
167289 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
167292 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
167293 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
167296 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
167299 Index *pIx = pLevel->u.pCoveringIdx;
167300 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
167301 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
167304 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
167306 if( pLevel->op==OP_Return ){
167307 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
167309 sqlite3VdbeGoto(v, pLevel->addrFirst);
167313 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
167314 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
167317 assert( pWInfo->nLevel<=pTabList->nSrc );
167318 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
167322 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
167323 Table *pTab = pTabItem->pTab;
167325 pLoop = pLevel->pWLoop;
167331 if( pLevel->pRJ ){
167336 /* For a co-routine, change all OP_Column references to the table of
167337 ** the co-routine into OP_Copy of result contained in a register.
167340 if( pTabItem->fg.viaCoroutine ){
167341 testcase( pParse->db->mallocFailed );
167342 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
167343 pTabItem->regResult, 0);
167358 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
167359 pIdx = pLoop->u.btree.pIndex;
167360 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
167361 pIdx = pLevel->u.pCoveringIdx;
167364 && !db->mallocFailed
167366 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
167369 last = pWInfo->iEndWhere;
167371 if( pIdx->bHasExpr ){
167372 IndexedExpr *p = pParse->pIdxEpr;
167374 if( p->iIdxCur==pLevel->iIdxCur ){
167377 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
167378 p->iIdxCur, p->iIdxCol);
167379 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
167382 p->iDataCur = -1;
167383 p->iIdxCur = -1;
167385 p = p->pIENext;
167388 k = pLevel->addrBody + 1;
167390 if( db->flags & SQLITE_VdbeAddopTrace ){
167391 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
167392 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
167395 pOp = sqlite3VdbeGetOp(v, k - 1);
167396 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
167397 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
167398 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
167401 pLastOp = pOp + (last - k);
167404 if( pOp->p1!=pLevel->iTabCur ){
167405 /* no-op */
167406 }else if( pOp->opcode==OP_Column
167408 || pOp->opcode==OP_Offset
167411 int x = pOp->p2;
167412 assert( pIdx->pTable==pTab );
167414 if( pOp->opcode==OP_Offset ){
167420 x = pPk->aiColumn[x];
167428 pOp->p2 = x;
167429 pOp->p1 = pLevel->iIdxCur;
167433 ** reference. Verify that this is harmless - that the
167437 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
167438 || cursorIsOpen(v,pOp->p1,k)
167439 || pOp->opcode==OP_Offset
167442 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
167443 || cursorIsOpen(v,pOp->p1,k)
167447 }else if( pOp->opcode==OP_Rowid ){
167448 pOp->p1 = pLevel->iIdxCur;
167449 pOp->opcode = OP_IdxRowid;
167451 }else if( pOp->opcode==OP_IfNullRow ){
167452 pOp->p1 = pLevel->iIdxCur;
167460 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
167468 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
167472 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
167474 pParse->withinRJSubrtn -= nRJ;
167523 ** the sub-query.
167528 ** select-list and ORDER BY expressions in the parent query are
167529 ** selected by the sub-query. For the purposes of the transformation,
167561 ** sub-query, which is always implemented as a co-routine. It then calls
167566 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
167567 ** When the sub-routine is invoked:
167569 ** * The results of all window-functions for the row are stored
167581 ** BUILT-IN WINDOW FUNCTIONS
167583 ** This implementation features the following built-in window functions:
167597 ** These are the same built-in window functions supported by Postgres.
167600 ** be implemented using an API, built-in window functions are much more
167603 ** As such, some built-in window functions use the same API as aggregate
167614 ** As well as some of the built-in window functions, aggregate window
167621 ** Implementation of built-in window function row_number(). Assumes that the
167652 ** Implementation of built-in window function dense_rank(). Assumes that
167664 if( p ) p->nStep = 1;
167672 if( p->nStep ){
167673 p->nValue++;
167674 p->nStep = 0;
167676 sqlite3_result_int64(pCtx, p->nValue);
167681 ** Implementation of built-in window function nth_value(). This
167682 ** implementation is used in "slow mode" only - when the EXCLUDE clause
167713 p->nStep++;
167714 if( iVal==p->nStep ){
167715 p->pValue = sqlite3_value_dup(apArg[0]);
167716 if( !p->pValue ){
167727 pCtx, "second argument to nth_value must be a positive integer", -1
167733 if( p && p->pValue ){
167734 sqlite3_result_value(pCtx, p->pValue);
167735 sqlite3_value_free(p->pValue);
167736 p->pValue = 0;
167749 if( p && p->pValue==0 ){
167750 p->pValue = sqlite3_value_dup(apArg[0]);
167751 if( !p->pValue ){
167761 if( p && p->pValue ){
167762 sqlite3_result_value(pCtx, p->pValue);
167763 sqlite3_value_free(p->pValue);
167764 p->pValue = 0;
167771 ** Implementation of built-in window function rank(). Assumes that
167784 p->nStep++;
167785 if( p->nValue==0 ){
167786 p->nValue = p->nStep;
167796 sqlite3_result_int64(pCtx, p->nValue);
167797 p->nValue = 0;
167802 ** Implementation of built-in window function percent_rank(). Assumes that
167817 p->nTotal++;
167829 p->nStep++;
167835 p->nValue = p->nStep;
167836 if( p->nTotal>1 ){
167837 double r = (double)p->nValue / (double)(p->nTotal-1);
167847 ** Implementation of built-in window function cume_dist(). Assumes that
167862 p->nTotal++;
167874 p->nStep++;
167880 double r = (double)(p->nStep) / (double)(p->nTotal);
167910 if( p->nTotal==0 ){
167911 p->nParam = sqlite3_value_int64(apArg[0]);
167912 if( p->nParam<=0 ){
167914 pCtx, "argument of ntile must be a positive integer", -1
167918 p->nTotal++;
167930 p->iRow++;
167935 if( p && p->nParam>0 ){
167936 int nSize = (p->nTotal / p->nParam);
167938 sqlite3_result_int64(pCtx, p->iRow+1);
167940 i64 nLarge = p->nTotal - p->nParam*nSize;
167942 i64 iRow = p->iRow;
167944 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
167949 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
167976 sqlite3_value_free(p->pVal);
167977 p->pVal = sqlite3_value_dup(apArg[0]);
167978 if( p->pVal==0 ){
167981 p->nVal++;
167995 p->nVal--;
167996 if( p->nVal==0 ){
167997 sqlite3_value_free(p->pVal);
167998 p->pVal = 0;
168005 if( p && p->pVal ){
168006 sqlite3_result_value(pCtx, p->pVal);
168012 if( p && p->pVal ){
168013 sqlite3_result_value(pCtx, p->pVal);
168014 sqlite3_value_free(p->pVal);
168015 p->pVal = 0;
168020 ** Static names for the built-in window function names. These static
168025 ** if( pFuncDef->zName==row_valueName ){ ... }
168040 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
168041 ** for built-in window functions that never call those interfaces.
168058 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
168069 ** no-op routines for their methods */
168087 ** Register those built-in window functions that are not also aggregates.
168112 for(p=pList; p; p=p->pNextWin){
168113 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
168134 ** * If the function is a built-in window function that requires the
168135 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
168144 if( pWin->zName && pWin->eFrmType==0 ){
168145 Window *p = windowFind(pParse, pList, pWin->zName);
168147 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
168148 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
168149 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
168150 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
168151 pWin->eStart = p->eStart;
168152 pWin->eEnd = p->eEnd;
168153 pWin->eFrmType = p->eFrmType;
168154 pWin->eExclude = p->eExclude;
168158 if( (pWin->eFrmType==TK_RANGE)
168159 && (pWin->pStart || pWin->pEnd)
168160 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
168166 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
168167 sqlite3 *db = pParse->db;
168168 if( pWin->pFilter ){
168190 if( pFunc->zName==aUp[i].zFunc ){
168191 sqlite3ExprDelete(db, pWin->pStart);
168192 sqlite3ExprDelete(db, pWin->pEnd);
168193 pWin->pEnd = pWin->pStart = 0;
168194 pWin->eFrmType = aUp[i].eFrmType;
168195 pWin->eStart = aUp[i].eStart;
168196 pWin->eEnd = aUp[i].eEnd;
168197 pWin->eExclude = 0;
168198 if( pWin->eStart==TK_FOLLOWING ){
168199 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
168206 pWin->pWFunc = pFunc;
168219 Select *pSubSelect; /* Current sub-select, if any */
168224 ** this function appends to the output expression-list and updates
168228 struct WindowRewrite *p = pWalker->u.pRewrite;
168229 Parse *pParse = pWalker->pParse;
168231 assert( p->pWin!=0 );
168233 /* If this function is being called from within a scalar sub-select
168237 ** to the scalar sub-select. */
168238 if( p->pSubSelect ){
168239 if( pExpr->op!=TK_COLUMN ){
168242 int nSrc = p->pSrc->nSrc;
168245 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
168251 switch( pExpr->op ){
168258 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
168259 if( pExpr->y.pWin==pWin ){
168260 assert( pWin->pOwner==pExpr );
168270 int iCol = -1;
168271 if( pParse->db->mallocFailed ) return WRC_Abort;
168272 if( p->pSub ){
168274 for(i=0; i<p->pSub->nExpr; i++){
168275 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
168282 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
168283 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
168284 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
168286 if( p->pSub ){
168287 int f = pExpr->flags & EP_Collate;
168290 sqlite3ExprDelete(pParse->db, pExpr);
168294 pExpr->op = TK_COLUMN;
168295 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
168296 pExpr->iTable = p->pWin->iEphCsr;
168297 pExpr->y.pTab = p->pTab;
168298 pExpr->flags = f;
168300 if( pParse->db->mallocFailed ) return WRC_Abort;
168304 default: /* no-op */
168311 struct WindowRewrite *p = pWalker->u.pRewrite;
168312 Select *pSave = p->pSubSelect;
168316 p->pSubSelect = pSelect;
168318 p->pSubSelect = pSave;
168325 ** Iterate through each expression in expression-list pEList. For each:
168332 ** Append the node to output expression-list (*ppSub). And replace it
168333 ** with a TK_COLUMN that reads the (N-1)th element of table
168334 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
168343 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
168368 ** Append a copy of each expression in expression-list pAppend to
168379 int nInit = pList ? pList->nExpr : 0;
168380 for(i=0; i<pAppend->nExpr; i++){
168381 sqlite3 *db = pParse->db;
168382 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
168383 if( db->mallocFailed ){
168392 pSub->op = TK_NULL;
168393 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
168394 pSub->u.zToken = 0;
168398 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
168407 ** then we have to increase the Expr->op2 values of those nodes
168413 if( pExpr->op==TK_AGG_FUNCTION
168414 && pExpr->op2>=pWalker->walkerDepth
168416 pExpr->op2++;
168422 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
168424 sqlite3ErrorMsg(pWalker->pParse,
168425 "misuse of aggregate: %s()", pExpr->u.zToken);
168432 ** any SQL window functions, this function is a no-op. Otherwise, it
168439 if( p->pWin
168440 && p->pPrior==0
168441 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
168445 sqlite3 *db = pParse->db;
168447 SrcList *pSrc = p->pSrc;
168448 Expr *pWhere = p->pWhere;
168449 ExprList *pGroupBy = p->pGroupBy;
168450 Expr *pHaving = p->pHaving;
168453 ExprList *pSublist = 0; /* Expression list for sub-query */
168454 Window *pMWin = p->pWin; /* Main window object */
168459 u32 selFlags = p->selFlags;
168467 if( (p->selFlags & SF_Aggregate)==0 ){
168470 sqlite3WalkExprList(&w, p->pOrderBy);
168473 p->pSrc = 0;
168474 p->pWhere = 0;
168475 p->pGroupBy = 0;
168476 p->pHaving = 0;
168477 p->selFlags &= ~SF_Aggregate;
168478 p->selFlags |= SF_WinRewrite;
168480 /* Create the ORDER BY clause for the sub-select. This is the concatenation
168483 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
168484 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
168485 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
168486 int nSave = pSort->nExpr;
168487 pSort->nExpr = p->pOrderBy->nExpr;
168488 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
168489 sqlite3ExprListDelete(db, p->pOrderBy);
168490 p->pOrderBy = 0;
168492 pSort->nExpr = nSave;
168498 pMWin->iEphCsr = pParse->nTab++;
168499 pParse->nTab += 3;
168501 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
168502 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
168503 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
168506 ** sub-select expression list. They are required to figure out where
168508 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
168509 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
168512 ** sub-select expression list. Also allocate two registers for each
168513 ** window function - one for the accumulator, another for interim
168515 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168517 assert( ExprUseXList(pWin->pOwner) );
168518 assert( pWin->pWFunc!=0 );
168519 pArgs = pWin->pOwner->x.pList;
168520 if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
168522 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168523 pWin->bExprArgs = 1;
168525 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168528 if( pWin->pFilter ){
168529 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
168532 pWin->regAccum = ++pParse->nMem;
168533 pWin->regResult = ++pParse->nMem;
168534 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
168553 ("New window-function subquery in FROM clause of (%u/%p)\n",
168554 p->selId, p));
168555 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
168556 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
168559 if( p->pSrc ){
168561 p->pSrc->a[0].pSelect = pSub;
168562 p->pSrc->a[0].fg.isCorrelated = 1;
168563 sqlite3SrcListAssignCursors(pParse, p->pSrc);
168564 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
168566 pSub->selFlags |= (selFlags & SF_Aggregate);
168569 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
168574 pTab->tabFlags |= TF_Ephemeral;
168575 p->pSrc->a[0].pTab = pTab;
168586 if( db->mallocFailed ) rc = SQLITE_NOMEM;
168590 ** result-set or ORDER BY clause of the SELECT statement p. */
168594 assert( rc==SQLITE_OK || pParse->nErr!=0 );
168603 if( p->ppThis ){
168604 *p->ppThis = p->pNextWin;
168605 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
168606 p->ppThis = 0;
168616 sqlite3ExprDelete(db, p->pFilter);
168617 sqlite3ExprListDelete(db, p->pPartition);
168618 sqlite3ExprListDelete(db, p->pOrderBy);
168619 sqlite3ExprDelete(db, p->pEnd);
168620 sqlite3ExprDelete(db, p->pStart);
168621 sqlite3DbFree(db, p->zName);
168622 sqlite3DbFree(db, p->zBase);
168632 Window *pNext = p->pNextWin;
168640 ** value should be a non-negative integer. If the value is not a
168641 ** constant, change it to NULL. The fact that it is then a non-negative
168648 sqlite3ExprDelete(pParse->db, pExpr);
168649 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
168704 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168706 pWin->eFrmType = eType;
168707 pWin->eStart = eStart;
168708 pWin->eEnd = eEnd;
168709 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
168712 pWin->eExclude = eExclude;
168713 pWin->bImplicitFrame = bImplicitFrame;
168714 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
168715 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
168719 sqlite3ExprDelete(pParse->db, pEnd);
168720 sqlite3ExprDelete(pParse->db, pStart);
168726 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
168727 ** equivalent nul-terminated string.
168737 pWin->pPartition = pPartition;
168738 pWin->pOrderBy = pOrderBy;
168740 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
168743 sqlite3ExprListDelete(pParse->db, pPartition);
168744 sqlite3ExprListDelete(pParse->db, pOrderBy);
168752 ** stored in the linked list starting at pWin->pNextWin. This function
168757 if( pWin->zBase ){
168758 sqlite3 *db = pParse->db;
168759 Window *pExist = windowFind(pParse, pList, pWin->zBase);
168763 if( pWin->pPartition ){
168765 }else if( pExist->pOrderBy && pWin->pOrderBy ){
168767 }else if( pExist->bImplicitFrame==0 ){
168772 "cannot override %s of window: %s", zErr, pWin->zBase
168775 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
168776 if( pExist->pOrderBy ){
168777 assert( pWin->pOrderBy==0 );
168778 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
168780 sqlite3DbFree(db, pWin->zBase);
168781 pWin->zBase = 0;
168792 assert( p->op==TK_FUNCTION );
168795 p->y.pWin = pWin;
168797 pWin->pOwner = p;
168798 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
168804 sqlite3WindowDelete(pParse->db, pWin);
168809 ** Possibly link window pWin into the list at pSel->pWin (window functions
168816 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
168817 pWin->pNextWin = pSel->pWin;
168818 if( pSel->pWin ){
168819 pSel->pWin->ppThis = &pWin->pNextWin;
168821 pSel->pWin = pWin;
168822 pWin->ppThis = &pSel->pWin;
168824 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
168825 pSel->selFlags |= SF_MultiPart;
168844 if( p1->eFrmType!=p2->eFrmType ) return 1;
168845 if( p1->eStart!=p2->eStart ) return 1;
168846 if( p1->eEnd!=p2->eEnd ) return 1;
168847 if( p1->eExclude!=p2->eExclude ) return 1;
168848 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
168849 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
168850 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
168853 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
168857 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
168867 ** to begin iterating through the sub-query results. It is used to allocate
168871 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
168872 Window *pMWin = pSelect->pWin;
168876 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
168877 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
168878 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
168879 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
168883 if( pMWin->pPartition ){
168884 int nExpr = pMWin->pPartition->nExpr;
168885 pMWin->regPart = pParse->nMem+1;
168886 pParse->nMem += nExpr;
168887 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
168890 pMWin->regOne = ++pParse->nMem;
168891 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
168893 if( pMWin->eExclude ){
168894 pMWin->regStartRowid = ++pParse->nMem;
168895 pMWin->regEndRowid = ++pParse->nMem;
168896 pMWin->csrApp = pParse->nTab++;
168897 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
168898 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
168899 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
168903 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
168904 FuncDef *p = pWin->pWFunc;
168905 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
168915 assert( ExprUseXList(pWin->pOwner) );
168916 pList = pWin->pOwner->x.pList;
168918 pWin->csrApp = pParse->nTab++;
168919 pWin->regApp = pParse->nMem+1;
168920 pParse->nMem += 3;
168921 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
168922 assert( pKeyInfo->aSortFlags[0]==0 );
168923 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
168925 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
168927 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
168929 else if( p->zName==nth_valueName || p->zName==first_valueName ){
168930 /* Allocate two registers at pWin->regApp. These will be used to
168932 pWin->regApp = pParse->nMem+1;
168933 pWin->csrApp = pParse->nTab++;
168934 pParse->nMem += 2;
168935 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
168937 else if( p->zName==leadName || p->zName==lagName ){
168938 pWin->csrApp = pParse->nTab++;
168939 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
168954 ** code to check that the value is a non-negative integer and throws an
168959 "frame starting offset must be a non-negative integer",
168960 "frame ending offset must be a non-negative integer",
168962 "frame starting offset must be a non-negative number",
168963 "frame ending offset must be a non-negative number",
169001 ** Return the number of arguments passed to the window-function associated
169006 assert( ExprUseXList(pWin->pOwner) );
169007 pList = pWin->pOwner->x.pList;
169008 return (pList ? pList->nExpr : 0);
169031 ** in an array of accumulator registers - one for each window function
169038 ** order to reduce memory requirements - it would always be safe just
169053 ** Consider a window-frame similar to the following:
169073 ** Depending on the window-frame in question, all three cursors may not
169101 Window *pMWin = p->pMWin;
169102 ExprList *pOrderBy = pMWin->pOrderBy;
169104 Vdbe *v = sqlite3GetVdbe(p->pParse);
169105 ExprList *pPart = pMWin->pPartition;
169106 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
169108 for(i=0; i<pOrderBy->nExpr; i++){
169116 ** xInverse (if bInverse is non-zero) for each window function in the
169117 ** linked list starting at pMWin. Or, for built-in window functions
169128 ** already populated with all columns from the current row of the sub-query.
169130 ** If argument regPartSize is non-zero, then it is a register containing the
169140 Parse *pParse = p->pParse;
169143 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169144 FuncDef *pFunc = pWin->pWFunc;
169146 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
169149 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
169156 if( i!=1 || pFunc->zName!=nth_valueName ){
169157 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
169159 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
169164 if( pMWin->regStartRowid==0
169165 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
169166 && (pWin->eStart!=TK_UNBOUNDED)
169171 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
169172 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
169173 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
169174 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
169176 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
169178 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
169179 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
169182 }else if( pWin->regApp ){
169183 assert( pFunc->zName==nth_valueName
169184 || pFunc->zName==first_valueName
169187 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
169188 }else if( pFunc->xSFunc!=noopStepFunc ){
169190 if( pWin->pFilter ){
169192 assert( ExprUseXList(pWin->pOwner) );
169193 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
169194 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
169196 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
169202 if( pWin->bExprArgs ){
169206 assert( ExprUseXList(pWin->pOwner) );
169207 nArg = pWin->pOwner->x.pList->nExpr;
169209 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
169213 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
169214 pOp->p1 = csr;
169218 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
169221 assert( ExprUseXList(pWin->pOwner) );
169222 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
169226 bInverse, regArg, pWin->regAccum);
169229 if( pWin->bExprArgs ){
169247 ** pMWin. Or, for built-in window-functions that do not use the standard
169251 Parse *pParse = p->pParse;
169252 Window *pMWin = p->pMWin;
169256 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169257 if( pMWin->regStartRowid==0
169258 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
169259 && (pWin->eStart!=TK_UNBOUNDED)
169261 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
169262 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
169264 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
169265 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
169266 }else if( pWin->regApp ){
169267 assert( pMWin->regStartRowid==0 );
169271 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
169272 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
169273 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
169274 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
169276 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
169277 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
169285 ** p->pMWin list by doing a full scan of the current window frame. Store the
169291 Parse *pParse = p->pParse;
169292 Window *pMWin = p->pMWin;
169293 Vdbe *v = p->pVdbe;
169309 csr = pMWin->csrApp;
169310 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
169322 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
169323 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
169325 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169326 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
169329 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
169333 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
169336 if( pMWin->eExclude==TK_CURRENT ){
169339 }else if( pMWin->eExclude!=TK_NO ){
169344 if( pMWin->pOrderBy ){
169345 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
169347 if( pMWin->eExclude==TK_TIES ){
169364 windowAggStep(p, pMWin, csr, 0, p->regArg);
169369 sqlite3VdbeJumpHere(v, addrNext-1);
169383 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
169387 ** for per-row processing is only generated for the following built-in window
169396 Window *pMWin = p->pMWin;
169397 Vdbe *v = p->pVdbe;
169399 if( pMWin->regStartRowid ){
169402 Parse *pParse = p->pParse;
169405 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169406 FuncDef *pFunc = pWin->pWFunc;
169407 assert( ExprUseXList(pWin->pOwner) );
169408 if( pFunc->zName==nth_valueName
169409 || pFunc->zName==first_valueName
169411 int csr = pWin->csrApp;
169414 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
169416 if( pFunc->zName==nth_valueName ){
169417 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
169422 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
169423 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
169427 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
169431 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
169432 int nArg = pWin->pOwner->x.pList->nExpr;
169433 int csr = pWin->csrApp;
169436 int iEph = pMWin->iEphCsr;
169439 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
169441 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
169445 int val = (pFunc->zName==leadName ? 1 : -1);
169448 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
169450 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
169457 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
169463 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
169469 ** any equivalent initialization required by any built-in window functions
169477 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169478 FuncDef *pFunc = pWin->pWFunc;
169479 assert( pWin->regAccum );
169480 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
169482 if( pMWin->regStartRowid==0 ){
169483 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
169484 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
169485 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
169488 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
169489 assert( pWin->eStart!=TK_UNBOUNDED );
169490 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
169491 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
169495 regArg = pParse->nMem+1;
169496 pParse->nMem += nArg;
169506 if( pMWin->regStartRowid ) return 1;
169507 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
169508 FuncDef *pFunc = pWin->pWFunc;
169509 if( (pFunc->zName==nth_valueName)
169510 || (pFunc->zName==first_valueName)
169511 || (pFunc->zName==leadName)
169512 || (pFunc->zName==lagName)
169522 ** pOrderBy->nExpr. This function generates code to compare the two
169539 int nVal = pOrderBy->nExpr;
169547 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
169562 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
169564 ** If the sort-order for the ORDER BY term in the window is DESC, then the
169566 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
169570 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
169580 int regVal, /* Register containing non-negative number */
169584 Parse *pParse = p->pParse;
169586 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
169589 int regString = ++pParse->nMem; /* Reg. for constant value '' */
169595 /* Read the peer-value from each cursor into a register */
169600 assert( pOrderBy && pOrderBy->nExpr==1 );
169601 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
169611 reg1, (arith==OP_Add ? "+" : "-"), regVal,
169634 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
169649 default: assert( op==OP_Lt ); /* no-op */ break;
169660 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
169663 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
169666 ** reg1 = reg1 +/- regVal
169669 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
169686 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
169715 Parse *pParse = p->pParse;
169716 Window *pMWin = p->pMWin;
169718 Vdbe *v = p->pVdbe;
169720 int bPeer = (pMWin->eFrmType!=TK_ROWS);
169725 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
169727 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
169733 if( pMWin->eFrmType==TK_RANGE ){
169737 if( pMWin->eStart==TK_FOLLOWING ){
169739 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
169743 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
169748 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
169757 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
169768 if( pMWin->eStart==pMWin->eEnd && regCountdown
169769 && pMWin->eFrmType==TK_RANGE
169774 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
169775 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
169778 }else if( p->regRowid ){
169779 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
169780 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
169785 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
169790 csr = p->current.csr;
169791 reg = p->current.reg;
169796 csr = p->start.csr;
169797 reg = p->start.reg;
169798 if( pMWin->regStartRowid ){
169799 assert( pMWin->regEndRowid );
169800 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
169802 windowAggStep(p, pMWin, csr, 1, p->regArg);
169808 csr = p->end.csr;
169809 reg = p->end.reg;
169810 if( pMWin->regStartRowid ){
169811 assert( pMWin->regEndRowid );
169812 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
169814 windowAggStep(p, pMWin, csr, 0, p->regArg);
169819 if( op==p->eDelete ){
169837 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
169840 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
169862 pNew->zName = sqlite3DbStrDup(db, p->zName);
169863 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
169864 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
169865 pNew->pWFunc = p->pWFunc;
169866 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
169867 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
169868 pNew->eFrmType = p->eFrmType;
169869 pNew->eEnd = p->eEnd;
169870 pNew->eStart = p->eStart;
169871 pNew->eExclude = p->eExclude;
169872 pNew->regResult = p->regResult;
169873 pNew->regAccum = p->regAccum;
169874 pNew->iArgCol = p->iArgCol;
169875 pNew->iEphCsr = p->iEphCsr;
169876 pNew->bExprArgs = p->bExprArgs;
169877 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
169878 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
169879 pNew->pOwner = pOwner;
169880 pNew->bImplicitFrame = p->bImplicitFrame;
169895 for(pWin=p; pWin; pWin=pWin->pNextWin){
169898 pp = &((*pp)->pNextWin);
169914 sqlite3 *db = pParse->db;
169916 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
169928 ** and invoke the sub-routine at instruction addrGosub once for each row.
169958 ** if( (regEnd--)<=0 ){
169960 ** if( (regStart--)<=0 ){
169971 ** if( (regStart--)<=0 ){
169977 ** The pseudo-code above uses the following shorthand:
169992 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
170011 ** if( (regEnd--)<=0 ){
170015 ** if( (regStart--)<=0 ){
170021 ** if( (regEnd--)<=0 ){
170037 ** regStart = regEnd - <expr1>
170040 ** if( (regEnd--)<=0 ){
170043 ** if( (regStart--)<=0 ){
170051 ** if( (regEnd--)<=0 ){
170055 ** if( (regStart--)<=0 ){
170067 ** This is optimized of course - branches that will never be taken and
170088 ** if( (regStart--)<=0 ){
170104 ** To handle this case, the pseudo-code programs depicted above are modified
170124 ** of the outer loop - the one started by sqlite3WhereBegin().
170266 Window *pMWin = p->pWin;
170267 ExprList *pOrderBy = pMWin->pOrderBy;
170270 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
170271 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
170282 WindowCodeArg s; /* Context object for sub-routines */
170287 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
170288 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
170290 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
170291 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
170293 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
170294 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
170295 || pMWin->eExclude==TK_NO
170307 s.current.csr = pMWin->iEphCsr;
170313 ** are four options - they may never be deleted (eDelete==0), they may
170318 switch( pMWin->eStart ){
170320 if( pMWin->eFrmType!=TK_RANGE
170321 && windowExprGtZero(pParse, pMWin->pStart)
170328 if( pMWin->eEnd==TK_PRECEDING ){
170329 if( pMWin->eFrmType!=TK_RANGE
170330 && windowExprGtZero(pParse, pMWin->pEnd)
170344 /* Allocate registers for the array of values from the sub-query, the
170347 regNew = pParse->nMem+1;
170348 pParse->nMem += nInput;
170349 regRecord = ++pParse->nMem;
170350 s.regRowid = ++pParse->nMem;
170355 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
170356 regStart = ++pParse->nMem;
170358 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
170359 regEnd = ++pParse->nMem;
170365 if( pMWin->eFrmType!=TK_ROWS ){
170366 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
170367 regNewPeer = regNew + pMWin->nBufferCol;
170368 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
170369 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
170370 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
170371 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
170372 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
170375 /* Load the column values for the row returned by the sub-select
170388 if( pMWin->pPartition ){
170390 ExprList *pPart = pMWin->pPartition;
170391 int nPart = pPart->nExpr;
170392 int regNewPart = regNew + pMWin->nBufferCol;
170395 regFlushPart = ++pParse->nMem;
170396 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
170402 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
170408 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
170415 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
170416 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
170419 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
170420 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
170423 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
170424 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
170435 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
170436 assert( pMWin->eEnd==TK_FOLLOWING );
170440 if( pMWin->eStart!=TK_UNBOUNDED ){
170446 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
170447 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
170448 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
170449 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
170460 if( pMWin->eStart==TK_FOLLOWING ){
170462 if( pMWin->eEnd!=TK_UNBOUNDED ){
170463 if( pMWin->eFrmType==TK_RANGE ){
170477 if( pMWin->eEnd==TK_PRECEDING ){
170478 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
170486 if( pMWin->eEnd!=TK_UNBOUNDED ){
170487 if( pMWin->eFrmType==TK_RANGE ){
170517 if( pMWin->pPartition ){
170525 if( pMWin->eEnd==TK_PRECEDING ){
170526 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
170530 }else if( pMWin->eStart==TK_FOLLOWING ){
170536 if( pMWin->eFrmType==TK_RANGE ){
170541 if( pMWin->eEnd==TK_UNBOUNDED ){
170546 assert( pMWin->eEnd==TK_FOLLOWING );
170571 if( pMWin->pPartition ){
170572 if( pMWin->regStartRowid ){
170573 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
170574 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
170589 ** 2001-09-15
170603 ** That input file is processed by Lemon to generate a C-language
170605 ** this comment as part of the translated C-code. Edits should be made
170612 ** Disable all error recovery processing in the parser push-down
170665 sqlite3 *db = pParse->db;
170666 pParse->disableLookaside++;
170686 sqlite3ExprListDelete(pParse->db, pOrderBy);
170687 sqlite3ExprDelete(pParse->db, pLimit);
170693 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
170699 if( p->pPrior ){
170703 pLoop->pNext = pNext;
170704 pLoop->selFlags |= SF_Compound;
170706 pLoop = pLoop->pPrior;
170709 if( pLoop->pOrderBy || pLoop->pLimit ){
170711 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
170712 sqlite3SelectOpName(pNext->op));
170716 if( (p->selFlags & SF_MultiValue)==0 &&
170717 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
170730 pSelect->pWith = pWith;
170733 sqlite3WithDelete(pParse->db, pWith);
170741 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
170744 p->op = (u8)op;
170745 p->affExpr = 0;
170746 p->flags = EP_Leaf;
170748 /* p->iAgg = -1; // Not required */
170749 p->pLeft = p->pRight = 0;
170750 p->pAggInfo = 0;
170751 memset(&p->x, 0, sizeof(p->x));
170752 memset(&p->y, 0, sizeof(p->y));
170753 p->op2 = 0;
170754 p->iTable = 0;
170755 p->iColumn = 0;
170756 p->u.zToken = (char*)&p[1];
170757 memcpy(p->u.zToken, t.z, t.n);
170758 p->u.zToken[t.n] = 0;
170759 p->w.iOfst = (int)(t.z - pParse->zTail);
170760 if( sqlite3Isquote(p->u.zToken[0]) ){
170764 p->nHeight = 1;
170777 sqlite3 *db = pParse->db;
170778 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
170779 pA->op = (u8)op;
170780 sqlite3ExprDelete(db, pA->pRight);
170781 pA->pRight = 0;
170799 && pParse->db->init.busy==0
170802 pIdToken->n, pIdToken->z);
171005 ** that represent terminal and non-terminal symbols.
171011 ** (also known as: "terminal symbols") have fall-back
171015 ** YYACTIONTYPE is the data type used for "action codes" - numbers
171020 ** value associated with a terminal or non-terminal
171023 ** Each non-terminal can have a different minor type.
171045 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
171046 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
171049 ** YY_NO_ACTION The yy_action[] code for no-op
171095 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
171096 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
171113 /* Define the yytestcase() macro to be a no-op if is not already defined
171138 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
171147 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
171160 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
171172 ** shifting non-terminals after a reduce.
171684 #define YY_REDUCE_MIN (-271)
171687 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
171688 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
171689 /* 20 */ 576, 598, -175, 686, 860, 615, 725, 1014, 778, 781,
171690 /* 30 */ 857, 616, 887, 87, 240, -192, 408, 626, 796, 843,
171691 /* 40 */ 854, 1004, -271, -271, -271, -271, -271, -271, -271, -271,
171692 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
171693 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
171694 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, 80,
171700 /* 130 */ 1473, -271, -271, -271, -271, -271, -271, -271, -271, -271,
171701 /* 140 */ -271, -271, 138, 459, 396, -158, 470, 302, -212, 521,
171702 /* 150 */ 201, -195, -92, 559, 630, 632, 630, -271, 632, 901,
171703 /* 160 */ 63, 407, 670, -271, -271, -271, -271, 161, 161, 161,
171705 /* 180 */ 688, -166, -161, 674, 787, 794, 799, 852, 996, -122,
171706 /* 190 */ 837, -120, 1018, 1035, 415, 1047, 1001, 958, 1082, 400,
171709 /* 220 */ 1195, -194, 56, 185, -135, 232, 522, 560, 571, 601,
171790 /********** End of lemon-generated parsing tables *****************************/
172013 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
172016 YYMINORTYPE minor; /* The user-supplied minor token value. This
172026 int yyhwm; /* High-water mark of the stack */
172826 newSize = p->yystksz*2 + 100;
172827 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
172828 if( p->yystack==&p->yystk0 ){
172830 if( pNew ) pNew[0] = p->yystk0;
172832 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
172835 p->yystack = pNew;
172836 p->yytos = &p->yystack[idx];
172840 yyTracePrompt, p->yystksz, newSize);
172843 p->yystksz = newSize;
172864 yypParser->yyhwm = 0;
172867 yypParser->yytos = NULL;
172868 yypParser->yystack = NULL;
172869 yypParser->yystksz = 0;
172871 yypParser->yystack = &yypParser->yystk0;
172872 yypParser->yystksz = 1;
172876 yypParser->yyerrcnt = -1;
172878 yypParser->yytos = yypParser->yystack;
172879 yypParser->yystack[0].stateno = 0;
172880 yypParser->yystack[0].major = 0;
172882 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
172927 ** terminal or non-terminal is destroyed. This can happen
172942 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
172957 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
172974 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
172983 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
172988 sqlite3WithDelete(pParse->db, (yypminor->yy521));
172994 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
173000 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
173009 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
173015 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
173020 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
173027 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
173043 assert( pParser->yytos!=0 );
173044 assert( pParser->yytos > pParser->yystack );
173045 yytos = pParser->yytos--;
173050 yyTokenName[yytos->major]);
173053 yy_destructor(pParser, yytos->major, &yytos->minor);
173061 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
173063 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
173094 return pParser->yyhwm;
173100 ** is in state X and has a lookahead token Y. In a well-tested
173137 ** look-ahead token iLookAhead.
173140 YYCODETYPE iLookAhead, /* The look-ahead token */
173178 int j = i - iLookAhead + YYWILDCARD;
173201 ** Find the appropriate action for a parser given the non-terminal
173202 ** look-ahead token iLookAhead.
173206 YYCODETYPE iLookAhead /* The look-ahead token */
173241 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
173260 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
173264 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
173265 yyNewState - YY_MIN_REDUCE);
173283 yypParser->yytos++;
173285 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
173286 yypParser->yyhwm++;
173287 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
173291 if( yypParser->yytos>yypParser->yystackEnd ){
173292 yypParser->yytos--;
173297 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
173299 yypParser->yytos--;
173306 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
173308 yytos = yypParser->yytos;
173309 yytos->stateno = yyNewState;
173310 yytos->major = yyMajor;
173311 yytos->minor.yy0 = yyMinor;
173315 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
173726 ** of symbols on the right-hand side of that rule. */
173728 -1, /* (0) explain ::= EXPLAIN */
173729 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
173730 -1, /* (2) cmdx ::= cmd */
173731 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
173733 -1, /* (5) transtype ::= DEFERRED */
173734 -1, /* (6) transtype ::= IMMEDIATE */
173735 -1, /* (7) transtype ::= EXCLUSIVE */
173736 -2, /* (8) cmd ::= COMMIT|END trans_opt */
173737 -2, /* (9) cmd ::= ROLLBACK trans_opt */
173738 -2, /* (10) cmd ::= SAVEPOINT nm */
173739 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
173740 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
173741 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
173742 -1, /* (14) createkw ::= CREATE */
173744 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
173745 -1, /* (17) temp ::= TEMP */
173747 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
173748 -2, /* (20) create_table_args ::= AS select */
173750 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
173751 -2, /* (23) table_option ::= WITHOUT nm */
173752 -1, /* (24) table_option ::= nm */
173753 -2, /* (25) columnname ::= nm typetoken */
173755 -4, /* (27) typetoken ::= typename LP signed RP */
173756 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
173757 -2, /* (29) typename ::= typename ID|STRING */
173760 -2, /* (32) ccons ::= CONSTRAINT nm */
173761 -3, /* (33) ccons ::= DEFAULT scantok term */
173762 -4, /* (34) ccons ::= DEFAULT LP expr RP */
173763 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
173764 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
173765 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
173766 -3, /* (38) ccons ::= NOT NULL onconf */
173767 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
173768 -2, /* (40) ccons ::= UNIQUE onconf */
173769 -4, /* (41) ccons ::= CHECK LP expr RP */
173770 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
173771 -1, /* (43) ccons ::= defer_subclause */
173772 -2, /* (44) ccons ::= COLLATE ID|STRING */
173773 -3, /* (45) generated ::= LP expr RP */
173774 -4, /* (46) generated ::= LP expr RP ID */
173776 -1, /* (48) autoinc ::= AUTOINCR */
173778 -2, /* (50) refargs ::= refargs refarg */
173779 -2, /* (51) refarg ::= MATCH nm */
173780 -3, /* (52) refarg ::= ON INSERT refact */
173781 -3, /* (53) refarg ::= ON DELETE refact */
173782 -3, /* (54) refarg ::= ON UPDATE refact */
173783 -2, /* (55) refact ::= SET NULL */
173784 -2, /* (56) refact ::= SET DEFAULT */
173785 -1, /* (57) refact ::= CASCADE */
173786 -1, /* (58) refact ::= RESTRICT */
173787 -2, /* (59) refact ::= NO ACTION */
173788 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
173789 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
173791 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
173792 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
173794 -1, /* (66) tconscomma ::= COMMA */
173795 -2, /* (67) tcons ::= CONSTRAINT nm */
173796 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
173797 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
173798 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
173799 …-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
173802 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
173804 -2, /* (76) orconf ::= OR resolvetype */
173805 -1, /* (77) resolvetype ::= IGNORE */
173806 -1, /* (78) resolvetype ::= REPLACE */
173807 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
173808 -2, /* (80) ifexists ::= IF EXISTS */
173810 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
173811 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
173812 -1, /* (84) cmd ::= select */
173813 -3, /* (85) select ::= WITH wqlist selectnowith */
173814 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
173815 -1, /* (87) select ::= selectnowith */
173816 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
173817 -1, /* (89) multiselect_op ::= UNION */
173818 -2, /* (90) multiselect_op ::= UNION ALL */
173819 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
173820 …-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
173821 …-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
173822 -4, /* (94) values ::= VALUES LP nexprlist RP */
173823 -5, /* (95) values ::= values COMMA LP nexprlist RP */
173824 -1, /* (96) distinct ::= DISTINCT */
173825 -1, /* (97) distinct ::= ALL */
173828 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
173829 -3, /* (101) selcollist ::= sclp scanpt STAR */
173830 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
173831 -2, /* (103) as ::= AS nm */
173834 -2, /* (106) from ::= FROM seltablist */
173835 -2, /* (107) stl_prefix ::= seltablist joinop */
173837 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
173838 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
173839 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
173840 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
173841 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
173843 -2, /* (115) dbnm ::= DOT nm */
173844 -1, /* (116) fullname ::= nm */
173845 -3, /* (117) fullname ::= nm DOT nm */
173846 -1, /* (118) xfullname ::= nm */
173847 -3, /* (119) xfullname ::= nm DOT nm */
173848 -5, /* (120) xfullname ::= nm DOT nm AS nm */
173849 -3, /* (121) xfullname ::= nm AS nm */
173850 -1, /* (122) joinop ::= COMMA|JOIN */
173851 -2, /* (123) joinop ::= JOIN_KW JOIN */
173852 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
173853 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
173854 -2, /* (126) on_using ::= ON expr */
173855 -4, /* (127) on_using ::= USING LP idlist RP */
173858 -3, /* (130) indexed_by ::= INDEXED BY nm */
173859 -2, /* (131) indexed_by ::= NOT INDEXED */
173861 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
173862 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
173863 -3, /* (135) sortlist ::= expr sortorder nulls */
173864 -1, /* (136) sortorder ::= ASC */
173865 -1, /* (137) sortorder ::= DESC */
173867 -2, /* (139) nulls ::= NULLS FIRST */
173868 -2, /* (140) nulls ::= NULLS LAST */
173871 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
173873 -2, /* (145) having_opt ::= HAVING expr */
173875 -2, /* (147) limit_opt ::= LIMIT expr */
173876 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
173877 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
173878 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
173880 -2, /* (152) where_opt ::= WHERE expr */
173882 -2, /* (154) where_opt_ret ::= WHERE expr */
173883 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
173884 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
173885 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
173886 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
173887 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
173888 -3, /* (160) setlist ::= nm EQ expr */
173889 -5, /* (161) setlist ::= LP idlist RP EQ expr */
173890 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
173891 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
173893 -2, /* (165) upsert ::= RETURNING selcollist */
173894 …-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
173895 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
173896 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
173897 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
173898 -2, /* (170) returning ::= RETURNING selcollist */
173899 -2, /* (171) insert_cmd ::= INSERT orconf */
173900 -1, /* (172) insert_cmd ::= REPLACE */
173902 -3, /* (174) idlist_opt ::= LP idlist RP */
173903 -3, /* (175) idlist ::= idlist COMMA nm */
173904 -1, /* (176) idlist ::= nm */
173905 -3, /* (177) expr ::= LP expr RP */
173906 -1, /* (178) expr ::= ID|INDEXED|JOIN_KW */
173907 -3, /* (179) expr ::= nm DOT nm */
173908 -5, /* (180) expr ::= nm DOT nm DOT nm */
173909 -1, /* (181) term ::= NULL|FLOAT|BLOB */
173910 -1, /* (182) term ::= STRING */
173911 -1, /* (183) term ::= INTEGER */
173912 -1, /* (184) expr ::= VARIABLE */
173913 -3, /* (185) expr ::= expr COLLATE ID|STRING */
173914 -6, /* (186) expr ::= CAST LP expr AS typetoken RP */
173915 -5, /* (187) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
173916 -8, /* (188) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP */
173917 -4, /* (189) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
173918 -6, /* (190) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
173919 … -9, /* (191) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist ORDER BY sortlist RP filter_over */
173920 -5, /* (192) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
173921 -1, /* (193) term ::= CTIME_KW */
173922 -5, /* (194) expr ::= LP nexprlist COMMA expr RP */
173923 -3, /* (195) expr ::= expr AND expr */
173924 -3, /* (196) expr ::= expr OR expr */
173925 -3, /* (197) expr ::= expr LT|GT|GE|LE expr */
173926 -3, /* (198) expr ::= expr EQ|NE expr */
173927 -3, /* (199) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
173928 -3, /* (200) expr ::= expr PLUS|MINUS expr */
173929 -3, /* (201) expr ::= expr STAR|SLASH|REM expr */
173930 -3, /* (202) expr ::= expr CONCAT expr */
173931 -2, /* (203) likeop ::= NOT LIKE_KW|MATCH */
173932 -3, /* (204) expr ::= expr likeop expr */
173933 -5, /* (205) expr ::= expr likeop expr ESCAPE expr */
173934 -2, /* (206) expr ::= expr ISNULL|NOTNULL */
173935 -3, /* (207) expr ::= expr NOT NULL */
173936 -3, /* (208) expr ::= expr IS expr */
173937 -4, /* (209) expr ::= expr IS NOT expr */
173938 -6, /* (210) expr ::= expr IS NOT DISTINCT FROM expr */
173939 -5, /* (211) expr ::= expr IS DISTINCT FROM expr */
173940 -2, /* (212) expr ::= NOT expr */
173941 -2, /* (213) expr ::= BITNOT expr */
173942 -2, /* (214) expr ::= PLUS|MINUS expr */
173943 -3, /* (215) expr ::= expr PTR expr */
173944 -1, /* (216) between_op ::= BETWEEN */
173945 -2, /* (217) between_op ::= NOT BETWEEN */
173946 -5, /* (218) expr ::= expr between_op expr AND expr */
173947 -1, /* (219) in_op ::= IN */
173948 -2, /* (220) in_op ::= NOT IN */
173949 -5, /* (221) expr ::= expr in_op LP exprlist RP */
173950 -3, /* (222) expr ::= LP select RP */
173951 -5, /* (223) expr ::= expr in_op LP select RP */
173952 -5, /* (224) expr ::= expr in_op nm dbnm paren_exprlist */
173953 -4, /* (225) expr ::= EXISTS LP select RP */
173954 -5, /* (226) expr ::= CASE case_operand case_exprlist case_else END */
173955 -5, /* (227) case_exprlist ::= case_exprlist WHEN expr THEN expr */
173956 -4, /* (228) case_exprlist ::= WHEN expr THEN expr */
173957 -2, /* (229) case_else ::= ELSE expr */
173961 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
173962 -1, /* (234) nexprlist ::= expr */
173964 -3, /* (236) paren_exprlist ::= LP exprlist RP */
173965 …-12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
173966 -1, /* (238) uniqueflag ::= UNIQUE */
173969 -3, /* (241) eidlist_opt ::= LP eidlist RP */
173970 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
173971 -3, /* (243) eidlist ::= nm collate sortorder */
173973 -2, /* (245) collate ::= COLLATE ID|STRING */
173974 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
173975 -2, /* (247) cmd ::= VACUUM vinto */
173976 -3, /* (248) cmd ::= VACUUM nm vinto */
173977 -2, /* (249) vinto ::= INTO expr */
173979 -3, /* (251) cmd ::= PRAGMA nm dbnm */
173980 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
173981 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
173982 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
173983 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
173984 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
173985 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
173986 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
173987 …-11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
173988 -1, /* (260) trigger_time ::= BEFORE|AFTER */
173989 -2, /* (261) trigger_time ::= INSTEAD OF */
173991 -1, /* (263) trigger_event ::= DELETE|INSERT */
173992 -1, /* (264) trigger_event ::= UPDATE */
173993 -3, /* (265) trigger_event ::= UPDATE OF idlist */
173995 -2, /* (267) when_clause ::= WHEN expr */
173996 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
173997 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
173998 -3, /* (270) trnm ::= nm DOT nm */
173999 -3, /* (271) tridxby ::= INDEXED BY nm */
174000 -2, /* (272) tridxby ::= NOT INDEXED */
174001 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
174002 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
174003 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
174004 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
174005 -4, /* (277) expr ::= RAISE LP IGNORE RP */
174006 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
174007 -1, /* (279) raisetype ::= ROLLBACK */
174008 -1, /* (280) raisetype ::= ABORT */
174009 -1, /* (281) raisetype ::= FAIL */
174010 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
174011 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
174012 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
174014 -2, /* (286) key_opt ::= KEY expr */
174015 -1, /* (287) cmd ::= REINDEX */
174016 -3, /* (288) cmd ::= REINDEX nm dbnm */
174017 -1, /* (289) cmd ::= ANALYZE */
174018 -3, /* (290) cmd ::= ANALYZE nm dbnm */
174019 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
174020 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
174021 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
174022 -1, /* (294) add_column_fullname ::= fullname */
174023 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
174024 -1, /* (296) cmd ::= create_vtab */
174025 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
174026 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
174028 -1, /* (300) vtabargtoken ::= ANY */
174029 -3, /* (301) vtabargtoken ::= lp anylist RP */
174030 -1, /* (302) lp ::= LP */
174031 -2, /* (303) with ::= WITH wqlist */