• Home
  • Raw
  • Download

Lines Matching +full:gcc +full:- +full:specs +full:- +full:uwp

17 ** language. The code for the "sqlite3" command-line shell is also in a
48 ** NO_TEST - The branches on this line are not
53 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
57 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
61 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
66 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
141 ** 2015-03-02
179 ** large file support, or if the OS is windows, these should be no-ops.
185 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
194 ** on 2008-11-28.) These days, all Linux kernels support large files, so
211 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
217 ** gcc version numbers and have reasonable settings for gcc version numbers,
218 ** so the GCC_VERSION macro will be set to a correct non-zero value even
270 ** MinGW via their preprocessor (e.g. if they customized their GCC to define
271 ** some MinGW-specific macros). When compiling for MinGW, either the
294 /* Optionally #include a user-defined header, whereby compilation options
312 ** 2001-09-15
323 ** presents to client programs. If a C-function, structure, datatype,
334 ** The official C-language API documentation for SQLite is derived
379 ** that require non-default calling conventions.
404 ** These no-op macros are used in front of interfaces to mark those
406 ** should not use deprecated interfaces - they are supported for backwards
430 ** CAPI3REF: Compile-Time Library Version Numbers
446 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
448 ** a string which identifies a particular check-in of SQLite
450 ** string contains the date and time of the check-in (UTC) and a SHA1
451 ** or SHA3-256 hash of the entire source tree. If the source code has
461 #define SQLITE_SOURCE_ID "2023-05-16 12:36:15 831d0fb2836b71c9bc51067c49fee4b8f18047814f2ff22d…
464 ** CAPI3REF: Run-Time Library Version Numbers
501 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
510 ** returning the N-th compile time option string. ^If N is out of range,
535 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
553 ** This interface only reports on the compile-time mutex setting
559 ** sqlite3_threadsafe() function shows only the compile-time setting of
560 ** thread safety, not any run-time changes to that setting made by
585 ** CAPI3REF: 64-Bit Integer Types
588 ** Because there is no cross-platform way to specify 64-bit integer types
589 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
596 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
619 ** substitute integer for floating-point.
661 ** argument is a harmless no-op.
674 ** CAPI3REF: One-Step Query Execution Interface
682 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
683 ** semicolon-separate SQL statements passed into its 2nd argument,
705 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
755 /* beginning-of-error-codes */
786 /* end-of-error-codes */
794 ** these result codes are too coarse-grained. They do not provide as
899 ** [sqlite3_open_v2()] has historically be a no-op and might become an
955 ** read-only media and cannot be changed even by processes with
1017 ** (Third-party VFS implementations might also make the distinction
1052 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1076 ** requested lock, then the call to xLock() is a no-op.
1079 ** to xUnlock() is a no-op.
1092 ** locking strategy (for example to use dot-file locks), to inquire
1139 ** fails to zero-fill short reads might seem to work. However,
1140 ** failure to zero-fill short reads will eventually lead to
1196 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1198 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1208 ** point to an integer (type int) containing the new chunk-size to use
1210 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1221 ** the [rollback journal] or the [write-ahead log]) for a particular database
1234 ** this file-control is NULL. However, if the database file is being synced
1235 ** as part of a multi-database commit, the argument points to a nul-terminated
1236 ** string containing the transactions super-journal file name. VFSes that
1253 ** anti-virus programs. By default, the windows VFS will retry file read,
1279 ** WAL mode. If the integer is -1, then it is overwritten with the current
1284 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1288 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1289 ** mode. If the integer is -1, then it is overwritten with the current
1290 ** zero-damage mode setting.
1301 ** final bottom-level VFS are written into memory obtained from
1305 ** all file-control actions, there is no guarantee that this will actually
1307 ** pointer in case this file-control is not implemented. This file-control
1311 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1315 ** to a pointer to the top-level VFS.)^
1317 ** upper-most shim only.
1334 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1336 ** of the result string if the string is non-NULL.
1342 ** it is able to override built-in [PRAGMA] statements.
1346 ** file-control may be invoked by SQLite on the database file handle
1348 ** to the connection's busy-handler callback. The argument is of type (void**)
1349 ** - an array of two (void *) values. The first (void *) actually points
1351 ** busy-handler, this function should be invoked with the second (void *) in
1352 ** the array as the only argument. If it returns non-zero, then the operation
1357 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1367 ** maximum number of bytes that will be used for memory-mapped I/O.
1373 ** file-control is used internally to implement [PRAGMA mmap_size].
1379 ** The argument is a zero-terminated string. Higher layers in the
1381 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1406 ** Applications should <em>not</em> use this file-control.
1455 ** The parameter is a pointer to a 32-bit signed integer that contains
1456 ** the value that M is to be set to. Before returning, the 32-bit signed
1461 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1487 ** file to the database file, but before the *-shm file is updated to
1492 ** whether or not there is a database client in another process with a wal-mode
1494 ** (void*) argument passed with this file-control should be a pointer to a
1498 ** the database is not a wal-mode db, or if there is no such connection in any
1508 ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1509 ** purges the contents of the in-memory page cache. If there is an open
1510 ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1588 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1631 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1649 ** consist of a single "-" character followed by no more than
1650 ** 11 alphanumeric and/or "-" characters.
1665 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1685 ** the open of a journal file a no-op. Writes to this journal would
1686 ** also be no-ops, and any attempt to read the journal would return
1688 ** file will be doing page-aligned sector reads and writes in a random
1727 ** flag is never actually used and is not implemented in the built-in
1730 ** non-zero error code if there is an I/O error or if the name of
1732 ** is returned, then non-zero or zero is written into *pResOut to indicate
1746 ** of good-quality randomness into zOut. The return value is
1754 ** a 24-hour day).
1780 void *pAppData; /* Pointer to application-specific data */
1890 ** are harmless no-ops.)^
1895 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1918 ** compile-time option, then the automatic calls to sqlite3_initialize()
1927 ** The sqlite3_os_init() routine does operating-system specific
1943 ** (using the [SQLITE_OS_OTHER=1] compile-time
1945 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1983 ** implementation of an application-defined [sqlite3_os_init()].
1987 ** then this routine returns a non-zero [error code].
2001 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2014 ** and low-level memory allocation routines.
2026 ** Note that SQLite comes with several [built-in memory allocators]
2031 ** memory allocator that simulates memory out-of-memory conditions in
2098 ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2113 ** non-zero [error code] if a discontinued or unsupported configuration option
2119 ** [threading mode] to Single-thread. In other words, it disables
2122 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2124 ** value of Single-thread and so [sqlite3_config()] will return
2130 ** [threading mode] to Multi-thread. In other words, it disables
2134 ** are enabled so that SQLite will be safe to use in a multi-threaded
2137 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2138 ** it is not possible to set the Multi-thread [threading mode] and
2153 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2162 ** alternative low-level memory allocation routines to be used in place of
2190 ** disabled, the following SQLite interfaces become non-operational:
2211 ** This configuration option is a no-op if an application-defined page
2214 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2222 ** argument must be either a NULL pointer or a pointer to an 8-byte
2229 ** ^If pMem is NULL and N is non-zero, then each database connection
2232 ** of -1024*N bytes if N is negative, . ^If additional
2245 ** An 8-byte aligned pointer to the memory,
2252 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2260 ** The argument specifies alternative low-level mutex routines to be used
2264 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2277 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2310 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2312 ** passed through as the first parameter to the application-defined logger
2320 ** In a multi-threaded application, the application-defined logger
2325 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2342 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2343 ** if that compile-time option is omitted.
2353 ** They are retained for backwards compatibility but are now no-ops.
2359 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2375 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2382 ** compile-time maximum mmap size set by the
2383 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2385 ** changed to its compile-time default.
2390 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2391 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2407 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2416 ** becomes the [statement journal] spill-to-disk threshold.
2419 ** Or if the threshold is -1, statement journals are always held
2425 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2430 ** of type (int) - the new value of the sorter-reference size threshold.
2434 ** of a table column that its values are likely to be very large - larger
2435 ** than the configured sorter-reference size threshold - then a reference
2441 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2446 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2449 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2451 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2452 ** compile-time option is not set, then the default maximum is 1073741824.
2468 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2469 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2495 ** non-zero [error code] if a discontinued or unsupported configuration option
2511 ** must be aligned to an 8-byte boundary. ^If the second argument to
2547 ** triggers in the main database schema or in the schemas of ATTACH-ed
2564 ** views in the main database schema or in the schemas of ATTACH-ed
2571 ** [FTS3] full-text search engine extension.
2586 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2588 ** When the first argument to this interface is 1, then only the C-API is
2590 ** this interface is 0, then both the C-API and the SQL function are disabled.
2591 ** If the first argument is -1, then no changes are made to state of either the
2592 ** C-API or the SQL function.
2615 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2618 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2619 ** have been disabled - 0 if they are not disabled, 1 if they are.
2642 ** behavior. The first parameter passed to this operation is an integer -
2646 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2704 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2713 ** the legacy [double-quoted string literal] misfeature for DML statements
2715 ** default value of this setting is determined by the [-DSQLITE_DQS]
2716 ** compile-time option.
2722 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2724 ** default value of this setting is determined by the [-DSQLITE_DQS]
2725 ** compile-time option.
2752 ** created database file to have a schema format version number (the 4-byte
2779 ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2794 ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2840 ** has a unique 64-bit signed
2921 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2943 ** any changes performed by sub-triggers, as the sqlite3_changes()
2944 ** value will be saved and restored after each sub-trigger has run.)^
3012 ** CAPI3REF: Interrupt A Long-Running Query
3018 ** or Ctrl-C where the user wants a long query operation to halt
3043 ** SQL statements is a no-op and has no effect on SQL statements
3055 ** These routines are useful during command-line input to determine if the
3061 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3076 ** then the return value from sqlite3_complete16() will be non-zero
3079 ** The input to [sqlite3_complete()] must be a zero-terminated
3080 ** UTF-8 string.
3082 ** The input to [sqlite3_complete16()] must be a zero-terminated
3083 ** UTF-16 string in native byte order.
3090 ** KEYWORDS: {busy-handler callback} {busy handler}
3112 ** ^If the callback returns non-zero, then another attempt
3188 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3190 ** to zero-terminated strings that contain the names of the columns.
3192 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3204 ** -----------------------
3226 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3258 ** These routines are work-alikes of the "printf()" family of functions
3262 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3263 ** See the [built-in printf()] documentation for details.
3286 ** guarantees that the buffer is always zero-terminated. ^The first
3289 ** written will be n-1 characters.
3293 ** See also: [built-in printf()], [printf() SQL function]
3305 ** does not include operating-system specific [VFS] implementation. The
3316 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3317 ** of a signed 32-bit integer.
3322 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3327 ** might result if sqlite3_free() is called with a non-NULL pointer that
3347 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3348 ** of a 32-bit signed integer.
3363 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3387 ** routines, which form the built-in memory allocation subsystem.
3392 ** value of [sqlite3_memory_used()] since the high-water mark
3399 ** ^The memory high-water mark is reset to the current value of
3402 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3409 ** CAPI3REF: Pseudo-Random Number Generator
3411 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3414 ** the built-in random() and randomblob() SQL functions. This interface allows
3425 ** non-NULL P then the pseudo-randomness is generated
3432 ** CAPI3REF: Compile-Time Authorization Callbacks
3463 ** to the callback are either NULL pointers or zero-terminated strings
3491 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3510 ** statement might be re-prepared during [sqlite3_step()] due to a
3556 ** is the name of the inner-most trigger or view that is responsible for
3558 ** top-level SQL code.
3608 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3612 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3614 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3619 ** the original statement text and an estimate of wall-clock time
3639 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3661 ** interface by using the X argument when X begins with "--" and invoking
3668 ** X argument points to a 64-bit integer which is approximately
3698 ** M argument should be the bitwise OR-ed combination of
3748 ** ^If the progress callback returns non-zero, the operation is
3772 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3773 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3784 ** ^The default encoding will be UTF-8 for databases created using
3786 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3800 ** <dd>The database is opened in read-only mode. If the database does
3808 ** read-write mode fails due to OS-level permissions, an attempt is
3809 ** made to open it in read-only mode. [sqlite3_db_readonly()] can be
3811 ** read-write.</dd>)^
3827 ** <dd>The database will be opened as an in-memory database. The database
3828 ** is named by the "filename" argument for the purposes of cache-sharing,
3833 ** <dd>The new database connection will use the "multi-thread"
3851 ** this option is a no-op.
3877 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3888 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3889 ** is created for the connection. ^This in-memory database will vanish when
3897 ** on-disk database will be created. ^This private database will be
3907 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3930 ** SQLite and its built-in [VFSes] interpret the
3945 ** ^If "ro" is specified, then the database is opened for read-only
3948 ** "rw", then the database is opened for read-write (but not create)
3952 ** set to "memory" then a pure [in-memory database] that never reads
3979 ** read-only media. ^When immutable is set, SQLite assumes that the
3981 ** privilege, and so the database is opened read-only and all locking
4006 ** <tr><td style="white-space:nowrap">
4010 ** necessary - space characters can be used literally
4013 ** Open file "data.db" in the current directory for read-only access.
4014 ** Regardless of whether or not shared-cache mode is enabled by
4016 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4017 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4018 ** that uses dot-files in place of posix advisory locking.
4026 ** percent sign - "%" - followed by exactly two hexadecimal digits
4028 ** URI filename are interpreted, they are encoded using UTF-8 and all
4030 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4034 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4036 ** characters must be converted to UTF-8 prior to passing them into
4046 const char *filename, /* Database filename (UTF-8) */
4050 const void *filename, /* Database filename (UTF-16) */
4054 const char *filename, /* Database filename (UTF-8) */
4091 ** case or if the value begins with a non-zero number. The
4099 ** 64-bit signed integer and returns that integer, or D if P does not
4104 ** the value) of the N-th query parameter for filename F, or a NULL
4106 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4165 ** ^If X is the name of a rollback or WAL-mode journal file that is
4171 ** only. It is not a general-purpose interface.
4215 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4251 ** change the value of the error code. The error-code preserving
4262 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4263 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4269 ** ^The sqlite3_errstr() interface returns the English-language text
4270 ** that describes the [result code], as UTF-8.
4279 ** SQL, then the sqlite3_error_offset() function returns -1.
4314 ** The life-cycle of a prepared statement object usually goes like this:
4329 ** CAPI3REF: Run-time Limits
4342 ** set at compile-time by a C preprocessor macro called
4351 ** simply invoke this interface with the third parameter set to -1.
4353 ** Run-time limits are intended for use in applications that manage
4366 ** New run-time limit categories may be added in future releases.
4371 ** CAPI3REF: Run-Time Limit Categories
4375 ** that can be lowered at run-time using [sqlite3_limit()].
4461 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4484 ** To execute an SQL statement, it must first be compiled into a byte-code
4493 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4494 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4495 ** as a convenience. The UTF-16 interfaces work by converting the
4496 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4503 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4505 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4506 ** and sqlite3_prepare16_v3() use UTF-16.
4512 ** If the caller knows that the supplied string is nul-terminated, then
4515 ** the nul-terminator.
4565 ** ^The specific value of a WHERE-clause [parameter] might influence the
4566 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4568 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4580 const char *zSql, /* SQL statement, UTF-8 encoded */
4587 const char *zSql, /* SQL statement, UTF-8 encoded */
4594 const char *zSql, /* SQL statement, UTF-8 encoded */
4602 const void *zSql, /* SQL statement, UTF-16 encoded */
4609 const void *zSql, /* SQL statement, UTF-16 encoded */
4616 const void *zSql, /* SQL statement, UTF-16 encoded */
4627 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4631 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4634 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4650 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4651 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4662 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4674 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4678 ** Note that [application-defined SQL functions] or
4682 ** change the database file through side-effects:
4708 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4710 ** read-only no-op if the table already exists, but
4735 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4772 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4783 ** implementation of [application-defined SQL functions] are protected.
4801 ** is always first parameter to [application-defined SQL functions].
4802 ** The application-defined SQL function implementation will pass this
4852 ** it should be a pointer to well-formed UTF8 text.
4854 ** it should be a pointer to well-formed UTF16 text.
4856 ** it should be a pointer to a well-formed unicode string that is
4860 ** [[byte-order determination rules]] ^The byte-order of
4861 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4878 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4918 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4920 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4992 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5003 ** ^If the value N is out of range or if the N-th parameter is
5005 ** always in UTF-8 encoding even if the named parameter was
5006 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5023 ** name must be given in UTF-8 even if the original statement
5024 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5065 ** interface returns a pointer to a zero-terminated UTF-8 string
5066 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5067 ** UTF-16 string. ^The first parameter is the [prepared statement]
5078 ** (for example during a conversion from UTF-8 to UTF-16) then a
5097 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5106 ** ^The names returned are the original un-aliased names of the
5112 ** ^The left-most column is column 0 for these routines.
5121 ** UTF-16 encoded strings and the other functions return UTF-8.
5124 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5148 ** ^The returned string is always UTF-8 encoded.
5161 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5211 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5237 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5267 ** will return non-zero if previous call to [sqlite3_step](P) returned
5269 ** where it always returns zero since each step of that multi-step
5283 ** <li> 64-bit signed integer
5284 ** <li> 64-bit IEEE floating point number
5317 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5318 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5319 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5320 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5325 ** or a UTF-8 TEXT result in bytes
5327 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5379 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5381 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5382 ** the string to UTF-8 and then returns the number of bytes.
5384 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5388 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5390 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5391 ** the string to UTF-16 and then returns the number of bytes.
5393 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5404 ** even empty strings, are always zero-terminated. ^The return
5405 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5421 ** [application-defined SQL functions] or [virtual tables], not within
5422 ** top-level application code.
5440 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5461 ** sqlite3_column_text16() is called. A zero-terminator might
5463 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5465 ** to UTF-16.</li>
5466 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5468 ** to UTF-8.</li>
5471 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5502 ** fail if an out-of-memory error occurs during a format conversion.
5503 ** Only the following subset of interfaces are subject to out-of-memory
5514 ** If an out-of-memory error occurs, then the return value from these
5516 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5550 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5565 ** object back to its initial state, ready to be re-executed.
5604 ** connection then application-defined SQL functions must be added
5608 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5609 ** representation, exclusive of the zero-terminator. ^Note that the name
5610 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5616 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5619 ** parameter is less than -1 or greater than 127 then the behavior is
5638 ** deterministic. The built-in [random()] SQL function is an example of a
5649 ** all application-defined SQL functions that do not need to be
5663 ** pointers to C-language functions that implement the SQL function or
5673 ** C-language callbacks that implement the new function. xStep and xFinal
5674 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5676 ** non-NULL, in which case the new function may be used as either an aggregate
5679 ** [user-defined window functions|available here].
5694 ** SQL function is used. ^A function implementation with a non-negative
5703 ** ^Built-in functions may be overloaded by new application-defined functions.
5705 ** ^An application-defined function is permitted to call other
5760 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5761 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5762 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5789 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5794 ** [application-defined SQL function]
5795 ** that has side-effects or that could potentially leak sensitive information.
5798 ** modified to invoke the application-defined function in ways that are
5802 ** [application-defined SQL functions], regardless of whether or not they
5806 ** that do not have access to the application-defined functions.
5825 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5827 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5829 ** security-adverse side-effects and information-leaks.
5834 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5836 ** functions. However, if it is not specified for a user-defined window
5837 ** function, then any sub-types belonging to arguments passed to the window
5876 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5877 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5879 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5880 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5882 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5883 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5886 ** or a UTF-8 TEXT in bytes
5888 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5906 ** implement [application-defined SQL functions] and [virtual tables].
5916 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5917 ** in the native byte-order of the host machine. ^The
5919 ** extract UTF-16 strings as big-endian and little-endian respectively.
5960 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5975 ** fail if an out-of-memory error occurs during a format conversion.
5976 ** Only the following subset of interfaces are subject to out-of-memory
5989 ** If an out-of-memory error occurs, then the return value from these
5991 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6039 ** an [application-defined SQL function] argument V. The subtype
6059 ** then sqlite3_value_free(V) is a harmless no-op.
6120 ** the application-defined function is running.
6140 ** These functions may be used by (non-aggregate) SQL functions to
6144 ** of where this might be useful is in a regular-expression matching
6153 ** value to the application-defined function. ^N is zero for the left-most
6158 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6159 ** argument of the application-defined function. ^Subsequent
6183 ** function parameters that are compile-time constants, including literal
6186 ** The value of the N parameter to these interfaces should be non-negative.
6213 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6229 ** an application-defined function to be the BLOB whose content is pointed
6234 ** interfaces set the result of the application-defined function to be
6238 ** an application-defined function to be a floating point value specified
6246 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6247 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6248 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6253 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6271 ** of the application-defined function to be the 32-bit signed integer
6274 ** of the application-defined function to be the 64-bit signed integer
6278 ** of the application-defined function to be NULL.
6282 ** set the return value of the application-defined function to be
6283 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6284 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6286 ** application-defined function to be a text string in an encoding
6296 ** is non-negative, then as many bytes (not characters) of the text
6297 ** pointed to by the 2nd parameter are taken as the application-defined
6298 ** function result. If the 3rd parameter is non-negative, then it
6305 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6321 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6323 ** byte-order specified by the BOM. ^The byte-order specified by
6324 ** the BOM at the beginning of the text overrides the byte-order
6327 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6338 ** the application-defined function to be a copy of the
6349 ** also associates the host-language pointer P or type T with that
6351 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6359 ** than the one containing the application-defined function that received
6391 ** the result from the [application-defined SQL function] with
6407 ** ^The name of the collation is a UTF-8 string
6409 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6472 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6515 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6516 ** the names are passed as UTF-16 in machine native byte order.
6517 ** ^A call to either function replaces the existing collation-needed callback.
6583 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6625 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6626 ** &nbsp; TemporaryFolder->Path->Data();
6629 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6642 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6681 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6686 ** the current directory on the sub-platforms of Win32 where that concept is
6687 ** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6690 ** UTF-8 or UTF-16, respectively.
6709 ** CAPI3REF: Test For Auto-Commit Mode
6713 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6717 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6719 ** If certain kinds of errors occur on a statement within a multi-statement
6750 ** for the N-th database on database connection D, or a NULL pointer of N is
6752 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6760 ** remember the string long-term should make their own copy. Applications that
6762 ** threads should mutex-protect calls to this API and should make their own
6774 ** connection D, or if database N is a temporary or in-memory database, then
6779 ** is [DETACH]-ed or until the database connection closes.
6800 ** CAPI3REF: Determine if a database is read-only
6804 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6823 ** a valid schema, then -1 is returned.
6889 ** ^If the callback on a commit hook function returns non-zero,
6911 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6913 ** hook returning non-zero, just as it would be with any other rollback.
6933 ** the schema-name of the attached database that is being autovacuumed,
6941 ** <p>^If there are multiple ATTACH-ed database files that are being
7056 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7057 ** compile-time option is recommended because the
7083 ** shared cache mode should be enabled per-database connection via
7087 ** 32-bit integer is atomic.
7089 ** See Also: [SQLite Shared-Cache Mode]
7097 ** of heap memory by deallocating non-essential memory allocations
7099 ** pages to improve performance is an example of non-essential memory.
7102 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7116 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7151 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7175 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7176 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7209 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7213 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7280 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7292 ** with various operating-system specific extensions added.
7301 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7340 ** [extension loading] while evaluating user-entered SQL, the following API
7348 ** ^This interface enables or disables both the C-API
7351 ** to enable or disable only the C-API.)^
7391 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7489 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7501 ** expr on the right-hand side can be evaluated (and thus the constraint
7516 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7522 ** non-zero.
7526 ** the right-hand side of the corresponding aConstraint[] is evaluated
7527 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7556 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7587 int iColumn; /* Column constrained. -1 for ROWID */
7590 int iTermOffset; /* Used internally - xBestIndex should ignore */
7632 ** ^The left-hand operand of the operator is given by the corresponding
7633 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7636 ** operators have no left-hand operand, and so for those operators the
7645 ** The right-hand operands for each constraint might be accessible using
7646 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7648 ** in the input SQL. If the right-hand operand is another column or an
7652 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7657 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7852 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7854 ** read-only access.
7890 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7906 ** and the built-in [zeroblob] SQL function may be used to create a
7907 ** zero-filled blob to read or write using the incremental-blob interface.
7937 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7959 ** ^If the blob handle being closed was opened for read-write access, and if
7960 ** the database is in auto-commit mode and there are no other open read-write
7968 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7995 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8024 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8073 ** ^Names are zero-terminated UTF-8 strings.
8104 ** is selected automatically at compile-time. The following
8115 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8120 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8157 ** cases where it really needs one. If a faster non-recursive mutex
8204 ** then any of the four routines behaves as a no-op.
8217 ** An instance of this structure defines the low-level routines
8267 ** xMutexInit() must be no-ops.
8313 ** the routine should return 1. This seems counter-intuitive since
8317 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8333 ** next. Applications that override the built-in mutex logic must be
8349 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8364 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8370 ** CAPI3REF: Low-Level Control Of Database Files
8482 ** by enclosing in double-quotes) so as not to confuse the parser.
8487 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8490 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8496 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8508 ** <li> Put all identifier names inside double-quotes. This is the official
8519 ** compile-time options. For example, "VACUUM" is not a keyword if
8520 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8531 ** An instance of the sqlite3_str object contains a dynamically-sized
8555 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8595 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8600 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8601 ** S must contain at least N non-zero bytes of content. To append a
8602 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8606 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8609 ** single-byte character C onto the end of [sqlite3_str] object X.
8635 ** [SQLITE_NOMEM] following any out-of-memory error, or
8642 ** zero-termination byte.
8677 ** SQLITE_OK on success and a non-zero [error code] on failure.
8680 ** be represented by a 32-bit integer, then the values returned by
8698 ** These integer constants designate various run-time status parameters
8706 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8791 ** non-zero [error code] on failure.
8807 ** The [sqlite3_db_status()] interface will return a non-zero error code
8817 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8825 ** Only the high-water value is meaningful;
8833 ** Only the high-water value is meaningful;
8856 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8896 ** to disk all at once. When pages spill mid-transaction, that introduces
8967 ** A non-zero value in this counter may indicate an opportunity to
8973 ** A non-zero value in this counter may indicate an opportunity to
9002 ** step was bypassed because a Bloom filter returned not-found. The
9072 ** The built-in page cache is recommended for most uses.
9087 ** built-in default page cache is used instead of the application defined
9119 ** false if it is used for an in-memory database. The cache implementation
9130 ** suggested maximum cache-size (number of pages stored by) the cache
9176 ** as its second argument. If the third parameter, discard, is non-zero,
9210 ** is not obligated to free any memory, but well-behaved implementations should
9269 ** for copying in-memory databases to or from persistent files.
9275 ** ^The source database is read-locked only while it is being read;
9309 ** there is already a read or read-write transaction open on the
9341 ** <li> the destination database was opened read-only, or
9342 ** <li> the destination database is using write-ahead-log journaling
9344 ** <li> the destination database is an in-memory database and the
9348 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9349 ** the [sqlite3_busy_handler | busy-handler function]
9351 ** busy-handler returns non-zero before the lock is available, then
9372 ** sqlite3_backup_step(), the source database may be modified mid-way
9389 ** active write-transaction on the destination database is rolled back.
9396 ** ^If an out-of-memory condition or IO error occurred during any prior
9465 ** ^When running in shared-cache mode, a database operation may fail with
9466 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9467 ** individual tables within the shared-cache cannot be obtained. See
9468 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9472 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9476 ** ^Shared-cache locks are released when a database connection concludes
9480 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9490 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9496 ** ^If the blocked connection is attempting to obtain a write-lock on a
9497 ** shared-cache table, and more than one other connection currently holds
9498 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9501 ** ^(There may be at most one unlock-notify callback registered by a
9503 ** blocked connection already has a registered unlock-notify callback,
9506 ** unlock-notify callback is canceled. ^The blocked connections
9507 ** unlock-notify callback may also be canceled by closing the blocked
9510 ** The unlock-notify callback is not reentrant. If an application invokes
9511 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9519 ** When an unlock-notify callback is registered, the application provides a
9523 ** an unlock-notify callback is a pointer to an array of void* pointers,
9527 ** more than one blocked connection that has registered for an unlock-notify
9537 ** Assuming that after registering for an unlock-notify callback a
9548 ** unlock-notify callback is registered. The system is said to be in
9549 ** a deadlocked state if connection A has registered for an unlock-notify
9551 ** B has itself registered for an unlock-notify callback when connection
9554 ** registered for an unlock-notify callback on the conclusion of connection
9566 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9567 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9587 ** and extensions to compare the contents of two buffers containing UTF-8
9588 ** strings in a case-independent fashion, using the same definition of "case
9604 ** Note that this routine returns zero on a match and non-zero if the strings
9621 ** insensitive - equivalent upper and lower case ASCII characters match
9627 ** Note that this routine returns zero on a match and non-zero if the strings
9651 ** a fixed-length buffer on the stack. If the log message is longer than
9658 ** CAPI3REF: Write-Ahead Log Commit Hook
9665 ** the associated write-lock on the database released)^, so the implementation
9671 ** ^The third parameter is the name of the database that was written to -
9672 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9673 ** is the number of pages currently in the write-ahead log file,
9684 ** A single database handle may have at most a single write-ahead log callback
9686 ** previously registered write-ahead log callback. ^The return value is
9699 ** CAPI3REF: Configure an auto-checkpoint
9706 ** more frames in the [write-ahead log] file. ^Passing zero or
9721 ** ^Every new [database connection] defaults to having the auto-checkpoint
9737 ** [write-ahead log] for database X on [database connection] D to be
9738 ** transferred into the database file and for the write-ahead log to
9764 ** in the log were checkpointed. ^The [busy-handler callback]
9771 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9780 ** [busy-handler callback])
9793 ** the log file or to -1 if the checkpoint could not run because
9797 ** was called) or to -1 if the checkpoint could not run due to an error or
9805 ** busy-handler configured, it will not be invoked in this case.
9809 ** obtained immediately, and a busy-handler is configured, it is invoked and
9810 ** the writer lock retried until either the busy-handler returns 0 or the lock
9811 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9812 ** database readers as described above. ^If the busy-handler returns 0 before
9815 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9831 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9907 ** If X is non-zero, then the virtual table implementation guarantees
9951 ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
9982 ** [xUpdate] method understands as a "no-change" value.
10123 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10124 ** only sees a single value from the right-hand side of the IN operator
10128 ** table to see all values on the right-hand of the IN operator all at
10133 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10134 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10136 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10137 ** by which the virtual table can ask SQLite if all-at-once processing
10143 ** the IN operator all-at-once, respectively. ^Thus when the third
10144 ** parameter (F) is non-negative, this interface is the mechanism by
10153 ** (non-zero), that means that the constraint is an IN operator
10154 ** that can be processed all-at-once. ^If the constraint is not an IN
10155 ** operator or cannot be processed all-at-once, then the interface returns
10158 ** ^(All-at-once processing of the IN operator is selected if both of the
10162 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10164 ** use the N-th constraint.
10167 ** non-negative had F>=1.
10171 ** the traditional one-at-a-time processing strategy for the IN constraint.
10172 ** ^If both conditions are true, then the argvIndex-th parameter to the
10175 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10181 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10191 ** a parameter that was previously selected for all-at-once IN constraint
10194 ** an xFilter argument that was selected for all-at-once IN constraint
10197 ** ^(Use these routines to access all values on the right-hand side
10239 ** J being a 0-based index into P->aConstraint[], then this routine
10240 ** attempts to set *V to the value of the right-hand operand of
10241 ** that constraint if the right-hand operand is known. ^If the
10242 ** right-hand operand is not known, then *V is set to a NULL pointer.
10245 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10251 ** the right-hand operand of a constraint is a literal value in the original
10252 ** SQL statement. If the right-hand operand is an expression or a reference
10257 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10266 ** "Right-Hand Side".
10301 ** not available, the output variable is set to -1 if the value is numeric,
10307 ** set to the total number of times that the X-th loop has run.</dd>
10311 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10316 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10323 ** to a zero-terminated UTF-8 string containing the name of the index or table
10324 ** used for the X-th loop.
10328 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10329 ** description for the X-th loop.
10333 ** id for the X-th query plan element. The id value is unique within the
10334 ** statement. The select-id is the same value as is output in the first
10345 ** according to the processor time-stamp counter, that elapsed while the
10347 ** all query elements - if it is unavailable the output variable is
10348 ** set to -1.
10371 ** compile-time option.
10379 ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10389 ** for. Query elements are numbered starting from zero. A value of -1 may be
10391 ** - less than -1 or greater than or equal to the total number of query
10392 ** elements used to implement the statement - a non-zero value is returned and
10418 ** CAPI3REF: Zero Scan-Status Counters
10423 ** This API is only available if the library is built with pre-processor
10429 ** CAPI3REF: Flush caches to disk mid-transaction
10432 ** ^If a write-transaction is open on [database connection] D when the
10434 ** pages in the pager-cache that are not currently in use are written out
10438 ** interface flushes caches for all schemas - "main", "temp", and
10443 ** immediately and there is a busy-handler callback configured, it is invoked
10451 ** example an IO error or out-of-memory condition), then processing is
10462 ** CAPI3REF: The pre-update hook.
10466 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10542 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10543 ** triggers; or 2 for changes resulting from triggers called by top-level
10547 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10552 ** pre-update hook is being invoked for some other reason, including a
10553 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10579 ** CAPI3REF: Low-level system error code
10584 ** The return value is OS-dependent. For example, on unix systems, after
10624 ** If there is not already a read-transaction open on schema S when
10656 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10690 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10691 ** SQLITE_IOERR error code - is returned, then the final state of the
10705 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10722 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10789 ** For an ordinary on-disk database file, the serialization is just a
10790 ** copy of the disk file. For an in-memory database or a "TEMP" database,
10827 ** Zero or more of the following constants can be OR-ed together for
10831 ** a pointer to contiguous in-memory database that it is currently using,
10833 ** a contiguous in-memory database, then this option causes
10835 ** using a contiguous in-memory database if it has been initialized by a
10845 ** reopen S as an in-memory database based on the serialization contained
10849 ** permitted to add content to the in-memory database as long as the total
10901 ** should be treated as read-only.
10905 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10958 /* The double-precision datatype used by RTree depends on the
10959 ** SQLITE_RTREE_INT_ONLY compile-time option.
10969 ** R-Tree geometry query as follows:
10994 ** Register a 2nd-generation geometry callback named zScore that can be
10995 ** used as part of an R-Tree geometry query as follows:
11101 ** is not possible for an application to register a pre-update hook on a
11104 ** which a pre-update hook is already defined. The results of attempting
11185 ** disabled - it does not. A newly created session object is enabled.
11192 ** no-op, and may be used to query the current state of the session.
11269 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11275 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11279 ** conflict-handler callback) then the X'' value is returned. The application
11327 ** database row along with the updated values for each updated non-primary-key
11336 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11338 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11351 ** are sorted in the same order in which they were attached (or auto-attached)
11365 ** recorded once - the first time a row with said primary key is inserted,
11372 ** The session object therefore accumulates two types of records - those
11390 ** found and one or more of the non-primary key fields have been
11407 ** This may appear to have some counter-intuitive effects if a single row
11423 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11445 ** does not have a primary key, this function is a no-op (but does not return
11465 ** used to update the table in database zFrom (call this the "from-table")
11467 ** object (call this the "to-table"). Specifically:
11470 ** <li> For each row (primary key) that exists in the to-table but not in
11471 ** the from-table, an INSERT record is added to the session object.
11473 ** <li> For each row (primary key) that exists in the to-table but not in
11474 ** the from-table, a DELETE record is added to the session object.
11477 ** different non-PK values in each, an UPDATE record is added to the
11522 ** Because the non-primary key "old.*" fields are omitted, no
11541 ** Return non-zero if no changes to attached tables have been recorded by
11549 ** are restored. However, if this function returns non-zero, then it is
11636 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11660 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11673 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11676 ** or until the conflict-handler function returns.
11723 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11732 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11763 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11797 ** conflict-handler callback by [sqlite3changeset_apply()] with either
11808 ** "conflicting row" associated with the current conflict-handler callback
11846 ** function with an iterator passed to a conflict-handler by
11854 ** returned. This is to allow the following pattern (pseudo-code):
12007 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12008 ** <tr><th style="white-space:pre">Existing Change </th>
12009 ** <th style="white-space:pre">New Change </th>
12054 ** returned. Or, if an out-of-memory condition occurs during processing, this
12112 ** Otherwise, if the return value is non-zero or the xFilter argument to
12148 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12152 ** returned by each invocation of the conflict-handler function. Refer to
12161 ** stored in all non-primary key columns also match the values stored in
12165 ** the non-primary key fields contains a value different from the original
12166 ** row value stored in the changeset, the conflict-handler function is
12169 ** only the values of those non-primary key fields are compared against
12170 ** the current database contents - any trailing database table columns
12174 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12179 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12198 ** This includes the case where the INSERT operation is re-attempted because
12206 ** stored in all modified non-primary key columns also match the values
12210 ** the modified non-primary key fields contains a value different from an
12211 ** original row value stored in the changeset, the conflict-handler function
12213 ** UPDATE changes only contain values for non-primary key fields that are
12215 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12218 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12222 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12240 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12331 ** Values that may be passed as the second argument to a conflict-handler.
12338 ** (non primary-key) fields modified by the update do not contain the
12499 ** combined on a per-field basis, not per-row. This means that in the
12583 ** corresponding non-streaming API functions:
12585 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12586 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12596 ** Non-streaming functions that accept changesets (or patchsets) as input
12601 ** low-memory environment is required to handle very large changesets, the
12865 ** non-negative but less than the number of columns in the table, return
12880 ** non-negative but less than the number of columns in the table, set
12894 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12992 ** If the bClear argument is non-zero, then the auxiliary data is cleared
13014 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13016 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13022 ** modify this structure directly - it should only be used as shown above
13030 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13044 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13046 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13055 ** xPhraseFirstColumn() set iCol to -1).
13124 ** The second and third arguments are an array of nul-terminated strings
13142 ** by argument pText. pText may or may not be nul-terminated. The first
13150 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13155 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13159 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13164 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13197 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13231 ** still appears to contain just two phrases - "(first OR 1st)"
13242 ** when tokenizing query text (it should not - to do so would be
13289 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13390 ** autoconf-based build
13418 ** The hard limit is the ability of a 32-bit signed integer
13419 ** to count the size: 2^31-1 or 2147483647.
13437 ** tell you that in a well-normalized database, you usually should
13495 ** The suggested maximum number of in-memory pages to use for
13498 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13500 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13501 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13504 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13518 ** counted using a signed 8-bit integer which has a maximum value of 127
13530 ** as a signed 32-bit integer can hold.
13537 ** imposed by the use of 16-bit offsets within each page.
13543 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13544 ** compiled with the default page-size limit will not be able to rollback
13567 ** device characteristics (sector-size and atomic write() support),
13584 ** This value can be lowered (or raised) at run-time using that the
13615 #pragma warn -rch /* unreachable code */
13616 #pragma warn -ccc /* Condition is always true or false */
13617 #pragma warn -aus /* Assigned value is never used */
13618 #pragma warn -csu /* Comparing signed and unsigned */
13619 #pragma warn -spa /* Suspicious pointer arithmetic */
13627 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13660 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13668 #elif defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
13673 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13674 #else /* Generates a warning - but it always works */
13721 ** level of threadsafety. 2 means the library is multithreaded - multiple
13729 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13737 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13743 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13750 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13779 # error "Two or more of the following compile-time configuration options\
13817 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13882 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13894 ** of SQLite to unexpected behavior - to make the code "self-healing"
13900 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
13986 ** Return true (non-zero) if the input is an integer that is too large
13987 ** to fit in 32-bits. This macro is used inside of various testcase()
13988 ** macros to verify that we have tested SQLite for large-file support.
14015 ** This is the header file for the generic hash-table implementation
14026 ** The internals of this structure are intended to be opaque -- client
14033 ** All elements of the hash table are on a single doubly-linked list.
14037 ** the global doubly-linked list. The contents of the bucket are the
14038 ** element pointed to plus the next _ht.count-1 elements in the list.
14057 ** structure. All elements are stored on a single doubly-linked list.
14088 #define sqliteHashFirst(H) ((H)->first)
14089 #define sqliteHashNext(E) ((E)->next)
14090 #define sqliteHashData(E) ((E)->data)
14091 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14092 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14097 #define sqliteHashCount(H) ((H)->count)
14307 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14312 ** substitute integer for floating-point
14343 ** the VDBE-level file format changes. The following macros define the
14354 ** changed at run-time using a pragma.
14362 ** on the command-line
14392 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14394 ** The default value of "20" was chosen to minimize the run-time of the
14395 ** speedtest1 test program with options: --shrink-memory --reprepare
14409 ** The compile-time options SQLITE_MMAP_READWRITE and
14418 ** GCC does not define the offsetof() macro so we'll have to do it
14422 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14453 ** types can be conveniently redefined at compile-type. Like this:
14455 ** cc '-DUINTPTR_TYPE=long long int' ...
14495 typedef sqlite_int64 i64; /* 8-byte signed integer */
14496 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14497 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14498 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14499 typedef INT16_TYPE i16; /* 2-byte signed integer */
14500 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14501 typedef INT8_TYPE i8; /* 1-byte signed integer */
14509 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14518 ** Estimated quantities used for query planning are stored as 16-bit
14520 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14529 ** 1 -> 0 20 -> 43 10000 -> 132
14530 ** 2 -> 10 25 -> 46 25000 -> 146
14531 ** 3 -> 16 100 -> 66 1000000 -> 199
14532 ** 4 -> 20 1000 -> 99 1048576 -> 200
14533 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14538 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14581 ** and whether or not that determination is run-time or compile-time.
14583 ** For best performance, an attempt is made to guess at the byte-order
14584 ** using C-preprocessor macros. If that is unsuccessful, or if
14585 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14586 ** at run-time.
14621 ** Constants for the largest and smallest possible 64-bit signed integers.
14622 ** These macros are designed to work correctly on both 32-bit and 64-bit
14627 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14631 ** to force 8-byte alignment on 64-bit architectures.
14636 ** pointers in size, and so it is a no-op on systems where the pointer
14652 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14657 ** underlying malloc() implementation might return us 4-byte aligned
14658 ** pointers. In that case, only verify 4-byte alignment.
14661 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
14663 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
14675 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14695 ** default MMAP_SIZE is specified at compile-time, make sure that it does
14719 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14731 ** 0x00000008 Result-set wildcard expansion
14737 ** 0x00000200 Count-of-view optimization
14742 ** 0x00004000 Push-down optimization
14743 ** 0x00008000 After all FROM-clause analysis
14764 ** (---any--) Top-level block structure
14765 ** 0x-------F High-level debug messages
14766 ** 0x----FFF- More detail
14767 ** 0xFFFF---- Low-level debug messages
14793 ** An instance of the following structure is used to store the busy-handler
14798 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14832 ** The root-page of the schema table.
14851 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14870 ** directly, we use its constant as a key to lookup the run-time allocated
14872 ** for the run-time allocated buffer.
14875 ** macros become no-ops and have zero performance impact.
14973 ** Changing this from a 64-bit to a 32-bit type limits the number of
14995 #define ALLBITS ((Bitmask)-1)
14996 #define TOPBIT (((Bitmask)1)<<(BMS-1))
15025 ** This header file (together with is companion C source-code file
15029 ** This header file is #include-ed by sqliteInt.h and thus ends up
15037 ** necessary pre-processor macros for it.
15053 ** This file contains pre-processor directives related to operating system
15074 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15137 ** a no-op
15170 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15172 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15173 ** Mcafee started using SQLite in their anti-virus product and it
15219 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15327 ** sqlite3_malloc() to obtain space for the file-handle structure.
15363 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15386 ** is devoted to storing a super-journal name - there are no more pages to
15390 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15391 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15399 #define PAGER_MEMORY 0x0002 /* In-memory database */
15404 #define PAGER_LOCKINGMODE_QUERY -1
15415 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15420 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15421 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15427 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15449 ** that make up the Pager sub-system API. See source code comments for
15597 ** This header file defines the interface that the sqlite B-Tree file
15610 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15617 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15618 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15631 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15646 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15647 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15701 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15704 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15707 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15708 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15728 ** For example, the free-page-count field is located at byte offset 36 of
15729 ** the database file header. The incr-vacuum-flag field is located at
15733 ** It is a read-only number computed by the pager. But we merge it with
15746 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15760 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15761 ** column of the b-tree of the cursor. The Expr tree will not contain
15762 ** any function calls nor subqueries nor references to b-trees other than
15765 ** The design of the _RANGE hint is aid b-tree implementations that try
15766 ** to prefetch content from remote machines - to provide those
15791 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15796 ** For read-only cursors the wrFlag argument is always zero. For read-write
15807 ** by this, the native b-tree engine of SQLite, but it is available to
15809 ** b-tree system. For alternative storage engines in which a delete of
15813 ** and DELETE operations as no-ops, and any READ operation against a
15816 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
15822 int wrFlag, /* 1 for writing. 0 for read-only */
15969 ** Enter and Leave procedures no-ops.
16056 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16083 u32 iSrcLine; /* Source-code line that generated this opcode
16095 ** A sub-routine used to implement a trigger program.
16098 VdbeOp *aOp; /* Array of opcodes for sub-program */
16104 SubProgram *pNext; /* Next sub-program already visited */
16124 #define P4_STATIC (-1) /* Pointer to a static string */
16125 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16126 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16127 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16128 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16130 #define P4_FREE_IF_LE (-6)
16131 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16132 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16133 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16134 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
16135 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
16136 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
16137 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16138 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16139 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16140 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16241 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16242 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16243 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16272 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16289 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16306 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16342 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16419 ** Additional non-public SQLITE_PREPARE_* flags
16474 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16479 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16556 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16593 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16598 ** // in distingishing equal and not-equal.
16601 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16602 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16610 ** is responsible for keeping track of this and reporting byte-code branches
16748 /* Modify the page-size after the cache has been created. */
16770 /* Change a page number. Used by incr-vacuum. */
16812 /* Set and get the suggested cache-size for the specified pager-cache.
16815 ** the total number of pages cached by purgeable pager-caches to the sum
16816 ** of the suggested cache-sizes.
16823 /* Set or get the suggested spill-size for the specified pager-cache.
16825 ** The spill-size is the minimum number of pages in cache before the cache
16889 ** at start-time.
16891 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
16894 ** start-time.
16896 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16898 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16915 ** If this is a no-op implementation, implement everything as macros.
16936 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16956 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16957 ** In other words, the zero-based numbers are used for all external interfaces
16958 ** and the one-based values are used internally.
16986 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
17015 ** Db.pSchema->flags field.
17017 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17018 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17019 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17020 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17023 ** Allowed values for the DB.pSchema->flags field.
17043 ** Lookaside malloc is a set of fixed-size buffers that can be used
17065 ** in a performance-critical path. sz should be set by to szTrue whenever
17070 ** come off of pFree first, then pInit as a fallback. This dual-list
17071 ** allows use to compute a high-water mark - the maximum number of allocations
17072 ** outstanding at any point in the past - by subtracting the number of
17075 ** Enhancement on 2019-12-12: Two-size-lookaside
17079 ** The two-size-lookaside enhancement breaks up the lookaside allocation
17080 ** into two pools: One of 128-byte slots and the other of the default size
17081 ** (1200-byte) slots. Allocations are filled from the small-pool first,
17082 ** failing over to the full-size pool if that does not work. Thus more
17099 void *pMiddle; /* First byte past end of full-size buffers and
17104 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17110 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17111 #define EnableLookaside db->lookaside.bDisable--;\
17112 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17114 /* Size of the smaller allocations in two-size lookside */
17122 ** A hash table for built-in function definitions. (Application-defined
17205 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17213 u8 autoCommit; /* The auto-commit flag. */
17217 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17223 u8 noSharedCache; /* True if no shared-cache backends */
17271 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17309 int nSavepoint; /* Number of non-transaction savepoints */
17310 int nStatement; /* Number of nested statement-transactions */
17339 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17340 #define ENC(db) ((db)->enc)
17345 ** C-compilers still do not accept LL integer literals.
17388 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17389 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17395 #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17411 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17432 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17436 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17438 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17446 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17451 #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17458 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17459 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17465 #define ConstFactorOk(P) ((P)->okConstFactor)
17480 ** structure. For global built-in functions (ex: substr(), max(), count())
17482 ** For per-connection application-defined functions, a pointer to this
17483 ** structure is held in the db->aHash hash table.
17485 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17486 ** field is used by per-connection app-def functions.
17489 i8 nArg; /* Number of arguments. -1 means unlimited */
17493 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17496 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17505 ** This structure encapsulates a user-function destructor callback (as
17537 ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
17544 ** See multiple instances of tag-20230109-1.
17548 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17551 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17552 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17553 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17554 /* 0x0200 -- available for reuse */
17555 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17559 ** single query - might change over time */
17560 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17561 /* 0x8000 -- available for reuse */
17562 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17565 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17567 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17568 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17571 /* Identifier numbers for each in-line function */
17589 ** as the user-data (sqlite3_user_data()) for the function. If
17600 ** zName is the name of a function that is implemented by in-line
17606 ** zName is the name of a test-only function implemented by in-line
17615 ** a single query. The iArg is ignored. The user-data is always set
17619 ** For math-library functions. xPtr is an arbitrary pointer.
17624 ** ignored and the user-data for these functions is set to an
17625 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17643 ** available as the function user-data (sqlite3_user_data()). The
17707 char *zName; /* Savepoint name (nul-terminated) */
17768 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17799 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17819 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17831 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17885 ** database connections, even when the rest of the in-memory database
17895 ** database schema are initially stored in a linked-list pointed to by
17902 ** When an in-memory Table object is deleted (for example when the
17953 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17984 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17985 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17995 #define TF_Readonly 0x00000001 /* Read-only system table */
18006 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18007 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18022 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18023 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18031 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18033 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18041 ** only works for non-virtual tables (ordinary tables and views) and is
18046 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18047 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18049 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18058 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18059 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18074 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18077 ** from-table == child-table
18078 ** to-table == parent-table
18081 ** which is attached to the from-table. The to-table need not exist when
18082 ** the from-table is created. The existence of the to-table is not checked.
18096 /* EV: R-30323-21917 */
18162 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18190 ** pKeyInfo->nField.
18194 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18198 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18199 ** multiple entries in the b-tree with the same key (when only looking
18200 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18205 ** get and equal results when comparing this structure to a b-tree record.
18209 ** b-tree.
18212 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18251 ** algorithm to employ when an attempt is made to insert a non-unique
18258 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18260 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18261 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18271 ** number (it cannot - the database page is not allocated until the VDBE
18296 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18308 IndexSample *aSample; /* Samples of the left-most key */
18309 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18310 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18324 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18327 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18332 #define XN_ROWID (-1) /* Indexed column is the rowid */
18333 #define XN_EXPR (-2) /* Indexed column is an expression */
18365 const char *z; /* Text of the token. Not NULL-terminated! */
18389 int sortingIdxPTab; /* Cursor number of pseudo-table */
18420 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
18423 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
18425 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
18428 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18429 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18430 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18435 ** the option is available (at compile-time).
18450 ** to represent the greater-than-or-equal-to operator in the expression
18464 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18483 ** subquery gives a constant result, then iTable is -1. If the subquery
18488 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18519 int iValue; /* Non-negative integer value if EP_IntValue */
18544 ** TK_TRIGGER: 1 -> new, 0 -> old
18549 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18552 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18616 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18617 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18618 #define ExprSetProperty(E,P) (E)->flags|=(P)
18619 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18620 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18621 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18626 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18627 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18628 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18629 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18630 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18631 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18632 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18641 ** processes but is a no-op for delivery.
18644 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18645 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18646 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18666 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18676 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18692 ** ---------- -------------------------
18751 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18760 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18771 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18782 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18783 ** contains more than 63 columns and the 64-th or later column is used.
18801 int regResult; /* Registers holding results of a co-routine */
18806 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18807 unsigned isCorrelated :1; /* True if sub-query is correlated */
18809 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18815 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18827 ExprList *pFuncArg; /* Arguments to table-valued-function */
18877 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18880 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18883 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18912 ** NameContexts can be nested. When resolving names, the inner-most
18928 ExprList *pEList; /* Optional list of result-set columns */
18969 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
18977 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18978 ** conflict-target clause.) The pUpsertTargetWhere is the optional
19082 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
19089 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
19097 ** identified by pDest->iSDParm.
19099 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
19101 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
19106 ** the side-effects of functions.
19113 ** in register pDest->iSDParm then abandon the rest
19117 ** row of result as the key in table pDest->iSDParm.
19118 ** Apply the affinity pDest->affSdst before storing
19121 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
19127 ** SRT_Coroutine Generate a co-routine that returns a new row of
19129 ** of the co-routine is stored in register pDest->iSDParm
19130 ** and the result row is stored in pDest->nDest registers
19131 ** starting with pDest->iSdst.
19133 ** SRT_Table Store results in temporary table pDest->iSDParm.
19139 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
19140 ** But also use temporary table pDest->iSDParm+1 as
19144 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
19148 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
19150 ** index at pDest->iSDParm+1 hold all prior stores.
19153 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
19154 ** table is an intkey table - in this case the first
19156 ** key. If (pDest->iSDParm>0), then the table is an index
19157 ** table. (pDest->iSDParm) is the number of key columns in
19169 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19175 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19203 ** the code generator needs. We have to keep per-table autoincrement
19222 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19304 ** The nTableLock and aTableLock variables are only used if the shared-cache
19305 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19306 ** used to store the set of table-locks required by the statement being
19339 ** of the base register during check-constraint eval */
19350 int nMaxArg; /* Max args passed to user function by sub-program */
19354 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19402 int nHeight; /* Expression tree height of current sub-select */
19414 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19436 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19438 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19447 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19453 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19459 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19483 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19487 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19491 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19526 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19527 the <column-list> is stored here */
19546 ** that is a part of a trigger-program.
19551 ** the first step of the trigger-program.
19558 ** orconf -> stores the ON CONFLICT algorithm
19559 ** pSelect -> The content to be inserted - either a SELECT statement or
19561 ** zTarget -> Dequoted name of the table to insert into.
19562 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19563 ** statement, then this stores the column-names to be
19565 ** pUpsert -> The ON CONFLICT clauses for an Upsert
19568 ** zTarget -> Dequoted name of the table to delete from.
19569 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19573 ** zTarget -> Dequoted name of the table to update.
19574 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19576 ** pExprList -> A list of the columns to update and the expressions to update
19581 ** pSelect -> The SELECT statement
19584 ** pExprList -> The list of expressions that follow the RETURNING keyword.
19600 TriggerStep *pNext; /* Next in the link-list */
19601 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19630 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19634 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19660 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19663 ** be used on trunk check-ins. They are a temporary mechanism available
19668 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
19670 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
19685 u8 bUseCis; /* Use covering indices for full-scans */
19689 int neverCorrupt; /* Database is always well-formed */
19692 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19693 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19694 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19695 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19705 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19707 /* The above might be initialized to non-zero. The following need to always
19738 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19740 /* vvvv--- must be last ---vvv */
19748 ** the assert is only valid on a well-formed database. Instead of:
19760 ** things that are always true for well-formed databases.
19765 ** Context pointer passed down through the tree-walk.
19807 const char *zType; /* Type of the container - used for error messages */
19808 const Token *pName; /* Name of the container - used for error messages */
19835 ** Return code from the parse-tree walking primitives and their
19896 ** data structures on sqlite3DebugPrintf() using a tree-like view.
19913 ** (2) All window functions in a single SELECT form a linked-list
19983 ** Assuming zIn points to the first byte of a UTF-8 character,
19984 ** advance zIn to point to the first byte of the next UTF-8 character.
19995 ** routines that report the line-number on which the error originated
20040 ** The ctype.h header is needed for non-ASCII systems. It is also
20176 # define MAN754 ((((u64)1)<<52)-1)
20321 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20322 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20331 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20591 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20592 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20654 ** Routines to read and write variable-length integers. These used to
20873 ** The interface to the LEMON-generated parser
20934 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
21010 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
21012 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
21014 ** provided (enforcement of FK constraints requires the triggers sub-system).
21065 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21118 ** sqlite3IoTrace is a pointer to a printf-like routine used to
21151 ** passed back to non-lookaside free() routines. Asserts such as the
21152 ** example above are placed on the non-lookaside free() routines to verify
21155 ** All of this is no-op for a production build. It only comes into
21156 ** play when the SQLITE_MEMDEBUG compile-time option is used.
21163 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21207 # define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
21229 ** all of the platform-specific files (os_*.c) and is #included into those
21241 ** switch. The following code should catch this problem at compile-time.
21256 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21280 || sqlite3_io_error_pending-- == 1 ) \
21295 sqlite3_diskfull_pending--; \
21338 ** This file implements routines used to report what compile-time options
21341 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21345 ** autoconf-based build
21359 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21365 ** An array of names of all compile-time options. This array should
21366 ** be sorted A-Z.
21369 ** only a handful of compile-time options, so most times this array is usually
21407 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21411 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21413 "COMPILER=gcc-" __VERSION__,
22124 /* An array to map all upper-case characters into their corresponding
22125 ** lower-case character.
22127 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22167 /* All of the upper-to-lower conversion data is above. The following
22184 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22185 ** be undefined behavior. That's goofy, but the C-standards people thought
22193 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22194 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22195 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22198 ** The following 256 byte lookup table is used to support SQLites built-in
22207 ** SQLite identifier character 0x40 $, _, or non-ascii
22211 ** case. i.e. if the character is a lower-case ASCII character.
22212 ** If x is a lower-case ASCII character, then its upper-case equivalent
22213 ** is (x - 0x20). Therefore toupper() can be implemented as:
22220 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22222 ** non-ASCII UTF character. Hence the test for whether or not a character is
22231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22263 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22267 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22268 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22270 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22278 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22279 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22280 ** that compile-time option is omitted.
22286 # error "Compile-time disabling of covering index scan using the\
22287 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22303 ** before 3.12.0). -1 means always keep the entire statement journal in
22313 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22315 ** and N is the number of slots. The lookaside-configuration can be
22316 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22317 ** or at run-time for an individual database connection using
22320 ** With the two-size-lookaside enhancement, less lookaside is required.
22321 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22322 ** and 93 128-byte slots, which is more lookaside than is available
22323 ** using the older 1200,100 configuration without two-size-lookaside.
22334 /* The default maximum size of an in-memory database created using
22406 ** Hash table for global functions - functions common to all
22408 ** read-only.
22427 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22434 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
22591 ** * A b-tree cursor
22592 ** - In the main database or in an ephemeral database
22593 ** - On either an index or a table
22596 ** * A one-row "pseudotable" stored in a single register
22601 i8 iDb; /* Index of cursor database in db->aDb[] */
22610 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22660 /* Return true if P is a null-only cursor
22663 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
22672 ** When a sub-program is executed (OP_Program), a structure of this type
22675 ** values stored in the Vdbe struct. When the sub-program is finished,
22677 ** restoring the state of the VM to as it was before the sub-program
22713 i64 nDbChange; /* Value of db->nChange */
22748 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22767 ** UPDATE no-change flag set
22773 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22777 ** length Mem.n. Zero-terminated if
22833 /* Return TRUE if Mem X contains dynamically allocated content - anything
22837 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
22843 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
22846 ** True if Mem X is a NULL-nochng type.
22849 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
22850 && (X)->n==0 && (X)->u.nZero==0)
22861 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
22927 int iSelectID; /* The "Select-ID" for this loop */
22932 /* The DblquoteStr object holds the text of a double-quoted
22936 ** list is consulted for each double-quoted identifier to see if the
22984 i64 startTime; /* Time when query started - used for profiling */
22997 bft changeCntOn:1; /* True to update the change-counter */
23001 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23007 DblquoteStr *pDblStr; /* List of double-quoted string literals */
23014 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23054 ** set of values on the right-hand side of an IN constraint.
23070 ** single-byte varint.
23227 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23272 ** we have to locate the state vector at run-time. In the more common
23302 ** The value of N is added to the current status value and the high-water
23306 ** mark is unchanged. N must be non-negative for StatusDown().
23326 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
23398 p = p->pNext; in countLookasideSlots()
23408 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
23409 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
23411 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
23412 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
23414 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
23415 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
23425 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
23426 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
23434 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
23439 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
23441 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
23442 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
23443 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
23444 db->lookaside.pFree = 0; in sqlite3_db_status()
23447 p = db->lookaside.pSmallFree; in sqlite3_db_status()
23449 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
23450 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
23451 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
23452 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
23465 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
23466 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
23468 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
23470 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
23485 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
23486 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
23512 db->pnBytesFreed = &nByte; in sqlite3_db_status()
23513 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
23514 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
23515 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
23516 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
23521 pSchema->tblHash.count in sqlite3_db_status()
23522 + pSchema->trigHash.count in sqlite3_db_status()
23523 + pSchema->idxHash.count in sqlite3_db_status()
23524 + pSchema->fkeyHash.count in sqlite3_db_status()
23526 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
23527 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
23528 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
23529 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
23531 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
23534 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
23539 db->pnBytesFreed = 0; in sqlite3_db_status()
23540 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
23557 db->pnBytesFreed = &nByte; in sqlite3_db_status()
23558 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
23559 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
23560 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ in sqlite3_db_status()
23563 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
23564 db->pnBytesFreed = 0; in sqlite3_db_status()
23566 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
23588 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
23589 if( db->aDb[i].pBt ){ in sqlite3_db_status()
23590 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
23594 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
23595 /* IMP: R-54100-20147 */ in sqlite3_db_status()
23596 /* IMP: R-29431-39229 */ in sqlite3_db_status()
23601 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
23606 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
23607 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
23615 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
23635 ** There is only one exported symbol in this file - the function
23644 ** 1970-01-01 00:00:00 is JD 2440587.5
23645 ** 2000-01-01 00:00:00 is JD 2451544.5
23647 ** This implementation requires years to be expressed as a 4-digit number
23648 ** which means that only dates between 0000-01-01 and 9999-12-31 can
23654 ** use the julian calendar for dates prior to 1582-10-15 and for some
23662 ** ISBN 0-943396-61-1
23663 ** Willmann-Bell, Inc
23710 ** of a four-character format specifiers ABCD is:
23724 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23725 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23726 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23727 ** the 2-digit day which is the last integer in the set.
23740 char N = zFormat[0] - '0'; in getDigits()
23741 char min = zFormat[1] - '0'; in getDigits()
23746 max = aMx[zFormat[2] - 'a']; in getDigits()
23749 while( N-- ){ in getDigits()
23753 val = val*10 + *zDate - '0'; in getDigits()
23770 ** Parse a timezone extension on the end of a date-time.
23773 ** (+/-)HH:MM
23780 ** of change in p->tz and return 0. If a parser error occurs,
23781 ** return non-zero.
23790 p->tz = 0; in parseTimezone()
23792 if( c=='-' ){ in parseTimezone()
23793 sgn = -1; in parseTimezone()
23807 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
23810 p->tzSet = 1; in parseTimezone()
23838 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
23847 p->validJD = 0; in parseHhMmSs()
23848 p->rawS = 0; in parseHhMmSs()
23849 p->validHMS = 1; in parseHhMmSs()
23850 p->h = h; in parseHhMmSs()
23851 p->m = m; in parseHhMmSs()
23852 p->s = s + ms; in parseHhMmSs()
23854 p->validTZ = (p->tz!=0)?1:0; in parseHhMmSs()
23863 p->isError = 1; in datetimeError()
23867 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
23868 ** that the YYYY-MM-DD is according to the Gregorian calendar.
23875 if( p->validJD ) return; in computeJD()
23876 if( p->validYMD ){ in computeJD()
23877 Y = p->Y; in computeJD()
23878 M = p->M; in computeJD()
23879 D = p->D; in computeJD()
23881 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
23885 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
23890 Y--; in computeJD()
23894 B = 2 - A + (A/4); in computeJD()
23897 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
23898 p->validJD = 1; in computeJD()
23899 if( p->validHMS ){ in computeJD()
23900 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); in computeJD()
23901 if( p->validTZ ){ in computeJD()
23902 p->iJD -= p->tz*60000; in computeJD()
23903 p->validYMD = 0; in computeJD()
23904 p->validHMS = 0; in computeJD()
23905 p->validTZ = 0; in computeJD()
23913 ** YYYY-MM-DD HH:MM:SS.FFF
23914 ** YYYY-MM-DD HH:MM:SS
23915 ** YYYY-MM-DD HH:MM
23916 ** YYYY-MM-DD
23919 ** on success and 1 if the input string is not a well-formed
23925 if( zDate[0]=='-' ){ in parseYyyyMmDd()
23931 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
23939 p->validHMS = 0; in parseYyyyMmDd()
23943 p->validJD = 0; in parseYyyyMmDd()
23944 p->validYMD = 1; in parseYyyyMmDd()
23945 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
23946 p->M = M; in parseYyyyMmDd()
23947 p->D = D; in parseYyyyMmDd()
23948 if( p->validTZ ){ in parseYyyyMmDd()
23960 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
23961 if( p->iJD>0 ){ in setDateTimeToCurrent()
23962 p->validJD = 1; in setDateTimeToCurrent()
23973 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23976 p->s = r; in setRawDateNumber()
23977 p->rawS = 1; in setRawDateNumber()
23979 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
23980 p->validJD = 1; in setRawDateNumber()
23990 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23994 ** In the first form, the +/-HH:MM is always optional. The fractional
24018 p->useSubsec = 1; in parseDateOrTime()
24024 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24028 ** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
24047 if( p->validYMD ) return; in computeYMD()
24048 if( !p->validJD ){ in computeYMD()
24049 p->Y = 2000; in computeYMD()
24050 p->M = 1; in computeYMD()
24051 p->D = 1; in computeYMD()
24052 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
24056 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
24057 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
24058 A = Z + 1 + A - (A/4); in computeYMD()
24060 C = (int)((B - 122.1)/365.25); in computeYMD()
24062 E = (int)((B-D)/30.6001); in computeYMD()
24064 p->D = B - D - X1; in computeYMD()
24065 p->M = E<14 ? E-1 : E-13; in computeYMD()
24066 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
24068 p->validYMD = 1; in computeYMD()
24076 if( p->validHMS ) return; in computeHMS()
24078 s = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
24079 p->s = s/1000.0; in computeHMS()
24080 s = (int)p->s; in computeHMS()
24081 p->s -= s; in computeHMS()
24082 p->h = s/3600; in computeHMS()
24083 s -= p->h*3600; in computeHMS()
24084 p->m = s/60; in computeHMS()
24085 p->s += s - p->m*60; in computeHMS()
24086 p->rawS = 0; in computeHMS()
24087 p->validHMS = 1; in computeHMS()
24102 p->validYMD = 0; in clearYMD_HMS_TZ()
24103 p->validHMS = 0; in clearYMD_HMS_TZ()
24104 p->validTZ = 0; in clearYMD_HMS_TZ()
24114 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24128 ** using whatever operating-system specific localtime facility that
24130 ** non-zero on any kind of error.
24132 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24135 ** invoked in place of the OS-defined localtime() function.
24137 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24203 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ in toLocaltime()
24204 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ in toLocaltime()
24206 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in toLocaltime()
24213 iYearDiff = (2000 + x.Y%4) - x.Y; in toLocaltime()
24217 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24220 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); in toLocaltime()
24223 sqlite3_result_error(pCtx, "local time unavailable", -1); in toLocaltime()
24226 p->Y = sLocal.tm_year + 1900 - iYearDiff; in toLocaltime()
24227 p->M = sLocal.tm_mon + 1; in toLocaltime()
24228 p->D = sLocal.tm_mday; in toLocaltime()
24229 p->h = sLocal.tm_hour; in toLocaltime()
24230 p->m = sLocal.tm_min; in toLocaltime()
24231 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; in toLocaltime()
24232 p->validYMD = 1; in toLocaltime()
24233 p->validHMS = 1; in toLocaltime()
24234 p->validJD = 0; in toLocaltime()
24235 p->rawS = 0; in toLocaltime()
24236 p->validTZ = 0; in toLocaltime()
24237 p->isError = 0; in toLocaltime()
24247 ** Where NNN is an arbitrary floating-point number and "days" can be one
24265 ** Process a modifier to a date-time stamp. The modifiers are
24306 if( idx>1 ) return 1; /* IMP: R-33611-57934 */ in parseModifier()
24307 if( !p->rawS || p->validJD ){ in parseModifier()
24309 p->rawS = 0; in parseModifier()
24310 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ in parseModifier()
24311 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ in parseModifier()
24313 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
24315 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
24316 p->validJD = 1; in parseModifier()
24317 p->rawS = 0; in parseModifier()
24327 ** Always interpret the prior number as a julian-day value. If this in parseModifier()
24333 if( idx>1 ) return 1; /* IMP: R-31176-64601 */ in parseModifier()
24334 if( p->validJD && p->rawS ){ in parseModifier()
24336 p->rawS = 0; in parseModifier()
24358 ** Treat the current value of p->s as the number of in parseModifier()
24361 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
24362 if( idx>1 ) return 1; /* IMP: R-49255-55373 */ in parseModifier()
24363 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
24366 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
24367 p->validJD = 1; in parseModifier()
24368 p->rawS = 0; in parseModifier()
24374 if( p->tzSet==0 ){ in parseModifier()
24381 iGuess = iOrigJD = p->iJD; in parseModifier()
24386 iGuess -= iErr; in parseModifier()
24392 iErr = new.iJD - iOrigJD; in parseModifier()
24395 p->iJD = iGuess; in parseModifier()
24396 p->validJD = 1; in parseModifier()
24397 p->tzSet = 1; in parseModifier()
24410 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
24417 p->validTZ = 0; in parseModifier()
24418 p->validJD = 0; in parseModifier()
24420 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
24421 if( Z>n ) Z -= 7; in parseModifier()
24422 p->iJD += (n - Z)*86400000; in parseModifier()
24445 p->useSubsec = 1; in parseModifier()
24450 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
24453 p->validHMS = 1; in parseModifier()
24454 p->h = p->m = 0; in parseModifier()
24455 p->s = 0.0; in parseModifier()
24456 p->rawS = 0; in parseModifier()
24457 p->validTZ = 0; in parseModifier()
24458 p->validJD = 0; in parseModifier()
24460 p->D = 1; in parseModifier()
24463 p->M = 1; in parseModifier()
24464 p->D = 1; in parseModifier()
24472 case '-': in parseModifier()
24491 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
24503 tx.iJD -= 43200000; in parseModifier()
24505 tx.iJD -= day*86400000; in parseModifier()
24506 if( z[0]=='-' ) tx.iJD = -tx.iJD; in parseModifier()
24509 p->iJD += tx.iJD; in parseModifier()
24520 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
24523 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
24527 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
24534 p->M += (int)r; in parseModifier()
24535 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
24536 p->Y += x; in parseModifier()
24537 p->M -= x*12; in parseModifier()
24538 p->validJD = 0; in parseModifier()
24539 r -= (int)r; in parseModifier()
24546 p->Y += y; in parseModifier()
24547 p->validJD = 0; in parseModifier()
24548 r -= (int)r; in parseModifier()
24553 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); in parseModifier()
24569 ** Process time function arguments. argv[0] is a date-time stamp.
24606 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
24637 ** the unix epoch of 1970-01-01 00:00:00 GMT.
24648 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); in unixepochFunc()
24650 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); in unixepochFunc()
24658 ** Return YYYY-MM-DD HH:MM:SS
24671 if( Y<0 ) Y = -Y; in datetimeFunc()
24676 zBuf[5] = '-'; in datetimeFunc()
24679 zBuf[8] = '-'; in datetimeFunc()
24707 zBuf[0] = '-'; in datetimeFunc()
24710 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); in datetimeFunc()
24760 ** Return YYYY-MM-DD
24773 if( Y<0 ) Y = -Y; in dateFunc()
24778 zBuf[5] = '-'; in dateFunc()
24781 zBuf[8] = '-'; in dateFunc()
24786 zBuf[0] = '-'; in dateFunc()
24801 ** %H hour 00-24
24802 ** %j day of year 000-366
24804 ** %m month 01-12
24805 ** %M minute 00-59
24806 ** %s seconds since 1970-01-01
24807 ** %S seconds 00-59
24808 ** %w day of week 0-6 sunday==0
24809 ** %W week of year 00-53
24810 ** %Y year 0000-9999
24827 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
24829 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); in strftimeFunc()
24835 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
24861 nDay = (int)((x.iJD-y.iJD+43200000)/86400000); in strftimeFunc()
24865 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7); in strftimeFunc()
24886 (x.iJD - 21086676*(i64)10000000)/1000.0); in strftimeFunc()
24888 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
24916 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
24965 ** If the library is compiled to omit the full-scale date and time
24971 ** This function uses the C-library functions time(), gmtime()
24973 ** as the user-data for the function.
24992 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
25003 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
25016 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
25017 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), in sqlite3RegisterDateTimeFunctions()
25018 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
25019 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
25020 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
25021 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
25027 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
25028 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
25060 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
25078 ** from within OsOpen()), but some third-party implementations may.
25118 if( pId->pMethods ){ in sqlite3OsClose()
25119 pId->pMethods->xClose(pId); in sqlite3OsClose()
25120 pId->pMethods = 0; in sqlite3OsClose()
25125 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
25129 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
25132 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
25136 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
25140 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
25145 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
25149 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
25153 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
25165 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
25175 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM in sqlite3OsFileControl()
25183 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
25190 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
25193 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
25197 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
25201 if( NEVER(id->pMethods==0) ) return 0; in sqlite3OsDeviceCharacteristics()
25202 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
25206 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
25209 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
25212 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
25222 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
25230 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
25233 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
25236 /* No-op stubs to use when memory-mapped I/O is disabled */
25264 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
25265 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
25271 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; in sqlite3OsDelete()
25280 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
25290 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
25295 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ in sqlite3OsDlOpen()
25296 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
25299 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
25302 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
25305 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
25315 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
25320 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
25323 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
25327 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
25333 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
25334 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
25337 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
25352 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
25410 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
25412 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
25424 /* No-op */ in vfsUnlink()
25426 vfsList = pVfs->pNext; in vfsUnlink()
25429 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
25430 p = p->pNext; in vfsUnlink()
25432 if( p->pNext==pVfs ){ in vfsUnlink()
25433 p->pNext = pVfs->pNext; in vfsUnlink()
25457 pVfs->pNext = vfsList; in sqlite3_vfs_register()
25460 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
25461 vfsList->pNext = pVfs; in sqlite3_vfs_register()
25503 ** Most malloc failures are non-benign. After they occur, SQLite
25527 ** we have to locate the state vector at run-time. In the more common
25557 ** indicates that subsequent malloc failures are non-benign.
25588 ** This file contains a no-op memory allocation drivers for use when
25598 ** used when no other memory allocator is specified using compile-time
25604 ** No-op versions of all memory allocation routines
25617 ** Populate the low-level memory allocation function pointers in
25650 ** This file contains low-level memory allocation drivers for when
25651 ** SQLite will use the standard C-library malloc/realloc/free interface
25654 ** This file contains implementations of the low-level memory allocation
25662 ** C-preprocessor macro summary:
25669 ** a different name, using a separate -D
25685 ** used when no other memory allocator is specified using compile-time
25705 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
25710 ** Use standard C library malloc and free on non-Apple systems.
25727 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
25761 ** For this low-level routine, we are guaranteed that nByte>0 because
25795 ** For this low-level routine, we already know that pPrior!=0 since
25797 ** by higher-level routines.
25805 p--; in sqlite3MemFree()
25822 p--; in sqlite3MemSize()
25831 ** For this low-level interface, we know that pPrior!=0. Cases where
25832 ** pPrior==0 while have been intercepted by higher-level routine and
25834 ** cases where nByte<=0 will have been intercepted by higher-level
25850 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
25851 p--; in sqlite3MemRealloc()
25911 ** Populate the low-level memory allocation function pointers in
25944 ** This file contains low-level memory allocation drivers for when
25945 ** SQLite will use the standard C-library malloc/realloc/free interface
25950 ** This file contains implementations of the low-level memory allocation
25976 ** ------------------------------------------------------------------------
25978 ** ------------------------------------------------------------------------
26062 if( i>NCSIZE-1 ){ in adjustStats()
26063 i = NCSIZE - 1; in adjustStats()
26072 mem.nCurrent[i]--; in adjustStats()
26090 p--; in sqlite3MemsysGetHeader()
26091 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
26092 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
26100 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
26113 return (int)pHdr->iSize; in sqlite3MemSize()
26146 ** Fill a buffer with pseudo-random bytes. This is used to preset
26155 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
26160 nByte -= 4; in randomFill()
26162 while( nByte-- > 0 ){ in randomFill()
26163 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
26191 pHdr->pNext = 0; in sqlite3MemMalloc()
26192 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
26194 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
26199 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
26200 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
26201 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
26202 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
26205 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
26206 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
26209 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
26212 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
26217 pHdr->iSize = nByte; in sqlite3MemMalloc()
26222 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
26240 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
26242 if( pHdr->pPrev ){ in sqlite3MemFree()
26243 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
26244 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
26247 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
26249 if( pHdr->pNext ){ in sqlite3MemFree()
26250 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
26251 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
26254 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
26257 z -= pHdr->nTitle; in sqlite3MemFree()
26258 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
26259 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
26260 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
26278 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
26282 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
26283 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
26284 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
26292 ** Populate the low-level memory allocation function pointers in
26316 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
26317 pHdr->eType = eType; in sqlite3MemdebugSetType()
26335 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
26336 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
26357 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
26358 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
26387 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
26396 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
26398 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
26399 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
26418 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
26420 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
26422 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
26423 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
26426 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
26427 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
26432 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
26438 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
26440 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
26441 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
26493 ** mean that the library will use a memory-pool by default, just that
26534 ** two fields form a double-linked list of chunks of related sizes.
26571 ** True if we are evaluating an out-of-memory callback.
26599 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26632 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
26634 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
26635 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
26638 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
26667 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
26668 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
26669 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
26672 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
26711 ** size parameters for check-out and return a pointer to the
26718 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
26719 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
26720 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
26721 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
26722 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
26723 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
26735 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
26745 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
26747 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
26748 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
26749 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
26750 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
26751 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
26752 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
26753 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
26783 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
26787 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
26788 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
26793 size = i + size/4 - prev; in memsys3Merge()
26794 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
26795 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
26796 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
26836 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
26838 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
26844 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
26877 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
26904 i = p - mem3.aPool; in memsys3FreeUnsafe()
26905 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
26906 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
26908 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
26909 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
26910 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
26915 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
26916 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
26917 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
26920 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
26921 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
26922 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
26924 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
26925 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
26927 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
26928 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
26929 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
26936 ** size returned omits the 8-byte header overhead. This only
26943 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
26944 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
26954 return ((n+11)&~7) - 4; in memsys3Roundup()
26994 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
27023 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
27069 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
27075 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
27080 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
27086 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
27088 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
27092 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
27097 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
27106 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
27111 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
27112 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
27128 ** Populate the low-level memory allocation function pointers in
27189 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
27198 ** N >= M*(1 + log2(n)/2) - n + 1
27227 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
27262 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
27293 ** structures, return a pointer to the idx-th such link.
27307 next = MEM5LINK(i)->next; in memsys5Unlink()
27308 prev = MEM5LINK(i)->prev; in memsys5Unlink()
27312 MEM5LINK(prev)->next = next; in memsys5Unlink()
27315 MEM5LINK(next)->prev = prev; in memsys5Unlink()
27330 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
27331 MEM5LINK(i)->prev = -1; in memsys5Link()
27334 MEM5LINK(x)->prev = i; in memsys5Link()
27356 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
27411 iBin--; in memsys5MallocUnsafe()
27422 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
27449 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
27451 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
27453 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
27458 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
27461 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
27466 mem5.currentCount--; in memsys5FreeUnsafe()
27467 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
27476 iBuddy = iBlock - size; in memsys5FreeUnsafe()
27538 ** memsys5Round(). Hence nBytes is always a non-negative power
27547 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
27570 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
27592 ** Examples: memsys5Log(1) -> 0
27593 ** memsys5Log(2) -> 1
27594 ** memsys5Log(4) -> 2
27595 ** memsys5Log(5) -> 3
27596 ** memsys5Log(8) -> 3
27597 ** memsys5Log(9) -> 4
27601 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
27626 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
27644 mem5.aiFreelist[ii] = -1; in memsys5Init()
27648 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
27698 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
27789 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
27800 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
27810 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
27813 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
27844 p->iType = iType; in checkMutexAlloc()
27847 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
27852 p = &staticMutexes[iType-2]; in checkMutexAlloc()
27855 if( p->mutex==0 ){ in checkMutexAlloc()
27856 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
27857 if( p->mutex==0 ){ in checkMutexAlloc()
27877 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
27881 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
27896 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
27897 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
27901 "illegal multi-threaded access to database connection" in checkMutexEnter()
27904 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
27912 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
27920 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
27950 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
27951 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
27979 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
27980 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
27981 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
27982 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
27983 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
27984 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
27985 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
27986 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
27988 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
28077 ** this function is a no-op.
28121 ** here are place-holders. Applications can substitute working
28122 ** mutex routines at start-time using the
28195 return p==0 || p->cnt>0; in debugMutexHeld()
28199 return p==0 || p->cnt==0; in debugMutexNotheld()
28214 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
28221 pNew->id = id; in debugMutexAlloc()
28222 pNew->cnt = 0; in debugMutexAlloc()
28228 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
28233 pNew = &aStatic[id-2]; in debugMutexAlloc()
28234 pNew->id = id; in debugMutexAlloc()
28246 assert( p->cnt==0 ); in debugMutexFree()
28247 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
28269 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
28270 p->cnt++; in debugMutexEnter()
28274 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
28275 p->cnt++; in debugMutexTry()
28288 p->cnt--; in debugMutexLeave()
28289 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
28311 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
28312 ** is used regardless of the run-time threadsafety setting.
28352 ** home-grown mutexes. Encapsulate these conditions into a single #define.
28395 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
28401 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
28404 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
28457 ** cases where it really needs one. If a faster non-recursive mutex
28498 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
28504 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
28508 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
28516 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
28518 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
28525 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
28530 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
28535 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
28547 assert( p->nRef==0 ); in pthreadMutexFree()
28549 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
28552 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
28574 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
28579 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
28580 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
28582 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
28589 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
28590 p->nRef++; in pthreadMutexEnter()
28592 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
28593 assert( p->nRef==0 ); in pthreadMutexEnter()
28594 p->owner = self; in pthreadMutexEnter()
28595 p->nRef = 1; in pthreadMutexEnter()
28599 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
28601 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
28603 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
28604 p->owner = pthread_self(); in pthreadMutexEnter()
28605 p->nRef++; in pthreadMutexEnter()
28610 if( p->trace ){ in pthreadMutexEnter()
28611 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
28617 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
28622 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
28623 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
28625 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
28632 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
28633 p->nRef++; in pthreadMutexTry()
28635 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
28636 assert( p->nRef==0 ); in pthreadMutexTry()
28637 p->owner = self; in pthreadMutexTry()
28638 p->nRef = 1; in pthreadMutexTry()
28645 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
28647 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
28649 p->owner = pthread_self(); in pthreadMutexTry()
28650 p->nRef++; in pthreadMutexTry()
28659 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
28660 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
28675 p->nRef--; in pthreadMutexLeave()
28676 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
28678 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
28681 if( p->nRef==0 ){ in pthreadMutexLeave()
28682 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
28685 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
28689 if( p->trace ){ in pthreadMutexLeave()
28690 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
28795 ** Determine if we are dealing with Windows CE - which has a much reduced
28823 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
28878 return p->nRef!=0 && p->owner==GetCurrentThreadId(); in winMutexHeld()
28882 return p->nRef==0 || p->owner!=tid; in winMutexNotheld2()
28927 static int winMutex_isNt = -1; /* <0 means "need to query" */
29005 ** cases where it really needs one. If a faster non-recursive mutex
29031 p->id = iType; in winMutexAlloc()
29034 p->trace = 1; in winMutexAlloc()
29038 InitializeCriticalSectionEx(&p->mutex, 0, 0); in winMutexAlloc()
29040 InitializeCriticalSection(&p->mutex); in winMutexAlloc()
29047 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ in winMutexAlloc()
29052 p = &winMutex_staticMutexes[iType-2]; in winMutexAlloc()
29055 InterlockedCompareExchange(&p->trace, 1, 0); in winMutexAlloc()
29061 assert( p==0 || p->id==iType ); in winMutexAlloc()
29073 assert( p->nRef==0 && p->owner==0 ); in winMutexFree()
29074 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ in winMutexFree()
29075 DeleteCriticalSection(&p->mutex); in winMutexFree()
29101 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexEnter()
29106 EnterCriticalSection(&p->mutex); in winMutexEnter()
29108 assert( p->nRef>0 || p->owner==0 ); in winMutexEnter()
29109 p->owner = tid; in winMutexEnter()
29110 p->nRef++; in winMutexEnter()
29111 if( p->trace ){ in winMutexEnter()
29112 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexEnter()
29113 tid, p->id, p, p->trace, p->nRef)); in winMutexEnter()
29124 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexTry()
29138 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); in winMutexTry()
29143 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ in winMutexTry()
29145 p->owner = tid; in winMutexTry()
29146 p->nRef++; in winMutexTry()
29154 if( p->trace ){ in winMutexTry()
29155 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", in winMutexTry()
29156 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); in winMutexTry()
29174 assert( p->nRef>0 ); in winMutexLeave()
29175 assert( p->owner==tid ); in winMutexLeave()
29176 p->nRef--; in winMutexLeave()
29177 if( p->nRef==0 ) p->owner = 0; in winMutexLeave()
29178 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in winMutexLeave()
29181 LeaveCriticalSection(&p->mutex); in winMutexLeave()
29183 if( p->trace ){ in winMutexLeave()
29184 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexLeave()
29185 tid, p->id, p, p->trace, p->nRef)); in winMutexLeave()
29232 ** Attempt to release up to n bytes of non-essential memory currently
29233 ** held by SQLite. An example of non-essential memory is memory used to
29240 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine in sqlite3_release_memory()
29241 ** is a no-op returning zero if SQLite is not compiled with in sqlite3_release_memory()
29283 ** no-op.
29298 ** Set the soft heap-size limit for the library. An argument of
29299 ** zero disables the limit. A negative argument is a no-op used to
29314 if( rc ) return -1; in sqlite3_soft_heap_limit64()
29329 excess = sqlite3_memory_used() - n; in sqlite3_soft_heap_limit64()
29339 ** Set the hard heap-size limit for the library. An argument of zero
29340 ** disables the hard heap limit. A negative argument is a no-op used
29354 if( rc ) return -1; in sqlite3_hard_heap_limit64()
29389 ** Return true if the heap is currently under memory pressure - in other
29447 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal in mallocWithAlarm()
29457 if( nUsed >= mem0.alarmThreshold - nFull ){ in mallocWithAlarm()
29462 if( nUsed >= mem0.hardLimit - nFull ){ in mallocWithAlarm()
29493 ** This provides a 256-byte safety margin for defense against 32-bit
29522 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ in sqlite3Malloc()
29549 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); in isLookaside()
29565 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; in lookasideMallocSize()
29567 return db->lookaside.szTrue; in lookasideMallocSize()
29582 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ in sqlite3DbMallocSize()
29584 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbMallocSize()
29585 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
29589 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbMallocSize()
29590 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
29591 return db->lookaside.szTrue; in sqlite3DbMallocSize()
29607 if( p==0 ) return; /* IMP: R-49053-54554 */ in sqlite3_free()
29623 ** *db->pnBytesFreed.
29626 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); in measureAllocationSize()
29631 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29632 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29635 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFreeNN()
29638 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbFreeNN()
29640 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbFreeNN()
29642 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
29646 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbFreeNN()
29647 db->lookaside.pSmallFree = pBuf; in sqlite3DbFreeNN()
29651 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbFreeNN()
29653 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
29655 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbFreeNN()
29657 pBuf->pNext = db->lookaside.pFree; in sqlite3DbFreeNN()
29658 db->lookaside.pFree = pBuf; in sqlite3DbFreeNN()
29662 if( db->pnBytesFreed ){ in sqlite3DbFreeNN()
29675 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbNNFreeNN()
29677 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbNNFreeNN()
29679 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbNNFreeNN()
29681 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
29685 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbNNFreeNN()
29686 db->lookaside.pSmallFree = pBuf; in sqlite3DbNNFreeNN()
29690 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbNNFreeNN()
29692 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
29694 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbNNFreeNN()
29696 pBuf->pNext = db->lookaside.pFree; in sqlite3DbNNFreeNN()
29697 db->lookaside.pFree = pBuf; in sqlite3DbNNFreeNN()
29701 if( db->pnBytesFreed ){ in sqlite3DbNNFreeNN()
29711 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFree()
29724 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ in sqlite3Realloc()
29727 sqlite3_free(pOld); /* IMP: R-26507-47431 */ in sqlite3Realloc()
29735 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second in sqlite3Realloc()
29745 nDiff = nNew - nOld; in sqlite3Realloc()
29747 mem0.alarmThreshold-nDiff ){ in sqlite3Realloc()
29749 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ in sqlite3Realloc()
29763 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); in sqlite3Realloc()
29769 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ in sqlite3Realloc()
29781 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ in sqlite3_realloc()
29825 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); in dbMallocRawFinish()
29834 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
29862 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
29863 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
29864 if( n>db->lookaside.sz ){ in sqlite3DbMallocRawNN()
29865 if( !db->lookaside.bDisable ){ in sqlite3DbMallocRawNN()
29866 db->lookaside.anStat[1]++; in sqlite3DbMallocRawNN()
29867 }else if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
29874 if( (pBuf = db->lookaside.pSmallFree)!=0 ){ in sqlite3DbMallocRawNN()
29875 db->lookaside.pSmallFree = pBuf->pNext; in sqlite3DbMallocRawNN()
29876 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
29878 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ in sqlite3DbMallocRawNN()
29879 db->lookaside.pSmallInit = pBuf->pNext; in sqlite3DbMallocRawNN()
29880 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
29885 if( (pBuf = db->lookaside.pFree)!=0 ){ in sqlite3DbMallocRawNN()
29886 db->lookaside.pFree = pBuf->pNext; in sqlite3DbMallocRawNN()
29887 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
29889 }else if( (pBuf = db->lookaside.pInit)!=0 ){ in sqlite3DbMallocRawNN()
29890 db->lookaside.pInit = pBuf->pNext; in sqlite3DbMallocRawNN()
29891 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
29894 db->lookaside.anStat[2]++; in sqlite3DbMallocRawNN()
29898 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
29899 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
29900 if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
29917 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbRealloc()
29918 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ in sqlite3DbRealloc()
29920 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ in sqlite3DbRealloc()
29924 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ in sqlite3DbRealloc()
29925 if( n<=db->lookaside.szTrue ) return p; in sqlite3DbRealloc()
29934 if( db->mallocFailed==0 ){ in dbReallocFinish()
29950 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); in dbReallocFinish()
30011 ** at least one non-space character */ in sqlite3DbSpanDup()
30015 n = (int)(zEnd - zStart); in sqlite3DbSpanDup()
30016 while( sqlite3Isspace(zStart[n-1]) ) n--; in sqlite3DbSpanDup()
30030 ** Call this routine to record the fact that an OOM (out-of-memory) error
30031 ** has happened. This routine will set db->mallocFailed, and also
30043 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ in sqlite3OomFault()
30044 db->mallocFailed = 1; in sqlite3OomFault()
30045 if( db->nVdbeExec>0 ){ in sqlite3OomFault()
30046 AtomicStore(&db->u1.isInterrupted, 1); in sqlite3OomFault()
30049 if( db->pParse ){ in sqlite3OomFault()
30051 sqlite3ErrorMsg(db->pParse, "out of memory"); in sqlite3OomFault()
30052 db->pParse->rc = SQLITE_NOMEM_BKPT; in sqlite3OomFault()
30053 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ in sqlite3OomFault()
30054 pParse->nErr++; in sqlite3OomFault()
30055 pParse->rc = SQLITE_NOMEM; in sqlite3OomFault()
30064 ** db->mallocFailed flag as necessary.
30070 if( db->mallocFailed && db->nVdbeExec==0 ){ in sqlite3OomClear()
30071 db->mallocFailed = 0; in sqlite3OomClear()
30072 AtomicStore(&db->u1.isInterrupted, 0); in sqlite3OomClear()
30073 assert( db->lookaside.bDisable>0 ); in sqlite3OomClear()
30082 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ in apiHandleError()
30087 return rc & db->errMask; in apiHandleError()
30099 ** If an OOM as occurred, then the connection error-code (the value
30104 ** Otherwise the read (and possible write) of db->mallocFailed in sqlite3ApiExit()
30108 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3ApiExit()
30109 if( db->mallocFailed || rc ){ in sqlite3ApiExit()
30112 return rc & db->errMask; in sqlite3ApiExit()
30123 ** This file contains code for a set of "printf"-like routines. These
30134 #define etRADIX 0 /* non-decimal integer types. %x %o */
30150 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
30151 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
30158 ** An "etByte" is an 8-bit unsigned value.
30187 static const char aPrefix[] = "-x0\000X0";
30226 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
30227 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
30245 ** 16 (the number of significant digits in a 64-bit float) '0' is
30252 (*cnt)--; in et_getdigit()
30256 *val = (*val - d)*10.0; in et_getdigit()
30269 *val -= x*(*msd); in et_getdigit_int()
30280 p->accError = eError; in sqlite3StrAccumSetError()
30281 if( p->mxAlloc ) sqlite3_str_reset(p); in sqlite3StrAccumSetError()
30282 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); in sqlite3StrAccumSetError()
30289 if( p->nArg<=p->nUsed ) return 0; in getIntArg()
30290 return sqlite3_value_int64(p->apArg[p->nUsed++]); in getIntArg()
30293 if( p->nArg<=p->nUsed ) return 0.0; in getDoubleArg()
30294 return sqlite3_value_double(p->apArg[p->nUsed++]); in getDoubleArg()
30297 if( p->nArg<=p->nUsed ) return 0; in getTextArg()
30298 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); in getTextArg()
30312 if( pAccum->accError ) return 0; in printfTempBuf()
30313 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ in printfTempBuf()
30317 z = sqlite3DbMallocRaw(pAccum->db, n); in printfTempBuf()
30334 ** Hard limit on the precision of floating-point conversions.
30354 etByte flag_leftjustify; /* True if "-" flag is present */ in sqlite3_str_vappendf()
30364 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ in sqlite3_str_vappendf()
30367 sqlite_uint64 msd; /* Divisor to get most-significant-digit in sqlite3_str_vappendf()
30386 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
30389 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ in sqlite3_str_vappendf()
30403 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); in sqlite3_str_vappendf()
30416 precision = -1; in sqlite3_str_vappendf()
30419 case '-': flag_leftjustify = 1; break; in sqlite3_str_vappendf()
30439 unsigned wx = c - '0'; in sqlite3_str_vappendf()
30441 wx = wx*10 + c - '0'; in sqlite3_str_vappendf()
30453 fmt--; in sqlite3_str_vappendf()
30465 width = width >= -2147483647 ? -width : 0; in sqlite3_str_vappendf()
30487 precision = precision >= -2147483647 ? -precision : -1; in sqlite3_str_vappendf()
30493 px = px*10 + c - '0'; in sqlite3_str_vappendf()
30505 --fmt; in sqlite3_str_vappendf()
30520 xtype = infop->type; in sqlite3_str_vappendf()
30531 ** flag_leftjustify TRUE if a '-' is present or if the in sqlite3_str_vappendf()
30536 ** always non-negative. Zero is the default. in sqlite3_str_vappendf()
30538 ** is -1. in sqlite3_str_vappendf()
30543 assert( precision>=(-1) ); in sqlite3_str_vappendf()
30554 if( infop->flags & FLAG_SIGNED ){ in sqlite3_str_vappendf()
30569 testcase( v==(-1) ); in sqlite3_str_vappendf()
30572 prefix = '-'; in sqlite3_str_vappendf()
30592 if( flag_zeropad && precision<width-(prefix!=0) ){ in sqlite3_str_vappendf()
30593 precision = width-(prefix!=0); in sqlite3_str_vappendf()
30595 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ in sqlite3_str_vappendf()
30606 bufpt = &zOut[nOut-1]; in sqlite3_str_vappendf()
30613 *(--bufpt) = zOrd[x*2+1]; in sqlite3_str_vappendf()
30614 *(--bufpt) = zOrd[x*2]; in sqlite3_str_vappendf()
30617 const char *cset = &aDigits[infop->charset]; in sqlite3_str_vappendf()
30618 u8 base = infop->base; in sqlite3_str_vappendf()
30620 *(--bufpt) = cset[longvalue%base]; in sqlite3_str_vappendf()
30624 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
30626 *(--bufpt) = '0'; /* Zero pad */ in sqlite3_str_vappendf()
30630 int nn = (length - 1)/3; /* Number of "," to insert */ in sqlite3_str_vappendf()
30631 int ix = (length - 1)%3 + 1; in sqlite3_str_vappendf()
30632 bufpt -= nn; in sqlite3_str_vappendf()
30635 ix--; in sqlite3_str_vappendf()
30638 nn--; in sqlite3_str_vappendf()
30643 if( prefix ) *(--bufpt) = prefix; /* Add sign */ in sqlite3_str_vappendf()
30644 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ in sqlite3_str_vappendf()
30647 pre = &aPrefix[infop->prefix]; in sqlite3_str_vappendf()
30648 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; in sqlite3_str_vappendf()
30650 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
30670 realvalue = -realvalue; in sqlite3_str_vappendf()
30671 prefix = '-'; in sqlite3_str_vappendf()
30676 if( xtype==etGENERIC && precision>0 ) precision--; in sqlite3_str_vappendf()
30686 while( kk-- > 0 ){ rnd /= 10; } in sqlite3_str_vappendf()
30694 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; } in sqlite3_str_vappendf()
30700 ex = -1023 + (int)((u>>52)&0x7ff); in sqlite3_str_vappendf()
30701 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16; in sqlite3_str_vappendf()
30722 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } in sqlite3_str_vappendf()
30723 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } in sqlite3_str_vappendf()
30749 if( exp<-4 || exp>precision ){ in sqlite3_str_vappendf()
30752 precision = precision - exp; in sqlite3_str_vappendf()
30784 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
30789 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
30800 for(e2++; e2<0; precision--, e2++){ in sqlite3_str_vappendf()
30806 while( (precision--)>0 ){ in sqlite3_str_vappendf()
30810 while( (precision--)>0 ){ in sqlite3_str_vappendf()
30816 while( bufpt[-1]=='0' ) *(--bufpt) = 0; in sqlite3_str_vappendf()
30818 if( bufpt[-1]=='.' ){ in sqlite3_str_vappendf()
30822 *(--bufpt) = 0; in sqlite3_str_vappendf()
30828 *(bufpt++) = aDigits[infop->charset]; in sqlite3_str_vappendf()
30830 *(bufpt++) = '-'; exp = -exp; in sqlite3_str_vappendf()
30846 length = (int)(bufpt-zOut); in sqlite3_str_vappendf()
30853 int nPad = width - length; in sqlite3_str_vappendf()
30854 for(i=width; i>=nPad; i--){ in sqlite3_str_vappendf()
30855 bufpt[i] = bufpt[i-nPad]; in sqlite3_str_vappendf()
30858 while( nPad-- ) bufpt[i++] = '0'; in sqlite3_str_vappendf()
30865 *(va_arg(ap,int*)) = pAccum->nChar; in sqlite3_str_vappendf()
30912 width -= precision-1; in sqlite3_str_vappendf()
30914 sqlite3_str_appendchar(pAccum, width-1, ' '); in sqlite3_str_vappendf()
30918 precision--; in sqlite3_str_vappendf()
30921 if( nPrior > precision-1 ) nPrior = precision - 1; in sqlite3_str_vappendf()
30923 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ in sqlite3_str_vappendf()
30926 if( pAccum->accError ) break; in sqlite3_str_vappendf()
30928 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); in sqlite3_str_vappendf()
30929 precision -= nPrior; in sqlite3_str_vappendf()
30947 if( pAccum->nChar==0 in sqlite3_str_vappendf()
30948 && pAccum->mxAlloc in sqlite3_str_vappendf()
30951 && pAccum->accError==0 in sqlite3_str_vappendf()
30956 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
30957 pAccum->zText = bufpt; in sqlite3_str_vappendf()
30958 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); in sqlite3_str_vappendf()
30959 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
30960 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3_str_vappendf()
30971 while( precision-- > 0 && z[0] ){ in sqlite3_str_vappendf()
30974 length = (int)(z - (unsigned char*)bufpt); in sqlite3_str_vappendf()
30983 /* Adjust width to account for extra bytes in UTF-8 characters */ in sqlite3_str_vappendf()
30984 int ii = length - 1; in sqlite3_str_vappendf()
30985 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; in sqlite3_str_vappendf()
31010 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ in sqlite3_str_vappendf()
31037 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31042 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); in sqlite3_str_vappendf()
31043 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); in sqlite3_str_vappendf()
31049 if( pToken && pToken->n ){ in sqlite3_str_vappendf()
31050 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); in sqlite3_str_vappendf()
31051 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); in sqlite3_str_vappendf()
31059 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
31062 if( pItem->zAlias && !flag_altform2 ){ in sqlite3_str_vappendf()
31063 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31064 }else if( pItem->zName ){ in sqlite3_str_vappendf()
31065 if( pItem->zDatabase ){ in sqlite3_str_vappendf()
31066 sqlite3_str_appendall(pAccum, pItem->zDatabase); in sqlite3_str_vappendf()
31069 sqlite3_str_appendall(pAccum, pItem->zName); in sqlite3_str_vappendf()
31070 }else if( pItem->zAlias ){ in sqlite3_str_vappendf()
31071 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
31073 Select *pSel = pItem->pSelect; in sqlite3_str_vappendf()
31075 if( pSel->selFlags & SF_NestedFrom ){ in sqlite3_str_vappendf()
31076 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); in sqlite3_str_vappendf()
31078 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); in sqlite3_str_vappendf()
31097 width -= length; in sqlite3_str_vappendf()
31107 sqlite3DbFree(pAccum->db, zExtra); in sqlite3_str_vappendf()
31126 if( db->errByteOffset!=(-2) ) return; in sqlite3RecordErrorByteOffset()
31127 pParse = db->pParse; in sqlite3RecordErrorByteOffset()
31129 zText =pParse->zTail; in sqlite3RecordErrorByteOffset()
31133 db->errByteOffset = (int)(z-zText); in sqlite3RecordErrorByteOffset()
31143 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) in sqlite3RecordErrorOffsetOfExpr()
31145 pExpr = pExpr->pLeft; in sqlite3RecordErrorOffsetOfExpr()
31148 db->errByteOffset = pExpr->w.iOfst; in sqlite3RecordErrorOffsetOfExpr()
31160 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ in sqlite3StrAccumEnlarge()
31161 if( p->accError ){ in sqlite3StrAccumEnlarge()
31162 testcase(p->accError==SQLITE_TOOBIG); in sqlite3StrAccumEnlarge()
31163 testcase(p->accError==SQLITE_NOMEM); in sqlite3StrAccumEnlarge()
31166 if( p->mxAlloc==0 ){ in sqlite3StrAccumEnlarge()
31168 return p->nAlloc - p->nChar - 1; in sqlite3StrAccumEnlarge()
31170 char *zOld = isMalloced(p) ? p->zText : 0; in sqlite3StrAccumEnlarge()
31171 i64 szNew = p->nChar + N + 1; in sqlite3StrAccumEnlarge()
31172 if( szNew+p->nChar<=p->mxAlloc ){ in sqlite3StrAccumEnlarge()
31175 szNew += p->nChar; in sqlite3StrAccumEnlarge()
31177 if( szNew > p->mxAlloc ){ in sqlite3StrAccumEnlarge()
31182 p->nAlloc = (int)szNew; in sqlite3StrAccumEnlarge()
31184 if( p->db ){ in sqlite3StrAccumEnlarge()
31185 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
31187 zNew = sqlite3Realloc(zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
31190 assert( p->zText!=0 || p->nChar==0 ); in sqlite3StrAccumEnlarge()
31191 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); in sqlite3StrAccumEnlarge()
31192 p->zText = zNew; in sqlite3StrAccumEnlarge()
31193 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); in sqlite3StrAccumEnlarge()
31194 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3StrAccumEnlarge()
31209 testcase( p->nChar + (i64)N > 0x7fffffff ); in sqlite3_str_appendchar()
31210 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ in sqlite3_str_appendchar()
31213 while( (N--)>0 ) p->zText[p->nChar++] = c; in sqlite3_str_appendchar()
31220 ** This is a helper routine to sqlite3_str_append() that does special-case
31227 memcpy(&p->zText[p->nChar], z, N); in enlargeAndAppend()
31228 p->nChar += N; in enlargeAndAppend()
31238 assert( p->zText!=0 || p->nChar==0 || p->accError ); in sqlite3_str_append()
31240 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); in sqlite3_str_append()
31241 if( p->nChar+N >= p->nAlloc ){ in sqlite3_str_append()
31244 assert( p->zText ); in sqlite3_str_append()
31245 p->nChar += N; in sqlite3_str_append()
31246 memcpy(&p->zText[p->nChar-N], z, N); in sqlite3_str_append()
31251 ** Append the complete text of zero-terminated string z[] to the p string.
31259 ** Finish off a string by making sure it is zero-terminated.
31265 assert( p->mxAlloc>0 && !isMalloced(p) ); in strAccumFinishRealloc()
31266 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); in strAccumFinishRealloc()
31268 memcpy(zText, p->zText, p->nChar+1); in strAccumFinishRealloc()
31269 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in strAccumFinishRealloc()
31273 p->zText = zText; in strAccumFinishRealloc()
31277 if( p->zText ){ in sqlite3StrAccumFinish()
31278 p->zText[p->nChar] = 0; in sqlite3StrAccumFinish()
31279 if( p->mxAlloc>0 && !isMalloced(p) ){ in sqlite3StrAccumFinish()
31283 return p->zText; in sqlite3StrAccumFinish()
31291 if( p->accError ){ in sqlite3ResultStrAccum()
31292 sqlite3_result_error_code(pCtx, p->accError); in sqlite3ResultStrAccum()
31295 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); in sqlite3ResultStrAccum()
31327 return p ? p->accError : SQLITE_NOMEM; in sqlite3_str_errcode()
31332 return p ? p->nChar : 0; in sqlite3_str_length()
31337 if( p==0 || p->nChar==0 ) return 0; in sqlite3_str_value()
31338 p->zText[p->nChar] = 0; in sqlite3_str_value()
31339 return p->zText; in sqlite3_str_value()
31347 sqlite3DbFree(p->db, p->zText); in sqlite3_str_reset()
31348 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; in sqlite3_str_reset()
31350 p->nAlloc = 0; in sqlite3_str_reset()
31351 p->nChar = 0; in sqlite3_str_reset()
31352 p->zText = 0; in sqlite3_str_reset()
31360 ** memory is used if not NULL. db->mallocFailed is set appropriately
31370 p->zText = zBase; in sqlite3StrAccumInit()
31371 p->db = db; in sqlite3StrAccumInit()
31372 p->nAlloc = n; in sqlite3StrAccumInit()
31373 p->mxAlloc = mx; in sqlite3StrAccumInit()
31374 p->nChar = 0; in sqlite3StrAccumInit()
31375 p->accError = 0; in sqlite3StrAccumInit()
31376 p->printfFlags = 0; in sqlite3StrAccumInit()
31384 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); in sqlite3_str_new()
31393 ** %-conversion extensions.
31401 db->aLimit[SQLITE_LIMIT_LENGTH]); in sqlite3VMPrintf()
31413 ** %-conversion extensions.
31426 ** %-conversion extensions.
31450 ** %-conversion extensions.
31514 ** stack space on small-stack systems when logging is disabled.
31576 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31589 ** 2015-06-08
31621 p->iLevel++; in sqlite3TreeViewPush()
31624 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; in sqlite3TreeViewPush()
31633 p->iLevel--; in sqlite3TreeViewPop()
31634 if( p->iLevel<0 ){ in sqlite3TreeViewPop()
31651 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ in sqlite3TreeViewLine()
31652 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); in sqlite3TreeViewLine()
31654 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); in sqlite3TreeViewLine()
31690 int colMoreToFollow = i<(nCol - 1); in sqlite3TreeViewColumnList()
31705 printf(" X-%s", z); in sqlite3TreeViewColumnList()
31713 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); in sqlite3TreeViewColumnList()
31724 ** Generate a human-readable description of a WITH clause.
31729 if( pWith->nCte==0 ) return; in sqlite3TreeViewWith()
31730 if( pWith->pOuter ){ in sqlite3TreeViewWith()
31731 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); in sqlite3TreeViewWith()
31735 if( pWith->nCte>0 ){ in sqlite3TreeViewWith()
31737 for(i=0; i<pWith->nCte; i++){ in sqlite3TreeViewWith()
31740 const struct Cte *pCte = &pWith->a[i]; in sqlite3TreeViewWith()
31742 sqlite3_str_appendf(&x, "%s", pCte->zName); in sqlite3TreeViewWith()
31743 if( pCte->pCols && pCte->pCols->nExpr>0 ){ in sqlite3TreeViewWith()
31746 for(j=0; j<pCte->pCols->nExpr; j++){ in sqlite3TreeViewWith()
31747 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); in sqlite3TreeViewWith()
31752 if( pCte->eM10d!=M10d_Any ){ in sqlite3TreeViewWith()
31754 pCte->eM10d==M10d_No ? "NOT " : ""); in sqlite3TreeViewWith()
31756 if( pCte->pUse ){ in sqlite3TreeViewWith()
31757 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, in sqlite3TreeViewWith()
31758 pCte->pUse->nUse); in sqlite3TreeViewWith()
31761 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); in sqlite3TreeViewWith()
31762 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); in sqlite3TreeViewWith()
31770 ** Generate a human-readable description of a SrcList object.
31775 for(i=0; i<pSrc->nSrc; i++){ in sqlite3TreeViewSrcList()
31776 const SrcItem *pItem = &pSrc->a[i]; in sqlite3TreeViewSrcList()
31782 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); in sqlite3TreeViewSrcList()
31783 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
31785 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed); in sqlite3TreeViewSrcList()
31787 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ in sqlite3TreeViewSrcList()
31788 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); in sqlite3TreeViewSrcList()
31789 }else if( pItem->fg.jointype & JT_LEFT ){ in sqlite3TreeViewSrcList()
31790 sqlite3_str_appendf(&x, " LEFT-JOIN"); in sqlite3TreeViewSrcList()
31791 }else if( pItem->fg.jointype & JT_RIGHT ){ in sqlite3TreeViewSrcList()
31792 sqlite3_str_appendf(&x, " RIGHT-JOIN"); in sqlite3TreeViewSrcList()
31793 }else if( pItem->fg.jointype & JT_CROSS ){ in sqlite3TreeViewSrcList()
31794 sqlite3_str_appendf(&x, " CROSS-JOIN"); in sqlite3TreeViewSrcList()
31796 if( pItem->fg.jointype & JT_LTORJ ){ in sqlite3TreeViewSrcList()
31799 if( pItem->fg.fromDDL ){ in sqlite3TreeViewSrcList()
31802 if( pItem->fg.isCte ){ in sqlite3TreeViewSrcList()
31803 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse); in sqlite3TreeViewSrcList()
31805 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ in sqlite3TreeViewSrcList()
31808 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); in sqlite3TreeViewSrcList()
31809 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); in sqlite3TreeViewSrcList()
31810 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); in sqlite3TreeViewSrcList()
31811 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); in sqlite3TreeViewSrcList()
31812 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); in sqlite3TreeViewSrcList()
31813 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); in sqlite3TreeViewSrcList()
31816 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); in sqlite3TreeViewSrcList()
31818 if( pItem->pSelect ) n++; in sqlite3TreeViewSrcList()
31819 if( pItem->fg.isTabFunc ) n++; in sqlite3TreeViewSrcList()
31820 if( pItem->fg.isUsing ) n++; in sqlite3TreeViewSrcList()
31821 if( pItem->fg.isUsing ){ in sqlite3TreeViewSrcList()
31822 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); in sqlite3TreeViewSrcList()
31824 if( pItem->pSelect ){ in sqlite3TreeViewSrcList()
31825 if( pItem->pTab ){ in sqlite3TreeViewSrcList()
31826 Table *pTab = pItem->pTab; in sqlite3TreeViewSrcList()
31827 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); in sqlite3TreeViewSrcList()
31829 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) ); in sqlite3TreeViewSrcList()
31830 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0); in sqlite3TreeViewSrcList()
31832 if( pItem->fg.isTabFunc ){ in sqlite3TreeViewSrcList()
31833 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); in sqlite3TreeViewSrcList()
31840 ** Generate a human-readable description of a Select object.
31846 sqlite3TreeViewLine(pView, "nil-SELECT"); in sqlite3TreeViewSelect()
31850 if( p->pWith ){ in sqlite3TreeViewSelect()
31851 sqlite3TreeViewWith(pView, p->pWith, 1); in sqlite3TreeViewSelect()
31856 if( p->selFlags & SF_WhereBegin ){ in sqlite3TreeViewSelect()
31861 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), in sqlite3TreeViewSelect()
31862 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), in sqlite3TreeViewSelect()
31863 p->selId, p, p->selFlags, in sqlite3TreeViewSelect()
31864 (int)p->nSelectRow in sqlite3TreeViewSelect()
31868 if( p->pPrior ){ in sqlite3TreeViewSelect()
31872 if( p->pSrc && p->pSrc->nSrc ) n++; in sqlite3TreeViewSelect()
31873 if( p->pWhere ) n++; in sqlite3TreeViewSelect()
31874 if( p->pGroupBy ) n++; in sqlite3TreeViewSelect()
31875 if( p->pHaving ) n++; in sqlite3TreeViewSelect()
31876 if( p->pOrderBy ) n++; in sqlite3TreeViewSelect()
31877 if( p->pLimit ) n++; in sqlite3TreeViewSelect()
31879 if( p->pWin ) n++; in sqlite3TreeViewSelect()
31880 if( p->pWinDefn ) n++; in sqlite3TreeViewSelect()
31883 if( p->pEList ){ in sqlite3TreeViewSelect()
31884 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); in sqlite3TreeViewSelect()
31886 n--; in sqlite3TreeViewSelect()
31888 if( p->pWin ){ in sqlite3TreeViewSelect()
31890 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
31891 sqlite3TreeViewLine(pView, "window-functions"); in sqlite3TreeViewSelect()
31892 for(pX=p->pWin; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
31893 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
31898 if( p->pSrc && p->pSrc->nSrc ){ in sqlite3TreeViewSelect()
31899 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
31901 sqlite3TreeViewSrcList(pView, p->pSrc); in sqlite3TreeViewSelect()
31904 if( p->pWhere ){ in sqlite3TreeViewSelect()
31905 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewSelect()
31906 sqlite3TreeViewExpr(pView, p->pWhere, 0); in sqlite3TreeViewSelect()
31909 if( p->pGroupBy ){ in sqlite3TreeViewSelect()
31910 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); in sqlite3TreeViewSelect()
31912 if( p->pHaving ){ in sqlite3TreeViewSelect()
31913 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); in sqlite3TreeViewSelect()
31914 sqlite3TreeViewExpr(pView, p->pHaving, 0); in sqlite3TreeViewSelect()
31918 if( p->pWinDefn ){ in sqlite3TreeViewSelect()
31920 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); in sqlite3TreeViewSelect()
31921 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
31922 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
31927 if( p->pOrderBy ){ in sqlite3TreeViewSelect()
31928 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); in sqlite3TreeViewSelect()
31930 if( p->pLimit ){ in sqlite3TreeViewSelect()
31931 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); in sqlite3TreeViewSelect()
31932 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); in sqlite3TreeViewSelect()
31933 if( p->pLimit->pRight ){ in sqlite3TreeViewSelect()
31934 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); in sqlite3TreeViewSelect()
31935 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); in sqlite3TreeViewSelect()
31940 if( p->pPrior ){ in sqlite3TreeViewSelect()
31942 switch( p->op ){ in sqlite3TreeViewSelect()
31949 p = p->pPrior; in sqlite3TreeViewSelect()
31993 ** Generate a human-readable explanation for a Window object
31998 if( pWin->pFilter ){ in sqlite3TreeViewWindow()
32000 sqlite3TreeViewExpr(pView, pWin->pFilter, 0); in sqlite3TreeViewWindow()
32004 if( pWin->zName ){ in sqlite3TreeViewWindow()
32005 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); in sqlite3TreeViewWindow()
32009 if( pWin->zBase ) nElement++; in sqlite3TreeViewWindow()
32010 if( pWin->pOrderBy ) nElement++; in sqlite3TreeViewWindow()
32011 if( pWin->eFrmType ) nElement++; in sqlite3TreeViewWindow()
32012 if( pWin->eExclude ) nElement++; in sqlite3TreeViewWindow()
32013 if( pWin->zBase ){ in sqlite3TreeViewWindow()
32014 sqlite3TreeViewPush(&pView, (--nElement)>0); in sqlite3TreeViewWindow()
32015 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); in sqlite3TreeViewWindow()
32018 if( pWin->pPartition ){ in sqlite3TreeViewWindow()
32019 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); in sqlite3TreeViewWindow()
32021 if( pWin->pOrderBy ){ in sqlite3TreeViewWindow()
32022 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); in sqlite3TreeViewWindow()
32024 if( pWin->eFrmType ){ in sqlite3TreeViewWindow()
32027 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; in sqlite3TreeViewWindow()
32028 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; in sqlite3TreeViewWindow()
32030 pWin->bImplicitFrame ? " (implied)" : ""); in sqlite3TreeViewWindow()
32031 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); in sqlite3TreeViewWindow()
32032 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); in sqlite3TreeViewWindow()
32033 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); in sqlite3TreeViewWindow()
32036 if( pWin->eExclude ){ in sqlite3TreeViewWindow()
32039 switch( pWin->eExclude ){ in sqlite3TreeViewWindow()
32045 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); in sqlite3TreeViewWindow()
32059 ** Generate a human-readable explanation for a Window Function object
32065 pWin->pWFunc->zName, pWin->pWFunc->nArg); in sqlite3TreeViewWinFunc()
32072 ** Generate a human-readable explanation of an expression tree.
32084 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ in sqlite3TreeViewExpr()
32088 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); in sqlite3TreeViewExpr()
32090 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
32093 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
32101 if( pExpr->pAggInfo!=0 ){ in sqlite3TreeViewExpr()
32102 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); in sqlite3TreeViewExpr()
32108 switch( pExpr->op ){ in sqlite3TreeViewExpr()
32111 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
32115 if( pExpr->iTable<0 ){ in sqlite3TreeViewExpr()
32118 if( pExpr->op2 ){ in sqlite3TreeViewExpr()
32119 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
32124 pExpr->iColumn, zFlgs, zOp2); in sqlite3TreeViewExpr()
32128 pExpr->iTable, pExpr->iColumn, in sqlite3TreeViewExpr()
32129 pExpr->y.pTab, zFlgs); in sqlite3TreeViewExpr()
32132 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32137 if( pExpr->flags & EP_IntValue ){ in sqlite3TreeViewExpr()
32138 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); in sqlite3TreeViewExpr()
32140 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32147 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32153 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32168 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
32175 pExpr->u.zToken, pExpr->iColumn); in sqlite3TreeViewExpr()
32179 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); in sqlite3TreeViewExpr()
32184 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); in sqlite3TreeViewExpr()
32191 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32192 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32229 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" in sqlite3TreeViewExpr()
32231 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); in sqlite3TreeViewExpr()
32232 assert( pExpr->pRight ); in sqlite3TreeViewExpr()
32233 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE ); in sqlite3TreeViewExpr()
32234 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); in sqlite3TreeViewExpr()
32241 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
32242 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32249 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE in sqlite3TreeViewExpr()
32254 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", in sqlite3TreeViewExpr()
32255 pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
32256 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32269 pFarg = pExpr->x.pList; in sqlite3TreeViewExpr()
32271 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0; in sqlite3TreeViewExpr()
32277 if( pExpr->op==TK_AGG_FUNCTION ){ in sqlite3TreeViewExpr()
32279 pExpr->op2, pExpr->u.zToken, zFlgs, in sqlite3TreeViewExpr()
32280 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, in sqlite3TreeViewExpr()
32281 pExpr->iAgg, pExpr->pAggInfo); in sqlite3TreeViewExpr()
32282 }else if( pExpr->op2!=0 ){ in sqlite3TreeViewExpr()
32285 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
32287 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; in sqlite3TreeViewExpr()
32288 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; in sqlite3TreeViewExpr()
32289 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; in sqlite3TreeViewExpr()
32290 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; in sqlite3TreeViewExpr()
32292 pExpr->u.zToken, zFlgs, zOp2); in sqlite3TreeViewExpr()
32294 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
32309 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32310 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32315 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32316 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32322 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
32323 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); in sqlite3TreeViewExpr()
32326 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); in sqlite3TreeViewExpr()
32331 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
32333 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
32335 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
32348 ** X is stored in pExpr->pLeft. in sqlite3TreeViewExpr()
32349 ** Y is stored in pExpr->pList->a[0].pExpr. in sqlite3TreeViewExpr()
32350 ** Z is stored in pExpr->pList->a[1].pExpr. in sqlite3TreeViewExpr()
32354 pX = pExpr->pLeft; in sqlite3TreeViewExpr()
32356 assert( pExpr->x.pList->nExpr==2 ); in sqlite3TreeViewExpr()
32357 pY = pExpr->x.pList->a[0].pExpr; in sqlite3TreeViewExpr()
32358 pZ = pExpr->x.pList->a[1].pExpr; in sqlite3TreeViewExpr()
32367 ** to a column in the new.* or old.* pseudo-tables available to in sqlite3TreeViewExpr()
32369 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn in sqlite3TreeViewExpr()
32370 ** is set to the column of the pseudo-table to read, or to -1 to in sqlite3TreeViewExpr()
32374 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); in sqlite3TreeViewExpr()
32379 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
32381 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
32387 switch( pExpr->affExpr ){ in sqlite3TreeViewExpr()
32394 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); in sqlite3TreeViewExpr()
32400 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
32401 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
32407 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); in sqlite3TreeViewExpr()
32412 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", in sqlite3TreeViewExpr()
32413 pExpr->iColumn, pExpr->iTable-1, in sqlite3TreeViewExpr()
32414 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); in sqlite3TreeViewExpr()
32415 assert( ExprUseXSelect(pExpr->pLeft) ); in sqlite3TreeViewExpr()
32416 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); in sqlite3TreeViewExpr()
32420 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); in sqlite3TreeViewExpr()
32421 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32428 tmp.op = pExpr->op2; in sqlite3TreeViewExpr()
32433 if( pExpr->iColumn<=0 ){ in sqlite3TreeViewExpr()
32437 pExpr->iColumn-1); in sqlite3TreeViewExpr()
32442 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); in sqlite3TreeViewExpr()
32448 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
32449 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
32452 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
32459 ** Generate a human-readable explanation of an expression list.
32472 for(i=0; i<pList->nExpr; i++){ in sqlite3TreeViewBareExprList()
32473 int j = pList->a[i].u.x.iOrderByCol; in sqlite3TreeViewBareExprList()
32474 char *zName = pList->a[i].zEName; in sqlite3TreeViewBareExprList()
32475 int moreToFollow = i<pList->nExpr - 1; in sqlite3TreeViewBareExprList()
32481 switch( pList->a[i].fg.eEName ){ in sqlite3TreeViewBareExprList()
32486 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); in sqlite3TreeViewBareExprList()
32487 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); in sqlite3TreeViewBareExprList()
32488 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); in sqlite3TreeViewBareExprList()
32489 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); in sqlite3TreeViewBareExprList()
32502 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); in sqlite3TreeViewBareExprList()
32521 ** Generate a human-readable explanation of an id-list.
32534 for(i=0; i<pList->nId; i++){ in sqlite3TreeViewBareIdList()
32535 char *zName = pList->a[i].zName; in sqlite3TreeViewBareIdList()
32536 int moreToFollow = i<pList->nId - 1; in sqlite3TreeViewBareIdList()
32540 if( pList->eU4==EU4_NONE ){ in sqlite3TreeViewBareIdList()
32542 }else if( pList->eU4==EU4_IDX ){ in sqlite3TreeViewBareIdList()
32543 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx); in sqlite3TreeViewBareIdList()
32545 assert( pList->eU4==EU4_EXPR ); in sqlite3TreeViewBareIdList()
32546 if( pList->a[i].u4.pExpr==0 ){ in sqlite3TreeViewBareIdList()
32550 sqlite3TreeViewPush(&pView, i<pList->nId-1); in sqlite3TreeViewBareIdList()
32551 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0); in sqlite3TreeViewBareIdList()
32571 ** Generate a human-readable explanation of a list of Upsert objects
32582 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); in sqlite3TreeViewUpsert()
32584 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); in sqlite3TreeViewUpsert()
32585 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); in sqlite3TreeViewUpsert()
32586 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); in sqlite3TreeViewUpsert()
32587 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); in sqlite3TreeViewUpsert()
32588 if( pUpsert->pUpsertWhere ){ in sqlite3TreeViewUpsert()
32589 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewUpsert()
32590 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); in sqlite3TreeViewUpsert()
32594 pUpsert = pUpsert->pNextUpsert; in sqlite3TreeViewUpsert()
32601 ** Generate a human-readable diagram of the data structure that go
32623 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
32628 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
32634 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
32640 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewDelete()
32643 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
32649 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewDelete()
32657 ** Generate a human-readable diagram of the data structure that go
32690 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
32695 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
32701 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); in sqlite3TreeViewInsert()
32704 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
32705 sqlite3TreeViewLine(pView, "DATA-SOURCE"); in sqlite3TreeViewInsert()
32710 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); in sqlite3TreeViewInsert()
32713 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
32719 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewInsert()
32727 ** Generate a human-readable diagram of the data structure that go
32762 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
32767 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
32773 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); in sqlite3TreeViewUpdate()
32776 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
32782 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewUpdate()
32785 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
32791 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
32797 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewUpdate()
32805 ** Show a human-readable graph of a TriggerStep
32816 moreToFollow || (showFullList && pStep->pNext!=0)); in sqlite3TreeViewTriggerStep()
32818 if( cnt++ && pStep->pNext==0 ){ in sqlite3TreeViewTriggerStep()
32822 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); in sqlite3TreeViewTriggerStep()
32823 }while( showFullList && (pStep = pStep->pNext)!=0 ); in sqlite3TreeViewTriggerStep()
32828 ** Show a human-readable graph of a Trigger
32839 moreToFollow || (showFullList && pTrigger->pNext!=0)); in sqlite3TreeViewTrigger()
32841 if( cnt++ && pTrigger->pNext==0 ){ in sqlite3TreeViewTrigger()
32845 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); in sqlite3TreeViewTrigger()
32847 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); in sqlite3TreeViewTrigger()
32849 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); in sqlite3TreeViewTrigger()
32856 ** These simplified versions of the tree-view routines omit unnecessary
32902 ** This file contains code to implement a pseudo-random number
32921 /* The RFC-7539 ChaCha20 block function
32923 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
32952 /* The "wsdPrng" macro will resolve to the pseudo-random number generator in sqlite3_randomness()
32954 ** we have to locate the state vector at run-time. In the more common in sqlite3_randomness()
33006 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); in sqlite3_randomness()
33007 wsdPrng.n -= N; in sqlite3_randomness()
33012 N -= wsdPrng.n; in sqlite3_randomness()
33063 ** This file presents a simple cross-platform threading interface for
33076 ** single-threaded if desired.
33088 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33111 /* This routine is never used in single-threaded mode */ in sqlite3ThreadCreate()
33118 p->xTask = xTask; in sqlite3ThreadCreate()
33119 p->pIn = pIn; in sqlite3ThreadCreate()
33127 rc = pthread_create(&p->tid, 0, xTask, pIn); in sqlite3ThreadCreate()
33130 p->done = 1; in sqlite3ThreadCreate()
33131 p->pOut = xTask(pIn); in sqlite3ThreadCreate()
33143 if( p->done ){ in sqlite3ThreadJoin()
33144 *ppOut = p->pOut; in sqlite3ThreadJoin()
33147 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; in sqlite3ThreadJoin()
33160 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33186 assert( p->id==GetCurrentThreadId() ); in sqlite3ThreadProc()
33188 assert( p->xTask!=0 ); in sqlite3ThreadProc()
33189 p->pResult = p->xTask(p->pIn); in sqlite3ThreadProc()
33216 p->xTask = xTask; in sqlite3ThreadCreate()
33217 p->pIn = pIn; in sqlite3ThreadCreate()
33218 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); in sqlite3ThreadCreate()
33219 if( p->tid==0 ){ in sqlite3ThreadCreate()
33223 if( p->xTask==0 ){ in sqlite3ThreadCreate()
33224 p->id = GetCurrentThreadId(); in sqlite3ThreadCreate()
33225 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
33240 if( p->xTask==0 ){ in sqlite3ThreadJoin()
33241 /* assert( p->id==GetCurrentThreadId() ); */ in sqlite3ThreadJoin()
33243 assert( p->tid==0 ); in sqlite3ThreadJoin()
33245 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); in sqlite3ThreadJoin()
33246 rc = sqlite3Win32Wait((HANDLE)p->tid); in sqlite3ThreadJoin()
33248 bRc = CloseHandle((HANDLE)p->tid); in sqlite3ThreadJoin()
33251 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; in sqlite3ThreadJoin()
33260 /********************************* Single-Threaded **************************/
33289 p->xTask = xTask; in sqlite3ThreadCreate()
33290 p->pIn = pIn; in sqlite3ThreadCreate()
33292 p->xTask = 0; in sqlite3ThreadCreate()
33293 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
33304 if( p->xTask ){ in sqlite3ThreadJoin()
33305 *ppOut = p->xTask(p->pIn); in sqlite3ThreadJoin()
33307 *ppOut = p->pResult; in sqlite3ThreadJoin()
33323 /****************************** End Single-Threaded *************************/
33339 ** This file contains routines used to translate between UTF-8,
33340 ** UTF-16, UTF-16BE, and UTF-16LE.
33342 ** Notes on UTF-8:
33344 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
33351 ** Notes on UTF-16: (with wwww+1==uuuuu)
33353 ** Word-0 Word-1 Value
33359 ** 0xff 0xfe little-endian utf-16 follows
33360 ** 0xfe 0xff big-endian utf-16 follows
33377 ** a multi-byte UTF8 character.
33416 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33417 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33428 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33429 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33436 ** Translate a single UTF-8 character. Return the unicode value.
33443 ** Notes On Invalid UTF-8:
33445 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
33446 ** be encoded as a multi-byte character. Any multi-byte character that
33450 ** If a multi-byte character attempts to encode a value between
33454 ** byte of a character are interpreted as single-byte characters
33458 ** * This routine accepts over-length UTF8 encodings
33459 ** for unicode values 0x80 and greater. It does not change over-length
33465 c = sqlite3Utf8Trans1[c-0xc0]; \
33479 ** For this routine, we assume the UTF8 string is always zero-terminated. in sqlite3Utf8Read()
33483 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8Read()
33517 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); in sqlite3VdbeMemTranslate()
33518 assert( pMem->flags&MEM_Str ); in sqlite3VdbeMemTranslate()
33519 assert( pMem->enc!=desiredEnc ); in sqlite3VdbeMemTranslate()
33520 assert( pMem->enc!=0 ); in sqlite3VdbeMemTranslate()
33521 assert( pMem->n>=0 ); in sqlite3VdbeMemTranslate()
33533 /* If the translation is between UTF-16 little and big endian, then in sqlite3VdbeMemTranslate()
33537 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
33545 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
33546 zTerm = &zIn[pMem->n&~1]; in sqlite3VdbeMemTranslate()
33553 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
33559 /* When converting from UTF-16, the maximum growth results from in sqlite3VdbeMemTranslate()
33560 ** translating a 2-byte character to a 4-byte UTF-8 character. in sqlite3VdbeMemTranslate()
33562 ** nul-terminator. in sqlite3VdbeMemTranslate()
33564 pMem->n &= ~1; in sqlite3VdbeMemTranslate()
33565 len = 2 * (sqlite3_int64)pMem->n + 1; in sqlite3VdbeMemTranslate()
33567 /* When converting from UTF-8 to UTF-16 the maximum growth is caused in sqlite3VdbeMemTranslate()
33568 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 in sqlite3VdbeMemTranslate()
33570 ** nul-terminator. in sqlite3VdbeMemTranslate()
33572 len = 2 * (sqlite3_int64)pMem->n + 2; in sqlite3VdbeMemTranslate()
33581 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
33582 zTerm = &zIn[pMem->n]; in sqlite3VdbeMemTranslate()
33583 zOut = sqlite3DbMallocRaw(pMem->db, len); in sqlite3VdbeMemTranslate()
33589 if( pMem->enc==SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
33591 /* UTF-8 -> UTF-16 Little-endian */ in sqlite3VdbeMemTranslate()
33598 /* UTF-8 -> UTF-16 Big-endian */ in sqlite3VdbeMemTranslate()
33604 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
33608 if( pMem->enc==SQLITE_UTF16LE ){ in sqlite3VdbeMemTranslate()
33609 /* UTF-16 Little-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
33621 zIn -= 2; in sqlite3VdbeMemTranslate()
33638 /* UTF-16 Big-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
33650 zIn -= 2; in sqlite3VdbeMemTranslate()
33667 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
33670 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); in sqlite3VdbeMemTranslate()
33672 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); in sqlite3VdbeMemTranslate()
33674 pMem->flags = c; in sqlite3VdbeMemTranslate()
33675 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
33676 pMem->z = (char*)zOut; in sqlite3VdbeMemTranslate()
33677 pMem->zMalloc = pMem->z; in sqlite3VdbeMemTranslate()
33678 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); in sqlite3VdbeMemTranslate()
33696 ** This routine checks for a byte-order mark at the beginning of the
33697 ** UTF-16 string stored in *pMem. If one is present, it is removed and
33699 ** byte-swapping, it just sets Mem.enc appropriately.
33708 assert( pMem->n>=0 ); in sqlite3VdbeMemHandleBom()
33709 if( pMem->n>1 ){ in sqlite3VdbeMemHandleBom()
33710 u8 b1 = *(u8 *)pMem->z; in sqlite3VdbeMemHandleBom()
33711 u8 b2 = *(((u8 *)pMem->z) + 1); in sqlite3VdbeMemHandleBom()
33723 pMem->n -= 2; in sqlite3VdbeMemHandleBom()
33724 memmove(pMem->z, &pMem->z[2], pMem->n); in sqlite3VdbeMemHandleBom()
33725 pMem->z[pMem->n] = '\0'; in sqlite3VdbeMemHandleBom()
33726 pMem->z[pMem->n+1] = '\0'; in sqlite3VdbeMemHandleBom()
33727 pMem->flags |= MEM_Term; in sqlite3VdbeMemHandleBom()
33728 pMem->enc = bom; in sqlite3VdbeMemHandleBom()
33736 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
33749 zTerm = (const u8*)(-1); in sqlite3Utf8CharLen()
33759 /* This test function is not currently used by the automated test-suite.
33764 ** Translate UTF-8 to UTF-8.
33766 ** This has the effect of making sure that the string is well-formed
33767 ** UTF-8. Miscoded characters are removed.
33769 ** The translation is done in-place and aborted if the output
33784 return (int)(zOut - zStart); in sqlite3Utf8To8()
33790 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
33791 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
33802 if( db->mallocFailed ){ in sqlite3Utf16to8()
33806 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
33807 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
33808 assert( m.z || db->mallocFailed ); in sqlite3Utf16to8()
33813 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
33815 ** in pZ. nChar must be non-negative.
33829 return (int)(z-(unsigned char const *)zIn) in sqlite3Utf16ByteLen()
33830 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); in sqlite3Utf16ByteLen()
33849 n = (int)(z-zBuf); in sqlite3UtfSelfTest()
33858 assert( (z-zBuf)==n ); in sqlite3UtfSelfTest()
33895 ** sqlite3FaultSim() function only returns non-zero during testing.
33897 ** During testing, if the test harness has set a fault-sim callback using
33899 ** each call to sqlite3FaultSim() is relayed to that application-supplied
33900 ** callback and the integer return value form the application-supplied
33938 ** lower 30 bits of a 32-bit signed integer.
33953 ** The column type is an extra string stored after the zero-terminator on
33957 if( pCol->colFlags & COLFLAG_HASTYPE ){ in sqlite3ColumnType()
33958 return pCol->zCnName + strlen(pCol->zCnName) + 1; in sqlite3ColumnType()
33959 }else if( pCol->eCType ){ in sqlite3ColumnType()
33960 assert( pCol->eCType<=SQLITE_N_STDTYPE ); in sqlite3ColumnType()
33961 return (char*)sqlite3StdType[pCol->eCType-1]; in sqlite3ColumnType()
33968 ** Helper function for sqlite3Error() - called rarely. Broken out into
33973 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorFinish()
33984 db->errCode = err_code; in sqlite3Error()
33985 if( err_code || db->pErr ){ in sqlite3Error()
33988 db->errByteOffset = -1; in sqlite3Error()
33998 db->errCode = SQLITE_OK; in sqlite3ErrorClear()
33999 db->errByteOffset = -1; in sqlite3ErrorClear()
34000 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorClear()
34011 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); in sqlite3SystemError()
34021 ** assumed to be encoded in UTF-8.
34029 db->errCode = err_code; in sqlite3ErrorWithMsg()
34033 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ in sqlite3ErrorWithMsg()
34039 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); in sqlite3ErrorWithMsg()
34047 sqlite3 *db = p->db; in sqlite3ProgressCheck()
34048 if( AtomicLoad(&db->u1.isInterrupted) ){ in sqlite3ProgressCheck()
34049 p->nErr++; in sqlite3ProgressCheck()
34050 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
34053 if( db->xProgress && (++p->nProgressSteps)>=db->nProgressOps ){ in sqlite3ProgressCheck()
34054 if( db->xProgress(db->pProgressArg) ){ in sqlite3ProgressCheck()
34055 p->nErr++; in sqlite3ProgressCheck()
34056 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
34058 p->nProgressSteps = 0; in sqlite3ProgressCheck()
34064 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
34076 sqlite3 *db = pParse->db; in sqlite3ErrorMsg()
34078 assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); in sqlite3ErrorMsg()
34079 db->errByteOffset = -2; in sqlite3ErrorMsg()
34083 if( db->errByteOffset<-1 ) db->errByteOffset = -1; in sqlite3ErrorMsg()
34084 if( db->suppressErr ){ in sqlite3ErrorMsg()
34086 if( db->mallocFailed ){ in sqlite3ErrorMsg()
34087 pParse->nErr++; in sqlite3ErrorMsg()
34088 pParse->rc = SQLITE_NOMEM; in sqlite3ErrorMsg()
34091 pParse->nErr++; in sqlite3ErrorMsg()
34092 sqlite3DbFree(db, pParse->zErrMsg); in sqlite3ErrorMsg()
34093 pParse->zErrMsg = zMsg; in sqlite3ErrorMsg()
34094 pParse->rc = SQLITE_ERROR; in sqlite3ErrorMsg()
34095 pParse->pWith = 0; in sqlite3ErrorMsg()
34106 if( db==0 || (pParse = db->pParse)==0 ) return errCode; in sqlite3ErrorToParser()
34107 pParse->rc = errCode; in sqlite3ErrorToParser()
34108 pParse->nErr++; in sqlite3ErrorToParser()
34113 ** Convert an SQL-style quoted string into a normal string by removing
34114 ** the quote characters. The conversion is done in-place. If the
34116 ** is a no-op.
34118 ** The input string must be zero-terminated. A new zero-terminator
34121 ** The return value is -1 if no dequoting occurs or the length of the
34125 ** 2002-02-14: This routine is extended to remove MS-Access style
34126 ** brackets from around identifiers. For example: "[a-b-c]" becomes
34127 ** "a-b-c".
34153 assert( sqlite3Isquote(p->u.zToken[0]) ); in sqlite3DequoteExpr()
34154 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; in sqlite3DequoteExpr()
34155 sqlite3Dequote(p->u.zToken); in sqlite3DequoteExpr()
34162 ** "abc" -> abc
34163 ** "ab""cd" -> (not possible because of the interior "")
34167 ** is always a no-op.
34171 if( p->n<2 ) return; in sqlite3DequoteToken()
34172 if( !sqlite3Isquote(p->z[0]) ) return; in sqlite3DequoteToken()
34173 for(i=1; i<p->n-1; i++){ in sqlite3DequoteToken()
34174 if( sqlite3Isquote(p->z[i]) ) return; in sqlite3DequoteToken()
34176 p->n -= 2; in sqlite3DequoteToken()
34177 p->z++; in sqlite3DequoteToken()
34184 p->z = z; in sqlite3TokenInit()
34185 p->n = sqlite3Strlen30(z); in sqlite3TokenInit()
34188 /* Convenient short-hand */
34195 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
34197 ** the contents of two buffers containing UTF-8 strings in a
34198 ** case-independent fashion, using the same definition of "case
34203 return zRight ? -1 : 0; in sqlite3_stricmp()
34220 c = (int)UpperToLower[c] - (int)UpperToLower[x]; in sqlite3StrICmp()
34231 return zRight ? -1 : 0; in sqlite3_strnicmp()
34237 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } in sqlite3_strnicmp()
34238 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; in sqlite3_strnicmp()
34242 ** Compute an 8-bit hash on a string that is insensitive to case differences
34255 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
34298 ** uses the encoding enc. The string is not necessarily zero-terminated.
34306 ** -1 => Not a valid number, but has a valid prefix which
34311 ** [+-]digits[E[+-]digits]
34312 ** [+-]digits.[digits][E[+-]digits]
34313 ** [+-].digits[E[+-]digits]
34335 int eValid = 1; /* True exponent is either not used or is well-formed */ in sqlite3AtoF()
34338 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ in sqlite3AtoF()
34354 for(i=3-enc; i<length && z[i]==0; i+=2){} in sqlite3AtoF()
34355 if( i<length ) eType = -100; in sqlite3AtoF()
34365 if( *z=='-' ){ in sqlite3AtoF()
34366 sign = -1; in sqlite3AtoF()
34374 s = s*10 + (*z - '0'); in sqlite3AtoF()
34376 if( s>=((LARGEST_INT64-9)/10) ){ in sqlite3AtoF()
34377 /* skip non-significant significand digits in sqlite3AtoF()
34391 if( s<((LARGEST_INT64-9)/10) ){ in sqlite3AtoF()
34392 s = s*10 + (*z - '0'); in sqlite3AtoF()
34393 d--; in sqlite3AtoF()
34410 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ in sqlite3AtoF()
34413 if( *z=='-' ){ in sqlite3AtoF()
34414 esign = -1; in sqlite3AtoF()
34421 e = e<10000 ? (e*10 + (*z - '0')) : 10000; in sqlite3AtoF()
34434 esign = -1; in sqlite3AtoF()
34435 e *= -1; in sqlite3AtoF()
34442 result = sign<0 ? -(double)0 : (double)0; in sqlite3AtoF()
34450 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/ in sqlite3AtoF()
34452 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ in sqlite3AtoF()
34455 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ in sqlite3AtoF()
34458 e--; in sqlite3AtoF()
34462 s = sign<0 ? -s : s; in sqlite3AtoF()
34464 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ in sqlite3AtoF()
34468 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ in sqlite3AtoF()
34469 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ in sqlite3AtoF()
34470 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308); in sqlite3AtoF()
34503 /* return true if number and no extra non-whitespace chracters after */ in sqlite3AtoF()
34507 return -1; in sqlite3AtoF()
34520 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
34532 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; in sqlite3Int64ToText()
34536 i = sizeof(zTemp)-2; in sqlite3Int64ToText()
34537 zTemp[sizeof(zTemp)-1] = 0; in sqlite3Int64ToText()
34538 while( 1 /*exit-by-break*/ ){ in sqlite3Int64ToText()
34542 i--; in sqlite3Int64ToText()
34544 if( v<0 ) zTemp[--i] = '-'; in sqlite3Int64ToText()
34545 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); in sqlite3Int64ToText()
34546 return sizeof(zTemp)-1-i; in sqlite3Int64ToText()
34550 ** Compare the 19-character string zNum against the text representation
34561 ** will return -8.
34569 c = (zNum[i*incr]-pow63[i])*10; in compare2pow63()
34572 c = zNum[18*incr] - '8'; in compare2pow63()
34573 testcase( c==(-1) ); in compare2pow63()
34581 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
34586 ** -1 Not even a prefix of the input text looks like an integer
34587 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34588 ** 1 Excess non-space text after the integer value
34589 ** 2 Integer too large for a 64-bit signed integer or is malformed
34593 ** The string is not necessarily zero-terminated. The encoding is
34602 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ in sqlite3Atoi64()
34613 for(i=3-enc; i<length && zNum[i]==0; i+=2){} in sqlite3Atoi64()
34620 if( *zNum=='-' ){ in sqlite3Atoi64()
34630 u = u*10 + c - '0'; in sqlite3Atoi64()
34637 ** from clang and -fsanitize=undefined. This test and assignment make in sqlite3Atoi64()
34639 ** them, but we must appaise the undefined-behavior pharisees. */ in sqlite3Atoi64()
34642 *pNum = -(i64)u; in sqlite3Atoi64()
34648 rc = -1; in sqlite3Atoi64()
34649 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ in sqlite3Atoi64()
34655 rc = 1; /* Extra non-space text after the integer */ in sqlite3Atoi64()
34666 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ in sqlite3Atoi64()
34680 assert( u-1==LARGEST_INT64 ); in sqlite3Atoi64()
34688 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
34689 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
34694 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34696 ** 2 Integer too large for a 64-bit signed integer or is malformed
34711 if( k-i>16 ) return 2; in sqlite3DecOrHexToI64()
34722 ** If zNum represents an integer that will fit in 32-bits, then set
34727 ** Any non-numeric characters that following zNum are ignored.
34729 ** input number to be zero-terminated.
34735 if( zNum[0]=='-' ){ in sqlite3GetInt32()
34762 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ in sqlite3GetInt32()
34769 ** 2^31 -> 2147483648 in sqlite3GetInt32()
34775 testcase( v-neg==2147483647 ); in sqlite3GetInt32()
34776 if( v-neg>2147483647 ){ in sqlite3GetInt32()
34780 v = -v; in sqlite3GetInt32()
34787 ** Return a 32-bit integer value extracted from a string. If the
34797 ** Try to convert z into an unsigned 32-bit integer. Return true on
34806 v = v*10 + z[i] - '0'; in sqlite3GetUInt32()
34815 ** The variable-length integer encoding is as follows:
34822 ** 7 bits - A
34823 ** 14 bits - BA
34824 ** 21 bits - BBA
34825 ** 28 bits - BBBA
34826 ** 35 bits - BBBBA
34827 ** 42 bits - BBBBBA
34828 ** 49 bits - BBBBBBA
34829 ** 56 bits - BBBBBBBA
34830 ** 64 bits - BBBBBBBBC
34834 ** Write a 64-bit variable-length integer to memory starting at p[0].
34838 ** A variable-length integer consists of the lower 7 bits of each byte
34849 for(i=7; i>=0; i--){ in putVarint64()
34862 for(i=0, j=n-1; j>=0; j--, i++){ in putVarint64()
34894 ** Read a 64-bit variable-length integer from memory starting at p[0].
35037 b = p[-4]; in sqlite3GetVarint()
35048 ** Read a 32-bit variable-length integer from memory starting at p[0].
35051 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
35055 ** single-byte case. All code should use the MACRO version as
35056 ** this function assumes the single-byte case has already been handled.
35061 /* The 1-byte case. Overwhelmingly the most common. Handled inline in sqlite3GetVarint32()
35074 /* The 2-byte case */ in sqlite3GetVarint32()
35087 /* The 3-byte case */ in sqlite3GetVarint32()
35102 /* A 32-bit varint is used to store size information in btrees. in sqlite3GetVarint32()
35103 ** Objects are rarely larger than 2MiB limit of a 3-byte varint. in sqlite3GetVarint32()
35104 ** A 3-byte varint is sufficient, for example, to record the size in sqlite3GetVarint32()
35105 ** of a 1048569-byte BLOB or string. in sqlite3GetVarint32()
35107 ** We only unroll the first 1-, 2-, and 3- byte cases. The very in sqlite3GetVarint32()
35108 ** rare larger cases can be handled by the slower 64-bit varint in sqlite3GetVarint32()
35116 n = sqlite3GetVarint(p-2, &v64); in sqlite3GetVarint32()
35128 ** unrolling for the 3- and 4-byte varint cases. This code is in sqlite3GetVarint32()
35161 ** slow) general-purpose sqlite3GetVarint() routine to extract the in sqlite3GetVarint32()
35167 p -= 4; in sqlite3GetVarint32()
35178 ** 64-bit integer.
35188 ** Read or write a four-byte big-endian integer value.
35255 n--; in sqlite3HexToBlob()
35298 eOpenState = db->eOpenState; in sqlite3SafetyCheckOk()
35311 eOpenState = db->eOpenState; in sqlite3SafetyCheckSickOrOk()
35324 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
35325 ** the other 64-bit signed integer at *pA and store the result in *pA.
35335 testcase( iB==-1 ); testcase( iB==0 ); in sqlite3AddInt64()
35337 testcase( iA>0 && LARGEST_INT64 - iA == iB ); in sqlite3AddInt64()
35338 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); in sqlite3AddInt64()
35339 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; in sqlite3AddInt64()
35341 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); in sqlite3AddInt64()
35342 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); in sqlite3AddInt64()
35343 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; in sqlite3AddInt64()
35355 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); in sqlite3SubInt64()
35357 *pA -= iB; in sqlite3SubInt64()
35360 return sqlite3AddInt64(pA, -iB); in sqlite3SubInt64()
35378 if( -iA>LARGEST_INT64/-iB ) return 1; in sqlite3MulInt64()
35387 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
35388 ** if the integer has a value of -2147483648, return +2147483647
35393 return -x; in sqlite3AbsInt32()
35398 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
35404 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
35409 ** test.db-journal => test.nal
35410 ** test.db-wal => test.wal
35411 ** test.db-shm => test.shm
35412 ** test.db-mj7f3319fa => test.9fa
35421 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} in sqlite3FileSuffix3()
35422 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); in sqlite3FileSuffix3()
35440 5, 5, 5, /* 12-14 */ in sqlite3LogEstAdd()
35441 4, 4, 4, 4, /* 15-18 */ in sqlite3LogEstAdd()
35442 3, 3, 3, 3, 3, 3, /* 19-24 */ in sqlite3LogEstAdd()
35443 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ in sqlite3LogEstAdd()
35448 return a+x[a-b]; in sqlite3LogEstAdd()
35452 return b+x[b-a]; in sqlite3LogEstAdd()
35465 while( x<8 ){ y -= 10; x <<= 1; } in sqlite3LogEst()
35468 int i = 60 - __builtin_clzll(x); in sqlite3LogEst()
35472 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ in sqlite3LogEst()
35476 return a[x&7] + y - 10; in sqlite3LogEst()
35490 e = (a>>52) - 1022; in sqlite3LogEstFromDouble()
35501 if( n>=5 ) n -= 2; in sqlite3LogEstToInt()
35502 else if( n>=1 ) n -= 1; in sqlite3LogEstToInt()
35504 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); in sqlite3LogEstToInt()
35511 ** db->mallocFailed flag is set.
35524 ** is always zero-terminated.
35611 ** High-resolution hardware timer used for debugging and testing only.
35630 ** This file contains inline asm code for retrieving "high-performance"
35637 ** The following routine only works on pentium-class (or newer) processors.
35639 ** processor and returns that value. This can be used for high-res
35721 ** This is the implementation of generic hash-tables
35734 pNew->first = 0;
35735 pNew->count = 0;
35736 pNew->htsize = 0;
35737 pNew->ht = 0;
35748 elem = pH->first;
35749 pH->first = 0;
35750 sqlite3_free(pH->ht);
35751 pH->ht = 0;
35752 pH->htsize = 0;
35754 HashElem *next_elem = elem->next;
35758 pH->count = 0;
35767 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
35770 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
35788 pHead = pEntry->count ? pEntry->chain : 0;
35789 pEntry->count++;
35790 pEntry->chain = pNew;
35795 pNew->next = pHead;
35796 pNew->prev = pHead->prev;
35797 if( pHead->prev ){ pHead->prev->next = pNew; }
35798 else { pH->first = pNew; }
35799 pHead->prev = pNew;
35801 pNew->next = pH->first;
35802 if( pH->first ){ pH->first->prev = pNew; }
35803 pNew->prev = 0;
35804 pH->first = pNew;
35823 if( new_size==pH->htsize ) return 0;
35839 sqlite3_free(pH->ht);
35840 pH->ht = new_ht;
35841 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
35843 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
35844 unsigned int h = strHash(elem->pKey) % new_size;
35845 next_elem = elem->next;
35866 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
35868 h = strHash(pKey) % pH->htsize;
35869 pEntry = &pH->ht[h];
35870 elem = pEntry->chain;
35871 count = pEntry->count;
35874 elem = pH->first;
35875 count = pH->count;
35880 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
35883 elem = elem->next;
35884 count--;
35898 if( elem->prev ){
35899 elem->prev->next = elem->next;
35901 pH->first = elem->next;
35903 if( elem->next ){
35904 elem->next->prev = elem->prev;
35906 if( pH->ht ){
35907 pEntry = &pH->ht[h];
35908 if( pEntry->chain==elem ){
35909 pEntry->chain = elem->next;
35911 assert( pEntry->count>0 );
35912 pEntry->count--;
35915 pH->count--;
35916 if( pH->count==0 ){
35917 assert( pH->first==0 );
35918 assert( pH->count==0 );
35930 return findElementWithHash(pH, pKey, 0)->data;
35955 if( elem->data ){
35956 void *old_data = elem->data;
35960 elem->data = data;
35961 elem->pKey = pKey;
35968 new_elem->pKey = pKey;
35969 new_elem->data = data;
35970 pH->count++;
35971 if( pH->count>=10 && pH->count > 2*pH->htsize ){
35972 if( rehash(pH, pH->count*2) ){
35973 assert( pH->htsize>0 );
35974 h = strHash(pKey) % pH->htsize;
35977 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
36054 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
36055 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
36056 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
36085 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
36102 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
36119 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
36155 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
36190 ** 2022-09-06
36233 ** VFS - the database and the rollback journal.
36242 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
36357 /* Forward declarations for the low-level storage engine
36373 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
36377 ** underlying key/value store to use - either "local" or "session".
36379 ** Both zKey and zData are zero-terminated pure text strings.
36393 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
36406 ** this routine is a no-op.
36412 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
36423 ** not counting the final zero terminator. Return -1 if the key does
36443 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36444 return -1;
36450 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
36459 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36460 return -1;
36462 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
36465 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
36492 ** for JavaScript-side implementations in WASM builds. In such builds
36521 ** ---------------
36523 ** * Non-zero bytes are encoded as upper-case hexadecimal
36525 ** * A sequence of one or more zero-bytes that are not at the
36526 ** beginning of the buffer are encoded as a little-endian
36527 ** base-26 number using a..z. "a" means 0. "b" means 1,
36531 ** of hexadecimal and base-26 numbers, it is always clear where
36543 /* A sequence of 1 or more zeros is stored as a little-endian
36544 ** base-26 number using a..z as the digits. So one zero is "b".
36550 i += k-1;
36562 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36563 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36564 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36565 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36566 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36567 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36568 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36569 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36571 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36572 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36573 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36574 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36575 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36576 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36577 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36578 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36601 n += (c - 'a')*mult;
36605 if( j+n>nOut ) return -1;
36621 ** Decode a complete journal file. Allocate space in pFile->aJrnl
36622 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
36625 ** The first few characters of the text encoding will be a little-endian
36626 ** base-26 number (digits a..z) that is the total number of bytes
36627 ** in the decoded journal file image. This base-26 number is followed
36629 ** separator is required to act as a terminator for the base-26 number.
36632 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36633 const char *zTxt, /* Text encoding. Zero-terminated */
36641 n += (zTxt[i] - 'a')*mult;
36644 sqlite3_free(pFile->aJrnl);
36645 pFile->aJrnl = sqlite3_malloc64( n );
36646 if( pFile->aJrnl==0 ){
36647 pFile->nJrnl = 0;
36650 pFile->nJrnl = n;
36651 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36652 if( n<pFile->nJrnl ){
36653 sqlite3_free(pFile->aJrnl);
36654 pFile->aJrnl = 0;
36655 pFile->nJrnl = 0;
36665 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36671 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36677 ** Close an kvvfs-file.
36682 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36683 pFile->isJournal ? "journal" : "db"));
36684 sqlite3_free(pFile->aJrnl);
36685 sqlite3_free(pFile->aData);
36690 ** Read from the -journal file.
36699 assert( pFile->isJournal );
36700 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36701 if( pFile->aJrnl==0 ){
36702 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36709 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36712 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36714 if( iOfst+iAmt>pFile->nJrnl ){
36717 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36734 char *aData = pFile->aData;
36737 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36742 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36745 pFile->szPage = iAmt;
36751 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey,
36752 aData, SQLITE_KVOS_SZ-1);
36760 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000);
36772 memset(zBuf+n, 0, iAmt-n);
36780 ** Write into the -journal file.
36790 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36792 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36793 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36797 pFile->aJrnl = aNew;
36798 if( pFile->nJrnl<iOfst ){
36799 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36801 pFile->nJrnl = iEnd;
36803 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36819 char *aData = pFile->aData;
36820 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36822 assert( (iAmt & (iAmt-1))==0 );
36823 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36824 pFile->szPage = iAmt;
36828 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36831 if( iOfst+iAmt > pFile->szDb ){
36832 pFile->szDb = iOfst + iAmt;
36838 ** Truncate an kvvfs-file.
36842 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36844 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36845 sqlite3_free(pFile->aJrnl);
36846 pFile->aJrnl = 0;
36847 pFile->nJrnl = 0;
36852 if( pFile->szDb>size
36853 && pFile->szPage>0
36854 && (size % pFile->szPage)==0
36858 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36859 pgno = 1 + size/pFile->szPage;
36860 pgnoMax = 2 + pFile->szDb/pFile->szPage;
36863 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36866 pFile->szDb = size;
36873 ** Sync an kvvfs-file.
36879 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36880 if( pFile->nJrnl<=0 ){
36883 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36887 n = pFile->nJrnl;
36894 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36895 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36904 ** Return the current file-size of an kvvfs-file.
36908 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36909 *pSize = pFile->nJrnl;
36914 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36915 if( pFile->szDb>=0 ){
36916 *pSize = pFile->szDb;
36924 ** Lock an kvvfs-file.
36928 assert( !pFile->isJournal );
36929 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36932 pFile->szDb = kvvfsReadFileSize(pFile);
36938 ** Unlock an kvvfs-file.
36942 assert( !pFile->isJournal );
36943 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36945 pFile->szDb = -1;
36951 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36960 ** File control method. For custom operations on an kvvfs-file.
36971 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36972 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36981 ** Return the sector-size in bytes for an kvvfs-file.
36988 ** Return the device characteristic flags supported by an kvvfs-file.
37012 pFile->isJournal = 0;
37013 pFile->base.pMethods = &kvvfs_db_io_methods;
37015 if( strcmp(zName, "local-journal")==0
37016 || strcmp(zName, "session-journal")==0
37018 pFile->isJournal = 1;
37019 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
37024 pFile->zClass = "session";
37026 pFile->zClass = "local";
37028 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ);
37029 if( pFile->aData==0 ){
37032 pFile->aJrnl = 0;
37033 pFile->nJrnl = 0;
37034 pFile->szPage = -1;
37035 pFile->szDb = -1;
37041 ** ensure the file-system modifications are synced to disk before
37045 if( strcmp(zPath, "local-journal")==0 ){
37048 if( strcmp(zPath, "session-journal")==0 ){
37065 if( strcmp(zPath, "local-journal")==0 ){
37068 if( strcmp(zPath, "session-journal")==0 ){
37101 if( nOut<nPath+1 ) nPath = nOut - 1;
37153 ** This routine is called initialize the KV-vfs as the default VFS.
37183 ** This file contains the VFS implementation for unix-like operating systems
37189 ** use flock(), dot-files, various proprietary locking schemas, or simply
37199 ** * General-purpose declarations and utility functions.
37203 ** + for no-op locks
37204 ** + for dot-file locks
37212 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
37225 ** 3. Dot-file locking,
37285 ** -DHAVE_GETHOSTUUID=0
37286 ** -DHAVE_GETHOSTUUID=1
37289 ** -DSQLITE_ENABLE_LOCKING_STYLE.
37331 ** If we are to be thread-safe, include the pthreads header.
37352 ** Maximum supported path-length.
37371 # define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
37372 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
37411 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
37440 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
37467 ** transaction counter in bytes 24-27 of database files are updated
37532 ** The threadid macro resolves to the thread-id or to 0. Used for
37553 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
37554 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
37562 ** Linux-specific IOCTL magic numbers used for controlling F2FS
37580 ** which always has the same well-defined interface.
37591 ** Many system calls are accessed through pointer-to-functions so that
37620 ** DJGPP. But it is DOS - what did you expect?
37773 ** log if they come from non-root processes. So avoid calling fchown() if
37853 int i = -1;
37857 for(i=0; i<ArraySize(aSyscall)-1; i++){
37885 ** The m parameter will be non-zero only when creating -wal, -journal,
37886 ** and -shm files. We want those files to have *exactly* the same
37888 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
37913 fd = -1;
37984 ** integer lock-type.
38003 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
38004 ** command-line option on the compiler. This code is normally
38020 if( p->l_type==F_RDLCK ){
38022 }else if( p->l_type==F_WRLCK ){
38024 }else if( p->l_type==F_UNLCK ){
38029 assert( p->l_whence==SEEK_SET );
38033 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
38034 (int)p->l_pid, s);
38035 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
38048 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
38068 /* On Android, ftruncate() always uses 32-bit offsets, even if
38129 ** a performance-critical path, so it is sufficient to put these
38154 ** Changes are made in-place. Return the new name length.
38156 ** The original filename is in z[0..n-1]. Return the number of
38161 while( n>1 && z[n-1]=='/' ){ n--; }
38170 while( j>0 && z[j-1]!='/' ){ j--; }
38171 if( j>0 ){ j--; }
38202 pNew->zCanonicalName = (char*)&pNew[1];
38203 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
38204 n = vxworksSimplifyName(pNew->zCanonicalName, n);
38211 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
38212 if( pCandidate->nName==n
38213 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
38216 pCandidate->nRef++;
38223 pNew->nRef = 1;
38224 pNew->nName = n;
38225 pNew->pNext = vxworksFileList;
38237 assert( pId->nRef>0 );
38238 pId->nRef--;
38239 if( pId->nRef==0 ){
38241 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
38243 *pp = pId->pNext;
38301 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
38329 ** on linux - with NPTL a lock created by thread A can override locks
38330 ** in thread B. But there is no way to know at compile-time which
38332 ** compile-time whether or not thread A can override locks on thread B.
38333 ** One has to do a run-time check to discover the behavior of the
38353 ** sizes ino_t at only 32-bits instead of 64-bits. (See
38354 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
38355 ** To work around this, always allocate 64-bits for the inode number.
38356 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
38423 assert( pFile->pInode );
38424 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
38427 assert( pFile->pInode );
38428 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
38434 ** This function - unixLogErrorAtLine(), is only ever called via the macro
38439 ** errno and, if possible, the human-readable equivalent from strerror() or
38445 ** failed (e.g. "unlink", "open") and the associated file-system path,
38459 ** the strerror() function to obtain the human-readable error message
38482 strerror_r(iErrno, aErr, sizeof(aErr)-1);
38488 /* Non-threadsafe build, use strerror(). */
38494 "os_unix.c:%d: (%d) %s(%s) - %s",
38523 pFile ? pFile->zPath : 0, lineno);
38528 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
38532 pFile->lastErrno = error;
38536 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
38539 unixInodeInfo *pInode = pFile->pInode;
38543 for(p=pInode->pUnused; p; p=pNext){
38544 pNext = p->pNext;
38545 robust_close(pFile, p->fd, __LINE__);
38548 pInode->pUnused = 0;
38558 unixInodeInfo *pInode = pFile->pInode;
38562 pInode->nRef--;
38563 if( pInode->nRef==0 ){
38564 assert( pInode->pShmNode==0 );
38565 sqlite3_mutex_enter(pInode->pLockMutex);
38567 sqlite3_mutex_leave(pInode->pLockMutex);
38568 if( pInode->pPrev ){
38569 assert( pInode->pPrev->pNext==pInode );
38570 pInode->pPrev->pNext = pInode->pNext;
38573 inodeList = pInode->pNext;
38575 if( pInode->pNext ){
38576 assert( pInode->pNext->pPrev==pInode );
38577 pInode->pNext->pPrev = pInode->pPrev;
38579 sqlite3_mutex_free(pInode->pLockMutex);
38601 struct stat statbuf; /* Low-level file information */
38606 /* Get low-level information about the file that we can used to
38609 fd = pFile->h;
38614 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
38621 ** incorrectly for zero-size files. See ticket #3260. To work
38630 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
38647 fileId.pId = pFile->pId;
38653 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
38654 pInode = pInode->pNext;
38662 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
38664 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38665 if( pInode->pLockMutex==0 ){
38670 pInode->nRef = 1;
38672 pInode->pNext = inodeList;
38673 pInode->pPrev = 0;
38674 if( inodeList ) inodeList->pPrev = pInode;
38677 pInode->nRef++;
38688 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
38691 return pFile->pInode!=0 &&
38692 (osStat(pFile->zPath, &buf)!=0
38693 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
38712 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
38714 rc = osFstat(pFile->h, &buf);
38716 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
38720 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
38724 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
38728 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
38737 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38748 assert( pFile->eFileLock<=SHARED_LOCK );
38749 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38752 if( pFile->pInode->eFileLock>SHARED_LOCK ){
38759 if( !reserved && !pFile->pInode->bProcessLock ){
38765 if( osFcntl(pFile->h, F_GETLK, &lock) ){
38774 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38775 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
38785 ** Set a posix-advisory-lock.
38789 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
38794 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
38805 int tm = pFile->iBusyTimeout;
38815 tm--;
38823 ** Attempt to set a system-lock on the file pFile. The lock is
38826 ** If the pFile was opened read/write from unix-excl, then the only lock
38829 ** operations become no-ops. Locking operations still happen internally,
38834 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
38835 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
38836 ** and is read-only.
38838 ** Zero is returned if the call completes successfully, or -1 if a call
38843 unixInodeInfo *pInode = pFile->pInode;
38845 assert( sqlite3_mutex_held(pInode->pLockMutex) );
38846 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
38847 if( pInode->bProcessLock==0 ){
38849 assert( pInode->nLock==0 );
38854 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
38856 pInode->bProcessLock = 1;
38857 pInode->nLock++;
38862 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
38868 ** Lock the file with the lock specified by parameter eFileLock - one
38882 ** UNLOCKED -> SHARED
38883 ** SHARED -> RESERVED
38884 ** SHARED -> EXCLUSIVE
38885 ** RESERVED -> (PENDING) -> EXCLUSIVE
38886 ** PENDING -> EXCLUSIVE
38894 ** lock primitives (called read-locks and write-locks below, to avoid
38903 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
38904 ** byte'. If this is successful, 'shared byte range' is read-locked
38907 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
38909 ** Windows95 is now pretty much extinct, but this work-around for the
38910 ** lack of shared-locks on Windows95 lives on, for backwards
38914 ** A RESERVED lock is implemented by grabbing a write-lock on the
38919 ** a write-lock on the entire 'shared byte range'. Since all other locks
38920 ** require a read-lock on one of the bytes within this range, this ensures
38925 ** obtaining a write-lock on the 'pending byte'. This ensures that no new
38929 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
38939 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
38940 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38941 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
38948 if( pFile->eFileLock>=eFileLock ){
38949 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
38959 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
38961 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
38963 /* This mutex is needed because pFile->pInode is shared across threads
38965 pInode = pFile->pInode;
38966 sqlite3_mutex_enter(pInode->pLockMutex);
38971 if( (pFile->eFileLock!=pInode->eFileLock &&
38972 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
38983 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
38985 assert( pFile->eFileLock==0 );
38986 assert( pInode->nShared>0 );
38987 pFile->eFileLock = SHARED_LOCK;
38988 pInode->nShared++;
38989 pInode->nLock++;
39001 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK)
39013 pFile->eFileLock = PENDING_LOCK;
39014 pInode->eFileLock = PENDING_LOCK;
39023 assert( pInode->nShared==0 );
39024 assert( pInode->eFileLock==0 );
39027 /* Now get the read-lock */
39051 pFile->eFileLock = SHARED_LOCK;
39052 pInode->nLock++;
39053 pInode->nShared = 1;
39055 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
39064 assert( 0!=pFile->eFileLock );
39087 /* Set up the transaction-counter change checking flags when
39093 && pFile->eFileLock<=SHARED_LOCK
39096 pFile->transCntrChng = 0;
39097 pFile->dbUpdate = 0;
39098 pFile->inNormalWrite = 1;
39103 pFile->eFileLock = eFileLock;
39104 pInode->eFileLock = eFileLock;
39108 sqlite3_mutex_leave(pInode->pLockMutex);
39109 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
39119 unixInodeInfo *pInode = pFile->pInode;
39120 UnixUnusedFd *p = pFile->pPreallocatedUnused;
39122 p->pNext = pInode->pUnused;
39123 pInode->pUnused = p;
39124 pFile->h = -1;
39125 pFile->pPreallocatedUnused = 0;
39133 ** the requested locking level, this routine is a no-op.
39148 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
39149 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
39153 if( pFile->eFileLock<=eFileLock ){
39156 pInode = pFile->pInode;
39157 sqlite3_mutex_enter(pInode->pLockMutex);
39158 assert( pInode->nShared!=0 );
39159 if( pFile->eFileLock>SHARED_LOCK ){
39160 assert( pInode->eFileLock==pFile->eFileLock );
39171 pFile->inNormalWrite = 0;
39175 ** before establishing the readlock - to avoid a race condition we downgrade
39191 off_t divSize = SHARED_SIZE - 1;
39197 if( unixFileLock(pFile, &lock)==(-1) ){
39207 if( unixFileLock(pFile, &lock)==(-1) ){
39218 lock.l_len = SHARED_SIZE-divSize;
39219 if( unixFileLock(pFile, &lock)==(-1) ){
39250 pInode->eFileLock = SHARED_LOCK;
39262 pInode->nShared--;
39263 if( pInode->nShared==0 ){
39268 pInode->eFileLock = NO_LOCK;
39272 pInode->eFileLock = NO_LOCK;
39273 pFile->eFileLock = NO_LOCK;
39281 pInode->nLock--;
39282 assert( pInode->nLock>=0 );
39283 if( pInode->nLock==0 ) closePendingFds(pFile);
39287 sqlite3_mutex_leave(pInode->pLockMutex);
39289 pFile->eFileLock = eFileLock;
39299 ** the requested locking level, this routine is a no-op.
39303 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
39328 if( pFile->h>=0 ){
39329 robust_close(pFile, pFile->h, __LINE__);
39330 pFile->h = -1;
39333 if( pFile->pId ){
39334 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39335 osUnlink(pFile->pId->zCanonicalName);
39337 vxworksReleaseFileId(pFile->pId);
39338 pFile->pId = 0;
39342 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39343 osUnlink(pFile->zPath);
39344 sqlite3_free(*(char**)&pFile->zPath);
39345 pFile->zPath = 0;
39348 OSTRACE(("CLOSE %-3d\n", pFile->h));
39349 OpenCounter(-1);
39350 sqlite3_free(pFile->pPreallocatedUnused);
39361 unixInodeInfo *pInode = pFile->pInode;
39372 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
39373 sqlite3_mutex_enter(pInode->pLockMutex);
39374 if( pInode->nLock ){
39377 ** descriptor to pInode->pUnused list. It will be automatically closed
39382 sqlite3_mutex_leave(pInode->pLockMutex);
39384 assert( pFile->pShm==0 );
39394 ****************************** No-op Locking **********************************
39400 ** This locking mode is appropriate for use on read-only databases
39401 ** (ex: databases that are burned into CD-ROM, for example.) It can
39431 /******************* End of the no-op lock implementation *********************
39435 ************************* Begin dot-file Locking ******************************
39465 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39480 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
39481 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
39487 ** Lock the file with the lock specified by parameter eFileLock - one
39501 ** UNLOCKED -> SHARED
39502 ** SHARED -> RESERVED
39503 ** SHARED -> (PENDING) -> EXCLUSIVE
39504 ** RESERVED -> (PENDING) -> EXCLUSIVE
39505 ** PENDING -> EXCLUSIVE
39515 char *zLockFile = (char *)pFile->lockingContext;
39522 if( pFile->eFileLock > NO_LOCK ){
39523 pFile->eFileLock = eFileLock;
39550 pFile->eFileLock = eFileLock;
39559 ** the requested locking level, this routine is a no-op.
39565 char *zLockFile = (char *)pFile->lockingContext;
39569 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
39570 pFile->eFileLock, osGetpid(0)));
39573 /* no-op if possible */
39574 if( pFile->eFileLock==eFileLock ){
39582 pFile->eFileLock = SHARED_LOCK;
39599 pFile->eFileLock = NO_LOCK;
39610 sqlite3_free(pFile->lockingContext);
39613 /****************** End of the dot-file lock implementation *******************
39621 ** flock() locking is like dot-file locking in that the various
39622 ** fine-grain locking levels supported by SQLite are collapsed into
39649 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39662 if( pFile->eFileLock>SHARED_LOCK ){
39669 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
39672 lrc = robust_flock(pFile->h, LOCK_UN);
39691 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
39704 ** Lock the file with the lock specified by parameter eFileLock - one
39718 ** UNLOCKED -> SHARED
39719 ** SHARED -> RESERVED
39720 ** SHARED -> (PENDING) -> EXCLUSIVE
39721 ** RESERVED -> (PENDING) -> EXCLUSIVE
39722 ** PENDING -> EXCLUSIVE
39740 if (pFile->eFileLock > NO_LOCK) {
39741 pFile->eFileLock = eFileLock;
39747 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
39756 pFile->eFileLock = eFileLock;
39758 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
39774 ** the requested locking level, this routine is a no-op.
39780 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
39781 pFile->eFileLock, osGetpid(0)));
39784 /* no-op if possible */
39785 if( pFile->eFileLock==eFileLock ){
39791 pFile->eFileLock = eFileLock;
39796 if( robust_flock(pFile->h, LOCK_UN) ){
39802 pFile->eFileLock = NO_LOCK;
39826 ** Semaphore locking is like dot-lock and flock in that it really only
39836 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39849 if( pFile->eFileLock>SHARED_LOCK ){
39855 sem_t *pSem = pFile->pInode->pSem;
39857 if( sem_trywait(pSem)==-1 ){
39864 reserved = (pFile->eFileLock < SHARED_LOCK);
39871 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
39878 ** Lock the file with the lock specified by parameter eFileLock - one
39892 ** UNLOCKED -> SHARED
39893 ** SHARED -> RESERVED
39894 ** SHARED -> (PENDING) -> EXCLUSIVE
39895 ** RESERVED -> (PENDING) -> EXCLUSIVE
39896 ** PENDING -> EXCLUSIVE
39908 sem_t *pSem = pFile->pInode->pSem;
39913 if (pFile->eFileLock > NO_LOCK) {
39914 pFile->eFileLock = eFileLock;
39920 if( sem_trywait(pSem)==-1 ){
39926 pFile->eFileLock = eFileLock;
39937 ** the requested locking level, this routine is a no-op.
39941 sem_t *pSem = pFile->pInode->pSem;
39945 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
39946 pFile->eFileLock, osGetpid(0)));
39949 /* no-op if possible */
39950 if( pFile->eFileLock==eFileLock ){
39956 pFile->eFileLock = eFileLock;
39961 if ( sem_post(pSem)==-1 ) {
39969 pFile->eFileLock = NO_LOCK;
40002 ** on Apple Macintosh computers - both OS9 and OSX.
40004 ** Third-party implementations of AFP are available. But this code here
40031 ** This is a utility for setting or clearing a bit-range lock on an
40050 pb.fd = pFile->h;
40053 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
40056 if ( err==-1 ) {
40079 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40091 context = (afpLockingContext *) pFile->lockingContext;
40092 if( context->reserved ){
40096 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
40098 if( pFile->pInode->eFileLock>SHARED_LOCK ){
40106 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40110 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40120 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
40121 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
40128 ** Lock the file with the lock specified by parameter eFileLock - one
40142 ** UNLOCKED -> SHARED
40143 ** SHARED -> RESERVED
40144 ** SHARED -> (PENDING) -> EXCLUSIVE
40145 ** RESERVED -> (PENDING) -> EXCLUSIVE
40146 ** PENDING -> EXCLUSIVE
40154 unixInodeInfo *pInode = pFile->pInode;
40155 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40158 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
40159 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
40160 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
40166 if( pFile->eFileLock>=eFileLock ){
40167 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
40177 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
40179 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
40181 /* This mutex is needed because pFile->pInode is shared across threads
40183 pInode = pFile->pInode;
40184 sqlite3_mutex_enter(pInode->pLockMutex);
40189 if( (pFile->eFileLock!=pInode->eFileLock &&
40190 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
40201 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
40203 assert( pFile->eFileLock==0 );
40204 assert( pInode->nShared>0 );
40205 pFile->eFileLock = SHARED_LOCK;
40206 pInode->nShared++;
40207 pInode->nLock++;
40216 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
40219 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
40233 assert( pInode->nShared==0 );
40234 assert( pInode->eFileLock==0 );
40237 /* Now get the read-lock SHARED_LOCK */
40240 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
40241 lrc1 = afpSetLock(context->dbPath, pFile,
40242 SHARED_FIRST+pInode->sharedByte, 1, 1);
40244 lrc1Errno = pFile->lastErrno;
40247 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40259 pFile->eFileLock = SHARED_LOCK;
40260 pInode->nLock++;
40261 pInode->nShared = 1;
40263 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40273 assert( 0!=pFile->eFileLock );
40274 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
40276 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40278 context->reserved = 1;
40287 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
40288 pInode->sharedByte, 1, 0)) ){
40291 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
40293 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
40294 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
40312 pFile->eFileLock = eFileLock;
40313 pInode->eFileLock = eFileLock;
40315 pFile->eFileLock = PENDING_LOCK;
40316 pInode->eFileLock = PENDING_LOCK;
40320 sqlite3_mutex_leave(pInode->pLockMutex);
40321 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
40331 ** the requested locking level, this routine is a no-op.
40337 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40340 int h = pFile->h;
40344 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
40345 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40349 if( pFile->eFileLock<=eFileLock ){
40352 pInode = pFile->pInode;
40353 sqlite3_mutex_enter(pInode->pLockMutex);
40354 assert( pInode->nShared!=0 );
40355 if( pFile->eFileLock>SHARED_LOCK ){
40356 assert( pInode->eFileLock==pFile->eFileLock );
40358 SimulateIOError( h=(-1) )
40370 assert( pFile->inNormalWrite==0
40371 || pFile->dbUpdate==0
40372 || pFile->transCntrChng==1 );
40373 pFile->inNormalWrite = 0;
40376 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
40377 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
40378 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
40379 /* only re-establish the shared lock if necessary */
40380 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40381 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
40386 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
40387 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40389 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
40390 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40392 context->reserved = 0;
40395 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
40396 pInode->eFileLock = SHARED_LOCK;
40405 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40406 pInode->nShared--;
40407 if( pInode->nShared==0 ){
40409 SimulateIOError( h=(-1) )
40412 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
40415 pInode->eFileLock = NO_LOCK;
40416 pFile->eFileLock = NO_LOCK;
40420 pInode->nLock--;
40421 assert( pInode->nLock>=0 );
40422 if( pInode->nLock==0 ) closePendingFds(pFile);
40426 sqlite3_mutex_leave(pInode->pLockMutex);
40428 pFile->eFileLock = eFileLock;
40443 if( pFile->pInode ){
40444 unixInodeInfo *pInode = pFile->pInode;
40445 sqlite3_mutex_enter(pInode->pLockMutex);
40446 if( pInode->nLock ){
40449 ** descriptor to pInode->aPending. It will be automatically closed when
40454 sqlite3_mutex_leave(pInode->pLockMutex);
40457 sqlite3_free(pFile->lockingContext);
40467 ** is available. If you don't compile for a mac, then the "unix-afp"
40482 ** the requested locking level, this routine is a no-op.
40498 **************** Non-locking sqlite3_file methods *****************************
40522 assert( id->h>2 );
40525 got = osPread(id->h, pBuf, cnt, offset);
40526 SimulateIOError( got = -1 );
40528 got = osPread64(id->h, pBuf, cnt, offset);
40529 SimulateIOError( got = -1 );
40531 newOffset = lseek(id->h, offset, SEEK_SET);
40532 SimulateIOError( newOffset = -1 );
40535 return -1;
40537 got = osRead(id->h, pBuf, cnt);
40546 cnt -= got;
40553 OSTRACE(("READ %-3d %5d %7lld %llu\n",
40554 id->h, got+prior, offset-prior, TIMER_ELAPSED));
40575 /* If this is a database file (not a journal, super-journal or temp
40578 assert( pFile->pPreallocatedUnused==0
40587 if( offset<pFile->mmapSize ){
40588 if( offset+amt <= pFile->mmapSize ){
40589 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
40592 int nCopy = pFile->mmapSize - offset;
40593 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
40595 amt -= nCopy;
40605 /* pFile->lastErrno has been set by seekAndRead().
40612 switch( pFile->lastErrno ){
40626 /* Unread parts of the buffer must be zero-filled */
40627 memset(&((char*)pBuf)[got], 0, amt-got);
40633 ** Attempt to seek the file-descriptor passed as the first argument to
40635 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
40661 SimulateIOError( iSeek = -1 );
40663 rc = -1;
40671 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
40679 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
40686 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
40705 /* If this is a database file (not a journal, super-journal or temp
40708 assert( pFile->pPreallocatedUnused==0
40716 ** doing a hot-journal rollback or a write to some file other than a
40721 if( pFile->inNormalWrite ){
40722 pFile->dbUpdate = 1; /* The database has been modified */
40729 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
40730 pFile->transCntrChng = 1; /* The transaction counter has changed */
40739 if( offset<pFile->mmapSize ){
40740 if( offset+amt <= pFile->mmapSize ){
40741 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
40744 int nCopy = pFile->mmapSize - offset;
40745 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
40747 amt -= nCopy;
40754 amt -= wrote;
40758 SimulateIOError(( wrote=(-1), amt=1 ));
40762 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
40787 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
40857 ** no-op. But go ahead and call fstat() to validate the file
40890 if( rc==-1 && errno==ENOTSUP ){
40896 if( OS_VXWORKS && rc!= -1 ){
40909 ** The directory file descriptor is used for only one thing - to
40918 ** replace this routine with a harmless no-op. To make this routine
40919 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
40927 int fd = -1;
40931 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
40940 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
40955 ** has been created by fsync-ing the directory that contains the file.
40960 ** will not roll back - possibly leading to database corruption.
40980 OSTRACE(("SYNC %-3d\n", pFile->h));
40981 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
40985 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
40989 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
40992 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
40994 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
40996 rc = osOpenDirectory(pFile->zPath, &dirfd);
41004 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
41018 /* If the user has configured a chunk-size for this file, truncate the
41023 if( pFile->szChunk>0 ){
41024 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
41027 rc = robust_ftruncate(pFile->h, nByte);
41030 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41034 ** doing a hot-journal rollback or a write to some file other than a
41037 ** when restoring a database using the backup API from a zero-length
41040 if( pFile->inNormalWrite && nByte==0 ){
41041 pFile->transCntrChng = 1;
41050 if( nByte<pFile->mmapSize ){
41051 pFile->mmapSize = nByte;
41066 rc = osFstat(((unixFile*)id)->h, &buf);
41070 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", ((unixFile*)id)->zPath);
41074 /* When opening a zero-size database, the findInodeInfo() procedure
41076 ** in the OS-X msdos filesystem. In order to avoid problems with upper
41088 ** Handler for proxy-locking file-control verbs. Defined below in the
41096 ** file-control operation. Enlarge the database to nBytes in size
41097 ** (rounded up to the next chunk-size). If the database is already
41098 ** nBytes or larger, this routine is a no-op.
41101 if( pFile->szChunk>0 ){
41105 if( osFstat(pFile->h, &buf) ){
41106 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", pFile->zPath);
41109 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
41118 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
41125 ** at offset (nSize-1), to set the size of the file correctly.
41129 int nBlk = buf.st_blksize; /* File-system block size */
41133 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
41136 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
41137 if( iWrite>=nSize ) iWrite = nSize - 1;
41146 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
41148 if( pFile->szChunk<=0 ){
41149 if( robust_ftruncate(pFile->h, nByte) ){
41151 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
41165 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
41167 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
41171 *pArg = (pFile->ctrlFlags & mask)!=0;
41173 pFile->ctrlFlags &= ~mask;
41175 pFile->ctrlFlags |= mask;
41193 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
41197 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
41201 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
41207 *(int*)pArg = pFile->eFileLock;
41211 *(int*)pArg = pFile->lastErrno;
41215 pFile->szChunk = *(int *)pArg;
41234 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
41238 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
41240 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
41251 int iOld = pFile->iBusyTimeout;
41252 pFile->iBusyTimeout = *(int*)pArg;
41267 ** 64-bit type. */
41272 *(i64*)pArg = pFile->mmapSizeMax;
41273 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
41274 pFile->mmapSizeMax = newLimit;
41275 if( pFile->mmapSize>0 ){
41277 rc = unixMapfile(pFile, -1);
41290 ((unixFile*)id)->dbUpdate = 0;
41314 ** If pFd->sectorSize is non-zero when this function is called, it is a
41315 ** no-op. Otherwise, the values of pFd->sectorSize and
41316 ** pFd->deviceCharacteristics are set according to the file-system
41324 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
41325 if( pFd->sectorSize==0 ){
41331 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
41333 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
41338 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
41339 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
41342 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41349 if( pFile->sectorSize == 0 ){
41352 /* Set defaults for non-supported filesystems */
41353 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41354 pFile->deviceCharacteristics = 0;
41355 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
41360 pFile->sectorSize = fsInfo.f_bsize;
41361 pFile->deviceCharacteristics =
41369 pFile->sectorSize = fsInfo.f_bsize;
41370 pFile->deviceCharacteristics =
41372 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
41379 pFile->sectorSize = fsInfo.f_bsize;
41380 pFile->deviceCharacteristics =
41388 pFile->sectorSize = fsInfo.f_bsize;
41389 pFile->deviceCharacteristics =
41391 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41396 pFile->sectorSize = fsInfo.f_bsize;
41397 pFile->deviceCharacteristics =
41399 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41404 pFile->deviceCharacteristics =
41413 if( pFile->sectorSize % 512 != 0 ){
41414 pFile->deviceCharacteristics = 0;
41415 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41426 ** if two files are created in the same file-system directory (i.e.
41433 return pFd->sectorSize;
41442 ** words, after a power-loss event, parts of the file that were never
41443 ** written might end up being altered.) However, non-PSOW behavior is very,
41446 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
41452 return pFd->deviceCharacteristics;
41480 ** When multiple threads all reference the same wal-index, each thread
41482 ** of this unixShmNode object. In other words, each wal-index is opened
41497 ** The following fields are read-only after the object is created:
41511 int szRegion; /* Size of shared-memory regions */
41513 u8 isReadonly; /* True if read-only */
41515 char **apRegion; /* Array of mapped shared-memory regions */
41518 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
41531 ** are read-only thereafter:
41536 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
41542 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
41556 ** wal-mode transactions in other processes on database file pFile. If
41565 if( pFile->pShm){
41566 unixShmNode *pShmNode = pFile->pShm->pShmNode;
41573 f.l_len = SQLITE_SHM_NLOCK - 3;
41575 sqlite3_mutex_enter(pShmNode->pShmMutex);
41576 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
41581 sqlite3_mutex_leave(pShmNode->pShmMutex);
41589 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
41591 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
41600 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
41605 pShmNode = pFile->pInode->pShmNode;
41606 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
41607 assert( pShmNode->nRef>0 || unixMutexHeld() );
41615 if( pShmNode->hShm>=0 ){
41622 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
41623 if( res==-1 ){
41625 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
41635 OSTRACE(("SHM-LOCK "));
41636 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
41640 pShmNode->exclMask &= ~mask;
41641 pShmNode->sharedMask &= ~mask;
41643 OSTRACE(("read-lock %d ok", ofst));
41644 pShmNode->exclMask &= ~mask;
41645 pShmNode->sharedMask |= mask;
41648 OSTRACE(("write-lock %d ok", ofst));
41649 pShmNode->exclMask |= mask;
41650 pShmNode->sharedMask &= ~mask;
41656 OSTRACE(("read-lock failed"));
41659 OSTRACE(("write-lock %d failed", ofst));
41662 OSTRACE((" - afterwards %03x,%03x\n",
41663 pShmNode->sharedMask, pShmNode->exclMask));
41673 ** current system page-size.
41676 ** to use 64KB pages - in this case each mapping must cover at least two
41682 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
41690 ** This is not a VFS shared-memory method; it is a utility function called
41691 ** by VFS shared-memory methods.
41694 unixShmNode *p = pFd->pInode->pShmNode;
41696 if( p && ALWAYS(p->nRef==0) ){
41699 assert( p->pInode==pFd->pInode );
41700 sqlite3_mutex_free(p->pShmMutex);
41701 for(i=0; i<p->nRegion; i+=nShmPerMap){
41702 if( p->hShm>=0 ){
41703 osMunmap(p->apRegion[i], p->szRegion);
41705 sqlite3_free(p->apRegion[i]);
41708 sqlite3_free(p->apRegion);
41709 if( p->hShm>=0 ){
41710 robust_close(pFd, p->hShm, __LINE__);
41711 p->hShm = -1;
41713 p->pInode->pShmNode = 0;
41725 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
41734 ** and proceed with opening the *-shm file.
41738 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
41745 ** EXCLUSIVE failed just before truncating the *-shm file, then this
41746 ** process might open and use the *-shm file without truncating it.
41747 ** And if the *-shm file has been corrupted by a power failure or
41753 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
41756 if( pShmNode->isReadonly ){
41757 pShmNode->isUnlocked = 1;
41761 /* The first connection to attach must truncate the -shm file. We
41763 ** -shm header size) rather than 0 as a system debugging aid, to
41764 ** help detect if a -shm file truncation is legitimate or is the work
41766 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
41767 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
41782 ** Open a shared-memory area associated with open database file pDbFd.
41785 ** The file used to implement shared-memory is in the same directory
41787 ** file with the "-shm" suffix added. For example, if the database file
41789 ** for shared memory will be called "/home/user1/config.db-shm".
41795 ** database to end up using different files for shared memory -
41796 ** meaning that their memory would not really be shared - resulting
41798 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
41799 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
41803 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
41806 ** When opening a new shared-memory file, if no other instances of that
41810 ** If the original database file (pDbFd) is using the "unix-excl" VFS
41828 assert( pDbFd->pShm==0 );
41835 pInode = pDbFd->pInode;
41836 pShmNode = pInode->pShmNode;
41840 const char *zBasePath = pDbFd->zPath;
41844 ** a new *-shm file is created, an attempt will be made to create it
41847 if( osFstat(pDbFd->h, &sStat) ){
41848 rc = unixLogError(SQLITE_IOERR_FSTAT, "fstat", pDbFd->zPath);
41863 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
41866 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
41869 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
41870 sqlite3FileSuffix3(pDbFd->zPath, zShm);
41872 pShmNode->hShm = -1;
41873 pDbFd->pInode->pShmNode = pShmNode;
41874 pShmNode->pInode = pDbFd->pInode;
41876 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
41877 if( pShmNode->pShmMutex==0 ){
41883 if( pInode->bProcessLock==0 ){
41884 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
41885 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
41888 if( pShmNode->hShm<0 ){
41889 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
41891 if( pShmNode->hShm<0 ){
41895 pShmNode->isReadonly = 1;
41902 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
41910 p->pShmNode = pShmNode;
41912 p->id = pShmNode->nextShmId++;
41914 pShmNode->nRef++;
41915 pDbFd->pShm = p;
41922 ** at pShmNode->pFirst. This must be done while holding the
41923 ** pShmNode->pShmMutex.
41925 sqlite3_mutex_enter(pShmNode->pShmMutex);
41926 p->pNext = pShmNode->pFirst;
41927 pShmNode->pFirst = p;
41928 sqlite3_mutex_leave(pShmNode->pShmMutex);
41941 ** shared-memory associated with the database file fd. Shared-memory regions
41942 ** are numbered starting from zero. Each shared-memory region is szRegion
41947 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
41950 ** bExtend is non-zero and the requested shared-memory region has not yet
41953 ** If the shared-memory region has already been allocated or is allocated by
41972 /* If the shared-memory file has not yet been opened, open it now. */
41973 if( pDbFd->pShm==0 ){
41978 p = pDbFd->pShm;
41979 pShmNode = p->pShmNode;
41980 sqlite3_mutex_enter(pShmNode->pShmMutex);
41981 if( pShmNode->isUnlocked ){
41984 pShmNode->isUnlocked = 0;
41986 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
41987 assert( pShmNode->pInode==pDbFd->pInode );
41988 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
41989 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
41994 if( pShmNode->nRegion<nReqRegion ){
41999 pShmNode->szRegion = szRegion;
42001 if( pShmNode->hShm>=0 ){
42003 ** Check to see if it has been allocated (i.e. if the wal-index file is
42006 if( osFstat(pShmNode->hShm, &sStat) ){
42034 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
42035 const char *zFile = pShmNode->zFilename;
42046 pShmNode->apRegion, nReqRegion*sizeof(char *)
42052 pShmNode->apRegion = apNew;
42053 while( pShmNode->nRegion<nReqRegion ){
42057 if( pShmNode->hShm>=0 ){
42059 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
42060 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
42063 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
42076 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
42078 pShmNode->nRegion += nShmPerMap;
42083 if( pShmNode->nRegion>iRegion ){
42084 *pp = pShmNode->apRegion[iRegion];
42088 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
42089 sqlite3_mutex_leave(pShmNode->pShmMutex);
42094 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
42104 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42107 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
42110 if( pX->exclMask & (1<<i) ){
42112 aLock[i] = -1;
42113 }else if( pX->sharedMask & (1<<i) ){
42120 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
42121 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
42126 ** Change the lock state for a shared-memory segment.
42146 p = pDbFd->pShm;
42148 pShmNode = p->pShmNode;
42150 aLock = pShmNode->aLock;
42152 assert( pShmNode==pDbFd->pInode->pShmNode );
42153 assert( pShmNode->pInode==pDbFd->pInode );
42161 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
42162 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42178 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
42180 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
42181 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
42182 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
42186 mask = (1<<(ofst+n)) - (1<<ofst);
42188 sqlite3_mutex_enter(pShmNode->pShmMutex);
42191 if( (p->exclMask|p->sharedMask) & mask ){
42196 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
42206 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
42208 aLock[ofst]--;
42213 p->exclMask &= ~mask;
42214 p->sharedMask &= ~mask;
42219 assert( (p->exclMask & (1<<ofst))==0 );
42220 if( (p->sharedMask & mask)==0 ){
42229 p->sharedMask |= mask;
42238 assert( (p->sharedMask & mask)==0 );
42239 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
42246 ** also update the in-memory values. */
42250 assert( (p->sharedMask & mask)==0 );
42251 p->exclMask |= mask;
42253 aLock[ii] = -1;
42259 sqlite3_mutex_leave(pShmNode->pShmMutex);
42260 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
42261 p->id, osGetpid(0), p->sharedMask, p->exclMask));
42275 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42276 assert( fd->pMethods->xLock==nolockLock
42284 ** Close a connection to shared-memory. Delete the underlying
42288 ** routine is a harmless no-op.
42292 int deleteFlag /* Delete shared-memory if true */
42295 unixShmNode *pShmNode; /* The underlying shared-memory file */
42300 p = pDbFd->pShm;
42302 pShmNode = p->pShmNode;
42304 assert( pShmNode==pDbFd->pInode->pShmNode );
42305 assert( pShmNode->pInode==pDbFd->pInode );
42309 sqlite3_mutex_enter(pShmNode->pShmMutex);
42310 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
42311 *pp = p->pNext;
42315 pDbFd->pShm = 0;
42316 sqlite3_mutex_leave(pShmNode->pShmMutex);
42318 /* If pShmNode->nRef has reached 0, then close the underlying
42319 ** shared-memory file, too */
42322 assert( pShmNode->nRef>0 );
42323 pShmNode->nRef--;
42324 if( pShmNode->nRef==0 ){
42325 if( deleteFlag && pShmNode->hShm>=0 ){
42326 osUnlink(pShmNode->zFilename);
42348 assert( pFd->nFetchOut==0 );
42349 if( pFd->pMapRegion ){
42350 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
42351 pFd->pMapRegion = 0;
42352 pFd->mmapSize = 0;
42353 pFd->mmapSizeActual = 0;
42377 int h = pFd->h; /* File descriptor open on db file */
42378 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
42379 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
42383 assert( pFd->nFetchOut==0 );
42384 assert( nNew>pFd->mmapSize );
42385 assert( nNew<=pFd->mmapSizeMax );
42387 assert( pFd->mmapSizeActual>=pFd->mmapSize );
42391 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
42396 i64 nReuse = pFd->mmapSize;
42399 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
42405 osMunmap(pReq, nOrig-nReuse);
42412 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
42415 osMunmap(pNew, nNew - nReuse);
42437 unixLogError(SQLITE_OK, zErr, pFd->zPath);
42442 pFd->mmapSizeMax = 0;
42444 pFd->pMapRegion = (void *)pNew;
42445 pFd->mmapSize = pFd->mmapSizeActual = nNew;
42449 ** Memory map or remap the file opened by file-descriptor pFd (if the file
42452 ** outstanding xFetch() references to it, this function is a no-op.
42454 ** If parameter nByte is non-negative, then it is the requested size of
42465 assert( nMap>=0 || pFd->nFetchOut==0 );
42466 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42467 if( pFd->nFetchOut>0 ) return SQLITE_OK;
42470 struct stat statbuf; /* Low-level file information */
42471 if( osFstat(pFd->h, &statbuf) ){
42476 if( nMap>pFd->mmapSizeMax ){
42477 nMap = pFd->mmapSizeMax;
42480 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42481 if( nMap!=pFd->mmapSize ){
42508 if( pFd->mmapSizeMax>0 ){
42509 if( pFd->pMapRegion==0 ){
42510 int rc = unixMapfile(pFd, -1);
42513 if( pFd->mmapSize >= iOff+nAmt ){
42514 *pp = &((u8 *)pFd->pMapRegion)[iOff];
42515 pFd->nFetchOut++;
42523 ** If the third argument is non-NULL, then this function releases a
42540 assert( (p==0)==(pFd->nFetchOut==0) );
42543 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
42546 pFd->nFetchOut--;
42551 assert( pFd->nFetchOut>=0 );
42569 ** of "finder" functions. A finder-function is used to locate the appropriate
42572 ** the correct finder-function for that VFS.
42575 ** object. The only interesting finder-function is autolockIoFinder, which
42579 ** For finder-function F, two objects are created:
42581 ** (1) The real finder-function named "FImpt()".
42588 ** directly at the finder-function since C90 rules prevent a void*
42705 ** The proxy locking method is a "super-method" in the sense that it
42707 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
42776 if( statfs(filePath, &fsInfo) != -1 ){
42788 ** Test byte-range lock using fcntl(). If the call succeeds,
42789 ** assume that the file-system supports POSIX style locks.
42795 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42835 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42873 assert( pNew->pInode==NULL );
42878 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
42879 pNew->h = h;
42880 pNew->pVfs = pVfs;
42881 pNew->zPath = zFilename;
42882 pNew->ctrlFlags = (u8)ctrlFlags;
42884 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
42888 pNew->ctrlFlags |= UNIXFILE_PSOW;
42890 if( strcmp(pVfs->zName,"unix-excl")==0 ){
42891 pNew->ctrlFlags |= UNIXFILE_EXCL;
42895 pNew->pId = vxworksFindFileId(zFilename);
42896 if( pNew->pId==0 ){
42905 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
42910 pNew->lockingContext = (void*)zFilename;
42920 rc = findInodeInfo(pNew, &pNew->pInode);
42933 ** handle h - as it is guaranteed that no posix locks will be released
42941 h = -1;
42952 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
42959 pCtx->dbPath = zFilename;
42960 pCtx->reserved = 0;
42963 rc = findInodeInfo(pNew, &pNew->pInode);
42965 sqlite3_free(pNew->lockingContext);
42967 h = -1;
42988 pNew->lockingContext = zLockFile;
42997 rc = findInodeInfo(pNew, &pNew->pInode);
42998 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
42999 char *zSemName = pNew->pInode->aSemName;
43002 pNew->pId->zCanonicalName);
43005 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
43006 if( pNew->pInode->pSem == SEM_FAILED ){
43008 pNew->pInode->aSemName[0] = '\0';
43019 h = -1;
43021 pNew->ctrlFlags |= UNIXFILE_DELETE;
43027 pId->pMethods = pLockingStyle;
43080 ** pVfs->mxPathname bytes.
43087 /* It's odd to simulate an io-error here, but really this is just
43088 ** using the io-error infrastructure to test that SQLite handles this
43103 zBuf[nBuf-2] = 0;
43106 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
43118 ** Routine to transform a unixFile into a proxy-locking unixFile.
43119 ** Implementation in the proxy-lock division, but used by unixOpen()
43127 ** file (not a journal or super-journal file) identified by pathname
43133 ** other file descriptor open on the same file is holding a file-lock.
43139 ** such file descriptor is located, -1 is returned.
43157 ** ignored and -1 is returned. The caller will try to open a new file
43166 while( pInode && (pInode->fileId.dev!=sStat.st_dev
43167 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
43168 pInode = pInode->pNext;
43172 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
43173 sqlite3_mutex_enter(pInode->pLockMutex);
43175 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
43178 *pp = pUnused->pNext;
43180 sqlite3_mutex_leave(pInode->pLockMutex);
43220 ** this function queries the file-system for the permissions on the
43249 ** "<path to db>-journal"
43250 ** "<path to db>-wal"
43251 ** "<path to db>-journalNN"
43252 ** "<path to db>-walNN"
43258 ** a '-' character. However in 8+3 filename mode, or if a corrupt
43259 ** rollback journal specifies a super-journal with a goofy name, then
43260 ** the '-' might be missing or the '-' might be the first character in
43263 nDb = sqlite3Strlen30(zPath) - 1;
43265 if( zPath[nDb]=='-' ){
43271 nDb--;
43300 ** ReadWrite() -> (READWRITE | CREATE)
43301 ** ReadOnly() -> (READONLY)
43302 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
43304 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
43318 int fd = -1; /* File descriptor returned by open() */
43337 /* If creating a super- or main-file journal, this function will open
43338 ** a file-descriptor on the directory too. The first time unixSync()
43365 /* The main DB, main journal, WAL file and super-journal are never
43372 /* Assert that the upper layer has set one of the "file-type" flags. */
43399 fd = pUnused->fd;
43406 p->pPreallocatedUnused = pUnused;
43408 /* Database filenames are double-zero terminated if they are not
43416 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
43422 /* Generated temporary filenames are always double-zero terminated
43443 assert( !p->pPreallocatedUnused );
43448 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
43456 /* Failed to open the file for read/write access. Try read-only. */
43473 ** the case. The chown() system call will be a no-op if the current
43492 if( p->pPreallocatedUnused ){
43493 p->pPreallocatedUnused->fd = fd;
43494 p->pPreallocatedUnused->flags =
43513 p->openFlags = openFlags;
43518 if( fstatfs(fd, &fsInfo) == -1 ){
43524 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43527 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43543 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
43548 ** never use proxy, NULL means use proxy for non-local files only. */
43561 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
43579 sqlite3_free(p->pPreallocatedUnused);
43597 if( osUnlink(zPath)==(-1) ){
43666 int rc; /* Non-zero following any error */
43681 const char *zName, /* Name to append to pPath. Not zero-terminated */
43689 if( pPath->nUsed>1 ){
43690 assert( pPath->zOut[0]=='/' );
43691 while( pPath->zOut[--pPath->nUsed]!='/' ){}
43696 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
43697 pPath->rc = SQLITE_ERROR;
43700 pPath->zOut[pPath->nUsed++] = '/';
43701 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
43702 pPath->nUsed += nName;
43704 if( pPath->rc==SQLITE_OK ){
43707 pPath->zOut[pPath->nUsed] = 0;
43708 zIn = pPath->zOut;
43711 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
43716 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
43717 pPath->rc = SQLITE_CANTOPEN_BKPT;
43720 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
43721 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
43722 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
43727 pPath->nUsed = 0;
43729 pPath->nUsed -= nName + 1;
43742 const char *zPath /* Path to append to pPath. Is zero-terminated */
43749 appendOnePathElement(pPath, &zPath[j], i-j);
43757 ** is stored as a nul-terminated string in the buffer pointed to by
43761 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
43779 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
43821 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
43824 ** use dlsym() with -pedantic-errors?
43832 ** This work-around is unlikely to work correctly on any system where
43861 ** errors. The reports issued by valgrind are incorrect - we would
43863 ** uninitialized space in zBuf - but valgrind errors tend to worry
43926 ** The following variable, if set to a non-zero value, is interpreted as
43990 ** low-level error message when operating-system problems come up
44009 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
44011 ** meta-layer over top of the primitive locking implemented above. For
44014 ** been defined - so that the primitive locking methods are available
44019 ** The default locking schemes in SQLite use byte-range locks on the
44024 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
44032 ** SHARED_RANGE 0x40000002 -> 0x40000200
44036 ** the read cache when byte-range locks are present. Enabling the read
44039 ** close-to-open semantics for ensuring cache coherency
44043 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
44052 ** -----------------
44084 ** -----------------------
44094 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
44095 ** by taking an sqlite-style shared lock on the conch file, reading the
44099 ** is patterned after the database file name as ".<databasename>-conch".
44107 ** The proxy file - a single-byte file used for all advisory file locks
44123 ** ---------------------
44127 ** Database files accessed on non-local file systems are
44172 int conchHeld; /* 1 if the conch is held, -1 if lockless */
44205 if( lPath[len-1]!='/' ){
44234 if( lockPath[i] == '/' && (i - start > 0) ){
44236 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
44237 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
44269 int fd = -1;
44278 ** 2. if that fails, and this is a lock file (not-conch), try creating
44280 ** 3. if that fails, try to open the file read-only
44285 fd = pUnused->fd;
44326 pNew->openFlags = openFlags;
44330 pUnused->fd = fd;
44331 pUnused->flags = openFlags;
44332 pNew->pPreallocatedUnused = pUnused;
44390 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
44402 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44403 unixFile *conchFile = pCtx->conchFile;
44406 char *cPath = pCtx->conchFilePath;
44410 int fd = -1;
44411 int rc = -1;
44414 /* create a new path by replace the trailing '-conch' with '-break' */
44417 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
44422 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
44443 robust_close(pFile, conchFile->h, __LINE__);
44444 conchFile->h = fd;
44445 conchFile->openFlags = O_RDWR | O_CREAT;
44462 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44463 unixFile *conchFile = pCtx->conchFile;
44470 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44480 if( osFstat(conchFile->h, &buf) ){
44499 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
44521 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
44524 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44534 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
44540 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44542 if( pCtx->conchHeld!=0 ){
44545 unixFile *conchFile = pCtx->conchFile;
44558 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
44559 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44575 storeLastErrno(pFile, conchFile->lastErrno);
44587 ** retry with a new auto-generated path
44595 if( !pCtx->lockProxyPath ){
44596 /* for auto-named local lock file, just check the host ID and we'll
44600 size_t pathLen = (readLen - PROXY_PATHINDEX);
44603 pathLen=MAXPATHLEN-1;
44613 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
44614 readLen-PROXY_PATHINDEX)
44622 if( (conchFile->openFlags&O_RDWR) == 0 ){
44628 if( !pCtx->lockProxyPath ){
44629 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
44638 futimes(conchFile->h, NULL);
44640 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
44656 if( pCtx->lockProxyPath!=NULL ){
44657 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
44663 robust_ftruncate(conchFile->h, writeSize);
44665 full_fsync(conchFile->h,0,0);
44671 int err = osFstat(pFile->h, &buf);
44677 osFchmod(conchFile->h, cmode);
44680 rc = osFchmod(conchFile->h, cmode);
44681 }while( rc==(-1) && errno==EINTR );
44697 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
44700 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
44701 if( rc==SQLITE_OK && pFile->openFlags ){
44703 if( pFile->h>=0 ){
44704 robust_close(pFile, pFile->h, __LINE__);
44706 pFile->h = -1;
44707 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
44710 pFile->h = fd;
44716 if( rc==SQLITE_OK && !pCtx->lockProxy ){
44717 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
44718 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
44721 ** so try again via auto-naming
44733 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
44734 if( !pCtx->lockProxyPath ){
44740 pCtx->conchHeld = 1;
44742 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
44744 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
44745 afpCtx->dbPath = pCtx->lockProxyPath;
44748 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44750 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
44753 } while (1); /* in case we need to retry the :auto: lock file -
44766 pCtx = (proxyLockingContext *)pFile->lockingContext;
44767 conchFile = pCtx->conchFile;
44768 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
44769 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44771 if( pCtx->conchHeld>0 ){
44772 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44774 pCtx->conchHeld = 0;
44775 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
44793 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
44805 for( i=(len-1); i>=0; i-- ){
44817 /* append the "-conch" suffix to the file */
44818 memcpy(&conchPath[i+1], "-conch", 7);
44825 /* Takes a fully configured proxy locking-style unix file and switches
44829 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44830 char *oldPath = pCtx->lockProxyPath;
44833 if( pFile->eFileLock!=NO_LOCK ){
44842 unixFile *lockProxy = pCtx->lockProxy;
44843 pCtx->lockProxy=NULL;
44844 pCtx->conchHeld = 0;
44846 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
44851 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
44866 if( pFile->pMethod == &afpIoMethods ){
44869 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44870 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
44874 if( pFile->pMethod == &dotlockIoMethods ){
44877 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
44878 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
44881 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44882 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
44892 ** ->lockingContext
44893 ** ->pMethod
44901 if( pFile->eFileLock!=NO_LOCK ){
44911 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
44920 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
44922 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
44923 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
44925 ** (c) the file system is read-only, then enable no-locking access.
44933 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
44935 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
44940 pCtx->conchHeld = -1; /* read only FS/ lockless */
44946 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
44950 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
44951 if( pCtx->dbPath==NULL ){
44959 pCtx->oldLockingContext = pFile->lockingContext;
44960 pFile->lockingContext = pCtx;
44961 pCtx->pOldMethod = pFile->pMethod;
44962 pFile->pMethod = &proxyIoMethods;
44964 if( pCtx->conchFile ){
44965 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
44966 sqlite3_free(pCtx->conchFile);
44968 sqlite3DbFree(0, pCtx->lockProxyPath);
44969 sqlite3_free(pCtx->conchFilePath);
44972 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
44986 if( pFile->pMethod == &proxyIoMethods ){
44987 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44989 if( pCtx->lockProxyPath ){
44990 *(const char **)pArg = pCtx->lockProxyPath;
45002 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
45005 /* turn off proxy locking - not supported. If support is added for
45011 /* turn off proxy locking - already off - NOOP */
45018 (proxyLockingContext*)pFile->lockingContext;
45020 || (pCtx->lockProxyPath &&
45021 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
45044 ** above this point are all utilities. The lock-related methods of the
45045 ** proxy-locking sqlite3_io_method object follow.
45052 ** to a non-zero value otherwise *pResOut is set to zero. The return value
45059 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45060 if( pCtx->conchHeld>0 ){
45061 unixFile *proxy = pCtx->lockProxy;
45062 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
45071 ** Lock the file with the lock specified by parameter eFileLock - one
45085 ** UNLOCKED -> SHARED
45086 ** SHARED -> RESERVED
45087 ** SHARED -> (PENDING) -> EXCLUSIVE
45088 ** RESERVED -> (PENDING) -> EXCLUSIVE
45089 ** PENDING -> EXCLUSIVE
45098 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45099 if( pCtx->conchHeld>0 ){
45100 unixFile *proxy = pCtx->lockProxy;
45101 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
45102 pFile->eFileLock = proxy->eFileLock;
45116 ** the requested locking level, this routine is a no-op.
45122 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45123 if( pCtx->conchHeld>0 ){
45124 unixFile *proxy = pCtx->lockProxy;
45125 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
45126 pFile->eFileLock = proxy->eFileLock;
45140 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45141 unixFile *lockProxy = pCtx->lockProxy;
45142 unixFile *conchFile = pCtx->conchFile;
45146 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
45148 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
45151 pCtx->lockProxy = 0;
45154 if( pCtx->conchHeld ){
45158 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
45162 sqlite3DbFree(0, pCtx->lockProxyPath);
45163 sqlite3_free(pCtx->conchFilePath);
45164 sqlite3DbFree(0, pCtx->dbPath);
45166 pFile->lockingContext = pCtx->oldLockingContext;
45167 pFile->pMethod = pCtx->pOldMethod;
45169 return pFile->pMethod->xClose(id);
45189 ** This routine registers all VFS implementations for unix-like operating
45206 ** when compiling with -pedantic-errors on GCC.)
45209 ** finder-function. The finder-function returns a pointer to the
45212 ** macro for addition information on finder-functions.
45260 UNIXVFS("unix-none", nolockIoFinder ),
45261 UNIXVFS("unix-dotfile", dotlockIoFinder ),
45262 UNIXVFS("unix-excl", posixIoFinder ),
45264 UNIXVFS("unix-namedsem", semIoFinder ),
45267 UNIXVFS("unix-posix", posixIoFinder ),
45270 UNIXVFS("unix-flock", flockIoFinder ),
45273 UNIXVFS("unix-afp", afpIoFinder ),
45274 UNIXVFS("unix-nfs", nfsIoFinder ),
45275 UNIXVFS("unix-proxy", proxyIoFinder ),
45280 /* Double-check that the aSyscall[] array has been constructed
45306 ** READ-0 UNIX_SHM_BASE+3 123
45307 ** READ-1 UNIX_SHM_BASE+4 124
45308 ** READ-2 UNIX_SHM_BASE+5 125
45309 ** READ-3 UNIX_SHM_BASE+6 126
45310 ** READ-4 UNIX_SHM_BASE+7 127
45313 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
45327 ** This routine is a no-op for unix.
45381 ** based on the sub-platform)?
45389 ** based on the sub-platform)?
45477 ** characters, so we allocate 4 bytes per character assuming worst-case of
45478 ** 4-bytes-per-character for UTF8.
45501 ** Returns non-zero if the character should be treated as a directory
45565 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
45578 typedef struct winShm winShm; /* A connection to shared-memory */
45579 typedef struct winShmNode winShmNode; /* A region of shared-memory */
45637 BOOL bNoLock; /* Non-zero if locking is disabled. */
45651 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
45661 * If this is non-zero, an isolated heap will be created by the native Win32
45667 * WARNING: It is important to note that when this setting is non-zero and the
45679 * This is the maximum possible initial size of the Win32-specific heap, in
45687 * This is the extra space for the initial size of the Win32-specific heap,
45700 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
45707 * Win32-specific heap. It cannot be negative.
45713 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
45719 * initial size of the Win32-specific heap to exceed the maximum amount
45728 * The initial size of the Win32-specific heap. This value may be zero.
45737 * The maximum size of the Win32-specific heap. This value may be zero.
45753 ** The winMemData structure stores information required by the Win32-specific
45837 ** Many system calls are accessed through pointer-to-functions so that
46454 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
46566 int i = -1;
46570 for(i=0; i<ArraySize(aSyscall)-1; i++){
46585 ** "pnLargest" argument, if non-zero, will be used to return the size of the
46675 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
46676 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
46677 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
46743 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
46771 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
46796 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
46891 if( n==(SIZE_T)-1 ){
46913 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46914 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46917 if( !pWinMemData->hHeap ){
46925 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
46927 if( !pWinMemData->hHeap ){
46934 pWinMemData->bOwned = TRUE;
46935 assert( pWinMemData->bOwned );
46938 pWinMemData->hHeap = osGetProcessHeap();
46939 if( !pWinMemData->hHeap ){
46944 pWinMemData->bOwned = FALSE;
46945 assert( !pWinMemData->bOwned );
46947 assert( pWinMemData->hHeap!=0 );
46948 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46950 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46962 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46963 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46965 if( pWinMemData->hHeap ){
46966 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46968 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46970 if( pWinMemData->bOwned ){
46971 if( !osHeapDestroy(pWinMemData->hHeap) ){
46973 osGetLastError(), (void*)pWinMemData->hHeap);
46975 pWinMemData->bOwned = FALSE;
46977 pWinMemData->hHeap = NULL;
46982 ** Populate the low-level memory allocation function pointers in
47009 ** Convert a UTF-8 string to Microsoft Unicode.
47017 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
47025 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
47035 ** Convert a Microsoft Unicode string to UTF-8.
47043 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
47051 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
47071 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
47080 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
47090 ** Convert a Microsoft Unicode string to a multi-byte character string,
47100 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
47108 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
47118 ** Convert a multi-byte character string to UTF-8.
47136 ** Convert a UTF-8 string to a multi-byte character string.
47251 ** it accepts a UTF-8 string.
47295 ** it accepts a UTF-16 string.
47328 ** is zero if the error message fits in the buffer, or non-zero
47408 ** This function - winLogErrorAtLine() - is only ever called via the macro
47413 ** error code and, if possible, the human-readable equivalent from
47419 ** failed and the associated file-system path, if any.
47439 "os_win.c:%d: (%lu) %s(%s) - %s",
47448 ** will be retried following a locking error - probably caused by
47465 ** non-zero if the error code is transient in nature and the operation
47553 y.tm_year = pTm.wYear - 1900;
47554 y.tm_mon = pTm.wMonth - 1;
47568 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
47602 memset(&pFile->local, 0, sizeof(pFile->local));
47612 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
47613 if (!pFile->hMutex){
47614 pFile->lastErrno = osGetLastError();
47616 return winLogError(SQLITE_IOERR, pFile->lastErrno,
47621 winceMutexAcquire(pFile->hMutex);
47624 ** case-sensitive, take advantage of that by uppercasing the mutex name
47628 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
47633 ** must be zero-initialized */
47642 if( pFile->hShared ){
47643 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
47646 if( !pFile->shared ){
47647 pFile->lastErrno = osGetLastError();
47648 winLogError(SQLITE_IOERR, pFile->lastErrno,
47651 osCloseHandle(pFile->hShared);
47652 pFile->hShared = NULL;
47657 if( pFile->hShared==NULL ){
47659 pFile->lastErrno = lastErrno;
47660 winLogError(SQLITE_IOERR, pFile->lastErrno,
47664 winceMutexRelease(pFile->hMutex);
47665 osCloseHandle(pFile->hMutex);
47666 pFile->hMutex = NULL;
47672 memset(pFile->shared, 0, sizeof(winceLock));
47675 winceMutexRelease(pFile->hMutex);
47683 if (pFile->hMutex){
47685 winceMutexAcquire(pFile->hMutex);
47689 if (pFile->local.nReaders){
47690 pFile->shared->nReaders --;
47692 if (pFile->local.bReserved){
47693 pFile->shared->bReserved = FALSE;
47695 if (pFile->local.bPending){
47696 pFile->shared->bPending = FALSE;
47698 if (pFile->local.bExclusive){
47699 pFile->shared->bExclusive = FALSE;
47702 /* De-reference and close our copy of the shared memory handle */
47703 osUnmapViewOfFile(pFile->shared);
47704 osCloseHandle(pFile->hShared);
47707 winceMutexRelease(pFile->hMutex);
47708 osCloseHandle(pFile->hMutex);
47709 pFile->hMutex = NULL;
47729 if (!pFile->hMutex) return TRUE;
47730 winceMutexAcquire(pFile->hMutex);
47735 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
47736 pFile->shared->bExclusive = TRUE;
47737 pFile->local.bExclusive = TRUE;
47742 /* Want a read-only lock? */
47745 if (pFile->shared->bExclusive == 0){
47746 pFile->local.nReaders ++;
47747 if (pFile->local.nReaders == 1){
47748 pFile->shared->nReaders ++;
47758 if (pFile->shared->bPending == 0) {
47759 pFile->shared->bPending = TRUE;
47760 pFile->local.bPending = TRUE;
47768 if (pFile->shared->bReserved == 0) {
47769 pFile->shared->bReserved = TRUE;
47770 pFile->local.bReserved = TRUE;
47775 winceMutexRelease(pFile->hMutex);
47795 if (!pFile->hMutex) return TRUE;
47796 winceMutexAcquire(pFile->hMutex);
47801 if (pFile->local.bExclusive){
47803 pFile->local.bExclusive = FALSE;
47804 pFile->shared->bExclusive = FALSE;
47809 else if (pFile->local.nReaders){
47812 pFile->local.nReaders --;
47813 if (pFile->local.nReaders == 0)
47815 pFile->shared->nReaders --;
47824 if (pFile->local.bPending){
47825 pFile->local.bPending = FALSE;
47826 pFile->shared->bPending = FALSE;
47833 if (pFile->local.bReserved) {
47834 pFile->local.bReserved = FALSE;
47835 pFile->shared->bReserved = FALSE;
47840 winceMutexRelease(pFile->hMutex);
47920 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
47926 ** Otherwise, set pFile->lastErrno and return non-zero.
47935 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
47941 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
47947 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
47951 pFile->lastErrno = lastErrno;
47952 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47953 "winSeekFile", pFile->zPath);
47954 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47958 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
47969 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
47972 pFile->lastErrno = osGetLastError();
47973 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47974 "winSeekFile", pFile->zPath);
47975 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47979 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
48007 assert( pFile->pShm==0 );
48009 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
48011 osGetCurrentProcessId(), pFile, pFile->h));
48018 rc = osCloseHandle(pFile->h);
48024 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
48025 if( pAppData==NULL || !pAppData->bNoLock ){
48029 if( pFile->zDeleteOnClose ){
48032 osDeleteFileW(pFile->zDeleteOnClose)==0
48033 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
48038 sqlite3_free(pFile->zDeleteOnClose);
48042 pFile->h = NULL;
48044 OpenCounter(-1);
48046 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
48049 "winClose", pFile->zPath);
48076 pFile->h, pBuf, amt, offset, pFile->locktype));
48081 if( offset<pFile->mmapSize ){
48082 if( offset+amt <= pFile->mmapSize ){
48083 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
48084 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48085 osGetCurrentProcessId(), pFile, pFile->h));
48088 int nCopy = (int)(pFile->mmapSize - offset);
48089 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
48091 amt -= nCopy;
48100 osGetCurrentProcessId(), pFile, pFile->h));
48103 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
48108 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
48113 pFile->lastErrno = lastErrno;
48115 osGetCurrentProcessId(), pFile, pFile->h));
48116 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
48117 "winRead", pFile->zPath);
48121 /* Unread parts of the buffer must be zero-filled */
48122 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
48124 osGetCurrentProcessId(), pFile, pFile->h));
48129 osGetCurrentProcessId(), pFile, pFile->h));
48154 pFile->h, pBuf, amt, offset, pFile->locktype));
48159 if( offset<pFile->mmapSize ){
48160 if( offset+amt <= pFile->mmapSize ){
48161 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
48162 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48163 osGetCurrentProcessId(), pFile, pFile->h));
48166 int nCopy = (int)(pFile->mmapSize - offset);
48167 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
48169 amt -= nCopy;
48197 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
48199 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
48215 nRem -= nWrite;
48218 pFile->lastErrno = lastErrno;
48224 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
48225 || ( pFile->lastErrno==ERROR_DISK_FULL )){
48227 osGetCurrentProcessId(), pFile, pFile->h));
48228 return winLogError(SQLITE_FULL, pFile->lastErrno,
48229 "winWrite1", pFile->zPath);
48232 osGetCurrentProcessId(), pFile, pFile->h));
48233 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
48234 "winWrite2", pFile->zPath);
48239 osGetCurrentProcessId(), pFile, pFile->h));
48252 if( pFile->nFetchOut>0 ){
48253 /* File truncation is a no-op if there are outstanding memory mapped
48259 ** No real harm comes of this - the database file is not corrupted,
48263 ** The only feasible work-around is to defer the truncation until after
48264 ** all references to memory-mapped content are closed. That is doable,
48267 ** now to simply make trancations a no-op if there are pending reads. We
48277 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
48279 /* If the user has configured a chunk-size for this file, truncate the
48284 if( pFile->szChunk>0 ){
48285 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
48289 if( pFile->pMapRegion ){
48290 oldMmapSize = pFile->mmapSize;
48297 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
48299 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48300 "winTruncate1", pFile->zPath);
48301 }else if( 0==osSetEndOfFile(pFile->h) &&
48303 pFile->lastErrno = lastErrno;
48304 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48305 "winTruncate2", pFile->zPath);
48311 winMapfile(pFile, -1);
48319 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
48365 osGetCurrentProcessId(), pFile, pFile->h, flags,
48366 pFile->locktype));
48378 ** no-op
48381 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48382 osGetCurrentProcessId(), pFile, pFile->h));
48386 if( pFile->pMapRegion ){
48387 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
48388 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48390 pFile, pFile->pMapRegion));
48392 pFile->lastErrno = osGetLastError();
48393 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48395 pFile, pFile->pMapRegion));
48396 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
48397 "winSync1", pFile->zPath);
48401 rc = osFlushFileBuffers(pFile->h);
48405 osGetCurrentProcessId(), pFile, pFile->h));
48408 pFile->lastErrno = osGetLastError();
48410 osGetCurrentProcessId(), pFile, pFile->h));
48411 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
48412 "winSync2", pFile->zPath);
48427 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
48432 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
48436 pFile->lastErrno = osGetLastError();
48437 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48438 "winFileSize", pFile->zPath);
48447 lowerBits = osGetFileSize(pFile->h, &upperBits);
48451 pFile->lastErrno = lastErrno;
48452 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48453 "winFileSize", pFile->zPath);
48458 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
48500 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48507 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
48509 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
48517 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
48518 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48519 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48523 pFile->lastErrno = osGetLastError();
48526 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
48536 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48538 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48542 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48546 pFile->lastErrno = lastErrno;
48547 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
48548 "winUnlockReadLock", pFile->zPath);
48550 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
48555 ** Lock the file with the lock specified by parameter locktype - one
48569 ** UNLOCKED -> SHARED
48570 ** SHARED -> RESERVED
48571 ** SHARED -> (PENDING) -> EXCLUSIVE
48572 ** RESERVED -> (PENDING) -> EXCLUSIVE
48573 ** PENDING -> EXCLUSIVE
48583 int newLocktype; /* Set pFile->locktype to this value before exiting */
48590 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48596 if( pFile->locktype>=locktype ){
48597 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
48601 /* Do not allow any kind of write-lock on a read-only database
48603 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
48609 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
48611 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
48617 newLocktype = pFile->locktype;
48618 if( pFile->locktype==NO_LOCK
48619 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
48622 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48625 ** around problems caused by indexing and/or anti-virus software on
48631 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
48632 pFile->h, cnt, res));
48634 pFile->lastErrno = lastErrno;
48636 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
48637 pFile->h, cnt, sqlite3ErrName(rc)));
48651 assert( pFile->locktype==NO_LOCK );
48663 assert( pFile->locktype==SHARED_LOCK );
48664 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
48682 assert( pFile->locktype>=SHARED_LOCK );
48684 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
48698 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48707 pFile->lastErrno = lastErrno;
48709 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
48710 pFile->h, locktype, newLocktype));
48712 pFile->locktype = (u8)newLocktype;
48714 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48721 ** non-zero, otherwise zero.
48728 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
48731 if( pFile->locktype>=RESERVED_LOCK ){
48733 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
48735 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
48737 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48740 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
48743 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
48744 pFile->h, pResOut, *pResOut));
48753 ** the requested locking level, this routine is a no-op.
48766 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48767 type = pFile->locktype;
48769 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48774 "winUnlock", pFile->zPath);
48778 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48784 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48786 pFile->locktype = (u8)locktype;
48788 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48793 ****************************** No-op Locking **********************************
48799 ** This locking mode is appropriate for use on read-only databases
48800 ** (ex: databases that are burned into CD-ROM, for example.) It can
48827 /******************* End of the no-op lock implementation *********************
48832 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
48834 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
48838 *pArg = (pFile->ctrlFlags & mask)!=0;
48840 pFile->ctrlFlags &= ~mask;
48842 pFile->ctrlFlags |= mask;
48857 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
48860 *(int*)pArg = pFile->locktype;
48861 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48865 *(int*)pArg = (int)pFile->lastErrno;
48866 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48870 pFile->szChunk = *(int *)pArg;
48871 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48875 if( pFile->szChunk>0 ){
48886 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48889 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48894 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48899 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48903 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
48904 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48919 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48924 *phFile = pFile->h;
48925 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48931 HANDLE hOldFile = pFile->h;
48932 pFile->h = *phFile;
48935 hOldFile, pFile->h));
48941 int rc = winGetTempname(pFile->pVfs, &zTFile);
48945 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48958 ** least a 64-bit type. */
48963 *(i64*)pArg = pFile->mmapSizeMax;
48964 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
48965 pFile->mmapSizeMax = newLimit;
48966 if( pFile->mmapSize>0 ){
48968 rc = winMapfile(pFile, -1);
48971 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48976 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
48986 ** if two files are created in the same file-system directory (i.e.
49001 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
49043 ** log-summary, each thread has its own winFile object, but they all
49045 ** log-summary is opened only once per process.
49053 ** The following fields are read-only after the object is created:
49068 int szRegion; /* Size of shared-memory regions */
49070 u8 isReadonly; /* True if read-only */
49099 ** are read-only thereafter:
49104 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
49131 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
49139 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
49141 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
49142 pFile->hFile.h, lockType, ofst, nByte));
49144 /* Release/Acquire the system-level lock */
49146 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
49151 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
49157 pFile->lastErrno = osGetLastError();
49161 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
49162 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
49163 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
49175 ** This is not a VFS shared-memory method; it is a utility function called
49176 ** by VFS shared-memory methods.
49182 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
49186 if( p->nRef==0 ){
49188 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
49189 for(i=0; i<p->nRegion; i++){
49190 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
49191 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
49194 bRc = osCloseHandle(p->aRegion[i].hMap);
49195 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
49199 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
49201 winClose((sqlite3_file *)&p->hFile);
49207 winDelete(pVfs, p->zFilename, 0);
49211 *pp = p->pNext;
49212 sqlite3_free(p->aRegion);
49215 pp = &p->pNext;
49227 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
49233 if( pShmNode->isReadonly ){
49234 pShmNode->isUnlocked = 1;
49237 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
49240 "winLockSharedMemory", pShmNode->zFilename);
49252 ** Open the shared-memory area associated with database file pDbFd.
49254 ** When opening a new shared-memory file, if no other instances of that
49265 assert( pDbFd->pShm==0 ); /* Not previously opened */
49272 nName = sqlite3Strlen30(pDbFd->zPath);
49278 pNew->zFilename = (char*)&pNew[1];
49279 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
49280 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
49286 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
49290 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
49300 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
49301 pShmNode->pNext = winShmNodeList;
49305 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
49306 if( pShmNode->mutex==0 ){
49312 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
49317 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
49318 (sqlite3_file*)&pShmNode->hFile,
49322 pShmNode->zFilename);
49325 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
49332 p->pShmNode = pShmNode;
49334 p->id = pShmNode->nextShmId++;
49336 pShmNode->nRef++;
49337 pDbFd->pShm = p;
49344 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
49347 sqlite3_mutex_enter(pShmNode->mutex);
49348 p->pNext = pShmNode->pFirst;
49349 pShmNode->pFirst = p;
49350 sqlite3_mutex_leave(pShmNode->mutex);
49356 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
49364 ** Close a connection to shared-memory. Delete the underlying
49371 winFile *pDbFd; /* Database holding shared-memory */
49373 winShmNode *pShmNode; /* The underlying shared-memory file */
49377 p = pDbFd->pShm;
49379 pShmNode = p->pShmNode;
49383 sqlite3_mutex_enter(pShmNode->mutex);
49384 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
49385 *pp = p->pNext;
49389 pDbFd->pShm = 0;
49390 sqlite3_mutex_leave(pShmNode->mutex);
49392 /* If pShmNode->nRef has reached 0, then close the underlying
49393 ** shared-memory file, too */
49395 assert( pShmNode->nRef>0 );
49396 pShmNode->nRef--;
49397 if( pShmNode->nRef==0 ){
49398 winShmPurge(pDbFd->pVfs, deleteFlag);
49406 ** Change the lock state for a shared-memory segment.
49415 winShm *p = pDbFd->pShm; /* The shared memory being locked */
49422 pShmNode = p->pShmNode;
49433 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
49435 sqlite3_mutex_enter(pShmNode->mutex);
49440 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49442 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
49443 allMask |= pX->sharedMask;
49446 /* Unlock the system-level locks */
49455 p->exclMask &= ~mask;
49456 p->sharedMask &= ~mask;
49465 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49466 if( (pX->exclMask & mask)!=0 ){
49470 allShared |= pX->sharedMask;
49484 p->sharedMask |= mask;
49490 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49491 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
49503 assert( (p->sharedMask & mask)==0 );
49504 p->exclMask |= mask;
49508 sqlite3_mutex_leave(pShmNode->mutex);
49509 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
49510 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
49525 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
49532 ** shared-memory associated with the database file fd. Shared-memory regions
49533 ** are numbered starting from zero. Each shared-memory region is szRegion
49538 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
49541 ** isWrite is non-zero and the requested shared-memory region has not yet
49544 ** If the shared-memory region has already been allocated or is allocated by
49557 winShm *pShm = pDbFd->pShm;
49566 pShm = pDbFd->pShm;
49569 pShmNode = pShm->pShmNode;
49571 sqlite3_mutex_enter(pShmNode->mutex);
49572 if( pShmNode->isUnlocked ){
49575 pShmNode->isUnlocked = 0;
49577 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
49579 if( pShmNode->nRegion<=iRegion ){
49582 sqlite3_int64 sz; /* Current size of wal-index file */
49584 pShmNode->szRegion = szRegion;
49587 ** Check to see if it has been allocated (i.e. if the wal-index file is
49590 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
49593 "winShmMap1", pDbFd->zPath);
49601 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
49605 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
49608 "winShmMap2", pDbFd->zPath);
49615 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
49621 pShmNode->aRegion = apNew;
49623 if( pShmNode->isReadonly ){
49628 while( pShmNode->nRegion<=iRegion ){
49629 HANDLE hMap = NULL; /* file-mapping handle */
49633 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
49637 hMap = osCreateFileMappingW(pShmNode->hFile.h,
49641 hMap = osCreateFileMappingA(pShmNode->hFile.h,
49645 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
49646 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
49649 int iOffset = pShmNode->nRegion*szRegion;
49653 iOffset - iOffsetShift, szRegion + iOffsetShift
49657 0, iOffset - iOffsetShift, szRegion + iOffsetShift
49660 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
49661 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
49665 pShmNode->lastErrno = osGetLastError();
49666 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
49667 "winShmMap3", pDbFd->zPath);
49672 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
49673 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
49674 pShmNode->nRegion++;
49679 if( pShmNode->nRegion>iRegion ){
49682 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
49687 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
49688 sqlite3_mutex_leave(pShmNode->mutex);
49705 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
49707 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
49708 pFile->mmapSize, pFile->mmapSizeMax));
49709 if( pFile->pMapRegion ){
49710 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
49711 pFile->lastErrno = osGetLastError();
49712 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
49714 pFile->pMapRegion));
49715 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49716 "winUnmapfile1", pFile->zPath);
49718 pFile->pMapRegion = 0;
49719 pFile->mmapSize = 0;
49721 if( pFile->hMap!=NULL ){
49722 if( !osCloseHandle(pFile->hMap) ){
49723 pFile->lastErrno = osGetLastError();
49724 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
49725 osGetCurrentProcessId(), pFile, pFile->hMap));
49726 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49727 "winUnmapfile2", pFile->zPath);
49729 pFile->hMap = NULL;
49731 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49737 ** Memory map or remap the file opened by file-descriptor pFd (if the file
49740 ** outstanding xFetch() references to it, this function is a no-op.
49742 ** If parameter nByte is non-negative, then it is the requested size of
49756 assert( nMap>=0 || pFd->nFetchOut==0 );
49757 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
49760 if( pFd->nFetchOut>0 ) return SQLITE_OK;
49765 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
49770 if( nMap>pFd->mmapSizeMax ){
49771 nMap = pFd->mmapSizeMax;
49773 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
49775 if( nMap==0 && pFd->mmapSize>0 ){
49778 if( nMap!=pFd->mmapSize ){
49785 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
49791 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
49793 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
49797 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
49801 if( pFd->hMap==NULL ){
49802 pFd->lastErrno = osGetLastError();
49803 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49804 "winMapfile1", pFd->zPath);
49806 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
49813 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
49815 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
49818 osCloseHandle(pFd->hMap);
49819 pFd->hMap = NULL;
49820 pFd->lastErrno = osGetLastError();
49821 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49822 "winMapfile2", pFd->zPath);
49824 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
49828 pFd->pMapRegion = pNew;
49829 pFd->mmapSize = nMap;
49832 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49860 if( pFd->mmapSizeMax>0 ){
49861 if( pFd->pMapRegion==0 ){
49862 int rc = winMapfile(pFd, -1);
49869 if( pFd->mmapSize >= iOff+nAmt ){
49870 assert( pFd->pMapRegion!=0 );
49871 *pp = &((u8 *)pFd->pMapRegion)[iOff];
49872 pFd->nFetchOut++;
49883 ** If the third argument is non-NULL, then this function releases a
49899 assert( (p==0)==(pFd->nFetchOut==0) );
49902 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
49908 pFd->nFetchOut--;
49912 ** is unnecessary can be omitted - potentially improving
49917 assert( pFd->nFetchOut>=0 );
50005 ** supports for filenames into UTF-8. Space to hold the result is
50024 ** Convert a UTF-8 filename into whatever form the underlying
50044 ** This function returns non-zero if the specified UTF-8 string buffer
50052 if( winIsDirSep(zBuf[nLen-1]) ){
50091 /* It's odd to simulate an io-error here, but really this is just
50092 ** using the io-error infrastructure to test that SQLite handles this
50100 nMax = pVfs->mxPathname; nBuf = nMax + 2;
50103 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50111 nDir = nMax - (nPre + 15);
50116 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
50122 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50164 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50177 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50185 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
50192 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
50198 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50216 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50222 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50234 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50244 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50249 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50259 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50273 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50291 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50295 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
50300 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
50306 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
50352 const char *zName, /* Name of the file (UTF-8) */
50369 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
50411 /* The main DB, main journal, WAL file and super-journal are never
50418 /* Assert that the upper layer has set one of the "file-type" flags. */
50427 pFile->h = INVALID_HANDLE_VALUE;
50449 /* Database filenames are double-zero terminated if they are not
50595 pFile->lastErrno = lastErrno;
50596 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
50613 pAppData = (winVfsAppData*)pVfs->pAppData;
50618 && ((pAppData==NULL) || !pAppData->bNoLock)
50624 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
50629 pFile->zDeleteOnClose = zConverted;
50637 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
50638 pFile->pVfs = pVfs;
50639 pFile->h = h;
50641 pFile->ctrlFlags |= WINFILE_RDONLY;
50646 pFile->ctrlFlags |= WINFILE_PSOW;
50648 pFile->lastErrno = NO_ERROR;
50649 pFile->zPath = zName;
50651 pFile->hMap = NULL;
50652 pFile->pMapRegion = 0;
50653 pFile->mmapSize = 0;
50654 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
50817 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
50863 ** Returns non-zero if the specified path name starts with the "long path"
50874 ** Returns non-zero if the specified path name starts with a drive letter
50884 ** Returns non-zero if the specified path name should be used verbatim. If
50885 ** non-zero is returned from this function, the calling function must simply
50886 ** use the provided path name verbatim -OR- resolve it into a full path name
50922 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
50948 assert( nFull>=pVfs->mxPathname );
50956 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50962 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
50972 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50978 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50984 zRelative, zOut, pVfs->mxPathname+1)<0 ){
50994 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
51013 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51016 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
51022 /* It's odd to simulate an io-error here, but really this is just
51023 ** using the io-error infrastructure to test that SQLite handles this
51035 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
51096 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
51127 int nFull = pVfs->mxPathname+1;
51202 for(j=0, k=p->i; j<sz; j++){
51203 p->a[k++] ^= x[j];
51204 if( k>=p->na ) k = 0;
51206 p->i = k;
51207 p->nXor += sz;
51278 ** The following variable, if set to a non-zero value, is interpreted as
51297 /* FILETIME structure is a 64-bit value representing the number of
51298 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
51305 /* 2^32 - to avoid use of LL and warnings in gcc */
51355 ** buffer with a nul-terminated UTF-8 encoded error message
51361 ** is zero if the error message fits in the buffer, or non-zero
51362 ** otherwise (if the message was truncated). If non-zero is returned,
51363 ** then it is not necessary to include the nul-terminator character
51420 "win32-longpath", /* zName */
51445 "win32-none", /* zName */
51470 "win32-longpath-none", /* zName */
51491 /* Double-check that the aSyscall[] array has been constructed
51544 ** 2016-09-07
51555 ** This file implements an in-memory VFS. A database is held as a contiguous
51571 /* Access to a lower-level VFS that (might) implement dynamic loading,
51574 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
51634 ** File-scope variables for holding the memdb files that are accessible
51738 sqlite3_mutex_enter(p->pMutex);
51741 sqlite3_mutex_leave(p->pMutex);
51748 ** Close an memdb-file.
51753 MemStore *p = ((MemFile*)pFile)->pStore;
51754 if( p->zFName ){
51763 if( p->nRef==1 ){
51764 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
51777 p->nRef--;
51778 if( p->nRef<=0 ){
51779 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
51780 sqlite3_free(p->aData);
51783 sqlite3_mutex_free(p->pMutex);
51792 ** Read data from an memdb-file.
51800 MemStore *p = ((MemFile*)pFile)->pStore;
51802 if( iOfst+iAmt>p->sz ){
51804 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
51808 memcpy(zBuf, p->aData+iOfst, iAmt);
51818 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
51821 if( newSz>p->szMax ){
51825 if( newSz>p->szMax ) newSz = p->szMax;
51826 pNew = sqlite3Realloc(p->aData, newSz);
51828 p->aData = pNew;
51829 p->szAlloc = newSz;
51834 ** Write data to an memdb-file.
51842 MemStore *p = ((MemFile*)pFile)->pStore;
51844 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51850 if( iOfst+iAmt>p->sz ){
51852 if( iOfst+iAmt>p->szAlloc
51858 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
51859 p->sz = iOfst+iAmt;
51861 memcpy(p->aData+iOfst, z, iAmt);
51867 ** Truncate an memdb-file.
51874 MemStore *p = ((MemFile*)pFile)->pStore;
51877 if( size>p->sz ){
51881 p->sz = size;
51888 ** Sync an memdb-file.
51897 ** Return the current file-size of an memdb-file.
51900 MemStore *p = ((MemFile*)pFile)->pStore;
51902 *pSize = p->sz;
51908 ** Lock an memdb-file.
51912 MemStore *p = pThis->pStore;
51914 if( eLock<=pThis->eLock ) return SQLITE_OK;
51917 assert( p->nWrLock==0 || p->nWrLock==1 );
51918 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
51919 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
51921 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51926 assert( pThis->eLock==SQLITE_LOCK_NONE );
51927 if( p->nWrLock>0 ){
51930 p->nRdLock++;
51937 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51938 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
51939 if( p->nWrLock>0 ){
51942 p->nWrLock = 1;
51950 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51951 if( p->nRdLock>1 ){
51953 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
51954 p->nWrLock = 1;
51960 if( rc==SQLITE_OK ) pThis->eLock = eLock;
51966 ** Unlock an memdb-file.
51970 MemStore *p = pThis->pStore;
51971 if( eLock>=pThis->eLock ) return SQLITE_OK;
51976 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
51977 p->nWrLock--;
51980 if( pThis->eLock>SQLITE_LOCK_SHARED ){
51981 p->nWrLock--;
51983 p->nRdLock--;
51986 pThis->eLock = eLock;
51994 ** occur on an in-memory database.
52004 ** File control method. For custom operations on an memdb-file.
52007 MemStore *p = ((MemFile*)pFile)->pStore;
52011 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
52016 if( iLimit<p->sz ){
52018 iLimit = p->szMax;
52020 iLimit = p->sz;
52023 p->szMax = iLimit;
52033 ** Return the sector-size in bytes for an memdb-file.
52041 ** Return the device characteristic flags supported by an memdb-file.
52051 /* Fetch a page of a memory-mapped file */
52058 MemStore *p = ((MemFile*)pFile)->pStore;
52060 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
52063 p->nMmap++;
52064 *pp = (void*)(p->aData + iOfst);
52070 /* Release a memory-mapped page */
52072 MemStore *p = ((MemFile*)pFile)->pStore;
52076 p->nMmap--;
52105 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
52127 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
52128 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52129 p->zFName = (char*)&p[1];
52130 memcpy(p->zFName, zName, szName+1);
52131 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
52132 if( p->pMutex==0 ){
52133 memdb_g.nMemStore--;
52138 p->nRef = 1;
52142 p->nRef++;
52151 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
52152 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52154 pFile->pStore = p;
52158 pFd->pMethods = &memdb_io_methods;
52163 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
52167 ** ensure the file-system modifications are synced to disk before
52214 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
52219 ** utf-8 string describing the most recent error encountered associated
52223 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
52230 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
52237 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
52245 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
52253 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
52261 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
52266 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
52269 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
52281 if( p->base.pMethods!=&memdb_io_methods ) return 0;
52282 pStore = p->pStore;
52284 if( pStore->zFName!=0 ) p = 0;
52315 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52318 if( piSize ) *piSize = -1;
52321 MemStore *pStore = p->pStore;
52322 assert( pStore->pMutex==0 );
52323 if( piSize ) *piSize = pStore->sz;
52325 pOut = pStore->aData;
52327 pOut = sqlite3_malloc64( pStore->sz );
52328 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
52332 pBt = db->aDb[iDb].pBt;
52336 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
52355 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
52395 sqlite3_mutex_enter(db->mutex);
52396 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52407 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
52411 db->init.iDb = (u8)iDb;
52412 db->init.reopenMemdb = 1;
52414 db->init.reopenMemdb = 0;
52423 MemStore *pStore = p->pStore;
52424 pStore->aData = pData;
52426 pStore->sz = szDb;
52427 pStore->szAlloc = szBuf;
52428 pStore->szMax = szBuf;
52429 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
52430 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
52432 pStore->mFlags = mFlags;
52441 sqlite3_mutex_leave(db->mutex);
52460 sz = pLower->szOsFile;
52485 ** This file implements an object that represents a fixed-length
52489 ** journalled during a transaction, or which pages have the "dont-write"
52518 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
52535 ** sub-dividing and re-hashing. */
52540 ** no fewer collisions than the no-op *1. */
52560 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
52569 u32 nSet; /* Number of bits that are set - only valid for aHash
52593 p->iSize = iSize;
52599 ** Check to see if the i-th bit is set. Return true or false.
52605 i--;
52606 if( i>=p->iSize ) return 0;
52607 while( p->iDivisor ){
52608 u32 bin = i/p->iDivisor;
52609 i = i%p->iDivisor;
52610 p = p->u.apSub[bin];
52615 if( p->iSize<=BITVEC_NBIT ){
52616 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
52619 while( p->u.aHash[h] ){
52620 if( p->u.aHash[h]==i ) return 1;
52631 ** Set the i-th bit. Return 0 on success and an error code if
52634 ** This routine might cause sub-bitmaps to be allocated. Failing
52635 ** to get the memory needed to hold the sub-bitmap is the only
52646 assert( i<=p->iSize );
52647 i--;
52648 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
52649 u32 bin = i/p->iDivisor;
52650 i = i%p->iDivisor;
52651 if( p->u.apSub[bin]==0 ){
52652 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
52653 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
52655 p = p->u.apSub[bin];
52657 if( p->iSize<=BITVEC_NBIT ){
52658 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
52664 /* worring about sub-dividing and re-hashing. */
52665 if( !p->u.aHash[h] ){
52666 if (p->nSet<(BITVEC_NINT-1)) {
52675 if( p->u.aHash[h]==i ) return SQLITE_OK;
52678 } while( p->u.aHash[h] );
52683 if( p->nSet>=BITVEC_MXHASH ){
52686 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
52690 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52691 memset(p->u.apSub, 0, sizeof(p->u.apSub));
52692 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
52702 p->nSet++;
52703 p->u.aHash[h] = i;
52708 ** Clear the i-th bit.
52716 i--;
52717 while( p->iDivisor ){
52718 u32 bin = i/p->iDivisor;
52719 i = i%p->iDivisor;
52720 p = p->u.apSub[bin];
52725 if( p->iSize<=BITVEC_NBIT ){
52726 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
52730 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52731 memset(p->u.aHash, 0, sizeof(p->u.aHash));
52732 p->nSet = 0;
52735 u32 h = BITVEC_HASH(aiValues[j]-1);
52736 p->nSet++;
52737 while( p->u.aHash[h] ){
52741 p->u.aHash[h] = aiValues[j];
52752 if( p->iDivisor ){
52755 sqlite3BitvecDestroy(p->u.apSub[i]);
52766 return p->iSize;
52808 ** If a memory allocation error occurs, return -1.
52813 int rc = -1;
52836 i = aOp[pc+2] - 1;
52848 if( (--aOp[pc+1]) > 0 ) nx = 0;
52864 ** match (rc==0). Change rc to non-zero if a discrepancy
52869 + (sqlite3BitvecSize(pBitvec) - sz);
52915 ** such that p was added to the list more recently than p->pDirtyNext.
52920 ** page to eject from the cache mid-transaction. It is better to eject
52963 pPg = (PgHdr*)pLower->pExtra;
52964 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
52965 a = (unsigned char *)pLower->pBuf;
52976 if( pCache->pCache==0 ) return;
52980 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
52982 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
52983 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
53000 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53020 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
53021 pCache = pPg->pCache;
53023 if( pPg->flags & PGHDR_CLEAN ){
53024 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
53027 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
53028 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
53029 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
53030 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
53034 if( pPg->flags & PGHDR_WRITEABLE ){
53035 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
53073 PCache *p = pPage->pCache;
53077 pPage->pgno));
53079 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
53080 assert( pPage->pDirtyPrev || pPage==p->pDirty );
53083 if( p->pSynced==pPage ){
53084 p->pSynced = pPage->pDirtyPrev;
53087 if( pPage->pDirtyNext ){
53088 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
53090 assert( pPage==p->pDirtyTail );
53091 p->pDirtyTail = pPage->pDirtyPrev;
53093 if( pPage->pDirtyPrev ){
53094 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
53100 assert( pPage==p->pDirty );
53101 p->pDirty = pPage->pDirtyNext;
53102 assert( p->bPurgeable || p->eCreate==2 );
53103 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
53104 assert( p->bPurgeable==0 || p->eCreate==1 );
53105 p->eCreate = 2;
53110 pPage->pDirtyPrev = 0;
53111 pPage->pDirtyNext = p->pDirty;
53112 if( pPage->pDirtyNext ){
53113 assert( pPage->pDirtyNext->pDirtyPrev==0 );
53114 pPage->pDirtyNext->pDirtyPrev = pPage;
53116 p->pDirtyTail = pPage;
53117 if( p->bPurgeable ){
53118 assert( p->eCreate==2 );
53119 p->eCreate = 1;
53122 p->pDirty = pPage;
53128 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
53129 if( !p->pSynced
53130 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
53132 p->pSynced = pPage;
53140 ** being used for an in-memory database, this function is a no-op.
53143 if( p->pCache->bPurgeable ){
53144 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
53145 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
53146 pcacheDump(p->pCache);
53151 ** Compute the number of pages of cache requested. p->szCache is the
53155 if( p->szCache>=0 ){
53156 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
53158 return p->szCache;
53161 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
53165 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
53178 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
53179 ** built-in default page cache is used instead of the application defined
53188 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
53219 p->szPage = 1;
53220 p->szExtra = szExtra;
53222 p->bPurgeable = bPurgeable;
53223 p->eCreate = 2;
53224 p->xStress = xStress;
53225 p->pStress = pStress;
53226 p->szCache = 100;
53227 p->szSpill = 1;
53237 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
53238 if( pCache->szPage ){
53241 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
53242 pCache->bPurgeable
53246 if( pCache->pCache ){
53247 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53249 pCache->pCache = pNew;
53250 pCache->szPage = szPage;
53289 assert( pCache->pCache!=0 );
53291 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
53300 eCreate = createFlag & pCache->eCreate;
53302 assert( createFlag==0 || pCache->eCreate==eCreate );
53303 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
53304 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
53328 if( pCache->eCreate==2 ) return 0;
53330 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
53331 /* Find a dirty page to write-out and recycle. First try to find a
53332 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
53340 for(pPg=pCache->pSynced;
53341 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
53342 pPg=pPg->pDirtyPrev
53344 pCache->pSynced = pPg;
53346 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
53352 "spill page %d making room for %d - cache used: %d/%d",
53353 pPg->pgno, pgno,
53354 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
53357 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
53358 rc = pCache->xStress(pCache->pStress, pPg);
53365 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
53385 pPgHdr = (PgHdr*)pPage->pExtra;
53386 assert( pPgHdr->pPage==0 );
53387 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
53388 pPgHdr->pPage = pPage;
53389 pPgHdr->pData = pPage->pBuf;
53390 pPgHdr->pExtra = (void *)&pPgHdr[1];
53391 memset(pPgHdr->pExtra, 0, 8);
53392 pPgHdr->pCache = pCache;
53393 pPgHdr->pgno = pgno;
53394 pPgHdr->flags = PGHDR_CLEAN;
53412 pPgHdr = (PgHdr *)pPage->pExtra;
53414 if( !pPgHdr->pPage ){
53417 pCache->nRefSum++;
53418 pPgHdr->nRef++;
53428 assert( p->nRef>0 );
53429 p->pCache->nRefSum--;
53430 if( (--p->nRef)==0 ){
53431 if( p->flags&PGHDR_CLEAN ){
53444 assert(p->nRef>0);
53446 p->nRef++;
53447 p->pCache->nRefSum++;
53456 assert( p->nRef==1 );
53458 if( p->flags&PGHDR_DIRTY ){
53461 p->pCache->nRefSum--;
53462 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
53470 assert( p->nRef>0 );
53472 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
53473 p->flags &= ~PGHDR_DONT_WRITE;
53474 if( p->flags & PGHDR_CLEAN ){
53475 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
53476 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
53477 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
53491 assert( (p->flags & PGHDR_DIRTY)!=0 );
53492 assert( (p->flags & PGHDR_CLEAN)==0 );
53494 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53495 p->flags |= PGHDR_CLEAN;
53496 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
53498 if( p->nRef==0 ){
53508 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
53509 while( (p = pCache->pDirty)!=0 ){
53519 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
53520 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53521 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53523 pCache->pSynced = pCache->pDirtyTail;
53531 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53532 p->flags &= ~PGHDR_NEED_SYNC;
53534 pCache->pSynced = pCache->pDirtyTail;
53541 PCache *pCache = p->pCache;
53543 assert( p->nRef>0 );
53546 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
53547 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
53549 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
53550 assert( pXPage->nRef==0 );
53551 pXPage->nRef++;
53552 pCache->nRefSum++;
53555 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
53556 p->pgno = newPgno;
53557 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
53573 if( pCache->pCache ){
53577 for(p=pCache->pDirty; p; p=pNext){
53578 pNext = p->pDirtyNext;
53583 assert( p->pgno>0 );
53584 if( p->pgno>pgno ){
53585 assert( p->flags&PGHDR_DIRTY );
53589 if( pgno==0 && pCache->nRefSum ){
53591 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
53593 ** pCache->nRefSum>0 */
53594 memset(pPage1->pBuf, 0, pCache->szPage);
53598 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
53606 assert( pCache->pCache!=0 );
53608 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53627 if( pA->pgno<pB->pgno ){
53628 pTail->pDirty = pA;
53630 pA = pA->pDirty;
53632 pTail->pDirty = pB;
53636 pTail->pDirty = pB;
53638 pB = pB->pDirty;
53640 pTail->pDirty = pA;
53665 pIn = p->pDirty;
53666 p->pDirty = 0;
53667 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
53676 if( NEVER(i==N_SORT_BUCKET-1) ){
53696 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53697 p->pDirty = p->pDirtyNext;
53699 return pcacheSortDirtyList(pCache->pDirty);
53709 return pCache->nRefSum;
53716 return p->nRef;
53723 assert( pCache->pCache!=0 );
53724 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
53729 ** Get the suggested cache-size value.
53737 ** Set the suggested cache-size value.
53740 assert( pCache->pCache!=0 );
53741 pCache->szCache = mxPage;
53742 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
53747 ** Set the suggested cache-spill value. Make no changes if if the
53748 ** argument is zero. Return the effective cache-spill size, which will
53753 assert( p->pCache!=0 );
53756 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
53758 p->szSpill = mxPage;
53761 if( res<p->szSpill ) res = p->szSpill;
53769 assert( pCache->pCache!=0 );
53770 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
53775 ** in the page-cache hierarchy.
53787 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
53796 return (pCache->pDirty!=0);
53808 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
53836 ** -------------------------------------------------------------
53838 ** -------------------------------------------------------------
53854 ** size can vary according to architecture, compile-time options, and
53860 ** allocations. However, the btree layer needs a small (16-byte) overrun
53875 ** (1) The general-purpose memory allocator - sqlite3Malloc()
53876 ** (2) Global page-cache memory provided using sqlite3_config() with
53878 ** (3) PCache-local bulk allocation.
53888 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
53892 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
53908 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
53918 ** but causes a 2-byte gap in the structure for most architectures (since
53919 ** pointers must be either 4 or 8-byte aligned). As this structure is located
53921 ** corrupt, code at the b-tree layer may overread the page buffer and
53927 ** The pLruNext and pLruPrev pointers form a double-linked circular list
53948 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
53949 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
53977 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
53983 ** open database file (including each in-memory database and each
53998 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
54016 PgHdr1 *pFree; /* List of unused pcache-local pages */
54017 void *pBulk; /* Bulk memory used by pcache-local */
54051 ** reading because (1) most platforms read a 32-bit integer atomically and
54068 # define pcache1EnterMutex(X) assert((X)->mutex==0)
54069 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
54072 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
54073 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
54083 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
54102 while( n-- ){
54104 p->pNext = pcache1.pFree;
54113 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
54114 ** true if pCache->pFree ends up containing one or more free pages.
54121 if( pCache->nMax<3 ) return 0;
54124 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
54126 szBulk = -1024 * (i64)pcache1.nInitPage;
54128 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
54129 szBulk = pCache->szAlloc*(i64)pCache->nMax;
54131 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
54134 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
54136 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
54137 pX->page.pBuf = zBulk;
54138 pX->page.pExtra = &pX[1];
54139 pX->isBulkLocal = 1;
54140 pX->isAnchor = 0;
54141 pX->pNext = pCache->pFree;
54142 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
54143 pCache->pFree = pX;
54144 zBulk += pCache->szAlloc;
54145 }while( --nBulk );
54147 return pCache->pFree!=0;
54166 pcache1.pFree = pcache1.pFree->pNext;
54167 pcache1.nFreeSlot--;
54204 pSlot->pNext = pcache1.pFree;
54251 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54252 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
54253 assert( pCache->pFree!=0 );
54254 p = pCache->pFree;
54255 pCache->pFree = p->pNext;
54256 p->pNext = 0;
54263 assert( pCache->pGroup==&pcache1.grp );
54264 pcache1LeaveMutex(pCache->pGroup);
54267 pPg = pcache1Alloc(pCache->szAlloc);
54270 pcache1EnterMutex(pCache->pGroup);
54273 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
54274 p->page.pBuf = pPg;
54275 p->page.pExtra = &p[1];
54276 p->isBulkLocal = 0;
54277 p->isAnchor = 0;
54278 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
54280 (*pCache->pnPurgeable)++;
54290 pCache = p->pCache;
54291 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
54292 if( p->isBulkLocal ){
54293 p->pNext = pCache->pFree;
54294 pCache->pFree = p;
54296 pcache1Free(p->page.pBuf);
54298 (*pCache->pnPurgeable)--;
54336 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
54357 assert( sqlite3_mutex_held(p->pGroup->mutex) );
54359 nNew = p->nHash*2;
54364 pcache1LeaveMutex(p->pGroup);
54365 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
54367 if( p->nHash ){ sqlite3EndBenignMalloc(); }
54368 pcache1EnterMutex(p->pGroup);
54370 for(i=0; i<p->nHash; i++){
54372 PgHdr1 *pNext = p->apHash[i];
54374 unsigned int h = pPage->iKey % nNew;
54375 pNext = pPage->pNext;
54376 pPage->pNext = apNew[h];
54380 sqlite3_free(p->apHash);
54381 p->apHash = apNew;
54382 p->nHash = nNew;
54389 ** LRU list, then this function is a no-op.
54396 assert( pPage->pLruNext );
54397 assert( pPage->pLruPrev );
54398 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
54399 pPage->pLruPrev->pLruNext = pPage->pLruNext;
54400 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
54401 pPage->pLruNext = 0;
54402 /* pPage->pLruPrev = 0;
54404 assert( pPage->isAnchor==0 );
54405 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
54406 pPage->pCache->nRecyclable--;
54420 PCache1 *pCache = pPage->pCache;
54423 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54424 h = pPage->iKey % pCache->nHash;
54425 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
54426 *pp = (*pp)->pNext;
54428 pCache->nPage--;
54437 PGroup *pGroup = pCache->pGroup;
54439 assert( sqlite3_mutex_held(pGroup->mutex) );
54440 while( pGroup->nPurgeable>pGroup->nMaxPage
54441 && (p=pGroup->lru.pLruPrev)->isAnchor==0
54443 assert( p->pCache->pGroup==pGroup );
54448 if( pCache->nPage==0 && pCache->pBulk ){
54449 sqlite3_free(pCache->pBulk);
54450 pCache->pBulk = pCache->pFree = 0;
54465 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
54467 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54468 assert( pCache->iMaxKey >= iLimit );
54469 assert( pCache->nHash > 0 );
54470 if( pCache->iMaxKey - iLimit < pCache->nHash ){
54475 h = iLimit % pCache->nHash;
54476 iStop = pCache->iMaxKey % pCache->nHash;
54477 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
54481 h = pCache->nHash/2;
54482 iStop = h - 1;
54487 assert( h<pCache->nHash );
54488 pp = &pCache->apHash[h];
54490 if( pPage->iKey>=iLimit ){
54491 pCache->nPage--;
54492 *pp = pPage->pNext;
54496 pp = &pPage->pNext;
54501 h = (h+1) % pCache->nHash;
54503 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
54520 ** private PGroup (mode-1). pcache1.separateCache is false if the single
54521 ** PGroup in pcache1.grp is used for all page caches (mode-2).
54523 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
54525 ** * Use a unified cache in single-threaded applications that have
54526 ** configured a start-time buffer for use as page-cache memory using
54527 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
54530 ** * Otherwise use separate caches (mode-1)
54584 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
54592 pGroup->mxPinned = 10;
54597 if( pGroup->lru.isAnchor==0 ){
54598 pGroup->lru.isAnchor = 1;
54599 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
54601 pCache->pGroup = pGroup;
54602 pCache->szPage = szPage;
54603 pCache->szExtra = szExtra;
54604 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
54605 pCache->bPurgeable = (bPurgeable ? 1 : 0);
54608 pCache->nMin = 10;
54609 pGroup->nMinPage += pCache->nMin;
54610 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54611 pCache->pnPurgeable = &pGroup->nPurgeable;
54613 pCache->pnPurgeable = &pCache->nPurgeableDummy;
54616 if( pCache->nHash==0 ){
54633 if( pCache->bPurgeable ){
54634 PGroup *pGroup = pCache->pGroup;
54637 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
54638 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
54640 pGroup->nMaxPage += (n - pCache->nMax);
54641 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54642 pCache->nMax = n;
54643 pCache->n90pct = pCache->nMax*9/10;
54656 if( pCache->bPurgeable ){
54657 PGroup *pGroup = pCache->pGroup;
54660 savedMaxPage = pGroup->nMaxPage;
54661 pGroup->nMaxPage = 0;
54663 pGroup->nMaxPage = savedMaxPage;
54674 pcache1EnterMutex(pCache->pGroup);
54675 n = pCache->nPage;
54676 pcache1LeaveMutex(pCache->pGroup);
54695 PGroup *pGroup = pCache->pGroup;
54699 assert( pCache->nPage >= pCache->nRecyclable );
54700 nPinned = pCache->nPage - pCache->nRecyclable;
54701 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
54702 assert( pCache->n90pct == pCache->nMax*9/10 );
54704 nPinned>=pGroup->mxPinned
54705 || nPinned>=pCache->n90pct
54706 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
54711 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
54712 assert( pCache->nHash>0 && pCache->apHash );
54715 if( pCache->bPurgeable
54716 && !pGroup->lru.pLruPrev->isAnchor
54717 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
54720 pPage = pGroup->lru.pLruPrev;
54724 pOther = pPage->pCache;
54725 if( pOther->szAlloc != pCache->szAlloc ){
54729 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
54741 unsigned int h = iKey % pCache->nHash;
54742 pCache->nPage++;
54743 pPage->iKey = iKey;
54744 pPage->pNext = pCache->apHash[h];
54745 pPage->pCache = pCache;
54746 pPage->pLruNext = 0;
54747 /* pPage->pLruPrev = 0;
54749 *(void **)pPage->page.pExtra = 0;
54750 pCache->apHash[h] = pPage;
54751 if( iKey>pCache->iMaxKey ){
54752 pCache->iMaxKey = iKey;
54768 ** For a non-purgeable cache (a cache used as the storage for an in-memory
54771 ** a non-purgeable cache.
54814 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
54826 pPage = pCache->apHash[iKey % pCache->nHash];
54827 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
54855 pcache1EnterMutex(pCache->pGroup);
54857 assert( pPage==0 || pCache->iMaxKey>=iKey );
54858 pcache1LeaveMutex(pCache->pGroup);
54872 assert( pCache->bPurgeable || createFlag!=1 );
54873 assert( pCache->bPurgeable || pCache->nMin==0 );
54874 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
54875 assert( pCache->nMin==0 || pCache->bPurgeable );
54876 assert( pCache->nHash>0 );
54878 if( pCache->pGroup->mutex ){
54900 PGroup *pGroup = pCache->pGroup;
54902 assert( pPage->pCache==pCache );
54908 assert( pPage->pLruNext==0 );
54911 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
54915 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
54916 pPage->pLruPrev = &pGroup->lru;
54917 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
54919 pCache->nRecyclable++;
54922 pcache1LeaveMutex(pCache->pGroup);
54938 assert( pPage->iKey==iOld );
54939 assert( pPage->pCache==pCache );
54942 pcache1EnterMutex(pCache->pGroup);
54945 hOld = iOld%pCache->nHash;
54946 pp = &pCache->apHash[hOld];
54948 pp = &(*pp)->pNext;
54950 *pp = pPage->pNext;
54953 hNew = iNew%pCache->nHash;
54954 pPage->iKey = iNew;
54955 pPage->pNext = pCache->apHash[hNew];
54956 pCache->apHash[hNew] = pPage;
54957 if( iNew>pCache->iMaxKey ){
54958 pCache->iMaxKey = iNew;
54961 pcache1LeaveMutex(pCache->pGroup);
54973 pcache1EnterMutex(pCache->pGroup);
54974 if( iLimit<=pCache->iMaxKey ){
54976 pCache->iMaxKey = iLimit-1;
54978 pcache1LeaveMutex(pCache->pGroup);
54988 PGroup *pGroup = pCache->pGroup;
54989 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
54991 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
54992 assert( pGroup->nMaxPage >= pCache->nMax );
54993 pGroup->nMaxPage -= pCache->nMax;
54994 assert( pGroup->nMinPage >= pCache->nMin );
54995 pGroup->nMinPage -= pCache->nMin;
54996 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54999 sqlite3_free(pCache->pBulk);
55000 sqlite3_free(pCache->apHash);
55060 && p->isAnchor==0
55062 nFree += pcache1MemSize(p->page.pBuf);
55086 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
55146 ** a non-zero batch number, it will see all prior INSERTs.
55174 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
55232 p->pChunk = 0;
55233 p->db = db;
55234 p->pEntry = 0;
55235 p->pLast = 0;
55236 p->pForest = 0;
55237 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
55238 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
55239 p->rsFlags = ROWSET_SORTED;
55240 p->iBatch = 0;
55253 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
55254 pNextChunk = pChunk->pNextChunk;
55255 sqlite3DbFree(p->db, pChunk);
55257 p->pChunk = 0;
55258 p->nFresh = 0;
55259 p->pEntry = 0;
55260 p->pLast = 0;
55261 p->pForest = 0;
55262 p->rsFlags = ROWSET_SORTED;
55272 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
55280 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
55285 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
55289 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
55293 pNew->pNextChunk = p->pChunk;
55294 p->pChunk = pNew;
55295 p->pFresh = pNew->aEntry;
55296 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
55298 p->nFresh--;
55299 return p->pFresh++;
55313 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
55317 pEntry->v = rowid;
55318 pEntry->pRight = 0;
55319 pLast = p->pLast;
55321 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
55324 p->rsFlags &= ~ROWSET_SORTED;
55326 pLast->pRight = pEntry;
55328 p->pEntry = pEntry;
55330 p->pLast = pEntry;
55349 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
55350 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
55351 if( pA->v<=pB->v ){
55352 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
55353 pA = pA->pRight;
55355 pTail->pRight = pB;
55359 pTail = pTail->pRight = pB;
55360 pB = pB->pRight;
55362 pTail->pRight = pA;
55380 pNext = pIn->pRight;
55381 pIn->pRight = 0;
55409 if( pIn->pLeft ){
55411 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
55412 p->pRight = pIn;
55416 if( pIn->pRight ){
55417 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
55421 assert( (*ppLast)->pRight==0 );
55444 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
55448 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
55452 pLeft = rowSetNDeepTree(ppList, iDepth-1);
55454 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
55459 p->pLeft = pLeft;
55460 *ppList = p->pRight;
55461 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
55464 *ppList = p->pRight;
55465 p->pLeft = p->pRight = 0;
55481 pList = p->pRight;
55482 p->pLeft = p->pRight = 0;
55486 pList = p->pRight;
55487 p->pLeft = pLeft;
55488 p->pRight = rowSetNDeepTree(&pList, iDepth);
55508 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
55511 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55512 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55513 p->pEntry = rowSetEntrySort(p->pEntry);
55515 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
55519 if( p->pEntry ){
55520 *pRowid = p->pEntry->v;
55521 p->pEntry = p->pEntry->pRight;
55522 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
55537 ** on pRowSet->pEntry, then sort those entries into the forest at
55538 ** pRowSet->pForest so that they can be tested.
55544 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
55549 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
55550 p = pRowSet->pEntry;
55552 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
55553 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55557 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55558 ppPrevTree = &pTree->pRight;
55559 if( pTree->pLeft==0 ){
55560 pTree->pLeft = rowSetListToTree(p);
55564 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
55565 pTree->pLeft = 0;
55572 pTree->v = 0;
55573 pTree->pRight = 0;
55574 pTree->pLeft = rowSetListToTree(p);
55577 pRowSet->pEntry = 0;
55578 pRowSet->pLast = 0;
55579 pRowSet->rsFlags |= ROWSET_SORTED;
55581 pRowSet->iBatch = iBatch;
55587 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55588 p = pTree->pLeft;
55590 if( p->v<iRowid ){
55591 p = p->pRight;
55592 }else if( p->v>iRowid ){
55593 p = p->pLeft;
55639 ** This header file defines the interface to the write-ahead logging
55679 /* Connection to a write-ahead log (WAL) file.
55684 /* Open and close a connection to a write-ahead log. */
55692 ** snapshot is like a read-transaction. It is the state of the database
55701 /* Read a page from the write-ahead log, if it is present. */
55728 Wal *pWal, /* Write-ahead log connection */
55752 /* Return true if the argument is non-NULL and the WAL module is using
55753 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
55754 ** WAL module is using shared-memory, return false.
55768 ** stored in each frame (i.e. the db page-size when the WAL was created).
55835 ** (6) If a super-journal file is used, then all writes to the database file
55836 ** are synced prior to the super-journal being deleted.
55852 ** method is a no-op, but that does not change the fact the SQLite will
55863 ** (11) A database file is well-formed at the beginning and at the conclusion
55887 ** to print out file-descriptors.
55890 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
55893 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
55901 ** OPEN <------+------+
55904 ** +---------> READER-------+ |
55907 ** |<-------WRITER_LOCKED------> ERROR
55910 ** |<------WRITER_CACHEMOD-------->|
55913 ** |<-------WRITER_DBMOD---------->|
55916 ** +<------WRITER_FINISHED-------->+
55921 ** OPEN -> READER [sqlite3PagerSharedLock]
55922 ** READER -> OPEN [pager_unlock]
55924 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
55925 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
55926 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
55927 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
55928 ** WRITER_*** -> READER [pager_end_transaction]
55930 ** WRITER_*** -> ERROR [pager_error]
55931 ** ERROR -> OPEN [pager_unlock]
55937 ** state - the file may or may not be locked and the database size is
55947 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
55948 ** was) in exclusive-locking mode, a user-level read transaction is
55952 ** it opens a read-transaction on the database and returns to state
55953 ** OPEN after the read-transaction is completed. However a connection
55955 ** this state even after the read-transaction is closed. The only way
55959 ** * A read transaction may be active (but a write-transaction cannot).
55961 ** * The dbSize variable may be trusted (even if a user-level read
55965 ** * Even if a read-transaction is not open, it is guaranteed that
55966 ** there is no hot-journal in the file-system.
55970 ** The pager moves to this state from READER when a write-transaction
55972 ** required to start a write-transaction are held, but no actual
55987 ** * If the connection is open in rollback-mode, a RESERVED or greater
55989 ** * If the connection is open in WAL-mode, a WAL write transaction
56028 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
56044 ** The ERROR state is entered when an IO or disk-full error (including
56046 ** difficult to be sure that the in-memory pager state (cache contents,
56047 ** db size etc.) are consistent with the contents of the file-system.
56049 ** Temporary pager files may enter the ERROR state, but in-memory pagers
56053 ** the contents of the page-cache may be left in an inconsistent state.
56065 ** page-cache and any other in-memory state at the same time. Everything
56066 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
56067 ** when a read-transaction is next opened on the pager (transitioning
56083 ** In other cases, the error is returned to the b-tree layer. The b-tree
56087 ** Condition (3) is necessary because it can be triggered by a read-only
56089 ** code were simply returned to the user, the b-tree layer would not
56091 ** read-only statement cannot leave the pager in an internally inconsistent
56097 ** * The pager is not an in-memory pager.
56107 ** state. There are two exceptions: immediately after exclusive-mode has
56123 ** following locking-states, according to the lock currently held on
56131 ** pagerUnlockDb() take a conservative approach - eLock is always updated
56142 ** from ERROR to OPEN state. At this point there may be a hot-journal file
56143 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
56147 ** of hot-journal detection.
56153 ** a hot-journal may be mistaken for a journal being created by an active
56160 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
56161 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
56162 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
56187 ** set to 0. If a journal-header is written into the main journal while
56190 ** journal before the journal-header. This is required during savepoint
56199 Pgno iSubRec; /* Index of first record in sub-journal */
56224 ** For a real on-disk database, the current lock held on the database file -
56227 ** For a temporary or in-memory database (neither of which require any
56239 ** This boolean variable is used to make sure that the change-counter
56240 ** (the 4-byte header field at byte offset 24 of the database file) is
56243 ** It is set to true when the change-counter field is updated, which
56248 ** updating the change-counter is omitted for the current transaction.
56251 ** need only update the change-counter once, for the first transaction
56257 ** (or may not) specify a super-journal name to be written into the
56260 ** Whether or not a journal file contains a super-journal pointer affects
56263 ** If a journal file does not contain a super-journal pointer, it is
56265 ** it does contain a super-journal pointer the journal file is finalized
56269 ** Journal files that contain super-journal pointers cannot be finalized
56270 ** simply by overwriting the first journal-header with zeroes, as the
56271 ** super-journal pointer could interfere with hot-journal rollback of any
56281 ** This variables control the behavior of cache-spills (calls made by
56283 ** to the file-system in order to free up memory).
56295 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
56296 ** is larger than the database page-size in order to prevent a journal sync
56301 ** This is a boolean variable. If true, then any required sub-journal
56302 ** is opened as an in-memory journal file. If false, then in-memory
56303 ** sub-journals are only used for in-memory pager files.
56306 ** write-transaction is opened.
56317 ** is not an integer multiple of the page-size, the value stored in
56318 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
56320 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
56323 ** During a write-transaction, if pages with page-numbers greater than
56334 ** Throughout a write-transaction, dbFileSize contains the size of
56336 ** write-transaction is first opened, and updated when VFS calls are made
56356 ** write-transaction is opened (at the same time as dbFileSize and
56359 ** size-hint passed to the method call. See pager_write_pagelist() for
56367 ** sub-codes.
56393 u8 readOnly; /* True for a read-only database */
56395 u8 memVfs; /* VFS-implemented memory database */
56408 u8 changeCountDone; /* Set after incrementing the change-counter */
56409 u8 setSuper; /* Super-jrnl name is written into jrnl */
56410 u8 doNotSpill; /* Do not spill the cache when non-zero */
56411 u8 subjInMemory; /* True to use in-memory sub-journals */
56419 int nRec; /* Pages journalled since last j-header written */
56420 u32 cksumInit; /* Quasi-random value added to every checksum */
56421 u32 nSubRec; /* Number of records written to sub-journal */
56425 sqlite3_file *sjfd; /* File descriptor for sub-journal */
56438 ** End of the routinely-changing class members
56441 u16 nExtra; /* Add this many bytes to each in-memory page */
56462 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
56463 char *zWal; /* File name for write-ahead log */
56480 ** a non-testing build. These variables are not thread-safe.
56499 ** written, semi-random garbage data might appear in the journal
56506 ** of a 32-bit checksum on each page of data. The checksum covers both
56507 ** the page number and the pPager->pageSize bytes of data for the page.
56508 ** This cksum is initialized to a 32-bit random value that appears in the
56524 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
56530 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
56533 ** The macro MEMDB is true if we are dealing with an in-memory database.
56541 # define MEMDB pPager->memDb
56546 ** interfaces to access the database using memory-mapped I/O.
56549 # define USEFETCH(x) ((x)->bUseFetch)
56556 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
56560 ** if( isOpen(pPager->jfd) ){ ...
56564 ** if( pPager->jfd->pMethods ){ ...
56566 #define isOpen(pFd) ((pFd)->pMethods!=0)
56571 ** by the b-tree layer. This is the case if:
56578 if( pPager->fd->pMethods==0 ) return 0;
56579 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
56581 if( pPager->pWal ){
56584 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
56593 # define pagerUseWal(x) ((x)->pWal!=0)
56615 assert( p->eState==PAGER_OPEN
56616 || p->eState==PAGER_READER
56617 || p->eState==PAGER_WRITER_LOCKED
56618 || p->eState==PAGER_WRITER_CACHEMOD
56619 || p->eState==PAGER_WRITER_DBMOD
56620 || p->eState==PAGER_WRITER_FINISHED
56621 || p->eState==PAGER_ERROR
56624 /* Regardless of the current state, a temp-file connection always behaves
56626 ** the change-counter field, so the changeCountDone flag is always set.
56628 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
56629 assert( p->tempFile==0 || pPager->changeCountDone );
56631 /* If the useJournal flag is clear, the journal-mode must be "OFF".
56632 ** And if the journal-mode is "OFF", the journal file must not be open.
56634 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
56635 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
56637 /* Check that MEMDB implies noSync. And an in-memory journal. Since
56638 ** this means an in-memory pager performs no IO at all, it cannot encounter
56640 ** a journal file. (although the in-memory journal implementation may
56642 ** is therefore not possible for an in-memory pager to enter the ERROR
56646 assert( !isOpen(p->fd) );
56647 assert( p->noSync );
56648 assert( p->journalMode==PAGER_JOURNALMODE_OFF
56649 || p->journalMode==PAGER_JOURNALMODE_MEMORY
56651 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
56658 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
56659 assert( p->eLock!=PENDING_LOCK );
56661 switch( p->eState ){
56664 assert( pPager->errCode==SQLITE_OK );
56665 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
56669 assert( pPager->errCode==SQLITE_OK );
56670 assert( p->eLock!=UNKNOWN_LOCK );
56671 assert( p->eLock>=SHARED_LOCK );
56675 assert( p->eLock!=UNKNOWN_LOCK );
56676 assert( pPager->errCode==SQLITE_OK );
56678 assert( p->eLock>=RESERVED_LOCK );
56680 assert( pPager->dbSize==pPager->dbOrigSize );
56681 assert( pPager->dbOrigSize==pPager->dbFileSize );
56682 assert( pPager->dbOrigSize==pPager->dbHintSize );
56683 assert( pPager->setSuper==0 );
56687 assert( p->eLock!=UNKNOWN_LOCK );
56688 assert( pPager->errCode==SQLITE_OK );
56695 assert( p->eLock>=RESERVED_LOCK );
56696 assert( isOpen(p->jfd)
56697 || p->journalMode==PAGER_JOURNALMODE_OFF
56698 || p->journalMode==PAGER_JOURNALMODE_WAL
56701 assert( pPager->dbOrigSize==pPager->dbFileSize );
56702 assert( pPager->dbOrigSize==pPager->dbHintSize );
56706 assert( p->eLock==EXCLUSIVE_LOCK );
56707 assert( pPager->errCode==SQLITE_OK );
56709 assert( p->eLock>=EXCLUSIVE_LOCK );
56710 assert( isOpen(p->jfd)
56711 || p->journalMode==PAGER_JOURNALMODE_OFF
56712 || p->journalMode==PAGER_JOURNALMODE_WAL
56713 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56715 assert( pPager->dbOrigSize<=pPager->dbHintSize );
56719 assert( p->eLock==EXCLUSIVE_LOCK );
56720 assert( pPager->errCode==SQLITE_OK );
56722 assert( isOpen(p->jfd)
56723 || p->journalMode==PAGER_JOURNALMODE_OFF
56724 || p->journalMode==PAGER_JOURNALMODE_WAL
56725 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56734 assert( pPager->errCode!=SQLITE_OK );
56735 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
56768 , p->zFilename
56769 , p->eState==PAGER_OPEN ? "OPEN" :
56770 p->eState==PAGER_READER ? "READER" :
56771 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
56772 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
56773 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
56774 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
56775 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
56776 , (int)p->errCode
56777 , p->eLock==NO_LOCK ? "NO_LOCK" :
56778 p->eLock==RESERVED_LOCK ? "RESERVED" :
56779 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
56780 p->eLock==SHARED_LOCK ? "SHARED" :
56781 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
56782 , p->exclusiveMode ? "exclusive" : "normal"
56783 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
56784 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
56785 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
56786 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
56787 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
56788 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
56789 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
56790 , p->journalOff, p->journalHdr
56791 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
56810 if( pPager->errCode ){
56811 pPager->xGet = getPageError;
56814 pPager->xGet = getPageMMap;
56817 pPager->xGet = getPageNormal;
56822 ** Return true if it is necessary to write page *pPg into the sub-journal.
56823 ** A page needs to be written into the sub-journal if there exists one
56826 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
56827 ** * The bit corresponding to the page-number is not set in
56831 Pager *pPager = pPg->pPager;
56833 Pgno pgno = pPg->pgno;
56835 for(i=0; i<pPager->nSavepoint; i++){
56836 p = &pPager->aSavepoint[i];
56837 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
56838 for(i=i+1; i<pPager->nSavepoint; i++){
56839 pPager->aSavepoint[i].bTruncateOnRelease = 0;
56852 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
56857 ** Read a 32-bit integer from the given file descriptor. Store the integer
56861 ** All values are stored on disk as big-endian.
56873 ** Write a 32-bit integer into a string buffer in big-endian byte order.
56879 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
56900 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
56903 if( isOpen(pPager->fd) ){
56904 assert( pPager->eLock>=eLock );
56905 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
56906 if( pPager->eLock!=UNKNOWN_LOCK ){
56907 pPager->eLock = (u8)eLock;
56911 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
56929 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
56930 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
56931 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
56932 pPager->eLock = (u8)eLock;
56940 ** This function determines whether or not the atomic-write or
56941 ** atomic-batch-write optimizations can be used with this pager. The
56942 ** atomic-write optimization can be used if:
56952 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
56953 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
56965 assert( isOpen(pPager->fd) );
56966 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
56972 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
56973 return -1;
56979 int nSector = pPager->sectorSize;
56980 int szPage = pPager->pageSize;
57002 ** Return a 32-bit hash of the page data for pPage.
57013 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
57016 pPage->pageHash = pager_pagehash(pPage);
57022 ** that the page is either dirty or still matches the calculated page-hash.
57026 Pager *pPager = pPg->pPager;
57027 assert( pPager->eState!=PAGER_ERROR );
57028 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
57040 ** This function attempts to read a super-journal file name from the
57043 ** used to store a super-journal file name at the end of a journal file.
57047 ** enough space to write the super-journal name). If the super-journal
57049 ** nul-terminator), then this is handled as if no super-journal name
57052 ** If a super-journal file name is present at the end of the journal
57054 ** nul-terminator byte is appended to the buffer following the
57055 ** super-journal file name.
57057 ** If it is determined that no super-journal file name is present
57065 u32 len; /* Length in bytes of super-journal name */
57074 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
57076 || len>szJ-16
57078 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
57079 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
57081 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
57086 /* See if the checksum matches the super-journal name */
57088 cksum -= zSuper[u];
57092 ** containing the super-journal filename is corrupted. This means
57094 ** super-journal filename.
57106 ** following the value in pPager->journalOff, assuming a sector
57107 ** size of pPager->sectorSize bytes.
57112 ** ---------------------------------------
57121 i64 c = pPager->journalOff;
57123 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
57127 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
57134 ** This function is a no-op if the journal file has not been written to
57137 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
57139 ** zero the 28-byte header at the start of the journal file. In either case,
57140 ** if the pager is not in no-sync mode, sync the journal file immediately
57143 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
57154 assert( isOpen(pPager->jfd) );
57155 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
57156 if( pPager->journalOff ){
57157 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
57161 rc = sqlite3OsTruncate(pPager->jfd, 0);
57164 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
57166 if( rc==SQLITE_OK && !pPager->noSync ){
57167 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
57178 rc = sqlite3OsFileSize(pPager->jfd, &sz);
57180 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
57193 ** - 8 bytes: Magic identifying journal format.
57194 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
57195 ** - 4 bytes: Random number used for page hash.
57196 ** - 4 bytes: Initial database page count.
57197 ** - 4 bytes: Sector size used by the process that wrote this journal.
57198 ** - 4 bytes: Database page size.
57200 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
57204 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
57205 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
57209 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57219 for(ii=0; ii<pPager->nSavepoint; ii++){
57220 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
57221 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
57225 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
57228 ** Write the nRec Field - the number of page records that follow this
57231 ** if in full-sync mode), the zero is overwritten with the true number
57241 ** * When the pager is in no-sync mode. Corruption can follow a
57247 assert( isOpen(pPager->fd) || pPager->noSync );
57248 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
57249 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
57257 /* The random check-hash initializer */
57258 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
57259 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
57261 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
57263 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
57266 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
57274 nHeader-(sizeof(aJournalMagic)+20));
57285 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
57288 ** The loop is required here in case the sector-size is larger than the
57294 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
57295 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
57296 assert( pPager->journalHdr <= pPager->journalOff );
57297 pPager->journalOff += nHeader;
57307 ** pPager->journalOff. See comments above function writeJournalHdr() for
57312 ** database before the transaction began, in pages. Also, pPager->cksumInit
57331 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57337 pPager->journalOff = journalHdrOffset(pPager);
57338 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
57341 iHdrOff = pPager->journalOff;
57348 if( isHot || iHdrOff!=pPager->journalHdr ){
57349 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
57358 /* Read the first three 32-bit fields of the journal header: The nRec
57359 ** field, the checksum-initializer and the database size at the start
57362 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
57363 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
57364 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
57369 if( pPager->journalOff==0 ){
57370 u32 iPageSize; /* Page-size field of journal header */
57371 u32 iSectorSize; /* Sector-size field of journal header */
57373 /* Read the page-size and sector-size journal header fields. */
57374 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
57375 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
57380 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
57385 iPageSize = pPager->pageSize;
57388 /* Check that the values read from the page-size and sector-size fields
57395 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
57397 /* If the either the page-size or sector-size in the journal-header is
57398 ** invalid, then the process that wrote the journal-header must have
57405 /* Update the page-size to match the value read from the journal.
57409 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
57412 /* Update the assumed sector-size to match the value used by
57418 pPager->sectorSize = iSectorSize;
57421 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
57427 ** Write the supplied super-journal name into the journal file for pager
57428 ** pPager at the current location. The super-journal name must be the last
57429 ** thing written to a journal file. If the pager is in full-sync mode, the
57434 ** + N bytes: super-journal filename in utf-8.
57435 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
57436 ** + 4 bytes: super-journal name checksum.
57439 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
57440 ** name, where each byte is interpreted as a signed 8-bit integer.
57443 ** this call is a no-op.
57452 assert( pPager->setSuper==0 );
57456 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57457 || !isOpen(pPager->jfd)
57461 pPager->setSuper = 1;
57462 assert( pPager->journalHdr <= pPager->journalOff );
57469 /* If in full-sync mode, advance to the next disk sector before writing
57470 ** the super-journal name. This is in case the previous page written to
57473 if( pPager->fullSync ){
57474 pPager->journalOff = journalHdrOffset(pPager);
57476 iHdrOff = pPager->journalOff;
57478 /* Write the super-journal data to the end of the journal file. If
57481 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
57482 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
57483 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
57484 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
57485 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
57490 pPager->journalOff += (nSuper+20);
57492 /* If the pager is in peristent-journal mode, then the physical
57493 ** journal-file may extend past the end of the super-journal name
57495 ** dangerous because the code to rollback a hot-journal file
57496 ** will not be able to find the super-journal name to determine
57502 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
57503 && jrnlSize>pPager->journalOff
57505 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
57511 ** Discard the entire contents of the in-memory page-cache.
57514 pPager->iDataVersion++;
57515 sqlite3BackupRestart(pPager->pBackup);
57516 sqlite3PcacheClear(pPager->pPCache);
57520 ** Return the pPager->iDataVersion value
57523 return pPager->iDataVersion;
57528 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
57533 for(ii=0; ii<pPager->nSavepoint; ii++){
57534 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
57536 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
57537 sqlite3OsClose(pPager->sjfd);
57539 sqlite3_free(pPager->aSavepoint);
57540 pPager->aSavepoint = 0;
57541 pPager->nSavepoint = 0;
57542 pPager->nSubRec = 0;
57554 for(ii=0; ii<pPager->nSavepoint; ii++){
57555 PagerSavepoint *p = &pPager->aSavepoint[ii];
57556 if( pgno<=p->nOrig ){
57557 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
57566 ** This function is a no-op if the pager is in exclusive mode and not
57570 ** If the pager is not in exclusive-access mode, the database file is
57571 ** completely unlocked. If the file is unlocked and the file-system does
57577 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
57578 ** or not, any journal file left in the file-system will be treated
57579 ** as a hot-journal and rolled back the next time a read-transaction
57584 assert( pPager->eState==PAGER_READER
57585 || pPager->eState==PAGER_OPEN
57586 || pPager->eState==PAGER_ERROR
57589 sqlite3BitvecDestroy(pPager->pInJournal);
57590 pPager->pInJournal = 0;
57594 assert( !isOpen(pPager->jfd) );
57595 sqlite3WalEndReadTransaction(pPager->pWal);
57596 pPager->eState = PAGER_OPEN;
57597 }else if( !pPager->exclusiveMode ){
57599 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
57613 || 1!=(pPager->journalMode & 5)
57615 sqlite3OsClose(pPager->jfd);
57624 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
57625 pPager->eLock = UNKNOWN_LOCK;
57629 ** without clearing the error code. This is intentional - the error
57632 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
57633 pPager->eState = PAGER_OPEN;
57639 ** normal and exclusive-locking mode.
57641 assert( pPager->errCode==SQLITE_OK || !MEMDB );
57642 if( pPager->errCode ){
57643 if( pPager->tempFile==0 ){
57645 pPager->changeCountDone = 0;
57646 pPager->eState = PAGER_OPEN;
57648 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
57650 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57651 pPager->errCode = SQLITE_OK;
57655 pPager->journalOff = 0;
57656 pPager->journalHdr = 0;
57657 pPager->setSuper = 0;
57664 ** the error-code about to be returned by a pager API function. The
57668 ** IOERR sub-codes, the pager enters the ERROR state and the error code
57672 ** The ERROR state indicates that the contents of the pager-cache
57674 ** the contents of the pager-cache. If a transaction was active when
57677 ** it were a hot-journal).
57683 pPager->errCode==SQLITE_FULL ||
57684 pPager->errCode==SQLITE_OK ||
57685 (pPager->errCode & 0xff)==SQLITE_IOERR
57688 pPager->errCode = rc;
57689 pPager->eState = PAGER_ERROR;
57705 ** * For non-TEMP databases, always sync to disk. This is necessary
57714 if( pPager->tempFile==0 ) return 1;
57716 if( !isOpen(pPager->fd) ) return 0;
57717 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
57723 ** after rollback of a hot-journal, or if an error occurs while opening
57724 ** the journal file or writing the very first journal-header of a
57729 ** exclusive than a RESERVED lock, it is a no-op.
57735 ** transaction. Nor will it be considered to be a hot-journal by this
57738 ** the current journal-mode (Pager.journalMode value), as follows:
57742 ** in-memory journal.
57761 ** If running in non-exclusive rollback mode, the lock on the file is
57779 ** is no write-transaction active but a RESERVED or greater lock is
57782 ** 1. After a successful hot-journal rollback, it is called with
57787 ** read-transaction, this function is called with eState==PAGER_READER
57788 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
57791 assert( pPager->eState!=PAGER_ERROR );
57792 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
57797 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
57798 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57800 if( isOpen(pPager->jfd) ){
57804 if( sqlite3JournalIsInMemory(pPager->jfd) ){
57805 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
57806 sqlite3OsClose(pPager->jfd);
57807 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
57808 if( pPager->journalOff==0 ){
57811 rc = sqlite3OsTruncate(pPager->jfd, 0);
57812 if( rc==SQLITE_OK && pPager->fullSync ){
57818 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
57821 pPager->journalOff = 0;
57822 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
57823 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
57825 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
57826 pPager->journalOff = 0;
57829 ** a hot-journal was just rolled back. In this case the journal
57831 ** the database file, it will do so using an in-memory journal.
57833 int bDelete = !pPager->tempFile;
57834 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
57835 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
57836 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57837 || pPager->journalMode==PAGER_JOURNALMODE_WAL
57839 sqlite3OsClose(pPager->jfd);
57841 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
57847 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
57848 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
57851 p->pageHash = 0;
57857 sqlite3BitvecDestroy(pPager->pInJournal);
57858 pPager->pInJournal = 0;
57859 pPager->nRec = 0;
57862 sqlite3PcacheCleanAll(pPager->pPCache);
57864 sqlite3PcacheClearWritable(pPager->pPCache);
57866 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
57870 /* Drop the WAL write-lock, if any. Also, if the connection was in
57874 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
57876 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
57877 /* This branch is taken when committing a transaction in rollback-journal
57883 assert( pPager->eLock==EXCLUSIVE_LOCK );
57884 rc = pager_truncate(pPager, pPager->dbSize);
57888 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
57892 if( !pPager->exclusiveMode
57893 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
57897 pPager->eState = PAGER_READER;
57898 pPager->setSuper = 0;
57909 ** call to pager_unlock() will discard all in-memory pages, unlock
57911 ** means that there is a hot-journal left in the file-system, the next
57921 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
57923 if( pPager->eState>=PAGER_WRITER_LOCKED ){
57927 }else if( !pPager->exclusiveMode ){
57928 assert( pPager->eState==PAGER_READER );
57936 ** Parameter aData must point to a buffer of pPager->pageSize bytes
57938 ** page of data and the current value of pPager->cksumInit.
57941 ** random initial value (pPager->cksumInit) and every 200th byte
57942 ** of the page data, starting with byte offset (pPager->pageSize%200).
57943 ** Each byte is interpreted as an 8-bit unsigned integer.
57955 u32 cksum = pPager->cksumInit; /* Checksum value to return */
57956 int i = pPager->pageSize-200; /* Loop counter */
57959 i -= 200;
57966 ** from the sub-journal (if isMainJrnl==0) and playback that page.
57970 ** The main rollback journal uses checksums - the statement journal does
57973 ** If the page number of the page record read from the (sub-)journal file
57983 ** If the page record is successfully read from the (sub-)journal file
57985 ** while reading the record from the (sub-)journal file or while writing
57987 ** is successfully read from the (sub-)journal file but appears to be
57991 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
58005 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
58018 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
58019 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
58021 aData = pPager->pTmpSpace;
58027 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
58028 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
58029 ** only reads from the main journal, not the sub-journal.
58031 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
58032 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
58034 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
58036 /* Read the page number and page data from the journal or sub-journal
58039 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
58042 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
58044 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
58055 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
58059 rc = read32bits(jfd, (*pOffset)-4, &cksum);
58075 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
58076 pPager->nReserve = ((u8*)aData)[20];
58083 ** An exception to the above rule: If the database is in no-sync mode
58093 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
58110 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
58120 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
58122 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
58123 (isMainJrnl?"main-journal":"sub-journal")
58126 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
58128 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
58130 if( isOpen(pPager->fd)
58131 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58134 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
58135 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
58139 ** This is usually safe even for an encrypted database - as the data
58141 ** is if the data was just read from an in-memory sub-journal. In that
58144 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
58146 if( pgno>pPager->dbFileSize ){
58147 pPager->dbFileSize = pgno;
58149 if( pPager->pBackup ){
58150 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
58154 ** the database and the page is not in-memory, there is a potential
58155 ** problem. When the page is next fetched by the b-tree layer, it
58161 ** if the page is on the free-list at the start of the transaction, then
58164 ** The solution is to add an in-memory page to the cache containing
58165 ** the data just read from the sub-journal. Mark the page as dirty
58166 ** and if the pager requires a journal-sync, then mark the page as
58167 ** requiring a journal-sync before it is written.
58170 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
58171 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
58173 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
58174 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
58186 pData = pPg->pData;
58187 memcpy(pData, (u8*)aData, pPager->pageSize);
58188 pPager->xReiniter(pPg);
58198 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
58206 ** Parameter zSuper is the name of a super-journal file. A single journal
58207 ** file that referred to the super-journal file has just been rolled back.
58208 ** This routine checks if it is possible to delete the super-journal file,
58214 ** When a super-journal file is created, it is populated with the names
58215 ** of all of its child journals, one after another, formatted as utf-8
58217 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
58220 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
58222 ** A super-journal file may only be deleted once all of its child
58225 ** This function reads the contents of the super-journal file into
58230 ** * if the child journal contains a reference to super-journal
58236 ** the file-system using sqlite3OsDelete().
58244 ** the entire contents of the super-journal file. This could be
58245 ** a couple of kilobytes or so - potentially larger than the page
58249 sqlite3_vfs *pVfs = pPager->pVfs;
58251 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
58252 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
58253 char *zSuperJournal = 0; /* Contents of super-journal file */
58254 i64 nSuperJournal; /* Size of super-journal file */
58256 char *zSuperPtr; /* Space to hold super-journal filename */
58261 ** If successful, open the super-journal file for reading.
58263 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
58270 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
58274 /* Load the entire super-journal file into space obtained from
58276 ** sufficient space (in zSuperPtr) to hold the names of super-journal
58277 ** files extracted from regular rollback-journals.
58281 nSuperPtr = pVfs->mxPathname+1;
58296 while( (zJournal-zSuperJournal)<nSuperJournal ){
58303 /* One of the journals pointed to by the super-journal exists.
58304 ** Open it and check if it points at the super-journal. If
58305 ** so, return without deleting the super-journal file.
58325 /* We have a match. Do not delete the super-journal file. */
58348 ** file in the file-system. This only happens when committing a transaction,
58349 ** or rolling back a transaction (including rolling back a hot-journal).
58352 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
58353 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
58368 assert( pPager->eState!=PAGER_ERROR );
58369 assert( pPager->eState!=PAGER_READER );
58373 if( isOpen(pPager->fd)
58374 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58377 int szPage = pPager->pageSize;
58378 assert( pPager->eLock==EXCLUSIVE_LOCK );
58380 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
58384 rc = sqlite3OsTruncate(pPager->fd, newSize);
58386 char *pTmp = pPager->pTmpSpace;
58388 testcase( (newSize-szPage) == currentSize );
58389 testcase( (newSize-szPage) > currentSize );
58390 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
58391 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
58394 pPager->dbFileSize = nPage;
58402 ** Return a sanitized version of the sector-size of OS file pFile. The
58421 ** super-journal pointers within created journal files.
58425 ** Otherwise, for non-temporary files, the effective sector size is
58432 ** pPager->sectorSize is to define the "blast radius" of bytes that
58440 assert( isOpen(pPager->fd) || pPager->tempFile );
58442 if( pPager->tempFile
58443 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
58449 pPager->sectorSize = 512;
58451 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
58462 ** (2) 4 byte big-endian integer which is the number of valid page records
58465 ** (3) 4 byte big-endian integer which is the initial value for the
58469 ** (5) 4 byte big-endian integer which is the sector size. The header
58471 ** (6) 4 byte big-endian integer which is the page size.
58475 ** + pPager->pageSize bytes of data.
58492 ** no-sync option for the journal. A power failure could lead to corruption
58496 ** If the file opened as the journal file is not a well-formed
58502 ** If an I/O or malloc() error occurs, the journal-file is not deleted
58513 sqlite3_vfs *pVfs = pPager->pVfs;
58520 char *zSuper = 0; /* Name of super-journal file if any */
58523 u32 savedPageSize = pPager->pageSize;
58528 assert( isOpen(pPager->jfd) );
58529 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
58534 /* Read the super-journal name from the journal, if it is present.
58535 ** If a super-journal file name is specified, but the file is not
58541 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
58545 zSuper = pPager->pTmpSpace;
58546 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58554 pPager->journalOff = 0;
58576 ** working in no-sync mode. This means that the rest of the journal
58581 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
58582 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
58600 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
58601 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
58607 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
58612 pPager->dbSize = mxPg;
58613 if( pPager->mxPgno<mxPg ){
58614 pPager->mxPgno = mxPg;
58626 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
58631 pPager->journalOff = szJ;
58657 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
58661 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
58665 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
58669 ** malloc error that occurred after the change-counter was updated but
58670 ** before the transaction was committed, then the change-counter
58673 ** update the change-counter at all. This may lead to cache inconsistency
58677 pPager->changeCountDone = pPager->tempFile;
58680 /* Leave 4 bytes of space before the super-journal filename in memory.
58684 zSuper = &pPager->pTmpSpace[4];
58685 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58689 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58698 /* If there was a super-journal and this routine will return success,
58699 ** see if it is possible to delete the super-journal.
58701 assert( zSuper==&pPager->pTmpSpace[4] );
58702 memset(pPager->pTmpSpace, 0, 4);
58708 nPlayback, pPager->zJournal);
58723 ** pPg->pData. A shared lock or greater must be held on the database
58733 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
58739 assert( pPager->eState>=PAGER_READER && !MEMDB );
58740 assert( isOpen(pPager->fd) );
58743 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
58747 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
58751 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
58752 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
58758 if( pPg->pgno==1 ){
58765 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
58772 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
58774 u8 *dbFileVers = &((u8*)pPg->pData)[24];
58775 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
58779 PAGER_INCR(pPager->nRead);
58780 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
58782 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
58788 ** Update the value of the change-counter at offsets 24 and 92 in
58792 ** routine which only updates the change-counter if the update is actually
58793 ** needed, as determined by the pPager->changeCountDone state variable.
58800 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
58801 put32bits(((char*)pPg->pData)+24, change_counter);
58806 put32bits(((char*)pPg->pData)+92, change_counter);
58807 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
58836 pPager->xReiniter(pPg);
58850 sqlite3BackupRestart(pPager->pBackup);
58869 pPager->dbSize = pPager->dbOrigSize;
58870 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
58871 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58873 PgHdr *pNext = pList->pDirty;
58874 rc = pagerUndoCallback((void *)pPager, pList->pgno);
58900 assert( pPager->pWal );
58904 for(p=pList; p && p->pDirty; p=p->pDirty){
58905 assert( p->pgno < p->pDirty->pgno );
58909 assert( pList->pDirty==0 || isCommit );
58917 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
58918 if( p->pgno<=nTruncate ){
58919 ppNext = &p->pDirty;
58927 pPager->aStat[PAGER_STAT_WRITE] += nList;
58929 if( pList->pgno==1 ) pager_write_changecounter(pList);
58930 rc = sqlite3WalFrames(pPager->pWal,
58931 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
58933 if( rc==SQLITE_OK && pPager->pBackup ){
58934 for(p=pList; p; p=p->pDirty){
58935 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
58940 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58941 for(p=pList; p; p=p->pDirty){
58962 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
58969 sqlite3WalEndReadTransaction(pPager->pWal);
58971 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
58974 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
58993 /* Query the WAL sub-system for the database size. The WalDbsize()
58996 ** available from the WAL sub-system if the log file is empty or
58999 assert( pPager->eState==PAGER_OPEN );
59000 assert( pPager->eLock>=SHARED_LOCK );
59001 assert( isOpen(pPager->fd) );
59002 assert( pPager->tempFile==0 );
59003 nPage = sqlite3WalDbsize(pPager->pWal);
59006 ** WAL sub-system, determine the page count based on the size of
59008 ** integer multiple of the page-size, round up the result.
59010 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
59012 int rc = sqlite3OsFileSize(pPager->fd, &n);
59016 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
59023 if( nPage>pPager->mxPgno ){
59024 pPager->mxPgno = (Pgno)nPage;
59033 ** Check if the *-wal file that corresponds to the database opened by pPager
59034 ** exists if the database is not empy, or verify that the *-wal file does
59037 ** If the database is not empty and the *-wal file exists, open the pager
59038 ** in WAL mode. If the database is empty or if no *-wal file exists and
59046 ** a WAL on a none-empty database, this ensures there is no race condition
59052 assert( pPager->eState==PAGER_OPEN );
59053 assert( pPager->eLock>=SHARED_LOCK );
59055 if( !pPager->tempFile ){
59058 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
59067 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
59069 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
59072 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
59073 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
59083 ** the entire super-journal file. The case pSavepoint==NULL occurs when
59087 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
59100 ** * Pages are then played back from the sub-journal file, starting
59114 ** (or transaction). No page with a page-number greater than this value
59119 i64 iHdrOff; /* End of first segment of main-journal records */
59123 assert( pPager->eState!=PAGER_ERROR );
59124 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59128 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
59137 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
59138 pPager->changeCountDone = pPager->tempFile;
59144 /* Use pPager->journalOff as the effective size of the main rollback
59147 ** past pPager->journalOff is off-limits to us.
59149 szJ = pPager->journalOff;
59155 ** greater than the current database size (pPager->dbSize) but those
59160 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
59161 pPager->journalOff = pSavepoint->iOffset;
59162 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
59163 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59167 pPager->journalOff = 0;
59172 ** of the main journal file. Continue to skip out-of-range pages and
59175 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
59183 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
59188 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
59190 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
59192 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
59193 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59197 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
59199 /* Finally, rollback pages from the sub-journal. Page that were
59201 ** will be skipped. Out-of-range pages are also skipped.
59205 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
59208 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
59210 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
59211 assert( offset==(i64)ii*(4+pPager->pageSize) );
59219 pPager->journalOff = szJ;
59226 ** Change the maximum number of in-memory pages that are allowed
59230 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
59234 ** Change the maximum number of in-memory pages that are allowed
59238 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
59246 sqlite3_file *fd = pPager->fd;
59247 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
59249 sz = pPager->szMmap;
59250 pPager->bUseFetch = (sz>0);
59252 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
59261 pPager->szMmap = szMmap;
59269 sqlite3PcacheShrink(pPager->pPCache);
59291 ** database (with some additional information - the nRec field
59292 ** of the journal header - being written in between the two
59302 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
59313 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
59328 if( pPager->tempFile ){
59329 pPager->noSync = 1;
59330 pPager->fullSync = 0;
59331 pPager->extraSync = 0;
59333 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
59334 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
59335 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
59337 if( pPager->noSync ){
59338 pPager->syncFlags = 0;
59340 pPager->syncFlags = SQLITE_SYNC_FULL;
59342 pPager->syncFlags = SQLITE_SYNC_NORMAL;
59344 pPager->walSyncFlags = (pPager->syncFlags<<2);
59345 if( pPager->fullSync ){
59346 pPager->walSyncFlags |= pPager->syncFlags;
59348 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
59349 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
59352 pPager->doNotSpill &= ~SPILLFLAG_OFF;
59354 pPager->doNotSpill |= SPILLFLAG_OFF;
59395 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
59403 ** The pager invokes the busy-handler if sqlite3OsLock() returns
59404 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
59408 ** (which occurs during hot-journal rollback). Summary:
59411 ** --------------------------------------------------------
59412 ** NO_LOCK -> SHARED_LOCK | Yes
59413 ** SHARED_LOCK -> RESERVED_LOCK | No
59414 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
59415 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
59417 ** If the busy-handler callback returns non-zero, the lock is
59423 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
59427 pPager->xBusyHandler = xBusyHandler;
59428 pPager->pBusyHandlerArg = pBusyHandlerArg;
59429 ap = (void **)&pPager->xBusyHandler;
59432 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
59440 ** is a no-op. The value returned is the error state error code (i.e.
59441 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
59450 ** * the database is either not an in-memory database or it is
59451 ** an in-memory database that currently consists of zero pages.
59475 ** is a no-op for that case anyhow.
59480 if( (pPager->memDb==0 || pPager->dbSize==0)
59481 && sqlite3PcacheRefCount(pPager->pPCache)==0
59482 && pageSize && pageSize!=(u32)pPager->pageSize
59487 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
59488 rc = sqlite3OsFileSize(pPager->fd, &nByte);
59491 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
59503 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
59506 sqlite3PageFree(pPager->pTmpSpace);
59507 pPager->pTmpSpace = pNew;
59508 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
59509 pPager->pageSize = pageSize;
59510 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
59516 *pPageSize = pPager->pageSize;
59518 if( nReserve<0 ) nReserve = pPager->nReserve;
59520 pPager->nReserve = (i16)nReserve;
59535 return pPager->pTmpSpace;
59547 pPager->mxPgno = mxPage;
59549 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
59550 /* assert( pPager->mxPgno>=pPager->dbSize ); */
59554 return pPager->mxPgno;
59562 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
59571 sqlite3_io_error_pending = -1;
59598 assert( isOpen(pPager->fd) || pPager->tempFile );
59606 if( isOpen(pPager->fd) ){
59608 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
59617 ** This function may only be called when a read-transaction is open on
59620 ** However, if the file is between 1 and <page-size> bytes in size, then
59624 assert( pPager->eState>=PAGER_READER );
59625 assert( pPager->eState!=PAGER_WRITER_FINISHED );
59626 *pnPage = (int)pPager->dbSize;
59632 ** a similar or greater lock is already held, this function is a no-op
59647 /* Check that this is either a no-op (because the requested lock is
59648 ** already held), or one of the transitions that the busy-handler
59652 assert( (pPager->eLock>=locktype)
59653 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
59654 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
59659 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
59665 ** following is true for all dirty pages currently in the page-cache:
59671 ** be necessary to write the current content out to the sub-journal.
59680 ** sub-journal rolled back the content could not be restored and the
59686 Pager *pPager = pPg->pPager;
59687 assert( pPg->flags&PGHDR_DIRTY );
59688 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
59689 Pgno pgno = pPg->pgno;
59691 for(i=0; i<pPg->pPager->nSavepoint; i++){
59692 PagerSavepoint *p = &pPager->aSavepoint[i];
59693 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
59698 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
59705 ** Truncate the in-memory database file image to nPage pages. This
59716 assert( pPager->dbSize>=nPage || CORRUPT_DB );
59717 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
59718 pPager->dbSize = nPage;
59733 ** This function is called before attempting a hot-journal rollback. It
59734 ** syncs the journal file to disk, then sets pPager->journalHdr to the
59738 ** Syncing a hot-journal to disk before attempting to roll it back ensures
59739 ** that if a power-failure occurs during the rollback, the process that
59748 if( !pPager->noSync ){
59749 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
59752 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
59776 if( pPager->pMmapFreelist ){
59777 *ppPage = p = pPager->pMmapFreelist;
59778 pPager->pMmapFreelist = p->pDirty;
59779 p->pDirty = 0;
59780 assert( pPager->nExtra>=8 );
59781 memset(p->pExtra, 0, 8);
59783 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
59785 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
59788 p->pExtra = (void *)&p[1];
59789 p->flags = PGHDR_MMAP;
59790 p->nRef = 1;
59791 p->pPager = pPager;
59794 assert( p->pExtra==(void *)&p[1] );
59795 assert( p->pPage==0 );
59796 assert( p->flags==PGHDR_MMAP );
59797 assert( p->pPager==pPager );
59798 assert( p->nRef==1 );
59800 p->pgno = pgno;
59801 p->pData = pData;
59802 pPager->nMmapOut++;
59813 Pager *pPager = pPg->pPager;
59814 pPager->nMmapOut--;
59815 pPg->pDirty = pPager->pMmapFreelist;
59816 pPager->pMmapFreelist = pPg;
59818 assert( pPager->fd->pMethods->iVersion>=3 );
59819 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
59828 for(p=pPager->pMmapFreelist; p; p=pNext){
59829 pNext = p->pDirty;
59836 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
59843 if( pPager->tempFile ) return SQLITE_OK;
59844 if( pPager->dbSize==0 ) return SQLITE_OK;
59845 assert( pPager->zFilename && pPager->zFilename[0] );
59846 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
59848 /* If the HAS_MOVED file-control is unimplemented, assume that the file
59874 u8 *pTmp = (u8*)pPager->pTmpSpace;
59880 /* pPager->errCode = 0; */
59881 pPager->exclusiveMode = 0;
59885 assert( db || pPager->pWal==0 );
59886 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
59891 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
59892 pPager->pWal = 0;
59907 ** back or finalize it. The next database user will have to do hot-journal
59910 if( isOpen(pPager->jfd) ){
59919 sqlite3OsClose(pPager->jfd);
59920 sqlite3OsClose(pPager->fd);
59922 sqlite3PcacheClose(pPager->pPCache);
59923 assert( !pPager->aSavepoint && !pPager->pInJournal );
59924 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
59935 return pPg->pgno;
59949 ** disk and can be restored in the event of a hot-journal rollback.
59951 ** If the Pager.noSync flag is set, then this function is a no-op.
59952 ** Otherwise, the actions required depend on the journal-mode and the
59953 ** device characteristics of the file-system, as follows:
59955 ** * If the journal file is an in-memory journal file, no action need
59961 ** been written following it. If the pager is operating in full-sync
59967 ** Or, in pseudo-code:
59969 ** if( NOT <in-memory journal> ){
59971 ** if( <full-sync mode> ) xSync(<journal file>);
59984 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59985 || pPager->eState==PAGER_WRITER_DBMOD
59993 if( !pPager->noSync ){
59994 assert( !pPager->tempFile );
59995 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
59996 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
59997 assert( isOpen(pPager->jfd) );
60001 ** that wrote to this database was operating in persistent-journal
60004 ** file happens to be a journal-header (written as part of the
60005 ** previous connection's transaction), and a crash or power-failure
60009 ** hot-journal rollback following recovery. It may roll back all
60011 ** out-of-date data that follows it. Database corruption.
60027 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
60030 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
60033 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
60040 ** full-synchronous mode, sync the journal first. This ensures that
60050 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
60053 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
60056 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
60058 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
60065 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
60066 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
60071 pPager->journalHdr = pPager->journalOff;
60073 pPager->nRec = 0;
60078 pPager->journalHdr = pPager->journalOff;
60086 sqlite3PcacheClearSyncFlags(pPager->pPCache);
60087 pPager->eState = PAGER_WRITER_DBMOD;
60095 ** in-memory pages in the list to the database file. The argument may
60097 ** a no-op.
60104 ** If the pager is a temp-file pager and the actual file-system file
60129 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
60130 assert( pPager->eLock==EXCLUSIVE_LOCK );
60131 assert( isOpen(pPager->fd) || pList->pDirty==0 );
60133 /* If the file is a temp-file has not yet been opened, open it now. It
60135 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
60137 if( !isOpen(pPager->fd) ){
60138 assert( pPager->tempFile && rc==SQLITE_OK );
60139 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
60145 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
60147 && pPager->dbHintSize<pPager->dbSize
60148 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
60150 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
60151 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
60152 pPager->dbHintSize = pPager->dbSize;
60156 Pgno pgno = pList->pgno;
60160 ** make the file smaller (presumably by auto-vacuum code). Do not write
60166 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
60167 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
60170 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
60171 if( pList->pgno==1 ) pager_write_changecounter(pList);
60173 pData = pList->pData;
60176 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
60183 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
60185 if( pgno>pPager->dbFileSize ){
60186 pPager->dbFileSize = pgno;
60188 pPager->aStat[PAGER_STAT_WRITE]++;
60191 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
60201 pList = pList->pDirty;
60208 ** Ensure that the sub-journal file is open. If it is already open, this
60209 ** function is a no-op.
60217 if( !isOpen(pPager->sjfd) ){
60222 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
60223 nStmtSpill = -1;
60225 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
60231 ** Append a record of the current state of page pPg to the sub-journal.
60233 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
60237 ** error code if the attempt to write to the sub-journal fails, or
60243 Pager *pPager = pPg->pPager;
60244 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
60246 /* Open the sub-journal, if it has not already been opened */
60247 assert( pPager->useJournal );
60248 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
60249 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
60252 || pPg->pgno>pPager->dbOrigSize
60256 /* If the sub-journal was opened successfully (or was already open),
60259 void *pData = pPg->pData;
60260 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
60263 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
60264 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
60266 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
60271 pPager->nSubRec++;
60272 assert( pPager->nSavepoint>0 );
60273 rc = addToSavepointBitvecs(pPager, pPg->pgno);
60288 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
60308 assert( pPg->pPager==pPager );
60309 assert( pPg->flags&PGHDR_DIRTY );
60327 if( NEVER(pPager->errCode) ) return SQLITE_OK;
60328 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
60329 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
60330 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
60331 if( pPager->doNotSpill
60332 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
60333 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
60338 pPager->aStat[PAGER_STAT_SPILL]++;
60339 pPg->pDirty = 0;
60349 if( pPager->tempFile==0 ){
60350 rc = sqlite3JournalCreate(pPager->jfd);
60356 if( pPg->flags&PGHDR_NEED_SYNC
60357 || pPager->eState==PAGER_WRITER_CACHEMOD
60364 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
60371 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
60382 int rc = pPager->errCode;
60384 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
60387 PgHdr *pNext = pList->pDirty;
60388 if( pList->nRef==0 ){
60404 ** If zFilename is NULL then a randomly-named temporary file is created
60408 ** This can be used to implement an in-memory database.
60434 int nExtra, /* Extra bytes append to each in-memory page */
60442 int tempFile = 0; /* True for temp files (incl. in-memory files) */
60443 int memDb = 0; /* True if this is an in-memory file */
60449 int readOnly = 0; /* True if this is a read-only file */
60459 /* Figure out how much space is required for each journal file-handle
60460 ** (there are two of them, the main journal and the sub-journal). */
60484 nPathname = pVfs->mxPathname+1;
60506 nUriByte = (int)(&z[1] - zUri);
60508 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
60510 ** the database being opened will be more than pVfs->mxPathname
60513 ** check for a hot-journal before reading.
60529 ** Database file handle (pVfs->szOsFile bytes)
60530 ** Sub-journal file handle (journalFileSize bytes)
60540 ** Some 3rd-party software, over which we have no control, depends on
60544 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
60547 ** filename format expected by 3rd-party software should be as follows:
60549 ** - Main Database Path
60550 ** - \0
60551 ** - Multiple URI components consisting of:
60552 ** - Key
60553 ** - \0
60554 ** - Value
60555 ** - \0
60556 ** - \0
60557 ** - Journal Path
60558 ** - \0
60559 ** - WAL Path (zWALName)
60560 ** - \0
60570 ROUND8(pVfs->szOsFile) + /* The main db file */
60588 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
60589 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
60590 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60591 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60592 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
60597 pPager->zFilename = (char*)pPtr;
60610 pPager->zJournal = (char*)pPtr;
60612 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
60614 sqlite3FileSuffix3(zFilename,pPager->zJournal);
60615 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
60618 pPager->zJournal = 0;
60624 pPager->zWal = (char*)pPtr;
60626 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
60628 sqlite3FileSuffix3(zFilename, pPager->zWal);
60629 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
60632 pPager->zWal = 0;
60638 pPager->pVfs = pVfs;
60639 pPager->vfsFlags = vfsFlags;
60645 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
60648 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
60661 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
60665 if( szPageDflt<pPager->sectorSize ){
60666 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
60669 szPageDflt = (u32)pPager->sectorSize;
60686 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
60688 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
60698 ** This branch is also run for an in-memory database. An in-memory
60699 ** database is the same as a temp-file that is never written out to
60700 ** disk and uses an in-memory rollback journal.
60706 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
60707 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
60708 pPager->noLock = 1; /* Do no locking */
60716 assert( pPager->memDb==0 );
60717 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
60726 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
60732 sqlite3OsClose(pPager->fd);
60733 sqlite3PageFree(pPager->pTmpSpace);
60738 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
60739 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
60741 pPager->useJournal = (u8)useJournal;
60742 /* pPager->stmtOpen = 0; */
60743 /* pPager->stmtInUse = 0; */
60744 /* pPager->nRef = 0; */
60745 /* pPager->stmtSize = 0; */
60746 /* pPager->stmtJSize = 0; */
60747 /* pPager->nPage = 0; */
60748 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
60749 /* pPager->state = PAGER_UNLOCK; */
60750 /* pPager->errMask = 0; */
60751 pPager->tempFile = (u8)tempFile;
60755 pPager->exclusiveMode = (u8)tempFile;
60756 pPager->changeCountDone = pPager->tempFile;
60757 pPager->memDb = (u8)memDb;
60758 pPager->readOnly = (u8)readOnly;
60759 assert( useJournal || pPager->tempFile );
60761 /* pPager->pFirst = 0; */
60762 /* pPager->pFirstSynced = 0; */
60763 /* pPager->pLast = 0; */
60764 pPager->nExtra = (u16)nExtra;
60765 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
60766 assert( isOpen(pPager->fd) || tempFile );
60769 pPager->journalMode = PAGER_JOURNALMODE_OFF;
60771 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
60773 /* pPager->xBusyHandler = 0; */
60774 /* pPager->pBusyHandlerArg = 0; */
60775 pPager->xReiniter = xReinit;
60777 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
60778 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
60791 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
60792 zName--;
60794 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
60795 return pPager->fd;
60802 ** the file-system for the given pager. A hot journal is one that
60803 ** needs to be played back. According to this function, a hot-journal
60817 ** This routine does not check if there is a super-journal filename
60818 ** at the end of the file. If there is, and that super-journal file
60820 ** case this routine will return a false-positive. The pager_playback()
60824 ** If a hot-journal file is found to exist, *pExists is set to 1 and
60825 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
60827 ** to determine whether or not a hot-journal file exists, the IO error
60831 sqlite3_vfs * const pVfs = pPager->pVfs;
60834 int jrnlOpen = !!isOpen(pPager->jfd);
60836 assert( pPager->useJournal );
60837 assert( isOpen(pPager->fd) );
60838 assert( pPager->eState==PAGER_OPEN );
60840 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
60846 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
60856 ** in fact there is none. This results in a false-positive which will
60859 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
60863 assert( pPager->tempFile==0 );
60877 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
60878 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
60884 ** at least one non-zero bytes at the start of the journal file.
60890 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
60894 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
60899 sqlite3OsClose(pPager->jfd);
60926 ** has been successfully called. If a shared-lock is already held when
60927 ** this function is called, it is a no-op.
60934 ** the SHARED lock, the file-system is checked for a hot-journal,
60935 ** which is played back if present. Following any hot-journal
60937 ** the 'change-counter' field of the database file header and
60940 ** 2) If the pager is running in exclusive-mode, and there are currently
60947 ** occurs while locking the database, checking for a hot-journal file or
60953 /* This routine is only called from b-tree and only when there are no
60957 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
60959 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
60960 assert( pPager->errCode==SQLITE_OK );
60962 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
60963 int bHotJournal = 1; /* True if there exists a hot journal-file */
60966 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
60970 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
60977 if( pPager->eLock<=SHARED_LOCK ){
60984 if( pPager->readOnly ){
60994 ** hot-journal back.
61011 ** in exclusive-access mode the file descriptor will be kept open
61012 ** and possibly used for a transaction later on. Also, write-access
61019 ** may mean that the pager was in the error-state when this
61022 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61023 sqlite3_vfs * const pVfs = pPager->pVfs;
61026 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
61030 assert( !pPager->tempFile );
61031 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
61032 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61035 sqlite3OsClose(pPager->jfd);
61042 ** playing back the hot-journal so that we don't end up with
61048 if( isOpen(pPager->jfd) ){
61052 rc = pager_playback(pPager, !pPager->tempFile);
61053 pPager->eState = PAGER_OPEN;
61055 }else if( !pPager->exclusiveMode ){
61061 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
61080 assert( pPager->eState==PAGER_OPEN );
61081 assert( (pPager->eLock==SHARED_LOCK)
61082 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
61086 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
61087 /* The shared-lock has just been acquired then check to
61091 ** single unnecessary sqlite3OsRead() call at the start-up.
61095 ** a 32-bit counter that is incremented with each change. The
61103 char dbFileVers[sizeof(pPager->dbFileVers)];
61106 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
61114 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
61124 sqlite3OsUnfetch(pPager->fd, 0, 0);
61129 /* If there is a WAL file in the file-system, open this database in WAL
61130 ** mode. Otherwise, the following function call is a no-op.
61134 assert( pPager->pWal==0 || rc==SQLITE_OK );
61143 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
61144 rc = pagerPagecount(pPager, &pPager->dbSize);
61151 assert( pPager->eState==PAGER_OPEN );
61153 pPager->eState = PAGER_READER;
61154 pPager->hasHeldSharedLock = 1;
61165 ** nothing to rollback, so this routine is a no-op.
61168 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
61169 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
61182 ** getPageNormal() -- The normal getter
61183 ** getPageError() -- Used if the pager is in an error state
61184 ** getPageMmap() -- Used if memory-mapped I/O is enabled
61206 ** a) When reading a free-list leaf page from the database, and
61224 ** to find a page in the in-memory cache first. If the page is not already
61226 ** just returns 0. This routine acquires a read-lock the first time it
61242 assert( pPager->errCode==SQLITE_OK );
61243 assert( pPager->eState>=PAGER_READER );
61245 assert( pPager->hasHeldSharedLock==1 );
61248 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
61251 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
61258 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
61260 assert( pPg->pgno==pgno );
61261 assert( pPg->pPager==pPager || pPg->pPager==0 );
61264 if( pPg->pPager && !noContent ){
61268 pPager->aStat[PAGER_STAT_HIT]++;
61283 pPg->pPager = pPager;
61285 assert( !isOpen(pPager->fd) || !MEMDB );
61286 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
61287 if( pgno>pPager->mxPgno ){
61289 if( pgno<=pPager->dbSize ){
61296 /* Failure to set the bits in the InJournal bit-vectors is benign.
61303 if( pgno<=pPager->dbOrigSize ){
61304 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
61311 memset(pPg->pData, 0, pPager->pageSize);
61314 assert( pPg->pPager==pPager );
61315 pPager->aStat[PAGER_STAT_MISS]++;
61336 /* The page getter for when memory-mapped I/O is enabled */
61347 /* It is acceptable to use a read-only (mmap) page for any page except
61348 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
61350 ** temporary or in-memory database. */
61352 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
61364 assert( pPager->eState>=PAGER_READER );
61366 assert( pPager->hasHeldSharedLock==1 );
61367 assert( pPager->errCode==SQLITE_OK );
61370 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
61378 rc = sqlite3OsFetch(pPager->fd,
61379 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
61382 if( pPager->eState>PAGER_READER || pPager->tempFile ){
61388 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
61414 assert( pPager->errCode!=SQLITE_OK );
61416 return pPager->errCode;
61429 return pPager->xGet(pPager, pgno, ppPage, flags);
61433 ** Acquire a page if it is already in the in-memory cache. Do
61447 assert( pPager->pPCache!=0 );
61448 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
61449 assert( pPage==0 || pPager->hasHeldSharedLock );
61451 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
61461 ** The assert() at tag-20230419-2 proves that this constraint is always
61469 TESTONLY( Pager *pPager = pPg->pPager; )
61471 if( pPg->flags & PGHDR_MMAP ){
61472 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
61478 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
61486 assert( pPg->pgno==1 );
61487 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
61488 pPager = pPg->pPager;
61499 ** to the start of it. If there are active savepoints, open the sub-journal
61517 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
61519 assert( pPager->eState==PAGER_WRITER_LOCKED );
61521 assert( pPager->pInJournal==0 );
61523 /* If already in the error state, this function is a no-op. But on
61526 if( NEVER(pPager->errCode) ) return pPager->errCode;
61528 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61529 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
61530 if( pPager->pInJournal==0 ){
61535 if( !isOpen(pPager->jfd) ){
61536 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
61537 sqlite3MemJournalOpen(pPager->jfd);
61542 if( pPager->tempFile ){
61556 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
61560 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61565 ** the sub-journal if necessary.
61569 pPager->nRec = 0;
61570 pPager->journalOff = 0;
61571 pPager->setSuper = 0;
61572 pPager->journalHdr = 0;
61578 sqlite3BitvecDestroy(pPager->pInJournal);
61579 pPager->pInJournal = 0;
61580 pPager->journalOff = 0;
61582 assert( pPager->eState==PAGER_WRITER_LOCKED );
61583 pPager->eState = PAGER_WRITER_CACHEMOD;
61590 ** Begin a write-transaction on the specified pager object. If a
61591 ** write-transaction has already been opened, this function is a no-op.
61598 ** If the subjInMemory argument is non-zero, then any sub-journal opened
61599 ** within this transaction will be opened as an in-memory file. This
61600 ** has no effect if the sub-journal is already opened (as it may be when
61602 ** sub-journal. If the subjInMemory argument is zero, then any required
61603 ** sub-journal is implemented in-memory if pPager is an in-memory database,
61609 if( pPager->errCode ) return pPager->errCode;
61610 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
61611 pPager->subjInMemory = (u8)subjInMemory;
61613 if( pPager->eState==PAGER_READER ){
61614 assert( pPager->pInJournal==0 );
61620 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
61625 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
61630 ** The busy-handler is not invoked if another connection already
61631 ** holds the write-lock. If possible, the upper layer will call it.
61633 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
61637 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
61652 ** transactions may copy data from the sub-journal into the database
61656 pPager->eState = PAGER_WRITER_LOCKED;
61657 pPager->dbHintSize = pPager->dbSize;
61658 pPager->dbFileSize = pPager->dbSize;
61659 pPager->dbOrigSize = pPager->dbSize;
61660 pPager->journalOff = 0;
61663 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
61664 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
61676 Pager *pPager = pPg->pPager;
61680 i64 iOff = pPager->journalOff;
61685 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
61687 assert( pPager->journalHdr<=pPager->journalOff );
61688 pData2 = pPg->pData;
61692 ** page in the block above, set the need-sync flag for the page.
61698 pPg->flags |= PGHDR_NEED_SYNC;
61700 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
61702 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
61704 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
61707 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
61708 pPager->journalOff, pPager->pageSize));
61711 PAGERID(pPager), pPg->pgno,
61712 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
61714 pPager->journalOff += 8 + pPager->pageSize;
61715 pPager->nRec++;
61716 assert( pPager->pInJournal!=0 );
61717 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
61720 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
61727 ** main journal or sub-journal as required. If the page is written into
61733 Pager *pPager = pPg->pPager;
61736 /* This routine is not called unless a write-transaction has already
61740 assert( pPager->eState==PAGER_WRITER_LOCKED
61741 || pPager->eState==PAGER_WRITER_CACHEMOD
61742 || pPager->eState==PAGER_WRITER_DBMOD
61745 assert( pPager->errCode==0 );
61746 assert( pPager->readOnly==0 );
61750 ** obtained the necessary locks to begin the write-transaction, but the
61758 if( pPager->eState==PAGER_WRITER_LOCKED ){
61762 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
61772 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
61773 if( pPager->pInJournal!=0
61774 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
61777 if( pPg->pgno<=pPager->dbOrigSize ){
61783 if( pPager->eState!=PAGER_WRITER_DBMOD ){
61784 pPg->flags |= PGHDR_NEED_SYNC;
61787 PAGERID(pPager), pPg->pgno,
61788 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
61792 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
61797 pPg->flags |= PGHDR_WRITEABLE;
61802 if( pPager->nSavepoint>0 ){
61807 if( pPager->dbSize<pPg->pgno ){
61808 pPager->dbSize = pPg->pgno;
61831 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
61832 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
61839 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
61840 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
61842 /* This trick assumes that both the page-size and sector-size are
61846 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
61848 nPageCount = pPager->dbSize;
61849 if( pPg->pgno>nPageCount ){
61850 nPage = (pPg->pgno - pg1)+1;
61851 }else if( (pg1+nPagePerSector-1)>nPageCount ){
61852 nPage = nPageCount+1-pg1;
61857 assert(pg1<=pPg->pgno);
61858 assert((pg1+nPage)>pPg->pgno);
61863 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
61868 if( pPage->flags&PGHDR_NEED_SYNC ){
61875 if( pPage->flags&PGHDR_NEED_SYNC ){
61893 pPage->flags |= PGHDR_NEED_SYNC;
61899 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
61900 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
61912 ** fit on a single disk sector. In this case all co-resident pages
61919 Pager *pPager = pPg->pPager;
61920 assert( (pPg->flags & PGHDR_MMAP)==0 );
61921 assert( pPager->eState>=PAGER_WRITER_LOCKED );
61923 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
61924 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
61926 }else if( pPager->errCode ){
61927 return pPager->errCode;
61928 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
61929 assert( pPager->tempFile==0 );
61943 return pPg->flags & PGHDR_WRITEABLE;
61961 ** This optimization cannot be used with a temp-file, as the page may
61968 Pager *pPager = pPg->pPager;
61969 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
61970 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
61971 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
61972 pPg->flags |= PGHDR_DONT_WRITE;
61973 pPg->flags &= ~PGHDR_WRITEABLE;
61974 testcase( pPg->flags & PGHDR_NEED_SYNC );
61981 ** change-counter, stored as a 4-byte big-endian integer starting at
61985 ** But this only happens if the pPager->changeCountDone flag is false.
61995 ** The isDirectMode flag may only be non-zero if the library was compiled
61997 ** if isDirect is non-zero, then the database file is updated directly
62004 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62005 || pPager->eState==PAGER_WRITER_DBMOD
62010 ** atomic-write optimization is enabled in this build, then isDirect
62014 ** The idea is that if the atomic-write optimization is not
62027 if( !pPager->changeCountDone && pPager->dbSize>0 ){
62030 assert( !pPager->tempFile && isOpen(pPager->fd) );
62037 ** operating in direct-mode, make page 1 writable. When not in
62039 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
62052 assert( pPager->dbFileSize>0 );
62053 zBuf = pPgHdr->pData;
62055 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
62056 pPager->aStat[PAGER_STAT_WRITE]++;
62059 /* Update the pager's copy of the change-counter. Otherwise, the
62061 ** flushed (as the change-counter values will not match). */
62063 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
62064 pPager->changeCountDone = 1;
62067 pPager->changeCountDone = 1;
62078 ** Sync the database file to disk. This is a no-op for in-memory databases
62081 ** If successful, or if called on a pager for which it is a no-op, this
62087 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
62089 if( rc==SQLITE_OK && !pPager->noSync ){
62091 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
62097 ** This function may only be called while a write-transaction is active in
62098 ** rollback. If the connection is in WAL mode, this call is a no-op.
62108 int rc = pPager->errCode;
62111 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62112 || pPager->eState==PAGER_WRITER_DBMOD
62113 || pPager->eState==PAGER_WRITER_LOCKED
62125 ** of a super-journal file that should be written into the individual
62127 ** super-journal (a single database transaction).
62131 ** * The database file change-counter is updated,
62132 ** * the journal is synced (unless the atomic-write optimization is used),
62139 ** delete the super-journal file if specified).
62144 ** If the final parameter - noSync - is true, then the database file itself
62151 const char *zSuper, /* If not NULL, the super-journal name */
62156 assert( pPager->eState==PAGER_WRITER_LOCKED
62157 || pPager->eState==PAGER_WRITER_CACHEMOD
62158 || pPager->eState==PAGER_WRITER_DBMOD
62159 || pPager->eState==PAGER_ERROR
62164 if( NEVER(pPager->errCode) ) return pPager->errCode;
62170 pPager->zFilename, zSuper, pPager->dbSize));
62173 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
62175 assert( MEMDB==0 || pPager->tempFile );
62176 assert( isOpen(pPager->fd) || pPager->tempFile );
62178 /* If this is an in-memory db, or no pages have been written to, or this
62179 ** function has already been called, it is mostly a no-op. However, any
62181 sqlite3BackupRestart(pPager->pBackup);
62186 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62189 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
62192 pList->pDirty = 0;
62196 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
62200 sqlite3PcacheCleanAll(pPager->pPCache);
62203 /* The bBatch boolean is true if the batch-atomic-write commit method
62204 ** should be used. No rollback journal is created if batch-atomic-write
62208 sqlite3_file *fd = pPager->fd;
62211 && !pPager->noSync
62212 && sqlite3JournalIsInMemory(pPager->jfd);
62218 /* The following block updates the change-counter. Exactly how it
62219 ** does this depends on whether or not the atomic-update optimization
62223 ** * The file-system supports the atomic-write property for
62224 ** blocks of size page-size, and
62225 ** * This commit is not part of a multi-file transaction, and
62230 ** counter in 'indirect-mode'. If the optimization is compiled in but
62233 ** pager_incr_changecounter() to update the change-counter in indirect
62237 ** then call pager_incr_changecounter() to update the change-counter
62243 assert( isOpen(pPager->jfd)
62244 || pPager->journalMode==PAGER_JOURNALMODE_OFF
62245 || pPager->journalMode==PAGER_JOURNALMODE_WAL
62247 if( !zSuper && isOpen(pPager->jfd)
62248 && pPager->journalOff==jrnlBufferSize(pPager)
62249 && pPager->dbSize>=pPager->dbOrigSize
62250 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
62252 /* Update the db file change counter via the direct-write method. The
62253 ** following call will modify the in-memory representation of page 1
62255 ** directly to the database file. Because of the atomic-write
62256 ** property of the host file-system, this is safe.
62260 rc = sqlite3JournalCreate(pPager->jfd);
62269 rc = sqlite3JournalCreate(pPager->jfd);
62278 /* Write the super-journal name into the journal file. If a
62279 ** super-journal file name has already been written to the journal file,
62280 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
62286 ** If the atomic-update optimization is being used, this sync will not
62289 ** Because the change-counter page was just modified, unless the
62290 ** atomic-update optimization is used it is almost certain that the
62294 ** xSync() call will be changed to a no-op by the OS anyhow.
62299 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62314 rc = sqlite3JournalCreate(pPager->jfd);
62316 sqlite3OsClose(pPager->jfd);
62321 sqlite3OsClose(pPager->jfd);
62333 sqlite3PcacheCleanAll(pPager->pPCache);
62338 ** last page in the db image moved to the free-list. In this case the
62341 if( pPager->dbSize>pPager->dbFileSize ){
62342 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
62343 assert( pPager->eState==PAGER_WRITER_DBMOD );
62358 pPager->eState = PAGER_WRITER_FINISHED;
62367 ** synced to disk. The journal file still exists in the file-system
62369 ** be used as a hot-journal and the current transaction rolled back.
62373 ** for hot-journal rollback. Once this is done the transaction is
62385 if( NEVER(pPager->errCode) ) return pPager->errCode;
62386 pPager->iDataVersion++;
62388 assert( pPager->eState==PAGER_WRITER_LOCKED
62389 || pPager->eState==PAGER_WRITER_FINISHED
62390 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
62395 ** this transaction, the pager is running in exclusive-mode and is
62396 ** using persistent journals, then this function is a no-op.
62400 ** a hot-journal during hot-journal rollback, 0 changes will be made
62405 if( pPager->eState==PAGER_WRITER_LOCKED
62406 && pPager->exclusiveMode
62407 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
62409 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
62410 pPager->eState = PAGER_READER;
62415 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
62421 ** transaction are reverted and the current write-transaction is closed.
62431 ** in-memory cache pages to the state they were in when the transaction
62440 ** In WAL mode, all cache-entries containing data modified within the
62442 ** their pre-transaction state by re-reading data from the database or
62449 /* PagerRollback() is a no-op if called in READER or OPEN state. If
62454 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
62455 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
62459 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
62460 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
62462 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
62463 int eState = pPager->eState;
62470 pPager->errCode = SQLITE_ABORT;
62471 pPager->eState = PAGER_ERROR;
62479 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
62492 ** Return TRUE if the database file is opened read-only. Return FALSE
62496 return pPager->readOnly;
62504 return sqlite3PcacheRefCount(pPager->pPCache);
62513 int perPageSize = pPager->pageSize + pPager->nExtra
62515 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
62517 + pPager->pageSize;
62533 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
62534 a[1] = sqlite3PcachePagecount(pPager->pPCache);
62535 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
62536 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
62537 a[4] = pPager->eState;
62538 a[5] = pPager->errCode;
62539 a[6] = pPager->aStat[PAGER_STAT_HIT];
62540 a[7] = pPager->aStat[PAGER_STAT_MISS];
62541 a[8] = 0; /* Used to be pPager->nOvfl */
62542 a[9] = pPager->nRead;
62543 a[10] = pPager->aStat[PAGER_STAT_WRITE];
62556 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
62572 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
62573 *pnVal += pPager->aStat[eStat];
62575 pPager->aStat[eStat] = 0;
62580 ** Return true if this is an in-memory or temp-file backed pager.
62583 return pPager->tempFile || pPager->memVfs;
62590 ** equal to nSavepoint, then this function is a no-op.
62593 ** occurs while opening the sub-journal file, then an IO error code is
62598 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
62602 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62604 assert( nSavepoint>nCurrent && pPager->useJournal );
62611 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
62616 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
62617 pPager->aSavepoint = aNew;
62621 aNew[ii].nOrig = pPager->dbSize;
62622 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
62623 aNew[ii].iOffset = pPager->journalOff;
62627 aNew[ii].iSubRec = pPager->nSubRec;
62628 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
62634 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
62636 pPager->nSavepoint = ii+1;
62638 assert( pPager->nSavepoint==nSavepoint );
62643 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62646 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
62666 ** (the first created). A value of (Pager.nSavepoint-1) means operate
62668 ** (Pager.nSavepoint-1), then this function is a no-op.
62685 int rc = pPager->errCode;
62694 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
62703 for(ii=nNew; ii<pPager->nSavepoint; ii++){
62704 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
62706 pPager->nSavepoint = nNew;
62708 /* Truncate the sub-journal so that it only includes the parts
62711 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
62712 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
62713 /* Only truncate if it is an in-memory sub-journal. */
62714 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
62715 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
62716 rc = sqlite3OsTruncate(pPager->sjfd, sz);
62719 pPager->nSubRec = pRel->iSubRec;
62723 ** If this is a temp-file, it is possible that the journal file has
62727 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
62728 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
62739 pPager->journalMode==PAGER_JOURNALMODE_OFF
62740 && pPager->eState>=PAGER_WRITER_CACHEMOD
62742 pPager->errCode = SQLITE_ABORT;
62743 pPager->eState = PAGER_ERROR;
62755 ** Except, if the pager is in-memory only, then return an empty string if
62759 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
62760 ** participate in shared-cache.
62767 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
62770 return pPager->zFilename;
62778 return pPager->pVfs;
62787 return pPager->fd;
62796 return pPager->jfd;
62798 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
62806 return pPager->zJournal;
62819 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
62827 ** If the fourth argument, isCommit, is non-zero, then this page is being
62837 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
62841 assert( pPg->nRef>0 );
62842 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62843 || pPager->eState==PAGER_WRITER_DBMOD
62847 /* In order to be able to rollback, an in-memory database must journal
62850 assert( pPager->tempFile || !MEMDB );
62851 if( pPager->tempFile ){
62858 ** sub-journal now. This is required to handle the following scenario:
62866 ** If page X were not written to the sub-journal here, it would not
62870 ** subjournalPage() may need to allocate space to store pPg->pgno into
62874 if( (pPg->flags & PGHDR_DIRTY)!=0
62881 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
62882 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
62884 /* If the journal needs to be sync()ed before page pPg->pgno can
62885 ** be written to, store pPg->pgno in local variable needSyncPgno.
62888 ** the journal needs to be sync()ed before database page pPg->pgno
62891 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
62892 needSyncPgno = pPg->pgno;
62893 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
62894 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
62895 assert( pPg->flags&PGHDR_DIRTY );
62898 /* If the cache contains a page with page-number pgno, remove it
62903 pPg->flags &= ~PGHDR_NEED_SYNC;
62905 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
62907 if( NEVER(pPgOld->nRef>1) ){
62911 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
62912 if( pPager->tempFile ){
62913 /* Do not discard pages from an in-memory database since we might
62915 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
62921 origPgno = pPg->pgno;
62925 /* For an in-memory database, make sure the original page continues
62929 if( pPager->tempFile && pPgOld ){
62935 /* If needSyncPgno is non-zero, then the journal file needs to be
62937 ** Currently, no such page exists in the page-cache and the
62939 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
62942 ** If the attempt to load the page into the page-cache fails, (due
62952 if( needSyncPgno<=pPager->dbOrigSize ){
62953 assert( pPager->pTmpSpace!=0 );
62954 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
62958 pPgHdr->flags |= PGHDR_NEED_SYNC;
62974 assert( pPg->pgno!=iNew );
62975 pPg->flags = flags;
62983 assert( pPg->nRef>0 || pPg->pPager->memDb );
62984 return pPg->pData;
62992 return pPg->pExtra;
62996 ** Get/set the locking-mode for this pager. Parameter eMode must be one
62999 ** the locking-mode is set to the value specified.
63003 ** locking-mode.
63011 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
63012 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
63013 pPager->exclusiveMode = (u8)eMode;
63015 return (int)pPager->exclusiveMode;
63019 ** Set the journal-mode for this pager. Parameter eMode must be one of:
63031 ** * An in-memory database can only have its journal_mode set to _OFF
63036 ** The returned indicate the current (possibly updated) journal-mode.
63039 u8 eOld = pPager->journalMode; /* Prior journalmode */
63053 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
63055 /* Do allow the journalmode of an in-memory database to be set to
63068 assert( pPager->eState!=PAGER_ERROR );
63069 pPager->journalMode = (u8)eMode;
63082 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
63083 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
63092 sqlite3OsClose(pPager->jfd);
63093 if( pPager->eLock>=RESERVED_LOCK ){
63094 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63097 int state = pPager->eState;
63102 if( pPager->eState==PAGER_READER ){
63107 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63114 assert( state==pPager->eState );
63117 sqlite3OsClose(pPager->jfd);
63122 return (int)pPager->journalMode;
63129 return (int)pPager->journalMode;
63139 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
63140 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
63145 ** Get/set the size-limit used for persistent journal files.
63147 ** Setting the size limit to -1 means no limit is enforced.
63148 ** An attempt to set a limit smaller than -1 is a no-op.
63151 if( iLimit>=-1 ){
63152 pPager->journalSizeLimit = iLimit;
63153 sqlite3WalLimit(pPager->pWal, iLimit);
63155 return pPager->journalSizeLimit;
63159 ** Return a pointer to the pPager->pBackup variable. The backup module
63165 return &pPager->pBackup;
63170 ** Unless this is an in-memory or temporary database, clear the pager cache.
63173 assert( MEMDB==0 || pPager->tempFile );
63174 if( pPager->tempFile==0 ) pager_reset(pPager);
63195 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
63207 if( pPager->pWal ){
63208 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
63209 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
63210 pPager->pBusyHandlerArg,
63211 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
63219 return sqlite3WalCallback(pPager->pWal);
63224 ** primitives necessary for write-ahead logging.
63227 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
63228 if( pPager->noLock ) return 0;
63229 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
63240 assert( pPager->eLock>=SHARED_LOCK );
63241 eOrigLock = pPager->eLock;
63254 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
63255 ** lock on the database file and use heap-memory to store the wal-index
63256 ** in. Otherwise, use the normal shared-memory.
63261 assert( pPager->pWal==0 && pPager->tempFile==0 );
63262 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63264 /* If the pager is already in exclusive-mode, the WAL module will use
63265 ** heap-memory for the wal-index instead of the VFS shared-memory
63269 if( pPager->exclusiveMode ){
63277 rc = sqlite3WalOpen(pPager->pVfs,
63278 pPager->fd, pPager->zWal, pPager->exclusiveMode,
63279 pPager->journalSizeLimit, &pPager->pWal
63293 ** file (not a temp file or an in-memory database), and the WAL file
63299 ** If the pager is open on a temp-file (or in-memory database), or if
63305 int *pbOpen /* OUT: Set to true if call is a no-op */
63310 assert( pPager->eState==PAGER_OPEN || pbOpen );
63311 assert( pPager->eState==PAGER_READER || !pbOpen );
63313 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
63315 if( !pPager->tempFile && !pPager->pWal ){
63319 sqlite3OsClose(pPager->jfd);
63323 pPager->journalMode = PAGER_JOURNALMODE_WAL;
63324 pPager->eState = PAGER_OPEN;
63345 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
63347 /* If the log file is not already open, but does exist in the file-system,
63351 if( !pPager->pWal ){
63356 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
63365 ** the database file, the log and log-summary files will be deleted.
63367 if( rc==SQLITE_OK && pPager->pWal ){
63370 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
63371 pPager->pageSize, (u8*)pPager->pTmpSpace);
63372 pPager->pWal = 0;
63374 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
63382 ** If pager pPager is a wal-mode database not in exclusive locking mode,
63389 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
63390 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
63401 sqlite3WalDb(pPager->pWal, db);
63413 if( pPager->pWal ){
63414 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
63429 if( pPager->pWal ){
63430 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
63443 if( pPager->pWal ){
63444 rc = sqlite3WalSnapshotRecover(pPager->pWal);
63465 if( pPager->pWal ){
63466 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
63478 assert( pPager->pWal );
63479 sqlite3WalSnapshotUnlock(pPager->pWal);
63487 ** A read-lock must be held on the pager when this function is called. If
63494 assert( pPager->eState>=PAGER_READER );
63495 return sqlite3WalFramesize(pPager->pWal);
63515 ** This file contains the implementation of a write-ahead log (WAL) used in
63518 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
63537 ** big-endian 32-bit unsigned integer values:
63543 ** 16: Salt-1, random integer incremented with each checkpoint
63544 ** 20: Salt-2, a different random integer changing with each ckpt
63545 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
63546 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
63548 ** Immediately following the wal-header are zero or more frames. Each
63549 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
63550 ** of page data. The frame-header is six big-endian 32-bit unsigned
63556 ** 8: Salt-1 (copied from the header)
63557 ** 12: Salt-2 (copied from the header)
63558 ** 16: Checksum-1.
63559 ** 20: Checksum-2.
63564 ** (1) The salt-1 and salt-2 values in the frame-header match
63565 ** salt values in the wal-header
63567 ** (2) The checksum values in the final 8 bytes of the frame-header
63572 ** The checksum is computed using 32-bit big-endian integers if the
63574 ** is computed using little-endian if the magic number is 0x377f0682.
63576 ** big-endian format regardless of which byte order is used to compute
63578 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
63581 ** for i from 0 to n-1 step 2:
63588 ** of the sequence being summed.) The s1 value spans all 32-bit
63591 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
63592 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
63593 ** The VFS.xSync operations serve as write barriers - all writes launched
63597 ** After each checkpoint, the salt-1 value is incremented and the salt-2
63626 ** data structure called the wal-index is maintained to expedite the
63629 ** WAL-INDEX FORMAT
63631 ** Conceptually, the wal-index is shared memory, though VFS implementations
63632 ** might choose to implement the wal-index using a mmapped file. Because
63633 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
63637 ** In the default unix and windows implementation, the wal-index is a mmapped
63638 ** file whose name is the database name with a "-shm" suffix added. For that
63639 ** reason, the wal-index is sometimes called the "shm" file.
63641 ** The wal-index is transient. After a crash, the wal-index can (and should
63643 ** to either truncate or zero the header of the wal-index when the last
63644 ** connection to it closes. Because the wal-index is transient, it can
63645 ** use an architecture-specific format; it does not have to be cross-platform.
63647 ** as big endian, the wal-index can store multi-byte values in the native
63650 ** The purpose of the wal-index is to answer this question quickly: Given
63655 ** The wal-index consists of a header region, followed by an one or
63658 ** The wal-index header contains the total number of frames within the WAL
63664 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
63666 ** wal-index. The values are:
63671 ** Each index block contains two sections, a page-mapping that contains the
63672 ** database page number associated with each wal frame, and a hash-table
63674 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
63675 ** for the first index block) 32-bit page numbers. The first entry in the
63676 ** first index-block contains the database page number corresponding to the
63681 ** The last index block in a wal-index usually contains less than the full
63682 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
63684 ** allocated size of the page-mapping array - the page-mapping array merely
63688 ** can be found by scanning the page-mapping sections of each index block
63694 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
63699 ** 1-based index of an entry in the mapping section of the same
63700 ** index block. Let K be the 1-based index of the largest entry in
63715 ** wrap-around.) Because the hash table is never more than half full,
63720 ** current index block. Otherwise the iMax-th mapping entry of the
63738 ** and to the wal-index) might be using a different value K1, where K1>K0.
63744 ** in the first place - which is what reader one wants. Meanwhile, the
63767 ** The maximum (and only) versions of the wal and wal-index formats
63771 ** values in the wal-header are correct and (b) the version field is not
63774 ** Similarly, if a client successfully reads a wal-index header (i.e. the
63776 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
63791 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
63801 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
63811 ** The following object holds a copy of the wal-index header content.
63813 ** The actual header in the wal-index consists of two copies of this
63820 ** Or it can be 1 to represent a 65536-byte page. The latter case was
63824 u32 iVersion; /* Wal-index version */
63828 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
63838 ** A copy of the following object occurs in the wal-index immediately
63861 ** holds read-lock K, then the value in aReadMark[K] is no greater than
63864 ** a special case; its value is never used and it exists as a place-holder
63893 ** We assume that 32-bit loads are atomic and so no locks are needed in
63906 ** This is a schematic view of the complete 136-byte header of the
63907 ** wal-index file (also known as the -shm file):
63909 ** +-----------------------------+
63911 ** +-----------------------------+ |
63913 ** +-----------------------------+ |
63915 ** +-------+-------+-------------+ |
63917 ** +-------+-------+-------------+ |
63919 ** +-----------------------------+ | WalIndexHdr object
63921 ** +-----------------------------+ |
63924 ** +-----------------------------+ |
63927 ** +-----------------------------+ |
63930 ** +-----------------------------+
63932 ** +-----------------------------+ |
63934 ** +-----------------------------+ |
63936 ** +-------+-------+-------------+ |
63938 ** +-------+-------+-------------+ | Second copy of the
63940 ** +-----------------------------+ |
63942 ** +-----------------------------+ |
63945 ** +-----------------------------+ |
63948 ** +-----------------------------+ |
63951 ** +-----------------------------+
63953 ** +-----------------------------+
63959 ** +-------+-------+------+------+
63961 ** +-------+-------+------+------+ ) 8 lock bytes
63963 ** +-------+-------+------+------+
63965 ** +-----------------------------+
63967 ** +-----------------------------+
63972 ** only support mandatory file-locks, we do not read or write data
63985 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
63986 ** big-endian format in the first 4 bytes of a WAL file.
63989 ** file are calculated by treating all data as an array of 32-bit
63990 ** big-endian words. Otherwise, they are calculated by interpreting
63991 ** all data as 32-bit little-endian words.
63996 ** Return the offset of frame iFrame in the write-ahead log file,
63998 ** is to the start of the write-ahead log frame-header.
64001 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
64005 ** An open write-ahead log file is represented by an instance of the
64016 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
64018 i16 readLock; /* Which read lock is being held. -1 for none */
64020 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
64027 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
64028 WalIndexHdr hdr; /* Wal-index header for current transaction */
64032 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
64059 ** Each page of the wal-index mapping contains a hash-table made up of
64073 ** walIteratorInit() - Create a new iterator,
64074 ** walIteratorNext() - Step an iterator,
64075 ** walIteratorFree() - Free an iterator.
64088 } aSegment[1]; /* One for every 32KB page in the wal-index */
64092 ** Define the parameters of the hash tables in the wal-index file. There
64093 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
64094 ** wal-index.
64096 ** Changing any of these constants will alter the wal-index format and
64104 ** The block of page numbers associated with the first hash-table in a
64105 ** wal-index is smaller than usual. This is so that there is a complete
64106 ** hash-table on each aligned 32KB page of the wal-index.
64108 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
64110 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
64116 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
64117 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
64120 ** If the wal-index is currently smaller the iPage pages then the size
64121 ** of the wal-index might be increased, but only if it is safe to do
64122 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
64123 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
64127 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
64131 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
64140 /* Enlarge the pWal->apWiData[] array if required */
64141 if( pWal->nWiData<=iPage ){
64144 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
64149 memset((void*)&apNew[pWal->nWiData], 0,
64150 sizeof(u32*)*(iPage+1-pWal->nWiData));
64151 pWal->apWiData = apNew;
64152 pWal->nWiData = iPage+1;
64156 assert( pWal->apWiData[iPage]==0 );
64157 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
64158 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
64159 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
64161 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
64162 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
64164 assert( pWal->apWiData[iPage]!=0
64166 || (pWal->writeLock==0 && iPage==0) );
64167 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
64171 pWal->readOnly |= WAL_SHM_RDONLY;
64178 *ppPage = pWal->apWiData[iPage];
64187 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
64194 ** Return a pointer to the WalCkptInfo structure in the wal-index.
64197 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64198 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
64202 ** Return a pointer to the WalIndexHdr structure in the wal-index.
64205 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64206 return (volatile WalIndexHdr*)pWal->apWiData[0];
64210 ** The argument to this macro must be of type u32. On a little-endian
64212 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
64214 ** of the input value as a little-endian integer.
64231 int nativeCksum, /* True for native byte-order, false for non-native */
64295 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64296 sqlite3OsShmBarrier(pWal->pDbFd);
64301 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
64303 ** might give false-positive TSAN warnings.
64305 ** See tag-20200519-1.
64314 ** Write the header information in pWal->hdr into the wal-index.
64316 ** The checksum on pWal->hdr is updated before it is written.
64322 assert( pWal->writeLock );
64323 pWal->hdr.isInit = 1;
64324 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
64325 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
64326 /* Possible TSAN false-positive. See tag-20200519-1 */
64327 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64329 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64334 ** supplied by the caller. A frame-header is made up of a series of
64335 ** 4-byte big-endian integers, as follows:
64340 ** 8: Salt-1 (copied from the wal-header)
64341 ** 12: Salt-2 (copied from the wal-header)
64342 ** 16: Checksum-1.
64343 ** 20: Checksum-2.
64346 Wal *pWal, /* The write-ahead log */
64348 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
64352 int nativeCksum; /* True for native byte-order checksums */
64353 u32 *aCksum = pWal->hdr.aFrameCksum;
64357 if( pWal->iReCksum==0 ){
64358 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
64360 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64362 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64377 Wal *pWal, /* The write-ahead log */
64383 int nativeCksum; /* True for native byte-order checksums */
64384 u32 *aCksum = pWal->hdr.aFrameCksum;
64388 /* A frame is only valid if the salt values in the frame-header
64389 ** match the salt values in the wal-header.
64391 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
64403 ** all prior frams, the first 16 bytes of this frame-header,
64404 ** and the frame-data matches the checksum in the last 8
64405 ** bytes of this frame-header.
64407 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64409 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64433 return "WRITE-LOCK";
64435 return "CKPT-LOCK";
64437 return "RECOVER-LOCK";
64440 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
64441 lockIdx-WAL_READ_LOCK(0));
64450 ** A lock cannot be moved directly between shared and exclusive - it must go
64453 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
64457 if( pWal->exclusiveMode ) return SQLITE_OK;
64458 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64460 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
64462 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64466 if( pWal->exclusiveMode ) return;
64467 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64469 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
64473 if( pWal->exclusiveMode ) return SQLITE_OK;
64474 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64476 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
64478 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64482 if( pWal->exclusiveMode ) return;
64483 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64485 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
64491 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
64496 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
64497 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
64500 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
64505 ** of a page hash table in the wal-index. This becomes the return value
64510 volatile ht_slot *aHash; /* Start of the wal-index hash table */
64517 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
64520 ** Set output variable pLoc->aHash to point to the start of the hash table
64521 ** in the wal-index file. Set pLoc->iZero to one less than the frame
64524 ** (pLoc->iZero+N) in the log.
64526 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
64527 ** first frame indexed by the hash table, frame (pLoc->iZero).
64536 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
64539 if( pLoc->aPgno ){
64540 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
64542 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
64543 pLoc->iZero = 0;
64545 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
64554 ** Return the number of the wal-index page that contains the hash-table
64555 ** and page-number array that contain entries corresponding to WAL frame
64556 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
64560 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
64577 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
64579 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
64584 ** than pWal->hdr.mxFrame.
64586 ** This function is called whenever pWal->hdr.mxFrame is decreased due
64589 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
64591 ** pWal->hdr.mxFrame advances to the point where those hash tables are
64600 assert( pWal->writeLock );
64601 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
64602 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
64603 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
64605 if( pWal->hdr.mxFrame==0 ) return;
64607 /* Obtain pointers to the hash-table and page-number array containing
64608 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
64609 ** that the page said hash-table and array reside on is already mapped.(1)
64611 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
64612 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
64613 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
64614 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
64616 /* Zero all hash-table entries that correspond to frame numbers greater
64617 ** than pWal->hdr.mxFrame.
64619 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
64628 ** frame numbers greater than pWal->hdr.mxFrame.
64630 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
64653 ** Set an entry in the wal-index that will map database page number
64658 WalHashLoc sLoc; /* Wal-index hash table location */
64662 /* Assuming the wal-index file was successfully mapped, populate the
64667 int idx; /* Value to write to hash-table slot */
64670 idx = iFrame - sLoc.iZero;
64673 /* If this is the first entry to be added to this hash-table, zero the
64677 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
64686 ** the hash-table before writing any new entries.
64688 if( sLoc.aPgno[idx-1] ){
64690 assert( !sLoc.aPgno[idx-1] );
64693 /* Write the aPgno[] array entry and the hash-table slot. */
64696 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
64698 sLoc.aPgno[idx-1] = iPage;
64714 ** thing to check, so only do this occasionally - not on every
64736 ** Recover the wal-index by reading the write-ahead log file.
64739 ** wal-index to prevent other threads/processes from doing anything
64740 ** with the WAL or wal-index while recovery is running. The
64757 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
64760 assert( pWal->writeLock );
64761 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
64762 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64769 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
64771 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
64778 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
64786 u32 iPg; /* Current 32KB wal-index page */
64790 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
64803 || szPage&(szPage-1)
64809 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
64810 pWal->szPage = szPage;
64811 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
64812 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
64815 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
64816 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
64818 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
64819 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
64843 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
64848 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
64853 pWal->apWiData[iPg] = aPrivate;
64861 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64868 /* If nTruncate is non-zero, this is a commit record. */
64870 pWal->hdr.mxFrame = iFrame;
64871 pWal->hdr.nPage = nTruncate;
64872 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
64875 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
64876 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
64879 pWal->apWiData[iPg] = aShare;
64891 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
64920 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
64921 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
64924 /* Reset the checkpoint-header. This is safe because this thread is
64926 ** checkpointers. Then set the values of read-mark slots 1 through N.
64929 pInfo->nBackfill = 0;
64930 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
64931 pInfo->aReadMark[0] = 0;
64935 if( i==1 && pWal->hdr.mxFrame ){
64936 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
64938 pInfo->aReadMark[i] = READMARK_NOT_USED;
64951 if( pWal->hdr.nPage ){
64954 pWal->hdr.mxFrame, pWal->zWalName
64961 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64966 ** Close an open wal-index.
64969 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
64971 for(i=0; i<pWal->nWiData; i++){
64972 sqlite3_free((void *)pWal->apWiData[i]);
64973 pWal->apWiData[i] = 0;
64976 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64977 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
64988 ** client from unlinking the WAL or wal-index file. If another process
64997 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
65000 int bNoShm, /* True to run in heap-memory mode */
65012 ** of these constants would result in an incompatible on-disk format
65013 ** for the -shm file. Any change that causes one of these asserts to
65017 ** This table also serves as a helpful cross-reference when trying to
65018 ** interpret hex dumps of the -shm file.
65058 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
65063 pRet->pVfs = pVfs;
65064 pRet->pWalFd = (sqlite3_file *)&pRet[1];
65065 pRet->pDbFd = pDbFd;
65066 pRet->readLock = -1;
65067 pRet->mxWalSize = mxWalSize;
65068 pRet->zWalName = zWalName;
65069 pRet->syncHeader = 1;
65070 pRet->padToSectorBoundary = 1;
65071 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
65073 /* Open file handle on the write-ahead log file. */
65075 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
65077 pRet->readOnly = WAL_RDONLY;
65082 sqlite3OsClose(pRet->pWalFd);
65086 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
65088 pRet->padToSectorBoundary = 0;
65100 if( pWal ) pWal->mxWalSize = iLimit;
65122 iMin = p->iPrior;
65124 for(i=p->nSegment-1; i>=0; i--){
65125 struct WalSegment *pSegment = &p->aSegment[i];
65126 while( pSegment->iNext<pSegment->nEntry ){
65127 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
65131 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
65135 pSegment->iNext++;
65139 *piPage = p->iPrior = iRet;
65167 const u32 *aContent, /* Pages in wal - keys for the sort */
65231 ht_slot *aList; /* Pointer to sub-list content */
65239 struct Sublist aSub[13]; /* Array of sub-lists */
65243 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
65252 assert( p->aList && p->nList<=(1<<iSub) );
65253 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
65254 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65265 assert( p->nList<=(1<<iSub) );
65266 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
65267 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65277 assert( aContent[aList[i]] > aContent[aList[i-1]] );
65293 ** nBackfill or earlier may be included - excluding them is an optimization
65309 ht_slot *aTmp; /* Temp space used by merge-sort */
65315 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
65316 iLast = pWal->hdr.mxFrame;
65321 + (nSegment-1)*sizeof(struct WalSegment)
65328 p->nSegment = nSegment;
65330 /* Allocate temporary space used by the merge-sort routine. This block
65350 nEntry = (int)(iLast - sLoc.iZero);
65352 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
65354 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
65361 p->aSegment[i].iZero = sLoc.iZero;
65362 p->aSegment[i].nEntry = nEntry;
65363 p->aSegment[i].aIndex = aIndex;
65364 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
65381 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
65386 if( pWal->db ){
65387 int tmout = pWal->db->busyTimeout;
65391 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
65404 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
65418 assert( pWal->readLock<0 || bLock==0 );
65420 assert( pWal->db );
65424 pWal->writeLock = 1;
65428 }else if( pWal->writeLock ){
65430 pWal->writeLock = 0;
65439 pWal->db = db;
65463 ** busy-handler function. Invoke it and retry the lock until either the
65464 ** lock is successfully obtained or the busy-handler returns 0.
65487 ** The cache of the wal-index header must be valid to call this function.
65488 ** Return the page-size in bytes used by the database.
65491 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65500 ** file - there are no readers that may attempt to read a frame from
65503 ** This function updates the shared-memory structures so that the next
65508 ** new wal-index header. It should be passed a pseudo-random value (i.e.
65514 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
65515 pWal->nCkpt++;
65516 pWal->hdr.mxFrame = 0;
65518 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
65520 AtomicStore(&pInfo->nBackfill, 0);
65521 pInfo->nBackfillAttempted = 0;
65522 pInfo->aReadMark[1] = 0;
65523 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
65524 assert( pInfo->aReadMark[0]==0 );
65536 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
65542 ** in the WAL and can be recovered following a power-loss or hard reset.
65549 ** This routine uses and updates the nBackfill field of the wal-index header.
65568 int szPage; /* Database page-size */
65581 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65583 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65592 mxSafeFrame = pWal->hdr.mxFrame;
65593 mxPage = pWal->hdr.nPage;
65595 u32 y = AtomicLoad(pInfo->aReadMark+i);
65597 assert( y<=pWal->hdr.mxFrame );
65601 AtomicStore(pInfo->aReadMark+i, iMark);
65613 if( pInfo->nBackfill<mxSafeFrame ){
65614 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
65621 u32 nBackfill = pInfo->nBackfill;
65623 pInfo->nBackfillAttempted = mxSafeFrame;
65626 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
65634 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
65635 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
65637 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
65640 ** maximum size of the pending-byte page (65536 bytes), then
65644 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
65654 if( AtomicLoad(&db->u1.isInterrupted) ){
65655 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
65663 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
65665 iOffset = (iDbpage-1)*(i64)szPage;
65667 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
65670 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
65674 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
65675 i64 szDb = pWal->hdr.nPage*(i64)szPage;
65677 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
65679 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
65683 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
65704 assert( pWal->writeLock );
65705 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65710 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
65711 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
65714 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
65720 ** wal-index header in shared memory, as all subsequent reader or
65724 ** the wal-index header do not match the contents of the
65725 ** file-system. To avoid this, update the wal-index header to
65728 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
65730 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65748 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
65750 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
65754 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
65770 int isDelete = 0; /* True to unlink wal and wal-index files */
65773 ** ordinary, rollback-mode locking methods, this guarantees that the
65776 ** the wal and wal-index files.
65781 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
65783 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
65784 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65790 int bPersist = -1;
65792 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
65796 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
65799 }else if( pWal->mxWalSize>=0 ){
65803 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
65812 sqlite3OsClose(pWal->pWalFd);
65815 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
65819 sqlite3_free((void *)pWal->apWiData);
65826 ** Try to read the wal-index header. Return 0 on success and 1 if
65829 ** The wal-index is in shared memory. Another thread or process might
65836 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
65839 ** If the checksum cannot be verified return non-zero. If the header
65847 /* The first page of the wal-index must be mapped at this point. */
65848 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65853 ** from the file. If this happens, return non-zero.
65855 ** tag-20200519-1:
65856 ** There are two copies of the header at the beginning of the wal-index.
65860 ** give false-positive warnings about these accesses because the tools do not
65861 ** account for the double-read and the memory barrier. The use of mutexes
65867 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
65875 return 1; /* Malformed header - probably all zeros */
65877 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
65882 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
65884 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
65885 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65886 testcase( pWal->szPage<=32768 );
65887 testcase( pWal->szPage>=65536 );
65898 #define WAL_RETRY (-1)
65901 ** Read the wal-index header from the wal-index and into pWal->hdr.
65902 ** If the wal-header appears to be corrupt, try to reconstruct the
65903 ** wal-index from the WAL before returning.
65905 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
65906 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
65909 ** If the wal-index header is successfully read, return SQLITE_OK.
65915 volatile u32 *page0; /* Chunk of wal-index containing header */
65917 /* Ensure that page 0 of the wal-index (the page that contains the
65918 ** wal-index header) is mapped. Return early if an error occurs here.
65925 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
65927 ** confirm that another write-capable connection has the shared-memory
65928 ** open, and hence the content of the shared-memory is unreliable,
65929 ** since the shared-memory might be inconsistent with the WAL file
65932 assert( pWal->writeLock==0 );
65933 assert( pWal->readOnly & WAL_SHM_RDONLY );
65934 pWal->bShmUnreliable = 1;
65935 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
65938 return rc; /* Any other non-OK return is just an error */
65941 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
65945 assert( page0!=0 || pWal->writeLock==0 );
65947 /* If the first page of the wal-index has been mapped, try to read the
65948 ** wal-index header immediately, without holding any lock. This usually
65949 ** works, but may fail if the wal-index header is corrupt or currently
65958 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
65964 int bWriteLock = pWal->writeLock;
65966 pWal->writeLock = 1;
65970 /* If the wal-index header is still malformed even while holding
65979 pWal->writeLock = 0;
65987 ** sure the wal-index was not constructed with some future format that
65990 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
65993 if( pWal->bShmUnreliable ){
65996 pWal->bShmUnreliable = 0;
65997 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
66003 pWal->exclusiveMode = WAL_NORMAL_MODE;
66010 ** Open a transaction in a connection where the shared-memory is read-only
66011 ** and where we cannot verify that there is a separate write-capable connection
66012 ** on hand to keep the shared-memory up-to-date with the WAL file.
66014 ** This can happen, for example, when the shared-memory is implemented by
66015 ** memory-mapping a *-shm file, where a prior writer has shut down and
66016 ** left the *-shm file on disk, and now the present connection is trying
66017 ** to use that database but lacks write permission on the *-shm file.
66022 ** The *-wal file has been read and an appropriate wal-index has been
66023 ** constructed in pWal->apWiData[] using heap memory instead of shared
66030 ** WAL_RETRY, then the heap memory wal-index has been discarded and
66032 ** beginning (including attempting to map the *-shm file).
66045 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
66047 assert( pWal->bShmUnreliable );
66048 assert( pWal->readOnly & WAL_SHM_RDONLY );
66049 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66059 pWal->readLock = 0;
66061 /* Check to see if a separate writer has attached to the shared-memory area,
66062 ** thus making the shared-memory "reliable" again. Do this by invoking
66066 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
66067 ** cause the heap-memory WAL-index to be discarded and the actual
66079 ** even if some external agent does a "chmod" to make the shared-memory
66083 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
66084 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
66090 /* We reach this point only if the real shared-memory is still unreliable.
66091 ** Assume the in-memory WAL-index substitute is correct and load it
66092 ** into pWal->hdr.
66094 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
66099 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
66104 /* If the wal file is too small to contain a wal-header and the
66105 ** wal-index header has mxFrame==0, then it must be safe to proceed
66111 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
66116 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
66120 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
66122 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
66129 assert( (pWal->szPage & (pWal->szPage-1))==0 );
66130 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
66131 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
66140 ** wal file since the heap-memory wal-index was created. If so, the
66141 ** heap-memory wal-index is discarded and WAL_RETRY returned to
66143 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
66144 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
66145 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
66153 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
66157 /* If nTruncate is non-zero, then a complete transaction has been
66165 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
66166 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
66172 for(i=0; i<pWal->nWiData; i++){
66173 sqlite3_free((void*)pWal->apWiData[i]);
66174 pWal->apWiData[i] = 0;
66176 pWal->bShmUnreliable = 0;
66195 ** to make a copy of the wal-index header into pWal->hdr. If the
66196 ** wal-index header has changed, *pChanged is set to 1 (as an indication
66198 ** flushed.) When useWal==1, the wal-index header is assumed to already
66209 ** bad luck when there is lots of contention for the wal-index, but that
66213 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
66214 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
66217 ** including frame number aReadMark[pWal->readLock]. The reader will
66218 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
66219 ** Or if pWal->readLock==0, then the reader will ignore the WAL
66222 ** this routine will always set pWal->readLock>0 on success.
66224 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
66234 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
66241 assert( pWal->readLock<0 ); /* Not currently locked */
66244 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
66252 ** paged out or take a page-fault that is time-consuming to resolve,
66266 VVA_ONLY( pWal->lockError = 1; )
66269 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
66270 sqlite3OsSleep(pWal->pVfs, nDelay);
66275 if( pWal->bShmUnreliable==0 ){
66287 if( pWal->apWiData[0]==0 ){
66292 ** code that determines whether or not the shared-memory region
66306 else if( pWal->bShmUnreliable ){
66311 assert( pWal->nWiData>0 );
66312 assert( pWal->apWiData[0]!=0 );
66314 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
66316 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
66325 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
66342 pWal->readLock = 0;
66352 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
66356 mxFrame = pWal->hdr.mxFrame;
66358 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
66359 mxFrame = pWal->pSnapshot->mxFrame;
66363 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
66370 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
66376 AtomicStore(pInfo->aReadMark+i,mxFrame);
66387 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
66395 /* Now that the read-lock has been obtained, check that neither the
66396 ** value in the aReadMark[] array or the contents of the wal-index
66399 ** It is necessary to check that the wal-index header did not change
66400 ** between the time it was read and when the shared-lock was obtained
66403 ** that occur later in the log than pWal->hdr.mxFrame may have been
66406 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
66409 ** Before checking that the live wal-index header has not changed
66412 ** to read any frames earlier than minFrame from the wal file - they
66416 ** nBackfill and checking that the wal-header in shared-memory still
66417 ** matches the one cached in pWal->hdr, it is guaranteed that the
66418 ** checkpointer that set nBackfill was not working with a wal-index
66419 ** header newer than that cached in pWal->hdr. If it were, that could
66421 ** a version of page X that lies before pWal->minFrame (call that version
66424 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
66427 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
66429 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
66431 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
66432 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
66437 assert( mxReadMark<=pWal->hdr.mxFrame );
66438 pWal->readLock = (i16)mxI;
66452 ** This is only really safe if the file-system is such that any page
66466 assert( pWal->readLock>=0 );
66470 int szPage = (int)pWal->szPage;
66473 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
66480 u32 i = pInfo->nBackfillAttempted;
66481 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
66489 assert( i - sLoc.iZero - 1 >=0 );
66490 pgno = sLoc.aPgno[i-sLoc.iZero-1];
66491 iDbOff = (i64)(pgno-1) * szPage;
66495 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
66498 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
66506 pInfo->nBackfillAttempted = i-1;
66524 ** it takes a snapshot of the state of the WAL and wal-index for the current
66526 ** Other threads might append new content to the WAL and wal-index but
66539 WalIndexHdr *pSnapshot = pWal->pSnapshot;
66542 assert( pWal->ckptLock==0 );
66546 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66554 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
66557 ** before checking pInfo->nBackfillAttempted. */
66565 pWal->ckptLock = 1;
66579 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66581 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
66582 ** is populated with the wal-index header corresponding to the head
66590 ** pSnapshot->mxFrame into the database file. Note that the
66595 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
66596 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
66600 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
66601 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
66604 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
66605 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
66607 assert( pWal->readLock>0 );
66608 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
66614 /* A client using a non-current snapshot may not ignore any frames
66620 pWal->minFrame = 1;
66629 if( pWal->ckptLock ){
66632 pWal->ckptLock = 0;
66640 ** read-lock.
66644 if( pWal->readLock>=0 ){
66645 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
66646 pWal->readLock = -1;
66664 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
66669 assert( pWal->readLock>=0 || pWal->lockError );
66671 /* If the "last page" field of the wal-index header snapshot is 0, then
66673 ** in this case as an optimization. Likewise, if pWal->readLock==0,
66677 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
66687 ** that adds entries to the wal-index (and possibly to this hash
66691 ** read transaction was opened may have been written incorrectly -
66698 ** if we had exclusive access to the hash-table:
66701 ** This condition filters out normal hash-table collisions.
66705 ** table after the current read-transaction had started.
66707 iMinHash = walFramePage(pWal->minFrame);
66708 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
66723 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
66727 if( (nCollide--)==0 ){
66737 ** of the wal-index file content. Make sure the results agree with the
66742 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
66743 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
66770 sz = pWal->hdr.szPage;
66776 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
66783 if( pWal && ALWAYS(pWal->readLock>=0) ){
66784 return pWal->hdr.nPage;
66807 /* If the write-lock is already held, then it was obtained before the
66808 ** read-transaction was even opened, making this call a no-op.
66810 if( pWal->writeLock ){
66811 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
66818 assert( pWal->readLock>=0 );
66819 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
66821 if( pWal->readOnly ){
66832 pWal->writeLock = 1;
66838 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
66840 pWal->writeLock = 0;
66852 if( pWal->writeLock ){
66854 pWal->writeLock = 0;
66855 pWal->iReCksum = 0;
66856 pWal->truncateOnCommit = 0;
66863 ** function moves the write-pointer back to the start of the transaction.
66875 if( ALWAYS(pWal->writeLock) ){
66876 Pgno iMax = pWal->hdr.mxFrame;
66879 /* Restore the clients cache of the wal-index header to the state it
66882 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
66884 for(iFrame=pWal->hdr.mxFrame+1;
66890 ** (b) has an outstanding reference, then xUndo is either a no-op
66902 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
66914 assert( pWal->writeLock );
66915 aWalData[0] = pWal->hdr.mxFrame;
66916 aWalData[1] = pWal->hdr.aFrameCksum[0];
66917 aWalData[2] = pWal->hdr.aFrameCksum[1];
66918 aWalData[3] = pWal->nCkpt;
66930 assert( pWal->writeLock );
66931 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
66933 if( aWalData[3]!=pWal->nCkpt ){
66934 /* This savepoint was opened immediately after the write-transaction
66939 aWalData[3] = pWal->nCkpt;
66942 if( aWalData[0]<pWal->hdr.mxFrame ){
66943 pWal->hdr.mxFrame = aWalData[0];
66944 pWal->hdr.aFrameCksum[0] = aWalData[1];
66945 pWal->hdr.aFrameCksum[1] = aWalData[2];
66957 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
66961 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
66968 if( pWal->readLock==0 ){
66970 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
66971 if( pInfo->nBackfill>0 ){
66974 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66979 ** wal-index header to reflect this.
66982 ** at this point. But updating the actual wal-index header is also
66986 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66992 pWal->readLock = -1;
67008 ** the next fsync should occur - passed from sqlite3WalFrames() into
67021 ** Do a sync when crossing the p->iSyncPoint boundary.
67034 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
67035 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
67036 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
67039 iAmt -= iFirstAmt;
67041 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
67042 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
67045 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
67060 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
67061 pData = pPage->pData;
67062 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
67066 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
67079 const int szPage = pWal->szPage;/* Database page size */
67082 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
67092 ** wal-file header. Otherwise, read them from the frame header of the
67094 assert( pWal->iReCksum>0 );
67095 if( pWal->iReCksum==1 ){
67098 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
67100 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
67101 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
67102 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
67104 iRead = pWal->iReCksum;
67105 pWal->iReCksum = 0;
67108 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
67115 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
67124 ** Write a set of frames to the log. The caller must hold the write-lock
67129 int szPage, /* Database page-size in bytes */
67147 assert( pWal->writeLock );
67154 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
67156 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
67161 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
67162 iFirst = pLive->mxFrame+1;
67166 ** log file, instead of appending to it at pWal->hdr.mxFrame.
67176 iFrame = pWal->hdr.mxFrame;
67178 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
67179 u32 aCksum[2]; /* Checksum for wal-header */
67184 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
67185 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
67186 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
67187 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
67191 pWal->szPage = szPage;
67192 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
67193 pWal->hdr.aFrameCksum[0] = aCksum[0];
67194 pWal->hdr.aFrameCksum[1] = aCksum[1];
67195 pWal->truncateOnCommit = 1;
67197 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
67198 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
67205 ** an out-of-order write following a WAL restart could result in
67210 if( pWal->syncHeader ){
67211 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67215 if( (int)pWal->szPage!=szPage ){
67221 w.pFd = pWal->pWalFd;
67229 for(p=pList; p; p=p->pDirty){
67234 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
67236 if( iFirst && (p->pDirty || isCommit==0) ){
67238 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
67243 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
67244 pWal->iReCksum = iWrite;
67246 pData = p->pData;
67247 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
67249 p->flags &= ~PGHDR_WAL_APPEND;
67256 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
67261 p->flags |= PGHDR_WAL_APPEND;
67265 if( isCommit && pWal->iReCksum ){
67286 if( pWal->padToSectorBoundary ){
67287 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
67288 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
67309 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
67310 i64 sz = pWal->mxWalSize;
67311 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
67315 pWal->truncateOnCommit = 0;
67318 /* Append data to the wal-index. It is not necessary to lock the
67319 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
67323 iFrame = pWal->hdr.mxFrame;
67324 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
67325 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
67327 rc = walIndexAppend(pWal, iFrame, p->pgno);
67332 nExtra--;
67333 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
67338 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
67341 pWal->hdr.mxFrame = iFrame;
67343 pWal->hdr.iChange++;
67344 pWal->hdr.nPage = nTruncate;
67346 /* If this is a commit, update the wal-index header too. */
67349 pWal->iCallback = iFrame;
67364 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
67380 int isChanged = 0; /* True if a new wal-index header is loaded */
67384 assert( pWal->ckptLock==0 );
67385 assert( pWal->writeLock==0 );
67387 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67391 if( pWal->readOnly ) return SQLITE_READONLY;
67395 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
67399 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
67401 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
67404 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
67411 pWal->ckptLock = 1;
67413 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
67417 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
67418 ** immediately, and a busy-handler is configured, it is invoked and the
67419 ** writer lock retried until either the busy-handler returns 0 or the
67425 pWal->writeLock = 1;
67435 /* Read the wal-index header. */
67440 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
67441 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
67448 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
67456 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
67457 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
67462 /* If a new wal-index header was loaded before the checkpoint was
67463 ** performed, then the pager-cache associated with pWal is now
67464 ** out of date. So zero the cached wal-index header to ensure that
67468 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
67476 if( pWal->ckptLock ){
67478 pWal->ckptLock = 0;
67495 ret = pWal->iCallback;
67496 pWal->iCallback = 0;
67507 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
67509 ** transition out of exclusive-mode is successful, return 1. This
67514 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
67516 ** WAL is already in exclusive-locking mode - meaning that this
67517 ** routine is a no-op. The pager must already hold the exclusive lock
67520 ** If op is negative, then do a dry-run of the op==1 case but do
67527 assert( pWal->writeLock==0 );
67528 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
67530 /* pWal->readLock is usually set, but might be -1 if there was a
67531 ** prior error while attempting to acquire are read-lock. This cannot
67534 ** upgrade to exclusive-mode following such an error.
67536 assert( pWal->readLock>=0 || pWal->lockError );
67537 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
67540 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
67541 pWal->exclusiveMode = WAL_NORMAL_MODE;
67542 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
67543 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67545 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67551 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
67552 assert( pWal->readLock>=0 );
67553 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
67554 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67557 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67563 ** Return true if the argument is non-NULL and the WAL module is using
67564 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
67565 ** WAL module is using shared-memory, return false.
67568 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
67581 assert( pWal->readLock>=0 && pWal->writeLock==0 );
67583 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
67591 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
67598 /* Try to open on pSnapshot when the next read-transaction starts
67604 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
67608 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
67617 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
67618 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
67619 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
67620 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
67640 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
67641 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
67665 ** read-lock. This function returns the database page-size if it is known,
67669 assert( pWal==0 || pWal->readLock>=0 );
67670 return (pWal ? pWal->szPage : 0);
67677 return pWal->pWalFd;
67714 ** This file implements an external (disk-based) database using BTrees.
67718 ** "Sorting And Searching", pages 473-480. Addison-Wesley
67724 ** ----------------------------------------------------------------
67725 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
67726 ** ----------------------------------------------------------------
67731 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
67753 ** page, or a pointer-map page.
67772 ** 40 60 15 4-byte meta values passed to higher layers
67777 ** 52 4 Largest root-page (auto/incr_vacuum)
67778 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
67781 ** 68 4 Application-ID
67783 ** 92 4 The version-valid-for number
67786 ** All of the integer values are big-endian (most significant byte first).
67794 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
67809 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
67812 ** |----------------|
67814 ** |----------------|
67816 ** |----------------|
67820 ** |----------------|
67823 ** |----------------| ^ Grows upwards
67826 ** |----------------|
67845 ** The cell pointer array contains zero or more 2-byte numbers which are
67876 ** appears first. A variable-length integer may not be more than 9 bytes long.
67878 ** allows a 64-bit integer to be encoded in 9 bytes.
67901 ** filled with data (pagesize - 4 bytes). The last page can have as little
67924 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
67931 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
67942 ** You can change this value at compile-time by specifying a
67943 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
67944 ** header must be exactly 16 bytes including the zero-terminator so
67956 ** first byte of on-disk image of every BTree page.
67966 ** is derived from the raw on-disk page content.
67973 ** stored in MemPage.pBt->mutex.
67977 u8 intKey; /* True if table b-trees. False for index b-trees */
67990 int nFree; /* Number of free bytes on the page. -1 for unknown */
67993 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
67994 ** non-overflow cell */
67998 u8 *aDataEnd; /* One byte past the end of the entire page - not just
68000 ** corruption-induced buffer overflow. */
68056 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
68070 ** If the shared-data extension is enabled, there may be multiple users
68082 # error wrong numeric code for no-transaction
68085 # error wrong numeric code for read-transaction
68088 # error wrong numeric code for write-transaction
68111 ** If a BtShared client fails to obtain a write-lock on a database
68112 ** table (because there exists one or more read-locks on the table),
68113 ** the shared-cache enters 'pending-lock' state and isPending is
68116 ** The shared-cache leaves the 'pending lock' state when either of
68122 ** while in the 'pending-lock' state, no connection may start a new
68125 ** This feature is included to help prevent writer-starvation.
68134 u8 autoVacuum; /* True if auto-vacuum is enabled */
68135 u8 incrVacuum; /* True if incr-vacuum is enabled */
68139 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
68142 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
68143 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
68152 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
68153 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
68157 BtLock *pLock; /* List of locks held on this shared-btree struct */
68173 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
68175 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
68187 u16 nSize; /* Size of the cell content on the main b-tree page */
68191 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
68194 ** root-node and 3 for all other internal nodes.
68203 ** b-tree within a database file.
68213 ** found at self->pBt->mutex.
68221 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
68222 ** sqlite3BtreePrevious() is no-op if skipNext<0.
68247 u8 curIntKey; /* Value of apPage[0]->intKey */
68249 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
68252 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
68278 ** Cursor is valid except that the Cursor.skipNext field is non-zero
68280 ** operation should be a no-op.
68305 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
68308 ** These macros define the location of the pointer-map entry for a
68313 ** PTRMAP_PAGENO returns the database page number of the pointer-map
68317 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
68319 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
68323 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
68340 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
68343 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
68351 ** overflow pages. The page-number identifies the previous
68354 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
68367 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
68368 assert( p->pBt->inTransaction>=p->inTrans );
68373 ** if the database supports auto-vacuum or not. Because it is used
68379 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
68390 ** the database. As the integrity-check proceeds, for each page used in
68391 ** the database the corresponding bit is set. This allows integrity-check to
68398 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
68410 u32 *heap; /* Min-heap used for analyzing cell coverage */
68415 ** Routines to read or write a two- and four-byte big-endian integer values.
68424 ** two-byte aligned address. get2bytea() is only used for accessing the
68443 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
68445 ** p->locked boolean to true.
68448 assert( p->locked==0 );
68449 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
68450 assert( sqlite3_mutex_held(p->db->mutex) );
68452 sqlite3_mutex_enter(p->pBt->mutex);
68453 p->pBt->db = p->db;
68454 p->locked = 1;
68458 ** Release the BtShared mutex associated with B-Tree handle p and
68459 ** clear the p->locked boolean.
68462 BtShared *pBt = p->pBt;
68463 assert( p->locked==1 );
68464 assert( sqlite3_mutex_held(pBt->mutex) );
68465 assert( sqlite3_mutex_held(p->db->mutex) );
68466 assert( p->db==pBt->db );
68468 sqlite3_mutex_leave(pBt->mutex);
68469 p->locked = 0;
68479 ** and this routine is a no-op. The underlying mutex is non-recursive.
68484 ** by all database connections. The p->pNext is a list of other
68487 ** p, then first unlock all of the others on p->pNext, then wait
68496 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
68497 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
68498 assert( p->pNext==0 || p->pNext->db==p->db );
68499 assert( p->pPrev==0 || p->pPrev->db==p->db );
68500 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
68503 assert( !p->locked || p->wantToLock>0 );
68504 assert( p->sharable || p->wantToLock==0 );
68507 assert( sqlite3_mutex_held(p->db->mutex) );
68511 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
68513 if( !p->sharable ) return;
68514 p->wantToLock++;
68515 if( p->locked ) return;
68532 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
68533 p->pBt->db = p->db;
68534 p->locked = 1;
68543 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68544 assert( pLater->sharable );
68545 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
68546 assert( !pLater->locked || pLater->wantToLock>0 );
68547 if( pLater->locked ){
68552 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68553 if( pLater->wantToLock ){
68564 assert( sqlite3_mutex_held(p->db->mutex) );
68565 if( p->sharable ){
68566 assert( p->wantToLock>0 );
68567 p->wantToLock--;
68568 if( p->wantToLock==0 ){
68577 ** B-Tree is not marked as sharable.
68582 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
68583 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
68584 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
68585 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
68587 return (p->sharable==0 || p->locked);
68599 ** There is a corresponding leave-all procedures.
68610 assert( sqlite3_mutex_held(db->mutex) );
68611 for(i=0; i<db->nDb; i++){
68612 p = db->aDb[i].pBt;
68613 if( p && p->sharable ){
68618 db->noSharedCache = skipOk;
68621 if( db->noSharedCache==0 ) btreeEnterAll(db);
68626 assert( sqlite3_mutex_held(db->mutex) );
68627 for(i=0; i<db->nDb; i++){
68628 p = db->aDb[i].pBt;
68633 if( db->noSharedCache==0 ) btreeLeaveAll(db);
68645 if( !sqlite3_mutex_held(db->mutex) ){
68648 for(i=0; i<db->nDb; i++){
68650 p = db->aDb[i].pBt;
68651 if( p && p->sharable &&
68652 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
68663 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
68667 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
68675 if( db->pVfs==0 && db->nDb==0 ) return 1;
68677 assert( iDb>=0 && iDb<db->nDb );
68678 if( !sqlite3_mutex_held(db->mutex) ) return 0;
68680 p = db->aDb[iDb].pBt;
68682 return p->sharable==0 || p->locked==1;
68690 ** these two routines, all mutex operations are no-ops in that case and
68694 ** the ones below, are no-ops and are null #defines in btree.h.
68698 p->pBt->db = p->db;
68702 for(i=0; i<db->nDb; i++){
68703 Btree *p = db->aDb[i].pBt;
68705 p->pBt->db = p->db;
68720 sqlite3BtreeEnter(pCur->pBtree);
68724 sqlite3BtreeLeave(pCur->pBtree);
68744 ** This file implements an external (disk-based) database using BTrees.
68768 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
68773 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
68776 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
68789 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
68834 ** shared-cache table level locks. If the library is compiled with the
68835 ** shared-cache feature disabled, then there is only ever one user
68837 ** So define the lock related functions as no-ops.
68852 u64 n = pBt->nSeek;
68853 pBt->nSeek = 0;
68864 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
68872 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
68883 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
68895 ** For example, when writing to a table with root-page iRoot via
68908 ** hold a write-lock on the schema table (root page 1). This is also
68913 Pgno iRoot, /* Root page of b-tree */
68914 int isIndex, /* True if iRoot is the root of an index b-tree */
68917 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
68922 ** and has the read-uncommitted flag set, then no lock is required.
68925 if( (pBtree->sharable==0)
68926 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
68933 ** the correct locks are held. So do not bother - just return true.
68936 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
68940 /* Figure out the root-page that the lock should be held on. For table
68941 ** b-trees, this is just the root page of the b-tree being read or
68942 ** written. For index b-trees, it is the root page of the associated
68947 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
68949 if( pIdx->tnum==iRoot ){
68956 iTab = pIdx->pTable->tnum;
68964 /* Search for the required lock. Either a write-lock on root-page iTab, a
68965 ** write-lock on the schema table, or (if the client is reading) a
68966 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
68967 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
68968 if( pLock->pBtree==pBtree
68969 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
68970 && pLock->eLock>=eLockType
68992 ** read-uncommitted flag set, then it is OK for the other object to
69002 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69003 if( p->pgnoRoot==iRoot
69004 && p->pBtree!=pBtree
69005 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
69016 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
69021 BtShared *pBt = p->pBt;
69026 assert( p->db!=0 );
69027 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
69029 /* If requesting a write-lock, then the Btree must have an open write
69033 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
69034 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
69036 /* This routine is a no-op if the shared-cache is not enabled */
69037 if( !p->sharable ){
69044 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
69045 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
69049 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69050 /* The condition (pIter->eLock!=eLock) in the following if(...)
69053 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
69059 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
69060 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
69061 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
69062 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
69064 assert( p==pBt->pWriter );
69065 pBt->btsFlags |= BTS_PENDING;
69076 ** Add a lock on the table with root-page iTable to the shared-btree used
69093 BtShared *pBt = p->pBt;
69099 assert( p->db!=0 );
69101 /* A connection with the read-uncommitted flag set will never try to
69102 ** obtain a read-lock using this function. The only read-lock obtained
69103 ** by a connection in read-uncommitted mode is on the sqlite_schema
69105 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
69107 /* This function should only be called on a sharable b-tree after it
69108 ** has been determined that no other b-tree holds a conflicting lock. */
69109 assert( p->sharable );
69113 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69114 if( pIter->iTable==iTable && pIter->pBtree==p ){
69128 pLock->iTable = iTable;
69129 pLock->pBtree = p;
69130 pLock->pNext = pBt->pLock;
69131 pBt->pLock = pLock;
69135 ** and the requested lock. This means if a write-lock was already held
69136 ** and a read-lock requested, we don't incorrectly downgrade the lock.
69139 if( eLock>pLock->eLock ){
69140 pLock->eLock = eLock;
69157 BtShared *pBt = p->pBt;
69158 BtLock **ppIter = &pBt->pLock;
69161 assert( p->sharable || 0==*ppIter );
69162 assert( p->inTrans>0 );
69166 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
69167 assert( pLock->pBtree->inTrans>=pLock->eLock );
69168 if( pLock->pBtree==p ){
69169 *ppIter = pLock->pNext;
69170 assert( pLock->iTable!=1 || pLock==&p->lock );
69171 if( pLock->iTable!=1 ){
69175 ppIter = &pLock->pNext;
69179 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
69180 if( pBt->pWriter==p ){
69181 pBt->pWriter = 0;
69182 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69183 }else if( pBt->nTransaction==2 ){
69193 pBt->btsFlags &= ~BTS_PENDING;
69198 ** This function changes all write-locks held by Btree p into read-locks.
69201 BtShared *pBt = p->pBt;
69202 if( pBt->pWriter==p ){
69204 pBt->pWriter = 0;
69205 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69206 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
69207 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
69208 pLock->eLock = READ_LOCK;
69226 return sqlite3_mutex_held(p->pBt->mutex);
69230 ** database connetion. This is important in shared-cache mode. If the database
69231 ** connection pointers get out-of-sync, it is possible for routines like
69234 ** statements only and for the purpose of double-checking that the btree code
69235 ** does keep the database connection pointers up-to-date.
69239 return (p->pBtree->db==p->pBt->db);
69247 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
69250 ** Invalidate the overflow page-list cache for all cursors opened
69255 assert( sqlite3_mutex_held(pBt->mutex) );
69256 for(p=pBt->pCursor; p; p=p->pNext){
69269 ** cursors open on any row within the table with root-page pgnoRoot.
69282 assert( pBtree->hasIncrblobCur );
69284 pBtree->hasIncrblobCur = 0;
69285 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69286 if( (p->curFlags & BTCF_Incrblob)!=0 ){
69287 pBtree->hasIncrblobCur = 1;
69288 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
69289 p->eState = CURSOR_INVALID;
69302 ** when a page that previously contained data becomes a free-list leaf
69307 ** free-list leaf pages:
69310 ** a free-list leaf page, the page is not written to the database
69311 ** (as free-list leaf pages contain no meaningful data). Sometimes
69315 ** 2) When a free-list leaf page is reused, its content is not read
69321 ** a page is moved to the free-list and then reused within the same
69323 ** it is moved to the free-list and it is also not journalled when it
69324 ** is extracted from the free-list and reused, then the original data
69329 ** moved to become a free-list leaf page, the corresponding bit is
69330 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
69337 if( !pBt->pHasContent ){
69338 assert( pgno<=pBt->nPage );
69339 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
69340 if( !pBt->pHasContent ){
69344 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
69345 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
69353 ** This function is called when a free-list leaf page is removed from the
69354 ** free-list for reuse. It returns false if it is safe to retrieve the
69355 ** page from the pager layer with the 'no-content' flag set. True otherwise.
69358 Bitvec *p = pBt->pHasContent;
69364 ** invoked at the conclusion of each write-transaction.
69367 sqlite3BitvecDestroy(pBt->pHasContent);
69368 pBt->pHasContent = 0;
69376 if( pCur->iPage>=0 ){
69377 for(i=0; i<pCur->iPage; i++){
69378 releasePageNotNull(pCur->apPage[i]);
69380 releasePageNotNull(pCur->pPage);
69381 pCur->iPage = -1;
69388 ** function saves the current cursor key in variables pCur->nKey and
69389 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
69393 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
69394 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
69395 ** set to point to a malloced buffer pCur->nKey bytes in size containing
69400 assert( CURSOR_VALID==pCur->eState );
69401 assert( 0==pCur->pKey );
69404 if( pCur->curIntKey ){
69406 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
69411 ** up to the size of 1 varint plus 1 8-byte value when the cursor
69415 pCur->nKey = sqlite3BtreePayloadSize(pCur);
69416 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
69418 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
69420 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
69421 pCur->pKey = pKey;
69429 assert( !pCur->curIntKey || !pCur->pKey );
69443 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
69444 assert( 0==pCur->pKey );
69447 if( pCur->curFlags & BTCF_Pinned ){
69450 if( pCur->eState==CURSOR_SKIPNEXT ){
69451 pCur->eState = CURSOR_VALID;
69453 pCur->skipNext = 0;
69459 pCur->eState = CURSOR_REQUIRESEEK;
69462 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
69471 ** the table with root-page iRoot. "Saving the cursor position" means that
69482 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
69492 assert( sqlite3_mutex_held(pBt->mutex) );
69493 assert( pExcept==0 || pExcept->pBt==pBt );
69494 for(p=pBt->pCursor; p; p=p->pNext){
69495 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
69498 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
69513 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
69514 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
69520 testcase( p->iPage>=0 );
69524 p = p->pNext;
69534 sqlite3_free(pCur->pKey);
69535 pCur->pKey = 0;
69536 pCur->eState = CURSOR_INVALID;
69555 KeyInfo *pKeyInfo = pCur->pKeyInfo;
69560 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
69565 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
69584 assert( pCur->eState>=CURSOR_REQUIRESEEK );
69585 if( pCur->eState==CURSOR_FAULT ){
69586 return pCur->skipNext;
69588 pCur->eState = CURSOR_INVALID;
69592 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
69595 sqlite3_free(pCur->pKey);
69596 pCur->pKey = 0;
69597 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
69598 if( skipNext ) pCur->skipNext = skipNext;
69599 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
69600 pCur->eState = CURSOR_SKIPNEXT;
69607 (p->eState>=CURSOR_REQUIRESEEK ? \
69627 assert( sizeof(pCur->eState)==1 );
69659 assert( pCur->eState!=CURSOR_VALID );
69665 if( pCur->eState!=CURSOR_VALID ){
69706 pCur->hints = x;
69713 ** number for the pointer-map page that contains the entry for the
69723 assert( sqlite3_mutex_held(pBt->mutex) );
69725 nPagesPerMapPage = (pBt->usableSize/5)+1;
69726 iPtrMap = (pgno-2)/nPagesPerMapPage;
69740 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
69741 ** a no-op. If an error occurs, the appropriate error code is written
69753 assert( sqlite3_mutex_held(pBt->mutex) );
69754 /* The super-journal page number must never be used as a pointer map page */
69757 assert( pBt->autoVacuum );
69763 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69780 assert( offset <= (int)pBt->usableSize-5 );
69784 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
69810 assert( sqlite3_mutex_held(pBt->mutex) );
69813 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69824 assert( offset <= (int)pBt->usableSize-5 );
69846 ** 4-byte child pointer found on interior pages, if there is one.
69851 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69853 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69859 ** on a single B-tree page. Make necessary adjustments to the CellInfo
69880 minLocal = pPage->minLocal;
69881 maxLocal = pPage->maxLocal;
69882 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
69886 pInfo->nLocal = (u16)surplus;
69888 pInfo->nLocal = (u16)minLocal;
69890 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
69899 maxLocal = pPage->maxLocal;
69905 minLocal = pPage->minLocal;
69906 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
69930 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69931 assert( pPage->leaf==0 );
69932 assert( pPage->childPtrSize==4 );
69936 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
69937 pInfo->nPayload = 0;
69938 pInfo->nLocal = 0;
69939 pInfo->pPayload = 0;
69951 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69952 assert( pPage->leaf==0 || pPage->leaf==1 );
69953 assert( pPage->intKeyLeaf );
69954 assert( pPage->childPtrSize==0 );
69975 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
69978 ** This routine is a high-runner.
70012 pInfo->nKey = *(i64*)&iKey;
70013 pInfo->nPayload = nPayload;
70014 pInfo->pPayload = pIter;
70015 testcase( nPayload==pPage->maxLocal );
70016 testcase( nPayload==(u32)pPage->maxLocal+1 );
70017 if( nPayload<=pPage->maxLocal ){
70021 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70022 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70023 pInfo->nLocal = (u16)nPayload;
70036 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70037 assert( pPage->leaf==0 || pPage->leaf==1 );
70038 assert( pPage->intKeyLeaf==0 );
70039 pIter = pCell + pPage->childPtrSize;
70049 pInfo->nKey = nPayload;
70050 pInfo->nPayload = nPayload;
70051 pInfo->pPayload = pIter;
70052 testcase( nPayload==pPage->maxLocal );
70053 testcase( nPayload==(u32)pPage->maxLocal+1 );
70054 if( nPayload<=pPage->maxLocal ){
70058 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70059 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70060 pInfo->nLocal = (u16)nPayload;
70070 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
70078 ** data area of the btree-page. The return number includes the cell
70098 pPage->xParseCell(pPage, pCell, &debuginfo);
70101 assert( pPage->childPtrSize==4 );
70111 testcase( nSize==pPage->maxLocal );
70112 testcase( nSize==(u32)pPage->maxLocal+1 );
70113 if( nSize<=pPage->maxLocal ){
70114 nSize += (u32)(pIter - pCell);
70117 int minLocal = pPage->minLocal;
70118 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70119 testcase( nSize==pPage->maxLocal );
70120 testcase( nSize==(u32)pPage->maxLocal+1 );
70121 if( nSize>pPage->maxLocal ){
70124 nSize += 4 + (u16)(pIter - pCell);
70140 pPage->xParseCell(pPage, pCell, &debuginfo);
70143 assert( pPage->childPtrSize==0 );
70153 testcase( nSize==pPage->maxLocal );
70154 testcase( nSize==(u32)pPage->maxLocal+1 );
70155 if( nSize<=pPage->maxLocal ){
70156 nSize += (u32)(pIter - pCell);
70159 int minLocal = pPage->minLocal;
70160 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70161 testcase( nSize==pPage->maxLocal );
70162 testcase( nSize==(u32)pPage->maxLocal+1 );
70163 if( nSize>pPage->maxLocal ){
70166 nSize += 4 + (u16)(pIter - pCell);
70181 pPage->xParseCell(pPage, pCell, &debuginfo);
70186 assert( pPage->childPtrSize==4 );
70189 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
70190 return (u16)(pIter - pCell);
70203 pPage->xParseCell(pPage, pCell, &debuginfo);
70215 /* pIter now points at the 64-bit integer key value, a variable length
70226 testcase( nSize==pPage->maxLocal );
70227 testcase( nSize==(u32)pPage->maxLocal+1 );
70228 if( nSize<=pPage->maxLocal ){
70229 nSize += (u32)(pIter - pCell);
70232 int minLocal = pPage->minLocal;
70233 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70234 testcase( nSize==pPage->maxLocal );
70235 testcase( nSize==(u32)pPage->maxLocal+1 );
70236 if( nSize>pPage->maxLocal ){
70239 nSize += 4 + (u16)(pIter - pCell);
70250 return pPage->xCellSize(pPage, findCell(pPage, iCell));
70258 ** pointer to an overflow page, insert an entry into the pointer-map for
70265 pPage->xParseCell(pPage, pCell, &info);
70268 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
70273 ovfl = get4byte(&pCell[info.nSize-4]);
70274 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
70282 ** page so that there are no free-blocks on the free-block list.
70287 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
70288 ** b-tree page so that there are no freeblocks or fragment bytes, all
70294 int pc; /* Address of the i-th cell */
70308 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70309 assert( pPage->pBt!=0 );
70310 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
70311 assert( pPage->nOverflow==0 );
70312 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70313 data = pPage->aData;
70314 hdr = pPage->hdrOffset;
70315 cellOffset = pPage->cellOffset;
70316 nCell = pPage->nCell;
70319 usableSize = pPage->pBt->usableSize;
70324 ** offsets to each pointer in the cell-pointer array than it is to
70328 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
70331 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
70345 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
70352 assert( cbrk+(iFree-top) <= usableSize );
70353 memmove(&data[cbrk], &data[top], iFree-top);
70365 iCellLast = usableSize - 4;
70368 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
70369 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
70372 u8 *pAddr; /* The i-th cell pointer */
70384 size = pPage->xCellSize(pPage, &src[pc]);
70385 cbrk -= size;
70399 assert( pPage->nFree>=0 );
70400 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
70407 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
70408 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70413 ** Search the free-list on page pPg for space to store a cell nByte bytes in
70415 ** from the free-list.
70417 ** If no suitable space can be found on the free-list, return NULL.
70427 const int hdr = pPg->hdrOffset; /* Offset to page header */
70428 u8 * const aData = pPg->aData; /* Page data */
70433 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
70438 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
70439 ** freeblock form a big-endian integer which is the size of the freeblock
70440 ** in bytes, including the 4-byte header. */
70443 if( (x = size - nByte)>=0 ){
70447 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
70451 /* Remove the slot from the free-list. Update the number of
70461 /* The slot remains on the free-list. Reduce its size to account
70478 if( pc>maxPC+nByte-4 ){
70486 ** Allocate nByte bytes of space from within the B-Tree page passed
70487 ** as the first argument. Write into *pIdx the index into pPage->aData[]
70499 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
70500 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
70506 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70507 assert( pPage->pBt );
70508 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70510 assert( pPage->nFree>=nByte );
70511 assert( pPage->nOverflow==0 );
70512 assert( nByte < (int)(pPage->pBt->usableSize-8) );
70514 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
70515 gap = pPage->cellOffset + 2*pPage->nCell;
70517 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
70520 ** However, that integer is too large to be stored in a 2-byte unsigned
70525 if( top==0 && pPage->pBt->usableSize==65536 ){
70530 }else if( top>(int)pPage->pBt->usableSize ){
70545 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
70546 *pIdx = g2 = (int)(pSpace-data);
70562 assert( pPage->nCell>0 || CORRUPT_DB );
70563 assert( pPage->nFree>=0 );
70564 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
70577 top -= nByte;
70579 assert( top+nByte <= (int)pPage->pBt->usableSize );
70585 ** Return a section of the pPage->aData to the freelist.
70586 ** The first byte of the new free block is pPage->aData[iStart]
70605 unsigned char *data = pPage->aData; /* Page content */
70608 assert( pPage->pBt!=0 );
70609 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70610 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
70611 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
70612 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70614 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
70619 hdr = pPage->hdrOffset;
70631 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
70643 nFrag = iFreeBlk - iEnd;
70646 if( iEnd > pPage->pBt->usableSize ){
70649 iSize = iEnd - iStart;
70661 nFrag += iStart - iPtrEnd;
70662 iSize = iEnd - iPtr;
70667 data[hdr+7] -= nFrag;
70671 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
70690 pPage->nFree += iOrigSize;
70707 BtShared *pBt; /* A copy of pPage->pBt */
70709 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
70710 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70711 pBt = pPage->pBt;
70712 pPage->max1bytePayload = pBt->max1bytePayload;
70714 pPage->childPtrSize = 0;
70715 pPage->leaf = 1;
70717 pPage->intKeyLeaf = 1;
70718 pPage->xCellSize = cellSizePtrTableLeaf;
70719 pPage->xParseCell = btreeParseCellPtr;
70720 pPage->intKey = 1;
70721 pPage->maxLocal = pBt->maxLeaf;
70722 pPage->minLocal = pBt->minLeaf;
70724 pPage->intKey = 0;
70725 pPage->intKeyLeaf = 0;
70726 pPage->xCellSize = cellSizePtrIdxLeaf;
70727 pPage->xParseCell = btreeParseCellPtrIndex;
70728 pPage->maxLocal = pBt->maxLocal;
70729 pPage->minLocal = pBt->minLocal;
70731 pPage->intKey = 0;
70732 pPage->intKeyLeaf = 0;
70733 pPage->xCellSize = cellSizePtrIdxLeaf;
70734 pPage->xParseCell = btreeParseCellPtrIndex;
70738 pPage->childPtrSize = 4;
70739 pPage->leaf = 0;
70741 pPage->intKey = 0;
70742 pPage->intKeyLeaf = 0;
70743 pPage->xCellSize = cellSizePtr;
70744 pPage->xParseCell = btreeParseCellPtrIndex;
70745 pPage->maxLocal = pBt->maxLocal;
70746 pPage->minLocal = pBt->minLocal;
70748 pPage->intKeyLeaf = 0;
70749 pPage->xCellSize = cellSizePtrNoPayload;
70750 pPage->xParseCell = btreeParseCellPtrNoPayload;
70751 pPage->intKey = 1;
70752 pPage->maxLocal = pBt->maxLeaf;
70753 pPage->minLocal = pBt->minLeaf;
70755 pPage->intKey = 0;
70756 pPage->intKeyLeaf = 0;
70757 pPage->xCellSize = cellSizePtr;
70758 pPage->xParseCell = btreeParseCellPtrIndex;
70767 ** in the pPage->nFree field.
70770 int pc; /* Address of a freeblock within pPage->aData[] */
70772 u8 *data; /* Equal to pPage->aData */
70779 assert( pPage->pBt!=0 );
70780 assert( pPage->pBt->db!=0 );
70781 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70782 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
70783 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
70784 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
70785 assert( pPage->isInit==1 );
70786 assert( pPage->nFree<0 );
70788 usableSize = pPage->pBt->usableSize;
70789 hdr = pPage->hdrOffset;
70790 data = pPage->aData;
70791 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
70795 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
70796 iCellLast = usableSize - 4;
70799 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
70803 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
70807 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
70834 ** of the cell-content area plus the number of free bytes within
70835 ** the cell-content area. If this is greater than the usable-size
70837 ** serves to verify that the offset to the start of the cell-content
70843 pPage->nFree = (u16)(nFree - iCellFirst);
70856 int pc; /* Address of a freeblock within pPage->aData[] */
70857 u8 *data; /* Equal to pPage->aData */
70861 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
70862 usableSize = pPage->pBt->usableSize;
70863 iCellLast = usableSize - 4;
70864 data = pPage->aData;
70865 cellOffset = pPage->cellOffset;
70866 if( !pPage->leaf ) iCellLast--;
70867 for(i=0; i<pPage->nCell; i++){
70874 sz = pPage->xCellSize(pPage, &data[pc]);
70887 ** not contain a well-formed database page, then return
70889 ** guarantee that the page is well-formed. It only shows that
70893 u8 *data; /* Equal to pPage->aData */
70896 assert( pPage->pBt!=0 );
70897 assert( pPage->pBt->db!=0 );
70898 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70899 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
70900 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
70901 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
70902 assert( pPage->isInit==0 );
70904 pBt = pPage->pBt;
70905 data = pPage->aData + pPage->hdrOffset;
70906 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
70907 ** the b-tree page type. */
70911 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
70912 pPage->maskPage = (u16)(pBt->pageSize - 1);
70913 pPage->nOverflow = 0;
70914 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
70915 pPage->aCellIdx = data + pPage->childPtrSize + 8;
70916 pPage->aDataEnd = pPage->aData + pBt->pageSize;
70917 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
70918 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
70920 pPage->nCell = get2byte(&data[3]);
70921 if( pPage->nCell>MX_CELL(pBt) ){
70925 testcase( pPage->nCell==MX_CELL(pBt) );
70926 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
70930 assert( pPage->nCell>0
70931 || get2byteNotZero(&data[5])==(int)pBt->usableSize
70933 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
70934 pPage->isInit = 1;
70935 if( pBt->db->flags & SQLITE_CellSizeCk ){
70946 unsigned char *data = pPage->aData;
70947 BtShared *pBt = pPage->pBt;
70948 u8 hdr = pPage->hdrOffset;
70951 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
70952 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
70953 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
70954 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70955 assert( sqlite3_mutex_held(pBt->mutex) );
70956 if( pBt->btsFlags & BTS_FAST_SECURE ){
70957 memset(&data[hdr], 0, pBt->usableSize - hdr);
70963 put2byte(&data[hdr+5], pBt->usableSize);
70964 pPage->nFree = (u16)(pBt->usableSize - first);
70966 pPage->cellOffset = first;
70967 pPage->aDataEnd = &data[pBt->pageSize];
70968 pPage->aCellIdx = &data[first];
70969 pPage->aDataOfst = &data[pPage->childPtrSize];
70970 pPage->nOverflow = 0;
70971 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
70972 pPage->maskPage = (u16)(pBt->pageSize - 1);
70973 pPage->nCell = 0;
70974 pPage->isInit = 1;
70984 if( pgno!=pPage->pgno ){
70985 pPage->aData = sqlite3PagerGetData(pDbPage);
70986 pPage->pDbPage = pDbPage;
70987 pPage->pBt = pBt;
70988 pPage->pgno = pgno;
70989 pPage->hdrOffset = pgno==1 ? 100 : 0;
70991 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
71016 assert( sqlite3_mutex_held(pBt->mutex) );
71017 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
71030 assert( sqlite3_mutex_held(pBt->mutex) );
71031 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
71040 ** error, return ((unsigned int)-1).
71043 return pBt->nPage;
71047 return btreePagecount(p->pBt);
71055 ** And if the fetch fails, this routine must decrement pCur->iPage.
71057 ** The page is fetched as read-write unless pCur is not NULL and is
71058 ** a read-only cursor.
71068 int bReadOnly /* True for a read-only page */
71072 assert( sqlite3_mutex_held(pBt->mutex) );
71073 assert( pCur==0 || ppPage==&pCur->pPage );
71074 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
71075 assert( pCur==0 || pCur->iPage>0 );
71081 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
71086 if( (*ppPage)->isInit==0 ){
71093 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
71094 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
71098 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
71108 pCur->iPage--;
71109 pCur->pPage = pCur->apPage[pCur->iPage];
71123 assert( pPage->aData );
71124 assert( pPage->pBt );
71125 assert( pPage->pDbPage!=0 );
71126 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71127 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71128 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71129 sqlite3PagerUnrefNotNull(pPage->pDbPage);
71136 assert( pPage->aData );
71137 assert( pPage->pBt );
71138 assert( pPage->pDbPage!=0 );
71139 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71140 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71141 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71142 sqlite3PagerUnrefPageOne(pPage->pDbPage);
71162 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
71167 (*ppPage)->isInit = 0;
71187 if( pPage->isInit ){
71188 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71189 pPage->isInit = 0;
71196 ** the call for every page that comes in for re-initing. */
71207 assert( pBt->db );
71208 assert( sqlite3_mutex_held(pBt->db->mutex) );
71209 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
71217 ** be exclusively in memory, or it might use a disk-based memory cache.
71221 ** If zFilename is ":memory:" then an in-memory database is created
71234 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
71251 /* Set the variable isMemdb to true for an in-memory database, or
71252 ** false for a file-based database.
71264 assert( sqlite3_mutex_held(db->mutex) );
71283 p->inTrans = TRANS_NONE;
71284 p->db = db;
71286 p->lock.pBtree = p;
71287 p->lock.iTable = 1;
71298 int nFullPathname = pVfs->mxPathname+1;
71302 p->sharable = 1;
71328 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
71329 assert( pBt->nRef>0 );
71330 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
71331 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
71333 for(iDb=db->nDb-1; iDb>=0; iDb--){
71334 Btree *pExisting = db->aDb[iDb].pBt;
71335 if( pExisting && pExisting->pBt==pBt ){
71343 p->pBt = pBt;
71344 pBt->nRef++;
71358 p->sharable = 1;
71380 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
71383 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
71384 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
71389 pBt->openFlags = (u8)flags;
71390 pBt->db = db;
71391 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
71392 p->pBt = pBt;
71394 pBt->pCursor = 0;
71395 pBt->pPage1 = 0;
71396 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
71398 pBt->btsFlags |= BTS_SECURE_DELETE;
71400 pBt->btsFlags |= BTS_OVERWRITE;
71402 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71403 ** determined by the 2-byte integer located at an offset of 16 bytes from
71405 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
71406 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
71407 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
71408 pBt->pageSize = 0;
71410 /* If the magic name ":memory:" will create an in-memory database, then
71411 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
71414 ** regular file-name. In this case the auto-vacuum applies as per normal.
71417 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
71418 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
71423 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
71424 ** determined by the one-byte unsigned integer found at an offset of 20
71427 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71429 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
71430 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
71433 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
71435 pBt->usableSize = pBt->pageSize - nReserve;
71436 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
71441 pBt->nRef = 1;
71442 if( p->sharable ){
71446 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
71447 if( pBt->mutex==0 ){
71453 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
71465 if( p->sharable ){
71468 for(i=0; i<db->nDb; i++){
71469 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
71470 while( pSib->pPrev ){ pSib = pSib->pPrev; }
71471 if( (uptr)p->pBt<(uptr)pSib->pBt ){
71472 p->pNext = pSib;
71473 p->pPrev = 0;
71474 pSib->pPrev = p;
71476 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
71477 pSib = pSib->pNext;
71479 p->pNext = pSib->pNext;
71480 p->pPrev = pSib;
71481 if( p->pNext ){
71482 p->pNext->pPrev = p;
71484 pSib->pNext = p;
71495 if( pBt && pBt->pPager ){
71496 sqlite3PagerClose(pBt->pPager, 0);
71504 /* If the B-Tree was successfully opened, set the pager-cache size to the
71505 ** default value. Except, when opening on an existing shared pager-cache,
71506 ** do not change the pager-cache size.
71512 pFile = sqlite3PagerFile(pBt->pPager);
71513 if( pFile->pMethods ){
71514 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
71537 assert( sqlite3_mutex_notheld(pBt->mutex) );
71540 pBt->nRef--;
71541 if( pBt->nRef<=0 ){
71543 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
71546 while( ALWAYS(pList) && pList->pNext!=pBt ){
71547 pList=pList->pNext;
71550 pList->pNext = pBt->pNext;
71554 sqlite3_mutex_free(pBt->mutex);
71566 ** Make sure pBt->pTmpSpace points to an allocation of
71567 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
71572 assert( pBt->pTmpSpace==0 );
71575 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
71576 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
71577 if( pBt->pTmpSpace==0 ){
71578 BtCursor *pCur = pBt->pCursor;
71579 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
71584 /* One of the uses of pBt->pTmpSpace is to format cells before
71597 ** left-child pointer to the beginning of a cell.
71599 memset(pBt->pTmpSpace, 0, 8);
71600 pBt->pTmpSpace += 4;
71605 ** Free the pBt->pTmpSpace allocation
71608 if( pBt->pTmpSpace ){
71609 pBt->pTmpSpace -= 4;
71610 sqlite3PageFree(pBt->pTmpSpace);
71611 pBt->pTmpSpace = 0;
71619 BtShared *pBt = p->pBt;
71622 assert( sqlite3_mutex_held(p->db->mutex) );
71628 BtCursor *pCur = pBt->pCursor;
71631 pCur = pCur->pNext;
71632 assert( pTmp->pBtree!=p );
71639 ** The call to sqlite3BtreeRollback() drops any table-locks held by
71645 /* If there are still other outstanding references to the shared-btree
71647 ** up the shared-btree.
71649 assert( p->wantToLock==0 && p->locked==0 );
71650 if( !p->sharable || removeFromSharingList(pBt) ){
71656 assert( !pBt->pCursor );
71657 sqlite3PagerClose(pBt->pPager, p->db);
71658 if( pBt->xFreeSchema && pBt->pSchema ){
71659 pBt->xFreeSchema(pBt->pSchema);
71661 sqlite3DbFree(0, pBt->pSchema);
71667 assert( p->wantToLock==0 );
71668 assert( p->locked==0 );
71669 if( p->pPrev ) p->pPrev->pNext = p->pNext;
71670 if( p->pNext ) p->pNext->pPrev = p->pPrev;
71685 BtShared *pBt = p->pBt;
71686 assert( sqlite3_mutex_held(p->db->mutex) );
71688 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
71704 BtShared *pBt = p->pBt;
71706 assert( sqlite3_mutex_held(p->db->mutex) );
71708 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
71719 BtShared *pBt = p->pBt;
71720 assert( sqlite3_mutex_held(p->db->mutex) );
71722 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
71733 ** is a very low but non-zero probability of damage. Level 3 reduces the
71741 BtShared *pBt = p->pBt;
71742 assert( sqlite3_mutex_held(p->db->mutex) );
71744 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
71773 BtShared *pBt = p->pBt;
71776 pBt->nReserveWanted = nReserve;
71777 x = pBt->pageSize - pBt->usableSize;
71779 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
71785 ((pageSize-1)&pageSize)==0 ){
71787 assert( !pBt->pCursor );
71789 pBt->pageSize = (u32)pageSize;
71792 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
71793 pBt->usableSize = pBt->pageSize - (u16)nReserve;
71794 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71803 return p->pBt->pageSize;
71808 ** may only be called if it is guaranteed that the b-tree mutex is already
71812 ** known that the shared b-tree mutex is held, but the mutex on the
71819 assert( sqlite3_mutex_held(p->pBt->mutex) );
71820 n = p->pBt->pageSize - p->pBt->usableSize;
71831 ** The amount of reserve can only grow - never shrink.
71836 n1 = (int)p->pBt->nReserveWanted;
71851 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
71862 ** newFlag==(-1) No changes
71867 ** freelist leaf pages are not written back to the database. Thus in-page
71881 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
71882 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
71884 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
71890 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
71891 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
71892 ** is disabled. The default value for the auto-vacuum property is
71899 BtShared *pBt = p->pBt;
71904 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
71907 pBt->autoVacuum = av ?1:0;
71908 pBt->incrVacuum = av==2 ?1:0;
71916 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
71926 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
71927 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
71936 ** If the user has not set the safety-level for this database connection
71937 ** using "PRAGMA synchronous", and if the safety-level is not already
71946 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
71947 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
71948 if( pDb->bSyncSet==0
71949 && pDb->safety_level!=safety_level
71950 && pDb!=&db->aDb[1]
71952 pDb->safety_level = safety_level;
71953 sqlite3PagerSetFlags(pBt->pPager,
71954 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
71971 ** well-formed database file, then SQLITE_CORRUPT is returned.
71981 assert( sqlite3_mutex_held(pBt->mutex) );
71982 assert( pBt->pPage1==0 );
71983 rc = sqlite3PagerSharedLock(pBt->pPager);
71991 nPage = get4byte(28+(u8*)pPage1->aData);
71992 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
71993 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
71996 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
72002 u8 *page1 = pPage1->aData;
72004 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
72013 pBt->btsFlags |= BTS_READ_ONLY;
72020 pBt->btsFlags |= BTS_READ_ONLY;
72031 ** may not be the latest version - there may be a newer one in the log
72034 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
72036 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
72052 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
72061 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
72062 ** determined by the 2-byte integer located at an offset of 16 bytes from
72065 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
72067 if( ((pageSize-1)&pageSize)!=0
72073 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72075 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
72079 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
72080 ** determined by the one-byte unsigned integer found at an offset of 20
72082 usableSize = pageSize - page1[20];
72083 if( (u32)pageSize!=pBt->pageSize ){
72085 ** of BtShared.pageSize, we have discovered that the page-size is
72086 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
72088 ** again with the correct page-size.
72091 pBt->usableSize = usableSize;
72092 pBt->pageSize = pageSize;
72094 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
72095 pageSize-usableSize);
72099 if( sqlite3WritableSchema(pBt->db)==0 ){
72106 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
72112 pBt->pageSize = pageSize;
72113 pBt->usableSize = usableSize;
72115 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
72116 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
72122 ** cells can will fit on one page. We assume a 10-byte page header.
72124 ** 2-byte pointer to the cell
72125 ** 4-byte child pointer
72126 ** 9-byte nKey value
72127 ** 4-byte nData value
72128 ** 4-byte overflow page pointer
72129 ** So a cell consists of a 2-byte pointer, a header which is as much as
72133 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
72134 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
72135 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
72136 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
72137 if( pBt->maxLocal>127 ){
72138 pBt->max1bytePayload = 127;
72140 pBt->max1bytePayload = (u8)pBt->maxLocal;
72142 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
72143 pBt->pPage1 = pPage1;
72144 pBt->nPage = nPage;
72149 pBt->pPage1 = 0;
72169 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
72170 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
72171 && pCur->eState!=CURSOR_FAULT ) r++;
72183 ** If there is a transaction in progress, this routine is a no-op.
72186 assert( sqlite3_mutex_held(pBt->mutex) );
72187 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
72188 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
72189 MemPage *pPage1 = pBt->pPage1;
72190 assert( pPage1->aData );
72191 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
72192 pBt->pPage1 = 0;
72207 assert( sqlite3_mutex_held(pBt->mutex) );
72208 if( pBt->nPage>0 ){
72211 pP1 = pBt->pPage1;
72213 data = pP1->aData;
72214 rc = sqlite3PagerWrite(pP1->pDbPage);
72218 data[16] = (u8)((pBt->pageSize>>8)&0xff);
72219 data[17] = (u8)((pBt->pageSize>>16)&0xff);
72222 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
72223 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
72227 memset(&data[24], 0, 100-24);
72229 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72231 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
72232 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
72233 put4byte(&data[36 + 4*4], pBt->autoVacuum);
72234 put4byte(&data[36 + 7*4], pBt->incrVacuum);
72236 pBt->nPage = 1;
72249 p->pBt->nPage = 0;
72250 rc = newDatabase(p->pBt);
72256 ** Attempt to start a new transaction. A write-transaction
72257 ** is started if the second argument is nonzero, otherwise a read-
72261 ** upgraded to exclusive by calling this routine a second time - the
72264 ** A write-transaction must be started before attempting any
72278 ** if there is one. But if there was previously a read-lock, do not
72279 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
72280 ** returned when there is already a read-lock in order to avoid a deadlock.
72291 BtShared *pBt = p->pBt;
72292 Pager *pPager = pBt->pPager;
72298 /* If the btree is already in a write-transaction, or it
72299 ** is already in a read-transaction and a read-transaction
72300 ** is requested, this is a no-op.
72302 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
72305 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
72307 if( (p->db->flags & SQLITE_ResetDatabase)
72310 pBt->btsFlags &= ~BTS_READ_ONLY;
72313 /* Write transactions are not possible on a read-only database */
72314 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
72323 ** on this shared-btree structure and a second write transaction is
72326 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
72327 || (pBt->btsFlags & BTS_PENDING)!=0
72329 pBlock = pBt->pWriter->db;
72332 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
72333 if( pIter->pBtree!=p ){
72334 pBlock = pIter->pBtree->db;
72340 sqlite3ConnectionBlocked(p->db, pBlock);
72347 /* Any read-only or read-write transaction implies a read-lock on
72348 ** page 1. So if some other shared-cache client already has a write-lock
72353 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
72354 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
72356 sqlite3PagerWalDb(pPager, p->db);
72361 if( pBt->pPage1==0 && wrflag ){
72362 assert( pBt->inTransaction==TRANS_NONE );
72368 /* Call lockBtree() until either pBt->pPage1 is populated or
72370 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
72371 ** reading page 1 it discovers that the page-size of the database
72372 ** file is not pBt->pageSize. In this case lockBtree() will update
72373 ** pBt->pageSize to the page-size of the file on disk.
72375 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
72378 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
72381 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
72384 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
72397 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
72405 if( p->inTrans==TRANS_NONE ){
72406 pBt->nTransaction++;
72408 if( p->sharable ){
72409 assert( p->lock.pBtree==p && p->lock.iTable==1 );
72410 p->lock.eLock = READ_LOCK;
72411 p->lock.pNext = pBt->pLock;
72412 pBt->pLock = &p->lock;
72416 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
72417 if( p->inTrans>pBt->inTransaction ){
72418 pBt->inTransaction = p->inTrans;
72421 MemPage *pPage1 = pBt->pPage1;
72423 assert( !pBt->pWriter );
72424 pBt->pWriter = p;
72425 pBt->btsFlags &= ~BTS_EXCLUSIVE;
72426 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
72429 /* If the db-size header field is incorrect (as it may be if an old
72432 ** re-read the database size from page 1 if a savepoint or transaction
72435 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
72436 rc = sqlite3PagerWrite(pPage1->pDbPage);
72438 put4byte(&pPage1->aData[28], pBt->nPage);
72447 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
72452 ** the sub-journal is not already open, then it will be opened here.
72454 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
72466 ** Set the pointer-map entries for all children of page pPage. Also, if
72474 BtShared *pBt = pPage->pBt;
72475 Pgno pgno = pPage->pgno;
72477 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72478 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72480 nCell = pPage->nCell;
72487 if( !pPage->leaf ){
72493 if( !pPage->leaf ){
72494 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72506 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
72509 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
72512 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
72516 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72517 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72520 if( get4byte(pPage->aData)!=iFrom ){
72523 put4byte(pPage->aData, iTo);
72529 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72531 nCell = pPage->nCell;
72537 pPage->xParseCell(pPage, pCell, &info);
72539 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
72542 if( iFrom==get4byte(pCell+info.nSize-4) ){
72543 put4byte(pCell+info.nSize-4, iTo);
72548 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
72560 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
72563 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
72575 ** the journal needs to be sync()ed before database page pDbPage->pgno
72583 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
72588 Pgno iDbPage = pDbPage->pgno;
72589 Pager *pPager = pBt->pPager;
72594 assert( sqlite3_mutex_held(pBt->mutex) );
72595 assert( pDbPage->pBt==pBt );
72601 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
72605 pDbPage->pgno = iFreePage;
72607 /* If pDbPage was a btree-page, then it may have child pages and/or cells
72621 Pgno nextOvfl = get4byte(pDbPage->aData);
72639 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
72657 ** Perform a single step of an incremental-vacuum. If successful, return
72662 ** More specifically, this function attempts to re-organize the database so
72668 ** If the bCommit parameter is non-zero, this function assumes that the
72670 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
72674 Pgno nFreeList; /* Number of pages still on the free-list */
72677 assert( sqlite3_mutex_held(pBt->mutex) );
72684 nFreeList = get4byte(&pBt->pPage1->aData[36]);
72699 /* Remove the page from the files free-list. This is not required
72700 ** if bCommit is non-zero. In that case, the free-list will be
72728 ** looping until a free-page located within the first nFin pages
72761 iLastPg--;
72763 pBt->bDoTruncate = 1;
72764 pBt->nPage = iLastPg;
72770 ** The database opened by the first argument is an auto-vacuum database
72772 ** size of the database in pages following an auto-vacuum operation.
72779 nEntry = pBt->usableSize/5;
72780 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
72781 nFin = nOrig - nFree - nPtrmap;
72783 nFin--;
72786 nFin--;
72793 ** A write-transaction must be opened before calling this function.
72802 BtShared *pBt = p->pBt;
72805 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
72806 if( !pBt->autoVacuum ){
72810 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
72822 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72823 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
72835 ** is committed for an auto-vacuum database.
72845 pBt = p->pBt;
72846 pPager = pBt->pPager;
72849 assert( sqlite3_mutex_held(pBt->mutex) );
72851 assert(pBt->autoVacuum);
72852 if( !pBt->incrVacuum ){
72862 ** is either a pointer-map page or the pending-byte page. If one
72868 nFree = get4byte(&pBt->pPage1->aData[36]);
72869 db = p->db;
72870 if( db->xAutovacPages ){
72872 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
72873 if( db->aDb[iDb].pBt==p ) break;
72875 nVac = db->xAutovacPages(
72876 db->pAutovacPagesArg,
72877 db->aDb[iDb].zDbSName,
72880 pBt->pageSize
72896 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
72900 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72902 put4byte(&pBt->pPage1->aData[32], 0);
72903 put4byte(&pBt->pPage1->aData[36], 0);
72905 put4byte(&pBt->pPage1->aData[28], nFin);
72906 pBt->bDoTruncate = 1;
72907 pBt->nPage = nFin;
72923 ** This routine does the first phase of a two-phase commit. This routine
72935 ** This call is a no-op if no write-transaction is currently active on pBt.
72938 ** the name of a super-journal file that should be written into the
72939 ** individual journal file, or is NULL, indicating no super-journal file
72942 ** When this is called, the super-journal should already have been
72946 ** the write-transaction for this database file is to delete the journal.
72950 if( p->inTrans==TRANS_WRITE ){
72951 BtShared *pBt = p->pBt;
72954 if( pBt->autoVacuum ){
72961 if( pBt->bDoTruncate ){
72962 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
72965 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
72976 BtShared *pBt = p->pBt;
72977 sqlite3 *db = p->db;
72981 pBt->bDoTruncate = 0;
72983 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
72985 ** handle, downgrade to a read-only transaction. The other statements
72988 p->inTrans = TRANS_READ;
72994 if( p->inTrans!=TRANS_NONE ){
72996 pBt->nTransaction--;
72997 if( 0==pBt->nTransaction ){
72998 pBt->inTransaction = TRANS_NONE;
73004 p->inTrans = TRANS_NONE;
73014 ** This routine implements the second phase of a 2-phase commit. The
73026 ** is non-zero then this b-tree transaction is part of a multi-file
73028 ** (by deleting a super-journal file) and the caller will ignore this
73030 ** reset the b-tree objects internal state to indicate that the write
73039 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
73043 /* If the handle has a write-transaction open, commit the shared-btrees
73046 if( p->inTrans==TRANS_WRITE ){
73048 BtShared *pBt = p->pBt;
73049 assert( pBt->inTransaction==TRANS_WRITE );
73050 assert( pBt->nTransaction>0 );
73051 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
73056 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
73057 pBt->inTransaction = TRANS_READ;
73091 ** flag is true, then only write-cursors need be tripped - read-only
73095 ** rolled back modified the database schema. In this case b-tree root
73100 ** saving the current position of a read-only cursor, all cursors,
73101 ** including all read-cursors are tripped.
73113 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
73114 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
73115 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
73124 p->eState = CURSOR_FAULT;
73125 p->skipNext = errCode;
73135 ** Set the pBt->nPage field correctly, according to the current
73136 ** state of the database. Assume pBt->pPage1 is valid.
73139 int nPage = get4byte(&pPage1->aData[28]);
73141 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
73142 testcase( pBt->nPage!=(u32)nPage );
73143 pBt->nPage = nPage;
73159 BtShared *pBt = p->pBt;
73178 if( p->inTrans==TRANS_WRITE ){
73181 assert( TRANS_WRITE==pBt->inTransaction );
73182 rc2 = sqlite3PagerRollback(pBt->pPager);
73187 /* The rollback may have destroyed the pPage1->aData value. So
73189 ** sure pPage1->aData is set correctly. */
73195 pBt->inTransaction = TRANS_READ;
73215 ** A statement sub-transaction is implemented as an anonymous savepoint. The
73217 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
73218 ** are no active savepoints and no other statement-transactions open,
73224 BtShared *pBt = p->pBt;
73226 assert( p->inTrans==TRANS_WRITE );
73227 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73229 assert( iStatement>p->db->nSavepoint );
73230 assert( pBt->inTransaction==TRANS_WRITE );
73236 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
73248 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
73255 if( p && p->inTrans==TRANS_WRITE ){
73256 BtShared *pBt = p->pBt;
73258 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
73264 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
73267 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
73268 pBt->nPage = 0;
73271 btreeSetNPage(pBt, pBt->pPage1);
73273 /* pBt->nPage might be zero if the database was corrupt when
73275 assert( CORRUPT_DB || pBt->nPage>0 );
73284 ** iTable. If a read-only cursor is requested, it is assumed that
73285 ** the caller already has at least a read-only transaction open
73286 ** on the database already. If a write-cursor is requested, then
73303 ** 3: The database must be writable (not on read-only media)
73314 ** operations on this cursor can be no-ops and all READ operations can
73315 ** return a null row (2-bytes: 0x01 0x00).
73318 ** root page of a b-tree. If it is not, then the cursor acquired
73327 int wrFlag, /* 1 to write. 0 read-only */
73331 BtShared *pBt = p->pBt; /* Shared b-tree handle */
73341 ** b-tree database, the connection is holding the required table locks,
73349 assert( p->inTrans>TRANS_NONE );
73350 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
73351 assert( pBt->pPage1 && pBt->pPage1->aData );
73352 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
73365 pCur->pgnoRoot = iTable;
73366 pCur->iPage = -1;
73367 pCur->pKeyInfo = pKeyInfo;
73368 pCur->pBtree = p;
73369 pCur->pBt = pBt;
73370 pCur->curFlags = 0;
73373 for(pX=pBt->pCursor; pX; pX=pX->pNext){
73374 if( pX->pgnoRoot==iTable ){
73375 pX->curFlags |= BTCF_Multiple;
73376 pCur->curFlags = BTCF_Multiple;
73379 pCur->eState = CURSOR_INVALID;
73380 pCur->pNext = pBt->pCursor;
73381 pBt->pCursor = pCur;
73383 pCur->curFlags |= BTCF_WriteFlag;
73384 pCur->curPagerFlags = 0;
73385 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
73387 pCur->curPagerFlags = PAGER_GET_READONLY;
73394 int wrFlag, /* 1 to write. 0 read-only */
73407 int wrFlag, /* 1 to write. 0 read-only */
73411 if( p->sharable ){
73423 ** to users so they cannot do the sizeof() themselves - they must call
73436 ** of run-time by skipping the initialization of those elements.
73447 Btree *pBtree = pCur->pBtree;
73449 BtShared *pBt = pCur->pBt;
73451 assert( pBt->pCursor!=0 );
73452 if( pBt->pCursor==pCur ){
73453 pBt->pCursor = pCur->pNext;
73455 BtCursor *pPrev = pBt->pCursor;
73457 if( pPrev->pNext==pCur ){
73458 pPrev->pNext = pCur->pNext;
73461 pPrev = pPrev->pNext;
73466 sqlite3_free(pCur->aOverflow);
73467 sqlite3_free(pCur->pKey);
73468 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
73471 assert( pBtree->sharable==0 );
73476 pCur->pBtree = 0;
73491 if( a->nKey!=b->nKey ) return 0;
73492 if( a->pPayload!=b->pPayload ) return 0;
73493 if( a->nPayload!=b->nPayload ) return 0;
73494 if( a->nLocal!=b->nLocal ) return 0;
73495 if( a->nSize!=b->nSize ) return 0;
73501 btreeParseCell(pCur->pPage, pCur->ix, &info);
73502 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
73508 if( pCur->info.nSize==0 ){
73509 pCur->curFlags |= BTCF_ValidNKey;
73510 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
73519 ** that is currently pointing to a row in a (non-empty) table.
73523 return pCur && pCur->eState==CURSOR_VALID;
73528 return pCur->eState==CURSOR_VALID;
73539 assert( pCur->eState==CURSOR_VALID );
73540 assert( pCur->curIntKey );
73542 return pCur->info.nKey;
73549 assert( (pCur->curFlags & BTCF_Pinned)==0 );
73550 pCur->curFlags |= BTCF_Pinned;
73553 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
73554 pCur->curFlags &= ~BTCF_Pinned;
73564 assert( pCur->eState==CURSOR_VALID );
73566 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
73567 (i64)(pCur->info.pPayload - pCur->pPage->aData);
73576 ** The caller must guarantee that the cursor is pointing to a non-NULL
73582 assert( pCur->eState==CURSOR_VALID );
73584 return pCur->info.nPayload;
73602 assert( pCur->eState==CURSOR_VALID );
73603 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
73609 ** linked list of overflow pages. If possible, it uses the auto-vacuum
73610 ** pointer-map data instead of reading the content of page ovfl to do so.
73622 ** the pointer-map was used to obtain the value for *pPgnoNext), then
73635 assert( sqlite3_mutex_held(pBt->mutex) );
73640 ** autovacuum pointer-map pages. Guess that the next page in
73645 if( pBt->autoVacuum ){
73669 next = get4byte(pPage->aData);
73697 int eOp, /* 0 -> copy from page, 1 -> copy to page */
73730 ** the overflow page-list cache array (BtCursor.aOverflow).
73734 ** Once an overflow page-list cache has been allocated, it must be
73736 ** the cursor is moved to a different row. Additionally, in auto-vacuum
73737 ** mode, the following events may invalidate an overflow page-list cache.
73748 int eOp /* zero to read. non-zero to write. */
73753 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
73754 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
73761 assert( pCur->eState==CURSOR_VALID );
73762 if( pCur->ix>=pPage->nCell ){
73768 aPayload = pCur->info.pPayload;
73769 assert( offset+amt <= pCur->info.nPayload );
73771 assert( aPayload > pPage->aData );
73772 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
73775 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
73782 if( offset<pCur->info.nLocal ){
73784 if( a+offset>pCur->info.nLocal ){
73785 a = pCur->info.nLocal - offset;
73787 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
73790 amt -= a;
73792 offset -= pCur->info.nLocal;
73797 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
73800 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
73809 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
73810 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
73811 if( pCur->aOverflow==0
73812 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
73815 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
73820 pCur->aOverflow = aNew;
73823 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
73824 pCur->curFlags |= BTCF_ValidOvfl;
73826 /* If the overflow page-list cache has been allocated and the
73830 if( pCur->aOverflow[offset/ovflSize] ){
73832 nextPage = pCur->aOverflow[iIdx];
73839 /* If required, populate the overflow page-list cache. */
73840 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
73841 assert( pCur->aOverflow[iIdx]==0
73842 || pCur->aOverflow[iIdx]==nextPage
73844 pCur->aOverflow[iIdx] = nextPage;
73850 ** page-list cache, if any, then fall back to the getOverflowPage()
73853 assert( pCur->curFlags & BTCF_ValidOvfl );
73854 assert( pCur->pBtree->db==pBt->db );
73855 if( pCur->aOverflow[iIdx+1] ){
73856 nextPage = pCur->aOverflow[iIdx+1];
73860 offset -= ovflSize;
73867 a = ovflSize - offset;
73875 ** 3) there are no dirty pages in the page-cache
73876 ** 4) the database is file-backed, and
73881 ** output buffer, bypassing the page-cache altogether. This speeds
73886 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
73887 && &pBuf[-4]>=pBufStart /* (6) */
73889 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
73891 u8 *aWrite = &pBuf[-4];
73894 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
73895 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
73903 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
73914 amt -= a;
73935 ** pCur can be pointing to either a table or an index b-tree.
73937 ** pCur is pointing to an index b-tree then the key section is read.
73949 assert( pCur->eState==CURSOR_VALID );
73950 assert( pCur->iPage>=0 && pCur->pPage );
73967 if ( pCur->eState==CURSOR_INVALID ){
73975 if( pCur->eState==CURSOR_VALID ){
73987 ** the key if index btrees (pPage->intKey==0) and is the data for
73988 ** table btrees (pPage->intKey==1). The number of bytes of available
74008 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
74009 assert( pCur->eState==CURSOR_VALID );
74010 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74012 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
74013 assert( pCur->info.nSize>0 );
74014 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
74015 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
74016 amt = pCur->info.nLocal;
74017 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
74021 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
74024 return (void*)pCur->info.pPayload;
74031 ** b-tree page. Write the number of available bytes into *pAmt.
74051 ** This function returns SQLITE_CORRUPT if the page-header flags field of
74053 ** if an intkey page appears to be the parent of a non-intkey page, or
74054 ** vice-versa).
74058 assert( pCur->eState==CURSOR_VALID );
74059 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
74060 assert( pCur->iPage>=0 );
74061 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
74064 pCur->info.nSize = 0;
74065 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74066 pCur->aiIdx[pCur->iPage] = pCur->ix;
74067 pCur->apPage[pCur->iPage] = pCur->pPage;
74068 pCur->ix = 0;
74069 pCur->iPage++;
74070 return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur,
74071 pCur->curPagerFlags);
74076 ** Page pParent is an internal (non-leaf) tree page. This function
74077 ** asserts that page number iChild is the left-child if the iIdx'th
74079 ** cells in pParent, that page number iChild is the right-child of
74085 assert( iIdx<=pParent->nCell );
74086 if( iIdx==pParent->nCell ){
74087 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
74099 ** pCur->idx is set to the cell index that contains the pointer
74101 ** right-most child page then pCur->idx is set to one more than
74107 assert( pCur->eState==CURSOR_VALID );
74108 assert( pCur->iPage>0 );
74109 assert( pCur->pPage );
74111 pCur->apPage[pCur->iPage-1],
74112 pCur->aiIdx[pCur->iPage-1],
74113 pCur->pPage->pgno
74115 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
74116 pCur->info.nSize = 0;
74117 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74118 pCur->ix = pCur->aiIdx[pCur->iPage-1];
74119 pLeaf = pCur->pPage;
74120 pCur->pPage = pCur->apPage[--pCur->iPage];
74125 ** Move the cursor to point to the root page of its b-tree structure.
74132 ** If the b-tree structure is empty, the cursor state is set to
74138 ** page-header flags indicate that the [virtual] root-page is the expected
74139 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
74141 ** indicating a table b-tree, or if the caller did specify a KeyInfo
74143 ** b-tree).
74153 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
74154 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
74156 if( pCur->iPage>=0 ){
74157 if( pCur->iPage ){
74158 releasePageNotNull(pCur->pPage);
74159 while( --pCur->iPage ){
74160 releasePageNotNull(pCur->apPage[pCur->iPage]);
74162 pRoot = pCur->pPage = pCur->apPage[0];
74165 }else if( pCur->pgnoRoot==0 ){
74166 pCur->eState = CURSOR_INVALID;
74169 assert( pCur->iPage==(-1) );
74170 if( pCur->eState>=CURSOR_REQUIRESEEK ){
74171 if( pCur->eState==CURSOR_FAULT ){
74172 assert( pCur->skipNext!=SQLITE_OK );
74173 return pCur->skipNext;
74177 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
74178 0, pCur->curPagerFlags);
74180 pCur->eState = CURSOR_INVALID;
74183 pCur->iPage = 0;
74184 pCur->curIntKey = pCur->pPage->intKey;
74186 pRoot = pCur->pPage;
74187 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
74189 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
74190 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
74191 ** NULL, the caller expects a table b-tree. If this is not the case,
74196 ** if pCur->iPage>=0). But this is not so if the database is corrupted
74197 ** in such a way that page pRoot is linked into a second b-tree table
74199 assert( pRoot->intKey==1 || pRoot->intKey==0 );
74200 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
74201 return SQLITE_CORRUPT_PAGE(pCur->pPage);
74205 pCur->ix = 0;
74206 pCur->info.nSize = 0;
74207 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
74209 if( pRoot->nCell>0 ){
74210 pCur->eState = CURSOR_VALID;
74211 }else if( !pRoot->leaf ){
74213 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
74214 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
74215 pCur->eState = CURSOR_VALID;
74218 pCur->eState = CURSOR_INVALID;
74225 ** Move the cursor down to the left-most leaf entry beneath the
74228 ** The left-most leaf is the one with the smallest key - the first
74237 assert( pCur->eState==CURSOR_VALID );
74238 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
74239 assert( pCur->ix<pPage->nCell );
74240 pgno = get4byte(findCell(pPage, pCur->ix));
74247 ** Move the cursor down to the right-most leaf entry beneath the
74250 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
74251 ** finds the right-most entry beneath the *page*.
74253 ** The right-most entry is the one with the largest key - the last
74262 assert( pCur->eState==CURSOR_VALID );
74263 while( !(pPage = pCur->pPage)->leaf ){
74264 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74265 pCur->ix = pPage->nCell;
74269 pCur->ix = pPage->nCell-1;
74270 assert( pCur->info.nSize==0 );
74271 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
74283 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74286 assert( pCur->pPage->nCell>0 );
74290 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74304 assert( pCur->eState==CURSOR_VALID );
74308 pCur->curFlags |= BTCF_AtLast;
74310 pCur->curFlags &= ~BTCF_AtLast;
74313 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74321 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74323 /* If the cursor already points to the last entry, this is a no-op. */
74324 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
74327 ** to the last entry in the b-tree. */
74329 for(ii=0; ii<pCur->iPage; ii++){
74330 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
74332 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
74333 testcase( pCur->ix!=pCur->pPage->nCell-1 );
74334 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
74335 assert( pCur->pPage->leaf );
74375 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74377 assert( pCur->pKeyInfo==0 );
74378 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
74382 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
74383 if( pCur->info.nKey==intKey ){
74387 if( pCur->info.nKey<intKey ){
74388 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
74389 *pRes = -1;
74396 if( pCur->info.nKey+1==intKey ){
74401 if( pCur->info.nKey==intKey ){
74412 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74418 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74419 *pRes = -1;
74424 assert( pCur->pPage );
74425 assert( pCur->pPage->isInit );
74426 assert( pCur->eState==CURSOR_VALID );
74427 assert( pCur->pPage->nCell > 0 );
74428 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
74429 assert( pCur->curIntKey );
74434 MemPage *pPage = pCur->pPage;
74437 /* pPage->nCell must be greater than zero. If this is the root-page
74439 ** not run. If this is not the root-page, then the moveToChild() routine
74441 ** be the right kind (index or table) of b-tree page. Otherwise
74443 assert( pPage->nCell>0 );
74444 assert( pPage->intKey );
74446 upr = pPage->nCell-1;
74448 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
74452 if( pPage->intKeyLeaf ){
74454 if( pCell>=pPage->aDataEnd ){
74462 if( lwr>upr ){ c = -1; break; }
74464 upr = idx-1;
74468 pCur->ix = (u16)idx;
74469 if( !pPage->leaf ){
74473 pCur->curFlags |= BTCF_ValidNKey;
74474 pCur->info.nKey = nCellKey;
74475 pCur->info.nSize = 0;
74483 assert( lwr==upr+1 || !pPage->leaf );
74484 assert( pPage->isInit );
74485 if( pPage->leaf ){
74486 assert( pCur->ix<pCur->pPage->nCell );
74487 pCur->ix = (u16)idx;
74493 if( lwr>=pPage->nCell ){
74494 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74498 pCur->ix = (u16)lwr;
74503 pCur->info.nSize = 0;
74504 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74509 ** Compare the "idx"-th cell on the page the cursor pCur is currently
74530 MemPage *pPage = pCur->pPage;
74536 if( nCell<=pPage->max1bytePayload ){
74537 /* This branch runs if the record-size field of the cell is a
74539 ** b-tree page. */
74540 testcase( pCell+nCell+1==pPage->aDataEnd );
74543 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
74545 /* The record-size field is a 2 byte varint and the record
74546 ** fits entirely on the main b-tree page. */
74547 testcase( pCell+nCell+2==pPage->aDataEnd );
74558 ** Return true (non-zero) if pCur is current pointing to the last
74563 assert( pCur->eState==CURSOR_VALID );
74564 for(i=0; i<pCur->iPage; i++){
74565 MemPage *pPage = pCur->apPage[i];
74566 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
74594 ** The pIdxKey->eqSeen field is set to 1 if there
74606 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74608 assert( pCur->pKeyInfo!=0 );
74611 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74615 pIdxKey->errCode = 0;
74616 assert( pIdxKey->default_rc==1
74617 || pIdxKey->default_rc==0
74618 || pIdxKey->default_rc==-1
74633 if( pCur->eState==CURSOR_VALID
74634 && pCur->pPage->leaf
74638 if( pCur->ix==pCur->pPage->nCell-1
74639 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
74640 && pIdxKey->errCode==SQLITE_OK
74645 if( pCur->iPage>0
74647 && pIdxKey->errCode==SQLITE_OK
74649 pCur->curFlags &= ~BTCF_ValidOvfl;
74650 if( !pCur->pPage->isInit ){
74655 pIdxKey->errCode = SQLITE_OK;
74661 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74662 *pRes = -1;
74669 assert( pCur->pPage );
74670 assert( pCur->pPage->isInit );
74671 assert( pCur->eState==CURSOR_VALID );
74672 assert( pCur->pPage->nCell > 0 );
74673 assert( pCur->curIntKey==0 );
74678 MemPage *pPage = pCur->pPage;
74681 /* pPage->nCell must be greater than zero. If this is the root-page
74683 ** not run. If this is not the root-page, then the moveToChild() routine
74685 ** be the right kind (index or table) of b-tree page. Otherwise
74687 assert( pPage->nCell>0 );
74688 assert( pPage->intKey==0 );
74690 upr = pPage->nCell-1;
74696 /* The maximum supported page-size is 65536 bytes. This means that
74697 ** the maximum number of record bytes stored on an index B-Tree
74698 ** page is less than 16384 bytes and may be stored as a 2-byte
74701 ** stored entirely within the b-tree page by inspecting the first
74705 if( nCell<=pPage->max1bytePayload ){
74706 /* This branch runs if the record-size field of the cell is a
74708 ** b-tree page. */
74709 testcase( pCell+nCell+1==pPage->aDataEnd );
74712 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
74714 /* The record-size field is a 2 byte varint and the record
74715 ** fits entirely on the main b-tree page. */
74716 testcase( pCell+nCell+2==pPage->aDataEnd );
74729 u8 * const pCellBody = pCell - pPage->childPtrSize;
74731 pPage->xParseCell(pPage, pCellBody, &pCur->info);
74732 nCell = (int)pCur->info.nKey;
74737 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
74746 pCur->ix = (u16)idx;
74749 pCur->curFlags &= ~BTCF_ValidOvfl;
74758 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
74759 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
74764 upr = idx-1;
74769 pCur->ix = (u16)idx;
74770 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
74777 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
74778 assert( pPage->isInit );
74779 if( pPage->leaf ){
74780 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
74781 pCur->ix = (u16)idx;
74786 if( lwr>=pPage->nCell ){
74787 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74791 pCur->ix = (u16)lwr;
74796 pCur->info.nSize = 0;
74797 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74814 return (CURSOR_VALID!=pCur->eState);
74827 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74832 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
74833 if( NEVER(pCur->pPage->leaf==0) ) return -1;
74835 n = pCur->pPage->nCell;
74836 for(i=0; i<pCur->iPage; i++){
74837 n *= pCur->apPage[i]->nCell;
74868 if( pCur->eState!=CURSOR_VALID ){
74869 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74874 if( CURSOR_INVALID==pCur->eState ){
74877 if( pCur->eState==CURSOR_SKIPNEXT ){
74878 pCur->eState = CURSOR_VALID;
74879 if( pCur->skipNext>0 ) return SQLITE_OK;
74883 pPage = pCur->pPage;
74884 idx = ++pCur->ix;
74885 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
74886 if( !pPage->isInit ){
74890 if( idx>=pPage->nCell ){
74891 if( !pPage->leaf ){
74892 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
74897 if( pCur->iPage==0 ){
74898 pCur->eState = CURSOR_INVALID;
74902 pPage = pCur->pPage;
74903 }while( pCur->ix>=pPage->nCell );
74904 if( pPage->intKey ){
74910 if( pPage->leaf ){
74921 pCur->info.nSize = 0;
74922 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74923 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
74924 pPage = pCur->pPage;
74925 if( (++pCur->ix)>=pPage->nCell ){
74926 pCur->ix--;
74929 if( pPage->leaf ){
74961 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
74962 assert( pCur->info.nSize==0 );
74963 if( pCur->eState!=CURSOR_VALID ){
74968 if( CURSOR_INVALID==pCur->eState ){
74971 if( CURSOR_SKIPNEXT==pCur->eState ){
74972 pCur->eState = CURSOR_VALID;
74973 if( pCur->skipNext<0 ) return SQLITE_OK;
74977 pPage = pCur->pPage;
74978 assert( pPage->isInit );
74979 if( !pPage->leaf ){
74980 int idx = pCur->ix;
74985 while( pCur->ix==0 ){
74986 if( pCur->iPage==0 ){
74987 pCur->eState = CURSOR_INVALID;
74992 assert( pCur->info.nSize==0 );
74993 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
74995 pCur->ix--;
74996 pPage = pCur->pPage;
74997 if( pPage->intKey && !pPage->leaf ){
75009 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
75010 pCur->info.nSize = 0;
75011 if( pCur->eState!=CURSOR_VALID
75012 || pCur->ix==0
75013 || pCur->pPage->leaf==0
75017 pCur->ix--;
75038 ** anywhere on the free-list, then it is guaranteed to be returned. If
75058 assert( sqlite3_mutex_held(pBt->mutex) );
75059 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
75060 pPage1 = pBt->pPage1;
75062 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
75064 n = get4byte(&pPage1->aData[36]);
75065 testcase( n==mxPage-1 );
75072 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
75075 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
75076 ** shows that the page 'nearby' is somewhere on the free-list, then
75077 ** the entire-list will be searched for that page.
75084 assert( pBt->autoVacuum );
75096 /* Decrement the free-list count by 1. Set iTrunk to the index of the
75097 ** first free-list trunk page. iPrevTrunk is initially 1.
75099 rc = sqlite3PagerWrite(pPage1->pDbPage);
75101 put4byte(&pPage1->aData[36], n-1);
75104 ** is not true. Otherwise, it runs once for each trunk-page on the
75105 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
75111 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
75114 iTrunk = get4byte(&pPrevTrunk->aData[0]);
75116 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
75119 iTrunk = get4byte(&pPage1->aData[32]);
75123 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
75132 assert( pTrunk->aData!=0 );
75133 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
75135 k = get4byte(&pTrunk->aData[4]);
75141 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75146 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
75149 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
75150 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
75164 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75170 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
75172 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75176 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
75180 ** pointers to free-list leaves. The first leaf becomes a trunk
75184 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
75194 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
75199 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
75200 put4byte(&pNewTrunk->aData[4], k-1);
75201 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
75204 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
75205 put4byte(&pPage1->aData[32], iNewTrunk);
75207 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75211 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
75215 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
75221 unsigned char *aData = pTrunk->aData;
75235 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
75237 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
75262 *pPgno, closest+1, k, pTrunk->pgno, n-1));
75263 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75265 if( closest<k-1 ){
75268 put4byte(&aData[4], k-1);
75272 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75289 ** pager layer with the 'no-content' flag set. This prevents the pager
75291 ** current transaction has already run one or more incremental-vacuum
75294 ** not set the no-content flag. This causes the pager to load and journal
75299 ** file on disk. So the effects of disabling the no-content optimization
75303 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
75305 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75307 pBt->nPage++;
75308 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
75311 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
75312 /* If *pPgno refers to a pointer-map page, allocate two new pages
75314 ** becomes a new pointer-map page, the second is used by the caller.
75317 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
75318 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
75319 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
75321 rc = sqlite3PagerWrite(pPg->pDbPage);
75325 pBt->nPage++;
75326 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
75329 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
75330 *pPgno = pBt->nPage;
75335 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75348 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
75349 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
75354 ** This function is used to add page iPage to the database file free-list.
75355 ** It is assumed that the page is not already a part of the free-list.
75366 MemPage *pTrunk = 0; /* Free-list trunk page */
75367 Pgno iTrunk = 0; /* Page number of free-list trunk page */
75368 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
75371 u32 nFree; /* Initial number of pages on free-list */
75373 assert( sqlite3_mutex_held(pBt->mutex) );
75375 assert( !pMemPage || pMemPage->pgno==iPage );
75377 if( iPage<2 || iPage>pBt->nPage ){
75382 sqlite3PagerRef(pPage->pDbPage);
75388 rc = sqlite3PagerWrite(pPage1->pDbPage);
75390 nFree = get4byte(&pPage1->aData[36]);
75391 put4byte(&pPage1->aData[36], nFree+1);
75393 if( pBt->btsFlags & BTS_SECURE_DELETE ){
75398 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
75402 memset(pPage->aData, 0, pPage->pBt->pageSize);
75405 /* If the database supports auto-vacuum, write an entry in the pointer-map
75413 /* Now manipulate the actual database free-list structure. There are two
75414 ** possibilities. If the free-list is currently empty, or if the first
75415 ** trunk page in the free-list is full, then this page will become a
75416 ** new free-list trunk page. Otherwise, it will become a leaf of the
75417 ** first trunk page in the current free-list. This block tests if it
75418 ** is possible to add the page as a new free-list leaf.
75423 iTrunk = get4byte(&pPage1->aData[32]);
75433 nLeaf = get4byte(&pTrunk->aData[4]);
75434 assert( pBt->usableSize>32 );
75435 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
75439 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
75444 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
75447 ** usableSize/4 - 8 entries will be reported as corrupt. In order
75449 ** we will continue to restrict the number of entries to usableSize/4 - 8
75452 ** to read "usableSize/4-2" instead of "usableSize/4-8".
75454 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
75459 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75461 put4byte(&pTrunk->aData[4], nLeaf+1);
75462 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
75463 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
75464 sqlite3PagerDontWrite(pPage->pDbPage);
75468 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
75474 ** the page being freed as a leaf page of the first trunk in the free-list.
75475 ** Possibly because the free-list is empty, or possibly because the
75476 ** first trunk in the free-list is full. Either way, the page being freed
75477 ** will become the new first trunk page in the free-list.
75482 rc = sqlite3PagerWrite(pPage->pDbPage);
75486 put4byte(pPage->aData, iTrunk);
75487 put4byte(&pPage->aData[4], 0);
75488 put4byte(&pPage1->aData[32], iPage);
75489 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
75493 pPage->isInit = 0;
75501 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
75519 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75520 assert( pInfo->nLocal!=pInfo->nPayload );
75521 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
75522 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
75523 if( pCell + pInfo->nSize > pPage->aDataEnd ){
75527 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
75528 pBt = pPage->pBt;
75529 assert( pBt->usableSize > 4 );
75530 ovflPageSize = pBt->usableSize - 4;
75531 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
75533 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
75535 while( nOvfl-- ){
75550 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
75557 ** freePage2() may zero the page contents if secure-delete mode is
75568 sqlite3PagerUnref(pOvfl->pDbPage);
75583 pPage->xParseCell(pPage, pCell, &sInfo); \
75598 ** Note that pCell does not necessary need to point to the pPage->aData
75600 ** be constructed in this temporary area then copied into pPage->aData
75620 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75624 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
75625 || sqlite3PagerIswriteable(pPage->pDbPage) );
75628 nHeader = pPage->childPtrSize;
75629 if( pPage->intKey ){
75630 nPayload = pX->nData + pX->nZero;
75631 pSrc = pX->pData;
75632 nSrc = pX->nData;
75633 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
75635 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
75637 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
75638 nSrc = nPayload = (int)pX->nKey;
75639 pSrc = pX->pKey;
75645 if( nPayload<=pPage->maxLocal ){
75656 memset(pPayload+nSrc, 0, nPayload-nSrc);
75663 mn = pPage->minLocal;
75664 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
75665 testcase( n==pPage->maxLocal );
75666 testcase( n==pPage->maxLocal+1 );
75667 if( n > pPage->maxLocal ) n = mn;
75673 pBt = pPage->pBt;
75690 pPage->xParseCell(pPage, pCell, &info);
75691 assert( nHeader==(int)(info.pPayload - pCell) );
75692 assert( info.nKey==pX->nKey );
75705 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
75709 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
75710 || sqlite3PagerIswriteable(pPage->pDbPage) );
75720 nPayload -= n;
75724 nSrc -= n;
75725 spaceLeft -= n;
75729 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
75730 if( pBt->autoVacuum ){
75740 /* If the database supports auto-vacuum, and the second or subsequent
75741 ** overflow page is being allocated, add an entry to the pointer-map
75745 ** to the pointer-map. If we write nothing to this pointer-map slot,
75750 if( pBt->autoVacuum && rc==SQLITE_OK ){
75765 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
75769 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
75770 || sqlite3PagerIswriteable(pPage->pDbPage) );
75775 pPrior = pOvfl->aData;
75777 pPayload = &pOvfl->aData[4];
75778 spaceLeft = pBt->usableSize - 4;
75786 ** Remove the i-th cell from pPage. This routine effects pPage only.
75795 u8 *data; /* pPage->aData */
75802 assert( idx<pPage->nCell );
75804 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75805 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75806 assert( pPage->nFree>=0 );
75807 data = pPage->aData;
75808 ptr = &pPage->aCellIdx[2*idx];
75809 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
75811 hdr = pPage->hdrOffset;
75813 testcase( pc+sz==pPage->pBt->usableSize );
75814 if( pc+sz > pPage->pBt->usableSize ){
75823 pPage->nCell--;
75824 if( pPage->nCell==0 ){
75827 put2byte(&data[hdr+5], pPage->pBt->usableSize);
75828 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
75829 - pPage->childPtrSize - 8;
75831 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
75832 put2byte(&data[hdr+3], pPage->nCell);
75833 pPage->nFree += 2;
75844 ** in pPage->apOvfl[] and make it point to the cell content (either
75846 ** Allocating a new entry in pPage->aCell[] implies that
75847 ** pPage->nOverflow is incremented.
75859 int i, /* New cell becomes the i-th cell of the page */
75863 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
75868 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
75870 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
75871 assert( MX_CELL(pPage->pBt)<=10921 );
75872 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
75873 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
75874 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
75875 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75876 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
75877 assert( pPage->nFree>=0 );
75879 if( pPage->nOverflow || sz+2>pPage->nFree ){
75885 j = pPage->nOverflow++;
75886 /* Comparison against ArraySize-1 since we hold back one extra slot
75889 assert( j < ArraySize(pPage->apOvfl)-1 );
75890 pPage->apOvfl[j] = pCell;
75891 pPage->aiOvfl[j] = (u16)i;
75898 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
75899 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
75901 int rc = sqlite3PagerWrite(pPage->pDbPage);
75905 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75906 data = pPage->aData;
75907 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
75913 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
75914 assert( idx+sz <= (int)pPage->pBt->usableSize );
75915 pPage->nFree -= (u16)(2 + sz);
75921 memcpy(&data[idx+4], pCell+4, sz-4);
75923 pIns = pPage->aCellIdx + i*2;
75924 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
75926 pPage->nCell++;
75928 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
75929 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
75931 if( pPage->pBt->autoVacuum ){
75956 int i, /* New cell becomes the i-th cell of the page */
75963 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
75965 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
75966 assert( MX_CELL(pPage->pBt)<=10921 );
75967 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
75968 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
75969 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
75970 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75971 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
75972 assert( pPage->nFree>=0 );
75973 assert( pPage->nOverflow==0 );
75974 if( sz+2>pPage->nFree ){
75975 j = pPage->nOverflow++;
75976 /* Comparison against ArraySize-1 since we hold back one extra slot
75979 assert( j < ArraySize(pPage->apOvfl)-1 );
75980 pPage->apOvfl[j] = pCell;
75981 pPage->aiOvfl[j] = (u16)i;
75988 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
75989 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
75991 int rc = sqlite3PagerWrite(pPage->pDbPage);
75995 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75996 data = pPage->aData;
75997 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
76003 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
76004 assert( idx+sz <= (int)pPage->pBt->usableSize );
76005 pPage->nFree -= (u16)(2 + sz);
76007 pIns = pPage->aCellIdx + i*2;
76008 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
76010 pPage->nCell++;
76012 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
76013 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
76015 if( pPage->pBt->autoVacuum ){
76041 ** tunable - as if you could change them and recompile and it would all work.
76056 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
76068 ** -----------
76070 ** -----------
76073 ** --------- --------- ---------
76074 ** |Child-1| |Child-2| |Child-3|
76075 ** --------- --------- ---------
76079 ** 1. All cells from Child-1 in order
76081 ** 3. All cells from Child-2 in order
76083 ** 5. All cells from Child-3 in order
76085 ** For a table-btree (with rowids) the items 2 and 4 are empty because
76089 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
76093 ** ixNx[0] = Number of cells in Child-1.
76094 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
76095 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
76096 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
76099 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
76102 ** ixNx[0] = Number of cells in Child-1.
76103 ** ixNx[1] = Number of cells in Child-1 and Child-2.
76122 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
76126 MemPage *pRef = p->pRef;
76127 u16 *szCell = p->szCell;
76128 assert( idx>=0 && idx+N<=p->nCell );
76130 assert( p->apCell[idx]!=0 );
76132 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
76135 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
76138 N--;
76146 assert( N>=0 && N<p->nCell );
76147 assert( p->szCell[N]==0 );
76148 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
76149 return p->szCell[N];
76152 assert( N>=0 && N<p->nCell );
76153 if( p->szCell[N] ) return p->szCell[N];
76158 ** Array apCell[] contains pointers to nCell b-tree page cells. The
76176 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
76177 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
76178 const int usableSize = pPg->pBt->usableSize;
76183 u8 *pCellptr = pPg->aCellIdx;
76184 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
76186 int k; /* Current slot in pCArray->apEnd[] */
76187 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
76192 memcpy(&pTmp[j], &aData[j], usableSize - j);
76194 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
76195 pSrcEnd = pCArray->apEnd[k];
76199 u8 *pCell = pCArray->apCell[i];
76200 u16 sz = pCArray->szCell[i];
76204 pCell = &pTmp[pCell - aData];
76211 pData -= sz;
76212 put2byte(pCellptr, (pData - aData));
76216 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
76219 if( pCArray->ixNx[k]<=i ){
76221 pSrcEnd = pCArray->apEnd[k];
76225 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
76226 pPg->nCell = nCell;
76227 pPg->nOverflow = 0;
76230 put2byte(&aData[hdr+3], pPg->nCell);
76231 put2byte(&aData[hdr+5], pData - aData);
76237 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
76240 ** will fit), non-zero is returned. Otherwise, if the cells are added
76243 ** Argument pCellptr points to the first entry in the cell-pointer array
76245 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
76247 ** that it is safe to overwrite this part of the cell-pointer array.
76255 ** end of the space required by this page for the cell-pointer area (for
76256 ** all cells - not just those inserted by the current call). If the content
76258 ** cells in apCell[], then the cells do not fit and non-zero is returned.
76262 u8 *pBegin, /* End of cell-pointer array */
76263 u8 **ppData, /* IN/OUT: Page content-area pointer */
76264 u8 *pCellptr, /* Pointer to cell-pointer area */
76269 int i = iFirst; /* Loop counter - cell index to insert */
76270 u8 *aData = pPg->aData; /* Complete page */
76273 int k; /* Current slot in pCArray->apEnd[] */
76275 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
76277 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
76278 pEnd = pCArray->apEnd[k];
76282 assert( pCArray->szCell[i]!=0 );
76283 sz = pCArray->szCell[i];
76285 if( (pData - pBegin)<sz ) return 1;
76286 pData -= sz;
76289 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
76292 assert( (pSlot+sz)<=pCArray->apCell[i]
76293 || pSlot>=(pCArray->apCell[i]+sz)
76295 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
76296 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
76302 memmove(pSlot, pCArray->apCell[i], sz);
76303 put2byte(pCellptr, (pSlot - aData));
76307 if( pCArray->ixNx[k]<=i ){
76309 pEnd = pCArray->apEnd[k];
76317 ** The pCArray object contains pointers to b-tree cells and their sizes.
76320 ** that is currently stored within the body of pPg to the pPg free-list.
76321 ** The cell-pointers and other fields of the page are not updated.
76323 ** This function returns the total number of cells added to the free-list.
76331 u8 * const aData = pPg->aData;
76332 u8 * const pEnd = &aData[pPg->pBt->usableSize];
76333 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
76342 u8 *pCell = pCArray->apCell[i];
76350 sz = pCArray->szCell[i]; assert( sz>0 );
76351 iOfst = (u16)(pCell - aData);
76365 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
76378 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
76385 ** balanced. The current page, pPg, has pPg->nCell cells starting with
76386 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
76392 ** The pPg->nFree field is invalid when this function returns. It is the
76402 u8 * const aData = pPg->aData;
76403 const int hdr = pPg->hdrOffset;
76404 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
76405 int nCell = pPg->nCell; /* Cells stored on pPg */
76409 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
76413 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
76414 memcpy(pTmp, aData, pPg->pBt->usableSize);
76420 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
76422 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
76423 nCell -= nShift;
76426 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
76428 nCell -= nTail;
76433 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
76437 int nAdd = MIN(nNew,iOld-iNew);
76438 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
76440 pCellptr = pPg->aCellIdx;
76450 for(i=0; i<pPg->nOverflow; i++){
76451 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
76453 pCellptr = &pPg->aCellIdx[iCell * 2];
76455 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
76468 pCellptr = &pPg->aCellIdx[nCell*2];
76471 iNew+nCell, nNew-nCell, pCArray
76474 pPg->nCell = nNew;
76475 pPg->nOverflow = 0;
76477 put2byte(&aData[hdr+3], pPg->nCell);
76478 put2byte(&aData[hdr+5], pData - aData);
76482 u8 *pCell = pCArray->apCell[i+iNew];
76483 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
76484 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
76485 pCell = &pTmp[pCell - aData];
76488 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
76503 ** a new entry is being inserted on the extreme right-end of the
76507 ** Instead of trying to balance the 3 right-most leaf pages, just add
76508 ** a new page to the right-hand side and put the one new entry in
76514 ** pPage is the leaf page which is the right-most page in the tree.
76516 ** which is also the right-most entry on the page.
76525 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
76530 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76531 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76532 assert( pPage->nOverflow==1 );
76534 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
76535 assert( pPage->nFree>=0 );
76536 assert( pParent->nFree>=0 );
76538 /* Allocate a new page. This page will become the right-sibling of
76547 u8 *pCell = pPage->apOvfl[0];
76548 u16 szCell = pPage->xCellSize(pPage, pCell);
76552 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
76553 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
76559 b.apEnd[0] = pPage->aDataEnd;
76566 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
76568 /* If this is an auto-vacuum database, update the pointer map
76578 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
76579 if( szCell>pNew->minLocal ){
76585 ** consists of a 4-byte page number (the page number of pPage) and
76589 ** To find the largest key value on pPage, first find the right-most
76591 ** record-length (a variable length integer at most 32-bits in size)
76593 ** The first of the while(...) loops below skips over the record-length
76597 pCell = findCell(pPage, pPage->nCell-1);
76605 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
76606 0, pPage->pgno);
76609 /* Set the right-child pointer of pParent to point to the new page. */
76610 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
76624 ** for setting pointer-map entries.
76632 BtShared *pBt = pPage->pBt;
76633 assert( pPage->isInit );
76635 for(j=0; j<pPage->nCell; j++){
76640 pPage->xParseCell(pPage, z, &info);
76642 Pgno ovfl = get4byte(&z[info.nSize-4]);
76644 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
76646 if( !pPage->leaf ){
76649 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76652 if( !pPage->leaf ){
76653 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76655 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76663 ** This function is used to copy the contents of the b-tree node stored
76665 ** the pointer-map entries for each child page are updated so that the
76681 BtShared * const pBt = pFrom->pBt;
76682 u8 * const aFrom = pFrom->aData;
76683 u8 * const aTo = pTo->aData;
76684 int const iFromHdr = pFrom->hdrOffset;
76685 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
76690 assert( pFrom->isInit );
76691 assert( pFrom->nFree>=iToHdr );
76692 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
76694 /* Copy the b-tree node content from page pFrom to page pTo. */
76696 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
76697 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
76704 pTo->isInit = 0;
76712 /* If this is an auto-vacuum database, update the pointer-map entries
76713 ** for any b-tree or overflow pages that pTo now contains the pointers to.
76755 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
76764 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
76765 int isRoot, /* True if pParent is a root-page */
76773 int nxDiv; /* Next divider slot in pParent->aCell[] */
76778 int pageFlags; /* Value of pPage->aData[0] */
76784 u8 *pRight; /* Location in parent of right-sibling pointer */
76785 u8 *apDiv[NB-1]; /* Divider cells in pParent */
76786 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
76788 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
76797 pBt = pParent->pBt;
76798 assert( sqlite3_mutex_held(pBt->mutex) );
76799 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76806 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
76807 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
76812 assert( pParent->nFree>=0 );
76825 i = pParent->nOverflow + pParent->nCell;
76833 nxDiv = i-2+bBulk;
76835 nxDiv = iParentIdx-1;
76837 i = 2-bBulk;
76840 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
76841 pRight = &pParent->aData[pParent->hdrOffset+8];
76843 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
76854 if( apOld[i]->nFree<0 ){
76861 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
76862 if( (i--)==0 ) break;
76864 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
76865 apDiv[i] = pParent->apOvfl[0];
76867 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
76868 pParent->nOverflow = 0;
76870 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
76872 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
76881 ** But not if we are in secure-delete mode. In secure-delete mode,
76886 if( pBt->btsFlags & BTS_FAST_SECURE ){
76891 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
76892 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
76894 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
76897 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
76901 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
76911 + pBt->pageSize; /* aSpace1 */
76913 assert( szScratch<=7*(int)pBt->pageSize );
76940 leafCorrection = b.pRef->leaf*4;
76941 leafData = b.pRef->intKeyLeaf;
76944 int limit = pOld->nCell;
76945 u8 *aData = pOld->aData;
76946 u16 maskPage = pOld->maskPage;
76947 u8 *piCell = aData + pOld->cellOffset;
76951 /* Verify that all sibling pages are of the same "type" (table-leaf,
76952 ** table-interior, index-leaf, or index-interior).
76954 if( pOld->aData[0]!=apOld[0]->aData[0] ){
76976 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
76977 if( pOld->nOverflow>0 ){
76978 if( NEVER(limit<pOld->aiOvfl[0]) ){
76982 limit = pOld->aiOvfl[0];
76988 for(k=0; k<pOld->nOverflow; k++){
76989 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
76990 b.apCell[b.nCell] = pOld->apOvfl[k];
76994 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
77001 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
77004 if( i<nOld-1 && !leafData){
77011 assert( sz<=pBt->maxLocal+23 );
77012 assert( iSpace1 <= (int)pBt->pageSize );
77016 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
77017 if( !pOld->leaf ){
77019 assert( pOld->hdrOffset==0 || CORRUPT_DB );
77022 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
77029 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
77041 ** size of all cells on the i-th page and cntNew[] which is the index
77048 ** szNew[i]: Spaced used on the i-th sibling page.
77050 ** the right of the i-th sibling page.
77054 usableSpace = pBt->usableSize - 12 + leafCorrection;
77057 b.apEnd[k] = p->aDataEnd;
77059 if( k && b.ixNx[k]==b.ixNx[k-1] ){
77060 k--; /* Omit b.ixNx[] entry for child pages with no cells */
77064 b.apEnd[k] = pParent->aDataEnd;
77067 assert( p->nFree>=0 );
77068 szNew[i] = usableSpace - p->nFree;
77069 for(j=0; j<p->nOverflow; j++){
77070 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
77081 szNew[k-1] = 0;
77082 cntNew[k-1] = b.nCell;
77084 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
77085 szNew[i] -= sz;
77094 cntNew[i]--;
77108 szNew[i+1] -= sz;
77112 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
77121 ** always nearly full, while the right-most sibling might be nearly empty.
77126 ** be so out of balance as to be illegal. For example, the right-most
77129 for(i=k-1; i>0; i--){
77131 int szLeft = szNew[i-1]; /* Size of sibling on the left */
77132 int r; /* Index of right-most cell in left sibling */
77135 r = cntNew[i-1] - 1;
77136 d = r + 1 - leafData;
77145 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
77149 szLeft -= szR + 2;
77150 cntNew[i-1] = r;
77151 r--;
77152 d--;
77155 szNew[i-1] = szLeft;
77156 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
77162 /* Sanity check: For a non-corrupt database file one of the follwing
77169 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
77171 apOld[0]->pgno, apOld[0]->nCell,
77172 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
77173 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
77179 pageFlags = apOld[0]->aData[0];
77185 rc = sqlite3PagerWrite(pNew->pDbPage);
77187 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
77202 /* Set the pointer-map entry for the new sibling page. */
77204 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
77225 aPgno[i] = apNew[i]->pgno;
77226 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
77227 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
77229 for(i=0; i<nNew-1; i++){
77232 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
77241 Pgno pgnoA = apNew[i]->pgno;
77242 Pgno pgnoB = apNew[iB]->pgno;
77243 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
77244 u16 fgA = apNew[i]->pDbPage->flags;
77245 u16 fgB = apNew[iB]->pDbPage->flags;
77246 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
77247 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
77248 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
77249 apNew[i]->pgno = pgnoB;
77250 apNew[iB]->pgno = pgnoA;
77256 apNew[0]->pgno, szNew[0], cntNew[0],
77257 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
77258 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
77259 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
77260 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
77261 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
77262 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
77263 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
77264 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
77267 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77269 assert( apNew[nNew-1]!=0 );
77270 put4byte(pRight, apNew[nNew-1]->pgno);
77272 /* If the sibling pages are not leaves, ensure that the right-child pointer
77273 ** of the right-most new sibling page is set to the value that was
77274 ** originally in the same field of the right-most old sibling page. */
77276 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
77277 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
77292 ** associated with the right-child of each sibling may also need to be
77299 int cntOldNext = pNew->nCell + pNew->nOverflow;
77310 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
77324 || pNew->pgno!=aPgno[iOld]
77325 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
77328 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
77330 if( cachedCellSize(&b,i)>pNew->minLocal ){
77339 for(i=0; i<nNew-1; i++){
77352 if( !pNew->leaf ){
77353 memcpy(&pNew->aData[8], pCell, 4);
77355 /* If the tree is a leaf-data tree, and the siblings are leaves,
77357 ** cell consists of the integer key for the right-most cell of
77358 ** the sibling-page assembled above only.
77361 j--;
77362 pNew->xParseCell(pNew, b.apCell[j], &info);
77367 pCell -= 4;
77368 /* Obscure case for non-leaf-data trees: If the cell at pCell was
77375 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
77381 sz = pParent->xCellSize(pParent, pCell);
77385 assert( sz<=pBt->maxLocal+23 );
77386 assert( iOvflSpace <= (int)pBt->pageSize );
77393 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
77395 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77402 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
77404 ** the left-hand sibling apNew[iPg-1] has been updated.
77408 ** the right-hand sibling apNew[iPg+1] has been updated.
77412 ** The iPg value in the following loop starts at nNew-1 goes down
77413 ** to 0, then back up to nNew-1 again, thus making two passes over
77420 for(i=1-nNew; i<nNew; i++){
77421 int iPg = i<0 ? -i : i;
77425 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
77432 ** only after iPg-1 has already been updated. */
77433 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
77443 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
77444 iNew = cntNew[iPg-1] + !leafData;
77445 nNewCell = cntNew[iPg] - iNew;
77451 apNew[iPg]->nFree = usableSpace-szNew[iPg];
77452 assert( apNew[iPg]->nOverflow==0 );
77453 assert( apNew[iPg]->nCell==nNewCell );
77463 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
77464 /* The root page of the b-tree now contains no cells. The only sibling
77465 ** page is the right-child of the parent. Copy the contents of the
77467 ** b-tree structure by one. This is described as the "balance-shallower"
77468 ** sub-algorithm in some documentation.
77470 ** If this is an auto-vacuum database, the call to copyNodeContent()
77471 ** sets all pointer-map entries corresponding to database image pages
77480 rc = defragmentPage(apNew[0], -1);
77482 assert( apNew[0]->nFree ==
77483 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
77484 - apNew[0]->nCell*2)
77490 /* Fix the pointer map entries associated with the right-child of each
77494 u32 key = get4byte(&apNew[i]->aData[8]);
77495 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
77499 assert( pParent->isInit );
77510 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
77537 ** This function is called when the root page of a b-tree structure is
77542 ** page is then overwritten to make it an empty page with the right-child
77545 ** Before returning, all pointer-map entries corresponding to pages
77546 ** that the new child-page now contains pointers to are updated. The
77547 ** entry corresponding to the new right-child pointer of the root
77559 BtShared *pBt = pRoot->pBt; /* The BTree */
77561 assert( pRoot->nOverflow>0 );
77562 assert( sqlite3_mutex_held(pBt->mutex) );
77564 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
77565 ** page that will become the new right-child of pPage. Copy the contents
77568 rc = sqlite3PagerWrite(pRoot->pDbPage);
77570 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
77573 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
77581 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
77582 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
77583 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
77585 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
77588 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
77589 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
77590 memcpy(pChild->apOvfl, pRoot->apOvfl,
77591 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
77592 pChild->nOverflow = pRoot->nOverflow;
77594 /* Zero the contents of pRoot. Then install pChild as the right-child. */
77595 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
77596 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
77604 ** on the same B-tree as pCur.
77607 ** pointing to the same b-tree. If an insert occurs on one SQL table
77609 ** table linked to the same b-tree. If the secondary insert causes a
77615 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
77617 && pOther->eState==CURSOR_VALID
77618 && pOther->pPage==pCur->pPage
77646 MemPage *pPage = pCur->pPage;
77648 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
77649 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
77655 }else if( (iPage = pCur->iPage)==0 ){
77656 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
77657 /* The root page of the b-tree is overfull. In this case call the
77658 ** balance_deeper() function to create a new child for the root-page
77659 ** and copy the current contents of the root-page to it. The
77660 ** next iteration of the do-loop will balance the child page.
77664 rc = balance_deeper(pPage, &pCur->apPage[1]);
77666 pCur->iPage = 1;
77667 pCur->ix = 0;
77668 pCur->aiIdx[0] = 0;
77669 pCur->apPage[0] = pPage;
77670 pCur->pPage = pCur->apPage[1];
77671 assert( pCur->pPage->nOverflow );
77676 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
77682 MemPage * const pParent = pCur->apPage[iPage-1];
77683 int const iIdx = pCur->aiIdx[iPage-1];
77685 rc = sqlite3PagerWrite(pParent->pDbPage);
77686 if( rc==SQLITE_OK && pParent->nFree<0 ){
77691 if( pPage->intKeyLeaf
77692 && pPage->nOverflow==1
77693 && pPage->aiOvfl[0]==pPage->nCell
77694 && pParent->pgno!=1
77695 && pParent->nCell==iIdx
77700 ** happens, the next iteration of the do-loop will balance pParent
77719 ** become overfull or underfull. The next iteration of the do-loop
77724 ** A subsequent iteration of the do-loop will deal with this by
77726 ** but it doesn't deal with overflow cells - just moves them to a
77733 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
77735 pCur->hints&BTREE_BULKLOAD);
77751 pPage->nOverflow = 0;
77753 /* The next iteration of the do-loop balances the parent page. */
77755 pCur->iPage--;
77756 assert( pCur->iPage>=0 );
77757 pCur->pPage = pCur->apPage[pCur->iPage];
77777 int nData = pX->nData - iOffset;
77783 int rc = sqlite3PagerWrite(pPage->pDbPage);
77785 memset(pDest + i, 0, iAmt - i);
77792 iAmt-nData);
77796 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
77797 int rc = sqlite3PagerWrite(pPage->pDbPage);
77803 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
77818 int iOffset; /* Next byte of pX->pData to write */
77819 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
77821 MemPage *pPage = pCur->pPage; /* Page being written */
77826 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
77829 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
77830 0, pCur->info.nLocal);
77834 iOffset = pCur->info.nLocal;
77837 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
77838 pBt = pPage->pBt;
77839 ovflPageSize = pBt->usableSize - 4;
77843 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
77847 ovflPgno = get4byte(pPage->aData);
77849 ovflPageSize = nTotal - iOffset;
77851 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
77854 sqlite3PagerUnref(pPage->pDbPage);
77866 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
77867 MemPage *pPage = pCur->pPage; /* Page being written */
77869 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
77870 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
77874 if( pCur->info.nLocal==nTotal ){
77876 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
77877 0, pCur->info.nLocal);
77900 ** If the seekResult parameter is non-zero, then a successful call to
77911 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
77912 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
77922 int loc = seekResult; /* -1: before desired location +1: after */
77926 Btree *p = pCur->pBtree;
77931 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
77935 ** In some cases, the call to btreeMoveto() below is a no-op. For
77936 ** example, when inserting data into a table with auto-generated integer
77939 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
77944 if( pCur->curFlags & BTCF_Multiple ){
77945 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
77947 if( loc && pCur->iPage<0 ){
77960 if( pCur->eState>=CURSOR_REQUIRESEEK ){
77961 testcase( pCur->eState==CURSOR_REQUIRESEEK );
77962 testcase( pCur->eState==CURSOR_FAULT );
77968 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
77969 && p->pBt->inTransaction==TRANS_WRITE
77970 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
77971 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
77974 ** expecting an index b-tree, then the caller should be inserting blob
77978 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
77980 if( pCur->pKeyInfo==0 ){
77981 assert( pX->pKey==0 );
77982 /* If this is an insert into a table b-tree, invalidate any incrblob
77984 if( p->hasIncrblobCur ){
77985 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
77993 assert( pCur->curFlags & BTCF_ValidNKey );
77994 assert( pX->nKey==pCur->info.nKey );
78003 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
78006 assert( pX->nData>=0 && pX->nZero>=0 );
78007 if( pCur->info.nSize!=0
78008 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
78019 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
78037 if( pX->nMem ){
78039 r.pKeyInfo = pCur->pKeyInfo;
78040 r.aMem = pX->aMem;
78041 r.nField = pX->nMem;
78046 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
78058 if( pCur->info.nKey==pX->nKey ){
78060 x2.pData = pX->pKey;
78061 x2.nData = pX->nKey;
78067 assert( pCur->eState==CURSOR_VALID
78068 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
78070 pPage = pCur->pPage;
78071 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
78072 assert( pPage->leaf || !pPage->intKey );
78073 if( pPage->nFree<0 ){
78074 if( NEVER(pCur->eState>CURSOR_INVALID) ){
78075 /* ^^^^^--- due to the moveToRoot() call above */
78084 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
78086 assert( pPage->isInit || CORRUPT_DB );
78087 newCell = p->pBt->pTmpSpace;
78092 szNew = p->pBt->nPreformatSize;
78094 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
78096 pPage->xParseCell(pPage, newCell, &info);
78098 Pgno ovfl = get4byte(&newCell[szNew-4]);
78099 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
78107 assert( szNew==pPage->xCellSize(pPage, newCell) );
78108 assert( szNew <= MX_CELL_SIZE(p->pBt) );
78109 idx = pCur->ix;
78110 pCur->info.nSize = 0;
78114 if( idx>=pPage->nCell ){
78117 rc = sqlite3PagerWrite(pPage->pDbPage);
78122 if( !pPage->leaf ){
78126 testcase( pCur->curFlags & BTCF_ValidOvfl );
78129 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
78139 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
78141 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
78144 if( oldCell+szNew > pPage->aDataEnd ){
78152 }else if( loc<0 && pPage->nCell>0 ){
78153 assert( pPage->leaf );
78154 idx = ++pCur->ix;
78155 pCur->curFlags &= ~BTCF_ValidNKey;
78157 assert( pPage->leaf );
78160 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
78161 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
78175 ** multiple records into an intkey b-tree using a single cursor (as can
78178 ** the b-tree if possible. If the cursor is left pointing to the last
78183 if( pPage->nOverflow ){
78185 pCur->curFlags &= ~(BTCF_ValidNKey);
78192 pCur->pPage->nOverflow = 0;
78193 pCur->eState = CURSOR_INVALID;
78196 if( pCur->pKeyInfo ){
78197 assert( pCur->pKey==0 );
78198 pCur->pKey = sqlite3Malloc( pX->nKey );
78199 if( pCur->pKey==0 ){
78202 memcpy(pCur->pKey, pX->pKey, pX->nKey);
78205 pCur->eState = CURSOR_REQUIRESEEK;
78206 pCur->nKey = pX->nKey;
78209 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
78231 BtShared *pBt = pDest->pBt;
78232 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
78238 if( pSrc->info.nPayload<0x80 ){
78239 *(aOut++) = pSrc->info.nPayload;
78241 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
78243 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
78244 nIn = pSrc->info.nLocal;
78245 aIn = pSrc->info.pPayload;
78246 if( aIn+nIn>pSrc->pPage->aDataEnd ){
78249 nRem = pSrc->info.nPayload;
78250 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
78252 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
78256 Pager *pSrcPager = pSrc->pBt->pPager;
78263 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
78264 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
78265 if( nOut<pSrc->info.nPayload ){
78267 pBt->nPreformatSize += 4;
78271 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
78274 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
78278 nRem -= nOut;
78284 nOut -= nCopy;
78285 nIn -= nCopy;
78297 nIn = pSrc->pBt->usableSize - 4;
78308 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
78313 pPgnoOut = pPageOut->aData;
78316 nOut = MIN(pBt->usableSize - 4, nRem);
78340 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
78345 Btree *p = pCur->pBtree;
78346 BtShared *pBt = p->pBt;
78356 assert( pBt->inTransaction==TRANS_WRITE );
78357 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78358 assert( pCur->curFlags & BTCF_WriteFlag );
78359 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
78360 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
78362 if( pCur->eState!=CURSOR_VALID ){
78363 if( pCur->eState>=CURSOR_REQUIRESEEK ){
78365 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
78366 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
78371 assert( pCur->eState==CURSOR_VALID );
78373 iCellDepth = pCur->iPage;
78374 iCellIdx = pCur->ix;
78375 pPage = pCur->pPage;
78376 if( pPage->nCell<=iCellIdx ){
78380 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
78383 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
78389 ** will cause a b-tree rebalance, then this is done by saving the cursor
78405 if( !pPage->leaf
78406 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
78407 (int)(pBt->usableSize*2/3)
78408 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
78410 /* A b-tree rebalance will be required after deleting this entry.
78424 ** sub-tree headed by the child page of the cell being deleted. This makes
78426 if( !pPage->leaf ){
78434 if( pCur->curFlags & BTCF_Multiple ){
78435 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
78439 /* If this is a delete operation to remove a row from a table b-tree,
78441 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
78442 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
78448 rc = sqlite3PagerWrite(pPage->pDbPage);
78455 ** is currently pointing to the largest entry in the sub-tree headed
78456 ** by the child-page of the cell that was just deleted from an internal
78459 if( !pPage->leaf ){
78460 MemPage *pLeaf = pCur->pPage;
78465 if( pLeaf->nFree<0 ){
78469 if( iCellDepth<pCur->iPage-1 ){
78470 n = pCur->apPage[iCellDepth+1]->pgno;
78472 n = pCur->pPage->pgno;
78474 pCell = findCell(pLeaf, pLeaf->nCell-1);
78475 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
78476 nCell = pLeaf->xCellSize(pLeaf, pCell);
78478 pTmp = pBt->pTmpSpace;
78480 rc = sqlite3PagerWrite(pLeaf->pDbPage);
78482 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
78484 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
78503 assert( pCur->pPage->nOverflow==0 );
78504 assert( pCur->pPage->nFree>=0 );
78505 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
78507 ** then balance() will always be a no-op. No need to invoke it. */
78512 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
78513 releasePageNotNull(pCur->pPage);
78514 pCur->iPage--;
78515 while( pCur->iPage>iCellDepth ){
78516 releasePage(pCur->apPage[pCur->iPage--]);
78518 pCur->pPage = pCur->apPage[pCur->iPage];
78524 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
78525 assert( pPage==pCur->pPage || CORRUPT_DB );
78526 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
78527 pCur->eState = CURSOR_SKIPNEXT;
78528 if( iCellIdx>=pPage->nCell ){
78529 pCur->skipNext = -1;
78530 pCur->ix = pPage->nCell-1;
78532 pCur->skipNext = 1;
78538 pCur->eState = CURSOR_REQUIRESEEK;
78558 BtShared *pBt = p->pBt;
78562 int ptfFlags; /* Page-type flage for the root page of new table */
78565 assert( pBt->inTransaction==TRANS_WRITE );
78566 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78574 if( pBt->autoVacuum ){
78575 Pgno pgnoMove; /* Move a page here to make room for the root-page */
78580 ** out to be an overflow page, delete all overflow page-map caches
78586 ** root page of the new table should go. meta[3] is the largest root-page
78587 ** created so far, so the new root-page is (meta[3]+1).
78595 /* The new root-page may not be allocated on a pointer-map page, or the
78614 /* pgnoRoot is the page that will be used for the root-page of
78658 rc = sqlite3PagerWrite(pRoot->pDbPage);
78667 /* Update the pointer-map and meta-data with the new root-page number. */
78678 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
78690 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
78697 sqlite3PagerUnref(pRoot->pDbPage);
78698 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
78727 assert( sqlite3_mutex_held(pBt->mutex) );
78733 if( (pBt->openFlags & BTREE_SINGLE)==0
78734 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
78739 hdr = pPage->hdrOffset;
78740 for(i=0; i<pPage->nCell; i++){
78742 if( !pPage->leaf ){
78749 if( !pPage->leaf ){
78750 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
78752 if( pPage->intKey ) pnChange = 0;
78755 testcase( !pPage->intKey );
78756 *pnChange += pPage->nCell;
78760 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
78761 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
78783 BtShared *pBt = p->pBt;
78785 assert( p->inTrans==TRANS_WRITE );
78791 ** is the root of a table b-tree - if it is not, the following call is
78792 ** a no-op). */
78793 if( p->hasIncrblobCur ){
78808 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
78834 BtShared *pBt = p->pBt;
78837 assert( p->inTrans==TRANS_WRITE );
78857 if( pBt->autoVacuum ){
78862 /* If the table being dropped is the table with the largest root-page
78871 /* The table being dropped does not have the largest root-page
78873 ** gap left by the deleted root-page.
78896 /* Set the new 'max-root-page' value in the database header. This
78898 ** be a root-page number, less one again if that is the
78901 maxRootPgno--;
78904 maxRootPgno--;
78926 ** This function may only be called if the b-tree connection already
78929 ** Read the meta-information out of a database file. Meta[0]
78932 ** is read-only, the others are read/write.
78946 BtShared *pBt = p->pBt;
78949 assert( p->inTrans>TRANS_NONE );
78951 assert( pBt->pPage1 );
78955 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
78957 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
78960 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
78961 ** database, mark the database as read-only. */
78964 pBt->btsFlags |= BTS_READ_ONLY;
78972 ** Write meta-information back into the database. Meta[0] is
78973 ** read-only and may not be written.
78976 BtShared *pBt = p->pBt;
78981 assert( p->inTrans==TRANS_WRITE );
78982 assert( pBt->pPage1!=0 );
78983 pP1 = pBt->pPage1->aData;
78984 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
78989 assert( pBt->autoVacuum || iMeta==0 );
78991 pBt->incrVacuum = (u8)iMeta;
79000 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
79001 ** number of entries in the b-tree and write the result to *pnEntry.
79018 ** page in the B-Tree structure (not including overflow pages).
79020 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
79022 MemPage *pPage; /* Current page of the b-tree */
79024 /* If this is a leaf page or the tree is not an int-key tree, then
79028 pPage = pCur->pPage;
79029 if( pPage->leaf || !pPage->intKey ){
79030 nEntry += pPage->nCell;
79036 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
79038 ** to visit is the right-child of its parent.
79043 if( pPage->leaf ){
79045 if( pCur->iPage==0 ){
79046 /* All pages of the b-tree have been visited. Return successfully. */
79051 }while ( pCur->ix>=pCur->pPage->nCell );
79053 pCur->ix++;
79054 pPage = pCur->pPage;
79058 ** points at. This is the right-child if (iIdx==pPage->nCell).
79060 iIdx = pCur->ix;
79061 if( iIdx==pPage->nCell ){
79062 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
79077 return p->pBt->pPager;
79085 pCheck->rc = SQLITE_NOMEM;
79086 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
79087 if( pCheck->nErr==0 ) pCheck->nErr++;
79095 sqlite3 *db = pCheck->db;
79096 if( AtomicLoad(&db->u1.isInterrupted) ){
79097 pCheck->rc = SQLITE_INTERRUPT;
79098 pCheck->nErr++;
79099 pCheck->mxErr = 0;
79102 if( db->xProgress ){
79103 assert( db->nProgressOps>0 );
79104 pCheck->nStep++;
79105 if( (pCheck->nStep % db->nProgressOps)==0
79106 && db->xProgress(db->pProgressArg)
79108 pCheck->rc = SQLITE_INTERRUPT;
79109 pCheck->nErr++;
79110 pCheck->mxErr = 0;
79126 if( !pCheck->mxErr ) return;
79127 pCheck->mxErr--;
79128 pCheck->nErr++;
79130 if( pCheck->errMsg.nChar ){
79131 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
79133 if( pCheck->zPfx ){
79134 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
79135 pCheck->v0, pCheck->v1, pCheck->v2);
79137 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
79139 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
79148 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
79152 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
79153 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
79160 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
79161 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
79167 ** reference to the page, add an error message to pCheck->zErrMsg.
79174 if( iPage>pCheck->nPage || iPage==0 ){
79188 ** Check that the entry in the pointer-map for page iChild maps to
79202 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
79229 int nErrAtStart = pCheck->nErr;
79230 while( iPage!=0 && pCheck->mxErr ){
79234 N--;
79235 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
79243 if( pCheck->pBt->autoVacuum ){
79247 if( n>pCheck->pBt->usableSize/4-2 ){
79250 N--;
79255 if( pCheck->pBt->autoVacuum ){
79261 N -= n;
79266 /* If this database supports auto-vacuum and iPage is not the last
79267 ** page in this overflow list, check that the pointer-map entry for
79270 if( pCheck->pBt->autoVacuum && N>0 ){
79279 if( N && nErrAtStart==pCheck->nErr ){
79283 expected-N, expected);
79289 ** An implementation of a min-heap.
79299 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
79328 aHeap[0]--;
79365 int depth = -1, d2; /* Depth of a subtree */
79381 u32 *heap = 0; /* Min-heap used for checking cell coverage */
79382 u32 x, prev = 0; /* Next and previous entry on the min-heap */
79383 const char *saved_zPfx = pCheck->zPfx;
79384 int saved_v1 = pCheck->v1;
79385 int saved_v2 = pCheck->v2;
79391 if( pCheck->mxErr==0 ) goto end_of_check;
79392 pBt = pCheck->pBt;
79393 usableSize = pBt->usableSize;
79396 pCheck->zPfx = "Tree %u page %u: ";
79397 pCheck->v0 = pCheck->v1 = iPage;
79406 savedIsInit = pPage->isInit;
79407 pPage->isInit = 0;
79419 data = pPage->aData;
79420 hdr = pPage->hdrOffset;
79423 pCheck->zPfx = "Tree %u page %u cell %u: ";
79427 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
79430 assert( pPage->nCell==nCell );
79432 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
79433 ** immediately follows the b-tree page header. */
79434 cellStart = hdr + 12 - 4*pPage->leaf;
79435 assert( pPage->aCellIdx==&data[cellStart] );
79436 pCellIdx = &data[cellStart + 2*(nCell-1)];
79438 if( !pPage->leaf ){
79439 /* Analyze the right-child page of internal pages */
79442 if( pBt->autoVacuum ){
79443 pCheck->zPfx = "Tree %u page %u right child: ";
79452 heap = pCheck->heap;
79456 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
79458 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
79462 pCheck->v2 = i;
79465 pCellIdx -= 2;
79466 if( pc<contentOffset || pc>usableSize-4 ){
79468 pc, contentOffset, usableSize-4);
79473 pPage->xParseCell(pPage, pCell, &info);
79481 if( pPage->intKey ){
79493 assert( pc + info.nSize - 4 <= usableSize );
79494 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
79495 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
79497 if( pBt->autoVacuum ){
79504 if( !pPage->leaf ){
79508 if( pBt->autoVacuum ){
79519 /* Populate the coverage-checking heap for leaf pages */
79520 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
79527 pCheck->zPfx = 0;
79528 if( doCoverageCheck && pCheck->mxErr>0 ){
79529 /* For leaf pages, the min-heap has already been initialized and the
79532 if( !pPage->leaf ){
79533 heap = pCheck->heap;
79535 for(i=nCell-1; i>=0; i--){
79538 size = pPage->xCellSize(pPage, &data[pc]);
79539 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
79542 /* Add the freeblocks to the min-heap
79544 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
79551 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79554 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
79555 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
79556 ** big-endian integer which is the offset in the b-tree page of the next
79560 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
79563 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79566 /* Analyze the min-heap looking for overlap between cells and/or
79569 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
79574 ** The loop below pulls entries from the min-heap in order and compares
79580 prev = contentOffset - 1; /* Implied first min-heap entry */
79587 nFrag += (x>>16) - (prev&0xffff) - 1;
79591 nFrag += usableSize - (prev&0xffff) - 1;
79592 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
79593 ** is stored in the fifth field of the b-tree page header.
79594 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
79605 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
79607 pCheck->zPfx = saved_zPfx;
79608 pCheck->v1 = saved_v1;
79609 pCheck->v2 = saved_v2;
79620 ** A read-only or read-write transaction must be opened before calling
79625 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
79629 ** root pages is incomplete. This is a "partial integrity-check". This
79648 BtShared *pBt = p->pBt;
79649 u64 savedDbFlags = pBt->db->flags;
79664 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
79665 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
79670 sCheck.pPager = pBt->pPager;
79684 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
79697 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
79698 get4byte(&pBt->pPage1->aData[36]));
79706 if( pBt->autoVacuum ){
79710 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
79717 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
79724 testcase( pBt->db->flags & SQLITE_CellSizeCk );
79725 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
79730 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
79736 pBt->db->flags = savedDbFlags;
79747 /* If the database supports auto-vacuum, make sure no tables contain
79748 ** references to pointer-map pages.
79751 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
79755 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
79775 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
79783 ** an empty string if the database is in-memory or a TEMP database.
79789 assert( p->pBt->pPager!=0 );
79790 return sqlite3PagerFilename(p->pBt->pPager, 1);
79802 assert( p->pBt->pPager!=0 );
79803 return sqlite3PagerJournalname(p->pBt->pPager);
79811 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
79812 return p ? p->inTrans : 0;
79820 ** transaction on the shared-cache the argument Btree is connected to.
79827 BtShared *pBt = p->pBt;
79829 if( pBt->inTransaction!=TRANS_NONE ){
79832 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
79845 assert( sqlite3_mutex_held(p->db->mutex) );
79846 return p->nBackup!=0;
79851 ** a single shared-btree. The memory is used by client code for its own
79852 ** purposes (for example, to store a high-level schema associated with
79853 ** the shared-btree). The btree layer manages reference counting issues.
79855 ** The first time this is called on a shared-btree, nBytes bytes of memory
79864 ** Just before the shared-btree is closed, the function passed as the
79870 BtShared *pBt = p->pBt;
79872 if( !pBt->pSchema && nBytes ){
79873 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
79874 pBt->xFreeSchema = xFree;
79877 return pBt->pSchema;
79887 assert( sqlite3_mutex_held(p->db->mutex) );
79904 assert( p->inTrans!=TRANS_NONE );
79905 if( p->sharable ){
79935 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
79936 assert( pCsr->curFlags & BTCF_Incrblob );
79942 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
79943 if( pCsr->eState!=CURSOR_VALID ){
79949 ** version of the b-tree page modified by the accessPayload call below.
79955 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
79961 ** (c) the connection holds a write-lock on the table (if required),
79962 ** (d) there are no conflicting read-locks, and
79965 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
79968 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
79969 && pCsr->pBt->inTransaction==TRANS_WRITE );
79970 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
79971 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
79972 assert( pCsr->pPage->intKey );
79981 pCur->curFlags |= BTCF_Incrblob;
79982 pCur->pBtree->hasIncrblobCur = 1;
79992 BtShared *pBt = pBtree->pBt;
80000 pBt->btsFlags &= ~BTS_NO_WAL;
80001 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
80005 u8 *aData = pBt->pPage1->aData;
80009 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
80018 pBt->btsFlags &= ~BTS_NO_WAL;
80027 return (pCsr->hints & mask)!=0;
80031 ** Return true if the given Btree is read-only.
80034 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
80043 ** If no transaction is active and the database is not a temp-db, clear
80044 ** the in-memory pager cache.
80047 BtShared *pBt = p->pBt;
80048 if( pBt->inTransaction==TRANS_NONE ){
80049 sqlite3PagerClearCache(pBt->pPager);
80058 return p->sharable;
80067 testcase( p->sharable );
80068 return p->pBt->nRef;
80096 Btree *pDest; /* Destination b-tree file */
80098 int bDestLocked; /* True once a write-transaction is open on pDest */
80102 Btree *pSrc; /* Source b-tree file */
80120 ** structure may be accessed via two groups of thread-safe entry points:
80134 ** backup_pagecount() are not thread-safe functions. If they are called
80142 ** Non-sharable Btrees (in-memory databases for example), do not have
80178 return pDb->aDb[i].pBt;
80187 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
80192 ** Check that there is no open read-transaction on the b-tree passed as the
80194 ** is an open read-transaction, return SQLITE_ERROR and leave an error
80236 sqlite3_mutex_enter(pSrcDb->mutex);
80237 sqlite3_mutex_enter(pDestDb->mutex);
80246 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
80257 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
80258 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
80259 p->pDestDb = pDestDb;
80260 p->pSrcDb = pSrcDb;
80261 p->iNext = 1;
80262 p->isAttached = 0;
80264 if( 0==p->pSrc || 0==p->pDest
80265 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
80277 p->pSrc->nBackup++;
80280 sqlite3_mutex_leave(pDestDb->mutex);
80281 sqlite3_mutex_leave(pSrcDb->mutex);
80305 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
80306 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
80307 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
80313 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
80314 assert( p->bDestLocked );
80315 assert( !isFatalError(p->rc) );
80316 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
80324 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
80327 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
80345 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
80357 ** this function is a no-op.
80377 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
80378 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80379 p->pNext = *pp;
80381 p->isAttached = 1;
80385 ** Copy nPage pages from the source b-tree to the destination.
80396 sqlite3_mutex_enter(p->pSrcDb->mutex);
80397 sqlite3BtreeEnter(p->pSrc);
80398 if( p->pDestDb ){
80399 sqlite3_mutex_enter(p->pDestDb->mutex);
80402 rc = p->rc;
80404 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
80405 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
80407 int nSrcPage = -1; /* Size of source db in pages */
80410 /* If the source pager is currently in a write-transaction, return
80413 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
80419 /* If there is no open read-transaction on the source database, open
80423 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
80424 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
80434 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
80439 if( SQLITE_OK==rc && p->bDestLocked==0
80440 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
80441 (int*)&p->iDestSchema))
80443 p->bDestLocked = 1;
80448 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
80449 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
80450 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
80458 /* Now that there is a read-lock on the source database, query the
80461 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
80463 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
80464 const Pgno iSrcPg = p->iNext; /* Source page number */
80465 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
80473 p->iNext++;
80476 p->nPagecount = nSrcPage;
80477 p->nRemaining = nSrcPage+1-p->iNext;
80478 if( p->iNext>(Pgno)nSrcPage ){
80480 }else if( !p->isAttached ){
80486 ** is to make sure that the schema-version really does change in
80492 rc = sqlite3BtreeNewDb(p->pDest);
80496 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
80499 if( p->pDestDb ){
80500 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
80503 rc = sqlite3BtreeSetVersion(p->pDest, 2);
80520 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
80521 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
80524 nDestTruncate = (nSrcPage+ratio-1)/ratio;
80525 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
80526 nDestTruncate--;
80534 /* If the source page-size is smaller than the destination page-size,
80540 ** pending-byte page in the source database may need to be
80553 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
80565 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
80609 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
80619 ** "committing" a read-only transaction cannot fail.
80623 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
80624 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
80631 p->rc = rc;
80633 if( p->pDestDb ){
80634 sqlite3_mutex_leave(p->pDestDb->mutex);
80636 sqlite3BtreeLeave(p->pSrc);
80637 sqlite3_mutex_leave(p->pSrcDb->mutex);
80651 pSrcDb = p->pSrcDb;
80652 sqlite3_mutex_enter(pSrcDb->mutex);
80653 sqlite3BtreeEnter(p->pSrc);
80654 if( p->pDestDb ){
80655 sqlite3_mutex_enter(p->pDestDb->mutex);
80659 if( p->pDestDb ){
80660 p->pSrc->nBackup--;
80662 if( p->isAttached ){
80663 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80666 pp = &(*pp)->pNext;
80669 *pp = p->pNext;
80673 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
80676 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
80677 if( p->pDestDb ){
80678 sqlite3Error(p->pDestDb, rc);
80681 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
80683 sqlite3BtreeLeave(p->pSrc);
80684 if( p->pDestDb ){
80685 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
80705 return p->nRemaining;
80719 return p->nPagecount;
80741 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
80742 if( !isFatalError(p->rc) && iPage<p->iNext ){
80748 assert( p->pDestDb );
80749 sqlite3_mutex_enter(p->pDestDb->mutex);
80751 sqlite3_mutex_leave(p->pDestDb->mutex);
80754 p->rc = rc;
80757 }while( (p = p->pNext)!=0 );
80776 for(p=pBackup; p; p=p->pNext){
80777 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
80778 p->iNext = 1;
80800 if( pFd->pMethods ){
80813 b.pSrcDb = pFrom->db;
80822 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
80829 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
80867 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
80880 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
80886 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
80889 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
80891 if( p->flags & MEM_Null ){
80893 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
80900 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
80903 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
80904 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
80905 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
80908 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
80916 assert( (p->flags & MEM_Cleared)==0 );
80920 assert( p->szMalloc==0
80921 || (p->flags==MEM_Undefined
80922 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
80923 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
80933 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
80935 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
80936 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
80937 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
80938 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
80951 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
80953 if( p->flags & MEM_Int ){
80955 /* Work-around for GCC bug
80956 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */
80958 assert( (p->flags&MEM_Int)*2==sizeof(x) );
80959 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
80960 p->n = sqlite3Int64ToText(x, zBuf);
80962 p->n = sqlite3Int64ToText(p->u.i, zBuf);
80967 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
80970 p->n = acc.nChar;
80987 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
81002 if( (p->flags & MEM_Str)==0 ) return 1;
81003 if( p->db && p->db->mallocFailed ) return 1;
81004 if( p->flags & MEM_Term ){
81005 /* Insure that the string is properly zero-terminated. Pay particular
81006 ** attention to the case where p->n is odd */
81007 if( p->szMalloc>0 && p->z==p->zMalloc ){
81008 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
81009 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
81011 assert( p->z[p->n]==0 );
81012 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
81013 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
81015 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
81018 z = p->z;
81021 if( p->enc!=SQLITE_UTF8 ){
81023 if( p->enc==SQLITE_UTF16BE ) z++;
81040 ** routine is a no-op.
81054 if( !(pMem->flags&MEM_Str) ){
81055 pMem->enc = desiredEnc;
81058 if( pMem->enc==desiredEnc ){
81061 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81071 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
81072 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
81078 ** Make sure pMem->z points to a writable allocation of at least n bytes.
81081 ** pMem->z into the new allocation. pMem must be either a string or
81083 ** in pMem->z is discarded.
81088 testcase( pMem->db==0 );
81092 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
81093 testcase( bPreserve && pMem->z==0 );
81095 assert( pMem->szMalloc==0
81096 || (pMem->flags==MEM_Undefined
81097 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
81098 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
81099 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
81100 if( pMem->db ){
81101 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
81103 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
81104 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
81105 pMem->z = pMem->zMalloc;
81109 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
81110 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
81112 if( pMem->zMalloc==0 ){
81114 pMem->z = 0;
81115 pMem->szMalloc = 0;
81118 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
81121 if( bPreserve && pMem->z ){
81122 assert( pMem->z!=pMem->zMalloc );
81123 memcpy(pMem->zMalloc, pMem->z, pMem->n);
81125 if( (pMem->flags&MEM_Dyn)!=0 ){
81126 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
81127 pMem->xDel((void *)(pMem->z));
81130 pMem->z = pMem->zMalloc;
81131 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
81136 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
81137 ** If pMem->zMalloc already meets or exceeds the requested size, this
81138 ** routine is a no-op.
81141 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
81150 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
81151 if( pMem->szMalloc<szNew ){
81154 assert( (pMem->flags & MEM_Dyn)==0 );
81155 pMem->z = pMem->zMalloc;
81156 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
81165 ** to be a double-zero byte at an even byte boundary in order to
81170 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
81173 pMem->z[pMem->n] = 0;
81174 pMem->z[pMem->n+1] = 0;
81175 pMem->z[pMem->n+2] = 0;
81176 pMem->flags |= MEM_Term;
81188 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81190 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
81192 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
81197 pMem->flags &= ~MEM_Ephem;
81199 pMem->pScopyFrom = 0;
81206 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
81213 assert( pMem->flags & MEM_Zero );
81214 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
81217 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81220 nByte = pMem->n + pMem->u.nZero;
81222 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
81228 assert( pMem->z!=0 );
81229 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
81231 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
81232 pMem->n += pMem->u.nZero;
81233 pMem->flags &= ~(MEM_Zero|MEM_Term);
81243 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81244 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
81245 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
81246 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
81271 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81272 assert( !(pMem->flags&MEM_Zero) );
81273 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
81274 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
81280 pMem->enc = 0;
81284 vdbeMemRenderNum(nByte, pMem->z, pMem);
81285 assert( pMem->z!=0 );
81286 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
81287 pMem->enc = SQLITE_UTF8;
81288 pMem->flags |= MEM_Str|MEM_Term;
81289 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
81307 assert( pMem->db!=0 );
81308 assert( pFunc->xFinalize!=0 );
81309 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
81310 assert( sqlite3_mutex_held(pMem->db->mutex) );
81314 t.db = pMem->db;
81319 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
81320 assert( (pMem->flags & MEM_Dyn)==0 );
81321 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
81338 assert( pFunc->xValue!=0 );
81339 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
81340 assert( pAccum->db!=0 );
81341 assert( sqlite3_mutex_held(pAccum->db->mutex) );
81347 ctx.enc = ENC(pAccum->db);
81348 pFunc->xValue(&ctx);
81363 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
81365 if( p->flags&MEM_Agg ){
81366 sqlite3VdbeMemFinalize(p, p->u.pDef);
81367 assert( (p->flags & MEM_Agg)==0 );
81368 testcase( p->flags & MEM_Dyn );
81370 if( p->flags&MEM_Dyn ){
81371 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
81372 p->xDel((void *)p->z);
81374 p->flags = MEM_Null;
81379 ** by p->xDel and memory in p->zMalloc.
81389 if( p->szMalloc ){
81390 sqlite3DbFreeNN(p->db, p->zMalloc);
81391 p->szMalloc = 0;
81393 p->z = 0;
81408 if( VdbeMemDynamic(p) || p->szMalloc ){
81418 if( p->szMalloc ) vdbeMemClear(p);
81422 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
81423 ** If the double is out of range of a 64-bit signed integer then
81424 ** return the closest available 64-bit signed integer.
81428 /* When floating-point is omitted, double and int64 are the same thing */
81433 ** minimum and maximum 64-bit integers, or they define them
81436 ** larger than a 32-bit integer constant.
81455 ** a floating-point then the value returned is the integer part.
81458 ** an SQL-NULL value, return 0.
81464 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
81470 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81472 flags = pMem->flags;
81475 return pMem->u.i;
81477 return doubleToInt64(pMem->u.r);
81478 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
81494 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
81499 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81501 if( pMem->flags & MEM_Real ){
81502 return pMem->u.r;
81503 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
81504 testcase( pMem->flags & MEM_IntReal );
81505 return (double)pMem->u.i;
81506 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
81519 testcase( pMem->flags & MEM_IntReal );
81520 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
81521 if( pMem->flags & MEM_Null ) return ifNull;
81531 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
81533 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81536 if( pMem->flags & MEM_IntReal ){
81539 i64 ix = doubleToInt64(pMem->u.r);
81543 ** (1) the round-trip conversion real->int->real is a no-op, and
81551 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
81552 pMem->u.i = ix;
81563 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81567 pMem->u.i = sqlite3VdbeIntValue(pMem);
81578 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81581 pMem->u.r = sqlite3VdbeRealValue(pMem);
81591 ** For some versions of GCC on 32-bit machines, if you do the more obvious
81593 ** though the r1 and (double)i values are bit-for-bit the same.
81599 && i >= -2251799813685248LL && i < 2251799813685248LL);
81622 testcase( pMem->flags & MEM_Int );
81623 testcase( pMem->flags & MEM_Real );
81624 testcase( pMem->flags & MEM_IntReal );
81625 testcase( pMem->flags & MEM_Null );
81626 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
81629 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
81630 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81631 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
81632 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
81633 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
81635 pMem->u.i = ix;
81641 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
81642 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
81654 if( pMem->flags & MEM_Null ) return SQLITE_OK;
81657 if( (pMem->flags & MEM_Blob)==0 ){
81659 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81660 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
81662 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
81681 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
81683 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81684 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
81685 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
81699 pMem->flags = flags;
81700 pMem->db = db;
81701 pMem->szMalloc = 0;
81721 pMem->flags = MEM_Null;
81735 pMem->flags = MEM_Blob|MEM_Zero;
81736 pMem->n = 0;
81738 pMem->u.nZero = n;
81739 pMem->enc = SQLITE_UTF8;
81740 pMem->z = 0;
81748 assert( pMem->z!=0 );
81749 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
81750 memset(pMem->z, 0, nByte);
81751 pMem->n = n>0?n:0;
81752 pMem->flags = MEM_Blob;
81753 pMem->enc = SQLITE_UTF8;
81761 ** a 64-bit integer.
81765 pMem->u.i = val;
81766 pMem->flags = MEM_Int;
81777 pMem->u.i = val;
81778 pMem->flags = MEM_Int;
81782 /* A no-op destructor */
81795 assert( pMem->flags==MEM_Null );
81797 pMem->u.zPType = zPType ? zPType : "";
81798 pMem->z = pPtr;
81799 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
81800 pMem->eSubtype = 'p';
81801 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
81812 pMem->u.r = val;
81813 pMem->flags = MEM_Real;
81824 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
81825 && pMem->xDel==sqlite3RowSetDelete;
81837 sqlite3 *db = pMem->db;
81844 pMem->z = (char*)p;
81845 pMem->flags = MEM_Blob|MEM_Dyn;
81846 pMem->xDel = sqlite3RowSetDelete;
81852 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
81855 assert( p->db!=0 );
81856 if( p->flags & (MEM_Str|MEM_Blob) ){
81857 int n = p->n;
81858 if( p->flags & MEM_Zero ){
81859 n += p->u.nZero;
81861 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
81872 ** This is used for testing and debugging only - to help ensure that shallow
81878 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
81879 if( pX->pScopyFrom==pMem ){
81881 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
81883 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
81891 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
81892 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
81895 ** undefined so that we can quickly detect the shallow-copy error */
81896 pX->flags = MEM_Undefined;
81897 pX->pScopyFrom = 0;
81900 pMem->pScopyFrom = 0;
81906 ** pTo are freed. The pFrom->z field is not duplicated. If
81907 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
81917 assert( pTo->db==pFrom->db );
81920 if( (pFrom->flags&MEM_Static)==0 ){
81921 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
81923 pTo->flags |= srcType;
81937 pTo->flags &= ~MEM_Dyn;
81938 if( pTo->flags&(MEM_Str|MEM_Blob) ){
81939 if( 0==(pFrom->flags&MEM_Static) ){
81940 pTo->flags |= MEM_Ephem;
81955 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
81956 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
81957 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
81961 pFrom->flags = MEM_Null;
81962 pFrom->szMalloc = 0;
81985 ** non-negative for blobs.
81994 i64 nByte = n; /* New value for pMem->n */
81996 u16 flags; /* New value for pMem->flags */
81999 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82009 if( pMem->db ){
82010 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
82031 sqlite3DbFree(pMem->db, (void*)z);
82037 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
82055 memcpy(pMem->z, z, nAlloc);
82058 pMem->z = (char *)z;
82060 pMem->zMalloc = pMem->z;
82061 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
82063 pMem->xDel = xDel;
82068 pMem->n = (int)(nByte & 0x7fffffff);
82069 pMem->flags = flags;
82070 pMem->enc = enc;
82089 ** pMem->zMalloc to hold the content from the btree, if possible. New
82090 ** pMem->zMalloc space will be allocated if necessary. The calling routine
82104 pMem->flags = MEM_Null;
82109 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
82111 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
82112 pMem->flags = MEM_Blob;
82113 pMem->n = (int)amt;
82134 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
82135 assert( pMem->z!=0 );
82138 pMem->flags = MEM_Blob|MEM_Ephem;
82139 pMem->n = (int)amt;
82150 ** to a zero-terminated version of that string.
82154 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
82157 assert( (pVal->flags & (MEM_Null))==0 );
82158 if( pVal->flags & (MEM_Blob|MEM_Str) ){
82160 pVal->flags |= MEM_Str;
82161 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
82164 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
82165 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
82170 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
82173 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
82175 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
82176 || pVal->db->mallocFailed );
82177 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
82179 return pVal->z;
82191 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
82197 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
82200 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
82202 return pVal->z;
82204 if( pVal->flags&MEM_Null ){
82216 p->flags = MEM_Null;
82217 p->db = db;
82238 ** Otherwise, if the second argument is non-zero, then this function is
82247 UnpackedRecord *pRec = p->ppRec[0];
82250 Index *pIdx = p->pIdx; /* Index being probed */
82253 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
82258 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
82259 if( pRec->pKeyInfo ){
82260 assert( pRec->pKeyInfo->nAllField==nCol );
82261 assert( pRec->pKeyInfo->enc==ENC(db) );
82262 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
82264 pRec->aMem[i].flags = MEM_Null;
82265 pRec->aMem[i].db = db;
82273 p->ppRec[0] = pRec;
82276 pRec->nField = p->iVal+1;
82277 return &pRec->aMem[p->iVal];
82324 assert( (p->flags & EP_TokenOnly)==0 );
82326 pList = p->x.pList;
82327 if( pList ) nVal = pList->nExpr;
82329 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
82334 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
82335 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
82347 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
82362 pFunc->xSFunc(&ctx, nVal, apVal);
82365 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
82372 pCtx->pParse->nErr++;
82379 pCtx->pParse->rc = rc;
82421 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
82422 if( op==TK_REGISTER ) op = pExpr->op2;
82428 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
82433 aff = sqlite3AffinityType(pExpr->u.zToken,0);
82434 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
82440 /* zero-blobs only come from functions, not literal values. And
82451 ** case when the value is -9223372036854775808.
82454 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
82455 pExpr = pExpr->pLeft;
82456 op = pExpr->op;
82457 negInt = -1;
82458 zNeg = "-";
82465 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
82467 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
82469 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
82476 assert( (pVal->flags & MEM_IntReal)==0 );
82477 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
82478 testcase( pVal->flags & MEM_Int );
82479 testcase( pVal->flags & MEM_Real );
82480 pVal->flags &= ~MEM_Str;
82486 /* This branch happens for multiple negative signs. Ex: -(-5) */
82487 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
82491 if( pVal->flags & MEM_Real ){
82492 pVal->u.r = -pVal->u.r;
82493 }else if( pVal->u.i==SMALLEST_INT64 ){
82495 pVal->u.r = -(double)SMALLEST_INT64;
82497 pVal->u.r = LARGEST_INT64;
82501 pVal->u.i = -pVal->u.i;
82514 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
82515 assert( pExpr->u.zToken[1]=='\'' );
82518 zVal = &pExpr->u.zToken[2];
82519 nVal = sqlite3Strlen30(zVal)-1;
82534 pVal->flags = MEM_Int;
82535 pVal->u.i = pExpr->u.zToken[4]==0;
82544 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
82605 sqlite3 *db = pParse->db;
82610 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
82616 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
82618 int iBindVar = pExpr->iColumn;
82619 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
82620 if( (v = pParse->pReprepare)!=0 ){
82623 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
82625 pVal->db = pParse->db;
82632 assert( pVal==0 || pVal->db==db );
82683 if( pExpr==0 || pExpr->op!=TK_SELECT ){
82694 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
82726 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
82768 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
82769 pMem->enc = ENC(db);
82781 int nCol = pRec->pKeyInfo->nAllField;
82782 Mem *aMem = pRec->aMem;
82787 sqlite3KeyInfoUnref(pRec->pKeyInfo);
82812 sqlite3DbFreeNN(((Mem*)v)->db, v);
82821 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
82825 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
82826 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
82827 return p->n;
82829 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
82830 return p->n;
82832 if( (p->flags & MEM_Blob)!=0 ){
82833 if( p->flags & MEM_Zero ){
82834 return p->n + p->u.nZero;
82836 return p->n;
82839 if( p->flags & MEM_Null ) return 0;
82870 sqlite3 *db = pParse->db;
82874 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
82875 p->db = db;
82876 if( db->pVdbe ){
82877 db->pVdbe->ppVPrev = &p->pVNext;
82879 p->pVNext = db->pVdbe;
82880 p->ppVPrev = &db->pVdbe;
82881 db->pVdbe = p;
82882 assert( p->eVdbeState==VDBE_INIT_STATE );
82883 p->pParse = pParse;
82884 pParse->pVdbe = p;
82885 assert( pParse->aLabel==0 );
82886 assert( pParse->nLabel==0 );
82887 assert( p->nOpAlloc==0 );
82888 assert( pParse->szOpAlloc==0 );
82897 return p->pParse;
82905 sqlite3DbFree(p->db, p->zErrMsg);
82907 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
82916 p->prepFlags = prepFlags;
82918 p->expmask = 0;
82920 assert( p->zSql==0 );
82921 p->zSql = sqlite3DbStrNDup(p->db, z, n);
82926 ** Add a new element to the Vdbe->pDblStr list.
82932 sizeof(*pStr)+n+1-sizeof(pStr->z));
82934 pStr->pNextStr = p->pDblStr;
82935 p->pDblStr = pStr;
82936 memcpy(pStr->z, z, n+1);
82944 ** zId of length nId is a double-quoted identifier. Check to see if
82949 const char *zId /* The double-quoted identifier, already dequoted */
82953 if( pVdbe->pDblStr==0 ) return 0;
82954 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
82955 if( strcmp(zId, pStr->z)==0 ) return 1;
82962 ** Swap byte-code between two VDBE structures.
82974 assert( pA->db==pB->db );
82978 pTmp = pA->pVNext;
82979 pA->pVNext = pB->pVNext;
82980 pB->pVNext = pTmp;
82981 ppTmp = pA->ppVPrev;
82982 pA->ppVPrev = pB->ppVPrev;
82983 pB->ppVPrev = ppTmp;
82984 zTmp = pA->zSql;
82985 pA->zSql = pB->zSql;
82986 pB->zSql = zTmp;
82988 zTmp = pA->zNormSql;
82989 pA->zNormSql = pB->zNormSql;
82990 pB->zNormSql = zTmp;
82992 pB->expmask = pA->expmask;
82993 pB->prepFlags = pA->prepFlags;
82994 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
82995 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
83003 ** If an out-of-memory error occurs while resizing the array, return
83010 Parse *p = v->pParse;
83012 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
83020 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
83021 : (sqlite3_int64)v->nOpAlloc+nOp);
83023 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
83029 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
83030 sqlite3OomFault(p->db);
83035 assert( nNew>=(v->nOpAlloc+nOp) );
83036 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
83038 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
83039 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
83040 v->aOp = pNew;
83082 assert( p->nOpAlloc<=p->nOp );
83084 assert( p->nOpAlloc>p->nOp );
83091 i = p->nOp;
83092 assert( p->eVdbeState==VDBE_INIT_STATE );
83094 if( p->nOpAlloc<=i ){
83097 assert( p->aOp!=0 );
83098 p->nOp++;
83099 pOp = &p->aOp[i];
83101 pOp->opcode = (u8)op;
83102 pOp->p5 = 0;
83103 pOp->p1 = p1;
83104 pOp->p2 = p2;
83105 pOp->p3 = p3;
83106 pOp->p4.p = 0;
83107 pOp->p4type = P4_NOTUSED;
83109 pOp->zComment = 0;
83112 pOp->nExec = 0;
83113 pOp->nCycle = 0;
83116 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83117 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
83118 test_addop_breakpoint(i, &p->aOp[i]);
83122 pOp->iSrcLine = 0;
83216 Vdbe *v = pParse->pVdbe;
83221 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
83222 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
83224 assert( pParse->db->mallocFailed );
83225 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
83228 pCtx->pOut = 0;
83229 pCtx->pFunc = (FuncDef*)pFunc;
83230 pCtx->pVdbe = 0;
83231 pCtx->isError = 0;
83232 pCtx->argc = nArg;
83233 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
83266 if( pParse->addrExplain==0 ) return 0;
83267 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
83268 return pOp->p2;
83293 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
83301 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
83303 v = pParse->pVdbe;
83304 iThis = v->nOp;
83305 addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
83307 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
83309 pParse->addrExplain = iThis;
83321 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
83337 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
83338 sqlite3MayAbort(p->pParse);
83353 if( p->db->mallocFailed==0 ){
83354 VdbeOp *pOp = &p->aOp[addr];
83355 pOp->p4type = P4_INT32;
83356 pOp->p4.i = p4;
83361 /* Insert the end of a co-routine
83367 ** co-routine has its own independent set of registers, because co-routines
83369 ** that could cause problems if two or more co-routines are using the same
83372 v->pParse->nTempReg = 0;
83373 v->pParse->nRangeReg = 0;
83385 ** always negative and P2 values are suppose to be non-negative.
83392 ** Parse.aLabel[x] Stores the address that the x-th label resolves
83394 ** labels stores -1, but that is not required.
83402 return --pParse->nLabel;
83411 int nNewSize = 10 - p->nLabel;
83412 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
83413 nNewSize*sizeof(p->aLabel[0]));
83414 if( p->aLabel==0 ){
83415 p->nLabelAlloc = 0;
83419 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
83421 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
83424 p->nLabelAlloc = nNewSize;
83425 p->aLabel[j] = v->nOp;
83429 Parse *p = v->pParse;
83431 assert( v->eVdbeState==VDBE_INIT_STATE );
83432 assert( j<-p->nLabel );
83435 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83436 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
83439 if( p->nLabelAlloc + p->nLabel < 0 ){
83442 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
83443 p->aLabel[j] = v->nOp;
83459 for(i=1; ALWAYS(i<p->nOp); i++){
83460 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
83461 p->aOp[1].opcode = OP_Noop;
83471 ** in a Vdbe main program and each of the sub-programs (triggers) it may
83482 ** sqlite3DbFree(v->db, sIter.apSub);
83491 int iSub; /* 0 = main program, 1 = first sub-program etc. */
83494 Vdbe *v = p->v;
83499 if( p->iSub<=p->nSub ){
83501 if( p->iSub==0 ){
83502 aOp = v->aOp;
83503 nOp = v->nOp;
83505 aOp = p->apSub[p->iSub-1]->aOp;
83506 nOp = p->apSub[p->iSub-1]->nOp;
83508 assert( p->iAddr<nOp );
83510 pRet = &aOp[p->iAddr];
83511 p->iAddr++;
83512 if( p->iAddr==nOp ){
83513 p->iSub++;
83514 p->iAddr = 0;
83517 if( pRet->p4type==P4_SUBPROGRAM ){
83518 int nByte = (p->nSub+1)*sizeof(SubProgram*);
83520 for(j=0; j<p->nSub; j++){
83521 if( p->apSub[j]==pRet->p4.pProgram ) break;
83523 if( j==p->nSub ){
83524 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
83525 if( !p->apSub ){
83528 p->apSub[p->nSub++] = pRet->p4.pProgram;
83541 ** sub-programs contains any of the following:
83558 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
83574 int opcode = pOp->opcode;
83581 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
83586 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
83590 ** where a "DELETE FROM tbl" has a statement-journal but does not
83591 ** require one. This is not so bad - it is an inefficiency, not a bug. */
83592 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
83597 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
83602 sqlite3DbFree(v->db, sIter.apSub);
83609 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
83613 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
83622 || (pC->eCurType!=CURTYPE_SORTER
83623 && pC->eCurType!=CURTYPE_PSEUDO
83624 && !pC->isEphemeral)
83626 p->nWrite++;
83637 assert( p->nWrite==0 || p->usesStmtJournal );
83665 Parse *pParse = p->pParse;
83666 int *aLabel = pParse->aLabel;
83668 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
83669 p->readOnly = 1;
83670 p->bIsReader = 0;
83671 pOp = &p->aOp[p->nOp-1];
83672 assert( p->aOp[0].opcode==OP_Init );
83680 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
83683 switch( pOp->opcode ){
83685 if( pOp->p2!=0 ) p->readOnly = 0;
83690 p->bIsReader = 1;
83698 p->readOnly = 0;
83699 p->bIsReader = 1;
83703 assert( pOp->p2>=0 );
83708 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
83713 assert( (pOp - p->aOp) >= 3 );
83714 assert( pOp[-1].opcode==OP_Integer );
83715 n = pOp[-1].p1;
83722 if( pOp->p2<0 ){
83724 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
83725 ** have non-negative values for P2. */
83726 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
83727 assert( ADDR(pOp->p2)<-pParse->nLabel );
83728 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
83729 pOp->p2 = aLabel[ADDR(pOp->p2)];
83735 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
83736 ** have non-negative values for P2. */
83737 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
83739 assert( pOp>p->aOp );
83740 pOp--;
83744 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
83745 pParse->aLabel = 0;
83747 pParse->nLabel = 0;
83749 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
83774 Vdbe *v, /* The byte-code program under construction */
83784 pParse = v->pParse;
83786 if( pParse->nErr ) return;
83788 assert( iLast<v->nOp );
83789 pOp = &v->aOp[iFirst];
83791 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
83792 int iDest = pOp->p2; /* Jump destination */
83794 if( pOp->opcode==OP_Gosub ) continue;
83798 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
83801 iDest = pParse->aLabel[j];
83805 for(; j<v->nOp; j++){
83806 VdbeOp *pX = &v->aOp[j];
83807 if( pX->opcode==OP_Return ){
83808 if( pX->p1==iRetReg ) break;
83811 if( pX->opcode==OP_Noop ) continue;
83812 if( pX->opcode==OP_Explain ) continue;
83840 assert( p->eVdbeState==VDBE_INIT_STATE );
83841 return p->nOp;
83850 ** sqlite3VdbeAddOpList() will always be non-NULL.
83854 assert( p->nOp + N <= p->nOpAlloc );
83868 for(i=0; i<p->nOp; i++){
83869 assert( p->aOp[i].opcode!=OP_ResultRow );
83897 VdbeOp *aOp = p->aOp;
83898 assert( aOp && !p->db->mallocFailed );
83901 assert( DbMaskAllZero(p->btreeMask) );
83904 *pnOp = p->nOp;
83905 p->aOp = 0;
83913 ** Non-zero P2 arguments to jump instructions are automatically adjusted
83920 int iLineno /* Source-file line number of first opcode */
83925 assert( p->eVdbeState==VDBE_INIT_STATE );
83926 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
83929 pFirst = pOut = &p->aOp[p->nOp];
83931 pOut->opcode = aOp->opcode;
83932 pOut->p1 = aOp->p1;
83933 pOut->p2 = aOp->p2;
83934 assert( aOp->p2>=0 );
83935 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
83936 pOut->p2 += p->nOp;
83938 pOut->p3 = aOp->p3;
83939 pOut->p4type = P4_NOTUSED;
83940 pOut->p4.p = 0;
83941 pOut->p5 = 0;
83943 pOut->zComment = 0;
83946 pOut->iSrcLine = iLineno+i;
83951 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83952 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
83956 p->nOp += nOp;
83972 if( IS_STMT_SCANSTATUS(p->db) ){
83973 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
83975 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
83977 ScanStatus *pNew = &aNew[p->nScan++];
83979 pNew->addrExplain = addrExplain;
83980 pNew->addrLoop = addrLoop;
83981 pNew->addrVisit = addrVisit;
83982 pNew->nEst = nEst;
83983 pNew->zName = sqlite3DbStrDup(p->db, zName);
83984 p->aScan = aNew;
84002 if( IS_STMT_SCANSTATUS(p->db) ){
84005 for(ii=p->nScan-1; ii>=0; ii--){
84006 pScan = &p->aScan[ii];
84007 if( pScan->addrExplain==addrExplain ) break;
84011 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
84012 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
84013 if( pScan->aAddrRange[ii]==0 ){
84014 pScan->aAddrRange[ii] = addrStart;
84015 pScan->aAddrRange[ii+1] = addrEnd;
84034 if( IS_STMT_SCANSTATUS(p->db) ){
84037 for(ii=p->nScan-1; ii>=0; ii--){
84038 pScan = &p->aScan[ii];
84039 if( pScan->addrExplain==addrExplain ) break;
84043 pScan->addrLoop = addrLoop;
84044 pScan->addrVisit = addrVisit;
84057 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
84061 sqlite3VdbeGetOp(p,addr)->p1 = val;
84064 assert( addr>=0 || p->db->mallocFailed );
84065 sqlite3VdbeGetOp(p,addr)->p2 = val;
84069 sqlite3VdbeGetOp(p,addr)->p3 = val;
84072 assert( p->nOp>0 || p->db->mallocFailed );
84073 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
84083 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
84084 pOp->p5 |= OPFLAG_TYPEOFARG;
84093 sqlite3VdbeChangeP2(p, addr, p->nOp);
84099 ** the previous opcode (and is thus a no-op) then simply back up
84104 ** strives to omit useless byte-code like this:
84110 if( addr==p->nOp-1 ){
84111 assert( p->aOp[addr].opcode==OP_Once
84112 || p->aOp[addr].opcode==OP_If
84113 || p->aOp[addr].opcode==OP_FkIfZero );
84114 assert( p->aOp[addr].p4type==0 );
84116 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
84118 p->nOp--;
84120 sqlite3VdbeChangeP2(p, addr, p->nOp);
84131 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
84140 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
84145 freeEphemeralFunction(db, p->pFunc);
84163 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
84177 if( db->pnBytesFreed==0 ){
84185 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
84200 Op *pOp = &aOp[nOp-1];
84202 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
84204 sqlite3DbFree(db, pOp->zComment);
84207 pOp--;
84215 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
84219 p->pNext = pVdbe->pProgram;
84220 pVdbe->pProgram = p;
84227 return pVdbe->pProgram!=0;
84235 if( p->db->mallocFailed ) return 0;
84236 assert( addr>=0 && addr<p->nOp );
84237 pOp = &p->aOp[addr];
84238 freeP4(p->db, pOp->p4type, pOp->p4.p);
84239 pOp->p4type = P4_NOTUSED;
84240 pOp->p4.z = 0;
84241 pOp->opcode = OP_Noop;
84250 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
84251 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
84269 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
84270 assert( pParse->pVdbe );
84272 assert( iFirst+N-1<=pParse->nMem );
84277 N--;
84279 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
84280 mask &= ~MASKBIT32(N-1);
84281 N--;
84285 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
84286 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
84315 if( pOp->p4type ){
84316 freeP4(p->db, pOp->p4type, pOp->p4.p);
84317 pOp->p4type = 0;
84318 pOp->p4.p = 0;
84321 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
84324 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
84325 pOp->p4type = P4_DYNAMIC;
84332 db = p->db;
84333 assert( p->eVdbeState==VDBE_INIT_STATE );
84334 assert( p->aOp!=0 || db->mallocFailed );
84335 if( db->mallocFailed ){
84339 assert( p->nOp>0 );
84340 assert( addr<p->nOp );
84342 addr = p->nOp - 1;
84344 pOp = &p->aOp[addr];
84345 if( n>=0 || pOp->p4type ){
84352 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
84353 pOp->p4type = P4_INT32;
84356 pOp->p4.p = (void*)zP4;
84357 pOp->p4type = (signed char)n;
84364 ** to the value defined by the arguments. This is a high-speed
84375 if( p->db->mallocFailed ){
84376 freeP4(p->db, n, pP4);
84379 assert( p->nOp>0 );
84380 pOp = &p->aOp[p->nOp-1];
84381 assert( pOp->p4type==P4_NOTUSED );
84382 pOp->p4type = n;
84383 pOp->p4.p = pP4;
84392 Vdbe *v = pParse->pVdbe;
84403 ** insert a No-op and add the comment to that new instruction. This
84408 assert( p->nOp>0 || p->aOp==0 );
84409 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
84410 if( p->nOp ){
84411 assert( p->aOp );
84412 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
84413 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
84440 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
84445 ** Return the opcode for a given address. The address must be non-negative.
84461 assert( p->eVdbeState==VDBE_INIT_STATE );
84462 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
84463 if( p->db->mallocFailed ){
84466 return &p->aOp[addr];
84473 return sqlite3VdbeGetOp(p, p->nOp - 1);
84482 if( c=='1' ) return pOp->p1;
84483 if( c=='2' ) return pOp->p2;
84484 if( c=='3' ) return pOp->p3;
84485 if( c=='4' ) return pOp->p4.i;
84486 return pOp->p5;
84497 ** "PX" -> "r[X]"
84498 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
84499 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
84500 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
84503 sqlite3 *db, /* Optional - Oom error reporting only */
84515 zOpName = sqlite3OpcodeName(pOp->opcode);
84531 if( pOp->zComment && pOp->zComment[0] ){
84532 sqlite3_str_appendall(&x, pOp->zComment);
84549 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
84552 sqlite3_context *pCtx = pOp->p4.pCtx;
84553 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
84555 }else if( pCtx->argc>1 ){
84556 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
84559 x.nChar -= 2;
84565 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
84574 if( !seenCom && pOp->zComment ){
84575 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
84577 }else if( pOp->zComment ){
84578 sqlite3_str_appendall(&x, pOp->zComment);
84594 switch( pExpr->op ){
84597 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
84600 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
84606 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
84610 if( pExpr->iColumn<0 ){
84613 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
84651 displayP4Expr(p, pExpr->pLeft);
84652 if( pExpr->pRight ){
84654 displayP4Expr(p, pExpr->pRight);
84672 switch( pOp->p4type ){
84675 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
84676 assert( pKeyInfo->aSortFlags!=0 );
84677 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
84678 for(j=0; j<pKeyInfo->nKeyField; j++){
84679 CollSeq *pColl = pKeyInfo->aColl[j];
84680 const char *zColl = pColl ? pColl->zName : "";
84683 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
84684 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
84692 displayP4Expr(&x, pOp->p4.pExpr);
84698 CollSeq *pColl = pOp->p4.pColl;
84699 assert( pColl->enc<4 );
84700 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
84701 encnames[pColl->enc]);
84705 FuncDef *pDef = pOp->p4.pFunc;
84706 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84710 FuncDef *pDef = pOp->p4.pCtx->pFunc;
84711 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84715 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
84719 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
84723 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
84727 Mem *pMem = pOp->p4.pMem;
84728 if( pMem->flags & MEM_Str ){
84729 zP4 = pMem->z;
84730 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84731 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
84732 }else if( pMem->flags & MEM_Real ){
84733 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
84734 }else if( pMem->flags & MEM_Null ){
84737 assert( pMem->flags & MEM_Blob );
84744 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
84751 u32 *ai = pOp->p4.ai;
84765 zP4 = pOp->p4.pTab->zName;
84769 zP4 = pOp->p4.z;
84781 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
84785 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
84786 ** p->btreeMask of databases that will require a lock.
84789 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
84790 assert( i<(int)sizeof(p->btreeMask)*8 );
84791 DbMaskSet(p->btreeMask, i);
84792 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
84793 DbMaskSet(p->lockMask, i);
84799 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
84803 ** that the correct busy-handler callback is invoked if required.
84805 ** If SQLite is not threadsafe but does support shared-cache mode, then
84810 ** If SQLite is not threadsafe and does not support shared-cache mode, this
84811 ** function is a no-op.
84813 ** The p->btreeMask field is a bitmask of all btrees that the prepared
84814 ** statement p will ever use. Let N be the number of bits in p->btreeMask
84824 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
84825 db = p->db;
84826 aDb = db->aDb;
84827 nDb = db->nDb;
84829 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
84845 db = p->db;
84846 aDb = db->aDb;
84847 nDb = db->nDb;
84849 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
84855 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
84868 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
84882 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
84883 zP4 ? zP4 : "", pOp->p5,
84896 ** This is a high-runner, so only those fields that really do need to
84911 p->flags = flags;
84912 p->db = db;
84913 p->szMalloc = 0;
84915 p->pScopyFrom = 0;
84918 }while( (--N)>0 );
84933 sqlite3 *db = p->db;
84934 if( db->pnBytesFreed ){
84936 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
84950 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
84951 ** sqlite3MemRelease() were called from here. With -O2, this jumps
84956 testcase( p->flags & MEM_Agg );
84957 testcase( p->flags & MEM_Dyn );
84958 if( p->flags&(MEM_Agg|MEM_Dyn) ){
84959 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
84961 p->flags = MEM_Undefined;
84962 }else if( p->szMalloc ){
84963 sqlite3DbNNFreeNN(db, p->zMalloc);
84964 p->szMalloc = 0;
84965 p->flags = MEM_Undefined;
84969 p->flags = MEM_Undefined;
84984 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
85000 pFrame->pParent = pFrame->v->pDelFrame;
85001 pFrame->v->pDelFrame = pFrame;
85021 int nSub = 0; /* Number of sub-vdbes seen so far */
85022 SubProgram **apSub = 0; /* Array of sub-vdbes */
85033 ** encountered, but p->pc will eventually catch up to nRow.
85035 nRow = p->nOp;
85037 if( pSub->flags&MEM_Blob ){
85040 nSub = pSub->n/sizeof(Vdbe*);
85041 apSub = (SubProgram **)pSub->z;
85044 nRow += apSub[i]->nOp;
85051 p->rc = SQLITE_OK;
85055 if( i<p->nOp ){
85058 aOp = p->aOp;
85063 i -= p->nOp;
85066 for(j=0; i>=apSub[j]->nOp; j++){
85067 i -= apSub[j]->nOp;
85068 assert( i<apSub[j]->nOp || j+1<nSub );
85070 aOp = apSub[j]->aOp;
85075 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
85085 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
85086 if( p->rc!=SQLITE_OK ){
85090 apSub = (SubProgram **)pSub->z;
85093 pSub->n = nSub*sizeof(SubProgram*);
85094 nRow += aOp[i].p4.pProgram->nOp;
85101 if( pOp->opcode==OP_OpenRead ) break;
85102 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
85103 if( pOp->opcode==OP_ReopenIdx ) break;
85127 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
85129 for(i=0; i<p->nChildCsr; i++){
85130 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
85132 releaseMemArray(aMem, p->nChildMem);
85133 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
85134 sqlite3DbFree(p->v->db, p);
85145 ** When p->explain==1, each instruction is listed. When
85146 ** p->explain==2, only OP_Explain instructions are listed and these
85147 ** are shown in a different format. p->explain==2 is used to implement
85149 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
85153 ** When p->explain==1, first the main program is listed, then each of
85160 sqlite3 *db = p->db; /* The database connection */
85163 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
85164 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
85168 assert( p->explain );
85169 assert( p->eVdbeState==VDBE_RUN_STATE );
85170 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
85174 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
85178 if( p->rc==SQLITE_NOMEM ){
85190 assert( p->nMem>9 );
85191 pSub = &p->aMem[9];
85197 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
85201 if( AtomicLoad(&db->u1.isInterrupted) ){
85202 p->rc = SQLITE_INTERRUPT;
85204 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
85207 if( p->explain==2 ){
85208 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
85209 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
85210 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
85211 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
85212 p->nResColumn = 4;
85215 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
85216 -1, SQLITE_UTF8, SQLITE_STATIC);
85217 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
85218 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
85219 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
85221 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
85225 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
85230 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
85231 p->nResColumn = 8;
85233 p->pResultRow = pMem;
85234 if( db->mallocFailed ){
85235 p->rc = SQLITE_NOMEM;
85238 p->rc = SQLITE_OK;
85253 if( p->zSql ){
85254 z = p->zSql;
85255 }else if( p->nOp>=1 ){
85256 const VdbeOp *pOp = &p->aOp[0];
85257 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
85258 z = pOp->p4.z;
85271 int nOp = p->nOp;
85275 pOp = &p->aOp[0];
85276 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
85279 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
85283 if( z[i-1]!=' ' ){
85306 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
85325 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
85328 if( nByte <= p->nFree ){
85329 p->nFree -= nByte;
85330 pBuf = &p->pSpace[p->nFree];
85332 p->nNeeded += nByte;
85348 assert( p->eVdbeState==VDBE_INIT_STATE
85349 || p->eVdbeState==VDBE_READY_STATE
85350 || p->eVdbeState==VDBE_HALT_STATE );
85354 assert( p->nOp>0 );
85356 p->eVdbeState = VDBE_READY_STATE;
85359 for(i=0; i<p->nMem; i++){
85360 assert( p->aMem[i].db==p->db );
85363 p->pc = -1;
85364 p->rc = SQLITE_OK;
85365 p->errorAction = OE_Abort;
85366 p->nChange = 0;
85367 p->cacheCtr = 1;
85368 p->minWriteFileFormat = 255;
85369 p->iStatement = 0;
85370 p->nFkConstraint = 0;
85372 for(i=0; i<p->nOp; i++){
85373 p->aOp[i].nExec = 0;
85374 p->aOp[i].nCycle = 0;
85410 assert( p->nOp>0 );
85412 assert( p->eVdbeState==VDBE_INIT_STATE );
85413 assert( pParse==p->pParse );
85414 p->pVList = pParse->pVList;
85415 pParse->pVList = 0;
85416 db = p->db;
85417 assert( db->mallocFailed==0 );
85418 nVar = pParse->nVar;
85419 nMem = pParse->nMem;
85420 nCursor = pParse->nTab;
85421 nArg = pParse->nMaxArg;
85435 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
85436 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
85438 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
85443 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
85444 if( pParse->explain ){
85451 p->explain = pParse->explain;
85452 if( pParse->explain==2 ){
85462 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
85466 p->expired = 0;
85474 ** This two-pass approach that reuses as much memory as possible from
85479 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
85480 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
85481 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
85482 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
85484 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
85486 if( !db->mallocFailed ){
85487 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
85488 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
85489 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
85490 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
85494 if( db->mallocFailed ){
85495 p->nVar = 0;
85496 p->nCursor = 0;
85497 p->nMem = 0;
85499 p->nCursor = nCursor;
85500 p->nVar = (ynVar)nVar;
85501 initMemArray(p->aVar, nVar, db, MEM_Null);
85502 p->nMem = nMem;
85503 initMemArray(p->aMem, nMem, db, MEM_Undefined);
85504 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
85517 switch( pCx->eCurType ){
85519 sqlite3VdbeSorterClose(p->db, pCx);
85523 assert( pCx->uc.pCursor!=0 );
85524 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
85529 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
85530 const sqlite3_module *pModule = pVCur->pVtab->pModule;
85531 assert( pVCur->pVtab->nRef>0 );
85532 pVCur->pVtab->nRef--;
85533 pModule->xClose(pVCur);
85545 for(i=0; i<p->nCursor; i++){
85546 VdbeCursor *pC = p->apCsr[i];
85549 p->apCsr[i] = 0;
85556 ** is used, for example, when a trigger sub-program is halted to restore
85560 Vdbe *v = pFrame->v;
85562 v->aOp = pFrame->aOp;
85563 v->nOp = pFrame->nOp;
85564 v->aMem = pFrame->aMem;
85565 v->nMem = pFrame->nMem;
85566 v->apCsr = pFrame->apCsr;
85567 v->nCursor = pFrame->nCursor;
85568 v->db->lastRowid = pFrame->lastRowid;
85569 v->nChange = pFrame->nChange;
85570 v->db->nChange = pFrame->nDbChange;
85571 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
85572 v->pAuxData = pFrame->pAuxData;
85573 pFrame->pAuxData = 0;
85574 return pFrame->pc;
85586 if( p->pFrame ){
85588 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
85590 p->pFrame = 0;
85591 p->nFrame = 0;
85593 assert( p->nFrame==0 );
85595 releaseMemArray(p->aMem, p->nMem);
85596 while( p->pDelFrame ){
85597 VdbeFrame *pDel = p->pDelFrame;
85598 p->pDelFrame = pDel->pParent;
85603 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
85604 assert( p->pAuxData==0 );
85615 sqlite3 *db = p->db;
85617 if( p->nResColumn ){
85618 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
85619 sqlite3DbFree(db, p->aColName);
85622 p->nResColumn = (u16)nResColumn;
85623 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
85624 if( p->aColName==0 ) return;
85625 initMemArray(p->aColName, n, db, MEM_Null);
85647 assert( idx<p->nResColumn );
85649 if( p->db->mallocFailed ){
85653 assert( p->aColName!=0 );
85654 pColName = &(p->aColName[idx+var*p->nResColumn]);
85655 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
85656 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
85663 ** write-transaction spanning more than one database file, this routine
85664 ** takes care of the super-journal trickery.
85668 int nTrans = 0; /* Number of databases with an active write-transaction
85669 ** that are candidates for a two-phase commit using a
85670 ** super-journal */
85683 ** be done before determining whether a super-journal file is
85692 ** one database file has an open write transaction, a super-journal
85695 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85696 Btree *pBt = db->aDb[i].pBt;
85698 /* Whether or not a database might need a super-journal depends upon
85700 ** journal modes use a super-journal and which do not */
85713 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
85728 /* If there are any write-transactions at all, invoke the commit hook */
85729 if( needXcommit && db->xCommitCallback ){
85730 rc = db->xCommitCallback(db->pCommitArg);
85736 /* The simple case - no more than one database file (not counting the
85738 ** super-journal.
85742 ** that case we do not support atomic multi-file commits, so use the
85745 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
85748 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85749 Btree *pBt = db->aDb[i].pBt;
85760 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85761 Btree *pBt = db->aDb[i].pBt;
85771 /* The complex case - There is a multi-file write-transaction active.
85772 ** This requires a super-journal file to ensure the transaction is
85777 sqlite3_vfs *pVfs = db->pVfs;
85778 char *zSuper = 0; /* File-name for the super-journal */
85779 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
85786 /* Select a super-journal file name */
85804 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
85806 /* The antipenultimate character of the super-journal name must
85808 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
85813 /* Open the super-journal. */
85820 sqlite3DbFree(db, zSuper-4);
85825 ** super-journal file. If an error occurs at this point close
85826 ** and delete the super-journal file. All the individual journal files
85827 ** still have 'null' as the super-journal pointer, so they will roll
85830 for(i=0; i<db->nDb; i++){
85831 Btree *pBt = db->aDb[i].pBt;
85843 sqlite3DbFree(db, zSuper-4);
85849 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
85857 sqlite3DbFree(db, zSuper-4);
85862 ** sets the super-journal pointer in each individual journal. If
85863 ** an error occurs here, do not delete the super-journal file.
85867 ** super-journal file will be orphaned. But we cannot delete it,
85868 ** in case the super-journal file name was written into the journal
85871 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85872 Btree *pBt = db->aDb[i].pBt;
85880 sqlite3DbFree(db, zSuper-4);
85884 /* Delete the super-journal file. This commits the transaction. After
85889 sqlite3DbFree(db, zSuper-4);
85904 for(i=0; i<db->nDb; i++){
85905 Btree *pBt = db->aDb[i].pBt;
85924 ** This is an internal self-check only - it is not an essential processing
85927 ** This is a no-op if NDEBUG is defined.
85935 p = db->pVdbe;
85939 if( p->readOnly==0 ) nWrite++;
85940 if( p->bIsReader ) nRead++;
85942 p = p->pVNext;
85944 assert( cnt==db->nVdbeActive );
85945 assert( nWrite==db->nVdbeWrite );
85946 assert( nRead==db->nVdbeRead );
85953 ** If the Vdbe passed as the first argument opened a statement-transaction,
85963 sqlite3 *const db = p->db;
85966 const int iSavepoint = p->iStatement-1;
85969 assert( db->nStatement>0 );
85970 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
85972 for(i=0; i<db->nDb; i++){
85974 Btree *pBt = db->aDb[i].pBt;
85987 db->nStatement--;
85988 p->iStatement = 0;
86003 db->nDeferredCons = p->nStmtDefCons;
86004 db->nDeferredImmCons = p->nStmtDefImmCons;
86009 if( p->db->nStatement && p->iStatement ){
86028 sqlite3 *db = p->db;
86029 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
86030 || (!deferred && p->nFkConstraint>0)
86032 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
86033 p->errorAction = OE_Abort;
86035 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
86057 sqlite3 *db = p->db;
86075 assert( p->eVdbeState==VDBE_RUN_STATE );
86076 if( db->mallocFailed ){
86077 p->rc = SQLITE_NOMEM_BKPT;
86084 if( p->bIsReader ){
86085 int mrc; /* Primary error code from p->rc */
86093 if( p->rc ){
86094 mrc = p->rc & 0xff;
86103 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
86108 ** Even if the statement is read-only, it is important to perform
86110 ** occurred while writing to the journal, sub-journal or database
86115 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
86116 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
86124 db->autoCommit = 1;
86125 p->nChange = 0;
86131 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
86135 /* If the auto-commit flag is set and this is the only active writer
86142 && db->autoCommit
86143 && db->nVdbeWrite==(p->readOnly==0)
86145 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
86148 if( NEVER(p->readOnly) ){
86153 }else if( db->flags & SQLITE_CorruptRdOnly ){
86155 db->flags &= ~SQLITE_CorruptRdOnly;
86157 /* The auto-commit flag is true, the vdbe program was successful
86163 if( rc==SQLITE_BUSY && p->readOnly ){
86167 p->rc = rc;
86169 p->nChange = 0;
86171 db->nDeferredCons = 0;
86172 db->nDeferredImmCons = 0;
86173 db->flags &= ~(u64)SQLITE_DeferFKs;
86176 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
86177 p->nChange = 0;
86180 p->nChange = 0;
86182 db->nStatement = 0;
86184 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
86186 }else if( p->errorAction==OE_Abort ){
86191 db->autoCommit = 1;
86192 p->nChange = 0;
86196 /* If eStatementOp is non-zero, then a statement transaction needs to
86205 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
86206 p->rc = rc;
86207 sqlite3DbFree(db, p->zErrMsg);
86208 p->zErrMsg = 0;
86212 db->autoCommit = 1;
86213 p->nChange = 0;
86218 ** has been rolled back, update the database connection change-counter.
86220 if( p->changeCntOn ){
86222 sqlite3VdbeSetChanges(db, p->nChange);
86226 p->nChange = 0;
86234 db->nVdbeActive--;
86235 if( !p->readOnly ) db->nVdbeWrite--;
86236 if( p->bIsReader ) db->nVdbeRead--;
86237 assert( db->nVdbeActive>=db->nVdbeRead );
86238 assert( db->nVdbeRead>=db->nVdbeWrite );
86239 assert( db->nVdbeWrite>=0 );
86240 p->eVdbeState = VDBE_HALT_STATE;
86242 if( db->mallocFailed ){
86243 p->rc = SQLITE_NOMEM_BKPT;
86246 /* If the auto-commit flag is set to true, then any locks that were held
86248 ** to invoke any required unlock-notify callbacks.
86250 if( db->autoCommit ){
86254 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
86255 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
86261 ** in p->rc. This routine sets that result back to SQLITE_OK.
86264 p->rc = SQLITE_OK;
86276 sqlite3 *db = p->db;
86277 int rc = p->rc;
86278 if( p->zErrMsg ){
86279 db->bBenignMalloc++;
86281 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
86282 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
86284 db->bBenignMalloc--;
86285 }else if( db->pErr ){
86286 sqlite3ValueSetNull(db->pErr);
86288 db->errCode = rc;
86289 db->errByteOffset = -1;
86299 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
86300 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
86301 assert( v->db->init.busy==0 );
86304 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
86306 sqlite3DbFree(v->db, zExpanded);
86331 db = p->db;
86337 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
86344 if( p->pc>=0 ){
86346 if( db->pErr || p->zErrMsg ){
86349 db->errCode = p->rc;
86358 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
86359 if( p->aMem ){
86360 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
86363 if( p->zErrMsg ){
86364 sqlite3DbFree(db, p->zErrMsg);
86365 p->zErrMsg = 0;
86367 p->pResultRow = 0;
86369 p->nWrite = 0;
86378 fprintf(out, "---- ");
86379 for(i=0; i<p->nOp; i++){
86380 fprintf(out, "%02x", p->aOp[i].opcode);
86383 if( p->zSql ){
86385 fprintf(out, "-- ");
86386 for(i=0; (c = p->zSql[i])!=0; i++){
86387 if( pc=='\n' ) fprintf(out, "-- ");
86393 for(i=0; i<p->nOp; i++){
86395 i64 cnt = p->aOp[i].nExec;
86396 i64 cycles = p->aOp[i].nCycle;
86403 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
86409 return p->rc & db->errMask;
86421 if( p->eVdbeState>=VDBE_READY_STATE ){
86423 assert( (rc & p->db->errMask)==rc );
86449 || (pAux->iAuxOp==iOp
86450 && pAux->iAuxArg>=0
86451 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
86453 testcase( pAux->iAuxArg==31 );
86454 if( pAux->xDeleteAux ){
86455 pAux->xDeleteAux(pAux->pAux);
86457 *pp = pAux->pNextAux;
86460 pp= &pAux->pNextAux;
86476 assert( p->db==0 || p->db==db );
86477 if( p->aColName ){
86478 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
86479 sqlite3DbNNFreeNN(db, p->aColName);
86481 for(pSub=p->pProgram; pSub; pSub=pNext){
86482 pNext = pSub->pNext;
86483 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
86486 if( p->eVdbeState!=VDBE_INIT_STATE ){
86487 releaseMemArray(p->aVar, p->nVar);
86488 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
86489 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
86491 vdbeFreeOpArray(db, p->aOp, p->nOp);
86492 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
86494 sqlite3DbFree(db, p->zNormSql);
86497 for(pThis=p->pDblStr; pThis; pThis=pNxt){
86498 pNxt = pThis->pNextStr;
86506 for(i=0; i<p->nScan; i++){
86507 sqlite3DbFree(db, p->aScan[i].zName);
86509 sqlite3DbFree(db, p->aScan);
86521 db = p->db;
86523 assert( sqlite3_mutex_held(db->mutex) );
86525 if( db->pnBytesFreed==0 ){
86526 assert( p->ppVPrev!=0 );
86527 *p->ppVPrev = p->pVNext;
86528 if( p->pVNext ){
86529 p->pVNext->ppVPrev = p->ppVPrev;
86545 assert( p->deferredMoveto );
86546 assert( p->isTable );
86547 assert( p->eCurType==CURTYPE_BTREE );
86548 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
86554 p->deferredMoveto = 0;
86555 p->cacheStatus = CACHE_STALE;
86568 assert( p->eCurType==CURTYPE_BTREE );
86569 assert( p->uc.pCursor!=0 );
86570 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
86571 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
86572 p->cacheStatus = CACHE_STALE;
86573 if( isDifferentRow ) p->nullRow = 1;
86582 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
86583 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
86595 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
86600 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
86607 ** serial-type and data blob separately.
86612 ** -------------- --------------- ---------------
86624 ** N>=12 and even (N-12)/2 BLOB
86625 ** N>=13 and odd (N-13)/2 text
86633 ** Return the serial-type for the value stored in pMem.
86637 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
86638 ** opcode in the byte-code engine. But by moving this routine in-line, we
86644 int flags = pMem->flags;
86654 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
86655 i64 i = pMem->u.i;
86680 ** as an integer, then we might as well make it an 8-byte floating
86682 pMem->u.r = (double)pMem->u.i;
86683 pMem->flags &= ~MEM_IntReal;
86684 pMem->flags |= MEM_Real;
86693 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
86694 assert( pMem->n>=0 );
86695 n = (u32)pMem->n;
86697 n += pMem->u.nZero;
86725 ** Return the length of the data corresponding to the supplied serial-type.
86729 return (serial_type-12)/2;
86732 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
86742 ** If we are on an architecture with mixed-endian floating
86746 ** For most architectures, this is a no-op.
86748 ** (later): It is reported to me that the mixed-endian problem
86749 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
86750 ** that early versions of GCC stored the two words of a 64-bit
86752 ** ever since. The blame is not necessarily with GCC, though.
86753 ** GCC might have just copying the problem from a prior compiler.
86754 ** I am also told that newer versions of GCC that follow a different
86758 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
86762 ** (2007-08-30) Frank van Vugt has studied this problem closely
86765 ** emulation that uses only 32-bit mantissas instead of a full
86766 ** 48-bits as required by the IEEE standard. (This is the
86769 ** the necessary byte swapping is carried out using a 64-bit integer
86770 ** rather than a 64-bit float. Frank assures us that the code here
86793 ** big-endian integer. Return the equivalent native integer
86819 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
86820 ** twos-complement integer. */
86821 pMem->u.i = *(i64*)&x;
86822 pMem->flags = MEM_Int;
86823 testcase( pMem->u.i<0 );
86825 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
86830 ** defined that 64-bit floating point values really are mixed
86839 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
86841 memcpy(&pMem->u.r, &x, sizeof(x));
86842 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
86852 ** UPDATE no-change flag set */
86853 pMem->flags = MEM_Null|MEM_Zero;
86854 pMem->n = 0;
86855 pMem->u.nZero = 0;
86860 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
86861 pMem->flags = MEM_Null;
86865 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
86867 pMem->u.i = ONE_BYTE_INT(buf);
86868 pMem->flags = MEM_Int;
86869 testcase( pMem->u.i<0 );
86872 case 2: { /* 2-byte signed integer */
86873 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
86874 ** twos-complement integer. */
86875 pMem->u.i = TWO_BYTE_INT(buf);
86876 pMem->flags = MEM_Int;
86877 testcase( pMem->u.i<0 );
86880 case 3: { /* 3-byte signed integer */
86881 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
86882 ** twos-complement integer. */
86883 pMem->u.i = THREE_BYTE_INT(buf);
86884 pMem->flags = MEM_Int;
86885 testcase( pMem->u.i<0 );
86888 case 4: { /* 4-byte signed integer */
86889 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
86890 ** twos-complement integer. */
86891 pMem->u.i = FOUR_BYTE_INT(buf);
86893 /* Work around a sign-extension bug in the HP compiler for HP/UX */
86894 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
86896 pMem->flags = MEM_Int;
86897 testcase( pMem->u.i<0 );
86900 case 5: { /* 6-byte signed integer */
86901 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
86902 ** twos-complement integer. */
86903 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
86904 pMem->flags = MEM_Int;
86905 testcase( pMem->u.i<0 );
86908 case 6: /* 8-byte signed integer */
86917 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
86918 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
86919 pMem->u.i = serial_type-8;
86920 pMem->flags = MEM_Int;
86924 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
86926 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
86927 ** (N-13)/2 bytes in length. */
86929 pMem->z = (char *)buf;
86930 pMem->n = (serial_type-12)/2;
86931 pMem->flags = aFlag[serial_type&1];
86956 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
86957 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
86959 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
86960 assert( pKeyInfo->aSortFlags!=0 );
86961 p->pKeyInfo = pKeyInfo;
86962 p->nField = pKeyInfo->nKeyField + 1;
86967 ** Given the nKey-byte encoding of a record in pKey[], populate the
86982 Mem *pMem = p->aMem;
86984 p->default_rc = 0;
86993 pMem->enc = pKeyInfo->enc;
86994 pMem->db = pKeyInfo->db;
86995 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
86996 pMem->szMalloc = 0;
86997 pMem->z = 0;
87001 if( (++u)>=p->nField ) break;
87008 sqlite3VdbeMemSetNull(pMem-1);
87010 assert( u<=pKeyInfo->nKeyField + 1 );
87011 p->nField = u;
87040 pKeyInfo = pPKey2->pKeyInfo;
87041 if( pKeyInfo->db==0 ) return 1;
87042 mem1.enc = pKeyInfo->enc;
87043 mem1.db = pKeyInfo->db;
87059 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
87060 assert( pKeyInfo->aSortFlags!=0 );
87061 assert( pKeyInfo->nKeyField>0 );
87088 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
87089 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
87092 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
87093 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
87095 rc = -rc;
87097 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
87098 rc = -rc; /* Invert the result for DESC sort order. */
87103 }while( idx1<szHdr1 && i<pPKey2->nField );
87114 rc = pPKey2->default_rc;
87121 if( pKeyInfo->db->mallocFailed ) return 1;
87130 ** limit given by pKeyInfo->nAllField.
87132 ** If this constraint is not satisfied, it means that the high-speed
87156 assert( nField <= pKeyInfo->nAllField );
87166 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
87174 if( pMem1->enc==pColl->enc ){
87177 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
87183 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
87184 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
87187 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
87188 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
87193 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
87203 ** with MEM_Zero. Return true if it could be a zero-blob.
87220 int n1 = pB1->n;
87221 int n2 = pB2->n;
87223 /* It is possible to have a Blob value that has some non-zero content
87227 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
87228 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
87230 if( (pB1->flags|pB2->flags) & MEM_Zero ){
87231 if( pB1->flags & pB2->flags & MEM_Zero ){
87232 return pB1->u.nZero - pB2->u.nZero;
87233 }else if( pB1->flags & MEM_Zero ){
87234 if( !isAllZero(pB2->z, pB2->n) ) return -1;
87235 return pB1->u.nZero - n2;
87237 if( !isAllZero(pB1->z, pB1->n) ) return +1;
87238 return n1 - pB2->u.nZero;
87241 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
87243 return n1 - n2;
87247 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
87257 if( x<r ) return -1;
87263 if( r<-9223372036854775808.0 ) return +1;
87264 if( r>=9223372036854775808.0 ) return -1;
87266 if( i<y ) return -1;
87269 if( s<r ) return -1;
87288 f1 = pMem1->flags;
87289 f2 = pMem2->flags;
87297 return (f2&MEM_Null) - (f1&MEM_Null);
87309 if( pMem1->u.i < pMem2->u.i ) return -1;
87310 if( pMem1->u.i > pMem2->u.i ) return +1;
87314 if( pMem1->u.r < pMem2->u.r ) return -1;
87315 if( pMem1->u.r > pMem2->u.r ) return +1;
87322 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
87324 if( pMem1->u.i < pMem2->u.i ) return -1;
87325 if( pMem1->u.i > pMem2->u.i ) return +1;
87328 return -1;
87335 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
87337 return -1;
87351 return -1;
87354 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
87355 assert( pMem1->enc==SQLITE_UTF8 ||
87356 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
87362 assert( !pColl || pColl->xCmp );
87377 ** The first argument passed to this function is a serial-type that
87378 ** corresponds to an integer - all values between 1 and 9 inclusive
87414 return (serial_type - 8);
87426 ** If argument bSkip is non-zero, it is assumed that the caller has already
87430 ** fields that appear in both keys are equal, then pPKey2->default_rc is
87433 ** If database corruption is discovered, set pPKey2->errCode to
87435 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
87436 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
87448 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
87477 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87482 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
87484 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
87485 assert( pPKey2->pKeyInfo->nKeyField>0 );
87487 while( 1 /*exit-by-break*/ ){
87491 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
87492 testcase( pRhs->flags & MEM_Int );
87493 testcase( pRhs->flags & MEM_IntReal );
87497 rc = serial_type==10 ? -1 : +1;
87499 rc = -1;
87502 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
87505 i64 rhs = pRhs->u.i;
87507 rc = -1;
87515 else if( pRhs->flags & MEM_Real ){
87522 rc = serial_type==10 ? -1 : +1;
87524 rc = -1;
87528 if( mem1.u.r<pRhs->u.r ){
87529 rc = -1;
87530 }else if( mem1.u.r>pRhs->u.r ){
87534 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
87540 else if( pRhs->flags & MEM_Str ){
87544 rc = -1;
87548 mem1.n = (serial_type - 12) / 2;
87552 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
87554 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87556 }else if( pKeyInfo->aColl[i] ){
87557 mem1.enc = pKeyInfo->enc;
87558 mem1.db = pKeyInfo->db;
87562 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
87565 int nCmp = MIN(mem1.n, pRhs->n);
87566 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87567 if( rc==0 ) rc = mem1.n - pRhs->n;
87573 else if( pRhs->flags & MEM_Blob ){
87574 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
87578 rc = -1;
87580 int nStr = (serial_type - 12) / 2;
87584 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87586 }else if( pRhs->flags & MEM_Zero ){
87590 rc = nStr - pRhs->u.nZero;
87593 int nCmp = MIN(nStr, pRhs->n);
87594 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87595 if( rc==0 ) rc = nStr - pRhs->n;
87607 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
87611 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
87613 rc = -rc;
87622 if( i==pPKey2->nField ) break;
87628 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87642 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
87643 || pPKey2->pKeyInfo->db->mallocFailed
87645 pPKey2->eqSeen = 1;
87646 return pPKey2->default_rc;
87659 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
87677 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
87680 case 1: { /* 1-byte signed integer */
87685 case 2: { /* 2-byte signed integer */
87690 case 3: { /* 3-byte signed integer */
87695 case 4: { /* 4-byte signed integer */
87701 case 5: { /* 6-byte signed integer */
87706 case 6: { /* 8-byte signed integer */
87721 ** this code. Including it causes gcc to generate a faster switch
87724 ** (as gcc is clever enough to combine the two like cases). Other
87733 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
87734 v = pPKey2->u.i;
87736 res = pPKey2->r1;
87738 res = pPKey2->r2;
87739 }else if( pPKey2->nField>1 ){
87745 ** fields. Return pPKey2->default_rc in this case. */
87746 res = pPKey2->default_rc;
87747 pPKey2->eqSeen = 1;
87757 ** uses the collation sequence BINARY and (c) that the size-of-header varint
87768 assert( pPKey2->aMem[0].flags & MEM_Str );
87769 assert( pPKey2->aMem[0].n == pPKey2->n );
87770 assert( pPKey2->aMem[0].z == pPKey2->u.z );
87771 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
87781 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
87783 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
87789 nStr = (serial_type-12) / 2;
87791 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87794 nCmp = MIN( pPKey2->n, nStr );
87795 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
87798 res = pPKey2->r2;
87800 res = pPKey2->r1;
87802 res = nStr - pPKey2->n;
87804 if( pPKey2->nField>1 ){
87807 res = pPKey2->default_rc;
87808 pPKey2->eqSeen = 1;
87811 res = pPKey2->r2;
87813 res = pPKey2->r1;
87820 || pPKey2->pKeyInfo->db->mallocFailed
87832 ** that the size-of-header varint that occurs at the start of each record
87844 if( p->pKeyInfo->nAllField<=13 ){
87845 int flags = p->aMem[0].flags;
87846 if( p->pKeyInfo->aSortFlags[0] ){
87847 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
87850 p->r1 = 1;
87851 p->r2 = -1;
87853 p->r1 = -1;
87854 p->r2 = 1;
87857 p->u.i = p->aMem[0].u.i;
87864 && p->pKeyInfo->aColl[0]==0
87867 p->u.z = p->aMem[0].z;
87868 p->n = p->aMem[0].n;
87893 ** than 2GiB are support - anything large must be database corruption.
87895 ** this code can safely assume that nCellKey is 32-bits
87918 /* The last field of the index should be an integer - the ROWID.
87920 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
87939 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
87974 assert( pC->eCurType==CURTYPE_BTREE );
87975 pCur = pC->uc.pCursor;
87999 assert( sqlite3_mutex_held(db->mutex) );
88000 db->nChange = nChange;
88001 db->nTotalChange += nChange;
88009 v->changeCntOn = 1;
88018 ** programs obsolete. Removing user-defined functions or collating
88032 for(p = db->pVdbe; p; p=p->pVNext){
88033 p->expired = iCode+1;
88041 return v->db;
88048 return v->prepFlags;
88062 Mem *pMem = &v->aVar[iVar-1];
88063 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
88064 if( 0==(pMem->flags & MEM_Null) ){
88065 sqlite3_value *pRet = sqlite3ValueNew(v->db);
88078 ** to sqlite3_reoptimize() that re-preparing the statement may result
88083 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
88085 v->expmask |= 0x80000000;
88087 v->expmask |= ((u32)1 << (iVar-1));
88096 ** throw an error if it is given inputs that would make it non-deterministic.
88097 ** This routine is invoked by date/time functions that use non-deterministic
88103 if( pCtx->pVdbe==0 ) return 1;
88105 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
88106 if( pOp->opcode==OP_PureFunc ){
88109 if( pOp->p5 & NC_IsCheck ){
88111 }else if( pOp->p5 & NC_GenCol ){
88116 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
88117 pCtx->pFunc->zName, zContext);
88118 sqlite3_result_error(pCtx, zMsg, -1);
88129 ** byte-code register values correctly initialized.
88132 if( pExpr->op==TK_REGISTER ){
88133 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
88146 if( pVtab->zErrMsg ){
88147 sqlite3 *db = p->db;
88148 sqlite3DbFree(db, p->zErrMsg);
88149 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
88150 sqlite3_free(pVtab->zErrMsg);
88151 pVtab->zErrMsg = 0;
88160 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
88171 Mem *pMem = &p->aMem[i];
88172 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
88181 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
88187 Vdbe *v, /* Vdbe pre-update hook is invoked by */
88196 sqlite3 *db = v->db;
88199 const char *zTbl = pTab->zName;
88203 if( pTab->tabFlags & TF_WithoutRowid ){
88204 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
88205 }else if( pTab->tabFlags & TF_HasVirtual ){
88206 nRealCol = pTab->nNVCol;
88208 nRealCol = pTab->nCol;
88212 assert( db->pPreUpdate==0 );
88219 iKey2 = v->aMem[iReg].u.i;
88226 assert( pCsr->eCurType==CURTYPE_BTREE );
88227 assert( pCsr->nField==nRealCol
88228 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
88237 preupdate.keyinfo.nKeyField = pTab->nCol;
88244 db->pPreUpdate = &preupdate;
88245 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
88246 db->pPreUpdate = 0;
88252 for(i=0; i<pCsr->nField; i++){
88283 ** Return TRUE (non-zero) of the statement supplied as an argument needs
88292 return p==0 || p->expired;
88302 if( p->db==0 ){
88326 assert( p->startTime>0 );
88327 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
88328 assert( db->init.busy==0 );
88329 assert( p->zSql!=0 );
88330 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
88331 iElapse = (iNow - p->startTime)*1000000;
88333 if( db->xProfile ){
88334 db->xProfile(db->pProfileArg, p->zSql, iElapse);
88337 if( db->mTrace & SQLITE_TRACE_PROFILE ){
88338 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
88340 p->startTime = 0;
88347 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
88349 # define checkProfileCallback(DB,P) /*no-op*/
88364 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
88365 ** pointer is a harmless no-op. */
88369 sqlite3 *db = v->db;
88371 sqlite3_mutex_enter(db->mutex);
88373 assert( v->eVdbeState>=VDBE_READY_STATE );
88396 sqlite3 *db = v->db;
88397 sqlite3_mutex_enter(db->mutex);
88401 assert( (rc & (db->errMask))==rc );
88403 sqlite3_mutex_leave(db->mutex);
88416 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
88419 for(i=0; i<p->nVar; i++){
88420 sqlite3VdbeMemRelease(&p->aVar[i]);
88421 p->aVar[i].flags = MEM_Null;
88423 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
88424 if( p->expmask ){
88425 p->expired = 1;
88438 if( p->flags & (MEM_Blob|MEM_Str) ){
88440 assert( p->flags==MEM_Null && p->z==0 );
88443 p->flags |= MEM_Blob;
88444 return p->n ? p->z : 0;
88466 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
88470 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
88473 && p->eSubtype=='p'
88474 && strcmp(p->u.zPType, zPType)==0
88476 return (void*)p->z;
88495 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
88496 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
88569 if( pVal->flags & MEM_Null ){
88571 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
88573 }else if( pVal->flags & MEM_Int ){
88575 }else if( pVal->flags & MEM_Str ){
88578 assert( eType == aType[pVal->flags&MEM_AffMask] );
88581 return aType[pVal->flags&MEM_AffMask];
88584 return pVal->enc;
88589 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
88594 return (pVal->flags&MEM_FromBind)!=0;
88606 pNew->flags &= ~MEM_Dyn;
88607 pNew->db = 0;
88608 if( pNew->flags&(MEM_Str|MEM_Blob) ){
88609 pNew->flags &= ~(MEM_Static|MEM_Dyn);
88610 pNew->flags |= MEM_Ephem;
88615 }else if( pNew->flags & MEM_Null ){
88617 pNew->flags &= ~(MEM_Term|MEM_Subtype);
88631 ** The following routines are used by user-defined functions to specify
88648 Mem *pOut = pCtx->pOut;
88661 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
88689 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88698 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88707 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88708 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
88711 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88712 pCtx->isError = SQLITE_ERROR;
88713 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
88717 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88718 pCtx->isError = SQLITE_ERROR;
88719 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
88723 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88724 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
88727 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88728 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
88731 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88732 sqlite3VdbeMemSetNull(pCtx->pOut);
88740 Mem *pOut = pCtx->pOut;
88741 assert( sqlite3_mutex_held(pOut->db->mutex) );
88743 pOut->flags = MEM_Null;
88747 Mem *pOut = pCtx->pOut;
88748 assert( sqlite3_mutex_held(pOut->db->mutex) );
88749 pOut->eSubtype = eSubtype & 0xff;
88750 pOut->flags |= MEM_Subtype;
88758 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88768 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88787 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88796 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88805 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88810 Mem *pOut = pCtx->pOut;
88811 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88813 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
88822 Mem *pOut = pCtx->pOut;
88823 assert( sqlite3_mutex_held(pOut->db->mutex) );
88824 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
88829 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
88832 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
88836 pCtx->isError = errCode ? errCode : -1;
88838 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
88840 if( pCtx->pOut->flags & MEM_Null ){
88841 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
88848 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88849 pCtx->isError = SQLITE_TOOBIG;
88850 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
88856 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88857 sqlite3VdbeMemSetNull(pCtx->pOut);
88858 pCtx->isError = SQLITE_NOMEM_BKPT;
88859 sqlite3OomFault(pCtx->pOut->db);
88865 ** test-control.
88868 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88869 if( pCtx->pOut->flags & MEM_Int ){
88870 pCtx->pOut->flags &= ~MEM_Int;
88871 pCtx->pOut->flags |= MEM_IntReal;
88885 for(i=0; i<db->nDb; i++){
88886 Btree *pBt = db->aDb[i].pBt;
88892 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
88893 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
88916 db = p->db;
88917 if( p->eVdbeState!=VDBE_RUN_STATE ){
88919 if( p->eVdbeState==VDBE_READY_STATE ){
88920 if( p->expired ){
88921 p->rc = SQLITE_SCHEMA;
88923 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
88925 ** error has occurred, then return the error code in p->rc to the
88938 if( db->nVdbeActive==0 ){
88939 AtomicStore(&db->u1.isInterrupted, 0);
88942 assert( db->nVdbeWrite>0 || db->autoCommit==0
88943 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
88947 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
88948 && !db->init.busy && p->zSql ){
88949 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
88951 assert( p->startTime==0 );
88955 db->nVdbeActive++;
88956 if( p->readOnly==0 ) db->nVdbeWrite++;
88957 if( p->bIsReader ) db->nVdbeRead++;
88958 p->pc = 0;
88959 p->eVdbeState = VDBE_RUN_STATE;
88962 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
88967 ** This "automatic-reset" change is not technically an incompatibility,
88973 ** returns, and those were broken by the automatic-reset change. As a
88974 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
88980 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
88988 assert( p->eVdbeState==VDBE_READY_STATE );
88994 p->rcApp = SQLITE_OK;
88997 if( p->explain ){
89002 db->nVdbeExec++;
89004 db->nVdbeExec--;
89008 assert( p->rc==SQLITE_OK );
89009 assert( db->mallocFailed==0 );
89010 db->errCode = SQLITE_ROW;
89017 p->pResultRow = 0;
89018 if( rc==SQLITE_DONE && db->autoCommit ){
89019 assert( p->rc==SQLITE_OK );
89020 p->rc = doWalCallbacks(db);
89021 if( p->rc!=SQLITE_OK ){
89024 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
89026 ** error has occurred, then return the error code in p->rc to the
89033 db->errCode = rc;
89034 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
89035 p->rc = SQLITE_NOMEM_BKPT;
89036 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
89041 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
89045 return (rc&db->errMask);
89049 ** This is the top-level implementation of sqlite3_step(). Call
89062 db = v->db;
89063 sqlite3_mutex_enter(db->mutex);
89066 int savedPc = v->pc;
89077 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
89078 sqlite3DbFree(db, v->zErrMsg);
89079 if( !db->mallocFailed ){
89080 v->zErrMsg = sqlite3DbStrDup(db, zErr);
89081 v->rc = rc = sqlite3ApiExit(db, rc);
89083 v->zErrMsg = 0;
89084 v->rc = rc = SQLITE_NOMEM_BKPT;
89093 ** SQLITE_SCHEMA. tag-20220401a */
89094 v->minWriteFileFormat = 254;
89096 assert( v->expired==0 );
89098 sqlite3_mutex_leave(db->mutex);
89108 assert( p && p->pFunc );
89109 return p->pFunc->pUserData;
89116 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
89123 assert( p && p->pOut );
89124 return p->pOut->db;
89138 ** performance by substituting a NULL result, or some other light-weight
89143 return sqlite3_value_nochange(p->pOut);
89171 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
89174 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
89176 assert( pVal->eSubtype=='p' );
89177 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
89178 pRhs = (ValueList*)pVal->z;
89181 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
89184 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
89185 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
89186 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
89192 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
89193 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
89197 sqlite3_value *pOut = pRhs->pOut;
89200 pOut->enc = ENC(pOut->db);
89201 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
89238 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
89239 assert( p->pVdbe!=0 );
89242 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
89245 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
89253 ** its pMem->z element.
89256 Mem *pMem = p->pMem;
89257 assert( (pMem->flags & MEM_Agg)==0 );
89260 pMem->z = 0;
89263 pMem->flags = MEM_Agg;
89264 pMem->u.pDef = p->pFunc;
89265 if( pMem->z ){
89266 memset(pMem->z, 0, nByte);
89269 return (void*)pMem->z;
89278 assert( p && p->pFunc && p->pFunc->xFinalize );
89279 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
89281 if( (p->pMem->flags & MEM_Agg)==0 ){
89284 return (void*)p->pMem->z;
89290 ** the user-function defined by pCtx.
89292 ** The left-most argument is 0.
89301 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89303 if( pCtx->pVdbe==0 ) return 0;
89305 assert( pCtx->pVdbe!=0 );
89307 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
89308 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
89309 return pAuxData->pAux;
89317 ** argument to the user-function defined by pCtx. Any previous value is
89320 ** The left-most argument is 0.
89333 Vdbe *pVdbe = pCtx->pVdbe;
89335 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89342 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
89343 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
89348 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
89350 pAuxData->iAuxOp = pCtx->iOp;
89351 pAuxData->iAuxArg = iArg;
89352 pAuxData->pNextAux = pVdbe->pAuxData;
89353 pVdbe->pAuxData = pAuxData;
89354 if( pCtx->isError==0 ) pCtx->isError = -1;
89355 }else if( pAuxData->xDeleteAux ){
89356 pAuxData->xDeleteAux(pAuxData->pAux);
89359 pAuxData->pAux = pAux;
89360 pAuxData->xDeleteAux = xDelete;
89380 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
89381 return p->pMem->n;
89390 return pVm ? pVm->nResColumn : 0;
89399 if( pVm==0 || pVm->pResultRow==0 ) return 0;
89400 return pVm->nResColumn;
89409 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
89410 ** instead of an 8-byte one. This all works fine, except that when
89412 ** that a Mem structure is located on an 8-byte boundary. To prevent
89414 ** using gcc, we force nullMem to be 8-byte aligned using the magical
89452 assert( pVm->db );
89453 sqlite3_mutex_enter(pVm->db->mutex);
89454 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
89455 pOut = &pVm->pResultRow[i];
89457 sqlite3Error(pVm->db, SQLITE_RANGE);
89490 assert( p->db!=0 );
89491 assert( sqlite3_mutex_held(p->db->mutex) );
89492 p->rc = sqlite3ApiExit(p->db, p->rc);
89493 sqlite3_mutex_leave(p->db->mutex);
89543 if( pOut->flags&MEM_Static ){
89544 pOut->flags &= ~MEM_Static;
89545 pOut->flags |= MEM_Ephem;
89564 ** Convert the N-th element of pStmt->pColName[] into a string using
89597 db = p->db;
89601 u8 prior_mallocFailed = db->mallocFailed;
89603 sqlite3_mutex_enter(db->mutex);
89606 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
89610 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
89615 assert( db->mallocFailed==0 || db->mallocFailed==1 );
89616 if( db->mallocFailed > prior_mallocFailed ){
89620 sqlite3_mutex_leave(db->mutex);
89719 ** The error code stored in database p->db is overwritten with the return
89727 sqlite3_mutex_enter(p->db->mutex);
89728 if( p->eVdbeState!=VDBE_READY_STATE ){
89729 sqlite3Error(p->db, SQLITE_MISUSE);
89730 sqlite3_mutex_leave(p->db->mutex);
89732 "bind on a busy prepared statement: [%s]", p->zSql);
89735 if( i>=(unsigned int)p->nVar ){
89736 sqlite3Error(p->db, SQLITE_RANGE);
89737 sqlite3_mutex_leave(p->db->mutex);
89740 pVar = &p->aVar[i];
89742 pVar->flags = MEM_Null;
89743 p->db->errCode = SQLITE_OK;
89748 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
89754 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
89755 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
89756 p->expired = 1;
89776 rc = vdbeUnbind(p, (u32)(i-1));
89779 pVar = &p->aVar[i-1];
89782 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
89785 sqlite3Error(p->db, rc);
89786 rc = sqlite3ApiExit(p->db, rc);
89789 sqlite3_mutex_leave(p->db->mutex);
89825 rc = vdbeUnbind(p, (u32)(i-1));
89827 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
89828 sqlite3_mutex_leave(p->db->mutex);
89838 rc = vdbeUnbind(p, (u32)(i-1));
89840 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
89841 sqlite3_mutex_leave(p->db->mutex);
89848 rc = vdbeUnbind(p, (u32)(i-1));
89850 sqlite3_mutex_leave(p->db->mutex);
89863 rc = vdbeUnbind(p, (u32)(i-1));
89865 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
89866 sqlite3_mutex_leave(p->db->mutex);
89911 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
89915 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
89917 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
89922 if( pValue->flags & MEM_Zero ){
89923 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
89925 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
89930 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
89931 pValue->enc);
89944 rc = vdbeUnbind(p, (u32)(i-1));
89947 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
89949 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
89951 sqlite3_mutex_leave(p->db->mutex);
89958 sqlite3_mutex_enter(p->db->mutex);
89959 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
89965 rc = sqlite3ApiExit(p->db, rc);
89966 sqlite3_mutex_leave(p->db->mutex);
89976 return p ? p->nVar : 0;
89983 ** The result is always UTF-8.
89988 return sqlite3VListNumToName(p->pVList, i);
89998 return sqlite3VListNameToNum(p->pVList, zName, nName);
90011 assert( pTo->db==pFrom->db );
90012 assert( pTo->nVar==pFrom->nVar );
90013 sqlite3_mutex_enter(pTo->db->mutex);
90014 for(i=0; i<pFrom->nVar; i++){
90015 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
90017 sqlite3_mutex_leave(pTo->db->mutex);
90037 if( pFrom->nVar!=pTo->nVar ){
90040 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
90041 if( pTo->expmask ){
90042 pTo->expired = 1;
90044 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
90045 if( pFrom->expmask ){
90046 pFrom->expired = 1;
90059 return pStmt ? ((Vdbe*)pStmt)->db : 0;
90067 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
90075 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
90083 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
90100 sqlite3_mutex_enter(pDb->mutex);
90102 pNext = (sqlite3_stmt*)pDb->pVdbe;
90104 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
90106 sqlite3_mutex_leave(pDb->mutex);
90118 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
90125 sqlite3 *db = pVdbe->db;
90126 sqlite3_mutex_enter(db->mutex);
90128 db->pnBytesFreed = (int*)&v;
90129 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
90130 db->lookaside.pEnd = db->lookaside.pStart;
90132 db->pnBytesFreed = 0;
90133 db->lookaside.pEnd = db->lookaside.pTrueEnd;
90134 sqlite3_mutex_leave(db->mutex);
90136 v = pVdbe->aCounter[op];
90137 if( resetFlag ) pVdbe->aCounter[op] = 0;
90147 return p ? p->zSql : 0;
90167 sqlite3_mutex_enter(p->db->mutex);
90169 sqlite3_mutex_leave(p->db->mutex);
90182 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
90183 sqlite3_mutex_enter(p->db->mutex);
90184 p->zNormSql = sqlite3Normalize(p, p->zSql);
90185 sqlite3_mutex_leave(p->db->mutex);
90187 return p->zNormSql;
90206 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
90213 ** This function is called from within a pre-update callback to retrieve
90217 PreUpdate *p = db->pPreUpdate;
90222 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
90223 if( !p || p->op==SQLITE_INSERT ){
90227 if( p->pPk ){
90228 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
90230 if( iIdx>=p->pCsr->nField || iIdx<0 ){
90236 if( p->pUnpacked==0 ){
90240 assert( p->pCsr->eCurType==CURTYPE_BTREE );
90241 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
90244 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
90246 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
90247 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
90253 p->aRecord = aRec;
90256 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
90257 if( iIdx==p->pTab->iPKey ){
90258 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
90259 }else if( iIdx>=p->pUnpacked->nField ){
90261 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
90262 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
90263 testcase( pMem->flags & MEM_Int );
90264 testcase( pMem->flags & MEM_IntReal );
90277 ** This function is called from within a pre-update callback to retrieve
90281 PreUpdate *p = db->pPreUpdate;
90282 return (p ? p->keyinfo.nKeyField : 0);
90288 ** This function is designed to be called from within a pre-update callback
90292 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
90293 ** top-level trigger etc.).
90299 PreUpdate *p = db->pPreUpdate;
90300 return (p ? p->v->nFrame : 0);
90306 ** This function is designed to be called from within a pre-update callback
90310 PreUpdate *p = db->pPreUpdate;
90311 return (p ? p->iBlobWrite : -1);
90317 ** This function is called from within a pre-update callback to retrieve
90321 PreUpdate *p = db->pPreUpdate;
90325 if( !p || p->op==SQLITE_DELETE ){
90329 if( p->pPk && p->op!=SQLITE_UPDATE ){
90330 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
90332 if( iIdx>=p->pCsr->nField || iIdx<0 ){
90337 if( p->op==SQLITE_INSERT ){
90338 /* For an INSERT, memory cell p->iNewReg contains the serialized record
90340 UnpackedRecord *pUnpack = p->pNewUnpacked;
90342 Mem *pData = &p->v->aMem[p->iNewReg];
90345 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
90350 p->pNewUnpacked = pUnpack;
90352 pMem = &pUnpack->aMem[iIdx];
90353 if( iIdx==p->pTab->iPKey ){
90354 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
90355 }else if( iIdx>=pUnpack->nField ){
90359 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
90364 assert( p->op==SQLITE_UPDATE );
90365 if( !p->aNew ){
90366 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
90367 if( !p->aNew ){
90372 assert( iIdx>=0 && iIdx<p->pCsr->nField );
90373 pMem = &p->aNew[iIdx];
90374 if( pMem->flags==0 ){
90375 if( iIdx==p->pTab->iPKey ){
90376 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
90378 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
90403 VdbeOp *aOp = p->aOp;
90404 int nOp = p->nOp;
90408 if( p->pFrame ){
90410 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
90411 aOp = pFrame->aOp;
90412 nOp = pFrame->nOp;
90429 pScan = &p->aScan[idx];
90433 for(idx=0; idx<p->nScan; idx++){
90434 pScan = &p->aScan[idx];
90435 if( pScan->zName ){
90436 iScan--;
90441 if( idx>=p->nScan ) return 1;
90445 if( pScan->addrLoop>0 ){
90446 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
90448 *(sqlite3_int64*)pOut = -1;
90453 if( pScan->addrVisit>0 ){
90454 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
90456 *(sqlite3_int64*)pOut = -1;
90462 LogEst x = pScan->nEst;
90471 *(const char**)pOut = pScan->zName;
90475 if( pScan->addrExplain ){
90476 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
90483 if( pScan->addrExplain ){
90484 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
90486 *(int*)pOut = -1;
90491 if( pScan->addrExplain ){
90492 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
90494 *(int*)pOut = -1;
90500 if( pScan->aAddrRange[0]==0 ){
90501 res = -1;
90504 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
90505 int iIns = pScan->aAddrRange[ii];
90506 int iEnd = pScan->aAddrRange[ii+1];
90517 if( pOp->p1!=iEnd ) continue;
90518 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
90554 for(ii=0; ii<p->nOp; ii++){
90555 Op *pOp = &p->aOp[ii];
90556 pOp->nExec = 0;
90557 pOp->nCycle = 0;
90579 ** The Vdbe parse-tree explainer is also found here.
90587 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
90612 ** This function returns a pointer to a nul-terminated string in memory
90616 ** then the returned string holds a copy of zRawSql with "-- " prepended
90622 ** with large (multi-megabyte) strings and blobs.
90632 ** parameter index is known, locate the value in p->aVar[]. Then render
90651 db = p->db;
90652 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
90653 if( db->nVdbeExec>1 ){
90657 sqlite3_str_append(&out, "-- ", 3);
90658 assert( (zRawSql - zStart) > 0 );
90659 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
90661 }else if( p->nVar==0 ){
90690 assert( idx>0 && idx<=p->nVar );
90691 pVar = &p->aVar[idx-1];
90692 if( pVar->flags & MEM_Null ){
90694 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
90695 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
90696 }else if( pVar->flags & MEM_Real ){
90697 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
90698 }else if( pVar->flags & MEM_Str ){
90705 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
90713 nOut = pVar->n;
90717 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
90720 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
90722 if( nOut<pVar->n ){
90723 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90729 }else if( pVar->flags & MEM_Zero ){
90730 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
90733 assert( pVar->flags & MEM_Blob );
90735 nOut = pVar->n;
90740 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
90744 if( nOut<pVar->n ){
90745 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90834 ** use this information to make sure that the zero-blob functionality
90841 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
90842 sqlite3_max_blobsize = p->n;
90852 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
90854 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
90909 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
90910 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
90911 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
90913 ** In other words, if M is 2, then I is either 0 (for fall-through) or
90915 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
90926 ** always taken, the flags should be 0x05 since the fall-through and
90928 ** flags should be 0x06 since only the fall-through approach is allowed.
90931 ** interested in equal or not-equal. In other words, I==0 and I==2
90936 ** should be no-ops except for special builds used to measure test coverage.
90941 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
90945 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
90957 ** iSrcLine - the line number of the VdbeCoverage() macro, with
90959 ** I - Mask of bits 0x07 indicating which cases are are
90961 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
90966 ** M - Type of jump. Same as M argument above
90991 if( ((P)->flags&MEM_Ephem)!=0 \
90995 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
91022 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
91023 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
91025 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
91033 assert( iCur>=0 && iCur<p->nCursor );
91034 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
91035 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
91036 p->apCsr[iCur] = 0;
91041 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
91042 ** to hold cursors, it is faster to in-line the logic. */
91043 assert( pMem->flags==MEM_Undefined );
91044 assert( (pMem->flags & MEM_Dyn)==0 );
91045 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
91046 if( pMem->szMalloc<nByte ){
91047 if( pMem->szMalloc>0 ){
91048 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
91050 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
91051 if( pMem->zMalloc==0 ){
91052 pMem->szMalloc = 0;
91055 pMem->szMalloc = nByte;
91058 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
91060 pCx->eCurType = eCurType;
91061 pCx->nField = nField;
91062 pCx->aOffset = &pCx->aType[nField];
91064 pCx->uc.pCursor = (BtCursor*)
91065 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
91066 sqlite3BtreeCursorZero(pCx->uc.pCursor);
91084 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
91104 u8 enc = pRec->enc;
91106 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
91107 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
91109 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
91110 pRec->flags |= MEM_Int;
91112 pRec->u.r = rValue;
91113 pRec->flags |= MEM_Real;
91116 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
91119 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
91120 pRec->flags &= ~MEM_Str;
91130 ** floating-point representation if an integer representation
91144 ** No-op. pRec is unchanged.
91154 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
91155 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
91156 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
91167 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
91168 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
91169 testcase( pRec->flags & MEM_Int );
91170 testcase( pRec->flags & MEM_Real );
91171 testcase( pRec->flags & MEM_IntReal );
91175 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
91210 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
91216 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
91217 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
91219 pMem->u.i = 0;
91222 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
91224 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
91225 pMem->u.i = ix;
91230 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
91231 pMem->u.i = ix;
91241 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
91242 ** But it does set pMem->u.r and pMem->u.i appropriately.
91245 assert( (pMem->flags & MEM_Null)==0
91246 || pMem->db==0 || pMem->db->mallocFailed );
91247 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
91248 testcase( pMem->flags & MEM_Int );
91249 testcase( pMem->flags & MEM_Real );
91250 testcase( pMem->flags & MEM_IntReal );
91251 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
91253 assert( pMem->flags & (MEM_Str|MEM_Blob) );
91254 testcase( pMem->flags & MEM_Str );
91255 testcase( pMem->flags & MEM_Blob );
91266 int f = pMem->flags;
91284 for(i=0; i<25 && i<pMem->n; i++){
91285 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
91288 for(i=0; i<25 && i<pMem->n; i++){
91289 char z = pMem->z[i];
91294 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
91311 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
91312 for(j=0; j<25 && j<pMem->n; j++){
91313 c = pMem->z[j];
91316 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
91326 if( p->flags & MEM_Undefined ){
91328 }else if( p->flags & MEM_Null ){
91329 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
91330 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
91331 printf(" si:%lld", p->u.i);
91332 }else if( (p->flags & (MEM_IntReal))!=0 ){
91333 printf(" ir:%lld", p->u.i);
91334 }else if( p->flags & MEM_Int ){
91335 printf(" i:%lld", p->u.i);
91337 }else if( p->flags & MEM_Real ){
91338 printf(" r:%.17g", p->u.r);
91349 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
91354 if( p->pScopyFrom ){
91355 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
91374 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
91380 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
91389 ** the number of non-transaction savepoints currently in the
91399 for(p=db->pSavepoint; p; p=p->pNext) n++;
91400 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
91406 ** Return the register of pOp->p2 after first preparing it to be
91411 pOut->flags = MEM_Int;
91416 assert( pOp->p2>0 );
91417 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91418 pOut = &p->aMem[pOp->p2];
91420 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
91423 pOut->flags = MEM_Int;
91429 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
91430 ** with pOp->p3. Return the hash.
91436 assert( pOp->p4type==P4_INT32 );
91437 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
91439 if( p->flags & (MEM_Int|MEM_IntReal) ){
91440 h += p->u.i;
91441 }else if( p->flags & MEM_Real ){
91443 }else if( p->flags & (MEM_Str|MEM_Blob) ){
91447 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
91464 return azTypes[sqlite3_value_type(pMem)-1];
91474 Op *aOp = p->aOp; /* Copy of p->aOp */
91482 sqlite3 *db = p->db; /* The database */
91490 Mem *aMem = p->aMem; /* Copy of p->aMem */
91501 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
91502 if( DbMaskNonZero(p->lockMask) ){
91506 if( db->xProgress ){
91507 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
91508 assert( 0 < db->nProgressOps );
91509 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
91514 if( p->rc==SQLITE_NOMEM ){
91519 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
91520 testcase( p->rc!=SQLITE_OK );
91521 p->rc = SQLITE_OK;
91522 assert( p->bIsReader || p->readOnly!=0 );
91523 p->iCurrentTime = 0;
91524 assert( p->explain==0 );
91525 db->busyHandler.nBusy = 0;
91526 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91530 if( p->pc==0
91531 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
91536 if( p->db->flags & SQLITE_VdbeListing ){
91538 for(i=0; i<p->nOp; i++){
91542 if( p->db->flags & SQLITE_VdbeEQP ){
91543 for(i=0; i<p->nOp; i++){
91551 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
91555 for(pOp=&aOp[p->pc]; 1; pOp++){
91560 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
91564 pOp->nExec++;
91565 pnCycle = &pOp->nCycle;
91566 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
91569 pOp->nExec++;
91570 pnCycle = &pOp->nCycle;
91571 *pnCycle -= sqlite3Hwtime();
91578 if( db->flags & SQLITE_VdbeTrace ){
91579 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
91580 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
91590 sqlite3_interrupt_count--;
91600 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
91602 assert( pOp->p1>0 );
91603 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
91604 assert( memIsValid(&aMem[pOp->p1]) );
91605 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
91606 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
91609 assert( pOp->p2>0 );
91610 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91611 assert( memIsValid(&aMem[pOp->p2]) );
91612 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
91613 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
91616 assert( pOp->p3>0 );
91617 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91618 assert( memIsValid(&aMem[pOp->p3]) );
91619 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
91620 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
91623 assert( pOp->p2>0 );
91624 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91625 memAboutToChange(p, &aMem[pOp->p2]);
91628 assert( pOp->p3>0 );
91629 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91630 memAboutToChange(p, &aMem[pOp->p3]);
91638 switch( pOp->opcode ){
91645 ** the switch statement will break with convention and be flush-left. Another
91683 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
91693 if( pOp->p5 ){
91694 assert( pOp->p2 < (int)(pOp - aOp) );
91695 assert( pOp->p2 > 1 );
91696 pOp = &aOp[pOp->p2 - 2];
91703 pOp = &aOp[pOp->p2 - 1];
91714 ** faster according to "valgrind --tool=cachegrind" */
91716 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91721 ** If the progress callback returns non-zero, exit the virtual machine with
91724 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
91725 assert( db->nProgressOps!=0 );
91726 nProgressLimit += db->nProgressOps;
91727 if( db->xProgress(db->pProgressArg) ){
91744 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91745 pIn1 = &aMem[pOp->p1];
91748 pIn1->flags = MEM_Int;
91749 pIn1->u.i = (int)(pOp-aOp);
91750 REGISTER_TRACE(pOp->p1, pIn1);
91761 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
91768 ** P2 is not used by the byte-code engine. However, if P2 is positive
91773 ** value is a byte-code indentation hint. See tag-20220407a in
91777 pIn1 = &aMem[pOp->p1];
91778 if( pIn1->flags & MEM_Int ){
91779 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
91780 pOp = &aOp[pIn1->u.i];
91781 }else if( ALWAYS(pOp->p3) ){
91799 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91800 assert( pOp->p2>=0 && pOp->p2<p->nOp );
91801 assert( pOp->p3>=0 && pOp->p3<p->nOp );
91802 pOut = &aMem[pOp->p1];
91804 pOut->u.i = pOp->p3 - 1;
91805 pOut->flags = MEM_Int;
91806 if( pOp->p2==0 ) break;
91811 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
91812 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
91813 pOp = &aOp[pOp->p2 - 1];
91827 pIn1 = &aMem[pOp->p1];
91828 assert( pIn1->flags==MEM_Int );
91829 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
91830 pCaller = &aOp[pIn1->u.i];
91831 assert( pCaller->opcode==OP_Yield );
91832 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
91833 pOp = &aOp[pCaller->p2 - 1];
91834 pIn1->flags = MEM_Undefined;
91853 pIn1 = &aMem[pOp->p1];
91855 pIn1->flags = MEM_Int;
91856 pcDest = (int)pIn1->u.i;
91857 pIn1->u.i = (int)(pOp - aOp);
91858 REGISTER_TRACE(pOp->p1, pIn1);
91868 ** value in register P3 is not NULL, then this routine is a no-op.
91872 pIn3 = &aMem[pOp->p3];
91874 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
91876 if( (pIn3->flags & MEM_Null)==0 ) break;
91916 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
91922 assert( pOp->p1!=SQLITE_INTERNAL );
91924 if( p->pFrame && pOp->p1==SQLITE_OK ){
91925 /* Halt the sub-program. Return control to the parent frame. */
91926 pFrame = p->pFrame;
91927 p->pFrame = pFrame->pParent;
91928 p->nFrame--;
91929 sqlite3VdbeSetChanges(db, p->nChange);
91931 if( pOp->p2==OE_Ignore ){
91932 /* Instruction pcx is the OP_Program that invoked the sub-program
91934 ** instruction is set to OE_Ignore, then the sub-program is throwing
91937 pcx = p->aOp[pcx].p2-1;
91939 aOp = p->aOp;
91940 aMem = p->aMem;
91944 p->rc = pOp->p1;
91945 p->errorAction = (u8)pOp->p2;
91946 assert( pOp->p5<=4 );
91947 if( p->rc ){
91948 if( pOp->p5 ){
91951 testcase( pOp->p5==1 );
91952 testcase( pOp->p5==2 );
91953 testcase( pOp->p5==3 );
91954 testcase( pOp->p5==4 );
91955 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
91956 if( pOp->p4.z ){
91957 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
91960 sqlite3VdbeError(p, "%s", pOp->p4.z);
91962 pcx = (int)(pOp - aOp);
91963 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
91968 p->rc = SQLITE_BUSY;
91970 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
91971 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
91972 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
91980 ** The 32-bit integer value P1 is written into register P2.
91984 pOut->u.i = pOp->p1;
91991 ** P4 is a pointer to a 64-bit integer value.
91996 assert( pOp->p4.pI64!=0 );
91997 pOut->u.i = *pOp->p4.pI64;
92005 ** P4 is a pointer to a 64-bit floating point value.
92010 pOut->flags = MEM_Real;
92011 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
92012 pOut->u.r = *pOp->p4.pReal;
92020 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
92026 assert( pOp->p4.z!=0 );
92028 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
92032 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
92036 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
92038 pOut->szMalloc = 0;
92039 pOut->flags |= MEM_Static;
92040 if( pOp->p4type==P4_DYNAMIC ){
92041 sqlite3DbFree(db, pOp->p4.z);
92043 pOp->p4type = P4_DYNAMIC;
92044 pOp->p4.z = pOut->z;
92045 pOp->p1 = pOut->n;
92048 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
92051 pOp->opcode = OP_String;
92070 assert( pOp->p4.z!=0 );
92072 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
92073 pOut->z = pOp->p4.z;
92074 pOut->n = pOp->p1;
92075 pOut->enc = encoding;
92078 if( pOp->p3>0 ){
92079 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92080 pIn3 = &aMem[pOp->p3];
92081 assert( pIn3->flags & MEM_Int );
92082 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
92091 ** Mark the beginning of a subroutine that can be entered in-line
92095 ** If the subroutine is entered in-line, then the OP_Return will simply
92101 ** a no-op that simply falls through to the next instruction (assuming that
92103 ** entered in-line, then the OP_Return will cause in-line execution to
92118 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
92127 cnt = pOp->p3-pOp->p2;
92128 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92129 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
92130 pOut->n = 0;
92132 pOut->uTemp = 0;
92138 pOut->flags = nullFlag;
92139 pOut->n = 0;
92140 cnt--;
92154 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92155 pOut = &aMem[pOp->p1];
92156 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
92165 ** a zero-filled blob that is P1 bytes long in P2.
92168 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
92170 if( pOp->p4.z==0 ){
92171 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
92174 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
92176 pOut->enc = encoding;
92192 assert( pOp->p1>0 && pOp->p1<=p->nVar );
92193 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
92194 pVar = &p->aVar[pOp->p1 - 1];
92198 pOut = &aMem[pOp->p2];
92201 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
92202 pOut->flags |= MEM_Static|MEM_FromBind;
92210 ** Move the P3 values in register P1..P1+P3-1 over into
92211 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
92213 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
92221 n = pOp->p3;
92222 p1 = pOp->p1;
92223 p2 = pOp->p2;
92230 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
92231 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
92236 pIn1->pScopyFrom = 0;
92238 for(i=1; i<p->nMem; i++){
92249 }while( --n );
92269 n = pOp->p3;
92270 pIn1 = &aMem[pOp->p1];
92271 pOut = &aMem[pOp->p2];
92277 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
92278 pOut->flags &= ~MEM_Subtype;
92281 pOut->pScopyFrom = 0;
92283 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
92284 if( (n--)==0 ) break;
92305 pIn1 = &aMem[pOp->p1];
92306 pOut = &aMem[pOp->p2];
92310 pOut->pScopyFrom = pIn1;
92311 pOut->mScopyFlags = pIn1->flags;
92325 pIn1 = &aMem[pOp->p1];
92326 assert( (pIn1->flags & MEM_Int)!=0 );
92327 pOut = &aMem[pOp->p2];
92328 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
92336 ** constraint violations, this is a no-op.
92353 ** The registers P1 through P1+P2-1 contain a single row of
92356 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
92360 assert( p->nResColumn==pOp->p2 );
92361 assert( pOp->p1>0 || CORRUPT_DB );
92362 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
92364 p->cacheCtr = (p->cacheCtr + 2)|1;
92365 p->pResultRow = &aMem[pOp->p1];
92368 Mem *pMem = p->pResultRow;
92370 for(i=0; i<pOp->p2; i++){
92372 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
92382 if( db->mallocFailed ) goto no_mem;
92383 if( db->mTrace & SQLITE_TRACE_ROW ){
92384 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
92386 p->pc = (int)(pOp - aOp) + 1;
92409 pIn1 = &aMem[pOp->p1];
92410 pIn2 = &aMem[pOp->p2];
92411 pOut = &aMem[pOp->p3];
92414 flags1 = pIn1->flags;
92416 testcase( pIn2->flags & MEM_Null );
92417 if( (flags1 | pIn2->flags) & MEM_Null ){
92423 flags1 = pIn1->flags & ~MEM_Str;
92426 flags1 = pIn1->flags & ~MEM_Str;
92428 flags2 = pIn2->flags;
92431 flags2 = pIn2->flags & ~MEM_Str;
92434 flags2 = pIn2->flags & ~MEM_Str;
92436 nByte = pIn1->n + pIn2->n;
92437 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
92445 memcpy(pOut->z, pIn2->z, pIn2->n);
92446 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
92447 pIn2->flags = flags2;
92449 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
92450 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
92451 pIn1->flags = flags1;
92453 pOut->z[nByte]=0;
92454 pOut->z[nByte+1] = 0;
92455 pOut->flags |= MEM_Term;
92456 pOut->n = (int)nByte;
92457 pOut->enc = encoding;
92478 ** Synopsis: r[P3]=r[P2]-r[P1]
92512 pIn1 = &aMem[pOp->p1];
92513 type1 = pIn1->flags;
92514 pIn2 = &aMem[pOp->p2];
92515 type2 = pIn2->flags;
92516 pOut = &aMem[pOp->p3];
92519 iA = pIn1->u.i;
92520 iB = pIn2->u.i;
92521 switch( pOp->opcode ){
92527 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
92533 if( iA==-1 ) iA = 1;
92538 pOut->u.i = iB;
92549 switch( pOp->opcode ){
92551 case OP_Subtract: rB -= rA; break;
92563 if( iA==-1 ) iA = 1;
92569 pOut->u.i = rB;
92575 pOut->u.r = rB;
92590 ** be returned. This is used by the built-in min(), max() and nullif()
92599 ** publicly. Only built-in functions have access to this feature.
92602 assert( pOp->p4type==P4_COLLSEQ );
92603 if( pOp->p1 ){
92604 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
92612 ** Take the bit-wise AND of the values in register P1 and P2 and
92619 ** Take the bit-wise OR of the values in register P1 and P2 and
92648 pIn1 = &aMem[pOp->p1];
92649 pIn2 = &aMem[pOp->p2];
92650 pOut = &aMem[pOp->p3];
92651 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
92657 op = pOp->opcode;
92668 op = 2*OP_ShiftLeft + 1 - op;
92669 iB = iB>(-64) ? -iB : 64;
92673 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
92680 /* Sign-extend on a right shift of a negative number */
92681 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
92686 pOut->u.i = iA;
92700 pIn1 = &aMem[pOp->p1];
92703 pIn1->u.i += pOp->p2;
92715 pIn1 = &aMem[pOp->p1];
92716 if( (pIn1->flags & MEM_Int)==0 ){
92718 if( (pIn1->flags & MEM_Int)==0 ){
92720 if( pOp->p2==0 ){
92744 pIn1 = &aMem[pOp->p1];
92745 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
92746 testcase( pIn1->flags & MEM_Int );
92747 testcase( pIn1->flags & MEM_IntReal );
92749 REGISTER_TRACE(pOp->p1, pIn1);
92772 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
92773 testcase( pOp->p2==SQLITE_AFF_TEXT );
92774 testcase( pOp->p2==SQLITE_AFF_BLOB );
92775 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
92776 testcase( pOp->p2==SQLITE_AFF_INTEGER );
92777 testcase( pOp->p2==SQLITE_AFF_REAL );
92778 pIn1 = &aMem[pOp->p1];
92782 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
92785 REGISTER_TRACE(pOp->p1, pIn1);
92796 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
92840 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
92890 u16 flags1; /* Copy of initial value of pIn1->flags */
92891 u16 flags3; /* Copy of initial value of pIn3->flags */
92893 pIn1 = &aMem[pOp->p1];
92894 pIn3 = &aMem[pOp->p3];
92895 flags1 = pIn1->flags;
92896 flags3 = pIn3->flags;
92899 if( pIn3->u.i > pIn1->u.i ){
92900 if( sqlite3aGTb[pOp->opcode] ){
92901 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92906 }else if( pIn3->u.i < pIn1->u.i ){
92907 if( sqlite3aLTb[pOp->opcode] ){
92908 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92911 iCompare = -1;
92914 if( sqlite3aEQb[pOp->opcode] ){
92915 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92921 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92926 if( pOp->p5 & SQLITE_NULLEQ ){
92932 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
92933 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
92939 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
92947 if( pOp->p5 & SQLITE_JUMPIFNULL ){
92955 /* Neither operand is NULL and we couldn't do the special high-speed
92956 ** integer comparison case. So do a general-case comparison. */
92957 affinity = pOp->p5 & SQLITE_AFF_MASK;
92962 assert( flags3==pIn3->flags || CORRUPT_DB );
92963 flags3 = pIn3->flags;
92971 testcase( pIn1->flags & MEM_Int );
92972 testcase( pIn1->flags & MEM_Real );
92973 testcase( pIn1->flags & MEM_IntReal );
92975 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
92976 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
92980 testcase( pIn3->flags & MEM_Int );
92981 testcase( pIn3->flags & MEM_Real );
92982 testcase( pIn3->flags & MEM_IntReal );
92984 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
92985 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
92988 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
92989 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
93001 res2 = sqlite3aLTb[pOp->opcode];
93003 res2 = sqlite3aEQb[pOp->opcode];
93005 res2 = sqlite3aGTb[pOp->opcode];
93011 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
93012 pIn3->flags = flags3;
93013 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
93014 pIn1->flags = flags1;
93016 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93038 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
93041 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
93066 assert( pOp->p4type==P4_INTARRAY );
93067 assert( pOp->p4.ai );
93074 ** Synopsis: r[P1@P3] <-> r[P2@P3]
93076 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
93077 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
93106 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
93110 assert( pOp[-1].opcode==OP_Permutation );
93111 assert( pOp[-1].p4type==P4_INTARRAY );
93112 aPermute = pOp[-1].p4.ai + 1;
93115 n = pOp->p3;
93116 pKeyInfo = pOp->p4.pKeyInfo;
93119 p1 = pOp->p1;
93120 p2 = pOp->p2;
93125 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
93126 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
93128 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
93129 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
93138 assert( i<pKeyInfo->nKeyField );
93139 pColl = pKeyInfo->aColl[i];
93140 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
93144 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
93147 iCompare = -iCompare;
93149 if( bRev ) iCompare = -iCompare;
93166 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
93169 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
93171 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
93173 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
93203 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
93204 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
93205 if( pOp->opcode==OP_And ){
93212 pOut = &aMem[pOp->p3];
93216 pOut->u.i = v1;
93243 assert( pOp->p4type==P4_INT32 );
93244 assert( pOp->p4.i==0 || pOp->p4.i==1 );
93245 assert( pOp->p3==0 || pOp->p3==1 );
93246 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
93247 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
93259 pIn1 = &aMem[pOp->p1];
93260 pOut = &aMem[pOp->p2];
93261 if( (pIn1->flags & MEM_Null)==0 ){
93273 ** ones-complement of the P1 value into register P2. If P1 holds
93277 pIn1 = &aMem[pOp->p1];
93278 pOut = &aMem[pOp->p2];
93280 if( (pIn1->flags & MEM_Null)==0 ){
93281 pOut->flags = MEM_Int;
93282 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
93290 ** encountered on each invocation of the byte-code program. Jump to P2
93293 ** Top-level programs determine first invocation by comparing the P1
93301 ** because the self-altering code trick does not work for recursive
93306 assert( p->aOp[0].opcode==OP_Init );
93307 if( p->pFrame ){
93308 iAddr = (int)(pOp - p->aOp);
93309 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
93313 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
93315 if( p->aOp[0].p1==pOp->p1 ){
93321 pOp->p1 = p->aOp[0].p1;
93328 ** is considered true if it is numeric and non-zero. If the value
93329 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
93333 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
93343 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
93347 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
93359 pIn1 = &aMem[pOp->p1];
93360 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
93361 if( (pIn1->flags & MEM_Null)!=0 ){
93380 ** If P1 is -1, then P3 is a register number and the datatype is taken
93403 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
93404 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
93405 if( pOp->p1>=0 ){
93406 pC = p->apCsr[pOp->p1];
93408 assert( pOp->p3>=0 );
93409 if( pOp->p3<pC->nHdrParsed ){
93410 serialType = pC->aType[pOp->p3];
93437 typeMask = 1 << (pOp->p4.i - 1);
93445 assert( memIsValid(&aMem[pOp->p3]) );
93446 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
93453 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
93454 if( typeMask & pOp->p5 ){
93468 if( (aMem[pOp->p1].flags & MEM_Null)!=0
93469 || (aMem[pOp->p3].flags & MEM_Null)!=0
93471 sqlite3VdbeMemSetNull(aMem + pOp->p2);
93473 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
93484 pIn1 = &aMem[pOp->p1];
93485 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
93486 if( (pIn1->flags & MEM_Null)==0 ){
93500 ** If P1 is not an open cursor, then this opcode is a no-op.
93504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93505 pC = p->apCsr[pOp->p1];
93506 if( pC && pC->nullRow ){
93507 sqlite3VdbeMemSetNull(aMem + pOp->p3);
93527 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
93531 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93532 pC = p->apCsr[pOp->p1];
93533 pOut = &p->aMem[pOp->p3];
93534 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
93537 if( pC->deferredMoveto ){
93541 if( sqlite3BtreeEof(pC->uc.pCursor) ){
93544 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
93556 ** information about the format of the data.) Extract the P2-th column
93576 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
93577 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
93585 u64 offset64; /* 64-bit offset */
93589 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93590 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93591 pC = p->apCsr[pOp->p1];
93592 p2 = (u32)pOp->p2;
93596 assert( p2<(u32)pC->nField
93597 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
93598 aOffset = pC->aOffset;
93599 assert( aOffset==pC->aType+pC->nField );
93600 assert( pC->eCurType!=CURTYPE_VTAB );
93601 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
93602 assert( pC->eCurType!=CURTYPE_SORTER );
93604 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
93605 if( pC->nullRow ){
93606 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
93607 /* For the special case of as pseudo-cursor, the seekResult field
93609 pReg = &aMem[pC->seekResult];
93610 assert( pReg->flags & MEM_Blob );
93612 pC->payloadSize = pC->szRow = pReg->n;
93613 pC->aRow = (u8*)pReg->z;
93615 pDest = &aMem[pOp->p3];
93621 pCrsr = pC->uc.pCursor;
93622 if( pC->deferredMoveto ){
93624 assert( !pC->isEphemeral );
93625 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
93626 pC = pC->pAltCursor;
93627 p2 = iMap - 1;
93637 assert( pC->eCurType==CURTYPE_BTREE );
93640 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
93641 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
93642 assert( pC->szRow<=pC->payloadSize );
93643 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
93645 pC->cacheStatus = p->cacheCtr;
93646 if( (aOffset[0] = pC->aRow[0])<0x80 ){
93647 pC->iHdrOffset = 1;
93649 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
93651 pC->nHdrParsed = 0;
93653 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
93654 /* pC->aRow does not have to hold the entire row, but it does at least
93655 ** need to cover the header of the record. If pC->aRow does not contain
93658 pC->aRow = 0;
93659 pC->szRow = 0;
93667 ** 3-byte type for each of the maximum of 32768 columns plus three
93670 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
93675 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
93687 zData = pC->aRow;
93688 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
93692 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
93699 ** parsed and valid information is in aOffset[] and pC->aType[].
93701 if( pC->nHdrParsed<=p2 ){
93703 ** to extract additional fields up through the p2+1-th field
93705 if( pC->iHdrOffset<aOffset[0] ){
93707 if( pC->aRow==0 ){
93709 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
93713 zData = pC->aRow;
93716 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
93718 i = pC->nHdrParsed;
93720 zHdr = zData + pC->iHdrOffset;
93724 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
93729 pC->aType[i] = t;
93740 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
93741 || (offset64 > pC->payloadSize)
93747 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93752 pC->nHdrParsed = i;
93753 pC->iHdrOffset = (u32)(zHdr - zData);
93754 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93763 if( pC->nHdrParsed<=p2 ){
93764 pDest = &aMem[pOp->p3];
93766 if( pOp->p4type==P4_MEM ){
93767 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
93774 t = pC->aType[p2];
93777 /* Extract the content for the p2+1-th column. Control can only
93778 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
93781 assert( p2<pC->nHdrParsed );
93783 pDest = &aMem[pOp->p3];
93789 assert( t==pC->aType[p2] );
93790 if( pC->szRow>=aOffset[p2+1] ){
93792 ** page - where the content is not on an overflow page */
93793 zData = pC->aRow + aOffset[p2];
93798 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
93802 pDest->n = len = (t-12)/2;
93803 pDest->enc = encoding;
93804 if( pDest->szMalloc < len+2 ){
93805 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
93806 pDest->flags = MEM_Null;
93809 pDest->z = pDest->zMalloc;
93811 memcpy(pDest->z, zData, len);
93812 pDest->z[len] = 0;
93813 pDest->z[len+1] = 0;
93814 pDest->flags = aFlag[t&1];
93817 pDest->enc = encoding;
93819 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
93820 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
93838 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
93839 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
93841 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
93842 pDest->flags &= ~MEM_Ephem;
93848 REGISTER_TRACE(pOp->p3, pDest);
93853 pOp = &aOp[aOp[0].p3-1];
93873 ** is zero. When P3 is non-zero, no type checking occurs for
93880 ** <li> P2 should be the number of non-virtual columns in the
93892 assert( pOp->p4type==P4_TABLE );
93893 pTab = pOp->p4.pTab;
93894 assert( pTab->tabFlags & TF_Strict );
93895 assert( pTab->nNVCol==pOp->p2 );
93896 aCol = pTab->aCol;
93897 pIn1 = &aMem[pOp->p1];
93898 for(i=0; i<pTab->nCol; i++){
93901 if( pOp->p3 ){ pIn1++; continue; }
93903 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
93905 if( (pIn1->flags & MEM_Null)==0 ){
93908 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
93913 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
93917 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
93921 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
93922 assert( (pIn1->flags & MEM_IntReal)==0 );
93923 if( pIn1->flags & MEM_Int ){
93926 ** so that we keep the high-resolution integer value but know that
93928 testcase( pIn1->u.i==140737488355328LL );
93929 testcase( pIn1->u.i==140737488355327LL );
93930 testcase( pIn1->u.i==-140737488355328LL );
93931 testcase( pIn1->u.i==-140737488355329LL );
93932 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
93933 pIn1->flags |= MEM_IntReal;
93934 pIn1->flags &= ~MEM_Int;
93936 pIn1->u.r = (double)pIn1->u.i;
93937 pIn1->flags |= MEM_Real;
93938 pIn1->flags &= ~MEM_Int;
93940 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
93951 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
93954 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
93959 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
93960 pTab->zName, aCol[i].zCnName);
93970 ** P4 is a string that is P2 characters long. The N-th character of the
93971 ** string indicates the column affinity that should be used for the N-th
93977 zAffinity = pOp->p4.z;
93979 assert( pOp->p2>0 );
93980 assert( zAffinity[pOp->p2]==0 );
93981 pIn1 = &aMem[pOp->p1];
93982 while( 1 /*exit-by-break*/ ){
93983 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
93986 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
93989 ** so that we keep the high-resolution integer value but know that
93991 testcase( pIn1->u.i==140737488355328LL );
93992 testcase( pIn1->u.i==140737488355327LL );
93993 testcase( pIn1->u.i==-140737488355328LL );
93994 testcase( pIn1->u.i==-140737488355329LL );
93995 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
93996 pIn1->flags |= MEM_IntReal;
93997 pIn1->flags &= ~MEM_Int;
93999 pIn1->u.r = (double)pIn1->u.i;
94000 pIn1->flags |= MEM_Real;
94001 pIn1->flags &= ~(MEM_Int|MEM_Str);
94004 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
94019 ** P4 may be a string that is P2 characters long. The N-th character of the
94020 ** string indicates the column affinity that should be used for the N-th
94029 ** compile-time option is enabled:
94032 ** of the right-most table that can be null-trimmed.
94036 ** accept no-change records with serial_type 10. This value is
94058 ** ------------------------------------------------------------------------
94059 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
94060 ** ------------------------------------------------------------------------
94067 ** hdr-size field is also a varint which is the offset from the beginning
94073 nField = pOp->p1;
94074 zAffinity = pOp->p4.z;
94075 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
94077 nField = pOp->p2;
94078 pLast = &pData0[nField-1];
94081 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
94082 pOut = &aMem[pOp->p3];
94092 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
94093 pRec->flags |= MEM_IntReal;
94094 pRec->flags &= ~(MEM_Int);
94096 REGISTER_TRACE((int)(pRec-aMem), pRec);
94106 ** have a non-NULL default value. Also, the record must be left with
94108 ** index of the right-most column with a non-NULL default value */
94109 if( pOp->p5 ){
94110 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
94111 pLast--;
94112 nField--;
94119 ** the Mem.uTemp field of each term should hold the serial-type that will
94123 ** --------------- ---------------
94125 ** 1 1-byte signed integer
94126 ** 2 2-byte signed integer
94127 ** 3 3-byte signed integer
94128 ** 4 4-byte signed integer
94129 ** 5 6-byte signed integer
94130 ** 6 8-byte signed integer
94146 if( pRec->flags & MEM_Null ){
94147 if( pRec->flags & MEM_Zero ){
94151 ** Give such values a special internal-use-only serial-type of 10
94155 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
94157 pRec->uTemp = 10;
94159 pRec->uTemp = 0;
94162 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
94164 i64 i = pRec->u.i;
94166 testcase( pRec->flags & MEM_Int );
94167 testcase( pRec->flags & MEM_IntReal );
94180 if( (i&1)==i && p->minWriteFileFormat>=4 ){
94181 pRec->uTemp = 8+(u32)uu;
94184 pRec->uTemp = 1;
94188 pRec->uTemp = 2;
94191 pRec->uTemp = 3;
94194 pRec->uTemp = 4;
94197 pRec->uTemp = 5;
94200 if( pRec->flags & MEM_IntReal ){
94202 ** as an integer, then we might as well make it an 8-byte floating
94204 pRec->u.r = (double)pRec->u.i;
94205 pRec->flags &= ~MEM_IntReal;
94206 pRec->flags |= MEM_Real;
94207 pRec->uTemp = 7;
94209 pRec->uTemp = 6;
94212 }else if( pRec->flags & MEM_Real ){
94215 pRec->uTemp = 7;
94217 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
94218 assert( pRec->n>=0 );
94219 len = (u32)pRec->n;
94220 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
94221 if( pRec->flags & MEM_Zero ){
94222 serial_type += pRec->u.nZero*2;
94225 len += pRec->u.nZero;
94227 nZero += pRec->u.nZero;
94232 pRec->uTemp = serial_type;
94235 pRec--;
94238 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
94256 ** the new record. The output register (pOp->p3) is not allowed to
94260 if( nByte+nZero<=pOut->szMalloc ){
94263 pOut->z = pOut->zMalloc;
94267 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94274 pOut->n = (int)nByte;
94275 pOut->flags = MEM_Blob;
94277 pOut->u.nZero = nZero;
94278 pOut->flags |= MEM_Zero;
94281 zHdr = (u8 *)pOut->z;
94292 while( 1 /*exit-by-break*/ ){
94293 serial_type = pRec->uTemp;
94294 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
94296 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
94306 assert( sizeof(v)==sizeof(pRec->u.r) );
94307 memcpy(&v, &pRec->u.r, sizeof(v));
94310 v = pRec->u.i;
94314 while( 1 /*exit-by-break*/ ){
94315 zPayload[--i] = (u8)(v&0xFF);
94323 if( serial_type>=14 && pRec->n>0 ){
94324 assert( pRec->z!=0 );
94325 memcpy(zPayload, pRec->z, pRec->n);
94326 zPayload += pRec->n;
94330 if( pRec->n ){
94331 assert( pRec->z!=0 );
94332 memcpy(zPayload, pRec->z, pRec->n);
94333 zPayload += pRec->n;
94339 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
94340 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
94342 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
94343 REGISTER_TRACE(pOp->p3, pOut);
94354 ** every btree page of the table. But if P3 is non-zero, an estimate
94361 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
94362 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
94364 if( pOp->p3 ){
94372 pOut->u.i = nEntry;
94393 p1 = pOp->p1;
94394 zName = pOp->p4.z;
94399 assert( db->pSavepoint==0 || db->autoCommit==0 );
94401 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
94403 assert( p->bIsReader );
94406 if( db->nVdbeWrite>0 ){
94410 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
94419 ** that the db->aVTrans[] array is empty. */
94420 assert( db->autoCommit==0 || db->nVTrans==0 );
94422 db->nStatement+db->nSavepoint);
94429 pNew->zName = (char *)&pNew[1];
94430 memcpy(pNew->zName, zName, nName+1);
94434 if( db->autoCommit ){
94435 db->autoCommit = 0;
94436 db->isTransactionSavepoint = 1;
94438 db->nSavepoint++;
94442 pNew->pNext = db->pSavepoint;
94443 db->pSavepoint = pNew;
94444 pNew->nDeferredCons = db->nDeferredCons;
94445 pNew->nDeferredImmCons = db->nDeferredImmCons;
94455 pSavepoint = db->pSavepoint;
94456 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
94457 pSavepoint = pSavepoint->pNext
94464 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
94468 sqlite3VdbeError(p, "cannot release savepoint - "
94477 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
94482 db->autoCommit = 1;
94484 p->pc = (int)(pOp - aOp);
94485 db->autoCommit = 0;
94486 p->rc = rc = SQLITE_BUSY;
94489 rc = p->rc;
94491 db->autoCommit = 0;
94493 db->isTransactionSavepoint = 0;
94497 iSavepoint = db->nSavepoint - iSavepoint - 1;
94499 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
94500 for(ii=0; ii<db->nDb; ii++){
94501 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
94510 for(ii=0; ii<db->nDb; ii++){
94511 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
94519 db->mDbFlags |= DBFLAG_SchemaChange;
94526 while( db->pSavepoint!=pSavepoint ){
94527 pTmp = db->pSavepoint;
94528 db->pSavepoint = pTmp->pNext;
94530 db->nSavepoint--;
94538 assert( pSavepoint==db->pSavepoint );
94539 db->pSavepoint = pSavepoint->pNext;
94542 db->nSavepoint--;
94546 db->nDeferredCons = pSavepoint->nDeferredCons;
94547 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
94557 if( p->eVdbeState==VDBE_HALT_STATE ){
94566 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
94577 desiredAutoCommit = pOp->p1;
94578 iRollback = pOp->p2;
94581 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
94582 assert( p->bIsReader );
94584 if( desiredAutoCommit!=db->autoCommit ){
94588 db->autoCommit = 1;
94589 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
94593 sqlite3VdbeError(p, "cannot commit transaction - "
94600 db->autoCommit = (u8)desiredAutoCommit;
94603 p->pc = (int)(pOp - aOp);
94604 db->autoCommit = (u8)(1-desiredAutoCommit);
94605 p->rc = rc = SQLITE_BUSY;
94609 if( p->rc==SQLITE_OK ){
94618 (iRollback)?"cannot rollback - no transaction is active":
94619 "cannot commit - no transaction is active"));
94631 ** If P2 is non-zero, then a write-transaction is started, or if a
94632 ** read-transaction is already active, it is upgraded to a write-transaction.
94633 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
94641 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
94667 assert( p->bIsReader );
94668 assert( p->readOnly==0 || pOp->p2==0 );
94669 assert( pOp->p2>=0 && pOp->p2<=2 );
94670 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94671 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94673 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
94674 if( db->flags & SQLITE_QueryOnly ){
94684 pDb = &db->aDb[pOp->p1];
94685 pBt = pDb->pBt;
94688 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
94693 p->pc = (int)(pOp - aOp);
94694 p->rc = rc;
94700 if( p->usesStmtJournal
94701 && pOp->p2
94702 && (db->autoCommit==0 || db->nVdbeRead>1)
94705 if( p->iStatement==0 ){
94706 assert( db->nStatement>=0 && db->nSavepoint>=0 );
94707 db->nStatement++;
94708 p->iStatement = db->nSavepoint + db->nStatement;
94711 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
94713 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
94719 p->nStmtDefCons = db->nDeferredCons;
94720 p->nStmtDefImmCons = db->nDeferredImmCons;
94723 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
94725 && pOp->p5
94726 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
94729 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
94733 sqlite3DbFree(db, p->zErrMsg);
94734 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
94735 /* If the schema-cookie from the database file matches the cookie
94736 ** stored with the in-memory representation of the schema, do
94739 ** If virtual-tables are in use, this is not just an optimization.
94740 ** Often, v-tables store their data in other SQLite tables, which
94741 ** are queried from within xNext() and other v-table methods using
94742 ** prepared queries. If such a query is out-of-date, we do not want to
94744 ** v-table would have to be ready for the sqlite3_vtab structure itself
94746 ** a v-table method.
94748 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
94749 sqlite3ResetOneSchema(db, pOp->p1);
94751 p->expired = 1;
94757 p->changeCntOn = 0;
94771 ** There must be a read-lock on the database (either a transaction
94780 assert( p->bIsReader );
94781 iDb = pOp->p1;
94782 iCookie = pOp->p3;
94783 assert( pOp->p3<SQLITE_N_BTREE_META );
94784 assert( iDb>=0 && iDb<db->nDb );
94785 assert( db->aDb[iDb].pBt!=0 );
94786 assert( DbMaskTest(p->btreeMask, iDb) );
94788 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
94790 pOut->u.i = iMeta;
94805 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
94813 assert( pOp->p2<SQLITE_N_BTREE_META );
94814 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94815 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94816 assert( p->readOnly==0 );
94817 pDb = &db->aDb[pOp->p1];
94818 assert( pDb->pBt!=0 );
94819 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
94821 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
94822 if( pOp->p2==BTREE_SCHEMA_VERSION ){
94824 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
94825 db->mDbFlags |= DBFLAG_SchemaChange;
94826 sqlite3FkClearTriggerCache(db, pOp->p1);
94827 }else if( pOp->p2==BTREE_FILE_FORMAT ){
94829 pDb->pSchema->file_format = pOp->p3;
94831 if( pOp->p1==1 ){
94835 p->expired = 0;
94844 ** Open a read-only cursor for the database table whose root page is
94861 ** object, then table being opened must be an [index b-tree] where the
94863 ** sequence of that index b-tree. Otherwise, if P4 is an integer
94864 ** value, then the table being opened must be a [table b-tree] with a
94874 ** b-tree and if it is this opcode becomes a no-op. In other words,
94896 ** OPFLAG_P2ISREG bit is set in P5 - see below).
94900 ** object, then table being opened must be an [index b-tree] where the
94902 ** sequence of that index b-tree. Otherwise, if P4 is an integer
94903 ** value, then the table being opened must be a [table b-tree] with a
94935 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
94936 assert( pOp->p4type==P4_KEYINFO );
94937 pCur = p->apCsr[pOp->p1];
94938 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
94939 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
94940 assert( pCur->eCurType==CURTYPE_BTREE );
94941 sqlite3BtreeClearCursor(pCur->uc.pCursor);
94949 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
94950 assert( p->bIsReader );
94951 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
94952 || p->readOnly==0 );
94954 if( p->expired==1 ){
94961 p2 = (u32)pOp->p2;
94962 iDb = pOp->p3;
94963 assert( iDb>=0 && iDb<db->nDb );
94964 assert( DbMaskTest(p->btreeMask, iDb) );
94965 pDb = &db->aDb[iDb];
94966 pX = pDb->pBt;
94968 if( pOp->opcode==OP_OpenWrite ){
94970 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
94972 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
94973 p->minWriteFileFormat = pDb->pSchema->file_format;
94978 if( pOp->p5 & OPFLAG_P2ISREG ){
94980 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
94981 assert( pOp->opcode==OP_OpenWrite );
94984 assert( (pIn2->flags & MEM_Int)!=0 );
94986 p2 = (int)pIn2->u.i;
94993 if( pOp->p4type==P4_KEYINFO ){
94994 pKeyInfo = pOp->p4.pKeyInfo;
94995 assert( pKeyInfo->enc==ENC(db) );
94996 assert( pKeyInfo->db==db );
94997 nField = pKeyInfo->nAllField;
94998 }else if( pOp->p4type==P4_INT32 ){
94999 nField = pOp->p4.i;
95001 assert( pOp->p1>=0 );
95004 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
95006 pCur->iDb = iDb;
95007 pCur->nullRow = 1;
95008 pCur->isOrdered = 1;
95009 pCur->pgnoRoot = p2;
95011 pCur->wrFlag = wrFlag;
95013 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
95014 pCur->pKeyInfo = pKeyInfo;
95016 ** SQLite used to check if the root-page flags were sane at this point
95019 pCur->isTable = pOp->p4type!=P4_KEYINFO;
95024 testcase( pOp->p5 & OPFLAG_BULKCSR );
95025 testcase( pOp->p2 & OPFLAG_SEEKEQ );
95026 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
95027 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
95038 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
95044 pOrig = p->apCsr[pOp->p2];
95046 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
95048 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
95050 pCx->nullRow = 1;
95051 pCx->isEphemeral = 1;
95052 pCx->pKeyInfo = pOrig->pKeyInfo;
95053 pCx->isTable = pOrig->isTable;
95054 pCx->pgnoRoot = pOrig->pgnoRoot;
95055 pCx->isOrdered = pOrig->isOrdered;
95056 pCx->ub.pBtx = pOrig->ub.pBtx;
95057 pCx->noReuse = 1;
95058 pOrig->noReuse = 1;
95059 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
95060 pCx->pKeyInfo, pCx->uc.pCursor);
95074 ** the main database is read-only. The ephemeral
95091 ** can be used as zero-length data for OP_Insert. This is an optimization
95113 assert( pOp->p1>=0 );
95114 assert( pOp->p2>=0 );
95115 if( pOp->p3>0 ){
95118 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
95119 assert( pOp->opcode==OP_OpenEphemeral );
95120 assert( aMem[pOp->p3].flags & MEM_Null );
95121 aMem[pOp->p3].n = 0;
95122 aMem[pOp->p3].z = "";
95124 pCx = p->apCsr[pOp->p1];
95125 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
95129 assert( pCx->isEphemeral );
95130 pCx->seqCount = 0;
95131 pCx->cacheStatus = CACHE_STALE;
95132 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
95134 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
95136 pCx->isEphemeral = 1;
95137 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
95138 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
95141 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
95146 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
95148 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
95149 assert( pOp->p4type==P4_KEYINFO );
95150 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
95151 BTREE_BLOBKEY | pOp->p5);
95153 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
95154 assert( pKeyInfo->db==db );
95155 assert( pKeyInfo->enc==ENC(db) );
95156 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
95157 pKeyInfo, pCx->uc.pCursor);
95159 pCx->isTable = 0;
95161 pCx->pgnoRoot = SCHEMA_ROOT;
95162 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
95163 0, pCx->uc.pCursor);
95164 pCx->isTable = 1;
95167 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
95169 sqlite3BtreeClose(pCx->ub.pBtx);
95174 pCx->nullRow = 1;
95182 ** tables using an external merge-sort algorithm.
95184 ** If argument P3 is non-zero, then it indicates that the sorter may
95191 assert( pOp->p1>=0 );
95192 assert( pOp->p2>=0 );
95193 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
95195 pCx->pKeyInfo = pOp->p4.pKeyInfo;
95196 assert( pCx->pKeyInfo->db==db );
95197 assert( pCx->pKeyInfo->enc==ENC(db) );
95198 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
95212 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95213 pC = p->apCsr[pOp->p1];
95215 if( (pC->seqCount++)==0 ){
95229 ** A pseudo-table created by this opcode is used to hold a single
95232 ** is the only cursor opcode that works with a pseudo-table.
95235 ** the pseudo-table.
95240 assert( pOp->p1>=0 );
95241 assert( pOp->p3>=0 );
95242 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
95244 pCx->nullRow = 1;
95245 pCx->seekResult = pOp->p2;
95246 pCx->isTable = 1;
95247 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
95249 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
95251 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
95252 assert( pOp->p5==0 );
95259 ** currently open, this instruction is a no-op.
95262 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95263 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
95264 p->apCsr[pOp->p1] = 0;
95273 ** table or index for cursor P1 are used. P4 is a 64-bit integer
95276 ** by the cursor. The high-order bit is set if any column after
95281 pC = p->apCsr[pOp->p1];
95282 assert( pC->eCurType==CURTYPE_BTREE );
95283 pC->maskUsed = *(u64*)pOp->p4.pI64;
95291 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95318 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95336 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95354 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95390 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95391 assert( pOp->p2!=0 );
95392 pC = p->apCsr[pOp->p1];
95394 assert( pC->eCurType==CURTYPE_BTREE );
95398 assert( pC->isOrdered );
95399 assert( pC->uc.pCursor!=0 );
95400 oc = pOp->opcode;
95402 pC->nullRow = 0;
95404 pC->seekOp = pOp->opcode;
95407 pC->deferredMoveto = 0;
95408 pC->cacheStatus = CACHE_STALE;
95409 if( pC->isTable ){
95412 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
95418 pIn3 = &aMem[pOp->p3];
95419 flags3 = pIn3->flags;
95424 newType = pIn3->flags; /* Record the type after applying numeric affinity */
95425 pIn3->flags = flags3; /* But convert the type back to its original */
95436 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
95441 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
95447 ** (x > 4.9) -> (x >= 5)
95448 ** (x <= 4.9) -> (x < 5)
95451 assert( OP_SeekGE==(OP_SeekGT-1) );
95452 assert( OP_SeekLT==(OP_SeekLE-1) );
95454 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
95466 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
95467 pC->movetoTarget = iKey; /* Used by OP_Delete */
95477 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
95479 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
95481 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
95482 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
95489 nField = pOp->p4.i;
95490 assert( pOp->p4type==P4_INT32 );
95492 r.pKeyInfo = pC->pKeyInfo;
95497 ** r.default_rc = -1;
95502 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
95503 assert( oc!=OP_SeekGT || r.default_rc==-1 );
95504 assert( oc!=OP_SeekLE || r.default_rc==-1 );
95508 r.aMem = &aMem[pOp->p3];
95514 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
95519 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
95534 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95550 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
95563 res = sqlite3BtreeEof(pC->uc.pCursor);
95567 assert( pOp->p2>0 );
95580 ** Synopsis: Scan-ahead up to P1 rows
95592 ** This opcode helps to optimize IN operators on a multi-column index
95595 ** of the b-tree instead. A correct answer is obtained if this opcode
95596 ** is omitted or is a no-op.
95603 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
95615 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
95655 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
95658 assert( pOp->p2>=(int)(pOp-aOp)+2 );
95660 if( pOp->p5==0 ){
95662 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
95663 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
95664 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
95665 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
95666 || aOp[pOp->p2-1].opcode==OP_IdxGE );
95667 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
95670 assert( pOp->p2==(int)(pOp-aOp)+2 );
95671 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
95675 assert( pOp->p1>0 );
95676 pC = p->apCsr[pOp[1].p1];
95678 assert( pC->eCurType==CURTYPE_BTREE );
95679 assert( !pC->isTable );
95680 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
95682 if( db->flags&SQLITE_VdbeTrace ){
95683 printf("... cursor not valid - fall through\n");
95688 nStep = pOp->p1;
95690 r.pKeyInfo = pC->pKeyInfo;
95707 if( res>0 && pOp->p5==0 ){
95711 if( db->flags&SQLITE_VdbeTrace ){
95712 printf("... %d steps and then skip\n", pOp->p1 - nStep);
95722 if( db->flags&SQLITE_VdbeTrace ){
95723 printf("... %d steps and then success\n", pOp->p1 - nStep);
95732 if( db->flags&SQLITE_VdbeTrace ){
95733 printf("... fall through after %d steps\n", pOp->p1);
95739 nStep--;
95740 pC->cacheStatus = CACHE_STALE;
95741 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95766 ** early, thus saving work. This is part of the IN-early-out optimization.
95768 ** P1 must be a valid b-tree cursor.
95772 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95773 pC = p->apCsr[pOp->p1];
95775 assert( pOp->p3>=pOp->p2 );
95776 if( pC->seekHit<pOp->p2 ){
95778 if( db->flags&SQLITE_VdbeTrace ){
95779 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
95782 pC->seekHit = pOp->p2;
95783 }else if( pC->seekHit>pOp->p3 ){
95785 if( db->flags&SQLITE_VdbeTrace ){
95786 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
95789 pC->seekHit = pOp->p3;
95803 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95804 pCur = p->apCsr[pOp->p1];
95805 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
95806 if( pCur==0 || pCur->nullRow ){
95867 ** This opcode is used in IN clause processing for a multi-column key.
95869 ** left-most element, and if there are no matches on the most recent
95888 ** record are not-NULL then a check is done to determine if any row in the
95904 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95905 pC = p->apCsr[pOp->p1];
95908 if( db->flags&SQLITE_VdbeTrace ){
95909 printf("seekHit is %d\n", pC->seekHit);
95912 if( pC->seekHit>=pOp->p4.i ) break;
95926 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
95929 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95930 assert( pOp->p4type==P4_INT32 );
95931 pC = p->apCsr[pOp->p1];
95934 pC->seekOp = pOp->opcode;
95936 r.aMem = &aMem[pOp->p3];
95937 assert( pC->eCurType==CURTYPE_BTREE );
95938 assert( pC->uc.pCursor!=0 );
95939 assert( pC->isTable==0 );
95940 r.nField = (u16)pOp->p4.i;
95943 r.pKeyInfo = pC->pKeyInfo;
95949 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
95952 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
95955 assert( r.aMem->flags & MEM_Blob );
95956 assert( pOp->opcode!=OP_NoConflict );
95960 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
95962 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
95963 pIdxKey->default_rc = 0;
95964 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
95970 alreadyExists = (pC->seekResult==0);
95971 pC->nullRow = 1-alreadyExists;
95972 pC->deferredMoveto = 0;
95973 pC->cacheStatus = CACHE_STALE;
95974 if( pOp->opcode==OP_Found ){
95982 if( pOp->opcode==OP_NoConflict ){
95994 if( pOp->opcode==OP_IfNoHope ){
95995 pC->seekHit = pOp->p4.i;
96017 ** (with arbitrary multi-value keys).
96036 ** P3 register to contain a non-integer value, in which case the jump is
96040 ** (with arbitrary multi-value keys).
96054 pIn3 = &aMem[pOp->p3];
96055 testcase( pIn3->flags & MEM_Int );
96056 testcase( pIn3->flags & MEM_IntReal );
96057 testcase( pIn3->flags & MEM_Real );
96058 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
96059 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
96060 /* If pIn3->u.i does not contain an integer, compute iKey as the
96074 pIn3 = &aMem[pOp->p3];
96075 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
96076 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96077 iKey = pIn3->u.i;
96079 pC = p->apCsr[pOp->p1];
96082 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
96084 assert( pC->isTable );
96085 assert( pC->eCurType==CURTYPE_BTREE );
96086 pCrsr = pC->uc.pCursor;
96091 pC->movetoTarget = iKey; /* Used by OP_Delete */
96092 pC->nullRow = 0;
96093 pC->cacheStatus = CACHE_STALE;
96094 pC->deferredMoveto = 0;
96096 pC->seekResult = res;
96099 if( pOp->p2==0 ){
96118 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96119 assert( p->apCsr[pOp->p1]!=0 );
96120 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
96122 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
96155 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96156 pC = p->apCsr[pOp->p1];
96158 assert( pC->isTable );
96159 assert( pC->eCurType==CURTYPE_BTREE );
96160 assert( pC->uc.pCursor!=0 );
96163 ** thing) is obtained in a two-step algorithm.
96175 assert( pC->isTable );
96187 if( !pC->useRandomRowid ){
96188 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
96193 v = 1; /* IMP: R-61914-48074 */
96195 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
96196 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96198 pC->useRandomRowid = 1;
96200 v++; /* IMP: R-29538-34987 */
96206 if( pOp->p3 ){
96208 assert( pOp->p3>0 );
96209 if( p->pFrame ){
96210 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
96212 assert( pOp->p3<=pFrame->nMem );
96213 pMem = &pFrame->aMem[pOp->p3];
96216 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
96217 pMem = &aMem[pOp->p3];
96222 REGISTER_TRACE(pOp->p3, pMem);
96224 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
96225 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
96226 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
96229 if( v<pMem->u.i+1 ){
96230 v = pMem->u.i + 1;
96232 pMem->u.i = v;
96235 if( pC->useRandomRowid ){
96236 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
96240 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
96246 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
96252 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
96255 assert( v>0 ); /* EV: R-40812-03570 */
96257 pC->deferredMoveto = 0;
96258 pC->cacheStatus = CACHE_STALE;
96260 pOut->u.i = v;
96289 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
96292 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
96293 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
96306 const char *zDb; /* database name - used by the update hook */
96307 Table *pTab; /* Table structure - used by update and pre-update hooks */
96310 pData = &aMem[pOp->p2];
96311 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96313 pC = p->apCsr[pOp->p1];
96315 assert( pC->eCurType==CURTYPE_BTREE );
96316 assert( pC->deferredMoveto==0 );
96317 assert( pC->uc.pCursor!=0 );
96318 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
96319 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
96320 REGISTER_TRACE(pOp->p2, pData);
96323 pKey = &aMem[pOp->p3];
96324 assert( pKey->flags & MEM_Int );
96326 REGISTER_TRACE(pOp->p3, pKey);
96327 x.nKey = pKey->u.i;
96329 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
96330 assert( pC->iDb>=0 );
96331 zDb = db->aDb[pC->iDb].zDbSName;
96332 pTab = pOp->p4.pTab;
96333 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
96340 /* Invoke the pre-update hook, if any */
96342 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
96343 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
96345 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
96346 /* Prevent post-update hook from running in cases when it should not */
96350 if( pOp->p5 & OPFLAG_ISNOOP ) break;
96353 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
96354 if( pOp->p5 & OPFLAG_NCHANGE ){
96355 p->nChange++;
96356 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
96358 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
96359 x.pData = pData->z;
96360 x.nData = pData->n;
96361 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
96362 if( pData->flags & MEM_Zero ){
96363 x.nZero = pData->u.nZero;
96369 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
96370 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
96373 pC->deferredMoveto = 0;
96374 pC->cacheStatus = CACHE_STALE;
96376 /* Invoke the update-hook if required. */
96379 assert( db->xUpdateCallback!=0 );
96380 assert( pTab->aCol!=0 );
96381 db->xUpdateCallback(db->pUpdateArg,
96382 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
96383 zDb, pTab->zName, x.nKey);
96391 ** of table - intkey or index. This opcode is used as part of copying
96404 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
96405 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
96407 pDest = p->apCsr[pOp->p1];
96408 pSrc = p->apCsr[pOp->p2];
96409 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
96410 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
96422 ** the next Next instruction will be a no-op. As a result, in this case
96436 ** P1 must not be pseudo-table. It has to be a real table with
96440 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
96442 ** this case. Specifically, if one is configured, the pre-update hook is
96443 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
96456 opflags = pOp->p2;
96457 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96458 pC = p->apCsr[pOp->p1];
96460 assert( pC->eCurType==CURTYPE_BTREE );
96461 assert( pC->uc.pCursor!=0 );
96462 assert( pC->deferredMoveto==0 );
96466 if( pOp->p4type==P4_TABLE
96467 && HasRowid(pOp->p4.pTab)
96468 && pOp->p5==0
96469 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
96472 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
96474 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96475 assert( CORRUPT_DB || pC->movetoTarget==iKey );
96479 /* If the update-hook or pre-update-hook will be invoked, set zDb to
96484 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
96485 assert( pC->iDb>=0 );
96486 assert( pOp->p4.pTab!=0 );
96487 zDb = db->aDb[pC->iDb].zDbSName;
96488 pTab = pOp->p4.pTab;
96489 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
96490 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96498 /* Invoke the pre-update-hook if required. */
96499 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
96500 if( db->xPreUpdateCallback && pTab ){
96503 || (aMem[pOp->p3].flags & MEM_Int)
96507 zDb, pTab, pC->movetoTarget,
96508 pOp->p3, -1
96515 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
96520 if( p->pFrame==0 ){
96521 if( pC->isEphemeral==0
96522 && (pOp->p5 & OPFLAG_AUXDELETE)==0
96523 && (pC->wrFlag & OPFLAG_FORDELETE)==0
96527 if( pOp->p2 & OPFLAG_NCHANGE ){
96528 nExtraDelete--;
96533 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
96534 pC->cacheStatus = CACHE_STALE;
96535 pC->seekResult = 0;
96538 /* Invoke the update-hook if required. */
96540 p->nChange++;
96541 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
96542 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
96543 pC->movetoTarget);
96544 assert( pC->iDb>=0 );
96558 sqlite3VdbeSetChanges(db, p->nChange);
96559 p->nChange = 0;
96583 pC = p->apCsr[pOp->p1];
96585 assert( pOp->p4type==P4_INT32 );
96586 pIn3 = &aMem[pOp->p3];
96587 nKeyCol = pOp->p4.i;
96603 ** a register that is the source for a pseudo-table cursor created using
96604 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
96611 pOut = &aMem[pOp->p2];
96612 pC = p->apCsr[pOp->p1];
96615 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
96616 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96618 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
96635 ** of a real table, not a pseudo-table.
96639 ** register will be invalidated as soon as the cursor moves - including
96657 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96658 pC = p->apCsr[pOp->p1];
96660 assert( pC->eCurType==CURTYPE_BTREE );
96662 assert( pC->nullRow==0 );
96663 assert( pC->uc.pCursor!=0 );
96664 pCrsr = pC->uc.pCursor;
96674 assert( pC->deferredMoveto==0 );
96678 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
96684 if( !pOp->p3 ) Deephemeralize(pOut);
96686 REGISTER_TRACE(pOp->p2, pOut);
96707 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96708 pC = p->apCsr[pOp->p1];
96710 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
96711 if( pC->nullRow ){
96712 pOut->flags = MEM_Null;
96714 }else if( pC->deferredMoveto ){
96715 v = pC->movetoTarget;
96717 }else if( pC->eCurType==CURTYPE_VTAB ){
96718 assert( pC->uc.pVCur!=0 );
96719 pVtab = pC->uc.pVCur->pVtab;
96720 pModule = pVtab->pModule;
96721 assert( pModule->xRowid );
96722 rc = pModule->xRowid(pC->uc.pVCur, &v);
96727 assert( pC->eCurType==CURTYPE_BTREE );
96728 assert( pC->uc.pCursor!=0 );
96731 if( pC->nullRow ){
96732 pOut->flags = MEM_Null;
96735 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96737 pOut->u.i = v;
96748 ** pseudo-cursor that always returns NULL for every column.
96753 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96754 pC = p->apCsr[pOp->p1];
96757 ** pseudo-cursor that always gives null rows. */
96758 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
96760 pC->seekResult = 0;
96761 pC->isTable = 1;
96762 pC->noReuse = 1;
96763 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
96765 pC->nullRow = 1;
96766 pC->cacheStatus = CACHE_STALE;
96767 if( pC->eCurType==CURTYPE_BTREE ){
96768 assert( pC->uc.pCursor!=0 );
96769 sqlite3BtreeClearCursor(pC->uc.pCursor);
96772 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
96805 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96806 pC = p->apCsr[pOp->p1];
96808 assert( pC->eCurType==CURTYPE_BTREE );
96809 pCrsr = pC->uc.pCursor;
96813 pC->seekOp = pOp->opcode;
96815 if( pOp->opcode==OP_SeekEnd ){
96816 assert( pOp->p2==0 );
96817 pC->seekResult = -1;
96823 pC->nullRow = (u8)res;
96824 pC->deferredMoveto = 0;
96825 pC->cacheStatus = CACHE_STALE;
96827 if( pOp->p2>0 ){
96845 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96846 pC = p->apCsr[pOp->p1];
96848 pCrsr = pC->uc.pCursor;
96854 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
96887 sqlite3_search_count--;
96889 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
96913 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96914 assert( pOp->p5==0 );
96915 assert( pOp->p2>=0 && pOp->p2<p->nOp );
96917 pC = p->apCsr[pOp->p1];
96919 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
96922 pC->seekOp = OP_Rewind;
96927 assert( pC->eCurType==CURTYPE_BTREE );
96928 pCrsr = pC->uc.pCursor;
96931 pC->deferredMoveto = 0;
96932 pC->cacheStatus = CACHE_STALE;
96935 pC->nullRow = (u8)res;
96936 if( pOp->p2>0 ){
96954 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
96963 ** number P5-1 in the prepared statement is incremented.
96979 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
96988 ** number P5-1 in the prepared statement is incremented.
97000 pC = p->apCsr[pOp->p1];
97006 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97007 assert( pOp->p5==0
97008 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
97009 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
97010 pC = p->apCsr[pOp->p1];
97012 assert( pC->deferredMoveto==0 );
97013 assert( pC->eCurType==CURTYPE_BTREE );
97014 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
97015 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
97016 || pC->seekOp==OP_NullRow);
97017 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
97021 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97022 assert( pOp->p5==0
97023 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
97024 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
97025 pC = p->apCsr[pOp->p1];
97027 assert( pC->deferredMoveto==0 );
97028 assert( pC->eCurType==CURTYPE_BTREE );
97029 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
97030 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
97031 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
97032 || pC->seekOp==OP_IfNoHope);
97033 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
97036 pC->cacheStatus = CACHE_STALE;
97039 pC->nullRow = 0;
97040 p->aCounter[pOp->p5]++;
97048 pC->nullRow = 1;
97064 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
97084 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97085 pC = p->apCsr[pOp->p1];
97089 pIn2 = &aMem[pOp->p2];
97090 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
97091 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
97092 assert( pC->eCurType==CURTYPE_BTREE );
97093 assert( pC->isTable==0 );
97096 x.nKey = pIn2->n;
97097 x.pKey = pIn2->z;
97098 x.aMem = aMem + pOp->p3;
97099 x.nMem = (u16)pOp->p4.i;
97100 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
97101 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
97102 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
97104 assert( pC->deferredMoveto==0 );
97105 pC->cacheStatus = CACHE_STALE;
97120 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97121 pC = p->apCsr[pOp->p1];
97125 pIn2 = &aMem[pOp->p2];
97126 assert( pIn2->flags & MEM_Blob );
97127 assert( pC->isTable==0 );
97148 ** this (self-correcting and non-critical) error if in writable_schema mode.
97156 assert( pOp->p3>0 );
97157 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
97158 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97159 pC = p->apCsr[pOp->p1];
97161 assert( pC->eCurType==CURTYPE_BTREE );
97163 pCrsr = pC->uc.pCursor;
97165 r.pKeyInfo = pC->pKeyInfo;
97166 r.nField = (u16)pOp->p3;
97168 r.aMem = &aMem[pOp->p2];
97174 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
97178 assert( pC->deferredMoveto==0 );
97179 pC->cacheStatus = CACHE_STALE;
97180 pC->seekResult = 0;
97197 ** is non-zero, then reading column a(i)-1 from cursor P3 is
97218 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97219 pC = p->apCsr[pOp->p1];
97221 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
97222 assert( pC->uc.pCursor!=0 );
97223 assert( pC->isTable==0 || IsNullCursor(pC) );
97224 assert( pC->deferredMoveto==0 );
97225 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
97236 if( !pC->nullRow ){
97238 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
97242 if( pOp->opcode==OP_DeferredSeek ){
97243 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
97244 pTabCur = p->apCsr[pOp->p3];
97246 assert( pTabCur->eCurType==CURTYPE_BTREE );
97247 assert( pTabCur->uc.pCursor!=0 );
97248 assert( pTabCur->isTable );
97249 pTabCur->nullRow = 0;
97250 pTabCur->movetoTarget = rowid;
97251 pTabCur->deferredMoveto = 1;
97252 pTabCur->cacheStatus = CACHE_STALE;
97253 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
97254 assert( !pTabCur->isEphemeral );
97255 pTabCur->ub.aAltMap = pOp->p4.ai;
97256 assert( !pC->isEphemeral );
97257 pTabCur->pAltCursor = pC;
97260 pOut->u.i = rowid;
97263 assert( pOp->opcode==OP_IdxRowid );
97264 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
97273 ** already occurred, this instruction is a no-op.
97278 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97279 pC = p->apCsr[pOp->p1];
97280 if( pC->deferredMoveto ){
97339 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97340 pC = p->apCsr[pOp->p1];
97342 assert( pC->isOrdered );
97343 assert( pC->eCurType==CURTYPE_BTREE );
97344 assert( pC->uc.pCursor!=0);
97345 assert( pC->deferredMoveto==0 );
97346 assert( pOp->p4type==P4_INT32 );
97347 r.pKeyInfo = pC->pKeyInfo;
97348 r.nField = (u16)pOp->p4.i;
97349 if( pOp->opcode<OP_IdxLT ){
97350 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
97351 r.default_rc = -1;
97353 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
97356 r.aMem = &aMem[pOp->p3];
97362 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
97373 assert( pC->eCurType==CURTYPE_BTREE );
97374 pCur = pC->uc.pCursor;
97392 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
97393 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
97394 res = -res;
97396 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
97417 ** value of the root page that moved - its value before the move occurred -
97428 ** and non-autovacuum modes.
97437 assert( p->readOnly==0 );
97438 assert( pOp->p1>1 );
97440 pOut->flags = MEM_Null;
97441 if( db->nVdbeRead > db->nVDestroy+1 ){
97443 p->errorAction = OE_Abort;
97446 iDb = pOp->p3;
97447 assert( DbMaskTest(p->btreeMask, iDb) );
97449 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
97450 pOut->flags = MEM_Int;
97451 pOut->u.i = iMoved;
97455 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
97475 ** If the P3 value is non-zero, then the row change count is incremented
97487 assert( p->readOnly==0 );
97488 assert( DbMaskTest(p->btreeMask, pOp->p2) );
97489 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
97490 if( pOp->p3 ){
97491 p->nChange += nChange;
97492 if( pOp->p3>0 ){
97493 assert( memIsValid(&aMem[pOp->p3]) );
97494 memAboutToChange(p, &aMem[pOp->p3]);
97495 aMem[pOp->p3].u.i += nChange;
97513 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97514 pC = p->apCsr[pOp->p1];
97517 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
97519 assert( pC->eCurType==CURTYPE_BTREE );
97520 assert( pC->isEphemeral );
97521 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
97530 ** Allocate a new b-tree in the main database file if P1==0 or in the
97534 ** The root page number of the new b-tree is stored in register P2.
97543 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
97544 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97545 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97546 assert( p->readOnly==0 );
97547 pDb = &db->aDb[pOp->p1];
97548 assert( pDb->pBt!=0 );
97549 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
97551 pOut->u.i = pgno;
97561 db->nSqlExec++;
97562 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
97563 db->nSqlExec--;
97575 ** then runs the new virtual machine. It is thus a re-entrant opcode.
97588 for(iDb=0; iDb<db->nDb; iDb++){
97589 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
97593 iDb = pOp->p1;
97594 assert( iDb>=0 && iDb<db->nDb );
97596 || db->mallocFailed
97597 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
97600 if( pOp->p4.z==0 ){
97601 sqlite3SchemaClear(db->aDb[iDb].pSchema);
97602 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
97603 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
97604 db->mDbFlags |= DBFLAG_SchemaChange;
97605 p->expired = 0;
97612 initData.pzErrMsg = &p->zErrMsg;
97614 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
97617 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
97621 assert( db->init.busy==0 );
97622 db->init.busy = 1;
97625 assert( !db->mallocFailed );
97629 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
97635 db->init.busy = 0;
97656 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97657 rc = sqlite3AnalysisLoad(db, pOp->p1);
97665 ** Remove the internal (in-memory) data structures that describe
97673 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
97679 ** Remove the internal (in-memory) data structures that describe
97687 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
97693 ** Remove the internal (in-memory) data structures that describe
97701 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
97733 assert( p->bIsReader );
97734 nRoot = pOp->p2;
97735 aRoot = pOp->p4.ai;
97738 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
97739 pnErr = &aMem[pOp->p3];
97740 assert( (pnErr->flags & MEM_Int)!=0 );
97741 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
97742 pIn1 = &aMem[pOp->p1];
97743 assert( pOp->p5<db->nDb );
97744 assert( DbMaskTest(p->btreeMask, pOp->p5) );
97745 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
97746 (int)pnErr->u.i+1, &nErr, &z);
97754 pnErr->u.i -= nErr-1;
97755 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
97772 pIn1 = &aMem[pOp->p1];
97773 pIn2 = &aMem[pOp->p2];
97774 assert( (pIn2->flags & MEM_Int)!=0 );
97775 if( (pIn1->flags & MEM_Blob)==0 ){
97779 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
97794 pIn1 = &aMem[pOp->p1];
97795 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
97796 if( (pIn1->flags & MEM_Blob)==0
97797 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
97806 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
97814 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
97823 ** must have P4==0, the final set must have P4==-1, and for all other sets
97828 ** (b) when P4==-1 there is no need to insert the value, as it will
97838 pIn1 = &aMem[pOp->p1];
97839 pIn3 = &aMem[pOp->p3];
97840 iSet = pOp->p4.i;
97841 assert( pIn3->flags&MEM_Int );
97846 if( (pIn1->flags & MEM_Blob)==0 ){
97850 assert( pOp->p4type==P4_INT32 );
97851 assert( iSet==-1 || iSet>=0 );
97853 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
97858 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
97871 ** cell in an array of values used as arguments to the sub-program. P2
97872 ** contains the address to jump to if the sub-program throws an IGNORE
97875 ** memory required by the sub-vdbe at runtime.
97879 ** If P5 is non-zero, then recursive program invocation is enabled.
97882 int nMem; /* Number of memory registers for sub-program */
97883 int nByte; /* Bytes of runtime space required for sub-program */
97888 SubProgram *pProgram; /* Sub-program to execute */
97891 pProgram = pOp->p4.pProgram;
97892 pRt = &aMem[pOp->p3];
97893 assert( pProgram->nOp>0 );
97896 ** disabled for backwards compatibility (p5 is set if this sub-program
97906 if( pOp->p5 ){
97907 t = pProgram->token;
97908 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
97912 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
97922 if( (pRt->flags&MEM_Blob)==0 ){
97928 nMem = pProgram->nMem + pProgram->nCsr;
97930 if( pProgram->nCsr==0 ) nMem++;
97933 + pProgram->nCsr * sizeof(VdbeCursor*)
97934 + (pProgram->nOp + 7)/8;
97940 pRt->flags = MEM_Blob|MEM_Dyn;
97941 pRt->z = (char*)pFrame;
97942 pRt->n = nByte;
97943 pRt->xDel = sqlite3VdbeFrameMemDel;
97945 pFrame->v = p;
97946 pFrame->nChildMem = nMem;
97947 pFrame->nChildCsr = pProgram->nCsr;
97948 pFrame->pc = (int)(pOp - aOp);
97949 pFrame->aMem = p->aMem;
97950 pFrame->nMem = p->nMem;
97951 pFrame->apCsr = p->apCsr;
97952 pFrame->nCursor = p->nCursor;
97953 pFrame->aOp = p->aOp;
97954 pFrame->nOp = p->nOp;
97955 pFrame->token = pProgram->token;
97957 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
97960 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
97962 pMem->flags = MEM_Undefined;
97963 pMem->db = db;
97966 pFrame = (VdbeFrame*)pRt->z;
97967 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
97968 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
97969 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
97970 assert( pProgram->nCsr==pFrame->nChildCsr );
97971 assert( (int)(pOp - aOp)==pFrame->pc );
97974 p->nFrame++;
97975 pFrame->pParent = p->pFrame;
97976 pFrame->lastRowid = db->lastRowid;
97977 pFrame->nChange = p->nChange;
97978 pFrame->nDbChange = p->db->nChange;
97979 assert( pFrame->pAuxData==0 );
97980 pFrame->pAuxData = p->pAuxData;
97981 p->pAuxData = 0;
97982 p->nChange = 0;
97983 p->pFrame = pFrame;
97984 p->aMem = aMem = VdbeFrameMem(pFrame);
97985 p->nMem = pFrame->nChildMem;
97986 p->nCursor = (u16)pFrame->nChildCsr;
97987 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
97988 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
97989 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
97990 p->aOp = aOp = pProgram->aOp;
97991 p->nOp = pProgram->nOp;
97997 for(i=0; i<p->nMem; i++){
97998 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
98003 pOp = &aOp[-1];
98009 ** This opcode is only ever present in sub-programs called via the
98023 pFrame = p->pFrame;
98024 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
98036 ** If P1 is non-zero, the database constraint counter is incremented
98041 if( db->flags & SQLITE_DeferFKs ){
98042 db->nDeferredImmCons += pOp->p2;
98043 }else if( pOp->p1 ){
98044 db->nDeferredCons += pOp->p2;
98046 p->nFkConstraint += pOp->p2;
98054 ** This opcode tests if a foreign key constraint-counter is currently zero.
98058 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
98060 ** zero, the jump is taken if the statement constraint-counter is zero
98064 if( pOp->p1 ){
98065 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
98066 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
98068 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
98069 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
98081 ** within a sub-program). Set the value of register P1 to the maximum of
98089 if( p->pFrame ){
98090 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
98091 pIn1 = &pFrame->aMem[pOp->p1];
98093 pIn1 = &aMem[pOp->p1];
98097 pIn2 = &aMem[pOp->p2];
98099 if( pIn1->u.i<pIn2->u.i){
98100 pIn1->u.i = pIn2->u.i;
98107 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
98117 pIn1 = &aMem[pOp->p1];
98118 assert( pIn1->flags&MEM_Int );
98119 VdbeBranchTaken( pIn1->u.i>0, 2);
98120 if( pIn1->u.i>0 ){
98121 pIn1->u.i -= pOp->p3;
98128 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
98141 ** and r[P2] is set to -1.
98147 pIn1 = &aMem[pOp->p1];
98148 pIn3 = &aMem[pOp->p3];
98150 assert( pIn1->flags & MEM_Int );
98151 assert( pIn3->flags & MEM_Int );
98152 x = pIn1->u.i;
98153 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
98161 pOut->u.i = -1;
98163 pOut->u.i = x;
98169 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
98173 ** If it is non-zero (negative or positive) and then also jump to P2.
98177 pIn1 = &aMem[pOp->p1];
98178 assert( pIn1->flags&MEM_Int );
98179 VdbeBranchTaken(pIn1->u.i<0, 2);
98180 if( pIn1->u.i ){
98181 if( pIn1->u.i>0 ) pIn1->u.i--;
98188 ** Synopsis: if (--r[P1])==0 goto P2
98194 pIn1 = &aMem[pOp->p1];
98195 assert( pIn1->flags&MEM_Int );
98196 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
98197 VdbeBranchTaken(pIn1->u.i==0, 2);
98198 if( pIn1->u.i==0 ) goto jump_to_p2;
98247 assert( pOp->p4type==P4_FUNCDEF );
98248 n = pOp->p5;
98249 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98250 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
98251 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
98253 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
98255 pCtx->pMem = 0;
98256 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
98257 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
98258 pCtx->pFunc = pOp->p4.pFunc;
98259 pCtx->iOp = (int)(pOp - aOp);
98260 pCtx->pVdbe = p;
98261 pCtx->skipFlag = 0;
98262 pCtx->isError = 0;
98263 pCtx->enc = encoding;
98264 pCtx->argc = n;
98265 pOp->p4type = P4_FUNCCTX;
98266 pOp->p4.pCtx = pCtx;
98269 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
98271 pOp->opcode = OP_AggStep1;
98280 assert( pOp->p4type==P4_FUNCCTX );
98281 pCtx = pOp->p4.pCtx;
98282 pMem = &aMem[pOp->p3];
98285 if( pOp->p1 ){
98288 assert( pMem->uTemp==0x1122e0e3 );
98291 pMem->uTemp = 0x1122e0e3;
98299 if( pCtx->pMem != pMem ){
98300 pCtx->pMem = pMem;
98301 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
98305 for(i=0; i<pCtx->argc; i++){
98306 assert( memIsValid(pCtx->argv[i]) );
98307 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
98311 pMem->n++;
98312 assert( pCtx->pOut->flags==MEM_Null );
98313 assert( pCtx->isError==0 );
98314 assert( pCtx->skipFlag==0 );
98316 if( pOp->p1 ){
98317 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
98320 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
98322 if( pCtx->isError ){
98323 if( pCtx->isError>0 ){
98324 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
98325 rc = pCtx->isError;
98327 if( pCtx->skipFlag ){
98328 assert( pOp[-1].opcode==OP_CollSeq );
98329 i = pOp[-1].p1;
98331 pCtx->skipFlag = 0;
98333 sqlite3VdbeMemRelease(pCtx->pOut);
98334 pCtx->pOut->flags = MEM_Null;
98335 pCtx->isError = 0;
98338 assert( pCtx->pOut->flags==MEM_Null );
98339 assert( pCtx->skipFlag==0 );
98372 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
98373 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
98374 pMem = &aMem[pOp->p1];
98375 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
98377 if( pOp->p3 ){
98378 memAboutToChange(p, &aMem[pOp->p3]);
98379 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
98380 pMem = &aMem[pOp->p3];
98384 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
98393 REGISTER_TRACE((int)(pMem-aMem), pMem);
98400 ** Checkpoint database P1. This is a no-op if P1 is not currently in
98407 ** mem[P3+2] are initialized to -1.
98414 assert( p->readOnly==0 );
98416 aRes[1] = aRes[2] = -1;
98417 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
98418 || pOp->p2==SQLITE_CHECKPOINT_FULL
98419 || pOp->p2==SQLITE_CHECKPOINT_RESTART
98420 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
98422 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
98428 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
98445 ** Write a string containing the final journal-mode to register P2.
98457 eNew = pOp->p3;
98466 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98467 assert( p->readOnly==0 );
98469 pBt = db->aDb[pOp->p1].pBt;
98484 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
98492 if( !db->autoCommit || db->nVdbeRead>1 ){
98503 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
98531 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
98532 pOut->z = (char *)sqlite3JournalModename(eNew);
98533 pOut->n = sqlite3Strlen30(pOut->z);
98534 pOut->enc = SQLITE_UTF8;
98552 assert( p->readOnly==0 );
98553 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
98554 pOp->p2 ? &aMem[pOp->p2] : 0);
98570 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98571 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98572 assert( p->readOnly==0 );
98573 pBt = db->aDb[pOp->p1].pBt;
98592 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
98602 assert( pOp->p2==0 || pOp->p2==1 );
98603 if( !pOp->p1 ){
98604 sqlite3ExpirePreparedStatements(db, pOp->p2);
98606 p->expired = pOp->p2+1;
98618 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98619 pC = p->apCsr[pOp->p1];
98621 assert( pC->eCurType==CURTYPE_BTREE );
98622 sqlite3BtreeCursorPin(pC->uc.pCursor);
98633 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98634 pC = p->apCsr[pOp->p1];
98636 assert( pC->eCurType==CURTYPE_BTREE );
98637 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
98646 ** the shared-cache feature is enabled.
98652 ** P2 contains the root-page of the table to lock.
98658 u8 isWriteLock = (u8)pOp->p3;
98659 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
98660 int p1 = pOp->p1;
98661 assert( p1>=0 && p1<db->nDb );
98662 assert( DbMaskTest(p->btreeMask, p1) );
98664 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
98667 const char *z = pOp->p4.z;
98689 pVTab = pOp->p4.pVtab;
98691 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
98711 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
98712 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
98713 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
98716 assert( zTab || db->mallocFailed );
98718 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
98733 db->nVDestroy++;
98734 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
98735 db->nVDestroy--;
98736 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
98755 assert( p->bIsReader );
98758 pVtab = pOp->p4.pVtab->pVtab;
98759 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
98763 pModule = pVtab->pModule;
98764 rc = pModule->xOpen(pVtab, &pVCur);
98769 pVCur->pVtab = pVtab;
98772 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
98774 pCur->uc.pVCur = pVCur;
98775 pVtab->nRef++;
98777 assert( db->mallocFailed );
98778 pModule->xClose(pVCur);
98800 pC = p->apCsr[pOp->p1];
98803 pRhs->pCsr = pC->uc.pCursor;
98804 pRhs->pOut = &aMem[pOp->p3];
98806 pOut->flags = MEM_Null;
98846 pQuery = &aMem[pOp->p3];
98848 pCur = p->apCsr[pOp->p1];
98850 REGISTER_TRACE(pOp->p3, pQuery);
98852 assert( pCur->eCurType==CURTYPE_VTAB );
98853 pVCur = pCur->uc.pVCur;
98854 pVtab = pVCur->pVtab;
98855 pModule = pVtab->pModule;
98858 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
98859 nArg = (int)pArgc->u.i;
98860 iQuery = (int)pQuery->u.i;
98863 apArg = p->apArg;
98867 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
98870 res = pModule->xEof(pVCur);
98871 pCur->nullRow = 0;
98882 ** Store in register P3 the value of the P2-th column of
98883 ** the current row of the virtual-table of cursor P1.
98899 VdbeCursor *pCur = p->apCsr[pOp->p1];
98901 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98902 pDest = &aMem[pOp->p3];
98904 if( pCur->nullRow ){
98908 assert( pCur->eCurType==CURTYPE_VTAB );
98909 pVtab = pCur->uc.pVCur->pVtab;
98910 pModule = pVtab->pModule;
98911 assert( pModule->xColumn );
98915 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
98916 if( pOp->p5 & OPFLAG_NOCHNG ){
98918 pDest->flags = MEM_Null|MEM_Zero;
98919 pDest->u.nZero = 0;
98923 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
98930 REGISTER_TRACE(pOp->p3, pDest);
98951 pCur = p->apCsr[pOp->p1];
98953 assert( pCur->eCurType==CURTYPE_VTAB );
98954 if( pCur->nullRow ){
98957 pVtab = pCur->uc.pVCur->pVtab;
98958 pModule = pVtab->pModule;
98959 assert( pModule->xNext );
98967 rc = pModule->xNext(pCur->uc.pVCur);
98970 res = pModule->xEof(pCur->uc.pVCur);
98992 isLegacy = (db->flags & SQLITE_LegacyAlter);
98993 db->flags |= SQLITE_LegacyAlter;
98994 pVtab = pOp->p4.pVtab->pVtab;
98995 pName = &aMem[pOp->p1];
98996 assert( pVtab->pModule->xRename );
98998 assert( p->readOnly==0 );
98999 REGISTER_TRACE(pOp->p1, pName);
99000 assert( pName->flags & MEM_Str );
99001 testcase( pName->enc==SQLITE_UTF8 );
99002 testcase( pName->enc==SQLITE_UTF16BE );
99003 testcase( pName->enc==SQLITE_UTF16LE );
99006 rc = pVtab->pModule->xRename(pVtab, pName->z);
99007 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
99009 p->expired = 0;
99022 ** invocation. The value in register (P3+P2-1) corresponds to the
99052 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
99053 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
99055 assert( p->readOnly==0 );
99056 if( db->mallocFailed ) goto no_mem;
99058 pVtab = pOp->p4.pVtab->pVtab;
99059 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
99063 pModule = pVtab->pModule;
99064 nArg = pOp->p2;
99065 assert( pOp->p4type==P4_VTAB );
99066 if( ALWAYS(pModule->xUpdate) ){
99067 u8 vtabOnConflict = db->vtabOnConflict;
99068 apArg = p->apArg;
99069 pX = &aMem[pOp->p3];
99076 db->vtabOnConflict = pOp->p5;
99077 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
99078 db->vtabOnConflict = vtabOnConflict;
99080 if( rc==SQLITE_OK && pOp->p1 ){
99081 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
99082 db->lastRowid = rowid;
99084 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
99085 if( pOp->p5==OE_Ignore ){
99088 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
99091 p->nChange++;
99106 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
99126 pBt = db->aDb[pOp->p1].pBt;
99128 if( pOp->p3 ){
99130 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
99132 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
99147 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
99166 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
99175 ** purely non-deterministic. Some built-in date/time functions can be
99176 ** either determinitic of non-deterministic, depending on their arguments.
99177 ** When those function are used in a non-deterministic way, they will check
99188 assert( pOp->p4type==P4_FUNCCTX );
99189 pCtx = pOp->p4.pCtx;
99195 pOut = &aMem[pOp->p3];
99196 if( pCtx->pOut != pOut ){
99197 pCtx->pVdbe = p;
99198 pCtx->pOut = pOut;
99199 pCtx->enc = encoding;
99200 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
99202 assert( pCtx->pVdbe==p );
99206 for(i=0; i<pCtx->argc; i++){
99207 assert( memIsValid(pCtx->argv[i]) );
99208 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
99212 assert( pCtx->isError==0 );
99213 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
99216 if( pCtx->isError ){
99217 if( pCtx->isError>0 ){
99219 rc = pCtx->isError;
99221 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
99222 pCtx->isError = 0;
99226 assert( (pOut->flags&MEM_Str)==0
99227 || pOut->enc==encoding
99228 || db->mallocFailed );
99231 REGISTER_TRACE(pOp->p3, pOut);
99242 pIn1 = &aMem[pOp->p1];
99243 pIn1->flags &= ~MEM_Subtype;
99256 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
99257 pIn1 = &aMem[pOp->p1];
99258 assert( pIn1->flags & MEM_Blob );
99259 assert( pIn1->n>0 );
99262 if( db->flags&SQLITE_VdbeTrace ){
99264 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
99267 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
99270 h %= pIn1->n;
99271 pIn1->z[h/8] |= 1<<(h&7);
99287 ** false positive - if the jump is taken when it should fall through.
99292 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
99293 pIn1 = &aMem[pOp->p1];
99294 assert( (pIn1->flags & MEM_Blob)!=0 );
99295 assert( pIn1->n >= 1 );
99298 if( db->flags&SQLITE_VdbeTrace ){
99300 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
99303 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
99306 h %= pIn1->n;
99307 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
99309 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
99312 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
99332 ** the UTF-8 string contained in P4 is emitted on the trace callback.
99351 ** The "--" string is broken up to prevent false-positives with srcck1.c.
99354 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
99356 ** using the X argument when X begins with "--" and invoking
99359 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
99362 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
99365 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
99366 && p->minWriteFileFormat!=254 /* tag-20220401a */
99367 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
99370 if( db->mTrace & SQLITE_TRACE_LEGACY ){
99372 db->trace.xLegacy(db->pTraceArg, z);
99376 if( db->nVdbeExec>1 ){
99377 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
99378 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
99381 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
99385 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
99388 for(j=0; j<db->nDb; j++){
99389 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
99390 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
99395 if( (db->flags & SQLITE_SqlTrace)!=0
99396 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
99398 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
99402 assert( pOp->p2>0 );
99403 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
99404 if( pOp->opcode==OP_Trace ) break;
99405 for(i=1; i<p->nOp; i++){
99406 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
99408 pOp->p1 = 0;
99410 pOp->p1++;
99411 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
99421 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
99426 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99427 assert( pOp->p4type==P4_EXPR );
99428 pC = p->apCsr[pOp->p1];
99430 assert( pC->eCurType==CURTYPE_BTREE );
99431 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
99432 pOp->p4.pExpr, aMem);
99489 assert( pOp->p1>0 );
99490 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
99491 pMem = &aMem[pOp->p1];
99492 constMask = pOp->p3;
99493 for(i=0; i<pOp->p2; i++, pMem++){
99495 pMem->pScopyFrom = 0;
99496 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
99512 ** the same as a no-op. This opcodesnever appears in a real VM program.
99515 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
99522 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
99544 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
99547 if( db->flags & SQLITE_VdbeTrace ){
99548 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
99551 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
99554 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
99571 if( db->mallocFailed ){
99578 if( db->flags & SQLITE_VdbeTrace ){
99579 const char *zTrace = p->zSql;
99586 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
99589 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
99592 p->rc = rc;
99596 (int)(pOp - aOp), p->zSql, p->zErrMsg);
99597 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
99599 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
99600 db->flags |= SQLITE_CorruptRdOnly;
99604 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
99624 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
99625 nProgressLimit += db->nProgressOps;
99626 if( db->xProgress(db->pProgressArg) ){
99633 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
99634 if( DbMaskNonZero(p->lockMask) ){
99638 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
99662 assert( AtomicLoad(&db->u1.isInterrupted) );
99708 ** the b-tree cursor associated with blob handle p to point to row iRow.
99719 ** If an error does occur, then the b-tree cursor is closed. All subsequent
99726 Vdbe *v = (Vdbe *)p->pStmt;
99731 v->aMem[1].flags = MEM_Int;
99732 v->aMem[1].u.i = iRow;
99738 if( v->pc>4 ){
99739 v->pc = 4;
99740 assert( v->aOp[v->pc].opcode==OP_NotExists );
99743 rc = sqlite3_step(p->pStmt);
99746 VdbeCursor *pC = v->apCsr[0];
99749 assert( pC->eCurType==CURTYPE_BTREE );
99750 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
99751 testcase( pC->nHdrParsed==p->iCol );
99752 testcase( pC->nHdrParsed==p->iCol+1 );
99754 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
99758 sqlite3_finalize(p->pStmt);
99759 p->pStmt = 0;
99761 p->iOffset = pC->aType[p->iCol + pC->nField];
99762 p->nByte = sqlite3VdbeSerialTypeLen(type);
99763 p->pCsr = pC->uc.pCursor;
99764 sqlite3BtreeIncrblobCursor(p->pCsr);
99770 }else if( p->pStmt ){
99771 rc = sqlite3_finalize(p->pStmt);
99772 p->pStmt = 0;
99774 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
99777 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
99797 int wrFlag, /* True -> read/write access, false -> read-only */
99801 int iCol; /* Index of zColumn in row-record */
99821 sqlite3_mutex_enter(db->mutex);
99856 pBlob->pTab = pTab;
99857 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
99860 for(iCol=0; iCol<pTab->nCol; iCol++) {
99861 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
99865 if( iCol==pTab->nCol ){
99880 if( db->flags&SQLITE_ForeignKeys ){
99887 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
99889 for(j=0; j<pFKey->nCol; j++){
99890 if( pFKey->aCol[j].iFrom==iCol ){
99897 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
99899 for(j=0; j<pIdx->nKeyCol; j++){
99901 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
99915 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
99916 assert( pBlob->pStmt || db->mallocFailed );
99917 if( pBlob->pStmt ){
99921 ** of writing code to use the b-tree layer directly is that the
99926 ** Code external to the Vdbe then "borrows" the b-tree cursor and
99931 ** which closes the b-tree cursor and (possibly) commits the
99944 Vdbe *v = (Vdbe *)pBlob->pStmt;
99945 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
99949 pTab->pSchema->schema_cookie,
99950 pTab->pSchema->iGeneration);
99952 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
99958 if( db->mallocFailed==0 ){
99965 aOp[0].p2 = pTab->tnum;
99967 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
99969 if( db->mallocFailed==0 ){
99973 ** parameter of the other to pTab->tnum. */
99975 aOp[1].p2 = pTab->tnum;
99986 aOp[1].p4.i = pTab->nCol+1;
99987 aOp[3].p2 = pTab->nCol;
99996 pBlob->iCol = iCol;
99997 pBlob->db = db;
99999 if( db->mallocFailed ){
100008 if( rc==SQLITE_OK && db->mallocFailed==0 ){
100011 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
100018 sqlite3_mutex_leave(db->mutex);
100032 sqlite3_stmt *pStmt = p->pStmt;
100033 db = p->db;
100034 sqlite3_mutex_enter(db->mutex);
100036 sqlite3_mutex_leave(db->mutex);
100060 db = p->db;
100061 sqlite3_mutex_enter(db->mutex);
100062 v = (Vdbe*)p->pStmt;
100064 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
100068 /* If there is no statement handle, then the blob-handle has
100074 ** returned, clean-up the statement handle.
100076 assert( db == v->db );
100077 sqlite3BtreeEnterCursor(p->pCsr);
100080 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
100081 /* If a pre-update hook is registered and this is a write cursor,
100084 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
100091 ** using the incremental-blob API, this works. For the sessions module
100095 iKey = sqlite3BtreeIntegerKey(p->pCsr);
100096 assert( v->apCsr[0]!=0 );
100097 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
100099 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
100104 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
100105 sqlite3BtreeLeaveCursor(p->pCsr);
100108 p->pStmt = 0;
100110 v->rc = rc;
100115 sqlite3_mutex_leave(db->mutex);
100141 return (p && p->pStmt) ? p->nByte : 0;
100160 db = p->db;
100161 sqlite3_mutex_enter(db->mutex);
100163 if( p->pStmt==0 ){
100164 /* If there is no statement handle, then the blob-handle has
100170 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
100180 assert( rc==SQLITE_OK || p->pStmt==0 );
100181 sqlite3_mutex_leave(db->mutex);
100190 ** 2011-07-09
100205 ** The VdbeSorter object implements a multi-threaded external merge sort
100209 ** Here is the (internal, non-API) interface between this module and the
100262 ** an in-memory merge sort. In this case, no temporary files are required
100269 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
100270 ** of PMAs may be created by merging existing PMAs together - for example
100271 ** merging two or more level-0 PMAs together creates a level-1 PMA.
100275 ** page-cache of the main database. Specifically, the threshold is set to
100279 ** If the sorter is running in single-threaded mode, then all PMAs generated
100281 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
100288 ** The sorter is running in multi-threaded mode if (a) the library was built
100289 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
100298 ** sorter is running in single-threaded mode, then these PMAs are merged
100303 ** Or, if running in multi-threaded mode, then a background thread is
100313 ** Rewind() is called, then a hierarchy of incremental-merges is used.
100319 ** If running in multi-threaded mode and there are more than
100332 ** characteristics of the sorter in multi-threaded mode.
100339 ** Hard-coded maximum amount of data to accumulate in memory before flushing
100352 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
100354 typedef struct SorterList SorterList; /* In-memory list of records */
100367 ** An in-memory list of objects to be sorted.
100376 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
100399 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
100413 ** aReadr[0] -> Banana
100414 ** aReadr[1] -> Feijoa
100415 ** aReadr[2] -> Elderberry
100416 ** aReadr[3] -> Currant
100417 ** aReadr[4] -> Grapefruit
100418 ** aReadr[5] -> Apple
100419 ** aReadr[6] -> Durian
100420 ** aReadr[7] -> EOF
100429 ** aReadr[5] -> Eggplant
100434 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
100457 ** single-threaded operation, there is exactly one instance of this object
100458 ** and for multi-threaded operation there are two or more instances.
100469 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
100486 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
100491 SorterFile file; /* Temp file for level-0 PMAs */
100515 SorterList list; /* List of in-memory records */
100558 ** There are two types of IncrMerger object - single (bUseThread==0) and
100559 ** multi-threaded (bUseThread==1).
100561 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
100576 ** A single-threaded IncrMerger does not open any temporary files of its
100578 ** at offset iStartOff of file pTask->file2. And instead of using a
100580 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
100598 ** size. But I/O is more efficient if it occurs in page-sized blocks where
100600 ** the PMA so that aligned, page-size blocks are written.
100603 int eFWErr; /* Non-zero if in an error state */
100617 ** by this module. If using a separate allocation for each in-memory record
100625 ** has finished passing records to the sorter, or when the in-memory buffer
100658 sqlite3_free(pReadr->aAlloc);
100659 sqlite3_free(pReadr->aBuffer);
100660 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
100661 vdbeIncrFree(pReadr->pIncr);
100682 if( p->aMap ){
100683 *ppOut = &p->aMap[p->iReadOff];
100684 p->iReadOff += nByte;
100688 assert( p->aBuffer );
100691 ** p->nBuffer bytes of data from the file into it. Or, if there are less
100692 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
100693 iBuf = p->iReadOff % p->nBuffer;
100699 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
100700 nRead = p->nBuffer;
100702 nRead = (int)(p->iEof - p->iReadOff);
100707 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
100711 nAvail = p->nBuffer - iBuf;
100714 /* The requested data is available in the in-memory buffer. In this
100717 *ppOut = &p->aBuffer[iBuf];
100718 p->iReadOff += nByte;
100720 /* The requested data is not all available in the in-memory buffer.
100721 ** In this case, allocate space at p->aAlloc[] to copy the requested
100722 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
100725 /* Extend the p->aAlloc[] allocation if required. */
100726 if( p->nAlloc<nByte ){
100728 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
100730 aNew = sqlite3Realloc(p->aAlloc, nNew);
100732 p->nAlloc = nNew;
100733 p->aAlloc = aNew;
100737 ** p->aAlloc[]. */
100738 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
100739 p->iReadOff += nAvail;
100740 nRem = nByte - nAvail;
100742 /* The following loop copies up to p->nBuffer bytes per iteration into
100743 ** the p->aAlloc[] buffer. */
100750 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
100753 assert( aNext!=p->aAlloc );
100754 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
100755 nRem -= nCopy;
100758 *ppOut = p->aAlloc;
100771 if( p->aMap ){
100772 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
100774 iBuf = p->iReadOff % p->nBuffer;
100775 if( iBuf && (p->nBuffer-iBuf)>=9 ){
100776 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
100803 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
100804 sqlite3_file *pFd = pFile->pFd;
100805 if( pFd->pMethods->iVersion>=3 ){
100806 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
100826 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
100829 if( pReadr->aMap ){
100830 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
100831 pReadr->aMap = 0;
100833 pReadr->iReadOff = iOff;
100834 pReadr->iEof = pFile->iEof;
100835 pReadr->pFd = pFile->pFd;
100837 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
100838 if( rc==SQLITE_OK && pReadr->aMap==0 ){
100839 int pgsz = pTask->pSorter->pgsz;
100840 int iBuf = pReadr->iReadOff % pgsz;
100841 if( pReadr->aBuffer==0 ){
100842 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
100843 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
100844 pReadr->nBuffer = pgsz;
100847 int nRead = pgsz - iBuf;
100848 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
100849 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
100852 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
100870 if( pReadr->iReadOff>=pReadr->iEof ){
100871 IncrMerger *pIncr = pReadr->pIncr;
100875 if( rc==SQLITE_OK && pIncr->bEof==0 ){
100877 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
100895 pReadr->nKey = (int)nRec;
100896 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
100905 ** starting at offset iStart and ending at offset iEof-1. This function
100921 assert( pFile->iEof>iStart );
100922 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
100923 assert( pReadr->aBuffer==0 );
100924 assert( pReadr->aMap==0 );
100930 pReadr->iEof = pReadr->iReadOff + nByte;
100947 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100951 UnpackedRecord *r2 = pTask->pUnpacked;
100953 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
100961 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
100965 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
100966 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
100969 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
100974 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100978 UnpackedRecord *r2 = pTask->pUnpacked;
100980 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
100993 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101008 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
101010 res = n1 - n2;
101014 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
101020 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
101021 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
101022 res = res * -1;
101035 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101057 if( (res = v1[i] - v2[i])!=0 ){
101059 res = v1[0] & 0x80 ? -1 : +1;
101065 res = s1 - s2;
101070 res = -1;
101072 res = s1 - s2;
101077 if( *v1 & 0x80 ) res = -1;
101084 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
101089 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
101090 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
101091 res = res * -1;
101100 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
101103 ** is non-zero and the sorter is able to guarantee a stable sort, nField
101111 ** The sorter can guarantee a stable sort when running in single-threaded
101112 ** mode, but not in multi-threaded mode.
101124 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
101125 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
101139 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
101147 nWorker = SORTER_MAX_MERGE_COUNT-1;
101151 assert( pCsr->pKeyInfo );
101152 assert( !pCsr->isEphemeral );
101153 assert( pCsr->eCurType==CURTYPE_SORTER );
101154 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
101158 pCsr->uc.pSorter = pSorter;
101162 Btree *pBt = db->aDb[0].pBt;
101163 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
101164 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
101165 pKeyInfo->db = 0;
101167 pKeyInfo->nKeyField = nField;
101170 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
101172 pSorter->nTask = nWorker + 1;
101173 pSorter->iPrev = (u8)(nWorker - 1);
101174 pSorter->bUseThreads = (pSorter->nTask>1);
101175 pSorter->db = db;
101176 for(i=0; i<pSorter->nTask; i++){
101177 SortSubtask *pTask = &pSorter->aTask[i];
101178 pTask->pSorter = pSorter;
101184 pSorter->mnPmaSize = szPma * pgsz;
101186 mxCache = db->aDb[0].pSchema->cache_size;
101188 /* A negative cache-size value C indicates that the cache is abs(C)
101190 mxCache = mxCache * -1024;
101195 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
101200 assert( pSorter->iMemory==0 );
101201 pSorter->nMemory = pgsz;
101202 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
101203 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
101207 if( pKeyInfo->nAllField<13
101208 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
101209 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
101211 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
101226 pNext = p->u.pNext;
101236 sqlite3DbFree(db, pTask->pUnpacked);
101238 /* pTask->list.aMemory can only be non-zero if it was handed memory
101240 if( pTask->list.aMemory ){
101241 sqlite3_free(pTask->list.aMemory);
101245 assert( pTask->list.aMemory==0 );
101246 vdbeSorterRecordFree(0, pTask->list.pList);
101248 if( pTask->file.pFd ){
101249 sqlite3OsCloseFree(pTask->file.pFd);
101251 if( pTask->file2.pFd ){
101252 sqlite3OsCloseFree(pTask->file2.pFd);
101260 int iTask = (pTask - pTask->pSorter->aTask);
101261 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101275 int iTask = (pTask - pTask->pSorter->aTask);
101276 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101286 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101299 ** Join thread pTask->thread.
101303 if( pTask->pThread ){
101305 int bDone = pTask->bDone;
101309 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
101312 assert( pTask->bDone==1 );
101313 pTask->bDone = 0;
101314 pTask->pThread = 0;
101327 assert( pTask->pThread==0 && pTask->bDone==0 );
101328 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
101333 ** level-0 PMAs.
101342 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
101345 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
101346 for(i=pSorter->nTask-1; i>=0; i--){
101347 SortSubtask *pTask = &pSorter->aTask[i];
101377 pNew->nTree = N;
101378 pNew->pTask = 0;
101379 pNew->aReadr = (PmaReader*)&pNew[1];
101380 pNew->aTree = (int*)&pNew->aReadr[N];
101391 for(i=0; i<pMerger->nTree; i++){
101392 vdbePmaReaderClear(&pMerger->aReadr[i]);
101405 if( pIncr->bUseThread ){
101406 vdbeSorterJoinThread(pIncr->pTask);
101407 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
101408 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
101411 vdbeMergeEngineFree(pIncr->pMerger);
101422 assert( pSorter->bUseThreads || pSorter->pReader==0 );
101424 if( pSorter->pReader ){
101425 vdbePmaReaderClear(pSorter->pReader);
101426 sqlite3DbFree(db, pSorter->pReader);
101427 pSorter->pReader = 0;
101430 vdbeMergeEngineFree(pSorter->pMerger);
101431 pSorter->pMerger = 0;
101432 for(i=0; i<pSorter->nTask; i++){
101433 SortSubtask *pTask = &pSorter->aTask[i];
101435 pTask->pSorter = pSorter;
101437 if( pSorter->list.aMemory==0 ){
101438 vdbeSorterRecordFree(0, pSorter->list.pList);
101440 pSorter->list.pList = 0;
101441 pSorter->list.szPMA = 0;
101442 pSorter->bUsePMA = 0;
101443 pSorter->iMemory = 0;
101444 pSorter->mxKeysize = 0;
101445 sqlite3DbFree(db, pSorter->pUnpacked);
101446 pSorter->pUnpacked = 0;
101454 assert( pCsr->eCurType==CURTYPE_SORTER );
101455 pSorter = pCsr->uc.pSorter;
101458 sqlite3_free(pSorter->list.aMemory);
101460 pCsr->uc.pSorter = 0;
101466 ** The first argument is a file-handle open on a temporary file. The file
101475 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
101489 ** Allocate space for a file-handle and open a temporary file. If successful,
101490 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
101500 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
101517 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
101521 if( pTask->pUnpacked==0 ){
101522 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
101523 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
101524 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
101525 pTask->pUnpacked->errCode = 0;
101546 res = pTask->xCompare(
101547 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
101552 pp = &p1->u.pNext;
101553 p1 = p1->u.pNext;
101560 pp = &p2->u.pNext;
101561 p2 = p2->u.pNext;
101577 if( p->typeMask==SORTER_TYPE_INTEGER ){
101579 }else if( p->typeMask==SORTER_TYPE_TEXT ){
101586 ** Sort the linked list of records headed at pTask->pList. Return
101599 p = pList->pList;
101600 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
101605 if( pList->aMemory ){
101606 if( (u8*)p==pList->aMemory ){
101609 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
101610 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
101613 pNext = p->u.pNext;
101616 p->u.pNext = 0;
101630 pList->pList = p;
101632 assert( pTask->pUnpacked->errCode==SQLITE_OK
101633 || pTask->pUnpacked->errCode==SQLITE_NOMEM
101635 return pTask->pUnpacked->errCode;
101639 ** Initialize a PMA-writer object.
101648 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
101649 if( !p->aBuffer ){
101650 p->eFWErr = SQLITE_NOMEM_BKPT;
101652 p->iBufEnd = p->iBufStart = (iStart % nBuf);
101653 p->iWriteOff = iStart - p->iBufStart;
101654 p->nBuffer = nBuf;
101655 p->pFd = pFd;
101665 while( nRem>0 && p->eFWErr==0 ){
101667 if( nCopy>(p->nBuffer - p->iBufEnd) ){
101668 nCopy = p->nBuffer - p->iBufEnd;
101671 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
101672 p->iBufEnd += nCopy;
101673 if( p->iBufEnd==p->nBuffer ){
101674 p->eFWErr = sqlite3OsWrite(p->pFd,
101675 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101676 p->iWriteOff + p->iBufStart
101678 p->iBufStart = p->iBufEnd = 0;
101679 p->iWriteOff += p->nBuffer;
101681 assert( p->iBufEnd<p->nBuffer );
101683 nRem -= nCopy;
101688 ** Flush any buffered data to disk and clean up the PMA-writer object.
101689 ** The results of using the PMA-writer after this call are undefined.
101698 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
101699 p->eFWErr = sqlite3OsWrite(p->pFd,
101700 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101701 p->iWriteOff + p->iBufStart
101704 *piEof = (p->iWriteOff + p->iBufEnd);
101705 sqlite3_free(p->aBuffer);
101706 rc = p->eFWErr;
101723 ** Write the current contents of in-memory linked-list pList to a level-0
101724 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
101732 ** * One or more records packed end-to-end in order of ascending keys.
101737 sqlite3 *db = pTask->pSorter->db;
101742 /* Set iSz to the expected size of file pTask->file after writing the PMA.
101744 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
101749 assert( pList->szPMA>0 );
101752 if( pTask->file.pFd==0 ){
101753 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
101754 assert( rc!=SQLITE_OK || pTask->file.pFd );
101755 assert( pTask->file.iEof==0 );
101756 assert( pTask->nPMA==0 );
101761 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
101773 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
101774 pTask->file.iEof);
101775 pTask->nPMA++;
101776 vdbePmaWriteVarint(&writer, pList->szPMA);
101777 for(p=pList->pList; p; p=pNext){
101778 pNext = p->u.pNext;
101779 vdbePmaWriteVarint(&writer, p->nVal);
101780 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
101781 if( pList->aMemory==0 ) sqlite3_free(p);
101783 pList->pList = p;
101784 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
101788 assert( rc!=SQLITE_OK || pList->pList==0 );
101789 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
101805 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
101806 SortSubtask *pTask = pMerger->pTask;
101809 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
101820 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
101821 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
101823 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
101826 if( pReadr1->pFd==0 ){
101828 }else if( pReadr2->pFd==0 ){
101829 iRes = -1;
101831 iRes = pTask->xCompare(pTask, &bCached,
101832 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
101838 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
101843 ** was actually called above, then pTask->pUnpacked now contains
101852 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
101853 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
101856 if( pReadr1->pFd ) bCached = 0;
101857 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
101858 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
101861 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
101864 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
101869 ** The main routine for background threads that write level-0 PMAs.
101874 assert( pTask->bDone==0 );
101875 rc = vdbeSorterListToPMA(pTask, &pTask->list);
101876 pTask->bDone = 1;
101887 pSorter->bUsePMA = 1;
101888 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
101893 int nWorker = (pSorter->nTask-1);
101897 pSorter->bUsePMA = 1;
101899 /* Select a sub-task to sort and flush the current list of in-memory
101900 ** records to disk. If the sorter is running in multi-threaded mode,
101901 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
101902 ** the background thread from a sub-tasks previous turn is still running,
101903 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
101904 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
101905 ** sub-tasks are prefered as they use background threads - the final
101906 ** sub-task uses the main thread. */
101908 int iTest = (pSorter->iPrev + i + 1) % nWorker;
101909 pTask = &pSorter->aTask[iTest];
101910 if( pTask->bDone ){
101913 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
101919 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
101926 assert( pTask->pThread==0 && pTask->bDone==0 );
101927 assert( pTask->list.pList==0 );
101928 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
101930 aMem = pTask->list.aMemory;
101932 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
101933 pTask->list = pSorter->list;
101934 pSorter->list.pList = 0;
101935 pSorter->list.szPMA = 0;
101937 pSorter->list.aMemory = aMem;
101938 pSorter->nMemory = sqlite3MallocSize(aMem);
101939 }else if( pSorter->list.aMemory ){
101940 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
101941 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
101967 assert( pCsr->eCurType==CURTYPE_SORTER );
101968 pSorter = pCsr->uc.pSorter;
101969 getVarint32NR((const u8*)&pVal->z[1], t);
101971 pSorter->typeMask &= SORTER_TYPE_INTEGER;
101973 pSorter->typeMask &= SORTER_TYPE_TEXT;
101975 pSorter->typeMask = 0;
101983 ** If using the single large allocation mode (pSorter->aMemory!=0), then
101990 ** * The total memory allocated for the in-memory list is greater
101991 ** than (page-size * cache-size), or
101993 ** * The total memory allocated for the in-memory list is greater
101994 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
101996 nReq = pVal->n + sizeof(SorterRecord);
101997 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
101998 if( pSorter->mxPmaSize ){
101999 if( pSorter->list.aMemory ){
102000 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
102003 (pSorter->list.szPMA > pSorter->mxPmaSize)
102004 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
102009 pSorter->list.szPMA = 0;
102010 pSorter->iMemory = 0;
102011 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
102015 pSorter->list.szPMA += nPMA;
102016 if( nPMA>pSorter->mxKeysize ){
102017 pSorter->mxKeysize = nPMA;
102020 if( pSorter->list.aMemory ){
102021 int nMin = pSorter->iMemory + nReq;
102023 if( nMin>pSorter->nMemory ){
102025 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
102026 int iListOff = -1;
102027 if( pSorter->list.pList ){
102028 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
102031 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
102033 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
102036 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
102038 pSorter->list.aMemory = aNew;
102039 pSorter->nMemory = nNew;
102042 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
102043 pSorter->iMemory += ROUND8(nReq);
102044 if( pSorter->list.pList ){
102045 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
102052 pNew->u.pNext = pSorter->list.pList;
102055 memcpy(SRVAL(pNew), pVal->z, pVal->n);
102056 pNew->nVal = pVal->n;
102057 pSorter->list.pList = pNew;
102063 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
102065 ** except that the number-of-bytes varint is omitted from the start.
102070 i64 iStart = pIncr->iStartOff;
102071 SorterFile *pOut = &pIncr->aFile[1];
102072 SortSubtask *pTask = pIncr->pTask;
102073 MergeEngine *pMerger = pIncr->pMerger;
102075 assert( pIncr->bEof==0 );
102079 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
102082 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
102083 int nKey = pReader->nKey;
102088 if( pReader->pFd==0 ) break;
102089 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
102093 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
102094 assert( pIncr->pMerger->pTask==pTask );
102095 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
102098 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
102107 ** multi-threaded IncrMerger objects.
102112 pIncr->pTask->bDone = 1;
102121 assert( pIncr->bUseThread );
102122 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
102132 ** For single-threaded objects, this is accomplished by literally reading
102133 ** keys from pIncr->pMerger and repopulating aFile[0].
102135 ** For multi-threaded objects, all that is required is to wait until the
102147 if( pIncr->bUseThread ){
102148 rc = vdbeSorterJoinThread(pIncr->pTask);
102151 SorterFile f0 = pIncr->aFile[0];
102152 pIncr->aFile[0] = pIncr->aFile[1];
102153 pIncr->aFile[1] = f0;
102157 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
102158 pIncr->bEof = 1;
102167 pIncr->aFile[0] = pIncr->aFile[1];
102168 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
102169 pIncr->bEof = 1;
102191 pIncr->pMerger = pMerger;
102192 pIncr->pTask = pTask;
102193 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
102194 pTask->file2.iEof += pIncr->mxSz;
102205 ** Set the "use-threads" flag on object pIncr.
102208 pIncr->bUseThread = 1;
102209 pIncr->pTask->file2.iEof -= pIncr->mxSz;
102216 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
102222 int iOut /* Store the result in pMerger->aTree[iOut] */
102230 assert( iOut<pMerger->nTree && iOut>0 );
102232 if( iOut>=(pMerger->nTree/2) ){
102233 i1 = (iOut - pMerger->nTree/2) * 2;
102236 i1 = pMerger->aTree[iOut*2];
102237 i2 = pMerger->aTree[iOut*2+1];
102240 p1 = &pMerger->aReadr[i1];
102241 p2 = &pMerger->aReadr[i2];
102243 if( p1->pFd==0 ){
102245 }else if( p2->pFd==0 ){
102248 SortSubtask *pTask = pMerger->pTask;
102251 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
102252 res = pTask->xCompare(
102253 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
102262 pMerger->aTree[iOut] = iRes;
102269 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
102315 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
102319 assert( pMerger->pTask==0 );
102320 pMerger->pTask = pTask;
102322 nTree = pMerger->nTree;
102327 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
102330 ** on this PmaReader before any of the multi-threaded PmaReaders takes
102331 ** better advantage of multi-processor hardware. */
102332 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
102334 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
102339 for(i=pMerger->nTree-1; i>0; i--){
102342 return pTask->pUnpacked->errCode;
102347 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
102349 ** object at (pReadr->pIncr).
102352 ** in the sub-tree headed by pReadr are also initialized. Data is then
102357 ** to be a multi-threaded PmaReader and this function is being called in a
102358 ** background thread. In this case all PmaReaders in the sub-tree are
102366 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
102367 ** this entire function is being run by thread (pTask->thread), that will
102371 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
102372 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
102380 IncrMerger *pIncr = pReadr->pIncr;
102381 SortSubtask *pTask = pIncr->pTask;
102382 sqlite3 *db = pTask->pSorter->db;
102384 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
102387 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
102389 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
102390 ** requires two temp files to itself, whereas a single-threaded object
102391 ** only requires a region of pTask->file2. */
102393 int mxSz = pIncr->mxSz;
102395 if( pIncr->bUseThread ){
102396 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
102398 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
102402 /*if( !pIncr->bUseThread )*/{
102403 if( pTask->file2.pFd==0 ){
102404 assert( pTask->file2.iEof>0 );
102405 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
102406 pTask->file2.iEof = 0;
102409 pIncr->aFile[1].pFd = pTask->file2.pFd;
102410 pIncr->iStartOff = pTask->file2.iEof;
102411 pTask->file2.iEof += mxSz;
102417 if( rc==SQLITE_OK && pIncr->bUseThread ){
102419 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
102421 ** pIncr->pTask->thread.
102450 pReader->pIncr->pTask->bDone = 1;
102456 ** If the PmaReader passed as the first argument is not an incremental-reader
102457 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
102461 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
102467 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
102471 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
102472 if( pIncr->bUseThread ){
102474 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
102485 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
102486 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
102491 ** first PMA to read from pTask->file. Assuming no error occurs, it is
102499 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
102500 MergeEngine **ppOut /* OUT: New merge-engine */
102512 PmaReader *pReadr = &pNew->aReadr[i];
102513 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
102514 iOff = pReadr->iEof;
102531 ** nPMA<=16 -> TreeDepth() == 0
102532 ** nPMA<=256 -> TreeDepth() == 1
102533 ** nPMA<=65536 -> TreeDepth() == 2
102546 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
102574 PmaReader *pReadr = &p->aReadr[iIter];
102576 if( pReadr->pIncr==0 ){
102581 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
102585 p = pReadr->pIncr->pMerger;
102591 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
102600 ** that has already written two or more level-0 PMAs to one or more temp
102618 /* If the sorter uses more than one task, then create the top-level
102620 ** one PmaReader per sub-task. */
102621 assert( pSorter->bUseThreads || pSorter->nTask==1 );
102622 if( pSorter->nTask>1 ){
102623 pMain = vdbeMergeEngineNew(pSorter->nTask);
102628 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102629 SortSubtask *pTask = &pSorter->aTask[iTask];
102630 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
102631 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
102633 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
102636 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
102637 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
102643 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
102644 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
102645 int nReader; /* Number of level-0 PMAs to merge */
102647 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
102658 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
102683 ** (for multi-threaded sorters) so that it can be used to iterate through
102690 SortSubtask *pTask0 = &pSorter->aTask[0];
102693 sqlite3 *db = pTask0->pSorter->db;
102696 for(i=0; i<pSorter->nTask; i++){
102697 pSorter->aTask[i].xCompare = xCompare;
102704 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
102705 if( pSorter->bUseThreads ){
102708 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
102712 pSorter->pReader = pReadr;
102716 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
102718 vdbeIncrMergerSetThreads(pReadr->pIncr);
102719 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
102721 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
102723 assert( pIncr->pTask!=pLast );
102726 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102731 ** b) If it is using task (nTask-1), it is configured to run
102732 ** in single-threaded mode. This is important, as the
102736 PmaReader *p = &pMain->aReadr[iTask];
102737 assert( p->pIncr==0 || (
102738 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
102739 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
102753 pSorter->pMerger = pMain;
102774 assert( pCsr->eCurType==CURTYPE_SORTER );
102775 pSorter = pCsr->uc.pSorter;
102780 ** from the in-memory list. */
102781 if( pSorter->bUsePMA==0 ){
102782 if( pSorter->list.pList ){
102784 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
102791 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
102795 assert( pSorter->list.pList );
102805 assert( pSorter->pReader==0 );
102826 assert( pCsr->eCurType==CURTYPE_SORTER );
102827 pSorter = pCsr->uc.pSorter;
102828 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
102829 if( pSorter->bUsePMA ){
102830 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
102831 assert( pSorter->bUseThreads==0 || pSorter->pReader );
102832 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
102834 if( pSorter->bUseThreads ){
102835 rc = vdbePmaReaderNext(pSorter->pReader);
102836 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
102839 /*if( !pSorter->bUseThreads )*/ {
102841 assert( pSorter->pMerger!=0 );
102842 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
102843 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
102847 SorterRecord *pFree = pSorter->list.pList;
102848 pSorter->list.pList = pFree->u.pNext;
102849 pFree->u.pNext = 0;
102850 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
102851 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
102865 if( pSorter->bUsePMA ){
102868 if( pSorter->bUseThreads ){
102869 pReader = pSorter->pReader;
102872 /*if( !pSorter->bUseThreads )*/{
102873 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
102875 *pnKey = pReader->nKey;
102876 pKey = pReader->aKey;
102878 *pnKey = pSorter->list.pList->nVal;
102879 pKey = SRVAL(pSorter->list.pList);
102891 assert( pCsr->eCurType==CURTYPE_SORTER );
102892 pSorter = pCsr->uc.pSorter;
102897 pOut->n = nKey;
102899 memcpy(pOut->z, pKey, nKey);
102932 assert( pCsr->eCurType==CURTYPE_SORTER );
102933 pSorter = pCsr->uc.pSorter;
102934 r2 = pSorter->pUnpacked;
102935 pKeyInfo = pCsr->pKeyInfo;
102937 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
102939 r2->nField = nKeyCol;
102941 assert( r2->nField==nKeyCol );
102946 if( r2->aMem[i].flags & MEM_Null ){
102947 *pRes = -1;
102952 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
102959 ** 2020-03-23
102970 ** This file implements virtual-tables for examining the bytecode content
102977 /* An instance of the bytecode() table-valued function.
102981 sqlite3_vtab base; /* Base class - must be first */
102990 sqlite3_vtab_cursor base; /* Base class - must be first */
103005 ** Create a new bytecode() table-valued function.
103052 pNew->db = db;
103053 pNew->bTablesUsed = isTabUsed*2;
103076 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
103077 *ppCursor = &pCur->base;
103085 sqlite3_free(pCur->zP4);
103086 pCur->zP4 = 0;
103087 sqlite3VdbeMemRelease(&pCur->sub);
103088 sqlite3VdbeMemSetNull(&pCur->sub);
103089 if( pCur->needFinalize ){
103090 sqlite3_finalize(pCur->pStmt);
103092 pCur->pStmt = 0;
103093 pCur->needFinalize = 0;
103094 pCur->zType = 0;
103095 pCur->zSchema = 0;
103096 pCur->zName = 0;
103115 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
103117 if( pCur->zP4 ){
103118 sqlite3_free(pCur->zP4);
103119 pCur->zP4 = 0;
103121 if( pCur->zName ){
103122 pCur->zName = 0;
103123 pCur->zType = 0;
103124 pCur->zSchema = 0;
103127 (Vdbe*)pCur->pStmt,
103128 pCur->showSubprograms ? &pCur->sub : 0,
103129 pTab->bTablesUsed,
103130 &pCur->iRowid,
103131 &pCur->iAddr,
103132 &pCur->aOp);
103134 sqlite3VdbeMemSetNull(&pCur->sub);
103135 pCur->aOp = 0;
103146 return pCur->aOp==0;
103159 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
103160 Op *pOp = pCur->aOp + pCur->iAddr;
103161 if( pVTab->bTablesUsed ){
103165 if( i<=2 && pCur->zType==0 ){
103168 int iDb = pOp->p3;
103169 Pgno iRoot = (Pgno)pOp->p2;
103170 sqlite3 *db = pVTab->db;
103171 pSchema = db->aDb[iDb].pSchema;
103172 pCur->zSchema = db->aDb[iDb].zDbSName;
103173 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
103175 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
103176 pCur->zName = pTab->zName;
103177 pCur->zType = "table";
103181 if( pCur->zName==0 ){
103182 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
103184 if( pIdx->tnum==iRoot ){
103185 pCur->zName = pIdx->zName;
103186 pCur->zType = "index";
103196 sqlite3_result_int(ctx, pCur->iAddr);
103199 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
103200 -1, SQLITE_STATIC);
103203 sqlite3_result_int(ctx, pOp->p1);
103206 sqlite3_result_int(ctx, pOp->p2);
103209 sqlite3_result_int(ctx, pOp->p3);
103213 if( pCur->zP4==0 ){
103214 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
103217 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
103220 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
103221 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
103226 sqlite3_result_int(ctx, pOp->p5);
103229 Op *aOp = pCur->aOp;
103231 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
103232 if( pCur->iRowid==pCur->iAddr+1 ){
103235 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
103242 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
103245 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
103248 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
103251 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
103263 *pRowid = pCur->iRowid;
103279 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
103284 pCur->iRowid = 0;
103285 pCur->iAddr = 0;
103286 pCur->showSubprograms = idxNum==0;
103293 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
103294 pCur->needFinalize = 1;
103297 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
103299 if( pCur->pStmt==0 ){
103300 pVTab->base.zErrMsg = sqlite3_mprintf(
103302 pVTab->bTablesUsed ? "tables_used" : "bytecode"
103324 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
103325 pIdxInfo->estimatedCost = (double)100;
103326 pIdxInfo->estimatedRows = 100;
103327 pIdxInfo->idxNum = 0;
103328 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
103329 if( p->usable==0 ) continue;
103330 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
103332 pIdxInfo->aConstraintUsage[i].omit = 1;
103333 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
103335 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
103336 pIdxInfo->aConstraintUsage[i].omit = 1;
103337 pIdxInfo->idxNum = 1;
103401 ** This file contains code use to implement an in-memory rollback journal.
103402 ** The in-memory rollback journal is used to journal transactions for
103405 ** Update: The in-memory journal is also used to temporarily cache
103406 ** smaller journals that are not critical for power-loss recovery.
103424 ** The zChunk array is always at least 8 bytes in size - usually much more.
103441 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
103453 ** This structure is a subclass of sqlite3_file. Each open memory-journal
103458 int nChunkSize; /* In-memory chunk-size */
103461 FileChunk *pFirst; /* Head of in-memory chunk-list */
103471 ** Read data from the in-memory journal file. This is the implementation
103486 if( (iAmt+iOfst)>p->endpoint.iOffset ){
103489 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
103490 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
103492 for(pChunk=p->pFirst;
103493 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
103494 pChunk=pChunk->pNext
103496 iOff += p->nChunkSize;
103499 pChunk = p->readpoint.pChunk;
103503 iChunkOffset = (int)(iOfst%p->nChunkSize);
103505 int iSpace = p->nChunkSize - iChunkOffset;
103506 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
103507 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
103509 nRead -= iSpace;
103511 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
103512 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
103513 p->readpoint.pChunk = pChunk;
103525 pNext = pIter->pNext;
103544 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
103546 nChunk = copy.endpoint.iOffset - iOff;
103548 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
103553 /* No error has occurred. Free the in-memory buffers. */
103559 ** the original before returning. This way, SQLite uses the in-memory
103560 ** journal data to roll back changes made to the internal page-cache
103587 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
103597 /* An in-memory journal file should only ever be appended to. Random
103599 ** the in-memory journal is being used by a connection using the
103600 ** atomic-write optimization. In this case the first 28 bytes of the
103602 assert( iOfst<=p->endpoint.iOffset );
103603 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
103606 if( iOfst==0 && p->pFirst ){
103607 assert( p->nChunkSize>iAmt );
103608 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
103611 FileChunk *pChunk = p->endpoint.pChunk;
103612 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
103613 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
103618 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
103622 pNew->pNext = 0;
103624 assert( p->pFirst );
103625 pChunk->pNext = pNew;
103627 assert( !p->pFirst );
103628 p->pFirst = pNew;
103630 pChunk = p->endpoint.pChunk = pNew;
103634 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
103636 nWrite -= iSpace;
103637 p->endpoint.iOffset += iSpace;
103646 ** Truncate the in-memory file.
103650 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
103651 if( size<p->endpoint.iOffset ){
103654 memjrnlFreeChunks(p->pFirst);
103655 p->pFirst = 0;
103657 i64 iOff = p->nChunkSize;
103658 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
103659 iOff += p->nChunkSize;
103662 memjrnlFreeChunks(pIter->pNext);
103663 pIter->pNext = 0;
103667 p->endpoint.pChunk = pIter;
103668 p->endpoint.iOffset = size;
103669 p->readpoint.pChunk = 0;
103670 p->readpoint.iOffset = 0;
103680 memjrnlFreeChunks(p->pFirst);
103688 ** syncing an in-memory journal is a no-op.
103700 *pSize = (sqlite_int64) p->endpoint.iOffset;
103735 ** all content is always stored in main-memory. Finally, if nSpill is a
103736 ** positive value, then the journal file is initially created in-memory
103752 /* Zero the file-handle object. If nSpill was passed zero, initialize
103755 ** made on the journal file-handle. */
103762 p->nChunkSize = nSpill;
103764 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
103765 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
103768 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
103769 p->nSpill = nSpill;
103770 p->flags = flags;
103771 p->zJournal = zName;
103772 p->pVfs = pVfs;
103777 ** Open an in-memory journal file.
103780 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
103787 ** in-memory-only journal file (i.e. is one that was opened with a +ve
103794 if( pJfd->pMethods==&MemJournalMethods && (
103796 p->nSpill>0
103801 NEVER(p->nSpill>0)
103804 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
103814 ** The file-handle passed as the only argument is open on a journal file.
103819 return p->pMethods==&MemJournalMethods;
103824 ** pVfs to create the underlying on-disk files.
103827 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
103858 for(pWin=pList; pWin; pWin=pWin->pNextWin){
103860 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
103862 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
103864 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
103866 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
103868 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
103890 ** return from the top-level walk call.
103900 rc = pWalker->xExprCallback(pWalker, pExpr);
103903 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
103904 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
103905 if( pExpr->pRight ){
103907 pExpr = pExpr->pRight;
103911 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
103913 if( pExpr->x.pList ){
103914 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
103918 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
103939 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
103940 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
103947 ** This is a no-op callback for Walker->xSelectCallback2. If this
103948 ** callback is set, then the Select->pWinDefn list is traversed.
103953 /* No-op */
103963 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
103964 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
103965 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
103966 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
103967 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
103968 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
103970 if( p->pWinDefn ){
103972 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
103973 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
103975 || pWalker->xSelectCallback2==sqlite3SelectPopWith
103980 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
104000 pSrc = p->pSrc;
104002 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
104003 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
104006 if( pItem->fg.isTabFunc
104007 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
104019 ** on the compound select chain, p->pPrior.
104024 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
104031 ** is a no-op returning WRC_Continue.
104036 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
104038 rc = pWalker->xSelectCallback(pWalker, p);
104045 if( pWalker->xSelectCallback2 ){
104046 pWalker->xSelectCallback2(pWalker, p);
104048 p = p->pPrior;
104058 pWalker->walkerDepth++;
104063 pWalker->walkerDepth--;
104068 ** No-op routine for the parse-tree walker.
104082 ** No-op routine for the parse-tree walker for SELECT statements.
104122 ** is a helper function - a callback for the tree walker.
104127 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
104141 ** Turn the pExpr expression into an alias for the iCol-th column of the
104162 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
104166 Expr *pOrig; /* The iCol-th column of the result set */
104170 assert( iCol>=0 && iCol<pEList->nExpr );
104171 pOrig = pEList->a[iCol].pExpr;
104173 db = pParse->db;
104175 if( db->mallocFailed ){
104181 if( pExpr->op==TK_COLLATE ){
104183 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
104189 if( ALWAYS(pExpr->y.pWin!=0) ){
104190 pExpr->y.pWin->pOwner = pExpr;
104212 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
104213 zSpan = pItem->zEName;
104231 ** Return TRUE if the double-quoted string mis-feature should be supported.
104234 if( db->init.busy ) return 1; /* Always support for legacy schemas */
104235 if( pTopNC->ncFlags & NC_IsDDL ){
104237 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
104240 return (db->flags & SQLITE_DqsDDL)!=0;
104243 return (db->flags & SQLITE_DqsDML)!=0;
104248 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
104255 n = pExpr->iColumn;
104257 pExTab = pExpr->y.pTab;
104259 if( (pExTab->tabFlags & TF_HasGenerated)!=0
104260 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
104262 testcase( pExTab->nCol==BMS-1 );
104263 testcase( pExTab->nCol==BMS );
104264 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
104266 testcase( n==BMS-1 );
104268 if( n>=BMS ) n = BMS-1;
104285 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
104287 pNew->iTable = pMatch->iCursor;
104288 pNew->iColumn = iColumn;
104289 pNew->y.pTab = pMatch->pTab;
104290 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
104297 ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
104302 Schema *pSchema /* non-NULL if a database qualifier is present */
104306 assert( pTab->tnum==1 );
104308 zLegacy = pTab->zName;
104328 ** pExpr->iDb Set the index in db->aDb[] of the database X
104330 ** pExpr->iTable Set to the cursor number for the table obtained
104332 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
104334 ** pExpr->iColumn Set to the column number within the table.
104335 ** pExpr->op Set to TK_COLUMN.
104336 ** pExpr->pLeft Any expression this points to is deleted
104337 ** pExpr->pRight Any expression this points to is deleted.
104361 sqlite3 *db = pParse->db; /* The database connection */
104366 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
104376 /* Initialize the node to no-match */
104377 pExpr->iTable = -1;
104385 testcase( pNC->ncFlags & NC_PartIdx );
104386 testcase( pNC->ncFlags & NC_IsCheck );
104387 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
104394 for(i=0; i<db->nDb; i++){
104395 assert( db->aDb[i].zDbSName );
104396 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
104397 pSchema = db->aDb[i].pSchema;
104401 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
104404 pSchema = db->aDb[0].pSchema;
104405 zDb = db->aDb[0].zDbSName;
104410 /* Start at the inner-most context and move outward until a match is found */
104414 SrcList *pSrcList = pNC->pSrcList;
104417 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
104419 pTab = pItem->pTab;
104420 assert( pTab!=0 && pTab->zName!=0 );
104421 assert( pTab->nCol>0 || pParse->nErr );
104422 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
104423 if( pItem->fg.isNestedFrom ){
104428 ** This pItem -------------^
104431 assert( pItem->pSelect!=0 );
104432 pEList = pItem->pSelect->pEList;
104434 assert( pEList->nExpr==pTab->nCol );
104435 for(j=0; j<pEList->nExpr; j++){
104436 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
104440 if( pItem->fg.isUsing==0
104441 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104449 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104450 /* An INNER or LEFT JOIN. Use the left-most table */
104453 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104454 /* A RIGHT JOIN. Use the right-most table */
104460 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104466 pExpr->iColumn = j;
104467 pEList->a[j].fg.bUsed = 1;
104469 if( pEList->a[j].fg.bUsingTerm ) break;
104476 if( pTab->pSchema!=pSchema ) continue;
104479 if( pItem->zAlias!=0 ){
104480 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
104483 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
104484 if( pTab->tnum!=1 ) continue;
104488 if( IN_RENAME_OBJECT && pItem->zAlias ){
104489 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
104493 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
104494 if( pCol->hName==hCol
104495 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104498 if( pItem->fg.isUsing==0
104499 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104507 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104508 /* An INNER or LEFT JOIN. Use the left-most table */
104511 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104512 /* A RIGHT JOIN. Use the right-most table */
104518 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104523 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
104524 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
104525 if( pItem->fg.isNestedFrom ){
104537 pExpr->iTable = pMatch->iCursor;
104539 pExpr->y.pTab = pMatch->pTab;
104540 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
104543 pSchema = pExpr->y.pTab->pSchema;
104556 if( pParse->pTriggerTab!=0 ){
104557 int op = pParse->eTriggerOp;
104559 if( pParse->bReturning ){
104560 if( (pNC->ncFlags & NC_UBaseReg)!=0
104562 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
104564 pExpr->iTable = op!=TK_DELETE;
104565 pTab = pParse->pTriggerTab;
104568 pExpr->iTable = 1;
104569 pTab = pParse->pTriggerTab;
104571 pExpr->iTable = 0;
104572 pTab = pParse->pTriggerTab;
104577 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
104578 Upsert *pUpsert = pNC->uNC.pUpsert;
104580 pTab = pUpsert->pUpsertSrc->a[0].pTab;
104581 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
104589 pSchema = pTab->pSchema;
104591 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
104592 if( pCol->hName==hCol
104593 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104595 if( iCol==pTab->iPKey ){
104596 iCol = -1;
104601 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
104602 /* IMP: R-51414-32910 */
104603 iCol = -1;
104605 if( iCol<pTab->nCol ){
104609 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
104610 testcase( iCol==(-1) );
104613 pExpr->iColumn = iCol;
104614 pExpr->y.pTab = pTab;
104617 pExpr->iTable = pNC->uNC.pUpsert->regData +
104625 pExpr->y.pTab = pTab;
104626 if( pParse->bReturning ){
104628 pExpr->op2 = TK_COLUMN;
104629 pExpr->iColumn = iCol;
104630 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
104633 pExpr->iColumn = (i16)iCol;
104637 pExpr->affExpr = SQLITE_AFF_INTEGER;
104638 }else if( pExpr->iTable==0 ){
104641 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104645 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104661 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
104663 && ALWAYS(VisibleRowid(pMatch->pTab))
104666 pExpr->iColumn = -1;
104667 pExpr->affExpr = SQLITE_AFF_INTEGER;
104672 ** might refer to an result-set alias. This happens, for example, when
104682 ** The ability to use an output result-set column in the WHERE, GROUP BY,
104689 && (pNC->ncFlags & NC_UEList)!=0
104692 pEList = pNC->uNC.pEList;
104694 for(j=0; j<pEList->nExpr; j++){
104695 char *zAs = pEList->a[j].zEName;
104696 if( pEList->a[j].fg.eEName==ENAME_NAME
104700 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
104701 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
104702 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
104703 pOrig = pEList->a[j].pExpr;
104704 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
104709 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
104734 pNC = pNC->pNext;
104741 ** supplied) and the value of Z is enclosed in double-quotes, then
104746 ** Because no reference was made to outer contexts, the pNC->nRef
104750 assert( pExpr->op==TK_ID );
104754 /* If a double-quoted identifier does not match any known column name,
104758 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
104769 "double-quoted string literal: \"%w\"", zCol);
104771 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
104773 pExpr->op = TK_STRING;
104774 memset(&pExpr->y, 0, sizeof(pExpr->y));
104794 if( pFJMatch->nExpr==cnt-1 ){
104798 sqlite3ExprDelete(db, pExpr->pLeft);
104799 pExpr->pLeft = 0;
104800 sqlite3ExprDelete(db, pExpr->pRight);
104801 pExpr->pRight = 0;
104803 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104804 pExpr->op = TK_FUNCTION;
104805 pExpr->u.zToken = "coalesce";
104806 pExpr->x.pList = pFJMatch;
104822 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104823 pParse->checkSchema = 1;
104824 pTopNC->nNcErr++;
104830 sqlite3ExprDelete(db, pExpr->pLeft);
104831 pExpr->pLeft = 0;
104832 sqlite3ExprDelete(db, pExpr->pRight);
104833 pExpr->pRight = 0;
104851 if( pExpr->iColumn>=0 && pMatch!=0 ){
104852 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
104855 pExpr->op = eNewExprOp;
104860 if( pParse->db->xAuth
104861 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
104863 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
104870 pTopNC->nRef++;
104872 pTopNC = pTopNC->pNext;
104887 SrcItem *pItem = &pSrc->a[iSrc];
104890 pTab = p->y.pTab = pItem->pTab;
104891 p->iTable = pItem->iCursor;
104892 if( p->y.pTab->iPKey==iCol ){
104893 p->iColumn = -1;
104895 p->iColumn = (ynVar)iCol;
104896 if( (pTab->tabFlags & TF_HasGenerated)!=0
104897 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
104899 testcase( pTab->nCol==63 );
104900 testcase( pTab->nCol==64 );
104901 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
104904 testcase( iCol==BMS-1 );
104905 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
104914 ** pNC->ncFlags values determined by validMask.
104936 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
104938 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
104941 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
104944 if( pExpr ) pExpr->op = TK_NULL;
104945 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
104949 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
104953 ** Return 1024 times this value. Or return -1 if p is not a floating point
104957 double r = -1.0;
104958 if( p->op!=TK_FLOAT ) return -1;
104960 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
104962 if( r>1.0 ) return -1;
104981 pNC = pWalker->u.pNC;
104983 pParse = pNC->pParse;
104984 assert( pParse==pWalker->pParse );
104987 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
104988 SrcList *pSrcList = pNC->pSrcList;
104990 for(i=0; i<pNC->pSrcList->nSrc; i++){
104991 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
104995 switch( pExpr->op ){
105003 SrcList *pSrcList = pNC->pSrcList;
105005 assert( pSrcList && pSrcList->nSrc>=1 );
105006 pItem = pSrcList->a;
105007 pExpr->op = TK_COLUMN;
105009 pExpr->y.pTab = pItem->pTab;
105010 pExpr->iTable = pItem->iCursor;
105011 pExpr->iColumn--;
105012 pExpr->affExpr = SQLITE_AFF_INTEGER;
105018 ** "expr IS NOT NULL" --> "TRUE"
105019 ** "expr IS NULL" --> "FALSE"
105024 ** If this optimization occurs, also restore the NameContext ref-counts
105035 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
105036 anRef[i] = p->nRef;
105038 sqlite3WalkExpr(pWalker, pExpr->pLeft);
105039 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
105042 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
105043 pExpr->flags |= EP_IntValue;
105044 pExpr->op = TK_INTEGER;
105046 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
105047 p->nRef = anRef[i];
105049 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
105050 pExpr->pLeft = 0;
105060 ** be one call to lookupName(). Then the compiler will in-line
105070 if( pExpr->op==TK_ID ){
105074 zColumn = pExpr->u.zToken;
105076 Expr *pLeft = pExpr->pLeft;
105077 testcase( pNC->ncFlags & NC_IdxExpr );
105078 testcase( pNC->ncFlags & NC_GenCol );
105081 pRight = pExpr->pRight;
105082 if( pRight->op==TK_ID ){
105085 assert( pRight->op==TK_DOT );
105087 zDb = pLeft->u.zToken;
105088 pLeft = pRight->pLeft;
105089 pRight = pRight->pRight;
105092 zTable = pLeft->u.zToken;
105093 zColumn = pRight->u.zToken;
105097 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
105106 ExprList *pList = pExpr->x.pList; /* The argument list */
105107 int n = pList ? pList->nExpr : 0; /* Number of arguments */
105113 u8 enc = ENC(pParse->db); /* The database encoding */
105114 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
105116 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
105119 zId = pExpr->u.zToken;
105120 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
105122 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
105129 is_agg = pDef->xFinalize!=0;
105130 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
105133 pExpr->iTable = exprProbability(pList->a[1].pExpr);
105134 if( pExpr->iTable<0 ){
105138 pNC->nNcErr++;
105141 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
105143 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
105144 ** short-hand for likelihood(X,0.0625).
105145 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
105147 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
105150 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
105155 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
105160 pNC->nNcErr++;
105162 pExpr->op = TK_NULL;
105167 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
105174 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
105175 /* Clearly non-deterministic functions like random(), but also
105181 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
105185 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
105186 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
105188 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
105189 && pParse->nested==0
105190 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
105192 /* Internal-use-only functions are disallowed unless the
105194 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
105199 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
105208 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
105209 || (pDef->xValue==0 && pDef->xInverse==0)
105210 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
105212 if( pDef && pDef->xValue==0 && pWin ){
105216 pNC->nNcErr++;
105218 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
105219 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
105220 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
105223 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
105229 pNC->nNcErr++;
105233 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
105235 pNC->nNcErr++;
105239 else if( no_such_func && pParse->db->init.busy==0
105241 && pParse->explain==0
105245 pNC->nNcErr++;
105249 pNC->nNcErr++;
105254 "FILTER may not be used with non-aggregate %#T()",
105257 pNC->nNcErr++;
105265 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
105267 pNC->ncFlags &= ~NC_AllowAgg;
105280 Select *pSel = pNC->pWinSelect;
105281 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
105283 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
105284 if( pParse->db->mallocFailed ) break;
105286 sqlite3WalkExprList(pWalker, pWin->pPartition);
105287 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
105288 sqlite3WalkExpr(pWalker, pWin->pFilter);
105290 pNC->ncFlags |= NC_HasWin;
105295 pExpr->op = TK_AGG_FUNCTION;
105296 pExpr->op2 = 0;
105299 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
105304 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
105306 pExpr->op2++;
105307 pNC2 = pNC2->pNext;
105313 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
105314 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
105315 pNC2->ncFlags |= NC_HasAgg
105316 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
105320 pNC->ncFlags |= savedAllowFlags;
105322 /* FIX ME: Compute pExpr->affinity based on the expected return
105329 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
105332 testcase( pExpr->op==TK_IN );
105334 int nRef = pNC->nRef;
105335 testcase( pNC->ncFlags & NC_IsCheck );
105336 testcase( pNC->ncFlags & NC_PartIdx );
105337 testcase( pNC->ncFlags & NC_IdxExpr );
105338 testcase( pNC->ncFlags & NC_GenCol );
105339 if( pNC->ncFlags & NC_SelfRef ){
105342 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
105344 assert( pNC->nRef>=nRef );
105345 if( nRef!=pNC->nRef ){
105348 pNC->ncFlags |= NC_Subquery;
105353 testcase( pNC->ncFlags & NC_IsCheck );
105354 testcase( pNC->ncFlags & NC_PartIdx );
105355 testcase( pNC->ncFlags & NC_IdxExpr );
105356 testcase( pNC->ncFlags & NC_GenCol );
105363 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
105367 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
105370 if( pRight->op==TK_TRUEFALSE ){
105371 pExpr->op2 = pExpr->op;
105372 pExpr->op = TK_TRUTH;
105386 if( pParse->db->mallocFailed ) break;
105387 assert( pExpr->pLeft!=0 );
105388 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
105389 if( pExpr->op==TK_BETWEEN ){
105391 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
105393 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
105396 assert( pExpr->pRight!=0 );
105397 nRight = sqlite3ExprVectorSize(pExpr->pRight);
105400 testcase( pExpr->op==TK_EQ );
105401 testcase( pExpr->op==TK_NE );
105402 testcase( pExpr->op==TK_LT );
105403 testcase( pExpr->op==TK_LE );
105404 testcase( pExpr->op==TK_GT );
105405 testcase( pExpr->op==TK_GE );
105406 testcase( pExpr->op==TK_IS );
105407 testcase( pExpr->op==TK_ISNOT );
105408 testcase( pExpr->op==TK_BETWEEN );
105410 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
105415 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
105416 return pParse->nErr ? WRC_Abort : WRC_Continue;
105423 ** to the AS-name of one of the terms of the expression list. If it is,
105440 if( pE->op==TK_ID ){
105443 zCol = pE->u.zToken;
105444 for(i=0; i<pEList->nExpr; i++){
105445 if( pEList->a[i].fg.eEName==ENAME_NAME
105446 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
105464 ** Attempt to match pE against result set columns in the left-most
105466 ** as an indication to the caller that it should sort by the i-th column.
105467 ** The left-most column is 1. In other words, the value returned is the
105471 ** If there is no match, return 0. Return -1 if an error occurs.
105483 u8 savedSuppErr; /* Saved value of db->suppressErr */
105486 pEList = pSelect->pEList;
105492 nc.pSrcList = pSelect->pSrc;
105496 db = pParse->db;
105497 savedSuppErr = db->suppressErr;
105498 db->suppressErr = 1;
105500 db->suppressErr = savedSuppErr;
105504 ** in the result set. Return an 1-based index of the matching
105505 ** result-set entry.
105507 for(i=0; i<pEList->nExpr; i++){
105508 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
105518 ** Generate an ORDER BY or GROUP BY term out-of-range error.
105523 int i, /* The index (1-based) of the term out of range */
105528 "%r %s BY term out of range - should be "
105530 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
105542 ** beginning with the left-most SELECT and working toward the right.
105558 pOrderBy = pSelect->pOrderBy;
105560 db = pParse->db;
105561 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105565 for(i=0; i<pOrderBy->nExpr; i++){
105566 pOrderBy->a[i].fg.done = 0;
105568 pSelect->pNext = 0;
105569 while( pSelect->pPrior ){
105570 pSelect->pPrior->pNext = pSelect;
105571 pSelect = pSelect->pPrior;
105576 pEList = pSelect->pEList;
105578 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105579 int iCol = -1;
105581 if( pItem->fg.done ) continue;
105582 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
105585 if( iCol<=0 || iCol>pEList->nExpr ){
105586 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
105604 if( !db->mallocFailed ){
105620 pNew->flags |= EP_IntValue;
105621 pNew->u.iValue = iCol;
105622 if( pItem->pExpr==pE ){
105623 pItem->pExpr = pNew;
105625 Expr *pParent = pItem->pExpr;
105626 assert( pParent->op==TK_COLLATE );
105627 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
105628 assert( pParent->pLeft==pE );
105629 pParent->pLeft = pNew;
105632 pItem->u.x.iOrderByCol = (u16)iCol;
105634 pItem->fg.done = 1;
105639 pSelect = pSelect->pNext;
105641 for(i=0; i<pOrderBy->nExpr; i++){
105642 if( pOrderBy->a[i].fg.done==0 ){
105659 ** return non-zero. Return zero if no errors are seen.
105668 sqlite3 *db = pParse->db;
105672 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
105673 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105677 pEList = pSelect->pEList;
105679 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105680 if( pItem->u.x.iOrderByCol ){
105681 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
105682 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
105685 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
105698 Window *pWin = pExpr->y.pWin;
105709 if( pSelect->pWin ){
105727 ** If the order-by term is an integer I between 1 and N (where N is the
105729 ** in the resolution is a copy of the I-th result-set expression. If
105730 ** the order-by term is an identifier that corresponds to the AS-name of
105731 ** a result-set expression, then the term resolves to a copy of the
105732 ** result-set expression. Otherwise, the expression is resolved in
105733 ** the usual way - using sqlite3ResolveExprNames().
105752 nResult = pSelect->pEList->nExpr;
105753 pParse = pNC->pParse;
105754 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105755 Expr *pE = pItem->pExpr;
105759 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
105761 /* If an AS-name match is found, mark this ORDER BY column as being
105762 ** a copy of the iCol-th result-set column. The subsequent call to
105764 ** copy of the iCol-th result-set expression. */
105765 pItem->u.x.iOrderByCol = (u16)iCol;
105772 ** order-by term to a copy of the result-set expression */
105777 pItem->u.x.iOrderByCol = (u16)iCol;
105782 pItem->u.x.iOrderByCol = 0;
105786 for(j=0; j<pSelect->pEList->nExpr; j++){
105787 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
105792 pItem->u.x.iOrderByCol = j+1;
105810 Select *pLeftmost; /* Left-most of SELECT of a compound */
105815 if( p->selFlags & SF_Resolved ){
105818 pOuterNC = pWalker->u.pNC;
105819 pParse = pWalker->pParse;
105820 db = pParse->db;
105830 if( (p->selFlags & SF_Expanded)==0 ){
105832 return pParse->nErr ? WRC_Abort : WRC_Prune;
105835 isCompound = p->pPrior!=0;
105839 assert( (p->selFlags & SF_Expanded)!=0 );
105840 assert( (p->selFlags & SF_Resolved)==0 );
105841 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
105842 p->selFlags |= SF_Resolved;
105851 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
105857 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
105858 ** as if it were part of the sub-query, not the parent. This block
105859 ** moves the pOrderBy down to the sub-query. It will be moved back
105861 if( p->selFlags & SF_Converted ){
105862 Select *pSub = p->pSrc->a[0].pSelect;
105863 assert( p->pSrc->nSrc==1 && p->pOrderBy );
105864 assert( pSub->pPrior && pSub->pOrderBy==0 );
105865 pSub->pOrderBy = p->pOrderBy;
105866 p->pOrderBy = 0;
105871 for(i=0; i<p->pSrc->nSrc; i++){
105872 SrcItem *pItem = &p->pSrc->a[i];
105873 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
105874 int nRef = pOuterNC ? pOuterNC->nRef : 0;
105875 const char *zSavedContext = pParse->zAuthContext;
105877 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
105878 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
105879 pParse->zAuthContext = zSavedContext;
105880 if( pParse->nErr ) return WRC_Abort;
105881 assert( db->mallocFailed==0 );
105884 ** expressions in the sub-select were resolved, the sub-select
105890 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
105891 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
105896 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
105897 ** resolve the result-set expression list.
105900 sNC.pSrcList = p->pSrc;
105904 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
105907 /* If there are no aggregate functions in the result-set, and no GROUP BY
105910 assert( (p->selFlags & SF_Aggregate)==0 );
105911 pGroupBy = p->pGroupBy;
105915 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
105920 /* Add the output column list to the name-context before parsing the
105926 ** re-evaluated for each reference to it.
105929 sNC.uNC.pEList = p->pEList;
105931 if( p->pHaving ){
105932 if( (p->selFlags & SF_Aggregate)==0 ){
105933 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
105936 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
105938 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
105940 /* Resolve names in table-valued-function arguments */
105941 for(i=0; i<p->pSrc->nSrc; i++){
105942 SrcItem *pItem = &p->pSrc->a[i];
105943 if( pItem->fg.isTabFunc
105944 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
105953 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
105954 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
105955 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
105970 ** the sub-query back to the parent query. At this point each term
105974 if( p->selFlags & SF_Converted ){
105975 Select *pSub = p->pSrc->a[0].pSelect;
105976 p->pOrderBy = pSub->pOrderBy;
105977 pSub->pOrderBy = 0;
105982 ** below, after all of the result-sets for all of the elements of
105985 ** If there is an ORDER BY clause on a term of a compound-select other
105986 ** than the right-most term, then that is a syntax error. But the error
105990 if( p->pOrderBy!=0
105991 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
105992 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
105996 if( db->mallocFailed ){
106007 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
106010 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
106011 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
106021 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
106022 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
106028 p = p->pPrior;
106044 ** table columns and result-set columns. At the same time, do error
106052 ** the symbolic name assigned to an ATTACH-ed database.
106063 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
106067 ** To resolve result-set references, look for expression nodes of the
106068 ** form Z (with no X and Y prefix) where the Z matches the right-hand
106069 ** size of an AS clause in the result-set of a SELECT. The Z expression
106070 ** is replaced by a copy of the left-hand side of the result-set expression.
106071 ** Table-name and function resolution occurs on the substituted expression
106098 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106099 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106100 w.pParse = pNC->pParse;
106102 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
106106 w.pParse->nHeight += pExpr->nHeight;
106107 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
106113 w.pParse->nHeight -= pExpr->nHeight;
106117 testcase( pNC->ncFlags & NC_HasAgg );
106118 testcase( pNC->ncFlags & NC_HasWin );
106119 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
106120 pNC->ncFlags |= savedHasAgg;
106121 return pNC->nNcErr>0 || w.pParse->nErr>0;
106137 w.pParse = pNC->pParse;
106142 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106143 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106144 for(i=0; i<pList->nExpr; i++){
106145 Expr *pExpr = pList->a[i].pExpr;
106148 w.pParse->nHeight += pExpr->nHeight;
106149 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
106155 w.pParse->nHeight -= pExpr->nHeight;
106159 testcase( pNC->ncFlags & NC_HasAgg );
106160 testcase( pNC->ncFlags & NC_HasWin );
106161 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
106162 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
106163 savedHasAgg |= pNC->ncFlags &
106165 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106167 if( w.pParse->nErr>0 ) return WRC_Abort;
106169 pNC->ncFlags |= savedHasAgg;
106175 ** decendents of the SELECT, including compounds off of p->pPrior,
106206 ** ------------
106214 ** nodes of the expression is set to -1 and the Expr.iColumn value is
106226 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
106227 NameContext sNC; /* Name context for pParse->pNewTable */
106237 sSrc.a[0].zName = pTab->zName;
106239 sSrc.a[0].iCursor = -1;
106240 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
106241 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
106280 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
106281 return pTab->aCol[iCol].affinity;
106288 ** or a sub-select with a column as the return value, then the
106302 op = pExpr->op;
106303 while( 1 /* exit-by-break */ ){
106304 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
106306 assert( pExpr->y.pTab!=0 );
106307 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
106311 assert( pExpr->x.pSelect!=0 );
106312 assert( pExpr->x.pSelect->pEList!=0 );
106313 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
106314 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
106319 return sqlite3AffinityType(pExpr->u.zToken, 0);
106323 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
106324 assert( pExpr->iColumn < pExpr->iTable );
106325 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
106327 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
106332 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
106335 assert( pExpr->op==TK_COLLATE
106336 || pExpr->op==TK_IF_NULL_ROW
106337 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
106338 pExpr = pExpr->pLeft;
106339 op = pExpr->op;
106342 if( op!=TK_REGISTER || (op = pExpr->op2)==TK_REGISTER ) break;
106344 return pExpr->affExpr;
106359 switch( pExpr->op ){
106363 pExpr = pExpr->pLeft;
106398 ExprList *pList = pExpr->x.pList;
106400 assert( pList->nExpr > 0);
106401 for(ii=1; ii<pList->nExpr; ii+=2){
106402 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
106404 if( pList->nExpr % 2 ){
106405 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
106422 ** If a memory allocation error occurs, that fact is recorded in pParse->db
106431 if( pCollName->n>0 ){
106432 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
106434 pNew->pLeft = pExpr;
106435 pNew->flags |= EP_Collate|EP_Skip;
106457 assert( pExpr->op==TK_COLLATE );
106458 pExpr = pExpr->pLeft;
106472 assert( pExpr->x.pList->nExpr>0 );
106473 assert( pExpr->op==TK_FUNCTION );
106474 pExpr = pExpr->x.pList->a[0].pExpr;
106476 assert( pExpr->op==TK_COLLATE );
106477 pExpr = pExpr->pLeft;
106498 sqlite3 *db = pParse->db;
106502 int op = p->op;
106503 if( op==TK_REGISTER ) op = p->op2;
106504 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
106509 assert( p->y.pTab!=0 );
106510 if( (j = p->iColumn)>=0 ){
106511 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
106517 p = p->pLeft;
106522 p = p->x.pList->a[0].pExpr;
106527 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
106530 if( p->flags & EP_Collate ){
106531 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
106532 p = p->pLeft;
106534 Expr *pNext = p->pRight;
106536 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
106537 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
106539 for(i=0; i<p->x.pList->nExpr; i++){
106540 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
106541 pNext = p->x.pList->a[i].pExpr;
106570 if( p==0 ) p = pParse->db->pDfltColl;
106581 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
106613 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
106614 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
106615 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
106616 assert( pExpr->pLeft );
106617 aff = sqlite3ExprAffinity(pExpr->pLeft);
106618 if( pExpr->pRight ){
106619 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
106621 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
106678 if( pLeft->flags & EP_Collate ){
106680 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
106701 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
106703 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
106724 if( pParse->nErr ) return 0;
106731 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
106733 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
106753 ** is a sub-select, return the number of columns in the sub-select. For
106757 u8 op = pExpr->op;
106758 if( op==TK_REGISTER ) op = pExpr->op2;
106761 return pExpr->x.pList->nExpr;
106764 return pExpr->x.pSelect->pEList->nExpr;
106771 ** Return a pointer to a subexpression of pVector that is the i-th
106781 ** just the expression for the i-th term of the result set, and may
106786 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
106788 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
106789 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
106791 return pVector->x.pSelect->pEList->a[i].pExpr;
106794 return pVector->x.pList->a[i].pExpr;
106803 ** the iField-th column of the vector expression pVector.
106823 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
106828 if( pVector->op==TK_SELECT ){
106836 ** pLeft->iTable: First in an array of register holding result, or 0
106848 pRet->iTable = nField;
106849 pRet->iColumn = iField;
106850 pRet->pLeft = pVector;
106853 if( pVector->op==TK_VECTOR ){
106856 ppVector = &pVector->x.pList->a[iField].pExpr;
106864 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
106872 ** sub-select returns more than one column, the first in an array
106880 if( pExpr->op==TK_SELECT ){
106888 ** Argument pVector points to a vector expression - either a TK_VECTOR
106896 ** containing the results of the sub-select.
106913 u8 op = pVector->op;
106917 return pVector->iTable+iField;
106921 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
106926 *ppExpr = pVector->x.pList->a[iField].pExpr;
106939 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
106940 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
106941 ** otherwise: op==pExpr->op and p5==0
106950 Vdbe *v = pParse->pVdbe;
106951 Expr *pLeft = pExpr->pLeft;
106952 Expr *pRight = pExpr->pRight;
106963 if( pParse->nErr ) return;
106968 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
106969 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
106970 || pExpr->op==TK_LT || pExpr->op==TK_GT
106971 || pExpr->op==TK_LE || pExpr->op==TK_GE
106973 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
106974 || (pExpr->op==TK_ISNOT && op==TK_NE) );
106975 assert( p5==0 || pExpr->op!=op );
106976 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
107004 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
107014 if( i==nLeft-1 ){
107022 if( i==nLeft-2 ) opx = op;
107040 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
107061 if( p->nHeight>*pnHeight ){
107062 *pnHeight = p->nHeight;
107069 for(i=0; i<p->nExpr; i++){
107070 heightOfExpr(p->a[i].pExpr, pnHeight);
107076 for(p=pSelect; p; p=p->pPrior){
107077 heightOfExpr(p->pWhere, pnHeight);
107078 heightOfExpr(p->pHaving, pnHeight);
107079 heightOfExpr(p->pLimit, pnHeight);
107080 heightOfExprList(p->pEList, pnHeight);
107081 heightOfExprList(p->pGroupBy, pnHeight);
107082 heightOfExprList(p->pOrderBy, pnHeight);
107097 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
107098 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
107099 nHeight = p->pRight->nHeight;
107102 heightOfSelect(p->x.pSelect, &nHeight);
107103 }else if( p->x.pList ){
107104 heightOfExprList(p->x.pList, &nHeight);
107105 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
107107 p->nHeight = nHeight + 1;
107119 if( pParse->nErr ) return;
107121 sqlite3ExprCheckHeight(pParse, p->nHeight);
107139 if( pParse->nErr ) return;
107140 if( p && ExprUseXList(p) && p->x.pList ){
107141 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
107158 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
107162 ** can be translated into a 32-bit integer, then the token is not
107179 if( op!=TK_INTEGER || pToken->z==0
107180 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
107181 nExtra = pToken->n+1;
107188 pNew->op = (u8)op;
107189 pNew->iAgg = -1;
107192 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
107193 pNew->u.iValue = iValue;
107195 pNew->u.zToken = (char*)&pNew[1];
107196 assert( pToken->z!=0 || pToken->n==0 );
107197 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
107198 pNew->u.zToken[pToken->n] = 0;
107199 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
107205 pNew->nHeight = 1;
107212 ** Allocate a new expression node from a zero-terminated token that has
107239 assert( db->mallocFailed );
107244 assert( pRoot->x.pSelect==0 );
107246 pRoot->pRight = pRight;
107247 pRoot->flags |= EP_Propagate & pRight->flags;
107249 pRoot->nHeight = pRight->nHeight+1;
107251 pRoot->nHeight = 1;
107255 pRoot->pLeft = pLeft;
107256 pRoot->flags |= EP_Propagate & pLeft->flags;
107258 if( pLeft->nHeight>=pRoot->nHeight ){
107259 pRoot->nHeight = pLeft->nHeight+1;
107270 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
107280 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
107283 p->op = op & 0xff;
107284 p->iAgg = -1;
107285 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
107286 sqlite3ExprCheckHeight(pParse, p->nHeight);
107288 sqlite3ExprDelete(pParse->db, pLeft);
107289 sqlite3ExprDelete(pParse->db, pRight);
107300 pExpr->x.pSelect = pSelect;
107304 assert( pParse->db->mallocFailed );
107305 sqlite3SelectDelete(pParse->db, pSelect);
107332 for(ii=0; ii<pEList->nExpr; ii++){
107334 Expr *pExpr = pEList->a[ii].pExpr;
107336 if( pExpr->op==TK_VECTOR ){
107338 nExprElem = pExpr->x.pList->nExpr;
107343 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
107349 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
107350 pExpr->x.pList = 0;
107353 pSel->op = TK_ALL;
107354 pSel->pPrior = pRet;
107360 if( pRet && pRet->pPrior ){
107361 pRet->selFlags |= SF_MultiValue;
107363 sqlite3ExprListDelete(pParse->db, pEList);
107376 sqlite3 *db = pParse->db;
107382 u32 f = pLeft->flags | pRight->flags;
107406 sqlite3 *db = pParse->db;
107414 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
107416 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
107417 && !pParse->nested
107421 pNew->x.pList = pList;
107433 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
107435 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
107436 ** top-level SQL
107446 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
107448 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
107449 || (pParse->db->flags & SQLITE_TrustedSchema)==0
107480 sqlite3 *db = pParse->db;
107486 z = pExpr->u.zToken;
107493 x = (ynVar)(++pParse->nVar);
107501 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
107502 i = z[1]-'0'; /* The common case of ?N for a single digit N */
107505 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
107509 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
107510 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
107511 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
107513 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
107514 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107518 if( x>pParse->nVar ){
107519 pParse->nVar = (int)x;
107521 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
107529 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
107531 x = (ynVar)(++pParse->nVar);
107536 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
107539 pExpr->iColumn = x;
107540 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
107542 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107552 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
107554 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
107555 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
107558 assert( p->pLeft==0 );
107559 assert( p->pRight==0 );
107560 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
107561 assert( !ExprUseXList(p) || p->x.pList==0 );
107566 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
107567 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
107568 if( p->pRight ){
107570 sqlite3ExprDeleteNN(db, p->pRight);
107573 sqlite3SelectDelete(db, p->x.pSelect);
107575 sqlite3ExprListDelete(db, p->x.pList);
107578 sqlite3WindowDelete(db, p->y.pWin);
107597 }else if( p->pOn ){
107598 sqlite3ExprDeleteNN(db, p->pOn);
107599 }else if( p->pUsing ){
107600 sqlite3IdListDelete(db, p->pUsing);
107612 ** pExpr to the pParse->pConstExpr list with a register number of 0.
107628 sqlite3ExprDeleteNN(pParse->db, p);
107654 ** The dupedExprStructSize() function returns two values OR-ed together:
107667 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
107682 if( 0==flags || p->op==TK_SELECT_COLUMN
107692 if( p->pLeft || p->x.pList ){
107695 assert( p->pRight==0 );
107709 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107710 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
107733 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
107742 ** to store the copy of expression p, the copies of p->u.zToken
107743 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
107772 ** by the copy of the p->u.zToken string (if any).
107777 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107778 nToken = sqlite3Strlen30(p->u.zToken) + 1;
107789 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
107794 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
107795 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
107796 pNew->flags |= staticFlag;
107802 /* Copy the p->u.zToken string, if any. */
107804 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
107805 memcpy(zToken, p->u.zToken, nToken);
107808 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
107809 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
107811 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
107813 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
107817 /* Fill in pNew->pLeft and pNew->pRight. */
107821 pNew->pLeft = p->pLeft ?
107822 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
107823 pNew->pRight = p->pRight ?
107824 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
107828 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
107837 if( pNew->op==TK_SELECT_COLUMN ){
107838 pNew->pLeft = p->pLeft;
107839 assert( p->pRight==0 || p->pRight==p->pLeft
107840 || ExprHasProperty(p->pLeft, EP_Subquery) );
107842 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
107844 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
107854 ** and the db->mallocFailed flag set.
107860 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
107864 pRet->nCte = p->nCte;
107865 for(i=0; i<p->nCte; i++){
107866 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
107867 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
107868 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
107869 pRet->a[i].eM10d = p->a[i].eM10d;
107884 ** objects found there, assembling them onto the linked list at Select->pWin.
107887 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
107888 Select *pSelect = pWalker->u.pSelect;
107889 Window *pWin = pExpr->y.pWin;
107892 assert( pWin->ppThis==0 );
107898 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
107927 ** part of the in-memory representation of the database schema.
107944 pNew->nExpr = p->nExpr;
107945 pNew->nAlloc = p->nAlloc;
107946 pItem = pNew->a;
107947 pOldItem = p->a;
107948 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
107949 Expr *pOldExpr = pOldItem->pExpr;
107951 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
107953 && pOldExpr->op==TK_SELECT_COLUMN
107954 && (pNewExpr = pItem->pExpr)!=0
107956 if( pNewExpr->pRight ){
107957 pPriorSelectColOld = pOldExpr->pRight;
107958 pPriorSelectColNew = pNewExpr->pRight;
107959 pNewExpr->pLeft = pNewExpr->pRight;
107961 if( pOldExpr->pLeft!=pPriorSelectColOld ){
107962 pPriorSelectColOld = pOldExpr->pLeft;
107964 pNewExpr->pRight = pPriorSelectColNew;
107966 pNewExpr->pLeft = pPriorSelectColNew;
107969 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
107970 pItem->fg = pOldItem->fg;
107971 pItem->fg.done = 0;
107972 pItem->u = pOldItem->u;
107991 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
107994 pNew->nSrc = pNew->nAlloc = p->nSrc;
107995 for(i=0; i<p->nSrc; i++){
107996 SrcItem *pNewItem = &pNew->a[i];
107997 const SrcItem *pOldItem = &p->a[i];
107999 pNewItem->pSchema = pOldItem->pSchema;
108000 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
108001 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108002 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
108003 pNewItem->fg = pOldItem->fg;
108004 pNewItem->iCursor = pOldItem->iCursor;
108005 pNewItem->addrFillSub = pOldItem->addrFillSub;
108006 pNewItem->regReturn = pOldItem->regReturn;
108007 if( pNewItem->fg.isIndexedBy ){
108008 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
108010 pNewItem->u2 = pOldItem->u2;
108011 if( pNewItem->fg.isCte ){
108012 pNewItem->u2.pCteUse->nUse++;
108014 if( pNewItem->fg.isTabFunc ){
108015 pNewItem->u1.pFuncArg =
108016 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
108018 pTab = pNewItem->pTab = pOldItem->pTab;
108020 pTab->nTabRef++;
108022 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
108023 if( pOldItem->fg.isUsing ){
108024 assert( pNewItem->fg.isUsing );
108025 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
108027 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
108029 pNewItem->colUsed = pOldItem->colUsed;
108038 assert( p->eU4!=EU4_EXPR );
108039 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
108041 pNew->nId = p->nId;
108042 pNew->eU4 = p->eU4;
108043 for(i=0; i<p->nId; i++){
108044 struct IdList_item *pNewItem = &pNew->a[i];
108045 const struct IdList_item *pOldItem = &p->a[i];
108046 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108047 pNewItem->u4 = pOldItem->u4;
108058 for(p=pDup; p; p=p->pPrior){
108061 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
108062 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
108063 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
108064 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
108065 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
108066 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
108067 pNew->op = p->op;
108068 pNew->pNext = pNext;
108069 pNew->pPrior = 0;
108070 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
108071 pNew->iLimit = 0;
108072 pNew->iOffset = 0;
108073 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
108074 pNew->addrOpenEphm[0] = -1;
108075 pNew->addrOpenEphm[1] = -1;
108076 pNew->nSelectRow = p->nSelectRow;
108077 pNew->pWith = sqlite3WithDup(db, p->pWith);
108079 pNew->pWin = 0;
108080 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
108081 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
108083 pNew->selId = p->selId;
108084 if( db->mallocFailed ){
108088 pNew->pNext = 0;
108093 pp = &pNew->pPrior;
108114 ** Reason: This routine assumes that the number of slots in pList->a[]
108119 ** NULL is returned. If non-NULL is returned, then it is guaranteed
108130 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
108135 pList->nAlloc = 4;
108136 pList->nExpr = 1;
108137 pItem = &pList->a[0];
108139 pItem->pExpr = pExpr;
108149 pList->nAlloc *= 2;
108151 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
108159 pItem = &pList->a[pList->nExpr++];
108161 pItem->pExpr = pExpr;
108171 return sqlite3ExprListAppendNew(pParse->db,pExpr);
108173 if( pList->nAlloc<pList->nExpr+1 ){
108174 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
108176 pItem = &pList->a[pList->nExpr++];
108178 pItem->pExpr = pExpr;
108199 sqlite3 *db = pParse->db;
108202 int iFirst = pList ? pList->nExpr : 0;
108213 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
108215 pColumns->nId, n);
108219 for(i=0; i<pColumns->nId; i++){
108220 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
108221 assert( pSubExpr!=0 || db->mallocFailed );
108225 assert( pList->nExpr==iFirst+i+1 );
108226 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
108227 pColumns->a[i].zName = 0;
108231 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
108232 Expr *pFirst = pList->a[iFirst].pExpr;
108234 assert( pFirst->op==TK_SELECT_COLUMN );
108238 pFirst->pRight = pExpr;
108243 pFirst->iTable = pColumns->nId;
108258 assert( p->nExpr>0 );
108270 pItem = &p->a[p->nExpr-1];
108271 assert( pItem->fg.bNulls==0 );
108275 pItem->fg.sortFlags = (u8)iSortOrder;
108278 pItem->fg.bNulls = 1;
108280 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
108290 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
108299 assert( pList!=0 || pParse->db->mallocFailed!=0 );
108300 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
108303 assert( pList->nExpr>0 );
108304 pItem = &pList->a[pList->nExpr-1];
108305 assert( pItem->zEName==0 );
108306 assert( pItem->fg.eEName==ENAME_NAME );
108307 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
108309 /* If dequote==0, then pName->z does not point to part of a DDL
108311 ** to the token-map. */
108312 sqlite3Dequote(pItem->zEName);
108314 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
108325 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
108334 sqlite3 *db = pParse->db;
108335 assert( pList!=0 || db->mallocFailed!=0 );
108337 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
108338 assert( pList->nExpr>0 );
108339 if( pItem->zEName==0 ){
108340 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
108341 pItem->fg.eEName = ENAME_SPAN;
108355 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
108356 testcase( pEList && pEList->nExpr==mx );
108357 testcase( pEList && pEList->nExpr==mx+1 );
108358 if( pEList && pEList->nExpr>mx ){
108367 int i = pList->nExpr;
108368 struct ExprList_item *pItem = pList->a;
108369 assert( pList->nExpr>0 );
108372 sqlite3ExprDelete(db, pItem->pExpr);
108373 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
108375 }while( --i>0 );
108383 ** Return the bitwise-OR of all Expr.flags fields in the given
108390 for(i=0; i<pList->nExpr; i++){
108391 Expr *pExpr = pList->a[i].pExpr;
108393 m |= pExpr->flags;
108399 ** This is a SELECT-node callback for the expression walker that
108401 ** pWalker->eCode to zero and abort.
108407 pWalker->eCode = 0;
108428 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
108433 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
108435 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
108437 pExpr->op = TK_TRUEFALSE;
108450 assert( pExpr->op==TK_TRUEFALSE );
108452 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
108453 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
108454 return pExpr->u.zToken[4]==0;
108472 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
108473 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
108474 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
108476 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
108478 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
108493 ** sqlite3ExprIsConstant() pWalker->eCode==1
108494 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
108495 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
108496 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
108513 /* If pWalker->eCode is 2 then any term of the expression that comes from
108516 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
108517 pWalker->eCode = 0;
108521 switch( pExpr->op ){
108523 ** and either pWalker->eCode==4 or 5 or the function has the
108526 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
108529 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
108532 pWalker->eCode = 0;
108545 testcase( pExpr->op==TK_ID );
108546 testcase( pExpr->op==TK_COLUMN );
108547 testcase( pExpr->op==TK_AGG_FUNCTION );
108548 testcase( pExpr->op==TK_AGG_COLUMN );
108549 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
108552 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
108559 testcase( pExpr->op==TK_REGISTER );
108560 testcase( pExpr->op==TK_IF_NULL_ROW );
108561 testcase( pExpr->op==TK_DOT );
108562 pWalker->eCode = 0;
108565 if( pWalker->eCode==5 ){
108569 pExpr->op = TK_NULL;
108570 }else if( pWalker->eCode==4 ){
108573 pWalker->eCode = 0;
108578 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
108579 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
108597 ** Walk an expression tree. Return non-zero if the expression is constant
108600 ** For the purposes of this function, a double-quoted string (ex: "abc")
108601 ** is considered a variable but a single-quoted string (ex: 'abc') is
108609 ** Walk an expression tree. Return non-zero if
108618 ** can be added to the pParse->pConstExpr list and evaluated once when
108626 ** Walk an expression tree. Return non-zero if the expression is constant
108628 ** expression must not refer to any non-deterministic function nor any
108637 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
108639 ** sources anywhere else in the query. Return true (non-zero) if pExpr
108646 ** To be an single-source constraint, the following must be true:
108648 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
108650 ** (2) pExpr cannot use subqueries or non-deterministic functions.
108670 ** Without this restriction, accepting pExpr as a single-table
108674 ** on push-down.
108681 const SrcItem *pSrc = &pSrcList->a[iSrc];
108682 if( pSrc->fg.jointype & JT_LTORJ ){
108685 if( pSrc->fg.jointype & JT_LEFT ){
108687 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
108692 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
108696 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
108697 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
108704 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
108712 ExprList *pGroupBy = pWalker->u.pGroupBy;
108717 for(i=0; i<pGroupBy->nExpr; i++){
108718 Expr *p = pGroupBy->a[i].pExpr;
108719 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
108720 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
108727 /* Check if pExpr is a sub-select. If so, consider it variable. */
108729 pWalker->eCode = 0;
108737 ** Walk the expression tree passed as the first argument. Return non-zero
108745 ** assumes that no other collating sequence will have a finer-grained
108768 ** in a CREATE TABLE statement. Return non-zero if the expression is
108769 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
108783 ** For the purposes of this function, a double-quoted string (ex: "abc")
108784 ** is considered a variable but a single-quoted string (ex: 'abc') is
108812 ** to fit in a 32-bit integer, return 1 and put the value of the integer
108814 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
108820 /* If an expression is an integer literal that fits in a signed 32-bit
108822 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
108823 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
108825 if( p->flags & EP_IntValue ){
108826 *pValue = p->u.iValue;
108829 switch( p->op ){
108831 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
108836 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
108838 *pValue = -v;
108865 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108866 p = p->pLeft;
108869 op = p->op;
108870 if( op==TK_REGISTER ) op = p->op2;
108880 p->y.pTab==0 || /* Reference to column of index on expression */
108881 (p->iColumn>=0
108882 && p->y.pTab->aCol!=0 /* Possible due to prior error */
108883 && p->y.pTab->aCol[p->iColumn].notNull==0);
108903 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108904 if( p->op==TK_UMINUS ) unaryMinus = 1;
108905 p = p->pLeft;
108907 op = p->op;
108908 if( op==TK_REGISTER ) op = p->op2;
108923 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
108924 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
108933 ** Return TRUE if the given string is a row-id column name.
108958 p = pX->x.pSelect;
108959 if( p->pPrior ) return 0; /* Not a compound SELECT */
108960 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
108961 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
108962 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
108965 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
108966 if( p->pLimit ) return 0; /* Has no LIMIT clause */
108967 if( p->pWhere ) return 0; /* Has no WHERE clause */
108968 pSrc = p->pSrc;
108970 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
108971 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
108972 pTab = pSrc->a[0].pTab;
108976 pEList = p->pEList;
108979 for(i=0; i<pEList->nExpr; i++){
108980 Expr *pRes = pEList->a[i].pExpr;
108981 if( pRes->op!=TK_COLUMN ) return 0;
108982 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
108990 ** Generate code that checks the left-most column of index table iCur to see if
108992 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
109010 ** right-hand side. Return TRUE if that list is constant.
109016 pLHS = pIn->pLeft;
109017 pIn->pLeft = 0;
109019 pIn->pLeft = pLHS;
109029 ** The job of this routine is to find or create a b-tree object that can
109033 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
109036 ** The returned value of this function indicates the b-tree type, as follows:
109038 ** IN_INDEX_ROWID - The cursor was opened on a database table.
109039 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
109040 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
109041 ** IN_INDEX_EPH - The cursor was opened on a specially created and
109043 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
109046 ** An existing b-tree might be used if the RHS expression pX is a simple
109053 ** pX->iTable made to point to the ephemeral table instead of an
109056 ** will be set on pX and the pX->y.sub fields will be set to show where
109065 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
109066 ** through the set members) then the b-tree must not contain duplicates.
109068 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
109071 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
109074 ** index can be found with the specified <columns> as its left-most.
109078 ** routine might decide that creating an ephemeral b-tree for membership
109083 ** When the b-tree is being used for membership tests, the calling function
109092 ** the value in that register will be NULL if the b-tree contains one or more
109093 ** NULL values, and it will be some non-NULL value if the b-tree contains no
109122 assert( pX->op==TK_IN );
109124 iTab = pParse->nTab++;
109133 ExprList *pEList = pX->x.pSelect->pEList;
109134 for(i=0; i<pEList->nExpr; i++){
109135 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
109137 if( i==pEList->nExpr ){
109145 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
109146 sqlite3 *db = pParse->db; /* Database connection */
109149 ExprList *pEList = p->pEList;
109150 int nExpr = pEList->nExpr;
109152 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
109153 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
109154 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
109155 pTab = p->pSrc->a[0].pTab;
109158 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
109161 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
109164 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
109172 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
109184 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
109185 int iCol = pEList->a[i].pExpr->iColumn;
109207 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
109210 if( pIdx->nColumn<nExpr ) continue;
109211 if( pIdx->pPartIdxWhere!=0 ) continue;
109212 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
109214 testcase( pIdx->nColumn==BMS-2 );
109215 testcase( pIdx->nColumn==BMS-1 );
109216 if( pIdx->nColumn>=BMS-1 ) continue;
109218 if( pIdx->nKeyCol>nExpr
109219 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
109227 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
109228 Expr *pRhs = pEList->a[i].pExpr;
109233 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
109234 assert( pIdx->azColl[j] );
109235 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
109247 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
109248 if( colUsed==(MASKBIT(nExpr)-1) ){
109252 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
109253 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
109255 VdbeComment((v, "%s", pIdx->zName));
109257 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
109261 i64 mask = (1<<nExpr)-1;
109265 *prRhsHasNull = ++pParse->nMem;
109287 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
109289 pParse->nTab--; /* Back out the allocation of the unused cursor */
109290 iTab = -1; /* Cursor is not allocated */
109295 /* Could not find an existing table or index to use as the RHS b-tree.
109298 u32 savedNQueryLoop = pParse->nQueryLoop;
109302 pParse->nQueryLoop = 0;
109304 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
109306 assert( pX->op==TK_IN );
109311 pParse->nQueryLoop = savedNQueryLoop;
109316 n = sqlite3ExprVectorSize(pX->pLeft);
109327 ** function allocates and returns a nul-terminated string containing
109334 Expr *pLeft = pExpr->pLeft;
109336 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
109339 assert( pExpr->op==TK_IN );
109340 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
109347 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
109363 ** "sub-select returns N columns - expected M"
109366 if( pParse->nErr==0 ){
109367 const char *zFmt = "sub-select returns %d columns - expected %d";
109375 ** it is not permitted. If pExpr is a sub-select vector, this routine
109378 ** "sub-select returns N columns - expected 1"
109387 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
109401 ** x IN (4,5,11) -- IN operator with list on right-hand side
109402 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
109406 ** table is computed, the cursor number is also stored in pExpr->iTable,
109430 v = pParse->pVdbe;
109436 ** * The right-hand side is a correlated subquery
109437 ** * The right-hand side is an expression list containing variables
109443 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
109452 pExpr->x.pSelect->selId));
109455 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
109456 pExpr->y.sub.iAddr);
109457 assert( iTab!=pExpr->iTable );
109458 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
109467 pExpr->y.sub.regReturn = ++pParse->nMem;
109468 pExpr->y.sub.iAddr =
109469 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
109475 pLeft = pExpr->pLeft;
109481 pExpr->iTable = iTab;
109482 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
109485 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
109490 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
109498 Select *pSelect = pExpr->x.pSelect;
109499 ExprList *pEList = pSelect->pEList;
109502 addrOnce?"":"CORRELATED ", pSelect->selId
109506 if( ALWAYS(pEList->nExpr==nVal) ){
109513 pSelect->iLimit = 0;
109514 testcase( pSelect->selFlags & SF_Distinct );
109516 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
109517 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
109518 sqlite3SelectDelete(pParse->db, pCopy);
109519 sqlite3DbFree(pParse->db, dest.zAffSdst);
109526 assert( pEList->nExpr>0 );
109530 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
109531 pParse, p, pEList->a[i].pExpr
109535 }else if( ALWAYS(pExpr->x.pList!=0) ){
109545 ExprList *pList = pExpr->x.pList;
109556 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109562 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
109563 Expr *pE2 = pItem->pExpr;
109567 ** this code only executes once. Because for a non-constant
109571 sqlite3VdbeChangeToNoop(v, addrOnce-1);
109593 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109594 || pParse->nErr );
109595 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109596 pExpr->y.sub.iAddr, 1);
109607 ** (SELECT a FROM b) -- subquery
109608 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
109612 ** Return the register that holds the result. For a multi-column SELECT,
109614 ** return value is the register of the left-most result column.
109629 Vdbe *v = pParse->pVdbe;
109631 if( pParse->nErr ) return 0;
109632 testcase( pExpr->op==TK_EXISTS );
109633 testcase( pExpr->op==TK_SELECT );
109634 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
109636 pSel = pExpr->x.pSelect;
109641 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
109643 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
109644 pExpr->y.sub.iAddr);
109645 return pExpr->iTable;
109652 pExpr->y.sub.regReturn = ++pParse->nMem;
109653 pExpr->y.sub.iAddr =
109654 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
109659 ** * The right-hand side is a correlated subquery
109660 ** * The right-hand side is an expression list containing variables
109681 addrOnce?"":"CORRELATED ", pSel->selId));
109682 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
109683 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
109684 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
109685 pParse->nMem += nReg;
109686 if( pExpr->op==TK_SELECT ){
109690 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
109697 if( pSel->pLimit ){
109698 /* The subquery already has a limit. If the pre-existing limit is X
109700 sqlite3 *db = pParse->db;
109703 pLimit->affExpr = SQLITE_AFF_NUMERIC;
109705 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
109707 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
109708 pSel->pLimit->pLeft = pLimit;
109710 /* If there is no pre-existing limit add a limit of 1 */
109711 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
109712 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
109714 pSel->iLimit = 0;
109716 pExpr->op2 = pExpr->op;
109717 pExpr->op = TK_ERROR;
109720 pExpr->iTable = rReg = dest.iSDParm;
109725 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
109729 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109730 || pParse->nErr );
109731 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109732 pExpr->y.sub.iAddr, 1);
109742 ** sub-select on the RHS of the IN() operator has the same number of
109744 ** a sub-query, that the LHS is a vector of size 1.
109747 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
109748 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
109749 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
109750 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
109754 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
109768 ** The left-hand side (LHS) is a scalar or vector expression. The
109769 ** right-hand side (RHS) is an array of zero or more scalar values, or a
109784 ** See the separate in-operator.md documentation file in the canonical
109808 int addrTop; /* Top of the step-6 loop */
109810 u8 okConstFactor = pParse->okConstFactor;
109813 pLeft = pExpr->pLeft;
109816 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
109818 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
109820 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
109826 v = pParse->pVdbe;
109834 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
109839 ** nVector-1. */
109859 assert( pParse->okConstFactor==okConstFactor );
109860 pParse->okConstFactor = 0;
109862 pParse->okConstFactor = okConstFactor;
109879 ** This is step (1) in the in-operator.md optimized algorithm.
109889 pList = pExpr->x.pList;
109890 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109895 for(ii=0; ii<pList->nExpr; ii++){
109896 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
109897 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
109901 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
109905 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
109906 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
109907 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
109908 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
109939 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
109940 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
109947 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
109952 /* In this case, the RHS is the ROWID of table b-tree and so we also
109953 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
109971 /* Step 4. If the RHS is known to be non-NULL and we did not find
110031 sqlite3DbFree(pParse->db, aiMap);
110032 sqlite3DbFree(pParse->db, zAff);
110039 ** value described by z[0..n-1] into register iMem.
110041 ** The z[] string will probably not be zero-terminated. But the
110050 if( negateFlag ) value = -value;
110059 ** text z[0..n-1] into register iMem.
110061 ** Expr.u.zToken is always UTF8 and zero-terminated.
110064 Vdbe *v = pParse->pVdbe;
110065 if( pExpr->flags & EP_IntValue ){
110066 int i = pExpr->u.iValue;
110068 if( negFlag ) i = -i;
110073 const char *z = pExpr->u.zToken;
110078 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
110083 negFlag?"-":"",pExpr);
110091 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
110099 ** appropriate for the iIdxCol-th column of index pIdx.
110108 i16 iTabCol = pIdx->aiColumn[iIdxCol];
110110 assert( pIdx->aColExpr );
110111 assert( pIdx->aColExpr->nExpr>iIdxCol );
110112 pParse->iSelfTab = iTabCur + 1;
110113 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
110114 pParse->iSelfTab = 0;
110116 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
110133 Vdbe *v = pParse->pVdbe;
110134 int nErr = pParse->nErr;
110136 assert( pParse->iSelfTab!=0 );
110137 if( pParse->iSelfTab>0 ){
110138 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
110143 if( pCol->affinity>=SQLITE_AFF_TEXT ){
110144 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
110147 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
110152 ** Generate code to extract the value of the iCol-th column of a table.
110165 if( iCol<0 || iCol==pTab->iPKey ){
110167 VdbeComment((v, "%s.rowid", pTab->zName));
110175 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
110177 if( pCol->colFlags & COLFLAG_BUSY ){
110179 pCol->zCnName);
110181 int savedSelfTab = pParse->iSelfTab;
110182 pCol->colFlags |= COLFLAG_BUSY;
110183 pParse->iSelfTab = iTabCur+1;
110185 pParse->iSelfTab = savedSelfTab;
110186 pCol->colFlags &= ~COLFLAG_BUSY;
110205 ** Generate code that will extract the iColumn-th column from
110219 assert( pParse->pVdbe!=0 );
110220 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
110222 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
110223 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
110229 ** Generate code to move content from registers iFrom...iFrom+nReg-1
110230 ** over to iTo..iTo+nReg-1.
110233 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
110244 p->op2 = p->op;
110245 p->op = TK_REGISTER;
110246 p->iTable = iReg;
110267 if( p->op==TK_SELECT ){
110275 iResult = pParse->nMem+1;
110276 pParse->nMem += nResult;
110279 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
110287 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
110291 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
110298 ** in-line rather than by using the usual callbacks.
110307 Vdbe *v = pParse->pVdbe;
110310 nFarg = pFarg->nExpr;
110311 assert( nFarg>0 ); /* All in-line functions have at least one argument */
110314 /* Attempt a direct implementation of the built-in COALESCE() and
110316 ** arguments past the first non-NULL argument.
110321 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
110325 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
110340 Expr *pArg = pFarg->a[0].pExpr;
110341 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
110342 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
110350 /* The UNLIKELY() function is a no-op. The result is the value
110354 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
110359 ** Test-only SQL functions that are only usable if enabled
110367 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
110376 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
110385 pA1 = pFarg->a[1].pExpr;
110386 if( pA1->op==TK_COLUMN ){
110388 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
110405 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
110409 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
110418 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
110430 for(p=pParse->pIdxEpr; p; p=p->pIENext){
110432 int iDataCur = p->iDataCur;
110434 if( pParse->iSelfTab ){
110435 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
110436 iDataCur = -1;
110438 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
110439 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
110441 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
110442 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
110443 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
110449 v = pParse->pVdbe;
110451 if( p->bMaybeNullRow ){
110456 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
110458 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
110459 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
110461 p = pParse->pIdxEpr;
110462 pParse->pIdxEpr = 0;
110464 pParse->pIdxEpr = p;
110467 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
110468 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
110472 return -1; /* Not found */
110488 Vdbe *v = pParse->pVdbe; /* The VM under construction */
110491 int regFree1 = 0; /* If non-zero free this temporary register */
110492 int regFree2 = 0; /* If non-zero free this temporary register */
110497 assert( target>0 && target<=pParse->nMem );
110503 }else if( pParse->pIdxEpr!=0
110510 op = pExpr->op;
110514 AggInfo *pAggInfo = pExpr->pAggInfo;
110517 assert( pExpr->iAgg>=0 );
110518 if( pExpr->iAgg>=pAggInfo->nColumn ){
110524 ** tag-20230325-2 */
110530 pCol = &pAggInfo->aCol[pExpr->iAgg];
110531 if( !pAggInfo->directMode ){
110532 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
110533 }else if( pAggInfo->useSortingIdx ){
110534 Table *pTab = pCol->pTab;
110535 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
110536 pCol->iSorterColumn, target);
110539 }else if( pCol->iColumn<0 ){
110540 VdbeComment((v,"%s.rowid",pTab->zName));
110543 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
110544 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
110549 }else if( pExpr->y.pTab==0 ){
110552 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
110559 int iTab = pExpr->iTable;
110563 ** constraints, and that constant is coded by the pExpr->pLeft
110569 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
110571 assert( pExpr->y.pTab!=0 );
110572 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
110578 &zAff[(aff-'B')*2], P4_STATIC);
110583 if( pParse->iSelfTab<0 ){
110587 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
110593 int iCol = pExpr->iColumn;
110595 pTab = pExpr->y.pTab;
110598 assert( iCol<pTab->nCol );
110600 return -1-pParse->iSelfTab;
110602 pCol = pTab->aCol + iCol;
110604 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
110606 if( pCol->colFlags & COLFLAG_GENERATED ){
110607 if( pCol->colFlags & COLFLAG_BUSY ){
110609 pCol->zCnName);
110612 pCol->colFlags |= COLFLAG_BUSY;
110613 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
110616 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
110620 if( pCol->affinity==SQLITE_AFF_REAL ){
110630 iTab = pParse->iSelfTab - 1;
110634 assert( pExpr->y.pTab!=0 );
110635 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
110636 pExpr->iColumn, iTab, target,
110637 pExpr->op2);
110651 codeReal(v, pExpr->u.zToken, 0, target);
110657 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
110665 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
110675 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
110676 assert( pExpr->u.zToken[1]=='\'' );
110677 z = &pExpr->u.zToken[2];
110678 n = sqlite3Strlen30(z) - 1;
110687 assert( pExpr->u.zToken!=0 );
110688 assert( pExpr->u.zToken[0]!=0 );
110689 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
110690 if( pExpr->u.zToken[1]!=0 ){
110691 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
110692 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
110693 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
110699 return pExpr->iTable;
110704 sqlite3ExprCode(pParse, pExpr->pLeft, target);
110708 sqlite3AffinityType(pExpr->u.zToken, 0));
110716 /* fall-through */
110723 Expr *pLeft = pExpr->pLeft;
110728 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
110730 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
110772 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110773 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
110780 Expr *pLeft = pExpr->pLeft;
110782 if( pLeft->op==TK_INTEGER ){
110786 }else if( pLeft->op==TK_FLOAT ){
110788 codeReal(v, pLeft->u.zToken, 1, target);
110797 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
110807 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110815 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110817 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
110818 bNormal = pExpr->op2==TK_IS;
110830 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110840 AggInfo *pInfo = pExpr->pAggInfo;
110842 || NEVER(pExpr->iAgg<0)
110843 || NEVER(pExpr->iAgg>=pInfo->nFunc)
110848 return AggInfoFuncReg(pInfo, pExpr->iAgg);
110859 sqlite3 *db = pParse->db; /* The database connection */
110865 return pExpr->y.pWin->regResult;
110872 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
110876 pFarg = pExpr->x.pList;
110877 nFarg = pFarg ? pFarg->nExpr : 0;
110879 zId = pExpr->u.zToken;
110882 if( pDef==0 && pParse->explain ){
110886 if( pDef==0 || pDef->xFinalize!=0 ){
110890 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
110891 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
110892 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
110894 SQLITE_PTR_TO_INT(pDef->pUserData), target);
110895 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
110900 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
110904 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
110905 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
110910 r1 = pParse->nMem+1;
110911 pParse->nMem += nFarg;
110921 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
110924 assert( pFarg->a[0].pExpr!=0 );
110925 exprOp = pFarg->a[0].pExpr->op;
110929 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
110930 pFarg->a[0].pExpr->op2 =
110931 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
110954 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
110956 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
110959 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
110960 if( !pColl ) pColl = db->pDfltColl;
110964 pDef, pExpr->op2);
110980 if( pParse->db->mallocFailed ){
110984 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
110994 Expr *pLeft = pExpr->pLeft;
110995 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
110996 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
110997 pLeft->op2 = pParse->withinRJSubrtn;
110999 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
111001 if( pExpr->iTable!=n ){
111003 pExpr->iTable, n);
111005 return pLeft->iTable + pExpr->iColumn;
111028 ** X is stored in pExpr->pLeft.
111029 ** Y is stored in pExpr->pList->a[0].pExpr.
111030 ** Z is stored in pExpr->pList->a[1].pExpr.
111038 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
111039 ** "SOFT-COLLATE" that is added to constraints that are pushed down
111040 ** from outer queries into sub-queries by the push-down optimization.
111043 assert( pExpr->pLeft );
111044 sqlite3ExprCode(pParse, pExpr->pLeft, target);
111048 pExpr = pExpr->pLeft;
111049 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
111054 pExpr = pExpr->pLeft;
111055 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
111060 ** to a column in the new.* or old.* pseudo-tables available to
111062 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
111063 ** is set to the column of the pseudo-table to read, or to -1 to
111068 ** to reference another column of the old.* pseudo-table, where
111070 ** set to (n+1), where n is the number of columns in each pseudo-table.
111071 ** For a reference to any other column in the new.* pseudo-table, p1
111080 ** p1==0 -> old.rowid p1==3 -> new.rowid
111081 ** p1==1 -> old.a p1==4 -> new.a
111082 ** p1==2 -> old.b p1==5 -> new.b
111089 pTab = pExpr->y.pTab;
111090 iCol = pExpr->iColumn;
111091 p1 = pExpr->iTable * (pTab->nCol+1) + 1
111094 assert( pExpr->iTable==0 || pExpr->iTable==1 );
111095 assert( iCol>=-1 && iCol<pTab->nCol );
111096 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
111097 assert( p1>=0 && p1<(pTab->nCol*2+2) );
111101 (pExpr->iTable ? "new" : "old"),
111102 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
111109 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
111111 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
111124 ** that derive from the right-hand table of a LEFT JOIN. The
111125 ** Expr.iTable value is the table number for the right-hand table.
111131 u8 okConstFactor = pParse->okConstFactor;
111132 AggInfo *pAggInfo = pExpr->pAggInfo;
111134 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
111135 if( !pAggInfo->directMode ){
111136 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
111139 if( pExpr->pAggInfo->useSortingIdx ){
111140 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
111141 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
111147 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target);
111155 pParse->okConstFactor = 0; /* note (1) above */
111156 sqlite3ExprCode(pParse, pExpr->pLeft, target);
111158 pParse->okConstFactor = okConstFactor;
111174 ** X (if it exists) is in pExpr->pLeft.
111175 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
111178 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
111195 sqlite3 *db = pParse->db;
111197 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
111198 assert(pExpr->x.pList->nExpr > 0);
111199 pEList = pExpr->x.pList;
111200 aListelem = pEList->a;
111201 nExpr = pEList->nExpr;
111203 if( (pX = pExpr->pLeft)!=0 ){
111205 if( db->mallocFailed ){
111209 testcase( pX->op==TK_COLUMN );
111217 ** The value in regFree1 might get SCopy-ed into the file result.
111222 for(i=0; i<nExpr-1; i=i+2){
111230 testcase( pTest->op==TK_COLUMN );
111232 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
111238 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
111249 assert( pExpr->affExpr==OE_Rollback
111250 || pExpr->affExpr==OE_Abort
111251 || pExpr->affExpr==OE_Fail
111252 || pExpr->affExpr==OE_Ignore
111254 if( !pParse->pTriggerTab && !pParse->nested ){
111256 "RAISE() may only be used within a trigger-program");
111259 if( pExpr->affExpr==OE_Abort ){
111263 if( pExpr->affExpr==OE_Ignore ){
111265 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
111269 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
111270 pExpr->affExpr, pExpr->u.zToken, 0, 0);
111306 p = pParse->pConstExpr;
111310 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
111311 if( pItem->fg.reusable
111312 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
111314 return pItem->u.iConstExprReg;
111318 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
111320 Vdbe *v = pParse->pVdbe;
111324 pParse->okConstFactor = 0;
111325 if( !pParse->db->mallocFailed ){
111326 if( regDest<0 ) regDest = ++pParse->nMem;
111329 pParse->okConstFactor = 1;
111330 sqlite3ExprDelete(pParse->db, pExpr);
111335 struct ExprList_item *pItem = &p->a[p->nExpr-1];
111336 pItem->fg.reusable = regDest<0;
111337 if( regDest<0 ) regDest = ++pParse->nMem;
111338 pItem->u.iConstExprReg = regDest;
111340 pParse->pConstExpr = p;
111363 && pExpr->op!=TK_REGISTER
111367 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
111390 assert( target>0 && target<=pParse->nMem );
111391 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
111392 if( pParse->pVdbe==0 ) return;
111397 && (ExprHasProperty(pExpr,EP_Subquery) || pExpr->op==TK_REGISTER)
111403 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
111413 sqlite3 *db = pParse->db;
111415 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
111426 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
111438 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
111463 Vdbe *v = pParse->pVdbe;
111466 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
111467 n = pList->nExpr;
111469 for(pItem=pList->a, i=0; i<n; i++, pItem++){
111470 Expr *pExpr = pItem->pExpr;
111472 if( pItem->fg.bSorterRef ){
111473 i--;
111474 n--;
111477 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
111479 i--;
111480 n--;
111482 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
111493 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
111494 && pOp->p1+pOp->p3+1==inReg
111495 && pOp->p2+pOp->p3+1==target+i
111496 && pOp->p5==0 /* The do-not-merge flag must be clear */
111498 pOp->p3++;
111540 sqlite3 *db = pParse->db;
111547 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
111548 if( db->mallocFailed==0 ){
111554 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
111557 compRight.pRight = pExpr->x.pList->a[1].pExpr;
111567 pDel->flags |= EP_OuterON;
111601 Vdbe *v = pParse->pVdbe;
111611 op = pExpr->op;
111621 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
111623 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111627 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111628 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111634 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111641 isNot = pExpr->op2==TK_ISNOT;
111642 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111646 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111649 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111667 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111669 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111670 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111671 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111691 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111719 /* No-op */
111744 Vdbe *v = pParse->pVdbe;
111755 /* The value of pExpr->op and op are related as follows:
111757 ** pExpr->op op
111758 ** --------- ----------
111768 ** For other values of pExpr->op, op is undefined and unused.
111773 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
111777 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
111778 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
111779 assert( pExpr->op!=TK_NE || op==OP_Eq );
111780 assert( pExpr->op!=TK_EQ || op==OP_Ne );
111781 assert( pExpr->op!=TK_LT || op==OP_Ge );
111782 assert( pExpr->op!=TK_LE || op==OP_Gt );
111783 assert( pExpr->op!=TK_GT || op==OP_Le );
111784 assert( pExpr->op!=TK_GE || op==OP_Lt );
111786 switch( pExpr->op ){
111792 }else if( pExpr->op==TK_AND ){
111794 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111795 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111799 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
111801 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111808 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111815 isNot = pExpr->op2==TK_ISNOT;
111816 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111821 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111826 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111833 testcase( pExpr->op==TK_IS );
111834 testcase( pExpr->op==TK_ISNOT );
111835 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
111844 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111846 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111847 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111848 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111866 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111896 /* no-op */
111917 sqlite3 *db = pParse->db;
111919 if( db->mallocFailed==0 ){
111929 ** If pExpr is a simple SQL value - an integer, real, string, blob
111930 ** or NULL value - then the VDBE currently being prepared is configured
111931 ** to re-prepare each time a new value is bound to variable pVar.
111934 ** same as that currently bound to variable pVar, non-zero is returned.
111947 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
111949 iVar = pVar->iColumn;
111950 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
111951 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
111954 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
111969 ** other than the top-level COLLATE operator.
111971 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
111983 ** this routine is used, it does not hurt to get an extra 2 - that
111988 ** pParse->pReprepare can be matched against literals in pB. The
111989 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
112004 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
112007 combinedFlags = pA->flags | pB->flags;
112009 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
112014 if( pA->op!=pB->op || pA->op==TK_RAISE ){
112015 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
112018 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
112021 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
112022 && pB->iTable<0 && pA->iTable==iTab
112031 if( pA->u.zToken ){
112032 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
112033 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
112035 assert( pA->op==pB->op );
112040 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
112045 }else if( pA->op==TK_NULL ){
112047 }else if( pA->op==TK_COLLATE ){
112048 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
112050 if( pB->u.zToken!=0
112051 && pA->op!=TK_COLUMN
112052 && pA->op!=TK_AGG_COLUMN
112053 && strcmp(pA->u.zToken,pB->u.zToken)!=0
112058 if( (pA->flags & (EP_Distinct|EP_Commuted))
112059 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
112063 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
112064 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
112065 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
112066 if( pA->op!=TK_STRING
112067 && pA->op!=TK_TRUEFALSE
112070 if( pA->iColumn!=pB->iColumn ) return 2;
112071 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
112072 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
112085 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
112088 ** This routine might return non-zero for equivalent ExprLists. The
112094 ** always differs from a non-NULL pointer.
112100 if( pA->nExpr!=pB->nExpr ) return 1;
112101 for(i=0; i<pA->nExpr; i++){
112103 Expr *pExprA = pA->a[i].pExpr;
112104 Expr *pExprB = pB->a[i].pExpr;
112105 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
112112 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
112123 ** Return non-zero if Expr p can only be true if pNN is not NULL.
112125 ** Or if seenNot is true, return non-zero if Expr p can only be
112126 ** non-NULL if pNN is not NULL
112133 int seenNot /* Return true only if p can be any non-NULL value */
112138 return pNN->op!=TK_NULL;
112140 switch( p->op ){
112143 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
112144 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112149 pList = p->x.pList;
112151 assert( pList->nExpr==2 );
112153 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
112154 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
112158 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112178 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
112185 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
112189 if( p->op2!=TK_IS ) return 0;
112190 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112194 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112217 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
112234 if( pE2->op==TK_OR
112235 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
112236 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
112240 if( pE2->op==TK_NOTNULL
112241 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
112250 ** If the expression node requires that the table at pWalker->iCur
112251 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
112254 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
112255 ** (never setting pWalker->eCode) is a harmless missed optimization.
112258 testcase( pExpr->op==TK_AGG_COLUMN );
112259 testcase( pExpr->op==TK_AGG_FUNCTION );
112261 switch( pExpr->op ){
112272 testcase( pExpr->op==TK_ISNOT );
112273 testcase( pExpr->op==TK_ISNULL );
112274 testcase( pExpr->op==TK_NOTNULL );
112275 testcase( pExpr->op==TK_IS );
112276 testcase( pExpr->op==TK_OR );
112277 testcase( pExpr->op==TK_VECTOR );
112278 testcase( pExpr->op==TK_CASE );
112279 testcase( pExpr->op==TK_IN );
112280 testcase( pExpr->op==TK_FUNCTION );
112281 testcase( pExpr->op==TK_TRUTH );
112284 if( pWalker->u.iCur==pExpr->iTable ){
112285 pWalker->eCode = 1;
112291 if( pWalker->eCode==0 ){
112292 sqlite3WalkExpr(pWalker, pExpr->pLeft);
112293 if( pWalker->eCode ){
112294 pWalker->eCode = 0;
112295 sqlite3WalkExpr(pWalker, pExpr->pRight);
112301 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
112302 assert( pWalker->eCode );
112316 Expr *pLeft = pExpr->pLeft;
112317 Expr *pRight = pExpr->pRight;
112318 testcase( pExpr->op==TK_EQ );
112319 testcase( pExpr->op==TK_NE );
112320 testcase( pExpr->op==TK_LT );
112321 testcase( pExpr->op==TK_LE );
112322 testcase( pExpr->op==TK_GT );
112323 testcase( pExpr->op==TK_GE );
112326 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
112327 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
112328 if( (pLeft->op==TK_COLUMN
112329 && ALWAYS(pLeft->y.pTab!=0)
112330 && IsVirtual(pLeft->y.pTab))
112331 || (pRight->op==TK_COLUMN
112332 && ALWAYS(pRight->y.pTab!=0)
112333 && IsVirtual(pRight->y.pTab))
112345 ** Return true (non-zero) if expression p can only be true if at least
112346 ** one column of table iTab is non-null. In other words, return true
112363 ** be non-NULL, then the LEFT JOIN can be safely converted into an
112370 if( p->op==TK_NOTNULL ){
112371 p = p->pLeft;
112373 while( p->op==TK_AND ){
112374 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
112375 p = p->pRight;
112401 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
112402 ** pWalker->u.pIdxCover->pIdx.
112405 if( pExpr->op==TK_COLUMN
112406 && pExpr->iTable==pWalker->u.pIdxCover->iCur
112407 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
112409 pWalker->eCode = 1;
112461 struct RefSrcList *p = pWalker->u.pRefSrcList;
112462 SrcList *pSrc = pSelect->pSrc;
112465 if( pSrc->nSrc==0 ) return WRC_Continue;
112466 j = p->nExclude;
112467 p->nExclude += pSrc->nSrc;
112468 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
112470 p->nExclude = 0;
112473 p->aiExclude = piNew;
112475 for(i=0; i<pSrc->nSrc; i++, j++){
112476 p->aiExclude[j] = pSrc->a[i].iCursor;
112481 struct RefSrcList *p = pWalker->u.pRefSrcList;
112482 SrcList *pSrc = pSelect->pSrc;
112483 if( p->nExclude ){
112484 assert( p->nExclude>=pSrc->nSrc );
112485 p->nExclude -= pSrc->nSrc;
112491 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
112494 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
112498 if( pExpr->op==TK_COLUMN
112499 || pExpr->op==TK_AGG_COLUMN
112502 struct RefSrcList *p = pWalker->u.pRefSrcList;
112503 SrcList *pSrc = p->pRef;
112504 int nSrc = pSrc ? pSrc->nSrc : 0;
112506 if( pExpr->iTable==pSrc->a[i].iCursor ){
112507 pWalker->eCode |= 1;
112511 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
112512 if( i>=p->nExclude ){
112513 pWalker->eCode |= 2;
112528 ** -1 pExpr only references no tables at all, or it only
112537 assert( pParse->db!=0 );
112544 x.db = pParse->db;
112546 assert( pExpr->op==TK_AGG_FUNCTION );
112548 sqlite3WalkExprList(&w, pExpr->x.pList);
112551 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
112554 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
112560 return -1;
112577 && pExpr->pAggInfo!=0
112579 AggInfo *pAggInfo = pExpr->pAggInfo;
112580 int iAgg = pExpr->iAgg;
112581 Parse *pParse = pWalker->pParse;
112582 sqlite3 *db = pParse->db;
112584 if( pExpr->op!=TK_AGG_FUNCTION ){
112585 if( iAgg<pAggInfo->nColumn
112586 && pAggInfo->aCol[iAgg].pCExpr==pExpr
112590 pAggInfo->aCol[iAgg].pCExpr = pExpr;
112595 assert( pExpr->op==TK_AGG_FUNCTION );
112596 if( ALWAYS(iAgg<pAggInfo->nFunc)
112597 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
112601 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
112616 pWalker->pParse = pParse;
112617 pWalker->xExprCallback = agginfoPersistExprCb;
112618 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
112622 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
112627 pInfo->aCol = sqlite3ArrayAllocate(
112629 pInfo->aCol,
112630 sizeof(pInfo->aCol[0]),
112631 &pInfo->nColumn,
112638 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
112643 pInfo->aFunc = sqlite3ArrayAllocate(
112645 pInfo->aFunc,
112646 sizeof(pInfo->aFunc[0]),
112647 &pInfo->nFunc,
112657 ** If no prior entry is found, create a new one and return -1. The
112658 ** new column will have an idex of pAggInfo->nColumn-1.
112668 assert( pAggInfo->iFirstReg==0 );
112669 pCol = pAggInfo->aCol;
112670 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
112671 if( pCol->iTable==pExpr->iTable
112672 && pCol->iColumn==pExpr->iColumn
112673 && pExpr->op!=TK_IF_NULL_ROW
112678 k = addAggInfoColumn(pParse->db, pAggInfo);
112681 assert( pParse->db->mallocFailed );
112684 pCol = &pAggInfo->aCol[k];
112686 pCol->pTab = pExpr->y.pTab;
112687 pCol->iTable = pExpr->iTable;
112688 pCol->iColumn = pExpr->iColumn;
112689 pCol->iSorterColumn = -1;
112690 pCol->pCExpr = pExpr;
112691 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
112693 ExprList *pGB = pAggInfo->pGroupBy;
112694 struct ExprList_item *pTerm = pGB->a;
112695 n = pGB->nExpr;
112697 Expr *pE = pTerm->pExpr;
112698 if( pE->op==TK_COLUMN
112699 && pE->iTable==pExpr->iTable
112700 && pE->iColumn==pExpr->iColumn
112702 pCol->iSorterColumn = j;
112707 if( pCol->iSorterColumn<0 ){
112708 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
112712 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
112713 pExpr->pAggInfo = pAggInfo;
112714 if( pExpr->op==TK_COLUMN ){
112715 pExpr->op = TK_AGG_COLUMN;
112717 pExpr->iAgg = (i16)k;
112727 NameContext *pNC = pWalker->u.pNC;
112728 Parse *pParse = pNC->pParse;
112729 SrcList *pSrcList = pNC->pSrcList;
112730 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
112732 assert( pNC->ncFlags & NC_UAggInfo );
112733 assert( pAggInfo->iFirstReg==0 );
112734 switch( pExpr->op ){
112738 assert( pParse->iSelfTab==0 );
112739 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
112740 if( pParse->pIdxEpr==0 ) break;
112741 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
112742 int iDataCur = pIEpr->iDataCur;
112744 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
112748 for(i=0; i<pSrcList->nSrc; i++){
112749 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
112751 if( i>=pSrcList->nSrc ) break;
112752 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
112753 if( pParse->nErr ){ return WRC_Abort; }
112761 tmp.iTable = pIEpr->iIdxCur;
112762 tmp.iColumn = pIEpr->iIdxCol;
112764 if( pParse->nErr ){ return WRC_Abort; }
112765 assert( pAggInfo->aCol!=0 );
112766 assert( tmp.iAgg<pAggInfo->nColumn );
112767 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
112768 pExpr->pAggInfo = pAggInfo;
112769 pExpr->iAgg = tmp.iAgg;
112775 testcase( pExpr->op==TK_AGG_COLUMN );
112776 testcase( pExpr->op==TK_COLUMN );
112777 testcase( pExpr->op==TK_IF_NULL_ROW );
112781 SrcItem *pItem = pSrcList->a;
112782 for(i=0; i<pSrcList->nSrc; i++, pItem++){
112784 if( pExpr->iTable==pItem->iCursor ){
112787 } /* endif pExpr->iTable==pItem->iCursor */
112793 if( (pNC->ncFlags & NC_InAggFunc)==0
112794 && pWalker->walkerDepth==pExpr->op2
112799 struct AggInfo_func *pItem = pAggInfo->aFunc;
112800 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
112801 if( pItem->pFExpr==pExpr ) break;
112802 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
112806 if( i>=pAggInfo->nFunc ){
112807 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
112809 u8 enc = ENC(pParse->db);
112810 i = addAggInfoFunc(pParse->db, pAggInfo);
112813 pItem = &pAggInfo->aFunc[i];
112814 pItem->pFExpr = pExpr;
112816 pItem->pFunc = sqlite3FindFunction(pParse->db,
112817 pExpr->u.zToken,
112818 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
112819 if( pExpr->flags & EP_Distinct ){
112820 pItem->iDistinct = pParse->nTab++;
112822 pItem->iDistinct = -1;
112826 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
112830 pExpr->iAgg = (i16)i;
112831 pExpr->pAggInfo = pAggInfo;
112843 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
112858 assert( pNC->pSrcList!=0 );
112872 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
112873 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
112882 if( pParse->nTempReg==0 ){
112883 return ++pParse->nMem;
112885 return pParse->aTempReg[--pParse->nTempReg];
112895 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
112896 pParse->aTempReg[pParse->nTempReg++] = iReg;
112907 i = pParse->iRangeReg;
112908 n = pParse->nRangeReg;
112910 pParse->iRangeReg += nReg;
112911 pParse->nRangeReg -= nReg;
112913 i = pParse->nMem+1;
112914 pParse->nMem += nReg;
112924 if( nReg>pParse->nRangeReg ){
112925 pParse->nRangeReg = nReg;
112926 pParse->iRangeReg = iReg;
112933 ** Always invoke this procedure after coding a subroutine or co-routine
112935 ** the sub/co-routine does not use registers in common with the code that
112936 ** invokes the sub/co-routine.
112939 pParse->nTempReg = 0;
112940 pParse->nRangeReg = 0;
112948 if( pParse->nMem<iReg ) pParse->nMem = iReg;
112959 const ExprList *pList = pParse->pConstExpr;
112962 for(i=0; i<pList->nExpr; i++){
112963 if( pList->a[i].u.iConstExprReg>=iMin ){
112964 iMin = pList->a[i].u.iConstExprReg + 1;
112968 pParse->nTempReg = 0;
112969 pParse->nRangeReg = 0;
112982 if( pParse->nRangeReg>0
112983 && pParse->iRangeReg+pParse->nRangeReg > iFirst
112984 && pParse->iRangeReg <= iLast
112988 for(i=0; i<pParse->nTempReg; i++){
112989 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
112993 if( pParse->pConstExpr ){
112994 ExprList *pList = pParse->pConstExpr;
112995 for(i=0; i<pList->nExpr; i++){
112996 int iReg = pList->a[i].u.iConstExprReg;
113033 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
113038 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
113040 || (pTab->tabFlags & TF_Eponymous)!=0
113041 || ( (pTab->tabFlags & TF_Shadow)!=0
113042 && sqlite3ReadOnlyShadowTables(pParse->db)
113046 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
113066 pParse->colNamesSet = 1;
113090 ** Generate VM code to replace any double-quoted strings (but not double-quoted
113092 ** database zDb with their single-quoted equivalents. If argument bTemp is
113118 Vdbe *v = pParse->pVdbe;
113121 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
113122 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
113138 char *zName = 0; /* NULL-terminated version of pName */
113139 sqlite3 *db = pParse->db; /* Database connection */
113140 int nTabName; /* Number of UTF-8 characters in zTabName */
113143 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
113145 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
113146 assert( pSrc->nSrc==1 );
113147 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113149 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113151 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113152 zDb = db->aDb[iDb].zDbSName;
113182 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
113189 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113200 if( pVTab->pVtab->pModule->xRename==0 ){
113216 /* figure out how many UTF-8 characters are in zName */
113217 zTabName = pTab->zName;
113218 nTabName = sqlite3Utf8CharLen(zTabName, -1);
113255 zDb, zName, pTab->zName);
113276 ** of any resources used by the v-table implementation (including other
113281 int i = ++pParse->nMem;
113316 ** The Table structure pParse->pNewTable was extended to include
113320 Table *pNew; /* Copy of pParse->pNewTable */
113325 char *zCol; /* Null-terminated column definition */
113332 db = pParse->db;
113333 assert( db->pParse==pParse );
113334 if( pParse->nErr ) return;
113335 assert( db->mallocFailed==0 );
113336 pNew = pParse->pNewTable;
113340 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
113341 zDb = db->aDb[iDb].zDbSName;
113342 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
113343 pCol = &pNew->aCol[pNew->nCol-1];
113350 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113360 if( pCol->colFlags & COLFLAG_PRIMKEY ){
113364 if( pNew->pIndex ){
113369 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
113374 assert( pDflt==0 || pDflt->op==TK_SPAN );
113375 if( pDflt && pDflt->pLeft->op==TK_NULL ){
113379 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
113381 "Cannot add a REFERENCES column with non-NULL default value");
113383 if( pCol->notNull && !pDflt ){
113398 assert( db->mallocFailed == 1 );
113403 "Cannot add a column with non-constant default");
113407 }else if( pCol->colFlags & COLFLAG_STORED ){
113413 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
113415 char *zEnd = &zCol[pColDef->n-1];
113417 *zEnd-- = '\0';
113428 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
113443 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
113453 if( pNew->pCheck!=0
113454 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
113470 ** This function is called by the parser after the table-name in
113471 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
113472 ** pSrc is the full-name of the table being altered.
113490 sqlite3 *db = pParse->db;
113493 assert( pParse->pNewTable==0 );
113495 if( db->mallocFailed ) goto exit_begin_add_column;
113496 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113517 assert( pTab->u.tab.addColOffset>0 );
113518 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113529 pParse->pNewTable = pNew;
113530 pNew->nTabRef = 1;
113531 pNew->nCol = pTab->nCol;
113532 assert( pNew->nCol>0 );
113533 nAlloc = (((pNew->nCol-1)/8)*8)+8;
113534 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
113535 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
113536 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
113537 if( !pNew->aCol || !pNew->zName ){
113538 assert( db->mallocFailed );
113541 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
113542 for(i=0; i<pNew->nCol; i++){
113543 Column *pCol = &pNew->aCol[i];
113544 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
113545 pCol->hName = sqlite3StrIHash(pCol->zCnName);
113548 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
113549 pNew->pSchema = db->aDb[iDb].pSchema;
113550 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
113551 pNew->nTabRef = 1;
113562 ** it loads an error message into pParse and returns non-zero.
113582 zType, pTab->zName
113599 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
113603 sqlite3 *db = pParse->db; /* Database connection */
113613 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113621 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
113623 zDb = db->aDb[iSchema].zDbSName;
113627 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113636 for(iCol=0; iCol<pTab->nCol; iCol++){
113637 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
113639 if( iCol==pTab->nCol ){
113644 /* Ensure the schema contains no double-quoted strings */
113655 assert( pNew->n>0 );
113656 bQuote = sqlite3Isquote(pNew->z[0]);
113663 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
113664 pTab->zName
113671 zDb, pTab->zName, iCol, zNew, bQuote
113726 ** rename-token list.
113728 ** 2. Dereferences each pointer in the rename-token list.
113731 ** address-sanitizer or similar. If any of these pointers no longer
113732 ** point to valid objects, an exception is raised by the memory-checking
113746 assert( pParse==pParse->db->pParse );
113747 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
113748 if( pParse->nErr==0 ){
113751 for(p=pParse->pRename; p; p=p->pNext){
113752 if( p->p ){
113753 assert( p->p!=pPtr );
113754 i += *(u8*)(p->p) | 1;
113770 ** in pParse->pRename.
113782 assert( pPtr || pParse->db->mallocFailed );
113784 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
113785 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
113787 pNew->p = pPtr;
113788 pNew->t = *pToken;
113789 pNew->pNext = pParse->pRename;
113790 pParse->pRename = pNew;
113805 for(p=pParse->pRename; p; p=p->pNext){
113806 if( p->p==pFrom ){
113807 p->p = pTo;
113817 Parse *pParse = pWalker->pParse;
113820 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
113830 With *pWith = pSelect->pWith;
113832 Parse *pParse = pWalker->pParse;
113835 assert( pWith->nCte>0 );
113836 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
113837 /* Push a copy of the With object onto the with-stack. We use a copy
113839 ** and SF_Resolved) below. And the parser code that uses the with-stack
113842 pCopy = sqlite3WithDup(pParse->db, pWith);
113845 for(i=0; i<pWith->nCte; i++){
113846 Select *p = pWith->a[i].pSelect;
113851 if( sNC.pParse->db->mallocFailed ) return;
113853 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
113855 if( pCopy && pParse->pWith==pCopy ){
113856 pParse->pWith = pCopy->pOuter;
113870 for(ii=0; ii<pIdList->nId; ii++){
113871 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
113879 Parse *pParse = pWalker->pParse;
113881 if( pParse->nErr ) return WRC_Abort;
113882 testcase( p->selFlags & SF_View );
113883 testcase( p->selFlags & SF_CopyCte );
113884 if( p->selFlags & (SF_View|SF_CopyCte) ){
113887 if( ALWAYS(p->pEList) ){
113888 ExprList *pList = p->pEList;
113889 for(i=0; i<pList->nExpr; i++){
113890 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
113891 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
113895 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
113896 SrcList *pSrc = p->pSrc;
113897 for(i=0; i<pSrc->nSrc; i++){
113898 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
113899 if( pSrc->a[i].fg.isUsing==0 ){
113900 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
113902 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
113915 u8 eMode = pParse->eParseMode;
113921 pParse->eParseMode = PARSE_MODE_UNMAP;
113923 pParse->eParseMode = eMode;
113927 ** Remove all nodes that are part of expression-list pEList from the
113938 for(i=0; i<pEList->nExpr; i++){
113939 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
113940 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
113953 pNext = p->pNext;
113976 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
113977 if( (*pp)->p==pPtr ){
113980 *pp = pToken->pNext;
113981 pToken->pNext = pCtx->pList;
113982 pCtx->pList = pToken;
113983 pCtx->nList++;
113994 ** descend into sub-select statements.
113997 if( p->selFlags & (SF_View|SF_CopyCte) ){
113998 testcase( p->selFlags & SF_View );
113999 testcase( p->selFlags & SF_CopyCte );
114013 ** constructed in RenameCtx object at pWalker->u.pRename.
114016 RenameCtx *p = pWalker->u.pRename;
114017 if( pExpr->op==TK_TRIGGER
114018 && pExpr->iColumn==p->iCol
114019 && pWalker->pParse->pTriggerTab==p->pTab
114021 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
114022 }else if( pExpr->op==TK_COLUMN
114023 && pExpr->iColumn==p->iCol
114025 && p->pTab==pExpr->y.pTab
114027 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
114042 RenameToken *pBest = pCtx->pList;
114046 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
114047 if( pToken->t.z>pBest->t.z ) pBest = pToken;
114049 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
114050 *pp = pBest->pNext;
114057 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
114059 ** sub-routine is currently stored in pParse->zErrMsg. This function
114073 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
114075 pParse->zErrMsg
114077 sqlite3_result_error(pCtx, zErr, -1);
114078 sqlite3DbFree(pParse->db, zErr);
114082 ** For each name in the the expression-list pEList (i.e. each
114083 ** pEList->a[i].zName) that matches the string in zOld, extract the
114084 ** corresponding rename-token from Parse object pParse and add it
114095 for(i=0; i<pEList->nExpr; i++){
114096 const char *zName = pEList->a[i].zEName;
114097 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
114108 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
114109 ** that matches the string in zOld, extract the corresponding rename-token
114120 for(i=0; i<pIdList->nId; i++){
114121 const char *zName = pIdList->a[i].zName;
114150 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
114151 p->eParseMode = PARSE_MODE_RENAME;
114152 p->db = db;
114153 p->nQueryLoop = 1;
114155 if( db->mallocFailed ) rc = SQLITE_NOMEM;
114157 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
114168 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
114169 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
114174 db->init.iDb = 0;
114214 nQuot = sqlite3Strlen30(zQuot)-1;
114218 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
114227 /* At this point pRename->pList contains a list of RenameToken objects
114229 ** with the new column name, or with single-quoted versions of themselves.
114234 while( pRename->pList ){
114241 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
114247 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
114250 /* Dequote the double-quoted token. Then requote it again, this time
114253 ** add another space after the new, single-quoted version of the
114256 memcpy(zBuf1, pBest->t.z, pBest->t.n);
114257 zBuf1[pBest->t.n] = 0;
114260 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
114266 iOff = pBest->t.z - zSql;
114267 if( pBest->t.n!=nReplace ){
114268 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
114269 nOut - (iOff + pBest->t.n)
114271 nOut += nReplace - pBest->t.n;
114278 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
114289 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
114294 for(i=0; i<pEList->nExpr; i++){
114295 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
114296 pEList->a[i].fg.eEName = val;
114302 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
114308 sqlite3 *db = pParse->db;
114309 Trigger *pNew = pParse->pNewTrigger;
114316 assert( pNew->pTabSchema );
114317 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
114318 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
114320 pParse->eTriggerOp = pNew->op;
114323 if( ALWAYS(pParse->pTriggerTab) ){
114324 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
114328 if( rc==SQLITE_OK && pNew->pWhen ){
114329 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
114332 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
114333 if( pStep->pSelect ){
114334 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
114335 if( pParse->nErr ) rc = pParse->rc;
114337 if( rc==SQLITE_OK && pStep->zTarget ){
114341 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
114344 pStep->pExprList = 0;
114348 /* pStep->pExprList contains an expression-list used for an UPDATE
114352 ** pStep->pExprList to ENAME_SPAN (from their current value of
114356 renameSetENames(pStep->pExprList, ENAME_SPAN);
114358 renameSetENames(pStep->pExprList, ENAME_NAME);
114359 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
114360 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
114361 assert( pSrc==pSel->pSrc );
114362 if( pStep->pExprList ) pSel->pEList = 0;
114363 pSel->pSrc = 0;
114366 if( pStep->pFrom ){
114368 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
114369 SrcItem *p = &pStep->pFrom->a[i];
114370 if( p->pSelect ){
114371 sqlite3SelectPrep(pParse, p->pSelect, 0);
114376 if( db->mallocFailed ){
114380 if( rc==SQLITE_OK && pStep->pWhere ){
114381 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
114384 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
114386 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
114387 if( pStep->pUpsert && rc==SQLITE_OK ){
114388 Upsert *pUpsert = pStep->pUpsert;
114389 pUpsert->pUpsertSrc = pSrc;
114392 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
114394 ExprList *pUpsertSet = pUpsert->pUpsertSet;
114398 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
114401 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
114423 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
114426 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
114427 sqlite3WalkSelect(pWalker, pStep->pSelect);
114428 sqlite3WalkExpr(pWalker, pStep->pWhere);
114429 sqlite3WalkExprList(pWalker, pStep->pExprList);
114430 if( pStep->pUpsert ){
114431 Upsert *pUpsert = pStep->pUpsert;
114432 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
114433 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
114434 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
114435 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
114437 if( pStep->pFrom ){
114439 for(i=0; i<pStep->pFrom->nSrc; i++){
114440 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
114451 sqlite3 *db = pParse->db;
114453 if( pParse->pVdbe ){
114454 sqlite3VdbeFinalize(pParse->pVdbe);
114456 sqlite3DeleteTable(db, pParse->pNewTable);
114457 while( (pIdx = pParse->pNewIndex)!=0 ){
114458 pParse->pNewIndex = pIdx->pNext;
114461 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
114462 sqlite3DbFree(db, pParse->zErrMsg);
114463 renameTokenFree(db, pParse->pRename);
114479 ** 7. bQuote: Non-zero if the new column name should be quoted.
114483 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
114513 sqlite3_xauth xAuth = db->xAuth;
114523 if( pTab==0 || iCol>=pTab->nCol ){
114527 zOld = pTab->aCol[iCol].zCnName;
114529 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
114532 db->xAuth = 0;
114547 Select *pSelect = sParse.pNewTable->u.view.pSelect;
114548 pSelect->selFlags &= ~SF_View;
114551 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
114558 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
114562 if( iCol<sParse.pNewTable->nCol ){
114564 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
114568 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
114570 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
114571 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
114572 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
114574 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
114575 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
114578 for(i=0; i<sParse.pNewTable->nCol; i++){
114580 &sParse.pNewTable->aCol[i]);
114587 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
114588 for(i=0; i<pFKey->nCol; i++){
114589 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
114590 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
114592 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
114593 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
114595 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
114601 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
114602 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114609 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
114610 if( pStep->zTarget ){
114611 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
114613 if( pStep->pUpsert ){
114614 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
114617 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
114618 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
114626 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
114650 db->xAuth = xAuth;
114659 RenameCtx *p = pWalker->u.pRename;
114660 if( pExpr->op==TK_COLUMN
114662 && p->pTab==pExpr->y.pTab
114664 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
114674 RenameCtx *p = pWalker->u.pRename;
114675 SrcList *pSrc = pSelect->pSrc;
114676 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
114677 testcase( pSelect->selFlags & SF_View );
114678 testcase( pSelect->selFlags & SF_CopyCte );
114682 assert( pWalker->pParse->db->mallocFailed );
114685 for(i=0; i<pSrc->nSrc; i++){
114686 SrcItem *pItem = &pSrc->a[i];
114687 if( pItem->pTab==p->pTab ){
114688 renameTokenFind(pWalker->pParse, p, pItem->zName);
114714 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
114737 sqlite3_xauth xAuth = db->xAuth;
114738 db->xAuth = 0;
114754 int isLegacy = (db->flags & SQLITE_LegacyAlter);
114760 Select *pSelect = pTab->u.view.pSelect;
114765 assert( pSelect->selFlags & SF_View );
114766 pSelect->selFlags &= ~SF_View;
114767 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
114771 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
114777 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
114782 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
114783 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
114784 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
114793 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
114796 sqlite3WalkExprList(&sWalker, pTab->pCheck);
114798 renameTokenFind(&sParse, &sCtx, pTab->zName);
114804 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
114806 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114814 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
114815 && sCtx.pTab->pSchema==pTrigger->pTabSchema
114817 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
114824 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
114825 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
114826 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
114828 if( pStep->pFrom ){
114830 for(i=0; i<pStep->pFrom->nSrc; i++){
114831 SrcItem *pItem = &pStep->pFrom->a[i];
114832 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
114833 renameTokenFind(&sParse, &sCtx, pItem->zName);
114861 db->xAuth = xAuth;
114869 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
114870 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
114878 ** double-quotes use single quotes instead.
114912 sqlite3_xauth xAuth = db->xAuth;
114913 db->xAuth = 0;
114938 Select *pSelect = sParse.pNewTable->u.view.pSelect;
114939 pSelect->selFlags &= ~SF_View;
114942 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
114948 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
114950 for(i=0; i<sParse.pNewTable->nCol; i++){
114953 &sParse.pNewTable->aCol[i]));
114958 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
114959 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114985 db->xAuth = xAuth;
115024 int isLegacy = (db->flags & SQLITE_LegacyAlter);
115029 sqlite3_xauth xAuth = db->xAuth;
115030 db->xAuth = 0;
115038 int flags = db->flags;
115039 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
115041 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
115047 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
115056 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
115074 db->xAuth = xAuth;
115083 ** argv[0]: An integer - the index of the schema containing the table
115085 ** argv[2]: An integer - the index of the column to remove.
115099 const char *zDb = db->aDb[iSchema].zDbSName;
115108 sqlite3_xauth xAuth = db->xAuth;
115109 db->xAuth = 0;
115116 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
115122 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
115123 if( iCol<pTab->nCol-1 ){
115125 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
115126 zEnd = (const char*)pEnd->t.z;
115129 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
115130 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
115133 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
115134 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
115140 db->xAuth = xAuth;
115156 sqlite3 *db = pParse->db; /* Database handle */
115161 int iCol; /* Index of column zCol in pTab->aCol[] */
115164 assert( pParse->pNewTable==0 );
115166 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
115167 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
115178 assert( db->mallocFailed );
115189 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
115191 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
115198 if( pTab->nCol<=1 ){
115204 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115206 zDb = db->aDb[iDb].zDbSName;
115209 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
115219 , zDb, iDb, iCol, pTab->zName
115227 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
115233 int nField = 0; /* Number of non-virtual columns after drop */
115236 iCur = pParse->nTab++;
115239 reg = ++pParse->nMem;
115242 pParse->nMem += pTab->nCol;
115245 pParse->nMem += pPk->nColumn;
115246 for(i=0; i<pPk->nKeyCol; i++){
115249 nField = pPk->nKeyCol;
115251 regRec = ++pParse->nMem;
115252 for(i=0; i<pTab->nCol; i++){
115253 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
115258 if( iPos<pPk->nKeyCol ) continue;
115259 regOut = reg+1+iPos-(iPos>iColPos);
115263 if( i==pTab->iPKey ){
115273 pParse->nMem++;
115279 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
115295 ** Register built-in functions used to help implement ALTER TABLE
115312 ** 2005-07-08
115362 ** columns. The N-th integer (for N>1) is the average number of rows in
115363 ** the index which have the same value for the first N-1 columns. For
115364 ** a K-column index, there will be K+1 integers in the stat column. If
115388 ** inclusive are samples of the left-most key value in the index taken at
115395 ** For i between 0 and S-1. Conceptually, the index space is divided into
115422 ** of entries in the index whose left-most column exactly matches
115423 ** the left-most column of the sample. The second integer in nEq
115429 ** left-most column is less than the left-most column of the sample.
115430 ** The K-th integer in the nLt entry is the number of index entries
115445 ** looks at the left-most column of the index. The sqlite_stat3.sample
115446 ** column contains the actual value of the left-most column instead
115467 ** appropriate compile-time options are provided.
115496 sqlite3 *db = pParse->db;
115510 pDb = &db->aDb[iDb];
115519 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
115522 ** side-effect of the CREATE TABLE statement is to leave the rootpage
115523 ** of the new table in register pParse->regRoot. This is important
115526 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
115528 aRoot[i] = (u32)pParse->regRoot;
115535 aRoot[i] = pStat->tnum;
115540 pDb->zDbSName, zTab, zWhereType, zWhere
115543 }else if( db->xPreUpdateCallback ){
115544 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
115570 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
115595 int nLimit; /* Analysis row-scan limit */
115598 u8 nSkipAhead; /* Number of times of skip-ahead */
115603 u32 iPrn; /* Pseudo-random number used for sampling */
115618 if( p->nRowid ){
115619 sqlite3DbFree(db, p->u.aRowid);
115620 p->nRowid = 0;
115630 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
115631 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
115632 if( p->u.aRowid ){
115633 p->nRowid = n;
115634 memcpy(p->u.aRowid, pData, n);
115636 p->nRowid = 0;
115646 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
115647 p->nRowid = 0;
115648 p->u.iRowid = iRowid;
115658 pTo->isPSample = pFrom->isPSample;
115659 pTo->iCol = pFrom->iCol;
115660 pTo->iHash = pFrom->iHash;
115661 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
115662 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
115663 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
115664 if( pFrom->nRowid ){
115665 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
115667 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
115678 if( p->mxSample ){
115680 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
115681 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
115682 sampleClear(p->db, &p->current);
115685 sqlite3DbFree(p->db, p);
115694 ** L: A limit on the number of rows to scan, or 0 for no-limit
115752 p->db = db;
115753 p->nEst = sqlite3_value_int64(argv[2]);
115754 p->nRow = 0;
115755 p->nLimit = sqlite3_value_int64(argv[3]);
115756 p->nCol = nCol;
115757 p->nKeyCol = nKeyCol;
115758 p->nSkipAhead = 0;
115759 p->current.anDLt = (tRowcnt*)&p[1];
115760 p->current.anEq = &p->current.anDLt[nColUp];
115763 p->mxSample = p->nLimit==0 ? mxSample : 0;
115766 int i; /* Used to iterate through p->aSample[] */
115768 p->iGet = -1;
115769 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
115770 p->current.anLt = &p->current.anEq[nColUp];
115771 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
115774 p->a = (struct StatSample*)&p->current.anLt[nColUp];
115775 p->aBest = &p->a[mxSample];
115776 pSpace = (u8*)(&p->a[mxSample+nCol]);
115778 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115779 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115780 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115782 assert( (pSpace - (u8*)p)==n );
115785 p->aBest[i].iCol = i;
115810 ** pNew and pOld are both candidate non-periodic samples selected for
115811 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
115818 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
115825 int nCol = pAccum->nCol;
115827 assert( pNew->iCol==pOld->iCol );
115828 for(i=pNew->iCol+1; i<nCol; i++){
115829 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
115830 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
115832 if( pNew->iHash>pOld->iHash ) return 1;
115842 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
115849 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
115850 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
115852 assert( pOld->isPSample==0 && pNew->isPSample==0 );
115853 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
115857 if( pNew->iCol<pOld->iCol ) return 1;
115858 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
115864 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
115865 ** remove the least desirable sample from p->a[] to make room.
115877 if( nEqZero>p->nMaxEqZero ){
115878 p->nMaxEqZero = nEqZero;
115880 if( pNew->isPSample==0 ){
115882 assert( pNew->anEq[pNew->iCol]>0 );
115889 for(i=p->nSample-1; i>=0; i--){
115890 StatSample *pOld = &p->a[i];
115891 if( pOld->anEq[pNew->iCol]==0 ){
115892 if( pOld->isPSample ) return;
115893 assert( pOld->iCol>pNew->iCol );
115901 pUpgrade->iCol = pNew->iCol;
115902 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
115908 if( p->nSample>=p->mxSample ){
115909 StatSample *pMin = &p->a[p->iMin];
115910 tRowcnt *anEq = pMin->anEq;
115911 tRowcnt *anLt = pMin->anLt;
115912 tRowcnt *anDLt = pMin->anDLt;
115913 sampleClear(p->db, pMin);
115914 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
115915 pSample = &p->a[p->nSample-1];
115916 pSample->nRowid = 0;
115917 pSample->anEq = anEq;
115918 pSample->anDLt = anDLt;
115919 pSample->anLt = anLt;
115920 p->nSample = p->mxSample-1;
115923 /* The "rows less-than" for the rowid column must be greater than that
115924 ** for the last sample in the p->a[] array. Otherwise, the samples would
115926 assert( p->nSample==0
115927 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
115930 pSample = &p->a[p->nSample];
115932 p->nSample++;
115935 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
115938 if( p->nSample>=p->mxSample ){
115939 int iMin = -1;
115940 for(i=0; i<p->mxSample; i++){
115941 if( p->a[i].isPSample ) continue;
115942 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
115947 p->iMin = iMin;
115955 ** p->current contains a sample that reflects the previous row of the
115964 for(i=(p->nCol-2); i>=iChng; i--){
115965 StatSample *pBest = &p->aBest[i];
115966 pBest->anEq[i] = p->current.anEq[i];
115967 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
115973 ** p->nMaxEqZero or greater set to zero. */
115974 for(i=p->nSample-1; i>=0; i--){
115976 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
115980 if( iChng<p->nMaxEqZero ){
115981 for(i=p->nSample-1; i>=0; i--){
115983 for(j=iChng; j<p->nCol; j++){
115984 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
115987 p->nMaxEqZero = iChng;
115997 ** C Index of left-most column to differ from previous row
116007 ** if it wants the byte-code to do special processing.
116024 assert( p->nCol>0 );
116025 assert( iChng<p->nCol );
116027 if( p->nRow==0 ){
116029 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
116033 if( p->mxSample ) samplePushPrevious(p, iChng);
116039 p->current.anEq[i]++;
116041 for(i=iChng; i<p->nCol; i++){
116042 p->current.anDLt[i]++;
116044 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
116046 p->current.anEq[i] = 1;
116050 p->nRow++;
116052 if( p->mxSample ){
116055 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
116057 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
116060 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
116062 nLt = p->current.anLt[p->nCol-1];
116064 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
116065 p->current.isPSample = 1;
116066 p->current.iCol = 0;
116067 sampleInsert(p, &p->current, p->nCol-1);
116068 p->current.isPSample = 0;
116072 for(i=0; i<(p->nCol-1); i++){
116073 p->current.iCol = i;
116074 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
116075 sampleCopy(p, &p->aBest[i], &p->current);
116080 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
116081 p->nSkipAhead++;
116082 sqlite3_result_int(context, p->current.anDLt[0]>0);
116120 ** a one-parameter function, stat_get(P), that always returns the
116137 assert( eCall==STAT_GET_STAT1 || p->mxSample );
116162 ** I = (K+D-1)/D
116172 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
116174 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
116175 for(i=0; i<p->nKeyCol; i++){
116176 u64 nDistinct = p->current.anDLt[i] + 1;
116177 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
116178 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
116180 assert( p->current.anEq[i] );
116186 if( p->iGet<0 ){
116188 p->iGet = 0;
116190 if( p->iGet<p->nSample ){
116191 StatSample *pS = p->a + p->iGet;
116192 if( pS->nRowid==0 ){
116193 sqlite3_result_int64(context, pS->u.iRowid);
116195 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
116204 assert( p->iGet<p->nSample );
116206 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
116207 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
116209 aCnt = p->a[p->iGet].anDLt;
116210 p->iGet++;
116214 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
116215 for(i=0; i<p->nCol; i++){
116218 if( sStat.nChar ) sStat.nChar--;
116240 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
116253 ** of the k-th column of the pIdx index.
116261 assert( k>=0 && k<pIdx->nColumn );
116262 i = pIdx->aiColumn[k];
116264 VdbeComment((v,"%s.rowid",pIdx->zName));
116266 assert( pIdx->bHasExpr );
116267 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
116269 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
116288 sqlite3 *db = pParse->db; /* Database handle */
116294 int jZeroRows = -1; /* Jump from here if number of rows is zero */
116308 int doOnce = 1; /* Flag for a one-time computation */
116324 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
116329 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116333 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
116334 db->aDb[iDb].zDbSName ) ){
116340 if( db->xPreUpdateCallback ){
116343 pStat1->zName = (char*)&pStat1[1];
116344 memcpy(pStat1->zName, "sqlite_stat1", 13);
116345 pStat1->nCol = 3;
116346 pStat1->iPKey = -1;
116347 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
116351 /* Establish a read-lock on the table at the shared-cache level.
116352 ** Open a read-only cursor on the table. Also allocate a cursor number
116355 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
116358 pParse->nTab = MAX(pParse->nTab, iTab);
116360 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
116362 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116370 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
116372 nCol = pIdx->nKeyCol;
116373 zIdxName = pTab->zName;
116374 nColTest = nCol - 1;
116376 nCol = pIdx->nColumn;
116377 zIdxName = pIdx->zName;
116378 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
116383 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
116386 ** Pseudo-code for loop that calls stat_push():
116423 /* Open a read-only cursor on the index being analyzed. */
116424 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
116425 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
116427 VdbeComment((v, "%s", pIdx->zName));
116438 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
116452 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
116485 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
116486 /* For a single-column UNIQUE index, once we have found a non-NULL
116493 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
116513 sqlite3VdbeJumpHere(v, addrNextRow-1);
116536 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
116538 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
116539 for(j=0; j<pPk->nKeyCol; j++){
116540 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
116541 assert( k>=0 && k<pIdx->nColumn );
116545 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
116546 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
116554 if( db->nAnalysisLimit ){
116576 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
116582 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
116598 for(pX=pTab->pIndex; pX; pX=pX->pNext){
116601 nColX = pX->nKeyCol;
116603 nColX = pX->nColumn;
116614 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
116618 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
116639 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
116653 VdbeComment((v, "%s", pTab->zName));
116663 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
116685 sqlite3 *db = pParse->db;
116686 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
116693 iStatCur = pParse->nTab;
116694 pParse->nTab += 3;
116696 iMem = pParse->nMem+1;
116697 iTab = pParse->nTab;
116699 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
116721 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116722 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116724 iStatCur = pParse->nTab;
116725 pParse->nTab += 3;
116727 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
116729 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
116731 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
116739 ** ANALYZE -- 1
116740 ** ANALYZE <database> -- 2
116741 ** ANALYZE ?<database>.?<tablename> -- 3
116748 sqlite3 *db = pParse->db;
116759 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116767 for(i=0; i<db->nDb; i++){
116771 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
116778 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
116782 analyzeTable(pParse, pIdx->pTable, pIdx);
116790 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
116806 ** The first argument points to a nul-terminated string containing a
116830 v = v*10 + c - '0';
116849 pIndex->bUnordered = 0;
116850 pIndex->noSkipScan = 0;
116853 pIndex->bUnordered = 1;
116854 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
116857 pIndex->szIdxRow = sqlite3LogEst(sz);
116859 pIndex->noSkipScan = 1;
116862 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
116863 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
116878 ** argv[2] = results of analysis - on integer for each column
116895 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
116904 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
116910 int nCol = pIndex->nKeyCol+1;
116915 if( pIndex->aiRowEst==0 ){
116916 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
116917 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
116919 aiRowEst = pIndex->aiRowEst;
116921 pIndex->bUnordered = 0;
116922 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
116923 pIndex->hasStat1 = 1;
116924 if( pIndex->pPartIdxWhere==0 ){
116925 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
116926 pTable->tabFlags |= TF_HasStat1;
116930 fakeIdx.szIdxRow = pTable->szTabRow;
116934 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
116935 pTable->szTabRow = fakeIdx.szIdxRow;
116936 pTable->tabFlags |= TF_HasStat1;
116950 if( pIdx->aSample ){
116952 for(j=0; j<pIdx->nSample; j++){
116953 IndexSample *p = &pIdx->aSample[j];
116954 sqlite3DbFree(db, p->p);
116956 sqlite3DbFree(db, pIdx->aSample);
116958 if( db->pnBytesFreed==0 ){
116959 pIdx->nSample = 0;
116960 pIdx->aSample = 0;
116970 ** Populate the pIdx->aAvgEq[] array based on the samples currently
116971 ** stored in pIdx->aSample[].
116975 IndexSample *aSample = pIdx->aSample;
116976 IndexSample *pFinal = &aSample[pIdx->nSample-1];
116979 if( pIdx->nSampleCol>1 ){
116984 nCol = pIdx->nSampleCol-1;
116985 pIdx->aAvgEq[nCol] = 1;
116988 int nSample = pIdx->nSample;
116996 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
116997 nRow = pFinal->anLt[iCol];
116998 nDist100 = (i64)100 * pFinal->anDLt[iCol];
116999 nSample--;
117001 nRow = pIdx->aiRowEst[0];
117002 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
117004 pIdx->nRowEst0 = nRow;
117011 if( i==(pIdx->nSample-1)
117020 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
117023 pIdx->aAvgEq[iCol] = avgEq;
117067 IndexSample *pSample; /* A slot in pIdx->aSample[] */
117069 assert( db->lookaside.bDisable );
117074 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
117092 assert( pIdx==0 || pIdx->nSample==0 );
117094 if( pIdx->aSample!=0 ){
117098 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
117099 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
117100 nIdxCol = pIdx->nKeyCol;
117102 nIdxCol = pIdx->nColumn;
117104 pIdx->nSampleCol = nIdxCol;
117105 pIdx->mxSample = nSample;
117110 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
117111 if( pIdx->aSample==0 ){
117115 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
117116 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
117117 pIdx->pTable->tabFlags |= TF_HasStat4;
117119 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
117120 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
117121 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
117123 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
117132 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
117145 if( pIdx->nSample>=pIdx->mxSample ){
117152 nCol = pIdx->nSampleCol;
117157 pSample = &pIdx->aSample[pIdx->nSample];
117158 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
117159 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
117160 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
117168 pSample->n = sqlite3_column_bytes(pStmt, 4);
117169 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
117170 if( pSample->p==0 ){
117174 if( pSample->n ){
117175 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
117177 pIdx->nSample++;
117192 assert( db->lookaside.bDisable );
117223 ** If an OOM error occurs, this function always sets db->mallocFailed.
117232 Schema *pSchema = db->aDb[iDb].pSchema;
117235 assert( iDb>=0 && iDb<db->nDb );
117236 assert( db->aDb[iDb].pBt!=0 );
117240 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
117242 pTab->tabFlags &= ~TF_HasStat1;
117244 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117246 pIdx->hasStat1 = 0;
117249 pIdx->aSample = 0;
117255 sInfo.zDatabase = db->aDb[iDb].zDbSName;
117271 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117273 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
117283 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117285 sqlite3_free(pIdx->aiRowEst);
117286 pIdx->aiRowEst = 0;
117339 if( pExpr->op!=TK_ID ){
117342 pExpr->op = TK_STRING;
117354 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
117360 ** An SQL user-function registered to do the work of an ATTACH statement. The
117370 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
117371 ** new database, close the database on db->init.iDb and reopen it as an
117399 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
117406 ** from sqlite3_deserialize() to close database db->init.iDb and
117418 pNew = &db->aDb[db->init.iDb];
117419 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
117420 pNew->pBt = pNewBt;
117421 pNew->pSchema = pNewSchema;
117437 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
117438 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
117439 db->aLimit[SQLITE_LIMIT_ATTACHED]
117443 for(i=0; i<db->nDb; i++){
117451 /* Allocate the new entry in the db->aDb[] array and initialize the schema
117454 if( db->aDb==db->aDbStatic ){
117455 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
117457 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
117459 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
117462 db->aDb = aNew;
117463 pNew = &db->aDb[db->nDb];
117470 flags = db->openFlags;
117471 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
117474 sqlite3_result_error(context, zErr, -1);
117480 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
117481 db->nDb++;
117482 pNew->zDbSName = sqlite3DbStrDup(db, zName);
117484 db->noSharedCache = 0;
117490 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
117491 if( !pNew->pSchema ){
117493 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
117498 sqlite3BtreeEnter(pNew->pBt);
117499 pPager = sqlite3BtreePager(pNew->pBt);
117500 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
117501 sqlite3BtreeSecureDelete(pNew->pBt,
117502 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
117504 sqlite3BtreeSetPagerFlags(pNew->pBt,
117505 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
117507 sqlite3BtreeLeave(pNew->pBt);
117509 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
117510 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
117517 ** remove the entry from the db->aDb[] array. i.e. put everything back the
117522 db->init.iDb = 0;
117523 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
117534 if( newAuth<db->auth.authLevel ){
117541 int iDb = db->nDb - 1;
117543 if( db->aDb[iDb].pBt ){
117544 sqlite3BtreeClose(db->aDb[iDb].pBt);
117545 db->aDb[iDb].pBt = 0;
117546 db->aDb[iDb].pSchema = 0;
117549 db->nDb = iDb;
117566 sqlite3_result_error(context, zErrDyn, -1);
117573 ** An SQL user-function registered to do the work of an DETACH statement. The
117595 for(i=0; i<db->nDb; i++){
117596 pDb = &db->aDb[i];
117597 if( pDb->pBt==0 ) continue;
117601 if( i>=db->nDb ){
117609 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
117610 || sqlite3BtreeIsInBackup(pDb->pBt)
117618 assert( db->aDb[1].pSchema );
117619 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
117622 if( pTrig->pTabSchema==pDb->pSchema ){
117623 pTrig->pTabSchema = pTrig->pSchema;
117628 sqlite3BtreeClose(pDb->pBt);
117629 pDb->pBt = 0;
117630 pDb->pSchema = 0;
117635 sqlite3_result_error(context, zErr, -1);
117654 sqlite3* db = pParse->db;
117659 if( pParse->nErr ) goto attach_end;
117674 if( pAuthArg->op==TK_STRING ){
117676 zAuthArg = pAuthArg->u.zToken;
117694 assert( v || db->mallocFailed );
117696 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
117697 pFunc->nArg, pFunc, 0);
117756 DbFixer *pFix = p->u.pFix;
117757 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
117758 if( pExpr->op==TK_VARIABLE ){
117759 if( pFix->pParse->db->init.busy ){
117760 pExpr->op = TK_NULL;
117762 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
117773 DbFixer *pFix = p->u.pFix;
117776 sqlite3 *db = pFix->pParse->db;
117777 int iDb = sqlite3FindDbName(db, pFix->zDb);
117778 SrcList *pList = pSelect->pSrc;
117781 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
117782 if( pFix->bTemp==0 ){
117783 if( pItem->zDatabase ){
117784 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
117785 sqlite3ErrorMsg(pFix->pParse,
117787 pFix->zType, pFix->pName, pItem->zDatabase);
117790 sqlite3DbFree(db, pItem->zDatabase);
117791 pItem->zDatabase = 0;
117792 pItem->fg.notCte = 1;
117794 pItem->pSchema = pFix->pSchema;
117795 pItem->fg.fromDDL = 1;
117798 if( pList->a[i].fg.isUsing==0
117799 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
117805 if( pSelect->pWith ){
117806 for(i=0; i<pSelect->pWith->nCte; i++){
117807 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
117826 sqlite3 *db = pParse->db;
117827 assert( db->nDb>iDb );
117828 pFix->pParse = pParse;
117829 pFix->zDb = db->aDb[iDb].zDbSName;
117830 pFix->pSchema = db->aDb[iDb].pSchema;
117831 pFix->zType = zType;
117832 pFix->pName = pName;
117833 pFix->bTemp = (iDb==1);
117834 pFix->w.pParse = pParse;
117835 pFix->w.xExprCallback = fixExprCb;
117836 pFix->w.xSelectCallback = fixSelectCb;
117837 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
117838 pFix->w.walkerDepth = 0;
117839 pFix->w.eCode = 0;
117840 pFix->w.u.pFix = pFix;
117854 ** pParse->zErrMsg and these routines return non-zero. If everything
117866 res = sqlite3WalkSelect(&pFix->w, &s);
117875 return sqlite3WalkSelect(&pFix->w, pSelect);
117881 return sqlite3WalkExpr(&pFix->w, pExpr);
117891 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
117892 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
117893 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
117894 || sqlite3FixSrcList(pFix, pStep->pFrom)
117901 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
117902 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
117903 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
117904 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
117905 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
117912 pStep = pStep->pNext;
117935 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
117982 ** means that the SQL statement will never-run - the sqlite3_exec() call
117998 sqlite3_mutex_enter(db->mutex);
117999 db->xAuth = (sqlite3_xauth)xAuth;
118000 db->pAuthArg = pArg;
118001 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
118002 sqlite3_mutex_leave(db->mutex);
118007 ** Write an error message into pParse->zErrMsg that explains that the
118008 ** user-supplied authorization function returned an illegal value.
118012 pParse->rc = SQLITE_ERROR;
118030 sqlite3 *db = pParse->db; /* Database handle */
118031 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
118034 if( db->init.busy ) return SQLITE_OK;
118035 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
118037 ,db->auth.zAuthUser
118042 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
118044 pParse->rc = SQLITE_AUTH;
118068 int iSrc; /* Index in pTabList->a[] of table being read */
118072 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
118074 assert( pParse->db->xAuth!=0 );
118075 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
118082 if( pExpr->op==TK_TRIGGER ){
118083 pTab = pParse->pTriggerTab;
118086 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
118087 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
118088 pTab = pTabList->a[iSrc].pTab;
118093 iCol = pExpr->iColumn;
118097 assert( iCol<pTab->nCol );
118098 zCol = pTab->aCol[iCol].zCnName;
118099 }else if( pTab->iPKey>=0 ){
118100 assert( pTab->iPKey<pTab->nCol );
118101 zCol = pTab->aCol[pTab->iPKey].zCnName;
118105 assert( iDb>=0 && iDb<pParse->db->nDb );
118106 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
118107 pExpr->op = TK_NULL;
118124 sqlite3 *db = pParse->db;
118130 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
118131 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
118135 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
118136 ** callback are either NULL pointers or zero-terminated strings that
118144 testcase( pParse->zAuthContext==0 );
118146 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
118148 ,db->auth.zAuthUser
118153 pParse->rc = SQLITE_AUTH;
118164 ** popped. Or if pParse==0, this routine is a no-op.
118172 pContext->pParse = pParse;
118173 pContext->zAuthContext = pParse->zAuthContext;
118174 pParse->zAuthContext = zContext;
118182 if( pContext->pParse ){
118183 pContext->pParse->zAuthContext = pContext->zAuthContext;
118184 pContext->pParse = 0;
118231 ** Record the fact that we want to lock a table at run-time.
118254 for(i=0; i<pToplevel->nTableLock; i++){
118255 p = &pToplevel->aTableLock[i];
118256 if( p->iDb==iDb && p->iTab==iTab ){
118257 p->isWriteLock = (p->isWriteLock || isWriteLock);
118262 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
118263 pToplevel->aTableLock =
118264 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
118265 if( pToplevel->aTableLock ){
118266 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
118267 p->iDb = iDb;
118268 p->iTab = iTab;
118269 p->isWriteLock = isWriteLock;
118270 p->zLockName = zName;
118272 pToplevel->nTableLock = 0;
118273 sqlite3OomFault(pToplevel->db);
118284 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
118294 Vdbe *pVdbe = pParse->pVdbe;
118297 for(i=0; i<pParse->nTableLock; i++){
118298 TableLock *p = &pParse->aTableLock[i];
118299 int p1 = p->iDb;
118300 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
118301 p->zLockName, P4_STATIC);
118309 ** Return TRUE if the given yDbMask object is empty - if it contains no
118336 assert( pParse->pToplevel==0 );
118337 db = pParse->db;
118338 assert( db->pParse==pParse );
118339 if( pParse->nested ) return;
118340 if( pParse->nErr ){
118341 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
118344 assert( db->mallocFailed==0 );
118349 v = pParse->pVdbe;
118351 if( db->init.busy ){
118352 pParse->rc = SQLITE_DONE;
118356 if( v==0 ) pParse->rc = SQLITE_ERROR;
118358 assert( !pParse->isMultiWrite
118359 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
118361 if( pParse->bReturning ){
118362 Returning *pReturning = pParse->u1.pReturning;
118366 if( pReturning->nRetCol ){
118369 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
118371 reg = pReturning->iRetReg;
118372 for(i=0; i<pReturning->nRetCol; i++){
118373 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
118376 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
118384 if( pParse->nTableLock>0 && db->init.busy==0 ){
118386 if( db->auth.authLevel<UAUTH_User ){
118388 pParse->rc = SQLITE_AUTH_USER;
118400 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
118402 assert( db->nDb>0 );
118406 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
118408 pSchema = db->aDb[iDb].pSchema;
118412 DbMaskTest(pParse->writeMask,iDb), /* P2 */
118413 pSchema->schema_cookie, /* P3 */
118414 pSchema->iGeneration /* P4 */
118416 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
118418 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
118419 }while( ++iDb<db->nDb );
118421 for(i=0; i<pParse->nVtabLock; i++){
118422 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
118425 pParse->nVtabLock = 0;
118429 ** obtain the required table-locks. This is a no-op unless the
118430 ** shared-cache feature is enabled.
118445 if( pParse->pConstExpr ){
118446 ExprList *pEL = pParse->pConstExpr;
118447 pParse->okConstFactor = 0;
118448 for(i=0; i<pEL->nExpr; i++){
118449 int iReg = pEL->a[i].u.iConstExprReg;
118450 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
118454 if( pParse->bReturning ){
118455 Returning *pRet = pParse->u1.pReturning;
118456 if( pRet->nRetCol ){
118457 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
118467 assert( v!=0 || pParse->nErr );
118468 assert( db->mallocFailed==0 || pParse->nErr );
118469 if( pParse->nErr==0 ){
118472 assert( pParse->pAinc==0 || pParse->nTab>0 );
118474 pParse->rc = SQLITE_DONE;
118476 pParse->rc = SQLITE_ERROR;
118489 ** * Built-in SQL functions always take precedence over application-defined
118491 ** built-in function.
118496 sqlite3 *db = pParse->db;
118497 u32 savedDbFlags = db->mDbFlags;
118500 if( pParse->nErr ) return;
118501 if( pParse->eParseMode ) return;
118502 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
118510 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
118511 pParse->nErr++;
118514 pParse->nested++;
118517 db->mDbFlags |= DBFLAG_PreferBuiltin;
118519 db->mDbFlags = savedDbFlags;
118522 pParse->nested--;
118536 ** Locate the in-memory structure that describes a particular database
118556 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
118561 for(i=0; i<db->nDb; i++){
118562 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
118564 if( i>=db->nDb ){
118573 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
118580 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
118585 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
118592 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
118595 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
118598 for(i=2; i<db->nDb; i++){
118600 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
118605 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
118607 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
118616 ** Locate the in-memory structure that describes a particular database
118619 ** error message in pParse->zErrMsg.
118622 ** routine leaves an error message in pParse->zErrMsg where
118632 sqlite3 *db = pParse->db;
118636 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
118648 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
118649 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
118654 testcase( pMod->pEpoTab==0 );
118655 return pMod->pEpoTab;
118660 pParse->checkSchema = 1;
118661 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
118673 assert( HasRowid(p) || p->iPKey<0 );
118684 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
118685 ** non-NULL if it is part of a view or trigger program definition. See
118694 assert( p->pSchema==0 || p->zDatabase==0 );
118695 if( p->pSchema ){
118696 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
118697 zDb = pParse->db->aDb[iDb].zDbSName;
118699 zDb = p->zDatabase;
118701 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
118721 ** Locate the in-memory structure that describes
118737 for(i=OMIT_TEMPDB; i<db->nDb; i++){
118739 Schema *pSchema = db->aDb[j].pSchema;
118743 p = sqlite3HashFind(&pSchema->idxHash, zName);
118756 sqlite3ExprDelete(db, p->pPartIdxWhere);
118757 sqlite3ExprListDelete(db, p->aColExpr);
118758 sqlite3DbFree(db, p->zColAff);
118759 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
118761 sqlite3_free(p->aiRowEst);
118777 pHash = &db->aDb[iDb].pSchema->idxHash;
118780 if( pIndex->pTable->pIndex==pIndex ){
118781 pIndex->pTable->pIndex = pIndex->pNext;
118786 p = pIndex->pTable->pIndex;
118787 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
118788 if( ALWAYS(p && p->pNext==pIndex) ){
118789 p->pNext = pIndex->pNext;
118794 db->mDbFlags |= DBFLAG_SchemaChange;
118798 ** Look through the list of open database files in db->aDb[] and if
118800 ** db->aDb[] structure to a smaller size, if possible.
118807 for(i=j=2; i<db->nDb; i++){
118808 struct Db *pDb = &db->aDb[i];
118809 if( pDb->pBt==0 ){
118810 sqlite3DbFree(db, pDb->zDbSName);
118811 pDb->zDbSName = 0;
118815 db->aDb[j] = db->aDb[i];
118819 db->nDb = j;
118820 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
118821 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
118822 sqlite3DbFree(db, db->aDb);
118823 db->aDb = db->aDbStatic;
118829 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
118834 assert( iDb<db->nDb );
118840 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
118843 if( db->nSchemaLock==0 ){
118844 for(i=0; i<db->nDb; i++){
118846 sqlite3SchemaClear(db->aDb[i].pSchema);
118859 for(i=0; i<db->nDb; i++){
118860 Db *pDb = &db->aDb[i];
118861 if( pDb->pSchema ){
118862 if( db->nSchemaLock==0 ){
118863 sqlite3SchemaClear(pDb->pSchema);
118869 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
118872 if( db->nSchemaLock==0 ){
118881 db->mDbFlags &= ~DBFLAG_SchemaChange;
118897 pList = pTab->u.tab.pDfltList;
118898 if( pCol->iDflt==0
118900 || NEVER(pList->nExpr<pCol->iDflt)
118902 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
118903 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
118905 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
118906 pList->a[pCol->iDflt-1].pExpr = pExpr;
118916 if( pCol->iDflt==0 ) return 0;
118918 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
118919 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
118920 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
118935 n = sqlite3Strlen30(pCol->zCnName) + 1;
118936 if( pCol->colFlags & COLFLAG_HASTYPE ){
118937 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
118940 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
118942 pCol->zCnName = zNew;
118943 memcpy(pCol->zCnName + n, zColl, nColl);
118944 pCol->colFlags |= COLFLAG_HASCOLL;
118953 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
118954 z = pCol->zCnName;
118956 if( pCol->colFlags & COLFLAG_HASTYPE ){
118971 if( (pCol = pTable->aCol)!=0 ){
118972 for(i=0; i<pTable->nCol; i++, pCol++){
118973 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
118974 sqlite3DbFree(db, pCol->zCnName);
118976 sqlite3DbNNFreeNN(db, pTable->aCol);
118978 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
118980 if( db->pnBytesFreed==0 ){
118981 pTable->aCol = 0;
118982 pTable->nCol = 0;
118984 pTable->u.tab.pDfltList = 0;
119002 ** db parameter can be used with db->pnBytesFreed to measure the memory
119018 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
119024 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
119025 pNext = pIndex->pNext;
119026 assert( pIndex->pSchema==pTable->pSchema
119027 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
119028 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
119029 char *zName = pIndex->zName;
119031 &pIndex->pSchema->idxHash, zName, 0
119033 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
119049 sqlite3SelectDelete(db, pTable->u.view.pSelect);
119055 sqlite3DbFree(db, pTable->zName);
119056 sqlite3DbFree(db, pTable->zColAff);
119057 sqlite3ExprListDelete(db, pTable->pCheck);
119067 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
119081 assert( iDb>=0 && iDb<db->nDb );
119084 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
119085 pDb = &db->aDb[iDb];
119086 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
119088 db->mDbFlags |= DBFLAG_SchemaChange;
119107 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
119123 if( p->nTab==0 ){
119124 p->nTab = 1;
119129 ** Parameter zName points to a nul-terminated buffer containing the name
119131 ** function returns the index of the named database in db->aDb[], or
119132 ** -1 if the named db cannot be found.
119135 int i = -1; /* Database number */
119138 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
119139 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
119151 ** index of the named database in db->aDb[], or -1 if the named db
119186 sqlite3 *db = pParse->db;
119189 if( pName2->n>0 ){
119190 if( db->init.busy ) {
119192 return -1;
119198 return -1;
119201 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
119202 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
119203 iDb = db->init.iDb;
119213 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
119214 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119216 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119218 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119220 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
119224 ** This routine is used to check if the UTF-8 string zName is a legal
119240 sqlite3 *db = pParse->db;
119242 || db->init.imposterTable
119248 if( db->init.busy ){
119249 if( sqlite3_stricmp(zType, db->init.azInit[0])
119250 || sqlite3_stricmp(zName, db->init.azInit[1])
119251 || sqlite3_stricmp(zTblName, db->init.azInit[2])
119257 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
119274 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
119281 ** find the (first) offset of that column in index pIdx. Or return -1
119286 for(i=0; i<pIdx->nColumn; i++){
119287 if( iCol==pIdx->aiColumn[i] ) return i;
119289 return -1;
119302 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
119305 if( pTab->tabFlags & TF_HasVirtual ){
119308 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
119320 ** the N-th virtual column (zero-based) then the storage number is
119321 ** the number of non-virtual columns in the table plus N.
119337 ** -- 0 1 2 3 4 5 6 7 8
119348 ** this routine is a no-op macro. If the pTab does not have any virtual
119349 ** columns, then this routine is no-op that always return iCol. If iCol
119355 assert( iCol<pTab->nCol );
119356 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
119358 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
119360 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
119362 return pTab->nNVCol + i - n;
119376 ** is a read-only no-op.
119379 int iReg = ++pParse->nMem;
119397 ** The new table record is initialized and put in pParse->pNewTable.
119414 sqlite3 *db = pParse->db;
119419 if( db->init.busy && db->init.newTnum==1 ){
119421 iDb = db->init.iDb;
119428 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
119440 pParse->sNameToken = *pName;
119445 if( db->init.iDb==1 ) isTemp = 1;
119456 char *zDb = db->aDb[iDb].zDbSName;
119475 char *zDb = db->aDb[iDb].zDbSName;
119485 assert( !db->init.busy || CORRUPT_DB );
119499 assert( db->mallocFailed );
119500 pParse->rc = SQLITE_NOMEM_BKPT;
119501 pParse->nErr++;
119504 pTable->zName = zName;
119505 pTable->iPKey = -1;
119506 pTable->pSchema = db->aDb[iDb].pSchema;
119507 pTable->nTabRef = 1;
119509 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
119511 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
119513 assert( pParse->pNewTable==0 );
119514 pParse->pNewTable = pTable;
119524 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
119541 reg1 = pParse->regRowid = ++pParse->nMem;
119542 reg2 = pParse->regRoot = ++pParse->nMem;
119543 reg3 = ++pParse->nMem;
119547 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
119553 /* This just creates a place-holder record in the sqlite_schema table.
119557 ** The rowid for the new entry is left in register pParse->regRowid.
119558 ** The root page number of the new table is left in reg pParse->regRoot.
119568 assert( !pParse->bReturning );
119569 pParse->u1.addrCrTab =
119580 /* Normal (non-error) return. */
119585 pParse->checkSchema = 1;
119595 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
119596 pCol->colFlags |= COLFLAG_HIDDEN;
119597 if( pTab ) pTab->tabFlags |= TF_HasHidden;
119598 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
119599 pTab->tabFlags |= TF_OOOHidden;
119607 ** with any application-generated triggers.
119616 pHash = &(db->aDb[1].pSchema->trigHash);
119618 sqlite3ExprListDelete(db, pRet->pReturnEL);
119641 sqlite3 *db = pParse->db;
119642 if( pParse->pNewTrigger ){
119645 assert( pParse->bReturning==0 || pParse->ifNotExists );
119647 pParse->bReturning = 1;
119653 pParse->u1.pReturning = pRet;
119654 pRet->pParse = pParse;
119655 pRet->pReturnEL = pList;
119658 testcase( pParse->earlyCleanup );
119659 if( db->mallocFailed ) return;
119660 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
119661 pRet->retTrig.op = TK_RETURNING;
119662 pRet->retTrig.tr_tm = TRIGGER_AFTER;
119663 pRet->retTrig.bReturning = 1;
119664 pRet->retTrig.pSchema = db->aDb[1].pSchema;
119665 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
119666 pRet->retTrig.step_list = &pRet->retTStep;
119667 pRet->retTStep.op = TK_RETURNING;
119668 pRet->retTStep.pTrig = &pRet->retTrig;
119669 pRet->retTStep.pExprList = pList;
119670 pHash = &(db->aDb[1].pSchema->trigHash);
119672 || pParse->nErr || pParse->ifNotExists );
119673 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
119674 ==&pRet->retTrig ){
119693 sqlite3 *db = pParse->db;
119700 if( (p = pParse->pNewTable)==0 ) return;
119701 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
119702 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
119712 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
119714 sType.n -= 6;
119715 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119717 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
119719 sType.n -= 9;
119720 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119749 for(i=0; i<p->nCol; i++){
119750 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
119756 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
119761 p->aCol = aNew;
119762 pCol = &p->aCol[p->nCol];
119763 memset(pCol, 0, sizeof(p->aCol[0]));
119764 pCol->zCnName = z;
119765 pCol->hName = hName;
119771 pCol->affinity = affinity;
119772 pCol->eCType = eType;
119773 pCol->szEst = szEst;
119777 pCol->colFlags |= COLFLAG_SORTERREF;
119786 pCol->affinity = sqlite3AffinityType(zType, pCol);
119787 pCol->colFlags |= COLFLAG_HASTYPE;
119789 p->nCol++;
119790 p->nNVCol++;
119791 pParse->constraintName.n = 0;
119803 p = pParse->pNewTable;
119804 if( p==0 || NEVER(p->nCol<1) ) return;
119805 pCol = &p->aCol[p->nCol-1];
119806 pCol->notNull = (u8)onError;
119807 p->tabFlags |= TF_HasNotNull;
119811 if( pCol->colFlags & COLFLAG_UNIQUE ){
119813 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
119814 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
119815 if( pIdx->aiColumn[0]==p->nCol-1 ){
119816 pIdx->uniqNotNull = 1;
119826 ** This routine does a case-independent search of zType for the
119834 ** --------------------------------
119892 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
119899 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
119904 pCol->colFlags |= COLFLAG_SORTERREF;
119909 pCol->szEst = v;
119932 sqlite3 *db = pParse->db;
119933 p = pParse->pNewTable;
119935 int isInit = db->init.busy && db->init.iDb!=1;
119936 pCol = &(p->aCol[p->nCol-1]);
119939 pCol->zCnName);
119941 }else if( pCol->colFlags & COLFLAG_GENERATED ){
119942 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119943 testcase( pCol->colFlags & COLFLAG_STORED );
119984 if( p->op==TK_STRING ){
119985 p->op = TK_ID;
119986 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
119987 p->pLeft->op = TK_ID;
119995 pCol->colFlags |= COLFLAG_PRIMKEY;
119997 if( pCol->colFlags & COLFLAG_GENERATED ){
119998 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119999 testcase( pCol->colFlags & COLFLAG_STORED );
120018 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
120031 Table *pTab = pParse->pNewTable;
120033 int iCol = -1, i;
120036 if( pTab->tabFlags & TF_HasPrimaryKey ){
120038 "table \"%s\" has more than one primary key", pTab->zName);
120041 pTab->tabFlags |= TF_HasPrimaryKey;
120043 iCol = pTab->nCol - 1;
120044 pCol = &pTab->aCol[iCol];
120048 nTerm = pList->nExpr;
120050 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
120053 if( pCExpr->op==TK_ID ){
120056 zCName = pCExpr->u.zToken;
120057 for(iCol=0; iCol<pTab->nCol; iCol++){
120058 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
120059 pCol = &pTab->aCol[iCol];
120069 && pCol->eCType==COLTYPE_INTEGER
120073 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
120074 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
120076 pTab->iPKey = iCol;
120077 pTab->keyConf = (u8)onError;
120079 pTab->tabFlags |= autoInc*TF_Autoincrement;
120080 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
120094 sqlite3ExprListDelete(pParse->db, pList);
120108 Table *pTab = pParse->pNewTable;
120109 sqlite3 *db = pParse->db;
120111 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
120113 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
120114 if( pParse->constraintName.n ){
120115 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
120119 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
120121 t.n = (int)(zEnd - t.z);
120122 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
120127 sqlite3ExprDelete(pParse->db, pCheckExpr);
120141 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
120142 i = p->nCol-1;
120143 db = pParse->db;
120149 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
120155 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120156 assert( pIdx->nKeyCol==1 );
120157 if( pIdx->aiColumn[0]==i ){
120158 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
120171 Table *pTab = pParse->pNewTable;
120177 pCol = &(pTab->aCol[pTab->nCol-1]);
120182 if( pCol->iDflt>0 ) goto generated_error;
120184 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
120185 /* no-op */
120186 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
120192 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
120193 pCol->colFlags |= eType;
120196 pTab->tabFlags |= eType;
120197 if( pCol->colFlags & COLFLAG_PRIMKEY ){
120200 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
120207 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
120214 pCol->zCnName);
120216 sqlite3ExprDelete(pParse->db, pExpr);
120219 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
120221 sqlite3ExprDelete(pParse->db, pExpr);
120235 ** This plan is not completely bullet-proof. It is possible for
120241 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
120242 ** the schema-version whenever the schema changes.
120245 sqlite3 *db = pParse->db;
120246 Vdbe *v = pParse->pVdbe;
120249 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
120272 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
120276 ** If the string zSignedIdent consists entirely of alpha-numeric
120279 ** it is quoted using double-quotes.
120315 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
120316 n += identLength(pCol->zCnName) + 5;
120318 n += identLength(p->zName);
120328 n += 35 + 6*p->nCol;
120336 identPut(zStmt, &k, p->zName);
120338 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
120350 sqlite3_snprintf(n-k, &zStmt[k], zSep);
120353 identPut(zStmt, &k, pCol->zCnName);
120354 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
120355 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
120356 testcase( pCol->affinity==SQLITE_AFF_BLOB );
120357 testcase( pCol->affinity==SQLITE_AFF_TEXT );
120358 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
120359 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
120360 testcase( pCol->affinity==SQLITE_AFF_REAL );
120361 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
120363 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
120365 assert( pCol->affinity==SQLITE_AFF_BLOB
120366 || pCol->affinity==SQLITE_AFF_FLEXNUM
120367 || pCol->affinity==sqlite3AffinityType(zType, 0) );
120372 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
120383 if( pIdx->nColumn>=N ) return SQLITE_OK;
120384 assert( pIdx->isResized==0 );
120388 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
120389 pIdx->azColl = (const char**)zExtra;
120391 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
120392 pIdx->aiRowLogEst = (LogEst*)zExtra;
120394 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
120395 pIdx->aiColumn = (i16*)zExtra;
120397 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
120398 pIdx->aSortOrder = (u8*)zExtra;
120399 pIdx->nColumn = N;
120400 pIdx->isResized = 1;
120411 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
120412 wTable += pTabCol->szEst;
120414 if( pTab->iPKey<0 ) wTable++;
120415 pTab->szTabRow = sqlite3LogEst(wTable*4);
120424 const Column *aCol = pIdx->pTable->aCol;
120425 for(i=0; i<pIdx->nColumn; i++){
120426 i16 x = pIdx->aiColumn[i];
120427 assert( x<pIdx->pTable->nCol );
120428 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
120430 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
120438 while( nCol-- > 0 ){
120448 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
120461 assert( nKey<=pIdx->nColumn );
120462 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
120463 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
120464 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
120465 assert( pPk->pTable==pIdx->pTable );
120467 j = pPk->aiColumn[iCol];
120470 assert( pIdx->aiColumn[i]>=0 || j>=0 );
120471 if( pIdx->aiColumn[i]==j
120472 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
120485 ** high-order bit of colNotIdxed is always 1. All unindexed columns
120488 ** 2019-10-24: For the purpose of this computation, virtual columns are
120496 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
120502 Table *pTab = pIdx->pTable;
120503 for(j=pIdx->nColumn-1; j>=0; j--){
120504 int x = pIdx->aiColumn[j];
120505 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
120506 testcase( x==BMS-1 );
120507 testcase( x==BMS-2 );
120508 if( x<BMS-1 ) m |= MASKBIT(x);
120511 pIdx->colNotIdxed = ~m;
120512 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
120545 sqlite3 *db = pParse->db;
120546 Vdbe *v = pParse->pVdbe;
120550 if( !db->init.imposterTable ){
120551 for(i=0; i<pTab->nCol; i++){
120552 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
120553 && (pTab->aCol[i].notNull==OE_None)
120555 pTab->aCol[i].notNull = OE_Abort;
120558 pTab->tabFlags |= TF_HasNotNull;
120564 assert( !pParse->bReturning );
120565 if( pParse->u1.addrCrTab ){
120567 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
120573 if( pTab->iPKey>=0 ){
120576 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
120580 pTab->tabFlags &= ~TF_WithoutRowid;
120584 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
120586 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
120587 assert( pParse->pNewTable==pTab );
120588 pTab->iPKey = -1;
120589 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
120591 if( pParse->nErr ){
120592 pTab->tabFlags &= ~TF_WithoutRowid;
120595 assert( db->mallocFailed==0 );
120597 assert( pPk->nKeyCol==1 );
120607 for(i=j=1; i<pPk->nKeyCol; i++){
120609 pPk->nColumn--;
120611 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
120612 pPk->azColl[j] = pPk->azColl[i];
120613 pPk->aSortOrder[j] = pPk->aSortOrder[i];
120614 pPk->aiColumn[j++] = pPk->aiColumn[i];
120617 pPk->nKeyCol = j;
120620 pPk->isCovering = 1;
120621 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
120622 nPk = pPk->nColumn = pPk->nKeyCol;
120628 if( v && pPk->tnum>0 ){
120629 assert( db->init.busy==0 );
120630 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
120634 pPk->tnum = pTab->tnum;
120636 /* Update the in-memory representation of all UNIQUE indices by converting
120639 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120643 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
120644 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
120650 pIdx->nColumn = pIdx->nKeyCol;
120653 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
120654 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
120655 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
120656 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
120657 pIdx->aiColumn[j] = pPk->aiColumn[i];
120658 pIdx->azColl[j] = pPk->azColl[i];
120659 if( pPk->aSortOrder[i] ){
120661 pIdx->bAscKeyBug = 1;
120666 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
120667 assert( pIdx->nColumn>=j );
120673 for(i=0; i<pTab->nCol; i++){
120674 if( !hasColumn(pPk->aiColumn, nPk, i)
120675 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
120678 for(i=0, j=nPk; i<pTab->nCol; i++){
120679 if( !hasColumn(pPk->aiColumn, j, i)
120680 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
120682 assert( j<pPk->nColumn );
120683 pPk->aiColumn[j] = i;
120684 pPk->azColl[j] = sqlite3StrBINARY;
120688 assert( pPk->nColumn==j );
120689 assert( pTab->nNVCol<=j );
120704 nName = sqlite3Strlen30(pTab->zName);
120705 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
120707 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
120709 if( pMod->pModule->iVersion<3 ) return 0;
120710 if( pMod->pModule->xShadowName==0 ) return 0;
120711 return pMod->pModule->xShadowName(zName+nName+1);
120723 int nName; /* Length of pTab->zName */
120728 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
120730 if( NEVER(pMod->pModule==0) ) return;
120731 if( pMod->pModule->iVersion<3 ) return;
120732 if( pMod->pModule->xShadowName==0 ) return;
120733 assert( pTab->zName!=0 );
120734 nName = sqlite3Strlen30(pTab->zName);
120735 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
120737 assert( pOther->zName!=0 );
120739 if( pOther->tabFlags & TF_Shadow ) continue;
120740 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
120741 && pOther->zName[nName]=='_'
120742 && pMod->pModule->xShadowName(pOther->zName+nName+1)
120744 pOther->tabFlags |= TF_Shadow;
120796 #define markExprListImmutable(X) /* no-op */
120809 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
120828 sqlite3 *db = pParse->db; /* The database connection */
120835 p = pParse->pNewTable;
120838 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
120839 p->tabFlags |= TF_Shadow;
120842 /* If the db->init.busy is 1 it means we are reading the SQL off the
120845 ** for the table from the db->init.newTnum field. (The page number
120849 ** table itself. So mark it read-only.
120851 if( db->init.busy ){
120852 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
120856 p->tnum = db->init.newTnum;
120857 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
120871 p->tabFlags |= TF_Strict;
120872 for(ii=0; ii<p->nCol; ii++){
120873 Column *pCol = &p->aCol[ii];
120874 if( pCol->eCType==COLTYPE_CUSTOM ){
120875 if( pCol->colFlags & COLFLAG_HASTYPE ){
120878 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
120882 p->zName, pCol->zCnName);
120885 }else if( pCol->eCType==COLTYPE_ANY ){
120886 pCol->affinity = SQLITE_AFF_BLOB;
120888 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
120889 && p->iPKey!=ii
120890 && pCol->notNull == OE_None
120892 pCol->notNull = OE_Abort;
120893 p->tabFlags |= TF_HasNotNull;
120898 assert( (p->tabFlags & TF_HasPrimaryKey)==0
120899 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
120900 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
120901 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
120905 if( (p->tabFlags & TF_Autoincrement) ){
120910 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
120911 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
120914 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
120917 iDb = sqlite3SchemaToIndex(db, p->pSchema);
120922 if( p->pCheck ){
120923 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
120924 if( pParse->nErr ){
120927 sqlite3ExprListDelete(db, p->pCheck);
120928 p->pCheck = 0;
120930 markExprListImmutable(p->pCheck);
120935 if( p->tabFlags & TF_HasGenerated ){
120937 testcase( p->tabFlags & TF_HasVirtual );
120938 testcase( p->tabFlags & TF_HasStored );
120939 for(ii=0; ii<p->nCol; ii++){
120940 u32 colFlags = p->aCol[ii].colFlags;
120942 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
120952 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
120960 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
120968 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120978 if( !db->init.busy ){
121006 ** statement to populate the new table. The root-page number for the
121007 ** new table is in register pParse->regRoot.
121013 ** A shared-cache write-lock is not required to write to the new table,
121014 ** as a schema-lock must have already been obtained to create it. Since
121015 ** a schema-lock excludes all other database users, the write-lock would
121020 int regYield; /* Register holding co-routine entry-point */
121021 int addrTop; /* Top of the co-routine */
121028 pParse->rc = SQLITE_ERROR;
121029 pParse->nErr++;
121032 regYield = ++pParse->nMem;
121033 regRec = ++pParse->nMem;
121034 regRowid = ++pParse->nMem;
121035 assert(pParse->nTab==1);
121037 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
121039 pParse->nTab = 2;
121042 if( pParse->nErr ) return;
121045 assert( p->aCol==0 );
121046 p->nCol = p->nNVCol = pSelTab->nCol;
121047 p->aCol = pSelTab->aCol;
121048 pSelTab->nCol = 0;
121049 pSelTab->aCol = 0;
121053 if( pParse->nErr ) return;
121055 sqlite3VdbeJumpHere(v, addrTop - 1);
121071 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
121072 n = (int)(pEnd2->z - pParse->sNameToken.z);
121073 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
121075 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
121087 db->aDb[iDb].zDbSName,
121089 p->zName,
121090 p->zName,
121091 pParse->regRoot,
121093 pParse->regRowid
121102 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
121103 Db *pDb = &db->aDb[iDb];
121105 if( pDb->pSchema->pSeqTab==0 ){
121108 pDb->zDbSName
121116 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
121119 /* Add the table to the in-memory representation of the database.
121121 if( db->init.busy ){
121123 Schema *pSchema = p->pSchema;
121125 assert( HasRowid(p) || p->iPKey<0 );
121126 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
121132 pParse->pNewTable = 0;
121133 db->mDbFlags |= DBFLAG_SchemaChange;
121138 assert( !pParse->nested );
121140 if( strcmp(p->zName, "sqlite_sequence")==0 ){
121142 p->pSchema->pSeqTab = p;
121150 if( pCons->z==0 ){
121153 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
121179 sqlite3 *db = pParse->db;
121181 if( pParse->nVar>0 ){
121186 p = pParse->pNewTable;
121187 if( p==0 || pParse->nErr ) goto create_view_fail;
121192 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
121195 p->tabFlags |= TF_NoVisibleRowid;
121199 iDb = sqlite3SchemaToIndex(db, p->pSchema);
121205 ** allocated rather than point to the input string - which means that
121208 pSelect->selFlags |= SF_View;
121210 p->u.view.pSelect = pSelect;
121213 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
121215 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
121216 p->eTabType = TABTYP_VIEW;
121217 if( db->mallocFailed ) goto create_view_fail;
121222 sEnd = pParse->sLastToken;
121228 n = (int)(sEnd.z - pBegin->z);
121230 z = pBegin->z;
121231 while( sqlite3Isspace(z[n-1]) ){ n--; }
121232 sEnd.z = &z[n-1];
121252 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
121258 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
121270 db->nSchemaLock++;
121272 db->nSchemaLock--;
121282 assert( pTable->nCol<=0 );
121299 if( pTable->nCol<0 ){
121300 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
121303 assert( pTable->nCol>=0 );
121313 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
121315 u8 eParseMode = pParse->eParseMode;
121316 int nTab = pParse->nTab;
121317 int nSelect = pParse->nSelect;
121318 pParse->eParseMode = PARSE_MODE_NORMAL;
121319 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
121320 pTable->nCol = -1;
121323 xAuth = db->xAuth;
121324 db->xAuth = 0;
121326 db->xAuth = xAuth;
121330 pParse->nTab = nTab;
121331 pParse->nSelect = nSelect;
121333 pTable->nCol = 0;
121335 }else if( pTable->pCheck ){
121338 ** arglist which is stored in pTable->pCheck. The pCheck field
121342 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
121343 &pTable->nCol, &pTable->aCol);
121344 if( pParse->nErr==0
121345 && pTable->nCol==pSel->pEList->nExpr
121347 assert( db->mallocFailed==0 );
121354 assert( pTable->aCol==0 );
121355 pTable->nCol = pSelTab->nCol;
121356 pTable->aCol = pSelTab->aCol;
121357 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
121358 pSelTab->nCol = 0;
121359 pSelTab->aCol = 0;
121360 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
121362 pTable->nNVCol = pTable->nCol;
121366 pParse->eParseMode = eParseMode;
121370 pTable->pSchema->schemaFlags |= DB_UnresetViews;
121371 if( db->mallocFailed ){
121379 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
121392 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
121407 ** root-page of a table or index in database iDb has changed from iFrom
121428 pDb = &db->aDb[iDb];
121429 pHash = &pDb->pSchema->tblHash;
121432 if( pTab->tnum==iFrom ){
121433 pTab->tnum = iTo;
121436 pHash = &pDb->pSchema->idxHash;
121439 if( pIdx->tnum==iFrom ){
121440 pIdx->tnum = iTo;
121447 ** Write code to erase the table with root-page iTable from database iDb.
121449 ** if a root-page of another table is moved by the btree-layer whilst
121450 ** erasing iTable (this can happen with an auto-vacuum database).
121460 ** is non-zero, then it is the root page number of a table moved to
121471 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
121479 ** in case a root-page belonging to another table is moved by the btree layer
121480 ** is also added (this can happen with an auto-vacuum database).
121483 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
121485 ** table and index root-pages in order, starting with the numerically
121486 ** largest root-page number. This guarantees that none of the root-pages
121494 ** and root page 5 happened to be the largest root-page number in the
121497 ** a free-list page.
121499 Pgno iTab = pTab->tnum;
121509 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121510 Pgno iIdx = pIdx->tnum;
121511 assert( pIdx->pSchema==pTab->pSchema );
121519 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
121520 assert( iDb>=0 && iDb<pParse->db->nDb );
121538 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
121542 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
121556 sqlite3 *db = pParse->db;
121558 Db *pDb = &db->aDb[iDb];
121576 assert( pTrigger->pSchema==pTab->pSchema ||
121577 pTrigger->pSchema==db->aDb[1].pSchema );
121579 pTrigger = pTrigger->pNext;
121586 ** move as a result of the drop (can happen in auto-vacuum mode).
121588 if( pTab->tabFlags & TF_Autoincrement ){
121591 pDb->zDbSName, pTab->zName
121606 pDb->zDbSName, pTab->zName);
121615 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
121618 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
121624 ** Return TRUE if shadow tables should be read-only in the current
121629 if( (db->flags & SQLITE_Defensive)!=0
121630 && db->pVtabCtx==0
121631 && db->nVdbeExec==0
121644 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
121645 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
121646 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
121649 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
121652 if( pTab->tabFlags & TF_Eponymous ){
121665 sqlite3 *db = pParse->db;
121668 if( db->mallocFailed ){
121671 assert( pParse->nErr==0 );
121672 assert( pName->nSrc==1 );
121674 if( noErr ) db->suppressErr++;
121676 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
121677 if( noErr ) db->suppressErr--;
121681 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
121686 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121687 assert( iDb>=0 && iDb<db->nDb );
121699 const char *zDb = db->aDb[iDb].zDbSName;
121713 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
121722 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
121725 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
121731 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
121740 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
121744 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
121756 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
121777 ** under construction in the pParse->pNewTable field.
121789 sqlite3 *db = pParse->db;
121793 Table *p = pParse->pNewTable;
121802 int iCol = p->nCol-1;
121804 if( pToCol && pToCol->nExpr!=1 ){
121807 p->aCol[iCol].zCnName, pTo);
121811 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
121817 nCol = pFromCol->nExpr;
121819 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
121821 for(i=0; i<pToCol->nExpr; i++){
121822 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
121829 pFKey->pFrom = p;
121831 pFKey->pNextFrom = p->u.tab.pFKey;
121832 z = (char*)&pFKey->aCol[nCol];
121833 pFKey->zTo = z;
121837 memcpy(z, pTo->z, pTo->n);
121838 z[pTo->n] = 0;
121840 z += pTo->n+1;
121841 pFKey->nCol = nCol;
121843 pFKey->aCol[0].iFrom = p->nCol-1;
121847 for(j=0; j<p->nCol; j++){
121848 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
121849 pFKey->aCol[i].iFrom = j;
121853 if( j>=p->nCol ){
121856 pFromCol->a[i].zEName);
121860 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
121866 int n = sqlite3Strlen30(pToCol->a[i].zEName);
121867 pFKey->aCol[i].zCol = z;
121869 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
121871 memcpy(z, pToCol->a[i].zEName, n);
121876 pFKey->isDeferred = 0;
121877 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
121878 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
121880 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
121881 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
121882 pFKey->zTo, (void *)pFKey
121889 assert( pNextTo->pPrevTo==0 );
121890 pFKey->pNextTo = pNextTo;
121891 pNextTo->pPrevTo = pFKey;
121897 p->u.tab.pFKey = pFKey;
121918 if( (pTab = pParse->pNewTable)==0 ) return;
121920 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
121921 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
121922 pFKey->isDeferred = (u8)isDeferred;
121935 ** the root page number of the index is taken from pIndex->tnum.
121938 Table *pTab = pIndex->pTable; /* The table that is indexed */
121939 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
121940 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
121949 sqlite3 *db = pParse->db; /* The database connection */
121950 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
121953 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
121954 db->aDb[iDb].zDbSName ) ){
121959 /* Require a write-lock on the table to perform this operation */
121960 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121967 tnum = pIndex->tnum;
121970 assert( pKey!=0 || pParse->nErr );
121973 iSorter = pParse->nTab++;
121974 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
122000 pIndex->nKeyCol); VdbeCoverage(v);
122015 if( !pIndex->bAscKeyBug ){
122040 ** of 8-byte aligned space after the Index object and return a
122060 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
122061 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
122062 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
122063 p->aSortOrder = (u8*)pExtra;
122064 p->nColumn = nCol;
122065 p->nKeyCol = nCol - 1;
122074 ** pParse and return non-zero. Otherwise, return zero.
122079 for(i=0; i<pList->nExpr; i++){
122080 if( pList->a[i].fg.bNulls ){
122081 u8 sf = pList->a[i].fg.sortFlags;
122096 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
122097 ** as the table to be indexed. pParse->pNewTable is a table that is
122101 ** is a primary key or unique-constraint on the most recent column added
122108 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
122124 sqlite3 *db = pParse->db;
122134 assert( db->pParse==pParse );
122135 if( pParse->nErr ){
122138 assert( db->mallocFailed==0 );
122154 /* Use the two-part index name to determine the database
122161 assert( pName && pName->z );
122168 if( !db->init.busy ){
122170 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
122182 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
122183 assert( db->mallocFailed==0 || pTab==0 );
122185 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
122187 "cannot create a TEMP index on non-TEMP table \"%s\"",
122188 pTab->zName);
122195 pTab = pParse->pNewTable;
122197 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122199 pDb = &db->aDb[iDb];
122202 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
122203 && db->init.busy==0
122206 && sqlite3UserAuthTable(pTab->zName)==0
122209 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
122241 assert( pName->z!=0 );
122242 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
122246 if( !db->init.busy ){
122247 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
122252 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
122256 assert( !db->init.busy );
122266 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
122267 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
122284 const char *zDb = pDb->zDbSName;
122290 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
122302 Column *pCol = &pTab->aCol[pTab->nCol-1];
122303 pCol->colFlags |= COLFLAG_UNIQUE;
122304 sqlite3TokenInit(&prevCol, pCol->zCnName);
122308 assert( pList->nExpr==1 );
122312 if( pParse->nErr ) goto exit_create_index;
122318 for(i=0; i<pList->nExpr; i++){
122319 Expr *pExpr = pList->a[i].pExpr;
122321 if( pExpr->op==TK_COLLATE ){
122323 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
122331 nExtraCol = pPk ? pPk->nKeyCol : 1;
122332 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
122333 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
122335 if( db->mallocFailed ){
122338 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
122339 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
122340 pIndex->zName = zExtra;
122342 memcpy(pIndex->zName, zName, nName+1);
122343 pIndex->pTable = pTab;
122344 pIndex->onError = (u8)onError;
122345 pIndex->uniqNotNull = onError!=OE_None;
122346 pIndex->idxType = idxType;
122347 pIndex->pSchema = db->aDb[iDb].pSchema;
122348 pIndex->nKeyCol = pList->nExpr;
122351 pIndex->pPartIdxWhere = pPIWhere;
122358 if( pDb->pSchema->file_format>=4 ){
122359 sortOrderMask = -1; /* Honor DESC */
122367 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
122373 pListItem = pList->a;
122375 pIndex->aColExpr = pList;
122378 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
122379 Expr *pCExpr; /* The i-th index expression */
122380 int requestedSortOrder; /* ASC or DESC on the i-th expression */
122383 sqlite3StringToId(pListItem->pExpr);
122384 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
122385 if( pParse->nErr ) goto exit_create_index;
122386 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
122387 if( pCExpr->op!=TK_COLUMN ){
122388 if( pTab==pParse->pNewTable ){
122393 if( pIndex->aColExpr==0 ){
122394 pIndex->aColExpr = pList;
122398 pIndex->aiColumn[i] = XN_EXPR;
122399 pIndex->uniqNotNull = 0;
122400 pIndex->bHasExpr = 1;
122402 j = pCExpr->iColumn;
122405 j = pTab->iPKey;
122407 if( pTab->aCol[j].notNull==0 ){
122408 pIndex->uniqNotNull = 0;
122410 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
122411 pIndex->bHasVCol = 1;
122412 pIndex->bHasExpr = 1;
122415 pIndex->aiColumn[i] = (i16)j;
122418 if( pListItem->pExpr->op==TK_COLLATE ){
122420 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
122421 zColl = pListItem->pExpr->u.zToken;
122427 nExtra -= nColl;
122429 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
122432 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
122435 pIndex->azColl[i] = zColl;
122436 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
122437 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
122445 for(j=0; j<pPk->nKeyCol; j++){
122446 int x = pPk->aiColumn[j];
122448 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
122449 pIndex->nColumn--;
122451 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
122452 pIndex->aiColumn[i] = x;
122453 pIndex->azColl[i] = pPk->azColl[j];
122454 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
122458 assert( i==pIndex->nColumn );
122460 pIndex->aiColumn[i] = XN_ROWID;
122461 pIndex->azColl[i] = sqlite3StrBINARY;
122464 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
122469 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
122471 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
122472 pIndex->isCovering = 1;
122473 for(j=0; j<pTab->nCol; j++){
122474 if( j==pTab->iPKey ) continue;
122476 pIndex->isCovering = 0;
122481 if( pTab==pParse->pNewTable ){
122504 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122507 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
122510 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
122511 for(k=0; k<pIdx->nKeyCol; k++){
122514 assert( pIdx->aiColumn[k]>=0 );
122515 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
122516 z1 = pIdx->azColl[k];
122517 z2 = pIndex->azColl[k];
122520 if( k==pIdx->nKeyCol ){
122521 if( pIdx->onError!=pIndex->onError ){
122529 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
122533 if( pIdx->onError==OE_Default ){
122534 pIdx->onError = pIndex->onError;
122537 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
122539 pIndex->pNext = pParse->pNewIndex;
122540 pParse->pNewIndex = pIndex;
122551 ** in-memory database structures.
122553 assert( pParse->nErr==0 );
122554 if( db->init.busy ){
122557 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
122559 pIndex->tnum = db->init.newTnum;
122562 pParse->rc = SQLITE_CORRUPT_BKPT;
122566 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
122567 pIndex->zName, pIndex);
122573 db->mDbFlags |= DBFLAG_SchemaChange;
122592 int iMem = ++pParse->nMem;
122605 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
122613 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
122614 if( pName->z[n-1]==';' ) n--;
122617 onError==OE_None ? "" : " UNIQUE", n, pName->z);
122628 db->aDb[iDb].zDbSName,
122629 pIndex->zName,
122630 pTab->zName,
122637 ** to invalidate all pre-compiled statements.
122643 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
122647 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
122650 if( db->init.busy || pTblName==0 ){
122651 pIndex->pNext = pTab->pIndex;
122652 pTab->pIndex = pIndex;
122656 assert( pParse->pNewIndex==0 );
122657 pParse->pNewIndex = pIndex;
122671 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
122673 if( pThis->onError!=OE_Replace ) continue;
122674 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
122676 pThis->pNext = pNext->pNext;
122677 pNext->pNext = pThis;
122678 ppFrom = &pNext->pNext;
122686 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
122687 assert( pThis->onError!=OE_Replace
122688 || pThis->pNext==0
122689 || pThis->pNext->onError==OE_Replace );
122700 ** Fill the Index.aiRowEst[] array with default information - information
122710 ** aiRowEst[N]<=aiRowEst[N-1]
122720 LogEst *a = pIdx->aiRowLogEst;
122722 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
122726 assert( !pIdx->hasStat1 );
122732 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
122738 x = pIdx->pTable->nRowLogEst;
122741 pIdx->pTable->nRowLogEst = x = 99;
122743 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
122749 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
122754 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
122764 sqlite3 *db = pParse->db;
122767 if( db->mallocFailed ){
122770 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
122771 assert( pName->nSrc==1 );
122775 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
122778 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
122780 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
122783 pParse->checkSchema = 1;
122786 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
122791 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
122795 Table *pTab = pIndex->pTable;
122796 const char *zDb = db->aDb[iDb].zDbSName;
122802 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
122814 db->aDb[iDb].zDbSName, pIndex->zName
122816 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
122818 destroyRootPage(pParse, pIndex->tnum, iDb);
122819 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
122832 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
122840 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
122852 if( (n & (n-1))==0 ){
122856 *pIdx = -1;
122874 sqlite3 *db = pParse->db;
122882 sizeof(IdList) + pList->nId*sizeof(pList->a));
122889 i = pList->nId++;
122890 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
122891 if( IN_RENAME_OBJECT && pList->a[i].zName ){
122892 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
122904 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
122905 for(i=0; i<pList->nId; i++){
122906 sqlite3DbFree(db, pList->a[i].zName);
122912 ** Return the index in pList of the identifier named zId. Return -1
122918 for(i=0; i<pList->nId; i++){
122919 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
122921 return -1;
122930 ** appropriate for small and memory-limited applications.
122959 int nExtra, /* Number of new slots to add to pSrc->a[] */
122960 int iStart /* Index in pSrc->a[] of first new slot */
122968 assert( iStart<=pSrc->nSrc );
122971 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
122973 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
122974 sqlite3 *db = pParse->db;
122976 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
122983 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
122985 assert( db->mallocFailed );
122989 pSrc->nAlloc = nAlloc;
122994 for(i=pSrc->nSrc-1; i>=iStart; i--){
122995 pSrc->a[i+nExtra] = pSrc->a[i];
122997 pSrc->nSrc += nExtra;
123000 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
123002 pSrc->a[i].iCursor = -1;
123055 assert( pParse->db!=0 );
123056 db = pParse->db;
123058 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
123060 pList->nAlloc = 1;
123061 pList->nSrc = 1;
123062 memset(&pList->a[0], 0, sizeof(pList->a[0]));
123063 pList->a[0].iCursor = -1;
123065 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
123073 pItem = &pList->a[pList->nSrc-1];
123074 if( pDatabase && pDatabase->z==0 ){
123078 pItem->zName = sqlite3NameFromToken(db, pDatabase);
123079 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
123081 pItem->zName = sqlite3NameFromToken(db, pTable);
123082 pItem->zDatabase = 0;
123093 assert( pList || pParse->db->mallocFailed );
123095 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
123096 if( pItem->iCursor>=0 ) continue;
123097 pItem->iCursor = pParse->nTab++;
123098 if( pItem->pSelect ){
123099 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
123113 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
123114 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
123115 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
123116 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
123117 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
123118 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
123119 sqlite3DeleteTable(db, pItem->pTab);
123120 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
123121 if( pItem->fg.isUsing ){
123122 sqlite3IdListDelete(db, pItem->u3.pUsing);
123123 }else if( pItem->u3.pOn ){
123124 sqlite3ExprDelete(db, pItem->u3.pOn);
123136 ** pDatabase is NULL if the database name qualifier is missing - the
123151 Token *pAlias, /* The right-hand side of the AS subexpression */
123156 sqlite3 *db = pParse->db;
123157 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
123159 (pOnUsing->pOn ? "ON" : "USING")
123167 assert( p->nSrc>0 );
123168 pItem = &p->a[p->nSrc-1];
123170 assert( pItem->zName==0 || pDatabase!=0 );
123171 if( IN_RENAME_OBJECT && pItem->zName ){
123172 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
123173 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
123176 if( pAlias->n ){
123177 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
123180 pItem->pSelect = pSubquery;
123181 if( pSubquery->selFlags & SF_NestedFrom ){
123182 pItem->fg.isNestedFrom = 1;
123185 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
123186 assert( pItem->fg.isUsing==0 );
123188 pItem->u3.pOn = 0;
123189 }else if( pOnUsing->pUsing ){
123190 pItem->fg.isUsing = 1;
123191 pItem->u3.pUsing = pOnUsing->pUsing;
123193 pItem->u3.pOn = pOnUsing->pOn;
123206 ** element of the source-list passed as the second argument.
123210 if( p && pIndexedBy->n>0 ){
123212 assert( p->nSrc>0 );
123213 pItem = &p->a[p->nSrc-1];
123214 assert( pItem->fg.notIndexed==0 );
123215 assert( pItem->fg.isIndexedBy==0 );
123216 assert( pItem->fg.isTabFunc==0 );
123217 if( pIndexedBy->n==1 && !pIndexedBy->z ){
123220 pItem->fg.notIndexed = 1;
123222 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
123223 pItem->fg.isIndexedBy = 1;
123224 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
123235 assert( p1 && p1->nSrc==1 );
123237 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
123239 sqlite3SrcListDelete(pParse->db, p2);
123242 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
123243 sqlite3DbFree(pParse->db, p2);
123244 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
123252 ** table-valued-function.
123256 SrcItem *pItem = &p->a[p->nSrc-1];
123257 assert( pItem->fg.notIndexed==0 );
123258 assert( pItem->fg.isIndexedBy==0 );
123259 assert( pItem->fg.isTabFunc==0 );
123260 pItem->u1.pFuncArg = pList;
123261 pItem->fg.isTabFunc = 1;
123263 sqlite3ExprListDelete(pParse->db, pList);
123279 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
123284 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
123291 if( p && p->nSrc>1 ){
123292 int i = p->nSrc-1;
123295 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
123296 }while( (--i)>0 );
123297 p->a[0].fg.jointype = 0;
123302 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
123303 i--;
123306 p->a[i].fg.jointype |= JT_LTORJ;
123307 }while( (--i)>=0 );
123321 db = pParse->db;
123329 for(i=0; i<db->nDb; i++){
123331 Btree *pBt = db->aDb[i].pBt;
123356 assert( pParse->db!=0 );
123374 char *zName = sqlite3NameFromToken(pParse->db, pName);
123382 sqlite3DbFree(pParse->db, zName);
123394 sqlite3 *db = pParse->db;
123395 if( db->aDb[1].pBt==0 && !pParse->explain ){
123405 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
123409 pParse->rc = rc;
123412 db->aDb[1].pBt = pBt;
123413 assert( db->aDb[1].pSchema );
123414 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
123425 ** will occur at the end of the top-level VDBE and will be generated
123429 assert( iDb>=0 && iDb<pToplevel->db->nDb );
123430 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
123432 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
123433 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
123434 DbMaskSet(pToplevel->cookieMask, iDb);
123450 sqlite3 *db = pParse->db;
123452 for(i=0; i<db->nDb; i++){
123453 Db *pDb = &db->aDb[i];
123454 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
123476 DbMaskSet(pToplevel->writeMask, iDb);
123477 pToplevel->isMultiWrite |= setStatement;
123489 pToplevel->isMultiWrite = 1;
123510 pToplevel->mayAbort = 1;
123527 assert( pParse->pVdbe!=0 );
123529 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
123548 Table *pTab = pIdx->pTable;
123550 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
123551 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
123552 if( pIdx->aColExpr ){
123553 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
123555 for(j=0; j<pIdx->nKeyCol; j++){
123557 assert( pIdx->aiColumn[j]>=0 );
123558 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
123560 sqlite3_str_appendall(&errMsg, pTab->zName);
123574 ** Code an OP_Halt due to non-unique rowid.
123579 Table *pTab /* The table with the non-unique rowid */
123583 if( pTab->iPKey>=0 ){
123584 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
123585 pTab->aCol[pTab->iPKey].zCnName);
123588 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
123603 for(i=0; i<pIndex->nColumn; i++){
123604 const char *z = pIndex->azColl[i];
123605 assert( z!=0 || pIndex->aiColumn[i]<0 );
123606 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
123623 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
123625 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
123627 sqlite3RefillIndex(pParse, pIndex, -1);
123643 sqlite3 *db = pParse->db; /* The database connection */
123648 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
123650 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
123661 ** REINDEX -- 1
123662 ** REINDEX <collation> -- 2
123663 ** REINDEX ?<database>.?<tablename> -- 3
123664 ** REINDEX ?<database>.?<indexname> -- 4
123679 sqlite3 *db = pParse->db; /* The database connection */
123691 }else if( NEVER(pName2==0) || pName2->z==0 ){
123693 assert( pName1->z );
123694 zColl = sqlite3NameFromToken(pParse->db, pName1);
123708 zDb = db->aDb[iDb].zDbSName;
123719 sqlite3RefillIndex(pParse, pIndex, -1);
123734 int nCol = pIdx->nColumn;
123735 int nKey = pIdx->nKeyCol;
123737 if( pParse->nErr ) return 0;
123738 if( pIdx->uniqNotNull ){
123739 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
123741 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
123746 const char *zColl = pIdx->azColl[i];
123747 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
123749 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
123750 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
123752 if( pParse->nErr ){
123753 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
123754 if( pIdx->bNoQuery==0 ){
123759 ** the missing index using the collation-needed callback. For
123762 pIdx->bNoQuery = 1;
123763 pParse->rc = SQLITE_ERROR_RETRY;
123778 Token *pName, /* Name of the common-table */
123784 sqlite3 *db = pParse->db;
123787 assert( pNew!=0 || db->mallocFailed );
123789 if( db->mallocFailed ){
123793 pNew->pSelect = pQuery;
123794 pNew->pCols = pArglist;
123795 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
123796 pNew->eM10d = eM10d;
123807 sqlite3ExprListDelete(db, pCte->pCols);
123808 sqlite3SelectDelete(db, pCte->pSelect);
123809 sqlite3DbFree(db, pCte->zName);
123832 sqlite3 *db = pParse->db;
123842 zName = pCte->zName;
123845 for(i=0; i<pWith->nCte; i++){
123846 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
123853 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
123858 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
123860 if( db->mallocFailed ){
123864 pNew->a[pNew->nCte++] = *pCte;
123877 for(i=0; i<pWith->nCte; i++){
123878 cteClear(db, &pWith->a[i]);
123910 assert( !db->xCollNeeded || !db->xCollNeeded16 );
123911 if( db->xCollNeeded ){
123914 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
123918 if( db->xCollNeeded16 ){
123921 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
123924 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
123935 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
123940 char *z = pColl->zName;
123945 if( pColl2->xCmp!=0 ){
123947 pColl->xDel = 0; /* Do not copy the destructor */
123966 if( pColl && pColl->xCmp==0 ){
123967 const char *zName = pColl->zName;
123968 sqlite3 *db = pParse->db;
123987 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
123999 pColl = sqlite3HashFind(&db->aCollSeq, zName);
124013 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
124031 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
124056 if( pColl ) pColl += enc-1;
124058 pColl = db->pDfltColl;
124069 db->enc = enc;
124070 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
124073 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
124098 sqlite3 *db = pParse->db;
124104 if( !p || !p->xCmp ){
124111 if( p && !p->xCmp && synthCollSeq(db, p) ){
124114 assert( !p || p->xCmp );
124117 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
124143 sqlite3 *db = pParse->db;
124145 u8 initbusy = db->init.busy;
124149 if( !initbusy && (!pColl || !pColl->xCmp) ){
124162 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
124163 ** is also -1. In other words, we are searching for a function that
124166 ** If nArg is -2 that means that we are searching for any function
124176 ** 4: UTF8/16 conversion required - argument count matches exactly
124177 ** 5: UTF16 byte order conversion required - argument count matches exactly
124180 ** If nArg==(-2) then any function with a non-null xSFunc is
124182 ** a non-match.
124187 int nArg, /* Desired number of arguments. (-1)==any */
124191 assert( p->nArg>=-1 );
124194 if( p->nArg!=nArg ){
124195 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
124196 if( p->nArg>=0 ) return 0;
124201 if( p->nArg==nArg ){
124208 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
124210 }else if( (enc & p->funcFlags & 2)!=0 ){
124226 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
124227 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
124228 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
124251 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
124252 aDef[i].pNext = pOther->pNext;
124253 pOther->pNext = &aDef[i];
124266 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
124274 ** If nArg is -2, then the first valid function found is returned. A
124275 ** function is valid if xSFunc is non-zero. The nArg==(-2)
124277 ** of arguments. If nArg is -2, then createFlag must be 0.
124285 const char *zName, /* Name of the function. zero-terminated */
124286 int nArg, /* Number of arguments. -1 means any number */
124296 assert( nArg>=(-2) );
124297 assert( nArg>=(-1) || createFlag==0 );
124300 /* First search for a match amongst the application-defined functions.
124302 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
124309 p = p->pNext;
124312 /* If no match is found, search the built-in functions.
124314 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
124315 ** functions even if a prior app-defined function was found. And give
124316 ** priority to built-in functions.
124321 ** new function. But the FuncDefs for built-in functions are read-only.
124322 ** So we must not search for built-ins when creating a new function.
124324 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
124334 p = p->pNext;
124346 pBest->zName = (const char*)&pBest[1];
124347 pBest->nArg = (u16)nArg;
124348 pBest->funcFlags = enc;
124350 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
124351 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
124357 pBest->pNext = pOther;
124361 if( pBest && (pBest->xSFunc || createFlag) ){
124383 temp1 = pSchema->tblHash;
124384 temp2 = pSchema->trigHash;
124385 sqlite3HashInit(&pSchema->trigHash);
124386 sqlite3HashClear(&pSchema->idxHash);
124391 sqlite3HashInit(&pSchema->tblHash);
124397 sqlite3HashClear(&pSchema->fkeyHash);
124398 pSchema->pSeqTab = 0;
124399 if( pSchema->schemaFlags & DB_SchemaLoaded ){
124400 pSchema->iGeneration++;
124402 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
124418 }else if ( 0==p->file_format ){
124419 sqlite3HashInit(&p->tblHash);
124420 sqlite3HashInit(&p->idxHash);
124421 sqlite3HashInit(&p->trigHash);
124422 sqlite3HashInit(&p->fkeyHash);
124423 p->enc = SQLITE_UTF8;
124456 ** pSrc->a[0].pTab Pointer to the Table object
124457 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
124461 SrcItem *pItem = pSrc->a;
124463 assert( pItem && pSrc->nSrc>=1 );
124465 sqlite3DeleteTable(pParse->db, pItem->pTab);
124466 pItem->pTab = pTab;
124468 pTab->nTabRef++;
124469 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
124476 /* Generate byte-code that will report the number of rows modified
124486 /* Return true if table pTab is read-only.
124488 ** A table is read-only if any of the following are true:
124503 ** is for a top-level SQL statement.
124506 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
124513 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
124516 if( pParse->pToplevel!=0
124517 && pTab->u.vtab.p->eVtabRisk >
124518 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
124521 pTab->zName);
124530 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
124531 db = pParse->db;
124532 if( (pTab->tabFlags & TF_Readonly)!=0 ){
124533 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
124535 assert( pTab->tabFlags & TF_Shadow );
124542 ** If pTab is not writable -> generate an error message and return 1.
124543 ** If pTab is writable but other errors have occurred -> return 1.
124544 ** If pTab is writable and no prior errors -> return 0;
124548 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
124553 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
124555 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
124580 sqlite3 *db = pParse->db;
124581 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
124585 assert( pFrom->nSrc==1 );
124586 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
124587 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
124588 assert( pFrom->a[0].fg.isUsing==0 );
124589 assert( pFrom->a[0].u3.pOn==0 );
124610 SrcList *pSrc, /* the FROM clause -- which tables to scan */
124616 sqlite3 *db = pParse->db;
124628 sqlite3ExprDelete(pParse->db, pWhere);
124629 sqlite3ExprListDelete(pParse->db, pOrderBy);
124649 pTab = pSrc->a[0].pTab;
124657 if( pPk->nKeyCol==1 ){
124658 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
124663 for(i=0; i<pPk->nKeyCol; i++){
124664 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
124669 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
124676 pSrc->a[0].pTab = 0;
124678 pSrc->a[0].pTab = pTab;
124679 if( pSrc->a[0].fg.isIndexedBy ){
124680 assert( pSrc->a[0].fg.isCte==0 );
124681 pSrc->a[0].u2.pIBIndex = 0;
124682 pSrc->a[0].fg.isIndexedBy = 0;
124683 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
124684 }else if( pSrc->a[0].fg.isCte ){
124685 pSrc->a[0].u2.pCteUse->nUse++;
124752 db = pParse->db;
124753 assert( db->pParse==pParse );
124754 if( pParse->nErr ){
124757 assert( db->mallocFailed==0 );
124758 assert( pTabList->nSrc==1 );
124787 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
124811 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124812 assert( iDb<db->nDb );
124813 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
124814 db->aDb[iDb].zDbSName);
124823 assert( pTabList->nSrc==1 );
124824 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
124825 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
124826 pParse->nTab++;
124832 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
124841 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
124870 if( (db->flags & SQLITE_CountRows)!=0
124871 && !pParse->nested
124872 && !pParse->pTriggerTab
124873 && !pParse->bReturning
124875 memCnt = ++pParse->nMem;
124886 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
124896 && db->xPreUpdateCallback==0
124900 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
124902 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
124903 pTab->zName, P4_STATIC);
124905 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124906 assert( pIdx->pSchema==pTab->pSchema );
124908 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
124910 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
124923 iRowSet = ++pParse->nMem;
124930 nPk = pPk->nKeyCol;
124931 iPk = pParse->nMem+1;
124932 pParse->nMem += nPk;
124933 iEphCur = pParse->nTab++;
124942 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
124943 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
124944 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
124964 assert( pPk->aiColumn[i]>=0 );
124966 pPk->aiColumn[i], iPk+i);
124970 iKey = ++pParse->nMem;
124971 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
124975 /* For ONEPASS, no need to store the rowid/primary-key. There is only
124986 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
124987 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
124993 iKey = ++pParse->nMem;
124996 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
125026 /* Set up a loop over the rowids/primary-keys that were found in the
125027 ** where-clause loop above.
125031 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
125060 pParse->isMultiWrite = 0;
125068 int count = (pParse->nested==0); /* True to count changes */
125073 /* End of the loop over all rowids/primary-keys. */
125084 } /* End non-truncate path */
125090 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
125136 ** cursor number iIdxCur+i for the i-th index.
125172 u8 count, /* If non-zero, increment the row change counter */
125177 Vdbe *v = pParse->pVdbe; /* Vdbe */
125211 iOld = pParse->nMem+1;
125212 pParse->nMem += (1 + pTab->nCol);
125214 /* Populate the OLD.* pseudo-table register array. These values will be
125217 for(iCol=0; iCol<pTab->nCol; iCol++){
125245 iIdxNoSeek = -1;
125258 ** If variable 'count' is non-zero, then this OP_Delete instruction should
125259 ** invoke the update-hook. The pre-update-hook, on the other hand should
125261 ** the update-hook is not invoked for rows removed by REPLACE, but the
125262 ** pre-update-hook is.
125268 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
125310 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
125311 ** index is the 0-th index.)
125325 int r1 = -1; /* Register holding an index key */
125332 v = pParse->pVdbe;
125334 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
125339 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
125343 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
125391 Vdbe *v = pParse->pVdbe;
125397 if( pIdx->pPartIdxWhere ){
125399 pParse->iSelfTab = iDataCur + 1;
125400 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
125402 pParse->iSelfTab = 0;
125403 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
125409 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
125411 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
125414 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
125415 && pPrior->aiColumn[j]!=XN_EXPR
125421 if( pIdx->aiColumn[j]>=0 ){
125439 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
125445 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
125462 ** This file contains the C-language implementations for many of the SQL
125479 assert( context->pVdbe!=0 );
125480 pOp = &context->pVdbe->aOp[context->iOp-1];
125481 assert( pOp->opcode==OP_CollSeq );
125482 assert( pOp->p4type==P4_COLLSEQ );
125483 return pOp->p4.pColl;
125491 assert( context->isError<=0 );
125492 context->isError = -1;
125493 context->skipFlag = 1;
125497 ** Implementation of the non-aggregate min() and max() functions
125510 mask = sqlite3_user_data(context)==0 ? 0 : -1;
125513 assert( mask==-1 || mask==0 );
125535 int i = sqlite3_value_type(argv[0]) - 1;
125543 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
125547 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
125592 sqlite3_result_int(context, (int)(z-z0));
125605 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
125616 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
125618 ** equivalent positive 64-bit two complement value. */
125619 sqlite3_result_error(context, "integer overflow", -1);
125622 iVal = -iVal;
125628 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
125635 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
125639 if( rVal<0 ) rVal = -rVal;
125706 nHaystack--;
125737 x.nArg = argc-1;
125740 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
125753 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
125754 ** of x. If x is text, then we actually count UTF-8 characters.
125798 ** as substr(X,1,N) - it returns the first N characters of X. This
125799 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
125800 ** from 2009-02-02 for compatibility of applications that exploited the
125807 p2 = -p2;
125811 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
125821 p1--;
125823 p2--;
125826 p1 -= p2;
125836 p1--;
125838 for(z2=z; *z2 && p2; p2--){
125841 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
125845 p2 = len-p1;
125869 /* If Y==0 and X will fit in a 64-bit int,
125873 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
125876 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
125901 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
125902 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
125903 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
125965 #define noopFunc versionFunc /* Substitute function - never called */
125980 ** (or -9223372036854775808) since when you do abs() of that
125985 ** therefore be no less than -9223372036854775807.
125987 r = -(r & LARGEST_INT64);
126027 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
126036 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
126061 /* IMP: R-11217-42568 This function is a wrapper around the
126067 ** A structure defining how to do GLOB-style comparisons.
126090 /* The correct SQL-92 behavior is for the LIKE operator to ignore
126105 ** Compare two UTF-8 strings for equality where the first string is
126125 ** range of characters can be specified using '-'. Example:
126126 ** "[a-z]" matches any single lower-case letter. To match a '-', make
126149 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
126150 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
126151 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
126168 if( pInfo->matchSet==0 ){
126174 assert( matchOther<0x80 ); /* '[' is a single-byte character */
126176 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
126189 ** For a case-insensitive search, set variable cx to be the same as
126222 if( pInfo->matchSet==0 ){
126242 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
126273 ** non-zero if there is no match.
126286 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
126287 ** a miss - like strcmp().
126311 ** the build-in LIKE operator. The first argument to the function is the
126349 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
126350 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
126351 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
126352 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
126356 /* The escape character string must consist of a single UTF-8 character.
126361 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
126363 "ESCAPE expression must be a single character", -1);
126367 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
126370 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
126371 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
126374 escape = pInfo->matchSet;
126414 /* IMP: R-48699-48617 This function is an SQL wrapper around the
126415 ** sqlite3_libversion() C-interface. */
126416 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
126430 /* IMP: R-24470-31136 This function is an SQL wrapper around the
126432 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
126438 ** its side-effects.
126464 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
126488 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
126492 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
126496 /* Array for converting from half-bytes (nybbles) into ASCII hex
126511 assert( pStr!=0 && pStr->nChar==0 );
126521 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
126538 if( pStr->accError==0 ){
126539 char *zText = pStr->zText;
126549 pStr->nChar = nBlob*2 + 3;
126571 ** Strings are surrounded by single-quotes with escapes on interior quotes
126581 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
126592 ** The unicode() function. Return the integer unicode code-point value
126644 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
126677 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
126757 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
126767 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
126780 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
126819 || sqlite3_context_db_handle(context)->mallocFailed );
126839 loopLimit = nStr - nPattern;
126846 nOut += nRep - nPattern;
126847 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
126848 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
126849 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
126855 if( (cntExpand&(cntExpand-1))==0 ){
126860 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
126870 i += nPattern-1;
126873 assert( j+nStr-i+1<=nOut );
126874 memcpy(&zOut[j], &zStr[i], nStr-i);
126875 j += nStr - i;
126930 aLen[nChar] = (unsigned)(z - azChar[nChar]);
126945 nIn -= len;
126953 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
126956 nIn -= len;
126971 ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
126972 ** When the "sqlite3" command-line shell is built using this functionality,
126974 ** involving application-defined functions to be examined in a generic
126982 /* no-op */
126990 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
126991 ** is only available if the SQLITE_SOUNDEX compile-time option is used
126998 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
127043 /* IMP: R-64894-50321 The string "?000" is returned if the argument
127052 ** A function that loads a shared-library extension then returns NULL.
127063 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
127064 sqlite3_result_error(context, "not authorized", -1);
127074 sqlite3_result_error(context, zErrMsg, -1);
127091 u8 approx; /* True if non-integer value was input to the sum */
127112 p->cnt++;
127115 p->rSum += v;
127116 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
127117 p->approx = p->overflow = 1;
127120 p->rSum += sqlite3_value_double(argv[0]);
127121 p->approx = 1;
127133 /* p is always non-NULL because sumStep() will have been called first
127136 assert( p->cnt>0 );
127137 p->cnt--;
127138 assert( type==SQLITE_INTEGER || p->approx );
127139 if( type==SQLITE_INTEGER && p->approx==0 ){
127141 p->rSum -= v;
127142 p->iSum -= v;
127144 p->rSum -= sqlite3_value_double(argv[0]);
127154 if( p && p->cnt>0 ){
127155 if( p->overflow ){
127156 sqlite3_result_error(context,"integer overflow",-1);
127157 }else if( p->approx ){
127158 sqlite3_result_double(context, p->rSum);
127160 sqlite3_result_int64(context, p->iSum);
127167 if( p && p->cnt>0 ){
127168 sqlite3_result_double(context, p->rSum/(double)p->cnt);
127175 sqlite3_result_double(context, p ? p->rSum : (double)0);
127197 p->n++;
127204 ** expressed as a 32-bit integer. */
127205 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
127206 || p->n==sqlite3_aggregate_count(context) );
127212 sqlite3_result_int64(context, p ? p->n : 0);
127218 /* p is always non-NULL since countStep() will have been called first */
127220 p->n--;
127222 p->bInverse = 1;
127246 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
127247 }else if( pBest->flags ){
127254 ** sqlite3_user_data() function returns (void *)-1. For min() it
127267 pBest->db = sqlite3_context_db_handle(context);
127275 if( pRes->flags ){
127306 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
127308 ** (Hence, its slots in use number nAccum-1 between method calls.)
127329 int firstTerm = pGCC->str.mxAlloc==0;
127330 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
127333 sqlite3_str_appendchar(&pGCC->str, 1, ',');
127337 pGCC->nFirstSepLength = 1;
127344 sqlite3_str_append(&pGCC->str, zSep, nSep);
127350 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
127351 int *pnsl = pGCC->pnSepLengths;
127354 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
127356 int i = 0, nA = pGCC->nAccum-1;
127357 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
127360 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
127363 if( ALWAYS(pGCC->nAccum>0) ){
127364 pnsl[pGCC->nAccum-1] = nSep;
127366 pGCC->pnSepLengths = pnsl;
127368 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
127375 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
127377 pGCC->nAccum += 1;
127381 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
127396 /* pGCC is always non-NULL since groupConcatStep() will have always
127404 pGCC->nAccum -= 1;
127405 if( pGCC->pnSepLengths!=0 ){
127406 assert(pGCC->nAccum >= 0);
127407 if( pGCC->nAccum>0 ){
127408 nVS += *pGCC->pnSepLengths;
127409 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
127410 (pGCC->nAccum-1)*sizeof(int));
127413 /* If removing single accumulated string, harmlessly over-do. */
127414 nVS += pGCC->nFirstSepLength;
127416 if( nVS>=(int)pGCC->str.nChar ){
127417 pGCC->str.nChar = 0;
127419 pGCC->str.nChar -= nVS;
127420 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
127422 if( pGCC->str.nChar==0 ){
127423 pGCC->str.mxAlloc = 0;
127424 sqlite3_free(pGCC->pnSepLengths);
127425 pGCC->pnSepLengths = 0;
127436 sqlite3ResultStrAccum(context, &pGCC->str);
127438 sqlite3_free(pGCC->pnSepLengths);
127447 StrAccum *pAccum = &pGCC->str;
127448 if( pAccum->accError==SQLITE_TOOBIG ){
127450 }else if( pAccum->accError==SQLITE_NOMEM ){
127454 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
127463 ** This routine does per-connection function registration. Most
127464 ** of the built-in functions above are part of the global function set.
127476 ** Re-register the built-in LIKE functions. The caseSensitive
127492 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
127493 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
127501 ** LIKE-style function then return FALSE.
127517 assert( pExpr->op==TK_FUNCTION );
127519 if( !pExpr->x.pList ){
127522 nExpr = pExpr->x.pList->nExpr;
127524 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
127528 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
127536 memcpy(aWc, pDef->pUserData, 3);
127544 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
127546 if( pEscape->op!=TK_STRING ) return 0;
127548 zEscape = pEscape->u.zToken;
127555 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
127571 /* Extra math functions that require linking with -lm
127629 ** ln(X) - natural logarithm
127630 ** log(X) - log X base 10
127631 ** log10(X) - log X base 10
127632 ** log(B,X) - log X base B
127686 ** Implementation of 1-argument SQL math functions:
127688 ** exp(X) - Compute e to the X-th power
127708 ** Implementation of 2-argument SQL math functions:
127710 ** power(X,Y) - Compute X to the Y-th power
127733 ** Implementation of 0-argument pi() function.
127762 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
127767 ** to the global function hash table. This occurs at start-time (as
127778 ** FuncDef.pHash elements at start-time. The elements of this array
127779 ** are read-only after initialization is complete.
127818 FUNCTION(min, -1, 0, 1, minmaxFunc ),
127822 FUNCTION(max, -1, 1, 1, minmaxFunc ),
127830 FUNCTION(printf, -1, 0, 0, printfFunc ),
127831 FUNCTION(format, -1, 0, 0, printfFunc ),
127833 FUNCTION(char, -1, 0, 0, charFunc ),
127883 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
127924 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
127935 #if 0 /* Enable to print out how the built-in functions are hashed */
127940 printf("FUNC-HASH %02d:", i);
127941 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
127942 int n = sqlite3Strlen30(p->zName);
127943 int h = p->zName[0] + n;
127944 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
127945 printf(" %s(%d)", p->zName, h);
127975 ** --------------------------
128057 ** ---------------
128059 ** Before coding an UPDATE or DELETE row operation, the code-generator
128064 ** accessed). No information is required by the code-generator before
128068 ** sqlite3FkRequired() - Test to see if FK processing is required.
128069 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
128073 ** --------------------------------------
128075 ** sqlite3FkCheck() - Check for foreign key violations.
128076 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
128077 ** sqlite3FkDelete() - Delete an FKey structure.
128082 ** -----------------------
128112 ** constraint to the parent table column stored in the left-most column
128114 ** child table column that corresponds to the second left-most column of
128133 ** then non-zero is returned, and a "foreign key mismatch" error loaded
128134 ** into pParse. If an OOM error occurs, non-zero is returned and the
128135 ** pParse->db->mallocFailed flag is set.
128146 int nCol = pFKey->nCol; /* Number of columns in parent key */
128147 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
128154 /* If this is a non-composite (single column) foreign key, check if it
128160 ** Non-composite foreign keys do not require the aiCol array.
128170 if( pParent->iPKey>=0 ){
128172 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
128178 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
128183 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
128184 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
128196 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
128201 /* If zKey is non-NULL, then this foreign key was declared to
128207 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
128216 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
128218 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
128220 zIdxCol = pParent->aCol[iCol].zCnName;
128222 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
128223 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
128235 if( !pParse->disableTriggers ){
128237 "foreign key mismatch - \"%w\" referencing \"%w\"",
128238 pFKey->pFrom->zName, pFKey->zTo);
128240 sqlite3DbFree(pParse->db, aiCol);
128252 ** affected - once to "delete" the old row, and then again to "insert" the
128262 ** --------------------------------------------------------------------------
128287 int iCur = pParse->nTab - 1; /* Cursor number to use */
128291 (!pFKey->isDeferred
128292 && !(pParse->db->flags & SQLITE_DeferFKs)
128293 && !pParse->pToplevel
128294 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
128304 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
128307 for(i=0; i<pFKey->nCol; i++){
128308 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
128325 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
128330 ** to increment the constraint-counter (i.e. this is an INSERT operation),
128332 ** increment the constraint-counter. */
128333 if( pTab==pFKey->pFrom && nIncr==1 ){
128341 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
128345 int nCol = pFKey->nCol;
128348 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
128352 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
128357 ** to increment the constraint-counter (i.e. this is an INSERT operation),
128359 ** increment the constraint-counter.
128361 ** If any of the parent-key values are NULL, then the row cannot match
128363 ** of the parent-key values are NULL (at this point it is known that
128366 if( pTab==pFKey->pFrom && nIncr==1 ){
128369 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
128372 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
128373 pIdx->aiColumn[i]);
128374 assert( pIdx->aiColumn[i]>=0 );
128375 assert( aiCol[i]!=pTab->iPKey );
128376 if( pIdx->aiColumn[i]==pTab->iPKey ){
128387 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
128394 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
128395 && !pParse->pToplevel
128396 && !pParse->isMultiWrite
128406 if( nIncr>0 && pFKey->isDeferred==0 ){
128409 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
128434 sqlite3 *db = pParse->db;
128438 if( iCol>=0 && iCol!=pTab->iPKey ){
128439 pCol = &pTab->aCol[iCol];
128440 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
128441 pExpr->affExpr = pCol->affinity;
128443 if( zColl==0 ) zColl = db->pDfltColl->zName;
128446 pExpr->iTable = regBase;
128447 pExpr->affExpr = SQLITE_AFF_INTEGER;
128466 pExpr->y.pTab = pTab;
128467 pExpr->iTable = iCursor;
128468 pExpr->iColumn = iCol;
128477 ** code for an SQL UPDATE operation, this function may be called twice -
128480 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
128489 ** --------------------------------------------------------------------------
128511 sqlite3 *db = pParse->db; /* Database handle */
128519 assert( pIdx==0 || pIdx->pTable==pTab );
128520 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
128521 assert( pIdx!=0 || pFKey->nCol==1 );
128525 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
128531 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
128537 for(i=0; i<pFKey->nCol; i++){
128544 iCol = pIdx ? pIdx->aiColumn[i] : -1;
128546 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
128548 zCol = pFKey->pFrom->aCol[iCol].zCnName;
128568 if( pTab==pFKey->pFrom && nIncr>0 ){
128573 pLeft = exprTableRegister(pParse, pTab, regData, -1);
128574 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
128579 for(i=0; i<pIdx->nKeyCol; i++){
128580 i16 iCol = pIdx->aiColumn[i];
128583 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
128601 if( pParse->nErr==0 ){
128603 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
128631 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
128637 ** and all of its sub-components.
128639 ** The Trigger structure or any of its sub-components may be allocated from
128644 TriggerStep *pStep = p->step_list;
128645 sqlite3ExprDelete(dbMem, pStep->pWhere);
128646 sqlite3ExprListDelete(dbMem, pStep->pExprList);
128647 sqlite3SelectDelete(dbMem, pStep->pSelect);
128648 sqlite3ExprDelete(dbMem, p->pWhen);
128660 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
128665 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
128666 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
128667 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
128690 sqlite3 *db = pParse->db;
128691 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
128704 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128705 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
128712 pParse->disableTriggers = 1;
128714 pParse->disableTriggers = 0;
128725 if( (db->flags & SQLITE_DeferFKs)==0 ){
128746 ** is set to -1). If the rowid column is modified by the UPDATE statement
128747 ** the bChngRowid argument is non-zero.
128759 for(i=0; i<p->nCol; i++){
128760 int iChildKey = p->aCol[i].iFrom;
128762 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
128773 ** is set to -1). If the rowid column is modified by the UPDATE statement
128774 ** the bChngRowid argument is non-zero.
128786 for(i=0; i<p->nCol; i++){
128787 char *zKey = p->aCol[i].zCol;
128789 for(iKey=0; iKey<pTab->nCol; iKey++){
128790 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
128791 Column *pCol = &pTab->aCol[iKey];
128793 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
128794 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
128810 if( pTop->pTriggerPrg ){
128811 Trigger *p = pTop->pTriggerPrg->pTrigger;
128812 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
128813 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
128827 ** first register in an array of (pTab->nCol+1) registers containing the
128833 ** first register of an array of (pTab->nCol+1) registers containing the new
128849 sqlite3 *db = pParse->db; /* Database handle */
128853 int isIgnoreErrors = pParse->disableTriggers;
128855 /* Exactly one of regOld and regNew should be non-zero. */
128858 /* If foreign-keys are disabled, this function is a no-op. */
128859 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
128862 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128863 zDb = db->aDb[iDb].zDbSName;
128867 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
128877 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
128887 if( pParse->disableTriggers ){
128888 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
128890 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
128894 if( !isIgnoreErrors || db->mallocFailed ) return;
128901 ** FK counter for each row of the current table with non-NULL keys.
128904 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
128905 for(i=0; i<pFKey->nCol; i++){
128907 iFromCol = pFKey->aCol[i].iFrom;
128908 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
128911 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
128915 assert( pFKey->nCol==1 || (aiFree && pIdx) );
128920 iCol = pFKey->aCol[0].iFrom;
128923 for(i=0; i<pFKey->nCol; i++){
128924 if( aiCol[i]==pTab->iPKey ){
128925 aiCol[i] = -1;
128927 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
128932 if( db->xAuth ){
128934 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
128935 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
128941 /* Take a shared-cache advisory read-lock on the parent table. Allocate
128944 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
128945 pParse->nTab++;
128951 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
128970 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
128979 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
128980 && !pParse->pToplevel && !pParse->isMultiWrite
128989 if( !isIgnoreErrors || db->mallocFailed ) return;
128992 assert( aiCol || pFKey->nCol==1 );
128998 SrcItem *pItem = pSrc->a;
128999 pItem->pTab = pFKey->pFrom;
129000 pItem->zName = pFKey->pFrom->zName;
129001 pItem->pTab->nTabRef++;
129002 pItem->iCursor = pParse->nTab++;
129005 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
129008 int eAction = pFKey->aAction[aChange!=0];
129013 ** So do not set the "may-abort" flag in this case.
129016 ** may-abort flag will eventually be set on this statement anyway
129027 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
129031 pItem->zName = 0;
129049 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
129052 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
129053 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
129055 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
129059 for(i=0; i<pIdx->nKeyCol; i++){
129060 assert( pIdx->aiColumn[i]>=0 );
129061 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
129076 ** entry in the aChange[] array is set to -1. If the column is modified,
129081 ** non-zero. If there is no foreign key related processing, this function
129097 int *aChange, /* Non-NULL for UPDATE operations */
129102 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
129107 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
129114 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
129116 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
129122 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
129124 if( p->aAction[1]!=OE_None ) return 2;
129135 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
129143 ** require no special handling by the triggers sub-system, code for them is
129166 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
129168 sqlite3 *db = pParse->db; /* Database handle */
129173 action = pFKey->aAction[iAction];
129174 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
129177 pTrigger = pFKey->apTrigger[iAction];
129183 int *aiCol = 0; /* child table cols -> parent key cols */
129192 assert( aiCol || pFKey->nCol==1 );
129194 for(i=0; i<pFKey->nCol; i++){
129202 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
129204 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
129205 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
129207 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
129208 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
129246 Column *pCol = pFKey->pFrom->aCol + iFromCol;
129248 if( pCol->colFlags & COLFLAG_GENERATED ){
129249 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
129250 testcase( pCol->colFlags & COLFLAG_STORED );
129253 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
129269 zFrom = pFKey->pFrom->zName;
129273 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129279 pRaise->affExpr = OE_Abort;
129283 assert( pSrc->nSrc==1 );
129284 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
129285 pSrc->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
129302 nFrom + 1 /* Space for pStep->zTarget */
129305 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
129306 pStep->zTarget = (char *)&pStep[1];
129307 memcpy((char *)pStep->zTarget, zFrom, nFrom);
129309 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
129310 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
129311 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
129314 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
129318 /* Re-enable the lookaside buffer, if it was disabled earlier. */
129325 if( db->mallocFailed==1 ){
129334 pStep->op = TK_SELECT;
129338 pStep->op = TK_DELETE;
129343 pStep->op = TK_UPDATE;
129345 pStep->pTrig = pTrigger;
129346 pTrigger->pSchema = pTab->pSchema;
129347 pTrigger->pTabSchema = pTab->pSchema;
129348 pFKey->apTrigger[iAction] = pTrigger;
129349 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
129362 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
129367 /* If foreign-key support is enabled, iterate through all FKs that
129370 ** trigger sub-program. */
129371 if( pParse->db->flags&SQLITE_ForeignKeys ){
129373 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
129393 FKey *pNext; /* Copy of pFKey->pNextFrom */
129397 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
129398 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
129401 if( db->pnBytesFreed==0 ){
129402 if( pFKey->pPrevTo ){
129403 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
129405 void *p = (void *)pFKey->pNextTo;
129406 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
129407 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
129409 if( pFKey->pNextTo ){
129410 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
129414 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
129417 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
129421 fkTriggerDelete(db, pFKey->apTrigger[0]);
129422 fkTriggerDelete(db, pFKey->apTrigger[1]);
129425 pNext = pFKey->pNextFrom;
129467 assert( pParse->pVdbe!=0 );
129468 v = pParse->pVdbe;
129470 sqlite3TableLock(pParse, iDb, pTab->tnum,
129471 (opcode==OP_OpenWrite)?1:0, pTab->zName);
129473 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
129474 VdbeComment((v, "%s", pTab->zName));
129478 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
129479 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
129481 VdbeComment((v, "%s", pTab->zName));
129491 ** ------------------------------
129515 Table *pTab = pIdx->pTable;
129516 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
129517 if( !pIdx->zColAff ){
129521 for(n=0; n<pIdx->nColumn; n++){
129522 i16 x = pIdx->aiColumn[n];
129525 aff = pTab->aCol[x].affinity;
129530 assert( pIdx->bHasExpr );
129531 assert( pIdx->aColExpr!=0 );
129532 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
129536 pIdx->zColAff[n] = aff;
129538 pIdx->zColAff[n] = 0;
129539 return pIdx->zColAff;
129542 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
129543 return pIdx->zColAff;
129554 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
129557 for(i=j=0; i<pTab->nCol; i++){
129558 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
129559 zColAff[j++] = pTab->aCol[i].affinity;
129563 zColAff[j--] = 0;
129573 ** For ordinary (legacy, non-strict) tables:
129574 ** -----------------------------------------
129580 ** which were then optimized out) then this routine becomes a no-op.
129590 ** --------- ---------------
129598 ** ------------------
129612 if( pTab->tabFlags & TF_Strict ){
129621 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
129622 pPrev->opcode = OP_TypeCheck;
129623 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
129626 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
129631 zColAff = pTab->zColAff;
129638 pTab->zColAff = zColAff;
129646 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
129647 || sqlite3VdbeDb(v)->mallocFailed );
129648 sqlite3VdbeChangeP4(v, -1, zColAff, i);
129654 ** Return non-zero if the table pTab in database iDb or any of its indices
129664 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
129670 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
129672 Pgno tnum = pOp->p2;
129673 if( tnum==pTab->tnum ){
129676 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
129677 if( tnum==pIndex->tnum ){
129683 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
129684 assert( pOp->p4.pVtab!=0 );
129685 assert( pOp->p4type==P4_VTAB );
129697 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
129698 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
129699 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
129723 assert( pTab->tabFlags & TF_HasGenerated );
129724 testcase( pTab->tabFlags & TF_HasVirtual );
129725 testcase( pTab->tabFlags & TF_HasStored );
129730 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
129731 if( (pTab->tabFlags & TF_HasStored)!=0 ){
129732 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
129733 if( pOp->opcode==OP_Affinity ){
129735 ** columns. '@' is the no-op affinity and those columns have not
129738 char *zP4 = pOp->p4.z;
129740 assert( pOp->p4type==P4_DYNAMIC );
129742 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
129745 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
129750 }else if( pOp->opcode==OP_TypeCheck ){
129754 pOp->p3 = 1;
129759 ** this is a two-pass algorithm. On the first pass, mark all generated
129762 for(i=0; i<pTab->nCol; i++){
129763 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
129764 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
129765 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
129766 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
129775 /* On the second pass, compute the value of each NOT-AVAILABLE column.
129780 pParse->iSelfTab = -iRegStore;
129784 for(i=0; i<pTab->nCol; i++){
129785 Column *pCol = pTab->aCol + i;
129786 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
129788 pCol->colFlags |= COLFLAG_BUSY;
129791 pCol->colFlags &= ~COLFLAG_BUSY;
129797 assert( pCol->colFlags & COLFLAG_GENERATED );
129800 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
129805 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
129807 pParse->iSelfTab = 0;
129842 assert( pParse->db->aDb[iDb].pSchema!=0 );
129843 if( (pTab->tabFlags & TF_Autoincrement)!=0
129844 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
129848 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
129852 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
129856 || pSeqTab->nCol!=2
129858 pParse->nErr++;
129859 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
129863 pInfo = pToplevel->pAinc;
129864 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
129866 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
129868 testcase( pParse->earlyCleanup );
129869 if( pParse->db->mallocFailed ) return 0;
129870 pInfo->pNext = pToplevel->pAinc;
129871 pToplevel->pAinc = pInfo;
129872 pInfo->pTab = pTab;
129873 pInfo->iDb = iDb;
129874 pToplevel->nMem++; /* Register to hold name of table */
129875 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
129876 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
129878 memId = pInfo->regCtr;
129889 sqlite3 *db = pParse->db; /* The database connection */
129892 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
129894 /* This routine is never called during trigger-generation. It is
129895 ** only called from the top-level */
129896 assert( pParse->pTriggerTab==0 );
129900 for(p = pParse->pAinc; p; p = p->pNext){
129917 pDb = &db->aDb[p->iDb];
129918 memId = p->regCtr;
129919 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129920 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
129921 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
129927 aOp[3].p1 = memId-1;
129936 if( pParse->nTab==0 ) pParse->nTab = 1;
129950 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
129963 Vdbe *v = pParse->pVdbe;
129964 sqlite3 *db = pParse->db;
129967 for(p = pParse->pAinc; p; p = p->pNext){
129977 Db *pDb = &db->aDb[p->iDb];
129979 int memId = p->regCtr;
129982 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129985 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
129990 aOp[2].p1 = memId-1;
129999 if( pParse->pAinc ) autoIncrementEnd(pParse);
130004 ** above are all no-ops
130028 ** then a list of all (non-hidden) columns for the table is substituted.
130033 ** first two forms shown above. A VALUES clause is really just short-hand
130039 ** insert with data coming from a single-row VALUES clause, the code executes
130040 ** once straight down through. Pseudo-code follows (we call this
130052 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
130076 ** X <- A
130084 ** end-coroutine X
130097 ** X <- A
130105 ** end co-routine R
130134 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
130163 db = pParse->db;
130164 assert( db->pParse==pParse );
130165 if( pParse->nErr ){
130168 assert( db->mallocFailed==0 );
130175 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
130176 pList = pSelect->pEList;
130177 pSelect->pEList = 0;
130184 assert( pTabList->nSrc==1 );
130189 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130190 assert( iDb<db->nDb );
130191 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
130192 db->aDb[iDb].zDbSName) ){
130217 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
130223 ** ViewGetColumnNames() is a no-op if pTab is not a view.
130229 /* Cannot insert into a read-only table.
130239 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
130271 regRowid = regIns = pParse->nMem+1;
130272 pParse->nMem += pTab->nCol + 1;
130275 pParse->nMem++;
130288 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
130289 ** loop, if ipkColumn==(-1), that means that integer primary key
130298 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
130300 assert( pColumn->eU4!=EU4_EXPR );
130301 pColumn->eU4 = EU4_IDX;
130302 for(i=0; i<pColumn->nId; i++){
130303 pColumn->a[i].u4.idx = -1;
130305 for(i=0; i<pColumn->nId; i++){
130306 for(j=0; j<pTab->nCol; j++){
130307 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
130308 pColumn->a[i].u4.idx = j;
130310 if( j==pTab->iPKey ){
130314 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
130317 pTab->aCol[j].zCnName);
130324 if( j>=pTab->nCol ){
130325 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
130330 pTabList->a, pColumn->a[i].zName);
130331 pParse->checkSchema = 1;
130339 ** is coming from a SELECT statement, then generate a co-routine that
130341 ** co-routine is the common header to the 3rd and 4th templates.
130344 /* Data is coming from a SELECT or from a multi-row VALUES clause.
130345 ** Generate a co-routine to run the SELECT. */
130346 int regYield; /* Register holding co-routine entry-point */
130347 int addrTop; /* Top of the co-routine */
130350 regYield = ++pParse->nMem;
130355 dest.nSdst = pTab->nCol;
130358 assert( db->pParse==pParse );
130359 if( rc || pParse->nErr ) goto insert_cleanup;
130360 assert( db->mallocFailed==0 );
130362 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
130363 assert( pSelect->pEList );
130364 nColumn = pSelect->pEList->nExpr;
130394 srcTab = pParse->nTab++;
130409 ** single-row VALUES clause
130414 srcTab = -1;
130417 nColumn = pList->nExpr;
130431 ipkColumn = pTab->iPKey;
130433 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
130434 testcase( pTab->tabFlags & TF_HasVirtual );
130435 testcase( pTab->tabFlags & TF_HasStored );
130436 for(i=ipkColumn-1; i>=0; i--){
130437 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
130438 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
130439 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
130440 ipkColumn--;
130452 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
130453 for(i=0; i<pTab->nCol; i++){
130454 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
130457 if( nColumn!=(pTab->nCol-nHidden) ){
130460 pTabList->a, pTab->nCol-nHidden, nColumn);
130464 if( pColumn!=0 && nColumn!=pColumn->nId ){
130465 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
130471 if( (db->flags & SQLITE_CountRows)!=0
130472 && !pParse->nested
130473 && !pParse->pTriggerTab
130474 && !pParse->bReturning
130476 regRowCount = ++pParse->nMem;
130483 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
130489 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
130491 aRegIdx[i] = ++pParse->nMem;
130492 pParse->nMem += pIdx->nColumn;
130494 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
130501 pTab->zName);
130508 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
130511 pTabList->a[0].iCursor = iDataCur;
130514 pNx->pUpsertSrc = pTabList;
130515 pNx->regData = regData;
130516 pNx->iDataCur = iDataCur;
130517 pNx->iIdxCur = iIdxCur;
130518 if( pNx->pUpsertTarget ){
130523 pNx = pNx->pNextUpsert;
130551 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
130555 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
130557 ** the value does not get overwritten by a NULL at tag-20191021-002. */
130571 for(i=0; i<pTab->nCol; i++, iRegStore++){
130575 if( i==pTab->iPKey ){
130576 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
130579 ** NULL - we cannot optimize further by skipping the column completely */
130583 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
130589 iRegStore--;
130593 ** triggers, the slots used for stored columns will be OP_Copy-ed
130604 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130610 assert( pColumn->eU4==EU4_IDX );
130611 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
130612 if( j>=pColumn->nId ){
130616 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130624 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130628 k = i - nHidden;
130638 Expr *pX = pList->a[k].pExpr;
130652 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
130658 ** not happened yet) so we substitute a rowid of -1
130661 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
130669 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
130672 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
130678 assert( pTab->nNVCol>0 || pParse->nErr>0 );
130679 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
130686 if( pTab->tabFlags & TF_HasGenerated ){
130687 testcase( pTab->tabFlags & TF_HasVirtual );
130688 testcase( pTab->tabFlags & TF_HasStored );
130704 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
130706 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
130719 /* Rowid already initialized at tag-20191021-001 */
130721 Expr *pIpk = pList->a[ipkColumn].pExpr;
130722 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
130726 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
130757 if( pTab->tabFlags & TF_HasGenerated ){
130769 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
130780 if( db->flags & SQLITE_ForeignKeys ){
130798 }else if( pParse->bReturning ){
130800 ** constant value -1, in case one or more of the returned expressions
130802 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
130815 pTab, regData-2-pTab->nCol, onError, endOfLoop);
130832 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
130833 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
130847 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
130883 ** Meanings of bits in of pWalker->eCode for
130890 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
130895 if( pExpr->op==TK_COLUMN ){
130896 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
130897 if( pExpr->iColumn>=0 ){
130898 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
130899 pWalker->eCode |= CKCNSTRNT_COLUMN;
130902 pWalker->eCode |= CKCNSTRNT_ROWID;
130909 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
130918 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
130919 ** The operation of this routine is the same - return true if an only if
130947 ** the indexes of a table in the order provided in the Table->pIndex list.
130948 ** However, sometimes (rarely - when there is an upsert) it wants to visit
130982 assert( pIter->i==0 );
130983 if( pIter->eType ){
130984 *pIx = pIter->u.ax.aIdx[0].ix;
130985 return pIter->u.ax.aIdx[0].p;
130988 return pIter->u.lx.pIdx;
130994 if( pIter->eType ){
130995 int i = ++pIter->i;
130996 if( i>=pIter->u.ax.nIdx ){
131000 *pIx = pIter->u.ax.aIdx[i].ix;
131001 return pIter->u.ax.aIdx[i].p;
131004 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
131005 return pIter->u.lx.pIdx;
131015 ** pTab->nCol+1 registers in this range. The first register (the one
131042 ** at pTab->pIndex.
131044 ** (2019-05-07) The generated code also creates a new record for the
131046 ** register identified by aRegIdx[nIdx] - in other words in the first
131057 ** for the first index in the pTab->pIndex list. Cursors for other indices
131058 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
131066 ** --------------- ---------- ----------------------------------------
131096 ** Or if overrideError==OE_Default, then the pParse->onError parameter
131097 ** is used. Or if pParse->onError==OE_Default then the onError value
131108 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
131142 db = pParse->db;
131143 v = pParse->pVdbe;
131146 nCol = pTab->nCol;
131157 nPkField = pPk->nKeyCol;
131166 if( pTab->tabFlags & TF_HasNotNull ){
131173 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
131174 int isGenerated; /* non-zero if column is generated */
131175 onError = pCol->notNull;
131177 if( i==pTab->iPKey ){
131180 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
131196 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
131198 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
131199 testcase( pCol->colFlags & COLFLAG_STORED );
131200 testcase( pCol->colFlags & COLFLAG_GENERATED );
131216 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
131228 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
131229 pCol->zCnName);
131230 testcase( zMsg==0 && db->mallocFailed==0 );
131255 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
131263 } /* end of 2-pass loop */
131264 } /* end if( has-not-null-constraints ) */
131269 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
131270 ExprList *pCheck = pTab->pCheck;
131271 pParse->iSelfTab = -(regNewData+1);
131273 for(i=0; i<pCheck->nExpr; i++){
131276 Expr *pExpr = pCheck->a[i].pExpr;
131291 if( !db->mallocFailed ){
131298 char *zName = pCheck->a[i].zEName;
131299 assert( zName!=0 || pParse->db->mallocFailed );
131300 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
131307 pParse->iSelfTab = 0;
131324 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
131342 sIdxIter.u.lx.pIdx = pTab->pIndex;
131344 if( pUpsert->pUpsertTarget==0 ){
131345 /* There is just on ON CONFLICT clause and it has no constraint-target */
131346 assert( pUpsert->pNextUpsert==0 );
131347 if( pUpsert->isDoUpdate==0 ){
131348 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
131356 }else if( pTab->pIndex!=0 ){
131364 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
131373 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
131374 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
131375 if( pTerm->pUpsertTarget==0 ) break;
131376 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
131378 pIdx = pTab->pIndex;
131379 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
131380 pIdx = pIdx->pNext;
131389 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
131419 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
131425 if( db->flags&SQLITE_RecTriggers ){
131435 regTrigCnt = ++pParse->nMem;
131450 onError = pTab->keyConf;
131461 if( pUpsertClause->isDoUpdate==0 ){
131482 && pTab->pIndex /* There exist other constraints */
131521 ** recursive-triggers flag is set, call GenerateRowDelete() to
131523 ** the triggers and remove both the table and index b-tree entries.
131525 ** Otherwise, if there are no triggers or the recursive-triggers
131527 ** GenerateRowIndexDelete(). This removes the index b-tree entries
131528 ** only. The table b-tree entry will be replaced by the new entry
131545 regNewData, 1, 0, OE_Replace, 1, -1);
131551 /* This OP_Delete opcode fires the pre-update-hook only. It does
131552 ** not modify the b-tree. It is more efficient to let the coming
131558 if( pTab->pIndex ){
131560 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
131583 sqlite3VdbeJumpHere(v, ipkTop-1);
131616 VdbeNoopComment((v, "prep index %s", pIdx->zName));
131621 if( pIdx->pPartIdxWhere ){
131623 pParse->iSelfTab = -(regNewData+1);
131624 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
131626 pParse->iSelfTab = 0;
131633 for(i=0; i<pIdx->nColumn; i++){
131634 int iField = pIdx->aiColumn[i];
131637 pParse->iSelfTab = -(regNewData+1);
131638 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
131639 pParse->iSelfTab = 0;
131640 VdbeComment((v, "%s column %d", pIdx->zName, i));
131641 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
131649 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
131652 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
131653 VdbeComment((v, "for %s", pIdx->zName));
131655 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
131656 sqlite3SetMakeRecordP5(v, pIdx->pTable);
131659 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
131671 onError = pIdx->onError;
131684 if( pUpsertClause->isDoUpdate==0 ){
131699 ** must be explicitly deleted in order to ensure any pre-update hook
131703 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
131706 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
131708 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
131709 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
131720 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
131728 ** is different from old-rowid */
131737 ** store it in registers regR..regR+nPk-1 */
131739 for(i=0; i<pPk->nKeyCol; i++){
131740 assert( pPk->aiColumn[i]>=0 );
131741 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
131743 VdbeComment((v, "%s.%s", pTab->zName,
131744 pTab->aCol[pPk->aiColumn[i]].zCnName));
131755 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
131759 for(i=0; i<pPk->nKeyCol; i++){
131760 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
131761 x = pPk->aiColumn[i];
131763 if( i==(pPk->nKeyCol-1) ){
131807 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
131808 assert( nConflictCk>0 || db->mallocFailed );
131836 if( pIdx->pPartIdxWhere ){
131839 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
131843 ** the constraint-ok jump destination to be the address of
131864 nConflictCk--;
131919 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
131933 ** to be the number of columns in table pTab that must not be NULL-trimmed.
131935 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
131941 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
131942 if( pTab->pSchema->file_format<2 ) return;
131944 for(i=pTab->nCol-1; i>0; i--){
131945 if( pTab->aCol[i].iDflt!=0 ) break;
131946 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
131955 ** PK index. This function adds code to invoke the pre-update hook,
131965 Vdbe *v = pParse->pVdbe;
131968 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
132008 v = pParse->pVdbe;
132011 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
132013 assert( pIdx->onError!=OE_Replace
132014 || pIdx->pNext==0
132015 || pIdx->pNext->onError==OE_Replace );
132017 if( pIdx->pPartIdxWhere ){
132031 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
132035 if( pParse->nested ){
132048 if( !pParse->nested ){
132064 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
132070 ** pTab->pIndex list.
132072 ** If pTab is a virtual table, then this routine is a no-op and the
132094 /* This routine is a no-op for virtual tables. Leave the output
132097 *piDataCur = *piIdxCur = -999;
132100 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
132101 v = pParse->pVdbe;
132103 if( iBase<0 ) iBase = pParse->nTab;
132109 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
132112 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
132114 assert( pIdx->pSchema==pTab->pSchema );
132120 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
132123 VdbeComment((v, "%s", pIdx->zName));
132126 if( iBase>pParse->nTab ) pParse->nTab = iBase;
132135 ** purposes only - to make sure the transfer optimization really
132157 assert( pDest->pTable!=pSrc->pTable );
132158 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
132161 if( pDest->onError!=pSrc->onError ){
132164 for(i=0; i<pSrc->nKeyCol; i++){
132165 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
132168 if( pSrc->aiColumn[i]==XN_EXPR ){
132169 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
132170 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
132171 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
132175 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
132178 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
132182 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
132200 ** There are lots of rules for determining compatibility - see comments
132205 ** is empty - a factor that can only be determined at run-time. In that
132222 sqlite3 *db = pParse->db;
132226 SrcItem *pItem; /* An element of pSelect->pSrc */
132239 if( pParse->pWith || pSelect->pWith ){
132251 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
132254 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
132255 if( pSelect->pSrc->nSrc!=1 ){
132258 if( pSelect->pSrc->a[0].pSelect ){
132261 if( pSelect->pWhere ){
132264 if( pSelect->pOrderBy ){
132269 if( pSelect->pGroupBy ){
132272 if( pSelect->pLimit ){
132275 if( pSelect->pPrior ){
132278 if( pSelect->selFlags & SF_Distinct ){
132281 pEList = pSelect->pEList;
132283 if( pEList->nExpr!=1 ){
132286 assert( pEList->a[0].pExpr );
132287 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
132295 pItem = pSelect->pSrc->a;
132300 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
132310 if( pDest->nCol!=pSrc->nCol ){
132313 if( pDest->iPKey!=pSrc->iPKey ){
132316 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
132317 return 0; /* Cannot feed from a non-strict into a strict table */
132319 for(i=0; i<pDest->nCol; i++){
132320 Column *pDestCol = &pDest->aCol[i];
132321 Column *pSrcCol = &pSrc->aCol[i];
132323 if( (db->mDbFlags & DBFLAG_Vacuum)==0
132324 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
132346 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
132347 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
132348 return 0; /* Both columns have the same generated-column type */
132354 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
132357 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
132358 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
132359 testcase( pDestCol->colFlags & COLFLAG_STORED );
132364 if( pDestCol->affinity!=pSrcCol->affinity ){
132371 if( pDestCol->notNull && !pSrcCol->notNull ){
132375 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
132378 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
132380 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
132383 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
132384 pSrcExpr->u.zToken)!=0)
132390 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
132394 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
132400 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
132405 return 0; /* Corrupt schema - two indexes on the same btree */
132409 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
132422 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
132426 if( (db->flags & SQLITE_CountRows)!=0 ){
132437 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
132440 iSrc = pParse->nTab++;
132441 iDest = pParse->nTab++;
132448 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
132449 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
132478 if( pDest->iPKey>=0 ){
132480 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132488 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
132492 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
132495 if( db->mDbFlags & DBFLAG_Vacuum ){
132502 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132511 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132512 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
132520 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
132521 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
132523 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
132525 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
132529 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
132531 VdbeComment((v, "%s", pSrcIdx->zName));
132532 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
132535 VdbeComment((v, "%s", pDestIdx->zName));
132537 if( db->mDbFlags & DBFLAG_Vacuum ){
132542 ** order. In this case, instead of seeking within the b-tree as part
132544 ** OP_IdxInsert to seek to the point within the b-tree where each key
132552 for(i=0; i<pSrcIdx->nColumn; i++){
132553 const char *zColl = pSrcIdx->azColl[i];
132556 if( i==pSrcIdx->nColumn ){
132561 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
132566 if( (db->mDbFlags & DBFLAG_Vacuum)==0
132642 sqlite3_mutex_enter(db->mutex);
132649 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
132655 /* this happens for a comment or white-space */
132668 && db->flags&SQLITE_NullCallback)) ){
132695 /* EVIDENCE-OF: R-38229-40159 If the callback function to
132696 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
132734 assert( (rc&db->errMask)==rc );
132735 sqlite3_mutex_leave(db->mutex);
133141 ** (part of the main SQLite library - not an extension) so that
133148 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
133150 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
133152 #define sqlite3_bind_blob sqlite3_api->bind_blob
133153 #define sqlite3_bind_double sqlite3_api->bind_double
133154 #define sqlite3_bind_int sqlite3_api->bind_int
133155 #define sqlite3_bind_int64 sqlite3_api->bind_int64
133156 #define sqlite3_bind_null sqlite3_api->bind_null
133157 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
133158 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
133159 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
133160 #define sqlite3_bind_text sqlite3_api->bind_text
133161 #define sqlite3_bind_text16 sqlite3_api->bind_text16
133162 #define sqlite3_bind_value sqlite3_api->bind_value
133163 #define sqlite3_busy_handler sqlite3_api->busy_handler
133164 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
133165 #define sqlite3_changes sqlite3_api->changes
133166 #define sqlite3_close sqlite3_api->close
133167 #define sqlite3_collation_needed sqlite3_api->collation_needed
133168 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
133169 #define sqlite3_column_blob sqlite3_api->column_blob
133170 #define sqlite3_column_bytes sqlite3_api->column_bytes
133171 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
133172 #define sqlite3_column_count sqlite3_api->column_count
133173 #define sqlite3_column_database_name sqlite3_api->column_database_name
133174 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
133175 #define sqlite3_column_decltype sqlite3_api->column_decltype
133176 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
133177 #define sqlite3_column_double sqlite3_api->column_double
133178 #define sqlite3_column_int sqlite3_api->column_int
133179 #define sqlite3_column_int64 sqlite3_api->column_int64
133180 #define sqlite3_column_name sqlite3_api->column_name
133181 #define sqlite3_column_name16 sqlite3_api->column_name16
133182 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
133183 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
133184 #define sqlite3_column_table_name sqlite3_api->column_table_name
133185 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
133186 #define sqlite3_column_text sqlite3_api->column_text
133187 #define sqlite3_column_text16 sqlite3_api->column_text16
133188 #define sqlite3_column_type sqlite3_api->column_type
133189 #define sqlite3_column_value sqlite3_api->column_value
133190 #define sqlite3_commit_hook sqlite3_api->commit_hook
133191 #define sqlite3_complete sqlite3_api->complete
133192 #define sqlite3_complete16 sqlite3_api->complete16
133193 #define sqlite3_create_collation sqlite3_api->create_collation
133194 #define sqlite3_create_collation16 sqlite3_api->create_collation16
133195 #define sqlite3_create_function sqlite3_api->create_function
133196 #define sqlite3_create_function16 sqlite3_api->create_function16
133197 #define sqlite3_create_module sqlite3_api->create_module
133198 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
133199 #define sqlite3_data_count sqlite3_api->data_count
133200 #define sqlite3_db_handle sqlite3_api->db_handle
133201 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
133202 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
133203 #define sqlite3_errcode sqlite3_api->errcode
133204 #define sqlite3_errmsg sqlite3_api->errmsg
133205 #define sqlite3_errmsg16 sqlite3_api->errmsg16
133206 #define sqlite3_exec sqlite3_api->exec
133208 #define sqlite3_expired sqlite3_api->expired
133210 #define sqlite3_finalize sqlite3_api->finalize
133211 #define sqlite3_free sqlite3_api->free
133212 #define sqlite3_free_table sqlite3_api->free_table
133213 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
133214 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
133215 #define sqlite3_get_table sqlite3_api->get_table
133217 #define sqlite3_global_recover sqlite3_api->global_recover
133219 #define sqlite3_interrupt sqlite3_api->interruptx
133220 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
133221 #define sqlite3_libversion sqlite3_api->libversion
133222 #define sqlite3_libversion_number sqlite3_api->libversion_number
133223 #define sqlite3_malloc sqlite3_api->malloc
133224 #define sqlite3_mprintf sqlite3_api->mprintf
133225 #define sqlite3_open sqlite3_api->open
133226 #define sqlite3_open16 sqlite3_api->open16
133227 #define sqlite3_prepare sqlite3_api->prepare
133228 #define sqlite3_prepare16 sqlite3_api->prepare16
133229 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
133230 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
133231 #define sqlite3_profile sqlite3_api->profile
133232 #define sqlite3_progress_handler sqlite3_api->progress_handler
133233 #define sqlite3_realloc sqlite3_api->realloc
133234 #define sqlite3_reset sqlite3_api->reset
133235 #define sqlite3_result_blob sqlite3_api->result_blob
133236 #define sqlite3_result_double sqlite3_api->result_double
133237 #define sqlite3_result_error sqlite3_api->result_error
133238 #define sqlite3_result_error16 sqlite3_api->result_error16
133239 #define sqlite3_result_int sqlite3_api->result_int
133240 #define sqlite3_result_int64 sqlite3_api->result_int64
133241 #define sqlite3_result_null sqlite3_api->result_null
133242 #define sqlite3_result_text sqlite3_api->result_text
133243 #define sqlite3_result_text16 sqlite3_api->result_text16
133244 #define sqlite3_result_text16be sqlite3_api->result_text16be
133245 #define sqlite3_result_text16le sqlite3_api->result_text16le
133246 #define sqlite3_result_value sqlite3_api->result_value
133247 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
133248 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
133249 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
133250 #define sqlite3_snprintf sqlite3_api->xsnprintf
133251 #define sqlite3_step sqlite3_api->step
133252 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
133253 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
133254 #define sqlite3_total_changes sqlite3_api->total_changes
133255 #define sqlite3_trace sqlite3_api->trace
133257 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
133259 #define sqlite3_update_hook sqlite3_api->update_hook
133260 #define sqlite3_user_data sqlite3_api->user_data
133261 #define sqlite3_value_blob sqlite3_api->value_blob
133262 #define sqlite3_value_bytes sqlite3_api->value_bytes
133263 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
133264 #define sqlite3_value_double sqlite3_api->value_double
133265 #define sqlite3_value_int sqlite3_api->value_int
133266 #define sqlite3_value_int64 sqlite3_api->value_int64
133267 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
133268 #define sqlite3_value_text sqlite3_api->value_text
133269 #define sqlite3_value_text16 sqlite3_api->value_text16
133270 #define sqlite3_value_text16be sqlite3_api->value_text16be
133271 #define sqlite3_value_text16le sqlite3_api->value_text16le
133272 #define sqlite3_value_type sqlite3_api->value_type
133273 #define sqlite3_vmprintf sqlite3_api->vmprintf
133274 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
133275 #define sqlite3_overload_function sqlite3_api->overload_function
133276 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
133277 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
133278 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
133279 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
133280 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
133281 #define sqlite3_blob_close sqlite3_api->blob_close
133282 #define sqlite3_blob_open sqlite3_api->blob_open
133283 #define sqlite3_blob_read sqlite3_api->blob_read
133284 #define sqlite3_blob_write sqlite3_api->blob_write
133285 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
133286 #define sqlite3_file_control sqlite3_api->file_control
133287 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
133288 #define sqlite3_memory_used sqlite3_api->memory_used
133289 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
133290 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
133291 #define sqlite3_mutex_free sqlite3_api->mutex_free
133292 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
133293 #define sqlite3_mutex_try sqlite3_api->mutex_try
133294 #define sqlite3_open_v2 sqlite3_api->open_v2
133295 #define sqlite3_release_memory sqlite3_api->release_memory
133296 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
133297 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
133298 #define sqlite3_sleep sqlite3_api->sleep
133299 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
133300 #define sqlite3_vfs_find sqlite3_api->vfs_find
133301 #define sqlite3_vfs_register sqlite3_api->vfs_register
133302 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
133303 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
133304 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
133305 #define sqlite3_result_error_code sqlite3_api->result_error_code
133306 #define sqlite3_test_control sqlite3_api->test_control
133307 #define sqlite3_randomness sqlite3_api->randomness
133308 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
133309 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
133310 #define sqlite3_limit sqlite3_api->limit
133311 #define sqlite3_next_stmt sqlite3_api->next_stmt
133312 #define sqlite3_sql sqlite3_api->sql
133313 #define sqlite3_status sqlite3_api->status
133314 #define sqlite3_backup_finish sqlite3_api->backup_finish
133315 #define sqlite3_backup_init sqlite3_api->backup_init
133316 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
133317 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
133318 #define sqlite3_backup_step sqlite3_api->backup_step
133319 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
133320 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
133321 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
133322 #define sqlite3_db_config sqlite3_api->db_config
133323 #define sqlite3_db_mutex sqlite3_api->db_mutex
133324 #define sqlite3_db_status sqlite3_api->db_status
133325 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
133326 #define sqlite3_log sqlite3_api->log
133327 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
133328 #define sqlite3_sourceid sqlite3_api->sourceid
133329 #define sqlite3_stmt_status sqlite3_api->stmt_status
133330 #define sqlite3_strnicmp sqlite3_api->strnicmp
133331 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
133332 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
133333 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
133334 #define sqlite3_wal_hook sqlite3_api->wal_hook
133335 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
133336 #define sqlite3_vtab_config sqlite3_api->vtab_config
133337 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
133339 #define sqlite3_close_v2 sqlite3_api->close_v2
133340 #define sqlite3_db_filename sqlite3_api->db_filename
133341 #define sqlite3_db_readonly sqlite3_api->db_readonly
133342 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
133343 #define sqlite3_errstr sqlite3_api->errstr
133344 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
133345 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
133346 #define sqlite3_stricmp sqlite3_api->stricmp
133347 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
133348 #define sqlite3_uri_int64 sqlite3_api->uri_int64
133349 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
133350 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
133351 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
133353 #define sqlite3_auto_extension sqlite3_api->auto_extension
133354 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
133355 #define sqlite3_bind_text64 sqlite3_api->bind_text64
133356 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
133357 #define sqlite3_load_extension sqlite3_api->load_extension
133358 #define sqlite3_malloc64 sqlite3_api->malloc64
133359 #define sqlite3_msize sqlite3_api->msize
133360 #define sqlite3_realloc64 sqlite3_api->realloc64
133361 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
133362 #define sqlite3_result_blob64 sqlite3_api->result_blob64
133363 #define sqlite3_result_text64 sqlite3_api->result_text64
133364 #define sqlite3_strglob sqlite3_api->strglob
133366 #define sqlite3_value_dup sqlite3_api->value_dup
133367 #define sqlite3_value_free sqlite3_api->value_free
133368 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
133369 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
133371 #define sqlite3_value_subtype sqlite3_api->value_subtype
133372 #define sqlite3_result_subtype sqlite3_api->result_subtype
133374 #define sqlite3_status64 sqlite3_api->status64
133375 #define sqlite3_strlike sqlite3_api->strlike
133376 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
133378 #define sqlite3_system_errno sqlite3_api->system_errno
133380 #define sqlite3_trace_v2 sqlite3_api->trace_v2
133381 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
133383 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
133385 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
133386 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
133387 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
133388 #define sqlite3_result_pointer sqlite3_api->result_pointer
133389 #define sqlite3_value_pointer sqlite3_api->value_pointer
133391 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
133392 #define sqlite3_value_nochange sqlite3_api->value_nochange
133393 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
133395 #define sqlite3_keyword_count sqlite3_api->keyword_count
133396 #define sqlite3_keyword_name sqlite3_api->keyword_name
133397 #define sqlite3_keyword_check sqlite3_api->keyword_check
133398 #define sqlite3_str_new sqlite3_api->str_new
133399 #define sqlite3_str_finish sqlite3_api->str_finish
133400 #define sqlite3_str_appendf sqlite3_api->str_appendf
133401 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
133402 #define sqlite3_str_append sqlite3_api->str_append
133403 #define sqlite3_str_appendall sqlite3_api->str_appendall
133404 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
133405 #define sqlite3_str_reset sqlite3_api->str_reset
133406 #define sqlite3_str_errcode sqlite3_api->str_errcode
133407 #define sqlite3_str_length sqlite3_api->str_length
133408 #define sqlite3_str_value sqlite3_api->str_value
133410 #define sqlite3_create_window_function sqlite3_api->create_window_function
133412 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
133414 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
133415 #define sqlite3_value_frombind sqlite3_api->value_frombind
133417 #define sqlite3_drop_modules sqlite3_api->drop_modules
133419 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
133420 #define sqlite3_uri_key sqlite3_api->uri_key
133421 #define sqlite3_filename_database sqlite3_api->filename_database
133422 #define sqlite3_filename_journal sqlite3_api->filename_journal
133423 #define sqlite3_filename_wal sqlite3_api->filename_wal
133425 #define sqlite3_create_filename sqlite3_api->create_filename
133426 #define sqlite3_free_filename sqlite3_api->free_filename
133427 #define sqlite3_database_file_object sqlite3_api->database_file_object
133429 #define sqlite3_txn_state sqlite3_api->txn_state
133431 #define sqlite3_changes64 sqlite3_api->changes64
133432 #define sqlite3_total_changes64 sqlite3_api->total_changes64
133434 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
133436 #define sqlite3_error_offset sqlite3_api->error_offset
133437 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
133438 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
133439 #define sqlite3_vtab_in sqlite3_api->vtab_in
133440 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
133441 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
133444 #define sqlite3_deserialize sqlite3_api->deserialize
133445 #define sqlite3_serialize sqlite3_api->serialize
133447 #define sqlite3_db_name sqlite3_api->db_name
133449 #define sqlite3_value_encoding sqlite3_api->value_encoding
133451 #define sqlite3_is_interrupted sqlite3_api->is_interrupted
133464 # define SQLITE_EXTENSION_INIT1 /*no-op*/
133466 # define SQLITE_EXTENSION_INIT3 /*no-op*/
133997 sqlite3_vfs *pVfs = db->pVfs;
134029 if( (db->flags & SQLITE_LoadExtension)==0 ){
134038 /* tag-20210611-1. Some dlopen() implementations will segfault if given
134043 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
134082 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
134102 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
134121 /* Append the new shared library handle to the db->aExtension array. */
134122 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
134126 if( db->nExtension>0 ){
134127 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
134129 sqlite3DbFree(db, db->aExtension);
134130 db->aExtension = aHandle;
134132 db->aExtension[db->nExtension++] = handle;
134143 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
134155 sqlite3_mutex_enter(db->mutex);
134158 sqlite3_mutex_leave(db->mutex);
134168 assert( sqlite3_mutex_held(db->mutex) );
134169 for(i=0; i<db->nExtension; i++){
134170 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
134172 sqlite3DbFree(db, db->aExtension);
134180 sqlite3_mutex_enter(db->mutex);
134182 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
134184 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
134186 sqlite3_mutex_leave(db->mutex);
134207 ** we have to locate the state vector at run-time. In the more common
134267 ** routine is a no-op.
134282 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
134284 wsdAutoext.nExt--;
134450 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
134454 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
134456 /* Names of columns for pragmas that return multi-column result
134457 ** or that return single-column results where the name of the
134524 /* Definitions of all built-in pragmas */
135115 ** Interpret the given string as an auto-vacuum mode value.
135133 ** backed temporary databases, 2 for the Red-Black tree in memory database
135134 ** and 0 to use the compile-time default.
135138 return z[0] - '0';
135155 sqlite3 *db = pParse->db;
135156 if( db->aDb[1].pBt!=0 ){
135157 if( !db->autoCommit
135158 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
135164 sqlite3BtreeClose(db->aDb[1].pBt);
135165 db->aDb[1].pBt = 0;
135180 sqlite3 *db = pParse->db;
135181 if( db->temp_store==ts ) return SQLITE_OK;
135185 db->temp_store = (u8)ts;
135197 u8 n = pPragma->nPragCName;
135200 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
135203 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
135237 if( db->autoCommit ){
135238 Db *pDb = db->aDb;
135239 int n = db->nDb;
135245 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
135246 while( (n--) > 0 ){
135247 if( pDb->pBt ){
135248 sqlite3BtreeSetPagerFlags(pDb->pBt,
135249 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
135256 # define setAllPagerFlags(X) /* no-op */
135261 ** Return a human-readable name for a constraint resolution action.
135282 ** journal-mode name.
135309 upr = ArraySize(aPragmaName)-1;
135315 upr = mid - 1;
135330 int isBuiltin, /* True if this is a built-in function */
135341 for(; p; p=p->pNext){
135350 if( p->xSFunc==0 ) continue;
135351 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
135356 if( p->xValue!=0 ){
135358 }else if( p->xFinalize!=0 ){
135364 p->zName, isBuiltin,
135365 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
135366 p->nArg,
135367 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
135376 ** Generate code to output a single-column result row with a value of the
135409 int minusFlag /* True if a '-' sign preceded <value> */
135411 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
135412 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
135418 sqlite3 *db = pParse->db; /* The database connection */
135425 pParse->nMem = 2;
135431 pDb = &db->aDb[iDb];
135443 zRight = sqlite3MPrintf(db, "-%T", pValue);
135449 zDb = pId2->n>0 ? pDb->zDbSName : 0;
135454 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
135456 ** handled the pragma and generate a no-op prepared statement.
135458 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
135463 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
135473 db->busyHandler.nBusy = 0;
135487 pParse->nErr++;
135488 pParse->rc = rc;
135495 /* IMP: R-43042-22504 No error messages are generated if an
135501 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
135506 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
135507 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
135513 switch( pPragma->ePragTyp ){
135548 pParse->nMem += 2;
135560 pDb->pSchema->cache_size = size;
135561 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
135578 Btree *pBt = pDb->pBt;
135584 /* Malloc may fail when setting the page-size, as there is an internal
135587 db->nextPagesize = sqlite3Atoi(zRight);
135588 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
135604 Btree *pBt = pDb->pBt;
135605 int b = -1;
135614 if( pId2->n==0 && b>=0 ){
135616 for(ii=0; ii<db->nDb; ii++){
135617 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
135646 iReg = ++pParse->nMem;
135670 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
135673 ** the locking-mode of the main database).
135675 eMode = db->dfltLockMode;
135678 if( pId2->n==0 ){
135680 ** of the PRAGMA command. In this case the locking-mode must be
135688 assert(pDb==&db->aDb[0]);
135689 for(ii=2; ii<db->nDb; ii++){
135690 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
135693 db->dfltLockMode = (u8)eMode;
135695 pPager = sqlite3BtreePager(pDb->pBt);
135732 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
135733 /* Do not allow journal-mode "OFF" in defensive since the database
135738 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
135741 pId2->n = 1;
135743 for(ii=db->nDb-1; ii>=0; ii--){
135744 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
135760 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135761 i64 iLimit = -2;
135764 if( iLimit<-1 ) iLimit = -1;
135777 ** Get or set the value of the database 'auto-vacuum' parameter.
135782 Btree *pBt = pDb->pBt;
135789 db->nextAutovac = (u8)eAuto;
135791 ** incr-vacuum flags. This is required in case this connection
135793 ** as an auto-vacuum capable db.
135800 ** that this really is an auto-vacuum capable database.
135819 aOp[4].p3 = eAuto - 1;
135842 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
135858 ** number of pages is adjusted so that the cache uses -N kibibytes
135864 returnSingleInt(v, pDb->pSchema->cache_size);
135867 pDb->pSchema->cache_size = size;
135868 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
135885 ** number of pages is adjusted so that the cache uses -N kibibytes
135899 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
135900 sqlite3BtreeSetSpillSize(pDb->pBt,0));
135904 sqlite3BtreeSetSpillSize(pDb->pBt, size);
135907 db->flags |= SQLITE_CacheSpill;
135909 db->flags &= ~(u64)SQLITE_CacheSpill;
135938 if( pId2->n==0 ) db->szMmap = sz;
135939 for(ii=db->nDb-1; ii>=0; ii--){
135940 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
135941 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
135945 sz = -1;
135954 pParse->nErr++;
135955 pParse->rc = rc;
135968 ** Note that it is possible for the library compile-time options to
135973 returnSingleInt(v, db->temp_store);
135998 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
136006 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
136007 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
136045 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
136076 Pager *pPager = sqlite3BtreePager(pDb->pBt);
136083 Pager *pPager = sqlite3BtreePager(pDb->pBt);
136113 returnSingleInt(v, pDb->safety_level-1);
136115 if( !db->autoCommit ){
136121 pDb->safety_level = iLevel;
136122 pDb->bSyncSet = 1;
136134 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
136136 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
136137 if( db->autoCommit==0 ){
136139 ** in auto-commit mode. */
136143 if( db->auth.authLevel==UAUTH_User ){
136144 /* Do not allow non-admin users to modify the schema arbitrarily */
136150 db->flags |= mask;
136152 db->flags &= ~mask;
136153 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
136157 /* IMP: R-60817-01178 If the argument is "RESET" then schema
136164 /* Many of the flag-pragmas modify the code generated by the SQL
136187 ** pk: Non-zero for PK fields.
136198 pParse->nMem = 7;
136200 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
136203 if( pCol->colFlags & COLFLAG_NOINSERT ){
136204 if( pPragma->iArg==0 ){
136208 if( pCol->colFlags & COLFLAG_VIRTUAL ){
136210 }else if( pCol->colFlags & COLFLAG_STORED ){
136212 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
136216 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
136221 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
136224 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
136227 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
136228 i-nHidden,
136229 pCol->zCnName,
136231 pCol->notNull ? 1 : 0,
136232 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
136255 pParse->nMem = 6;
136257 for(ii=0; ii<db->nDb; ii++){
136261 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
136268 pHash = &db->aDb[ii].pSchema->tblHash;
136270 while( initNCol-- ){
136275 if( pTab->nCol==0 ){
136276 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
136279 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
136283 if( db->mallocFailed ){
136284 sqlite3ErrorMsg(db->pParse, "out of memory");
136285 db->pParse->rc = SQLITE_NOMEM_BKPT;
136287 pHash = &db->aDb[ii].pSchema->tblHash;
136296 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
136301 }else if( pTab->tabFlags & TF_Shadow ){
136307 db->aDb[ii].zDbSName,
136308 sqlite3PreferredTableName(pTab->zName),
136310 pTab->nCol,
136311 (pTab->tabFlags & TF_WithoutRowid)!=0,
136312 (pTab->tabFlags & TF_Strict)!=0
136323 pParse->nMem = 5;
136325 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
136328 sqlite3PreferredTableName(pTab->zName),
136330 pTab->szTabRow,
136331 pTab->nRowLogEst,
136332 pTab->tabFlags);
136333 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136335 pIdx->zName,
136336 pIdx->szIdxRow,
136337 pIdx->aiRowLogEst[0],
136338 pIdx->hasStat1);
136360 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
136363 if( pPragma->iArg ){
136365 mx = pIdx->nColumn;
136366 pParse->nMem = 6;
136369 mx = pIdx->nKeyCol;
136370 pParse->nMem = 3;
136372 pTab = pIdx->pTable;
136374 assert( pParse->nMem<=pPragma->nPragCName );
136376 i16 cnum = pIdx->aiColumn[i];
136378 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
136379 if( pPragma->iArg ){
136381 pIdx->aSortOrder[i],
136382 pIdx->azColl[i],
136383 i<pIdx->nKeyCol);
136385 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
136397 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136398 pParse->nMem = 5;
136400 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
136404 pIdx->zName,
136406 azOrigin[pIdx->idxType],
136407 pIdx->pPartIdxWhere!=0);
136415 pParse->nMem = 3;
136416 for(i=0; i<db->nDb; i++){
136417 if( db->aDb[i].pBt==0 ) continue;
136418 assert( db->aDb[i].zDbSName!=0 );
136421 db->aDb[i].zDbSName,
136422 sqlite3BtreeGetFilename(db->aDb[i].pBt));
136430 pParse->nMem = 2;
136431 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
136433 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
136443 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
136444 pParse->nMem = 6;
136446 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
136447 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
136451 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
136453 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
136462 pParse->nMem = 1;
136463 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
136465 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
136488 pFK = pTab->u.tab.pFKey;
136490 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136492 pParse->nMem = 8;
136496 for(j=0; j<pFK->nCol; j++){
136500 pFK->zTo,
136501 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
136502 pFK->aCol[j].zCol,
136503 actionName(pFK->aAction[1]), /* ON UPDATE */
136504 actionName(pFK->aAction[0]), /* ON DELETE */
136508 pFK = pFK->pNextFrom;
136533 regResult = pParse->nMem+1;
136534 pParse->nMem += 4;
136535 regRow = ++pParse->nMem;
136536 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
136545 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
136546 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136547 zDb = db->aDb[iDb].zDbSName;
136549 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
136550 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
136552 sqlite3VdbeLoadString(v, regResult, pTab->zName);
136554 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
136555 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
136558 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
136564 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
136572 assert( pParse->nErr>0 || pFK==0 );
136574 if( pParse->nTab<i ) pParse->nTab = i;
136577 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
136578 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
136583 assert( x==0 || db->mallocFailed );
136591 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
136592 for(j=0; j<pFK->nCol; j++){
136593 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
136601 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
136602 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
136603 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
136609 assert( pFK->nCol==1 || db->mallocFailed );
136618 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
136657 ** without the overhead of cross-checking indexes. Quick_check
136685 ** to -1 here, to indicate that the VDBE should verify the integrity
136688 assert( iDb==0 || pId2->z );
136689 if( pId2->z==0 ) iDb = -1;
136692 pParse->nMem = 6;
136703 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
136706 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
136709 for(i=0; i<db->nDb; i++){
136720 pParse->okConstFactor = 0; /* tag-20230327-1 */
136722 /* Do an integrity check of the B-Tree
136728 pTbls = &db->aDb[i].pSchema->tblHash;
136735 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
136748 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
136749 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136750 aRoot[++cnt] = pIdx->tnum;
136759 /* Do the b-tree integrity checks */
136764 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
136778 int r1 = -1;
136781 int mxCol; /* Maximum non-virtual column number */
136790 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
136791 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
136796 ** reg[8+i] counts the number of entries in the i-th index
136799 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136802 assert( pParse->nMem>=8+j );
136807 /* Fetch the right-most column from the table. This will cause
136814 mxCol = -1;
136815 for(j=0; j<pTab->nCol; j++){
136816 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
136818 if( mxCol==pTab->iPKey ) mxCol--;
136821 ** PK index column-count, so there is no need to account for them
136823 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
136835 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
136840 pTab->zName);
136845 for(j=0; j<pPk->nKeyCol; j++){
136853 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
136854 ** (3) Datatype for TEXT columns in non-STRICT tables must be
136856 ** (4) Datatype for numeric columns in non-STRICT tables must not
136859 bStrict = (pTab->tabFlags & TF_Strict)!=0;
136860 for(j=0; j<pTab->nCol; j++){
136862 Column *pCol = pTab->aCol + j; /* The column to be checked */
136868 if( j==pTab->iPKey ) continue;
136870 doTypeCheck = pCol->eCType>COLTYPE_ANY;
136872 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
136874 if( pCol->notNull==0 && !doTypeCheck ) continue;
136878 if( pCol->colFlags & COLFLAG_VIRTUAL ){
136880 p1 = -1;
136883 if( pCol->iDflt ){
136886 pCol->affinity, &pDfltValue);
136904 if( pCol->notNull ){
136922 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
136923 pCol->zCnName);
136934 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
136944 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
136945 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
136947 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
136948 sqlite3StdType[pCol->eCType-1],
136949 pTab->zName, pTab->aCol[j].zCnName);
136951 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
136952 /* (3) Datatype for TEXT columns in non-STRICT tables must be
136958 pTab->zName, pTab->aCol[j].zCnName);
136960 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
136961 /* (4) Datatype for numeric columns in non-STRICT tables must not
136970 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
136974 pTab->zName, pTab->aCol[j].zCnName);
136982 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
136983 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
136984 if( db->mallocFailed==0 ){
136989 pParse->iSelfTab = iDataCur + 1;
136990 for(k=pCheck->nExpr-1; k>0; k--){
136991 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
136993 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
136996 pParse->iSelfTab = 0;
136998 pTab->zName);
137007 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
137018 pIdx->nColumn); VdbeCoverage(v);
137023 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
137035 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
137038 "rowid not at end-of-record for row ");
137041 sqlite3VdbeGoto(v, jmp5-1);
137045 /* Any indexed columns with non-BINARY collations must still hold
137048 for(kk=0; kk<pIdx->nKeyCol; kk++){
137049 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
137060 sqlite3VdbeGoto(v, jmp5-1);
137070 for(kk=0; kk<pIdx->nKeyCol; kk++){
137071 int iCol = pIdx->aiColumn[kk];
137072 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
137073 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
137081 pIdx->nKeyCol); VdbeCoverage(v);
137082 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
137091 sqlite3VdbeJumpHere(v, loopTop-1);
137094 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
137099 sqlite3VdbeLoadString(v, 4, pIdx->zName);
137105 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
137125 aOp[0].p2 = 1-mxErr;
137131 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
137140 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
137145 ** The second form of this pragma is a no-op if the main database file
137166 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
137167 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
137168 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
137171 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
137181 returnSingleText(v, encnames[ENC(pParse->db)].zName);
137188 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
137189 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
137190 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
137191 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
137197 if( !pEnc->zName ){
137222 ** the value of the schema-version and user-version, respectively. Both
137223 ** the schema-version and the user-version are 32-bit signed integers
137226 ** The schema-cookie is usually only manipulated internally by SQLite. It
137233 ** the schema-version is potentially dangerous and may lead to program
137236 ** The user-version is not used internally by SQLite. It may be used by
137240 int iCookie = pPragma->iArg; /* Which cookie to read or write */
137242 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
137257 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
137259 ** mode. Change the OP_SetCookie opcode into a no-op. */
137286 ** Return the names of all compile-time options used in this build,
137292 pParse->nMem = 1;
137309 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
137320 pParse->nMem = 3;
137339 db->xWalCallback==sqlite3WalDefaultHook ?
137340 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
137348 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
137390 ** ever added that should be off by default, those off-by-default
137400 ** (2) The query planner used sqlite_stat1-style statistics for one or
137428 iTabCur = pParse->nTab++;
137429 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
137432 pSchema = db->aDb[iDb].pSchema;
137433 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
137439 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
137442 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
137443 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
137444 if( !pIdx->hasStat1 ){
137456 db->aDb[iDb].zDbSName, pTab->zName);
137480 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
137484 returnSingleInt(v, db->busyTimeout);
137492 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
137494 ** specified and is a non-negative integer.
137495 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
137497 ** sqlite3_soft_heap_limit64(-1) C-language function.
137504 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
137515 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
137522 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
137525 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
137544 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
137558 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
137561 db->nAnalysisLimit = (int)(N&0x7fffffff);
137563 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
137576 pParse->nMem = 2;
137577 for(i=0; i<db->nDb; i++){
137581 if( db->aDb[i].zDbSName==0 ) continue;
137582 pBt = db->aDb[i].pBt;
137585 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
137589 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
137597 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
137606 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
137611 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
137662 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
137667 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
137671 if( pPragma->mPragFlg & PragFlg_Result1 ){
137675 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
137681 assert( strlen(zBuf) < sizeof(zBuf)-1 );
137689 pTab->pName = pPragma;
137690 pTab->db = db;
137691 pTab->iHidden = i;
137692 pTab->nHidden = j;
137724 pIdxInfo->estimatedCost = (double)1;
137725 if( pTab->nHidden==0 ){ return SQLITE_OK; }
137726 pConstraint = pIdxInfo->aConstraint;
137729 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
137730 if( pConstraint->usable==0 ) continue;
137731 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
137732 if( pConstraint->iColumn < pTab->iHidden ) continue;
137733 j = pConstraint->iColumn - pTab->iHidden;
137738 pIdxInfo->estimatedCost = (double)2147483647;
137739 pIdxInfo->estimatedRows = 2147483647;
137742 j = seen[0]-1;
137743 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
137744 pIdxInfo->aConstraintUsage[j].omit = 1;
137746 pIdxInfo->estimatedCost = (double)20;
137747 pIdxInfo->estimatedRows = 20;
137748 j = seen[1]-1;
137749 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
137750 pIdxInfo->aConstraintUsage[j].omit = 1;
137760 pCsr->base.pVtab = pVtab;
137761 *ppCursor = &pCsr->base;
137768 sqlite3_finalize(pCsr->pPragma);
137769 pCsr->pPragma = 0;
137770 for(i=0; i<ArraySize(pCsr->azArg); i++){
137771 sqlite3_free(pCsr->azArg[i]);
137772 pCsr->azArg[i] = 0;
137790 pCsr->iRowid++;
137791 assert( pCsr->pPragma );
137792 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
137793 rc = sqlite3_finalize(pCsr->pPragma);
137794 pCsr->pPragma = 0;
137809 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137818 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
137821 assert( j<ArraySize(pCsr->azArg) );
137822 assert( pCsr->azArg[j]==0 );
137824 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
137825 if( pCsr->azArg[j]==0 ){
137830 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
137832 if( pCsr->azArg[1] ){
137833 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
137835 sqlite3_str_appendall(&acc, pTab->pName->zName);
137836 if( pCsr->azArg[0] ){
137837 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
137841 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
137844 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
137855 return (pCsr->pPragma==0);
137867 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137868 if( i<pTab->iHidden ){
137869 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
137871 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
137881 *p = pCsr->iRowid;
137888 0, /* xCreate - create a table */
137889 pragmaVtabConnect, /* xConnect - connect to an existing table */
137890 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
137891 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
137892 0, /* xDestroy - Drop a table */
137893 pragmaVtabOpen, /* xOpen - open a cursor */
137894 pragmaVtabClose, /* xClose - close a cursor */
137895 pragmaVtabFilter, /* xFilter - configure scan constraints */
137896 pragmaVtabNext, /* xNext - advance a cursor */
137898 pragmaVtabColumn, /* xColumn - read data */
137899 pragmaVtabRowid, /* xRowid - read data */
137900 0, /* xUpdate - write data */
137901 0, /* xBegin - begin transaction */
137902 0, /* xSync - sync transaction */
137903 0, /* xCommit - commit transaction */
137904 0, /* xRollback - rollback transaction */
137905 0, /* xFindFunction - function overloading */
137906 0, /* xRename - rename the table */
137923 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
137924 assert( sqlite3HashFind(&db->aModule, zName)==0 );
137960 sqlite3 *db = pData->db;
137961 if( db->mallocFailed ){
137962 pData->rc = SQLITE_NOMEM_BKPT;
137963 }else if( pData->pzErrMsg[0]!=0 ){
137965 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
137971 *pData->pzErrMsg = sqlite3MPrintf(db,
137973 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
137976 pData->rc = SQLITE_ERROR;
137977 }else if( db->flags & SQLITE_WriteSchema ){
137978 pData->rc = SQLITE_CORRUPT_BKPT;
137983 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
137984 *pData->pzErrMsg = z;
137985 pData->rc = SQLITE_CORRUPT_BKPT;
137996 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
137997 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
138005 const char *zSql, /* UTF-8 encoded SQL statement. */
138030 sqlite3 *db = pData->db;
138031 int iDb = pData->iDb;
138035 assert( sqlite3_mutex_held(db->mutex) );
138036 db->mDbFlags |= DBFLAG_EncodingFixed;
138038 pData->nInitRow++;
138039 if( db->mallocFailed ){
138044 assert( iDb>=0 && iDb<db->nDb );
138051 ** But because db->init.busy is set to 1, no VDBE code is generated
138061 u8 saved_iDb = db->init.iDb;
138065 assert( db->init.busy );
138066 db->init.iDb = iDb;
138067 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
138068 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
138074 db->init.orphanTrigger = 0;
138075 db->init.azInit = (const char**)argv;
138077 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
138078 rc = db->errCode;
138080 db->init.iDb = saved_iDb;
138081 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
138083 if( db->init.orphanTrigger ){
138086 if( rc > pData->rc ) pData->rc = rc;
138094 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
138106 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
138110 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
138111 || pIndex->tnum<2
138112 || pIndex->tnum>pData->mxPage
138143 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
138145 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
138146 assert( iDb>=0 && iDb<db->nDb );
138147 assert( db->aDb[iDb].pSchema );
138148 assert( sqlite3_mutex_held(db->mutex) );
138149 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
138151 db->init.busy = 1;
138153 /* Construct the in-memory representation schema tables (sqlite_schema or
138157 ** the schema table as read-only. */
138173 db->mDbFlags &= mask;
138181 pDb = &db->aDb[iDb];
138182 if( pDb->pBt==0 ){
138189 /* If there is not already a read-only (or read-write) transaction opened
138190 ** on the b-tree database, open one now. If a transaction is opened, it
138192 sqlite3BtreeEnter(pDb->pBt);
138193 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
138194 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
138209 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
138220 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
138222 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
138225 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
138227 /* If opening a non-empty database, check the text encoding. For the
138232 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
138233 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
138237 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
138242 if( db->nVdbeActive>0 && encoding!=ENC(db)
138243 && (db->mDbFlags & DBFLAG_Vacuum)==0
138252 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
138260 pDb->pSchema->enc = ENC(db);
138262 if( pDb->pSchema->cache_size==0 ){
138264 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
138266 pDb->pSchema->cache_size = size;
138268 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
138270 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
138276 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
138279 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
138280 if( pDb->pSchema->file_format==0 ){
138281 pDb->pSchema->file_format = 1;
138283 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
138294 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
138295 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
138300 assert( db->init.busy );
138301 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
138306 db->aDb[iDb].zDbSName, zSchemaTabName);
138310 xAuth = db->xAuth;
138311 db->xAuth = 0;
138315 db->xAuth = xAuth;
138326 assert( pDb == &(db->aDb[iDb]) );
138327 if( db->mallocFailed ){
138330 pDb = &db->aDb[iDb];
138332 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
138353 sqlite3BtreeCommit(pDb->pBt);
138355 sqlite3BtreeLeave(pDb->pBt);
138364 db->init.busy = 0;
138369 ** Initialize all database files - the main database file, the file
138379 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
138381 assert( sqlite3_mutex_held(db->mutex) );
138382 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
138383 assert( db->init.busy==0 );
138385 assert( db->nDb>0 );
138392 for(i=db->nDb-1; i>0; i--){
138393 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
138406 ** This routine is a no-op if the database schema is already initialized.
138411 sqlite3 *db = pParse->db;
138412 assert( sqlite3_mutex_held(db->mutex) );
138413 if( !db->init.busy ){
138414 rc = sqlite3Init(db, &pParse->zErrMsg);
138416 pParse->rc = rc;
138417 pParse->nErr++;
138418 }else if( db->noSharedCache ){
138419 db->mDbFlags |= DBFLAG_SchemaKnownOk;
138428 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
138429 ** make no changes to pParse->rc.
138432 sqlite3 *db = pParse->db;
138437 assert( pParse->checkSchema );
138438 assert( sqlite3_mutex_held(db->mutex) );
138439 for(iDb=0; iDb<db->nDb; iDb++){
138441 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
138444 /* If there is not already a read-only (or read-write) transaction opened
138445 ** on the b-tree database, open one now. If a transaction is opened, it
138446 ** will be closed immediately after reading the meta-value. */
138451 pParse->rc = SQLITE_NOMEM;
138458 ** value stored as part of the in-memory schema representation,
138462 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
138463 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
138476 ** which database file in db->aDb[] the schema refers to.
138482 int i = -32768;
138484 /* If pSchema is NULL, then return -32768. This happens when code in
138486 ** created by a sub-select). In this case the return value of this
138489 ** We return -32768 instead of the more usual -1 simply because using
138490 ** -32768 as the incorrect index into db->aDb[] is much
138491 ** more likely to cause a segfault than -1 (of course there are assert()
138493 ** -32768 will still fit into a 16-bit signed integer.
138495 assert( sqlite3_mutex_held(db->mutex) );
138498 assert( i<db->nDb );
138499 if( db->aDb[i].pSchema==pSchema ){
138503 assert( i>=0 && i<db->nDb );
138512 sqlite3 *db = pParse->db;
138514 assert( db->pParse==pParse );
138515 assert( pParse->nested==0 );
138517 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
138519 while( pParse->pCleanup ){
138520 ParseCleanup *pCleanup = pParse->pCleanup;
138521 pParse->pCleanup = pCleanup->pNext;
138522 pCleanup->xCleanup(db, pCleanup->pPtr);
138525 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
138526 if( pParse->pConstExpr ){
138527 sqlite3ExprListDelete(db, pParse->pConstExpr);
138529 assert( db->lookaside.bDisable >= pParse->disableLookaside );
138530 db->lookaside.bDisable -= pParse->disableLookaside;
138531 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
138532 assert( pParse->db->pParse==pParse );
138533 db->pParse = pParse->pOuterParse;
138534 pParse->db = 0;
138535 pParse->disableLookaside = 0;
138546 ** common cleanups, we save a single NULL-pointer comparison in
138551 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
138553 ** use-after-free errors following an OOM. The preferred way to do this is
138556 ** testcase( pParse->earlyCleanup );
138571 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
138573 pCleanup->pNext = pParse->pCleanup;
138574 pParse->pCleanup = pCleanup;
138575 pCleanup->pPtr = pPtr;
138576 pCleanup->xCleanup = xCleanup;
138578 xCleanup(pParse->db, pPtr);
138581 pParse->earlyCleanup = 1;
138599 assert( db->pParse!=pParse );
138600 pParse->pOuterParse = db->pParse;
138601 db->pParse = pParse;
138602 pParse->db = db;
138603 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
138615 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
138619 const char *zSql, /* UTF-8 encoded SQL statement. */
138633 sParse.pOuterParse = db->pParse;
138634 db->pParse = &sParse;
138638 if( db->mallocFailed ){
138640 db->errCode = rc = SQLITE_NOMEM;
138643 assert( sqlite3_mutex_held(db->mutex) );
138645 /* For a long-term use prepared statement avoid the use of
138656 ** some other database connection is holding a write-lock, which in
138677 if( !db->noSharedCache ){
138678 for(i=0; i<db->nDb; i++) {
138679 Btree *pBt = db->aDb[i].pBt;
138684 const char *zDb = db->aDb[i].zDbSName;
138686 testcase( db->flags & SQLITE_ReadUncommit );
138694 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
138697 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
138699 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138710 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
138724 if( db->init.busy==0 ){
138725 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
138727 if( db->mallocFailed ){
138732 if( sParse.checkSchema && db->init.busy==0 ){
138757 sParse.pTriggerPrg = pT->pNext;
138768 const char *zSql, /* UTF-8 encoded SQL statement. */
138785 sqlite3_mutex_enter(db->mutex);
138793 if( rc==SQLITE_OK || db->mallocFailed ) break;
138795 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
138798 assert( (rc&db->errMask)==rc );
138799 db->busyHandler.nBusy = 0;
138800 sqlite3_mutex_leave(db->mutex);
138820 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
138824 assert( sqlite3_mutex_held(db->mutex) );
138826 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
138854 const char *zSql, /* UTF-8 encoded SQL statement. */
138866 const char *zSql, /* UTF-8 encoded SQL statement. */
138872 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
138884 const char *zSql, /* UTF-8 encoded SQL statement. */
138891 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
138908 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
138912 const void *zSql, /* UTF-16 encoded SQL statement. */
138918 /* This function currently works by first transforming the UTF-16
138919 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
138939 sqlite3_mutex_enter(db->mutex);
138942 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
138947 ** equivalent pointer into the UTF-16 string by counting the unicode
138949 ** the same number of characters into the UTF-16 string.
138951 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
138956 sqlite3_mutex_leave(db->mutex);
138970 const void *zSql, /* UTF-16 encoded SQL statement. */
138982 const void *zSql, /* UTF-16 encoded SQL statement. */
138994 const void *zSql, /* UTF-16 encoded SQL statement. */
139045 ** The aDefer[] array is used by the sorter-references optimization. For
139055 ** When the sorter-reference optimization is used, there is one entry in the
139064 int regReturn; /* Register holding block-output return address */
139065 int labelBkOut; /* Start label for the block-output subroutine */
139096 Select *pPrior = p->pPrior;
139097 sqlite3ExprListDelete(db, p->pEList);
139098 sqlite3SrcListDelete(db, p->pSrc);
139099 sqlite3ExprDelete(db, p->pWhere);
139100 sqlite3ExprListDelete(db, p->pGroupBy);
139101 sqlite3ExprDelete(db, p->pHaving);
139102 sqlite3ExprListDelete(db, p->pOrderBy);
139103 sqlite3ExprDelete(db, p->pLimit);
139104 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
139106 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
139107 sqlite3WindowListDelete(db, p->pWinDefn);
139109 while( p->pWin ){
139110 assert( p->pWin->ppThis==&p->pWin );
139111 sqlite3WindowUnlinkFromSelect(p->pWin);
139124 pDest->eDest = (u8)eDest;
139125 pDest->iSDParm = iParm;
139126 pDest->iSDParm2 = 0;
139127 pDest->zAffSdst = 0;
139128 pDest->iSdst = 0;
139129 pDest->nSdst = 0;
139140 SrcList *pSrc, /* the FROM clause -- which tables to scan */
139150 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
139152 assert( pParse->db->mallocFailed );
139157 sqlite3Expr(pParse->db,TK_ASTERISK,0));
139159 pNew->pEList = pEList;
139160 pNew->op = TK_SELECT;
139161 pNew->selFlags = selFlags;
139162 pNew->iLimit = 0;
139163 pNew->iOffset = 0;
139164 pNew->selId = ++pParse->nSelect;
139165 pNew->addrOpenEphm[0] = -1;
139166 pNew->addrOpenEphm[1] = -1;
139167 pNew->nSelectRow = 0;
139168 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
139169 pNew->pSrc = pSrc;
139170 pNew->pWhere = pWhere;
139171 pNew->pGroupBy = pGroupBy;
139172 pNew->pHaving = pHaving;
139173 pNew->pOrderBy = pOrderBy;
139174 pNew->pPrior = 0;
139175 pNew->pNext = 0;
139176 pNew->pLimit = pLimit;
139177 pNew->pWith = 0;
139179 pNew->pWin = 0;
139180 pNew->pWinDefn = 0;
139182 if( pParse->db->mallocFailed ) {
139183 clearSelect(pParse->db, pNew, pNew!=&standin);
139186 assert( pNew->pSrc!=0 || pParse->nErr>0 );
139200 ** Return a pointer to the right-most SELECT statement in a compound.
139203 while( p->pNext ) p = p->pNext;
139228 ** ------- ----- ----- ------------
139229 ** CROSS - - JT_CROSS
139230 ** INNER - - JT_INNER
139231 ** LEFT - - JT_LEFT|JT_OUTER
139232 ** LEFT OUTER - JT_LEFT|JT_OUTER
139233 ** RIGHT - - JT_RIGHT|JT_OUTER
139234 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
139235 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
139236 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
139237 ** NATURAL INNER - JT_NATURAL|JT_INNER
139238 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
139240 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
139242 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
139246 ** of other non-standard and in many cases non-sensical join types.
139251 ** INNER CROSS JOIN -> same as JOIN
139252 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
139253 ** OUTER LEFT JOIN -> same as LEFT JOIN
139254 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
139255 ** LEFT RIGHT JOIN -> same as FULL JOIN
139256 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
139257 ** CROSS CROSS CROSS JOIN -> same as JOIN
139296 if( p->n==aKeyword[j].nChar
139297 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
139325 ** Return the index of a column in a table. Return -1 if the column
139332 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
139333 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
139335 return -1;
139343 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
139344 if( pItem->fg.isNestedFrom ){
139346 assert( pItem->pSelect!=0 );
139347 pResults = pItem->pSelect->pEList;
139349 assert( iCol>=0 && iCol<pResults->nExpr );
139350 pResults->a[iCol].fg.bUsed = 1;
139356 ** table that has a column named zCol. The search is left-to-right.
139366 int iStart, /* First member of pSrc->a[] to check */
139367 int iEnd, /* Last member of pSrc->a[] to check */
139369 int *piTab, /* Write index of pSrc->a[] here */
139370 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
139376 assert( iEnd<pSrc->nSrc );
139381 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
139383 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
139386 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
139428 p->w.iJoin = iTable;
139429 if( p->op==TK_FUNCTION ){
139431 if( p->x.pList ){
139433 for(i=0; i<p->x.pList->nExpr; i++){
139434 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
139438 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
139439 p = p->pRight;
139459 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
139463 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
139466 if( p->op==TK_FUNCTION ){
139468 if( p->x.pList ){
139470 for(i=0; i<p->x.pList->nExpr; i++){
139471 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
139475 unsetJoinExpr(p->pLeft, iTable, nullable);
139476 p = p->pRight;
139493 ** The left most table is the first entry in Select.pSrc. The right-most
139507 pSrc = p->pSrc;
139508 pLeft = &pSrc->a[0];
139510 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
139511 Table *pRightTab = pRight->pTab;
139514 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
139515 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
139520 if( pRight->fg.jointype & JT_NATURAL ){
139522 if( pRight->fg.isUsing || pRight->u3.pOn ){
139527 for(j=0; j<pRightTab->nCol; j++){
139530 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
139531 zName = pRightTab->aCol[j].zCnName;
139535 assert( pUsing->nId>0 );
139536 assert( pUsing->a[pUsing->nId-1].zName==0 );
139537 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
139542 pRight->fg.isUsing = 1;
139543 pRight->fg.isSynthUsing = 1;
139544 pRight->u3.pUsing = pUsing;
139546 if( pParse->nErr ) return 1;
139556 if( pRight->fg.isUsing ){
139557 IdList *pList = pRight->u3.pUsing;
139558 sqlite3 *db = pParse->db;
139560 for(j=0; j<pList->nId; j++){
139569 zName = pList->a[j].zName;
139573 pRight->fg.isSynthUsing)==0
139575 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
139580 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
139581 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139584 ** contains the zName column, then this branch is a no-op.
139591 ** non-USING references to zName on the left of an INNER or LEFT
139598 pRight->fg.isSynthUsing)!=0 ){
139599 if( pSrc->a[iLeft].fg.isUsing==0
139600 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
139608 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
139623 pEq->w.iJoin = pE2->iTable;
139625 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
139632 else if( pRight->u3.pOn ){
139633 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
139634 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
139635 pRight->u3.pOn = 0;
139636 pRight->fg.isOn = 1;
139665 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
139666 0, pInfo->ecelFlags);
139668 if( pInfo->pExtra ){
139669 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
139670 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
139688 int nOBSat = pSort->nOBSat;
139689 Vdbe *v = pParse->pVdbe;
139690 int regOut = ++pParse->nMem;
139691 if( pSort->pDeferredRowLoad ){
139692 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
139694 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
139700 ** through regData+nData-1 onto the sorter.
139711 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
139712 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
139713 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
139717 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
139740 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
139745 regBase = regData - nPrefixReg;
139747 regBase = pParse->nMem + 1;
139748 pParse->nMem += nBase;
139750 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
139751 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
139752 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
139753 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
139756 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
139770 regPrevKey = pParse->nMem+1;
139771 pParse->nMem += pSort->nOBSat;
139772 nKey = nExpr - pSort->nOBSat + bSeq;
139776 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
139779 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
139780 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
139781 if( pParse->db->mallocFailed ) return;
139782 pOp->p2 = nKey + nData;
139783 pKI = pOp->p4.pKeyInfo;
139784 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
139785 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
139786 testcase( pKI->nAllField > pKI->nKeyField+2 );
139787 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
139788 pKI->nAllField-pKI->nKeyField-1);
139792 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
139793 pSort->regReturn = ++pParse->nMem;
139794 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
139795 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
139797 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
139801 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
139815 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
139820 int iCsr = pSort->iECursor;
139825 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
139832 if( pSort->sortFlags & SORTFLAG_UseSorter ){
139837 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
139838 regBase+nOBSat, nBase-nOBSat);
139841 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
139844 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
139914 int nResultCol = pEList->nExpr;
139915 Vdbe *v = pParse->pVdbe;
139924 iRet = regPrev = pParse->nMem+1;
139925 pParse->nMem += nResultCol;
139929 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
139930 if( i<nResultCol-1 ){
139937 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
139940 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
139941 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
139976 ** No adjustments necessary. This function is a no-op.
139995 if( pParse->nErr==0
139998 Vdbe *v = pParse->pVdbe;
140000 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
140009 pOp->opcode = OP_Null;
140010 pOp->p1 = 1;
140011 pOp->p2 = iVal;
140018 ** This function is called as part of inner-loop generation for a SELECT
140020 ** determines the expressions, if any, that the sorter-reference
140021 ** optimization should be used for. The sorter-reference optimization
140034 ** for which the sorter-reference optimization should be enabled.
140035 ** Additionally, the pSort->aDefer[] array is populated with entries
140050 for(i=0; i<pEList->nExpr; i++){
140051 struct ExprList_item *pItem = &pEList->a[i];
140052 if( pItem->u.x.iOrderByCol==0 ){
140053 Expr *pExpr = pItem->pExpr;
140055 if( pExpr->op==TK_COLUMN
140056 && pExpr->iColumn>=0
140058 && (pTab = pExpr->y.pTab)!=0
140060 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
140064 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
140067 if( nDefer==ArraySize(pSort->aDefer) ){
140075 nKey = pPk->nKeyCol;
140080 pNew->iTable = pExpr->iTable;
140082 pNew->y.pTab = pExpr->y.pTab;
140083 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
140087 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
140088 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
140089 pSort->aDefer[nDefer].nKey = nKey;
140093 pItem->fg.bSorterRef = 1;
140097 pSort->nDefer = (u8)nDefer;
140106 ** If srcTab is negative, then the p->pEList expressions
140108 ** zero or more, then data is pulled from srcTab and p->pEList is used only
140114 int srcTab, /* Pull data from this table if non-negative */
140121 Vdbe *v = pParse->pVdbe;
140124 int eDest = pDest->eDest; /* How to dispose of results */
140125 int iParm = pDest->iSDParm; /* First argument to disposal method */
140133 ** values for any expressions that are also part of the sort-key are omitted
140139 assert( p->pEList!=0 );
140140 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
140141 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
140144 codeOffset(v, p->iOffset, iContinue);
140149 nResultCol = p->pEList->nExpr;
140151 if( pDest->iSdst==0 ){
140153 nPrefixReg = pSort->pOrderBy->nExpr;
140154 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
140155 pParse->nMem += nPrefixReg;
140157 pDest->iSdst = pParse->nMem+1;
140158 pParse->nMem += nResultCol;
140159 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
140161 ** on the right-hand side of an INSERT contains more result columns than
140165 pParse->nMem += nResultCol;
140167 pDest->nSdst = nResultCol;
140168 regOrig = regResult = pDest->iSdst;
140172 VdbeComment((v, "%s", p->pEList->a[i].zEName));
140189 /* For each expression in p->pEList that is a copy of an expression in
140190 ** the ORDER BY clause (pSort->pOrderBy), set the associated
140192 ** expression within the sort-key that pushOntoSorter() will generate.
140193 ** This allows the p->pEList field to be omitted from the sorted record,
140197 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
140199 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
140200 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
140204 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
140205 if( pExtra && pParse->db->mallocFailed==0 ){
140211 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
140212 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
140213 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
140214 pParse->nMem += pExtra->nExpr;
140220 pEList = p->pEList;
140221 for(i=0; i<pEList->nExpr; i++){
140222 if( pEList->a[i].u.x.iOrderByCol>0
140224 || pEList->a[i].fg.bSorterRef
140227 nResultCol--;
140246 if( pExtra ) nResultCol += pExtra->nExpr;
140248 if( p->iLimit
140254 pSort->pDeferredRowLoad = &sRowLoadInfo;
140266 int eType = pDistinct->eTnctType;
140267 int iTab = pDistinct->tabTnct;
140268 assert( nResultCol==p->pEList->nExpr );
140269 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
140270 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
140272 codeOffset(v, p->iOffset, iContinue);
140345 int i2 = pDest->iSDParm2;
140354 regResult+(i2<0), nResultCol-(i2<0), r1);
140379 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
140381 r1, pDest->zAffSdst, nResultCol);
140403 assert( nResultCol<=pDest->nSdst );
140407 assert( nResultCol==pDest->nSdst );
140415 case SRT_Coroutine: /* Send data to a co-routine */
140423 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
140432 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
140433 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
140434 ** pSO->nExpr columns, then make sure all keys are unique by adding a
140443 pSO = pDest->pOrderBy;
140445 nKey = pSO->nExpr;
140464 regResult + pSO->a[i].u.x.iOrderByCol - 1,
140482 ** user-defined functions that have side effects. We do not care
140496 if( pSort==0 && p->iLimit ){
140497 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
140506 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
140509 p->aSortFlags = (u8*)&p->aColl[N+X];
140510 p->nKeyField = (u16)N;
140511 p->nAllField = (u16)(N+X);
140512 p->enc = ENC(db);
140513 p->db = db;
140514 p->nRef = 1;
140527 assert( p->db!=0 );
140528 assert( p->nRef>0 );
140529 p->nRef--;
140530 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
140539 assert( p->nRef>0 );
140540 p->nRef++;
140552 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
140578 sqlite3 *db = pParse->db;
140581 nExpr = pList->nExpr;
140582 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
140585 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
140586 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
140587 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
140610 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
140613 ** "USE TEMP B-TREE FOR xxx"
140619 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
140623 ** Assign expression b to lvalue a. A second, no-op, version of this macro
140632 /* No-op versions of the explainXXX() functions and macros. */
140639 ** If the inner loop was generated using a non-null pOrderBy argument,
140651 Vdbe *v = pParse->pVdbe; /* The prepared statement */
140652 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
140657 ExprList *pOrderBy = pSort->pOrderBy;
140658 int eDest = pDest->eDest;
140659 int iParm = pDest->iSDParm;
140668 struct ExprList_item *aOutEx = p->pEList->a;
140674 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat>0?"RIGHT PART OF ":"")
140676 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
140677 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
140681 if( pSort->labelBkOut ){
140682 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
140684 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
140688 /* Open any cursors needed for sorter-reference expressions */
140689 for(i=0; i<pSort->nDefer; i++){
140690 Table *pTab = pSort->aDefer[i].pTab;
140691 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
140692 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
140693 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
140697 iTab = pSort->iECursor;
140699 if( eDest==SRT_Mem && p->iOffset ){
140700 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
140703 regRow = pDest->iSdst;
140713 nKey = pOrderBy->nExpr - pSort->nOBSat;
140714 if( pSort->sortFlags & SORTFLAG_UseSorter ){
140715 int regSortOut = ++pParse->nMem;
140716 iSortTab = pParse->nTab++;
140717 if( pSort->labelBkOut ){
140725 assert( p->iLimit==0 && p->iOffset==0 );
140730 codeOffset(v, p->iOffset, addrContinue);
140733 if( p->iOffset>0 ){
140734 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
140737 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
140744 if( pSort->nDefer ){
140748 for(i=0; i<pSort->nDefer; i++){
140749 int iCsr = pSort->aDefer[i].iCsr;
140750 Table *pTab = pSort->aDefer[i].pTab;
140751 int nKey = pSort->aDefer[i].nKey;
140761 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
140774 for(i=nColumn-1; i>=0; i--){
140783 iRead = aOutEx[i].u.x.iOrderByCol-1;
140785 iRead = iCol--;
140791 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
140803 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
140805 pDest->zAffSdst, nColumn);
140815 int i2 = pDest->iSDParm2;
140817 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
140830 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
140832 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
140848 if( pSort->sortFlags & SORTFLAG_UseSorter ){
140853 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
140854 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
140866 ** result-set expression in all of the following SELECT statements is
140877 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
140904 assert( pNC->pSrcList!=0 );
140905 switch( pExpr->op ){
140913 int iCol = pExpr->iColumn; /* Index of column in pTab */
140915 SrcList *pTabList = pNC->pSrcList;
140916 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
140917 if( j<pTabList->nSrc ){
140918 pTab = pTabList->a[j].pTab;
140919 pS = pTabList->a[j].pSelect;
140921 pNC = pNC->pNext;
140936 ** sub-select. In this case, set the column type to NULL, even
140946 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
140948 /* The "table" is actually a sub-select or a view in the FROM clause
140950 ** data for the result-set column of the sub-select.
140952 if( iCol<pS->pEList->nExpr
140960 ** rowid of the sub-select or view. This expression is legal (see
140961 ** test case misc2.2.2) - it always evaluates to NULL.
140964 Expr *p = pS->pEList->a[iCol].pExpr;
140965 sNC.pSrcList = pS->pSrc;
140967 sNC.pParse = pNC->pParse;
140974 if( iCol<0 ) iCol = pTab->iPKey;
140975 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140980 zOrigCol = pTab->aCol[iCol].zCnName;
140981 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
140983 zOrigTab = pTab->zName;
140984 if( pNC->pParse && pTab->pSchema ){
140985 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
140986 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
140989 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140993 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
141001 /* The expression is a sub-select. Return the declaration type and
141009 pS = pExpr->x.pSelect;
141010 p = pS->pEList->a[0].pExpr;
141011 sNC.pSrcList = pS->pSrc;
141013 sNC.pParse = pNC->pParse;
141041 Vdbe *v = pParse->pVdbe;
141047 for(i=0; i<pEList->nExpr; i++){
141048 Expr *p = pEList->a[i].pExpr;
141056 /* The vdbe must make its own copy of the column-type and other
141106 Vdbe *v = pParse->pVdbe;
141111 sqlite3 *db = pParse->db;
141117 if( pParse->explain ){
141122 if( pParse->colNamesSet ) return;
141123 /* Column names are determined by the left-most term of a compound select */
141124 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
141126 pTabList = pSelect->pSrc;
141127 pEList = pSelect->pEList;
141130 pParse->colNamesSet = 1;
141131 fullName = (db->flags & SQLITE_FullColNames)!=0;
141132 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
141133 sqlite3VdbeSetNumCols(v, pEList->nExpr);
141134 for(i=0; i<pEList->nExpr; i++){
141135 Expr *p = pEList->a[i].pExpr;
141138 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
141139 assert( p->op!=TK_COLUMN
141140 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
141141 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
141143 char *zName = pEList->a[i].zEName;
141145 }else if( srcName && p->op==TK_COLUMN ){
141147 int iCol = p->iColumn;
141148 pTab = p->y.pTab;
141150 if( iCol<0 ) iCol = pTab->iPKey;
141151 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
141155 zCol = pTab->aCol[iCol].zCnName;
141159 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
141165 const char *z = pEList->a[i].zEName;
141201 sqlite3 *db = pParse->db; /* Database connection */
141213 nCol = pEList->nExpr;
141225 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
141226 struct ExprList_item *pX = &pEList->a[i];
141230 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
141233 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
141234 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
141235 pColExpr = pColExpr->pRight;
141238 if( pColExpr->op==TK_COLUMN
141240 && ALWAYS( pColExpr->y.pTab!=0 )
141243 int iCol = pColExpr->iColumn;
141244 pTab = pColExpr->y.pTab;
141245 if( iCol<0 ) iCol = pTab->iPKey;
141246 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
141247 }else if( pColExpr->op==TK_ID ){
141249 zName = pColExpr->u.zToken;
141252 assert( zName==pX->zEName ); /* pointer comparison intended */
141266 if( pCollide->fg.bUsingTerm ){
141267 pCol->colFlags |= COLFLAG_NOEXPAND;
141271 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
141280 pCol->zCnName = zName;
141281 pCol->hName = sqlite3StrIHash(zName);
141282 if( pX->fg.bNoExpand ){
141283 pCol->colFlags |= COLFLAG_NOEXPAND;
141291 if( pParse->nErr ){
141298 return pParse->rc;
141320 sqlite3 *db = pParse->db;
141329 assert( (pSelect->selFlags & SF_Resolved)!=0 );
141330 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
141332 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
141333 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
141334 a = pSelect->pEList->a;
141336 sNC.pSrcList = pSelect->pSrc;
141337 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
141340 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
141342 /* pCol->szEst = ... // Column size est for SELECT tables never used */
141343 pCol->affinity = sqlite3ExprAffinity(p);
141344 if( pCol->affinity<=SQLITE_AFF_NONE ){
141345 pCol->affinity = aff;
141347 if( pCol->affinity>=SQLITE_AFF_TEXT && pSelect->pNext ){
141350 for(m=0, pS2=pSelect->pNext; pS2; pS2=pS2->pNext){
141351 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
141353 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
141354 pCol->affinity = SQLITE_AFF_BLOB;
141356 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
141357 pCol->affinity = SQLITE_AFF_BLOB;
141359 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
141360 pCol->affinity = SQLITE_AFF_FLEXNUM;
141364 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
141365 if( pCol->affinity==SQLITE_AFF_NUMERIC
141366 || pCol->affinity==SQLITE_AFF_FLEXNUM
141372 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
141381 n = sqlite3Strlen30(pCol->zCnName);
141382 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
141383 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
141384 if( pCol->zCnName ){
141385 memcpy(&pCol->zCnName[n+1], zType, m+1);
141386 pCol->colFlags |= COLFLAG_HASTYPE;
141391 assert( pTab->pIndex==0 );
141392 sqlite3ColumnSetColl(db, pCol, pColl->zName);
141395 pTab->szTabRow = 1; /* Any non-zero value works */
141404 sqlite3 *db = pParse->db;
141407 savedFlags = db->flags;
141408 db->flags &= ~(u64)SQLITE_FullColNames;
141409 db->flags |= SQLITE_ShortColNames;
141411 db->flags = savedFlags;
141412 if( pParse->nErr ) return 0;
141413 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
141418 pTab->nTabRef = 1;
141419 pTab->zName = 0;
141420 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141421 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
141423 pTab->iPKey = -1;
141424 if( db->mallocFailed ){
141436 if( pParse->pVdbe ){
141437 return pParse->pVdbe;
141439 if( pParse->pToplevel==0
141440 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
141442 pParse->okConstFactor = 1;
141450 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
141458 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
141466 ** Only if pLimit->pLeft!=0 do the limit registers get
141476 Expr *pLimit = p->pLimit;
141478 if( p->iLimit ) return;
141481 ** "LIMIT -1" always shows all rows. There is some
141487 assert( pLimit->op==TK_LIMIT );
141488 assert( pLimit->pLeft!=0 );
141489 p->iLimit = iLimit = ++pParse->nMem;
141492 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
141497 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
141498 p->nSelectRow = sqlite3LogEst((u64)n);
141499 p->selFlags |= SF_FixedLimit;
141502 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
141507 if( pLimit->pRight ){
141508 p->iOffset = iOffset = ++pParse->nMem;
141509 pParse->nMem++; /* Allocate an extra register for limit+offset */
141510 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
141521 ** Return the appropriate collating sequence for the iCol-th column of
141522 ** the result set for the compound-select statement "p". Return NULL if
141526 ** left-most term of the select that has a collating sequence.
141530 if( p->pPrior ){
141531 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
141536 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
141539 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
141540 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
141555 ExprList *pOrderBy = p->pOrderBy;
141556 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
141557 sqlite3 *db = pParse->db;
141562 struct ExprList_item *pItem = &pOrderBy->a[i];
141563 Expr *pTerm = pItem->pExpr;
141566 if( pTerm->flags & EP_Collate ){
141569 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
141570 if( pColl==0 ) pColl = db->pDfltColl;
141571 pOrderBy->a[i].pExpr =
141572 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
141575 pRet->aColl[i] = pColl;
141576 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
141588 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
141590 ** p->pPrior p
141593 ** There is exactly one reference to the recursive-table in the FROM clause
141594 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
141596 ** The setup-query runs once to generate an initial set of rows that go
141600 ** recursive-table for a recursive-query run. The output of the recursive-query
141625 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
141626 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
141627 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
141629 Select *pFirstRec; /* Left-most recursive term */
141645 if( p->pWin ){
141656 p->nSelectRow = 320; /* 4 billion rows */
141658 pLimit = p->pLimit;
141659 regLimit = p->iLimit;
141660 regOffset = p->iOffset;
141661 p->pLimit = 0;
141662 p->iLimit = p->iOffset = 0;
141663 pOrderBy = p->pOrderBy;
141666 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
141667 if( pSrc->a[i].fg.isRecursive ){
141668 iCurrent = pSrc->a[i].iCursor;
141676 iQueue = pParse->nTab++;
141677 if( p->op==TK_UNION ){
141679 iDistinct = pParse->nTab++;
141686 regCurrent = ++pParse->nMem;
141690 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
141698 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
141699 p->selFlags |= SF_UsesEphemeral;
141703 p->pOrderBy = 0;
141709 ** iDistinct table. pFirstRec is left pointing to the left-most
141712 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
141713 if( pFirstRec->selFlags & SF_Aggregate ){
141717 pFirstRec->op = TK_ALL;
141718 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
141721 /* Store the results of the setup-query in Queue. */
141722 pSetup = pFirstRec->pPrior;
141723 pSetup->pNext = 0;
141726 pSetup->pNext = p;
141735 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
141753 ** the value for the recursive-table. Store the results in the Queue.
141755 pFirstRec->pPrior = 0;
141758 assert( pFirstRec->pPrior==0 );
141759 pFirstRec->pPrior = pSetup;
141766 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
141767 p->pOrderBy = pOrderBy;
141768 p->pLimit = pLimit;
141776 Select *p, /* The right-most of SELECTs to be coded */
141781 ** Handle the special case of a compound-select that originates from a
141794 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
141798 Select *p, /* The right-most of SELECTs to be coded */
141803 int bShowAll = p->pLimit==0;
141804 assert( p->selFlags & SF_MultiValue );
141806 assert( p->selFlags & SF_Values );
141807 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
141808 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
141810 if( p->pWin ) return -1;
141812 if( p->pPrior==0 ) break;
141813 assert( p->pPrior->pNext==p );
141814 p = p->pPrior;
141820 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
141822 p->nSelectRow = nRow;
141823 p = p->pNext;
141834 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
141843 ** "p" points to the right-most of the two queries. the query on the
141844 ** left is p->pPrior. The left query could also be a compound query
141850 ** Example 1: Consider a three-way compound SQL statement.
141858 ** `-----> SELECT b FROM t2
141860 ** `------> SELECT a FROM t1
141864 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
141871 Select *p, /* The right-most of SELECTs to be coded */
141882 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
141884 assert( p && p->pPrior ); /* Calling function guarantees this much */
141885 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
141886 assert( p->selFlags & SF_Compound );
141887 db = pParse->db;
141888 pPrior = p->pPrior;
141890 assert( pPrior->pOrderBy==0 );
141891 assert( pPrior->pLimit==0 );
141899 assert( p->pEList );
141900 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
141904 /* Special handling for a compound-select that originates as a VALUES clause.
141906 if( p->selFlags & SF_MultiValue ){
141915 assert( p->pEList && pPrior->pEList );
141916 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
141919 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
141926 if( p->pOrderBy ){
141931 if( pPrior->pPrior==0 ){
141933 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
141939 switch( p->op ){
141943 assert( !pPrior->pLimit );
141944 pPrior->iLimit = p->iLimit;
141945 pPrior->iOffset = p->iOffset;
141946 pPrior->pLimit = p->pLimit;
141949 pPrior->pLimit = 0;
141953 p->pPrior = 0;
141954 p->iLimit = pPrior->iLimit;
141955 p->iOffset = pPrior->iOffset;
141956 if( p->iLimit ){
141957 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
141959 if( p->iOffset ){
141961 p->iLimit, p->iOffset+1, p->iOffset);
141968 pDelete = p->pPrior;
141969 p->pPrior = pPrior;
141970 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141971 if( p->pLimit
141972 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
141973 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
141975 p->nSelectRow = sqlite3LogEst((u64)nLimit);
141987 Expr *pLimit; /* Saved values of p->nLimit */
141991 testcase( p->op==TK_EXCEPT );
141992 testcase( p->op==TK_UNION );
141998 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
142004 unionTab = pParse->nTab++;
142005 assert( p->pOrderBy==0 );
142007 assert( p->addrOpenEphm[0] == -1 );
142008 p->addrOpenEphm[0] = addr;
142009 findRightmost(p)->selFlags |= SF_UsesEphemeral;
142010 assert( p->pEList );
142016 assert( !pPrior->pOrderBy );
142026 if( p->op==TK_EXCEPT ){
142029 assert( p->op==TK_UNION );
142032 p->pPrior = 0;
142033 pLimit = p->pLimit;
142034 p->pLimit = 0;
142036 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
142037 sqlite3SelectOpName(p->op)));
142041 assert( p->pOrderBy==0 );
142042 pDelete = p->pPrior;
142043 p->pPrior = pPrior;
142044 p->pOrderBy = 0;
142045 if( p->op==TK_UNION ){
142046 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
142048 sqlite3ExprDelete(db, p->pLimit);
142049 p->pLimit = pLimit;
142050 p->iLimit = 0;
142051 p->iOffset = 0;
142057 assert( p->pEList || db->mallocFailed );
142058 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
142074 default: assert( p->op==TK_INTERSECT ); {
142086 tab1 = pParse->nTab++;
142087 tab2 = pParse->nTab++;
142088 assert( p->pOrderBy==0 );
142091 assert( p->addrOpenEphm[0] == -1 );
142092 p->addrOpenEphm[0] = addr;
142093 findRightmost(p)->selFlags |= SF_UsesEphemeral;
142094 assert( p->pEList );
142108 assert( p->addrOpenEphm[1] == -1 );
142109 p->addrOpenEphm[1] = addr;
142110 p->pPrior = 0;
142111 pLimit = p->pLimit;
142112 p->pLimit = 0;
142114 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
142115 sqlite3SelectOpName(p->op)));
142119 pDelete = p->pPrior;
142120 p->pPrior = pPrior;
142121 if( p->nSelectRow>pPrior->nSelectRow ){
142122 p->nSelectRow = pPrior->nSelectRow;
142124 sqlite3ExprDelete(db, p->pLimit);
142125 p->pLimit = pLimit;
142131 assert( p->pEList );
142153 if( p->pNext==0 ){
142158 if( pParse->nErr ) goto multi_select_end;
142164 ** This section is run by the right-most SELECT statement only.
142165 ** SELECT statements to the left always skip this part. The right-most
142169 if( p->selFlags & SF_UsesEphemeral ){
142173 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
142176 assert( p->pNext==0 );
142177 assert( p->pEList!=0 );
142178 nCol = p->pEList->nExpr;
142184 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
142187 *apColl = db->pDfltColl;
142191 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
142193 int addr = pLoop->addrOpenEphm[i];
142197 assert( pLoop->addrOpenEphm[1]<0 );
142203 pLoop->addrOpenEphm[i] = -1;
142210 pDest->iSdst = dest.iSdst;
142211 pDest->nSdst = dest.nSdst;
142226 if( p->selFlags & SF_Values ){
142231 sqlite3SelectOpName(p->op));
142239 ** The data to be output is contained in pIn->iSdst. There are
142240 ** pIn->nSdst columns to be output. pDest is where the output should
142252 ** If the LIMIT found in p->iLimit is reached, jump immediately to
142265 Vdbe *v = pParse->pVdbe;
142277 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
142281 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
142284 if( pParse->db->mallocFailed ) return 0;
142288 codeOffset(v, p->iOffset, iContinue);
142290 assert( pDest->eDest!=SRT_Exists );
142291 assert( pDest->eDest!=SRT_Table );
142292 switch( pDest->eDest ){
142298 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
142299 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
142300 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
142312 testcase( pIn->nSdst>1 );
142314 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
142315 r1, pDest->zAffSdst, pIn->nSdst);
142316 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
142317 pIn->iSdst, pIn->nSdst);
142325 ** if it is the RHS of a row-value IN operator.
142328 testcase( pIn->nSdst>1 );
142329 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
142336 ** starting at pDest->iSdst. Then the co-routine yields.
142339 if( pDest->iSdst==0 ){
142340 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
142341 pDest->nSdst = pIn->nSdst;
142343 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
142344 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
142357 assert( pDest->eDest==SRT_Output );
142358 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
142365 if( p->iLimit ){
142366 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
142387 ** co-routines. Then run the co-routines in parallel and merge the results
142414 ** ------------- ----------------- -------------- -----------------
142465 Select *p, /* The right-most of SELECTs to be coded */
142470 Select *pSplit; /* Left-most SELECT in the right-hand group */
142475 int regAddrA; /* Address register for select-A coroutine */
142476 int regAddrB; /* Address register for select-B coroutine */
142477 int addrSelectA; /* Address of the select-A coroutine */
142478 int addrSelectB; /* Address of the select-B coroutine */
142479 int regOutA; /* Address register for the output-A subroutine */
142480 int regOutB; /* Address register for the output-B subroutine */
142481 int addrOutA; /* Address of the output-A subroutine */
142482 int addrOutB = 0; /* Address of the output-B subroutine */
142483 int addrEofA; /* Address of the select-A-exhausted subroutine */
142485 int addrEofB; /* Address of the select-B-exhausted subroutine */
142489 int regLimitA; /* Limit register for select-A */
142490 int regLimitB; /* Limit register for select-A */
142492 int savedLimit; /* Saved value of p->iLimit */
142493 int savedOffset; /* Saved value of p->iOffset */
142505 assert( p->pOrderBy!=0 );
142507 db = pParse->db;
142508 v = pParse->pVdbe;
142516 op = p->op;
142517 assert( p->pPrior->pOrderBy==0 );
142518 pOrderBy = p->pOrderBy;
142520 nOrderBy = pOrderBy->nExpr;
142527 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
142529 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
142531 assert( pItem->u.x.iOrderByCol>0 );
142532 if( pItem->u.x.iOrderByCol==i ) break;
142537 pNew->flags |= EP_IntValue;
142538 pNew->u.iValue = i;
142539 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
142540 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
142556 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
142558 assert( pItem->u.x.iOrderByCol>0 );
142559 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
142560 aPermute[i] = pItem->u.x.iOrderByCol - 1;
142574 int nExpr = p->pEList->nExpr;
142575 assert( nOrderBy>=nExpr || db->mallocFailed );
142576 regPrev = pParse->nMem+1;
142577 pParse->nMem += nExpr+1;
142583 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
142584 pKeyDup->aSortFlags[i] = 0;
142595 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
142597 assert( pSplit->pPrior->pNext==pSplit );
142604 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
142606 pPrior = pSplit->pPrior;
142608 pSplit->pPrior = 0;
142609 pPrior->pNext = 0;
142610 assert( p->pOrderBy == pOrderBy );
142611 assert( pOrderBy!=0 || db->mallocFailed );
142612 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
142613 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
142614 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
142618 if( p->iLimit && op==TK_ALL ){
142619 regLimitA = ++pParse->nMem;
142620 regLimitB = ++pParse->nMem;
142621 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
142627 sqlite3ExprDelete(db, p->pLimit);
142628 p->pLimit = 0;
142630 regAddrA = ++pParse->nMem;
142631 regAddrB = ++pParse->nMem;
142632 regOutA = ++pParse->nMem;
142633 regOutB = ++pParse->nMem;
142637 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
142640 ** left of the compound operator - the "A" select.
142645 pPrior->iLimit = regLimitA;
142652 ** the right - the "B" select
142657 savedLimit = p->iLimit;
142658 savedOffset = p->iOffset;
142659 p->iLimit = regLimitB;
142660 p->iOffset = 0;
142663 p->iLimit = savedLimit;
142664 p->iOffset = savedOffset;
142692 VdbeNoopComment((v, "eof-A subroutine"));
142697 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
142705 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
142707 VdbeNoopComment((v, "eof-B subroutine"));
142715 VdbeNoopComment((v, "A-lt-B subroutine"));
142728 VdbeNoopComment((v, "A-eq-B subroutine"));
142736 VdbeNoopComment((v, "A-gt-B subroutine"));
142765 if( pSplit->pPrior ){
142767 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
142769 pSplit->pPrior = pPrior;
142770 pPrior->pNext = pSplit;
142771 sqlite3ExprListDelete(db, pPrior->pOrderBy);
142772 pPrior->pOrderBy = 0;
142777 return pParse->nErr!=0;
142792 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
142815 ** when processing a non-matched row of the left.
142832 ** a column in table number iTable with a copy of the iColumn-th
142849 && pExpr->w.iJoin==pSubst->iTable
142852 pExpr->w.iJoin = pSubst->iNewTable;
142854 if( pExpr->op==TK_COLUMN
142855 && pExpr->iTable==pSubst->iTable
142859 if( pExpr->iColumn<0 ){
142860 pExpr->op = TK_NULL;
142865 int iColumn = pExpr->iColumn;
142866 Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
142868 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
142869 assert( pExpr->pRight==0 );
142871 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
142873 sqlite3 *db = pSubst->pParse->db;
142874 if( pSubst->isOuterJoin
142875 && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable)
142880 ifNullRow.iTable = pSubst->iNewTable;
142881 ifNullRow.iColumn = -99;
142887 if( db->mallocFailed ){
142891 if( pSubst->isOuterJoin ){
142895 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
142896 pExpr->flags & (EP_OuterON|EP_InnerON));
142900 if( pExpr->op==TK_TRUEFALSE ){
142901 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
142902 pExpr->op = TK_INTEGER;
142907 ** just as it did when it was a column of a view or sub-query. */
142909 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
142910 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
142911 pSubst->pCList->a[iColumn].pExpr
142913 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
142914 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
142915 (pColl ? pColl->zName : "BINARY")
142923 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
142924 pExpr->iTable = pSubst->iNewTable;
142926 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
142927 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
142929 substSelect(pSubst, pExpr->x.pSelect, 1);
142931 substExprList(pSubst, pExpr->x.pList);
142935 Window *pWin = pExpr->y.pWin;
142936 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
142937 substExprList(pSubst, pWin->pPartition);
142938 substExprList(pSubst, pWin->pOrderBy);
142950 for(i=0; i<pList->nExpr; i++){
142951 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
142957 int doPrior /* Do substitutes on p->pPrior too */
142964 substExprList(pSubst, p->pEList);
142965 substExprList(pSubst, p->pGroupBy);
142966 substExprList(pSubst, p->pOrderBy);
142967 p->pHaving = substExpr(pSubst, p->pHaving);
142968 p->pWhere = substExpr(pSubst, p->pWhere);
142969 pSrc = p->pSrc;
142971 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
142972 substSelect(pSubst, pItem->pSelect, 1);
142973 if( pItem->fg.isTabFunc ){
142974 substExprList(pSubst, pItem->u1.pFuncArg);
142977 }while( doPrior && (p = p->pPrior)!=0 );
142987 ** pSrcItem->colUsed mask.
142991 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
142992 pItem = pWalker->u.pSrcItem;
142993 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
142994 if( pExpr->iColumn<0 ) return WRC_Continue;
142995 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
143003 if( NEVER(pSrcItem->pTab==0) ) return;
143008 pSrcItem->colUsed = 0;
143024 ** If pSrc contains any sub-selects, call this routine recursively
143025 ** on the FROM clause of each such sub-select, with iExcept set to -1.
143035 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
143038 assert( pItem->iCursor < aCsrMap[0] );
143039 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
143040 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
143042 pItem->iCursor = aCsrMap[pItem->iCursor+1];
143043 for(p=pItem->pSelect; p; p=p->pPrior){
143044 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
143054 int *aCsrMap = pWalker->u.aiCol;
143066 int op = pExpr->op;
143068 renumberCursorDoMapping(pWalker, &pExpr->iTable);
143071 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
143079 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
143101 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
143116 while( pSel->pPrior ){
143117 pSel = pSel->pPrior;
143119 return pSel->pEList;
143123 ** Return true if any of the result-set columns in the compound query
143130 assert( p->pEList!=0 );
143131 assert( p->pPrior!=0 );
143132 pList = p->pEList;
143133 for(ii=0; ii<pList->nExpr; ii++){
143136 assert( pList->a[ii].pExpr!=0 );
143137 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
143138 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
143139 assert( pSub1->pEList!=0 );
143140 assert( pSub1->pEList->nExpr>ii );
143141 assert( pSub1->pEList->a[ii].pExpr!=0 );
143142 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
143186 ** other than the one FROM-clause subquery that is a candidate
143188 ** from 2015-02-09.)
143202 ** sub-queries that were excluded from this optimization. Restriction
143217 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
143218 ** accidently carried the comment forward until 2014-09-15. Original
143255 ** The parent and sub-query may contain WHERE clauses. Subject to
143262 ** Also, each component of the sub-query must return the same number
143265 ** such (illegal) sub-query is flattened. The caller will detect the
143268 ** (18) If the sub-query is a compound select, then all terms of the
143275 ** (20) If the sub-query is a compound select, then it must not use
143286 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
143292 ** The subquery may not be an aggregate that uses the built-in min() or
143314 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
143317 ** If flattening is not attempted, this routine is a no-op and returns 0.
143326 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
143329 const char *zSavedAuthContext = pParse->zAuthContext;
143332 Select *pSub1; /* Pointer to the rightmost select in sub-query */
143336 int iNewParent = -1;/* Replacement table for iParent */
143341 sqlite3 *db = pParse->db;
143348 assert( p->pPrior==0 );
143350 pSrc = p->pSrc;
143351 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
143352 pSubitem = &pSrc->a[iFrom];
143353 iParent = pSubitem->iCursor;
143354 pSub = pSubitem->pSelect;
143358 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
143361 pSubSrc = pSub->pSrc;
143365 ** because they could be computed at compile-time. But when LIMIT and OFFSET
143368 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
143369 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
143370 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
143373 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
143374 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
143375 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
143378 if( p->pOrderBy && pSub->pOrderBy ){
143381 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
143382 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
143383 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
143386 if( pSub->selFlags & (SF_Recursive) ){
143405 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
143406 if( pSubSrc->nSrc>1 /* (3a) */
143407 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
143408 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
143409 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
143416 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
143417 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143420 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
143424 /* Restriction (17): If the sub-query is a compound SELECT, then it must
143429 if( pSub->pPrior ){
143431 if( pSub->pOrderBy ){
143434 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
143437 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
143438 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
143439 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
143440 assert( pSub->pSrc!=0 );
143441 assert( (pSub->selFlags & SF_Recursive)==0 );
143442 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
143443 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
143444 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
143445 || pSub1->pSrc->nSrc<1 /* (17c) */
143447 || pSub1->pWin /* (17e) */
143452 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143454 ** omitted on left-hand tables of the right join that is being
143458 testcase( pSub1->pSrc->nSrc>1 );
143462 if( p->pOrderBy ){
143463 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
143464 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
143469 if( (p->selFlags & SF_Recursive) ) return 0;
143474 if( pSrc->nSrc>1 ){
143475 if( pParse->nSelect>500 ) return 0;
143477 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
143478 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
143484 pSub->selId, pSub, iFrom));
143487 pParse->zAuthContext = pSubitem->zName;
143490 pParse->zAuthContext = zSavedAuthContext;
143493 pSub1 = pSubitem->pSelect;
143494 sqlite3DbFree(db, pSubitem->zDatabase);
143495 sqlite3DbFree(db, pSubitem->zName);
143496 sqlite3DbFree(db, pSubitem->zAlias);
143497 pSubitem->zDatabase = 0;
143498 pSubitem->zName = 0;
143499 pSubitem->zAlias = 0;
143500 pSubitem->pSelect = 0;
143501 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
143503 /* If the sub-query is a compound SELECT statement, then (by restrictions
143507 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
143510 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
143511 ** OFFSET clauses and joins them to the left-hand-side of the original
143513 ** select statements in the compound sub-query.
143534 ** We call this the "compound-subquery flattening".
143536 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
143538 ExprList *pOrderBy = p->pOrderBy;
143539 Expr *pLimit = p->pLimit;
143540 Select *pPrior = p->pPrior;
143541 Table *pItemTab = pSubitem->pTab;
143542 pSubitem->pTab = 0;
143543 p->pOrderBy = 0;
143544 p->pPrior = 0;
143545 p->pLimit = 0;
143547 p->pLimit = pLimit;
143548 p->pOrderBy = pOrderBy;
143549 p->op = TK_ALL;
143550 pSubitem->pTab = pItemTab;
143552 p->pPrior = pPrior;
143554 pNew->selId = ++pParse->nSelect;
143555 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
143558 pNew->pPrior = pPrior;
143559 if( pPrior ) pPrior->pNext = pNew;
143560 pNew->pNext = p;
143561 p->pPrior = pNew;
143562 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
143563 " creates %u as peer\n",pNew->selId));
143565 assert( pSubitem->pSelect==0 );
143568 if( db->mallocFailed ){
143569 pSubitem->pSelect = pSub1;
143578 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
143580 if( ALWAYS(pSubitem->pTab!=0) ){
143581 Table *pTabToDel = pSubitem->pTab;
143582 if( pTabToDel->nTabRef==1 ){
143587 testcase( pToplevel->earlyCleanup );
143589 pTabToDel->nTabRef--;
143591 pSubitem->pTab = 0;
143594 /* The following loop runs once for each term in a compound-subquery
143596 ** of flattening - a flattening other than a compound-subquery flattening -
143608 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
143611 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
143613 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
143614 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
143615 pSrc = pParent->pSrc; /* FROM clause of the outer query */
143618 jointype = pSubitem->fg.jointype; /* First time through the loop */
143637 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
143639 pParent->pSrc = pSrc;
143646 SrcItem *pItem = &pSrc->a[i+iFrom];
143647 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
143648 assert( pItem->fg.isTabFunc==0 );
143649 *pItem = pSubSrc->a[i];
143650 pItem->fg.jointype |= ltorj;
143651 iNewParent = pSubSrc->a[i].iCursor;
143652 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
143654 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
143655 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
143669 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
143670 /* At this point, any non-zero iOrderByCol values indicate that the
143677 ** function attempts to flatten a compound sub-query into pParent
143678 ** (the only way this can happen is if the compound sub-query is
143679 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
143680 ExprList *pOrderBy = pSub->pOrderBy;
143681 for(i=0; i<pOrderBy->nExpr; i++){
143682 pOrderBy->a[i].u.x.iOrderByCol = 0;
143684 assert( pParent->pOrderBy==0 );
143685 pParent->pOrderBy = pOrderBy;
143686 pSub->pOrderBy = 0;
143688 pWhere = pSub->pWhere;
143689 pSub->pWhere = 0;
143694 if( pParent->pWhere ){
143695 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
143697 pParent->pWhere = pWhere;
143700 if( db->mallocFailed==0 ){
143706 x.pEList = pSub->pEList;
143713 pParent->selFlags |= pSub->selFlags & SF_Compound;
143714 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
143722 if( pSub->pLimit ){
143723 pParent->pLimit = pSub->pLimit;
143724 pSub->pLimit = 0;
143730 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
143759 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
143783 assert( pColumn->op==TK_COLUMN );
143788 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
143792 /* 2018-10-25 ticket [cf5ed20f]
143794 for(i=0; i<pConst->nConst; i++){
143795 const Expr *pE2 = pConst->apExpr[i*2];
143796 assert( pE2->op==TK_COLUMN );
143797 if( pE2->iTable==pColumn->iTable
143798 && pE2->iColumn==pColumn->iColumn
143804 pConst->bHasAffBlob = 1;
143807 pConst->nConst++;
143808 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
143809 pConst->nConst*2*sizeof(Expr*));
143810 if( pConst->apExpr==0 ){
143811 pConst->nConst = 0;
143813 pConst->apExpr[pConst->nConst*2-2] = pColumn;
143814 pConst->apExpr[pConst->nConst*2-1] = pValue;
143821 ** is part of the AND-connected terms of the expression. For each term
143827 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
143832 if( pExpr->op==TK_AND ){
143833 findConstInWhere(pConst, pExpr->pRight);
143834 findConstInWhere(pConst, pExpr->pLeft);
143837 if( pExpr->op!=TK_EQ ) return;
143838 pRight = pExpr->pRight;
143839 pLeft = pExpr->pLeft;
143842 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
143845 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
143854 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
143856 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
143865 if( pConst->pOomFault[0] ) return WRC_Prune;
143866 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
143867 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
143873 for(i=0; i<pConst->nConst; i++){
143874 Expr *pColumn = pConst->apExpr[i*2];
143876 if( pColumn->iTable!=pExpr->iTable ) continue;
143877 if( pColumn->iColumn!=pExpr->iColumn ) continue;
143882 pConst->nChng++;
143885 assert( pExpr->pLeft==0 );
143886 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
143887 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
143896 ** any substitutions based on the contents of pWalker->u.pConst should
143902 ** one of the columns in pWalker->u.pConst, or
143907 ** pWalker->u.pConst.
143910 WhereConst *pConst = pWalker->u.pConst;
143915 if( pConst->bHasAffBlob ){
143916 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
143917 || pExpr->op==TK_IS
143919 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
143920 if( pConst->pOomFault[0] ) return WRC_Prune;
143921 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
143922 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
143926 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
143930 ** The WHERE-clause constant propagation optimization.
143933 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
143965 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
143988 x.pOomFault = &pParse->db->mallocFailed;
143994 if( ALWAYS(p->pSrc!=0)
143995 && p->pSrc->nSrc>0
143996 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
144005 findConstInWhere(&x, p->pWhere);
144014 sqlite3WalkExpr(&w, p->pWhere);
144015 sqlite3DbFree(x.pParse->db, x.apExpr);
144026 ** push WHERE clause expression pExpr down to FROM clause sub-query
144033 ** BY clause of all window function used by the sub-query. It is safe
144039 ** * the sub-query uses only one distinct window frame, and
144043 assert( pSubq->pWin->pPartition );
144044 assert( (pSubq->selFlags & SF_MultiPart)==0 );
144045 assert( pSubq->pPrior==0 );
144046 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
144056 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
144060 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
144068 ** (1) (** This restriction was removed on 2017-09-29. We used to
144086 ** of a LEFT JOIN where iCursor is not the right-hand table of that
144105 ** (6b) The inner query is a compound and uses window-functions.
144109 ** all window-functions used by the sub-query. It is safe to
144111 ** window over which any window-function is calculated.
144131 ** Without this restriction, the push-down optimization might move
144136 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
144140 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
144153 pSrc = &pSrcList->a[iSrc];
144155 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){
144158 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){
144162 if( pSubq->pPrior ){
144165 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
144166 u8 op = pSel->op;
144173 if( pSel->pWin ) return 0; /* restriction (6b) */
144179 ** non-BINARY collating sequence. */
144180 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
144182 const ExprList *pList = pSel->pEList;
144184 for(ii=0; ii<pList->nExpr; ii++){
144185 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
144194 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
144205 for(pX=pSubq; pX; pX=pX->pPrior){
144206 assert( (pX->selFlags & (SF_Recursive))==0 );
144211 if( pSubq->pLimit!=0 ){
144214 while( pWhere->op==TK_AND ){
144215 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
144216 pWhere = pWhere->pLeft;
144221 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */
144225 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
144230 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT)!=0 ){
144239 || pWhere->w.iJoin!=iCursor)
144244 && pWhere->w.iJoin!=iCursor
144252 pSubq->selFlags |= SF_PushDown;
144255 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
144256 unsetJoinExpr(pNew, -1, 1);
144258 x.iTable = pSrc->iCursor;
144259 x.iNewTable = pSrc->iCursor;
144261 x.pEList = pSubq->pEList;
144265 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
144266 /* Restriction 6c has prevented push-down in this case */
144267 sqlite3ExprDelete(pParse->db, pNew);
144268 nChng--;
144272 if( pSubq->selFlags & SF_Aggregate ){
144273 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
144275 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
144277 pSubq = pSubq->pPrior;
144285 ** Check to see if a subquery contains result-set columns that are
144286 ** never used. If it does, change the value of those result-set columns
144301 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
144304 assert( pItem->pTab!=0 );
144305 pTab = pItem->pTab;
144306 assert( pItem->pSelect!=0 );
144307 pSub = pItem->pSelect;
144308 assert( pSub->pEList->nExpr==pTab->nCol );
144309 if( (pSub->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){
144310 testcase( pSub->selFlags & SF_Distinct );
144311 testcase( pSub->selFlags & SF_Aggregate );
144314 for(pX=pSub; pX; pX=pX->pPrior){
144315 if( pX->pPrior && pX->op!=TK_ALL ){
144321 if( pX->pWin ){
144328 colUsed = pItem->colUsed;
144329 if( pSub->pOrderBy ){
144330 ExprList *pList = pSub->pOrderBy;
144331 for(j=0; j<pList->nExpr; j++){
144332 u16 iCol = pList->a[j].u.x.iOrderByCol;
144334 iCol--;
144335 colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
144339 nCol = pTab->nCol;
144341 Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT;
144343 for(pX=pSub; pX; pX=pX->pPrior) {
144344 Expr *pY = pX->pEList->a[j].pExpr;
144345 if( pY->op==TK_NULL ) continue;
144346 pY->op = TK_NULL;
144348 pX->selFlags |= SF_PushDown;
144380 assert( pFunc->op==TK_AGG_FUNCTION );
144383 pEList = pFunc->x.pList;
144385 || pEList->nExpr!=1
144392 zFunc = pFunc->u.zToken;
144395 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
144405 assert( pOrderBy!=0 || db->mallocFailed );
144406 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
144412 ** The second argument is the associated aggregate-info object. This
144417 ** where table is a database table, not a sub-select or view. If the query
144431 assert( !p->pGroupBy );
144433 if( p->pWhere
144434 || p->pEList->nExpr!=1
144435 || p->pSrc->nSrc!=1
144436 || p->pSrc->a[0].pSelect
144437 || pAggInfo->nFunc!=1
144438 || p->pHaving
144442 pTab = p->pSrc->a[0].pTab;
144446 pExpr = p->pEList->a[0].pExpr;
144448 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
144449 if( pExpr->pAggInfo!=pAggInfo ) return 0;
144450 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
144451 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
144460 ** If the source-list item passed as an argument was augmented with an
144464 ** pFrom->pIndex and return SQLITE_OK.
144467 Table *pTab = pFrom->pTab;
144468 char *zIndexedBy = pFrom->u1.zIndexedBy;
144471 assert( pFrom->fg.isIndexedBy!=0 );
144473 for(pIdx=pTab->pIndex;
144474 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
144475 pIdx=pIdx->pNext
144479 pParse->checkSchema = 1;
144482 assert( pFrom->fg.isCte==0 );
144483 pFrom->u2.pIBIndex = pIdx;
144518 if( p->pPrior==0 ) return WRC_Continue;
144519 if( p->pOrderBy==0 ) return WRC_Continue;
144520 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
144522 a = p->pOrderBy->a;
144524 /* If iOrderByCol is already non-zero, then it has already been matched
144526 ** SELECT is rewritten for window-functions processing and then passed
144531 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
144532 if( a[i].pExpr->flags & EP_Collate ) break;
144538 pParse = pWalker->pParse;
144539 db = pParse->db;
144546 p->pSrc = pNewSrc;
144547 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
144548 p->op = TK_SELECT;
144549 p->pWhere = 0;
144550 pNew->pGroupBy = 0;
144551 pNew->pHaving = 0;
144552 pNew->pOrderBy = 0;
144553 p->pPrior = 0;
144554 p->pNext = 0;
144555 p->pWith = 0;
144557 p->pWinDefn = 0;
144559 p->selFlags &= ~SF_Compound;
144560 assert( (p->selFlags & SF_Converted)==0 );
144561 p->selFlags |= SF_Converted;
144562 assert( pNew->pPrior!=0 );
144563 pNew->pPrior->pNext = pNew;
144564 pNew->pLimit = 0;
144569 ** Check to see if the FROM clause term pFrom has table-valued function
144571 ** non-zero, since pFrom is not allowed to be a table-valued function.
144574 if( pFrom->fg.isTabFunc ){
144575 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
144585 ** FROM clause element pItem is really a common-table-expression (CTE)
144589 ** If a non-NULL value is returned, set *ppContext to point to the With
144597 const char *zName = pItem->zName;
144599 assert( pItem->zDatabase==0 );
144601 for(p=pWith; p; p=p->pOuter){
144603 for(i=0; i<p->nCte; i++){
144604 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
144606 return &p->a[i];
144609 if( p->bView ) break;
144615 ** with the inner-most WITH clause being at the top of the stack.
144639 if( pParse->nErr==0 ){
144640 assert( pParse->pWith!=pWith );
144641 pWith->pOuter = pParse->pWith;
144642 pParse->pWith = pWith;
144651 ** pParse->pWith linked list). And if currently processing a CTE
144655 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
144670 assert( pFrom->pTab==0 );
144671 if( pParse->pWith==0 ){
144675 if( pParse->nErr ){
144676 /* Prior errors might have left pParse->pWith in a goofy state, so
144680 if( pFrom->zDatabase!=0 ){
144685 if( pFrom->fg.notCte ){
144693 pCte = searchWith(pParse->pWith, pFrom, &pWith);
144695 sqlite3 *db = pParse->db;
144699 Select *pLeft; /* Left-most SELECT statement */
144700 Select *pRecTerm; /* Left-most recursive term */
144702 With *pSavedWith; /* Initial value of pParse->pWith */
144703 int iRecTab = -1; /* Cursor for recursive table */
144706 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
144708 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
144710 if( pCte->zCteErr ){
144711 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
144716 assert( pFrom->pTab==0 );
144719 pCteUse = pCte->pUse;
144721 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
144728 pCteUse->eM10d = pCte->eM10d;
144730 pFrom->pTab = pTab;
144731 pTab->nTabRef = 1;
144732 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
144733 pTab->iPKey = -1;
144734 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
144735 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
144736 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
144737 if( db->mallocFailed ) return 2;
144738 pFrom->pSelect->selFlags |= SF_CopyCte;
144739 assert( pFrom->pSelect );
144740 if( pFrom->fg.isIndexedBy ){
144741 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
144744 pFrom->fg.isCte = 1;
144745 pFrom->u2.pCteUse = pCteUse;
144746 pCteUse->nUse++;
144749 pRecTerm = pSel = pFrom->pSelect;
144750 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
144751 while( bMayRecursive && pRecTerm->op==pSel->op ){
144753 SrcList *pSrc = pRecTerm->pSrc;
144754 assert( pRecTerm->pPrior!=0 );
144755 for(i=0; i<pSrc->nSrc; i++){
144756 SrcItem *pItem = &pSrc->a[i];
144757 if( pItem->zDatabase==0
144758 && pItem->zName!=0
144759 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
144761 pItem->pTab = pTab;
144762 pTab->nTabRef++;
144763 pItem->fg.isRecursive = 1;
144764 if( pRecTerm->selFlags & SF_Recursive ){
144766 "multiple references to recursive table: %s", pCte->zName
144770 pRecTerm->selFlags |= SF_Recursive;
144771 if( iRecTab<0 ) iRecTab = pParse->nTab++;
144772 pItem->iCursor = iRecTab;
144775 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
144776 pRecTerm = pRecTerm->pPrior;
144779 pCte->zCteErr = "circular reference: %s";
144780 pSavedWith = pParse->pWith;
144781 pParse->pWith = pWith;
144782 if( pSel->selFlags & SF_Recursive ){
144785 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
144786 assert( pRecTerm->pNext!=0 );
144787 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
144788 assert( pRecTerm->pWith==0 );
144789 pRecTerm->pWith = pSel->pWith;
144791 pRecTerm->pWith = 0;
144793 pParse->pWith = pSavedWith;
144798 pParse->pWith = pSavedWith;
144802 pParse->pWith = pWith;
144804 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
144805 pEList = pLeft->pEList;
144806 if( pCte->pCols ){
144807 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
144809 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
144811 pParse->pWith = pSavedWith;
144814 pEList = pCte->pCols;
144817 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
144819 if( pSel->selFlags & SF_Recursive ){
144820 pCte->zCteErr = "multiple recursive references: %s";
144822 pCte->zCteErr = "recursive reference in a subquery: %s";
144826 pCte->zCteErr = 0;
144827 pParse->pWith = pSavedWith;
144844 Parse *pParse = pWalker->pParse;
144845 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
144846 With *pWith = findRightmost(p)->pWith;
144848 assert( pParse->pWith==pWith || pParse->nErr );
144849 pParse->pWith = pWith->pOuter;
144857 ** sub-query in the FROM clause of a SELECT statement. This function
144863 Select *pSel = pFrom->pSelect;
144867 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
144869 pTab->nTabRef = 1;
144870 if( pFrom->zAlias ){
144871 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
144873 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
144875 while( pSel->pPrior ){ pSel = pSel->pPrior; }
144876 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
144877 pTab->iPKey = -1;
144878 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
144880 /* The usual case - do not allow ROWID on a subquery */
144881 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
144883 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
144885 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
144904 N--;
144906 if( pBase->fg.isUsing==0 ) continue;
144907 if( NEVER(pBase->u3.pUsing==0) ) continue;
144908 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
144921 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
144923 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
144939 Parse *pParse = pWalker->pParse;
144944 sqlite3 *db = pParse->db;
144946 u16 selFlags = p->selFlags;
144949 p->selFlags |= SF_Expanded;
144950 if( db->mallocFailed ){
144953 assert( p->pSrc!=0 );
144957 if( pWalker->eCode ){
144959 p->selId = ++pParse->nSelect;
144961 pTabList = p->pSrc;
144962 pEList = p->pEList;
144963 if( pParse->pWith && (p->selFlags & SF_View) ){
144964 if( p->pWith==0 ){
144965 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
144966 if( p->pWith==0 ){
144970 p->pWith->bView = 1;
144972 sqlite3WithPush(pParse, p->pWith, 0);
144983 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144985 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
144986 if( pFrom->pTab ) continue;
144987 assert( pFrom->fg.isRecursive==0 );
144988 if( pFrom->zName==0 ){
144990 Select *pSel = pFrom->pSelect;
144991 /* A sub-query in the FROM clause of a SELECT */
144993 assert( pFrom->pTab==0 );
145000 pTab = pFrom->pTab;
145005 assert( pFrom->pTab==0 );
145006 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
145008 if( pTab->nTabRef>=0xffff ){
145010 pTab->zName);
145011 pFrom->pTab = 0;
145014 pTab->nTabRef++;
145021 u8 eCodeOrig = pWalker->eCode;
145023 assert( pFrom->pSelect==0 );
145025 if( (db->flags & SQLITE_EnableView)==0
145026 && pTab->pSchema!=db->aDb[1].pSchema
145029 pTab->zName);
145031 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
145035 && pFrom->fg.fromDDL
145036 && ALWAYS(pTab->u.vtab.p!=0)
145037 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
145040 pTab->zName);
145044 nCol = pTab->nCol;
145045 pTab->nCol = -1;
145046 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
145047 sqlite3WalkSelect(pWalker, pFrom->pSelect);
145048 pWalker->eCode = eCodeOrig;
145049 pTab->nCol = nCol;
145055 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
145062 assert( db->mallocFailed==0 || pParse->nErr!=0 );
145063 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
145078 for(k=0; k<pEList->nExpr; k++){
145079 pE = pEList->a[k].pExpr;
145080 if( pE->op==TK_ASTERISK ) break;
145081 assert( pE->op!=TK_DOT || pE->pRight!=0 );
145082 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
145083 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
145084 elistFlags |= pE->flags;
145086 if( k<pEList->nExpr ){
145092 struct ExprList_item *a = pEList->a;
145094 int flags = pParse->db->flags;
145098 for(k=0; k<pEList->nExpr; k++){
145100 elistFlags |= pE->flags;
145101 pRight = pE->pRight;
145102 assert( pE->op!=TK_DOT || pRight!=0 );
145103 if( pE->op!=TK_ASTERISK
145104 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
145110 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
145111 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
145120 if( pE->op==TK_DOT ){
145121 assert( pE->pLeft!=0 );
145122 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
145123 zTName = pE->pLeft->u.zToken;
145125 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
145126 Table *pTab = pFrom->pTab; /* Table for this data source */
145127 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
145133 if( (zTabName = pFrom->zAlias)==0 ){
145134 zTabName = pTab->zName;
145136 if( db->mallocFailed ) break;
145137 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
145138 if( pFrom->fg.isNestedFrom ){
145139 assert( pFrom->pSelect!=0 );
145140 pNestedFrom = pFrom->pSelect->pEList;
145142 assert( pNestedFrom->nExpr==pTab->nCol );
145148 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
145149 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
145151 if( i+1<pTabList->nSrc
145157 for(ii=0; ii<pUsing->nId; ii++){
145158 const char *zUName = pUsing->a[ii].zName;
145162 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
145163 assert( pX->zEName==0 );
145164 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
145165 pX->fg.eEName = ENAME_TAB;
145166 pX->fg.bUsingTerm = 1;
145172 for(j=0; j<pTab->nCol; j++){
145173 char *zName = pTab->aCol[j].zCnName;
145179 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
145185 ** result-set list unless the SELECT has the SF_IncludeHidden
145188 if( (p->selFlags & SF_IncludeHidden)==0
145189 && IsHiddenColumn(&pTab->aCol[j])
145193 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
145202 if( pFrom->fg.isUsing
145203 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
145211 if( (pTabList->nSrc>1
145212 && ( (pFrom->fg.jointype & JT_LTORJ)==0
145214 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
145222 if( IN_RENAME_OBJECT && pE->pLeft ){
145223 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
145236 pX = &pNew->a[pNew->nExpr-1];
145237 assert( pX->zEName==0 );
145240 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
145241 testcase( pX->zEName==0 );
145243 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
145245 testcase( pX->zEName==0 );
145247 pX->fg.eEName = ENAME_TAB;
145248 if( (pFrom->fg.isUsing
145249 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
145251 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
145253 pX->fg.bNoExpand = 1;
145256 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
145257 pX->fg.eEName = ENAME_NAME;
145259 pX->zEName = sqlite3DbStrDup(db, zName);
145260 pX->fg.eEName = ENAME_NAME;
145274 p->pEList = pNew;
145276 if( p->pEList ){
145277 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
145282 p->selFlags |= SF_ComplexResult;
145287 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
145314 ** The calling function can detect the problem by looking at pParse->nErr
145315 ** and/or pParse->db->mallocFailed.
145321 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
145338 ** For each FROM-clause subquery, add Column.zType, Column.zColl, and
145353 assert( p->selFlags & SF_Resolved );
145354 if( p->selFlags & SF_HasTypeInfo ) return;
145355 p->selFlags |= SF_HasTypeInfo;
145356 pParse = pWalker->pParse;
145357 pTabList = p->pSrc;
145358 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
145359 Table *pTab = pFrom->pTab;
145361 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
145362 /* A sub-query in the FROM clause of a SELECT */
145363 Select *pSel = pFrom->pSelect;
145375 ** the Table structures of all FROM-clause subqueries in a
145396 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
145397 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
145409 assert( p!=0 || pParse->db->mallocFailed );
145410 assert( pParse->db->pParse==pParse );
145411 if( pParse->db->mallocFailed ) return;
145412 if( p->selFlags & SF_HasTypeInfo ) return;
145414 if( pParse->nErr ) return;
145416 if( pParse->nErr ) return;
145426 for(ii=0; ii<pAggInfo->nColumn; ii++){
145427 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
145429 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
145431 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
145432 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
145433 pCol->iSorterColumn,
145434 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
145435 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
145437 for(ii=0; ii<pAggInfo->nFunc; ii++){
145438 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
145439 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
145440 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
145446 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
145468 assert( pAggInfo->iFirstReg==0 );
145469 pNC->ncFlags |= NC_InAggFunc;
145470 for(i=0; i<pAggInfo->nFunc; i++){
145471 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
145473 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
145477 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
145481 pNC->ncFlags &= ~NC_InAggFunc;
145495 NameContext *pNC /* Name context used to resolve agg-func args */
145497 assert( pAggInfo->iFirstReg==0 );
145499 assert( pSelect->pGroupBy!=0 );
145500 pAggInfo->nColumn = pAggInfo->nAccumulator;
145501 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
145502 int mx = pSelect->pGroupBy->nExpr - 1;
145504 for(j=0; j<pAggInfo->nColumn; j++){
145505 k = pAggInfo->aCol[j].iSorterColumn;
145508 pAggInfo->nSortingColumn = mx+1;
145517 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
145518 printf("data-cursor=%d index={%d,%d}\n",
145519 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
145520 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
145537 if( pExpr->pAggInfo==0 ) return WRC_Continue;
145538 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
145539 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
145540 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
145541 pAggInfo = pExpr->pAggInfo;
145542 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue;
145543 assert( pExpr->iAgg>=0 );
145544 pCol = &pAggInfo->aCol[pExpr->iAgg];
145545 pExpr->op = TK_AGG_COLUMN;
145546 pExpr->iTable = pCol->iTable;
145547 pExpr->iColumn = pCol->iColumn;
145553 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
145562 for(i=0; i<pAggInfo->nFunc; i++){
145563 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
145570 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
145571 ** register in this block is stored in pAggInfo->iFirstReg.
145587 assert( pAggInfo->iFirstReg==0 );
145588 pAggInfo->iFirstReg = pParse->nMem + 1;
145589 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
145601 Vdbe *v = pParse->pVdbe;
145604 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
145605 assert( pAggInfo->iFirstReg>0 );
145606 assert( pParse->db->pParse==pParse );
145607 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
145609 if( pParse->nErr ) return;
145610 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
145611 pAggInfo->iFirstReg+nReg-1);
145612 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
145613 if( pFunc->iDistinct>=0 ){
145614 Expr *pE = pFunc->pFExpr;
145616 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
145619 pFunc->iDistinct = -1;
145621 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
145622 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
145623 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
145624 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
145625 pFunc->pFunc->zName));
145636 Vdbe *v = pParse->pVdbe;
145639 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
145641 assert( ExprUseXList(pF->pFExpr) );
145642 pList = pF->pFExpr->x.pList;
145644 pList ? pList->nExpr : 0);
145645 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
145654 ** If regAcc is non-zero and there are no min() or max() aggregates
145655 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
145665 Vdbe *v = pParse->pVdbe;
145672 assert( pAggInfo->iFirstReg>0 );
145673 if( pParse->nErr ) return;
145674 pAggInfo->directMode = 1;
145675 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
145680 assert( ExprUseXList(pF->pFExpr) );
145681 assert( !IsWindowFunc(pF->pFExpr) );
145682 pList = pF->pFExpr->x.pList;
145683 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
145684 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
145685 if( pAggInfo->nAccumulator
145686 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
145692 if( regHit==0 ) regHit = ++pParse->nMem;
145706 nArg = pList->nExpr;
145713 if( pF->iDistinct>=0 && pList ){
145717 pF->iDistinct = codeDistinct(pParse, eDistinctType,
145718 pF->iDistinct, addrNext, pList, regAgg);
145720 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
145724 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
145725 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
145726 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
145729 pColl = pParse->db->pDfltColl;
145731 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
145735 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
145742 if( regHit==0 && pAggInfo->nAccumulator ){
145748 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
145749 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
145752 pAggInfo->directMode = 0;
145768 if( pParse->explain==2 ){
145771 pTab->zName,
145773 bCover ? pIdx->zName : ""
145788 ** sub-expression matches the criteria for being moved to the WHERE
145789 ** clause. If so, add it to the WHERE clause and replace the sub-expression
145793 if( pExpr->op!=TK_AND ){
145794 Select *pS = pWalker->u.pSelect;
145796 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
145802 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
145804 && pExpr->pAggInfo==0
145806 sqlite3 *db = pWalker->pParse->db;
145809 Expr *pWhere = pS->pWhere;
145811 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
145812 pS->pWhere = pNew;
145813 pWalker->eCode = 1;
145842 sqlite3WalkExpr(&sWalker, p->pHaving);
145852 ** Check to see if the pThis entry of pTabList is a self-join of another view.
145853 ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
145856 ** If pThis is a self-join, then return the SrcItem for the first other
145857 ** instance of that view found. If pThis is not a self-join then return 0.
145860 SrcList *pTabList, /* Search for self-joins in this FROM clause */
145862 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
145865 assert( pThis->pSelect!=0 );
145866 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
145869 pItem = &pTabList->a[iFirst++];
145870 if( pItem->pSelect==0 ) continue;
145871 if( pItem->fg.viaCoroutine ) continue;
145872 if( pItem->zName==0 ) continue;
145873 assert( pItem->pTab!=0 );
145874 assert( pThis->pTab!=0 );
145875 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
145876 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
145877 pS1 = pItem->pSelect;
145878 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
145883 if( pItem->pSelect->selFlags & SF_PushDown ){
145897 sqlite3DbFree(db, p->aCol);
145898 sqlite3DbFree(db, p->aFunc);
145926 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
145927 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
145928 if( p->pWhere ) return 0;
145929 if( p->pHaving ) return 0;
145930 if( p->pGroupBy ) return 0;
145931 if( p->pOrderBy ) return 0;
145932 pExpr = p->pEList->a[0].pExpr;
145933 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
145935 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
145937 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
145938 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
145940 pSub = p->pSrc->a[0].pSelect;
145942 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
145943 if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */
145945 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
145946 if( pSub->pWhere ) return 0; /* No WHERE clause */
145947 if( pSub->pLimit ) return 0; /* No LIMIT clause */
145948 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
145949 assert( pSub->pHaving==0 ); /* Due to the previous */
145950 pSub = pSub->pPrior; /* Repeat over compound */
145955 db = pParse->db;
145958 pSub = p->pSrc->a[0].pSelect;
145959 p->pSrc->a[0].pSelect = 0;
145960 sqlite3SrcListDelete(db, p->pSrc);
145961 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
145964 pPrior = pSub->pPrior;
145965 pSub->pPrior = 0;
145966 pSub->pNext = 0;
145967 pSub->selFlags |= SF_Aggregate;
145968 pSub->selFlags &= ~SF_Compound;
145969 pSub->nSelectRow = 0;
145970 sqlite3ExprListDelete(db, pSub->pEList);
145972 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
145982 p->pEList->a[0].pExpr = pExpr;
145983 p->selFlags &= ~SF_Aggregate;
145987 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
145995 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
146001 for(i=0; i<pSrc->nSrc; i++){
146002 SrcItem *p1 = &pSrc->a[i];
146004 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
146007 if( p1->pSelect
146008 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
146009 && sameSrcAlias(p0, p1->pSelect->pSrc)
146018 ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
146019 ** be implemented as a co-routine. The i-th entry is guaranteed to be
146022 ** The subquery is implemented as a co-routine if all of the following are
146029 ** (b) The subquery is the left-most term and a CROSS JOIN or similar
146032 ** (i) The subquery is the left-most subquery in the FROM clause
146043 ** (5) The subquery is not self-joined
146051 SrcItem *pItem = &pTabList->a[i];
146052 if( pItem->fg.isCte ){
146053 const CteUse *pCteUse = pItem->u2.pCteUse;
146054 if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */
146055 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */
146057 if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */
146058 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */
146059 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
146063 if( pTabList->nSrc==1 ) return 1; /* (1a) */
146064 if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */
146065 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
146068 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
146069 while( 1 /*exit-by-break*/ ){
146070 if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */
146072 i--;
146073 pItem--;
146074 if( pItem->pSelect!=0 ) return 0; /* (1c-i) */
146087 ** pParse->zErrMsg.
146115 db = pParse->db;
146116 assert( pParse==db->pParse );
146118 if( p==0 || pParse->nErr ){
146121 assert( db->mallocFailed==0 );
146124 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
146134 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
146135 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
146136 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
146137 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
146139 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
146140 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
146141 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
146143 if( p->pOrderBy ){
146147 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
146152 p->pOrderBy);
146153 testcase( pParse->earlyCleanup );
146154 p->pOrderBy = 0;
146156 p->selFlags &= ~SF_Distinct;
146157 p->selFlags |= SF_NoopOrderBy;
146160 if( pParse->nErr ){
146163 assert( db->mallocFailed==0 );
146164 assert( p->pEList!=0 );
146174 ** In this case, it is an error if the target object (pSrc->a[0]) name
146175 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
146181 if( p->selFlags & SF_UFSrcCheck ){
146182 SrcItem *p0 = &p->pSrc->a[0];
146183 if( sameSrcAlias(p0, p->pSrc) ){
146186 p0->zAlias ? p0->zAlias : p0->pTab->zName
146192 ** and leaving this flag set can cause errors if a compound sub-query
146193 ** in p->pSrc is flattened into this query and this function called
146195 p->selFlags &= ~SF_UFSrcCheck;
146198 if( pDest->eDest==SRT_Output ){
146204 assert( pParse->nErr );
146208 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
146214 pTabList = p->pSrc;
146215 isAgg = (p->selFlags & SF_Aggregate)!=0;
146217 sSort.pOrderBy = p->pOrderBy;
146223 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
146224 SrcItem *pItem = &pTabList->a[i];
146225 Select *pSub = pItem->pSelect;
146226 Table *pTab = pItem->pTab;
146236 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
146237 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
146241 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
146242 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
146243 assert( pItem->iCursor>=0 );
146244 unsetJoinExpr(p->pWhere, pItem->iCursor,
146245 pTabList->a[0].fg.jointype & JT_LTORJ);
146253 if( pTab->nCol!=pSub->pEList->nExpr ){
146255 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
146263 ** will be implemented as a co-routine and there is no advantage to
146266 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
146267 assert( pSub->pGroupBy==0 );
146269 /* If a FROM-clause subquery has an ORDER BY clause that is not
146278 ** (2) The subquery was added to help with window-function
146282 ** the built-in count(), min(), or max().
146289 if( pSub->pOrderBy!=0
146290 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
146291 && pSub->pLimit==0 /* Condition (1) */
146292 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
146293 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
146297 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
146300 pSub->pOrderBy);
146301 pSub->pOrderBy = 0;
146307 ** it will be implemented as a co-routine, then do not flatten. This
146322 if( pSub->pOrderBy!=0
146324 && (p->selFlags & SF_ComplexResult)!=0
146325 && (pTabList->nSrc==1
146326 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
146332 if( pParse->nErr ) goto select_end;
146334 i = -1;
146336 pTabList = p->pSrc;
146337 if( db->mallocFailed ) goto select_end;
146339 sSort.pOrderBy = p->pOrderBy;
146348 if( p->pPrior ){
146351 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
146356 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
146361 /* Do the WHERE-clause constant propagation optimization if this is
146362 ** a join. No need to speed time on this operation for non-join queries
146366 if( p->pWhere!=0
146367 && p->pWhere->op==TK_AND
146384 if( db->mallocFailed ) goto select_end;
146385 pTabList = p->pSrc;
146390 ** (2) Generate code for all sub-queries
146392 for(i=0; i<pTabList->nSrc; i++){
146393 SrcItem *pItem = &pTabList->a[i];
146406 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
146407 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
146414 ** assume the column name is non-NULL and segfault. The use of an empty
146417 if( pItem->colUsed==0 && pItem->zName!=0 ){
146418 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
146422 /* Generate code for all sub-queries in the FROM clause
146424 pSub = pItem->pSelect;
146428 assert( pItem->addrFillSub==0 );
146433 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
146437 pParse->nHeight += sqlite3SelectExprHeight(p);
146439 /* Make copies of constant WHERE-clause terms in the outer query down
146443 && (pItem->fg.isCte==0
146444 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
146445 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
146450 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
146454 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
146456 TREETRACE(0x4000,pParse,p,("Push-down not possible\n"));
146469 pSub->selId));
146475 zSavedAuthContext = pParse->zAuthContext;
146476 pParse->zAuthContext = pItem->zName;
146480 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
146481 /* Implement a co-routine that will return a single row of the result
146486 pItem->regReturn = ++pParse->nMem;
146487 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
146489 pItem->addrFillSub = addrTop;
146490 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
146491 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
146493 pItem->pTab->nRowLogEst = pSub->nSelectRow;
146494 pItem->fg.viaCoroutine = 1;
146495 pItem->regResult = dest.iSdst;
146496 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
146497 sqlite3VdbeJumpHere(v, addrTop-1);
146499 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
146502 ** the make the pItem->iCursor be a copy of the ephemerial table that
146504 CteUse *pCteUse = pItem->u2.pCteUse;
146505 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
146506 if( pItem->iCursor!=pCteUse->iCur ){
146507 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
146510 pSub->nSelectRow = pCteUse->nRowEst;
146514 if( pPrior->addrFillSub ){
146515 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
146517 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
146518 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
146529 pItem->regReturn = ++pParse->nMem;
146531 pItem->addrFillSub = topAddr+1;
146532 pItem->fg.isMaterialized = 1;
146533 if( pItem->fg.isCorrelated==0 ){
146542 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
146546 pItem->pTab->nRowLogEst = pSub->nSelectRow;
146548 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
146550 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
146553 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
146554 CteUse *pCteUse = pItem->u2.pCteUse;
146555 pCteUse->addrM9e = pItem->addrFillSub;
146556 pCteUse->regRtn = pItem->regReturn;
146557 pCteUse->iCur = pItem->iCursor;
146558 pCteUse->nRowEst = pSub->nSelectRow;
146561 if( db->mallocFailed ) goto select_end;
146562 pParse->nHeight -= sqlite3SelectExprHeight(p);
146563 pParse->zAuthContext = zSavedAuthContext;
146569 pEList = p->pEList;
146570 pWhere = p->pWhere;
146571 pGroupBy = p->pGroupBy;
146572 pHaving = p->pHaving;
146573 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
146577 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
146583 ** if the select-list is the same as the ORDER BY list, then this query
146592 ** The second form is preferred as a single index (or temp-table) may be
146594 ** written the query must use a temp-table for at least one of the ORDER
146595 ** BY and DISTINCT, and an index or separate temp-table for the other.
146597 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
146598 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
146600 && p->pWin==0
146603 p->selFlags &= ~SF_Distinct;
146604 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
146605 p->selFlags |= SF_Aggregate;
146606 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
146622 ** being unused if the data can be extracted in pre-sorted order.
146631 pParse, sSort.pOrderBy, 0, pEList->nExpr);
146632 sSort.iECursor = pParse->nTab++;
146635 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
146639 sSort.addrSortIndex = -1;
146644 if( pDest->eDest==SRT_EphemTab ){
146645 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
146646 if( p->selFlags & SF_NestedFrom ){
146647 /* Delete or NULL-out result columns that will never be used */
146649 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
146650 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
146651 sqlite3DbFree(db, pEList->a[ii].zEName);
146652 pEList->nExpr--;
146654 for(ii=0; ii<pEList->nExpr; ii++){
146655 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
146663 if( (p->selFlags & SF_FixedLimit)==0 ){
146664 p->nSelectRow = 320; /* 4 billion rows */
146666 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
146667 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
146674 if( p->selFlags & SF_Distinct ){
146675 sDistinct.tabTnct = pParse->nTab++;
146678 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
146689 | (p->selFlags & SF_FixedLimit);
146691 Window *pWin = p->pWin; /* Main window object (or NULL) */
146702 p->pEList, p, wctrlFlags, p->nSelectRow);
146704 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
146705 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
146713 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
146727 assert( p->pEList==pEList );
146733 int regGosub = ++pParse->nMem;
146739 VdbeNoopComment((v, "inner-loop subroutine"));
146741 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
146744 VdbeComment((v, "end inner-loop subroutine"));
146750 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
146782 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
146783 pItem->u.x.iAlias = 0;
146785 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
146786 pItem->u.x.iAlias = 0;
146789 if( p->nSelectRow>66 ) p->nSelectRow = 66;
146794 ** in the correct order. It also may not - the GROUP BY might use a
146799 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
146802 ** ASC or DESC order - only that each group is returned contiguously.
146806 for(ii=0; ii<pGroupBy->nExpr; ii++){
146808 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
146809 pGroupBy->a[ii].fg.sortFlags = sortFlags;
146811 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
146817 p->nSelectRow = 0;
146831 testcase( pParse->earlyCleanup );
146833 if( db->mallocFailed ){
146836 pAggInfo->selId = p->selId;
146838 pAggInfo->pSelect = p;
146845 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
146846 pAggInfo->pGroupBy = pGroupBy;
146851 assert( pWhere==p->pWhere );
146852 assert( pHaving==p->pHaving );
146853 assert( pGroupBy==p->pGroupBy );
146855 pWhere = p->pWhere;
146859 pAggInfo->nAccumulator = pAggInfo->nColumn;
146860 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
146861 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
146866 if( db->mallocFailed ) goto select_end;
146885 int addr1; /* A-vs-B comparision jump */
146897 if( pAggInfo->nFunc==1
146898 && pAggInfo->aFunc[0].iDistinct>=0
146899 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
146900 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
146901 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
146903 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
146915 pAggInfo->sortingIdx = pParse->nTab++;
146917 0, pAggInfo->nColumn);
146919 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
146924 iUseFlag = ++pParse->nMem;
146925 iAbortFlag = ++pParse->nMem;
146926 regOutputRow = ++pParse->nMem;
146928 regReset = ++pParse->nMem;
146930 iAMem = pParse->nMem + 1;
146931 pParse->nMem += pGroupBy->nExpr;
146932 iBMem = pParse->nMem + 1;
146933 pParse->nMem += pGroupBy->nExpr;
146936 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
146953 if( pParse->pIdxEpr ){
146959 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
146977 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
146981 nGroupBy = pGroupBy->nExpr;
146984 for(i=0; i<pAggInfo->nColumn; i++){
146985 if( pAggInfo->aCol[i].iSorterColumn>=j ){
146993 pAggInfo->directMode = 1;
146994 for(i=0; i<pAggInfo->nColumn; i++){
146995 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
146996 if( pCol->iSorterColumn>=j ){
146997 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
147001 pAggInfo->directMode = 0;
147004 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
147009 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
147012 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
147014 pAggInfo->useSortingIdx = 1;
147017 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
147022 if( pParse->pIdxEpr ){
147038 ** This is an optimization - the correct answer should result regardless.
147055 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
147058 for(j=0; j<pGroupBy->nExpr; j++){
147062 pAggInfo->directMode = 1;
147063 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
147066 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
147080 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
147099 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
147119 ** is less than or equal to zero, the subroutine is a no-op. If
147136 selectInnerLoop(pParse, p, -1, &sSort,
147142 /* Generate a subroutine that will reset the group-by accumulator
147151 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
147152 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
147171 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
147172 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
147176 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
147179 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
147183 ** (2011-04-15) Do not do a full scan of an unordered index.
147185 ** (2013-10-03) Do not count the entries in a partial index.
147191 if( !p->pSrc->a[0].fg.notIndexed ){
147192 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
147193 if( pIdx->bUnordered==0
147194 && pIdx->szIdxRow<pTab->szTabRow
147195 && pIdx->pPartIdxWhere==0
147196 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
147203 iRoot = pBest->tnum;
147207 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
147210 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
147231 if( pAggInfo->nAccumulator ){
147232 for(i=0; i<pAggInfo->nFunc; i++){
147233 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
147236 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
147240 if( i==pAggInfo->nFunc ){
147241 regAcc = ++pParse->nMem;
147244 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
147245 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
147246 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
147255 assert( p->pGroupBy==0 );
147264 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
147276 struct AggInfo_func *pF = pAggInfo->aFunc;
147278 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
147293 selectInnerLoop(pParse, p, -1, 0, 0,
147308 assert( p->pEList==pEList );
147309 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
147318 rc = (pParse->nErr>0);
147324 assert( db->mallocFailed==0 || db->mallocFailed==1 );
147325 assert( db->mallocFailed==0 || pParse->nErr!=0 );
147328 if( pAggInfo && !db->mallocFailed ){
147329 for(i=0; i<pAggInfo->nColumn; i++){
147330 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
147332 assert( pExpr->pAggInfo==pAggInfo );
147333 assert( pExpr->iAgg==i );
147335 for(i=0; i<pAggInfo->nFunc; i++){
147336 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
147338 assert( pExpr->pAggInfo==pAggInfo );
147339 assert( pExpr->iAgg==i );
147399 int need; /* Slots needed in p->azResult[] */
147403 /* Make sure there is enough space in p->azResult to hold everything
147406 if( p->nRow==0 && argv!=0 ){
147411 if( p->nData + need > p->nAlloc ){
147413 p->nAlloc = p->nAlloc*2 + need;
147414 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
147416 p->azResult = azNew;
147422 if( p->nRow==0 ){
147423 p->nColumn = nCol;
147427 p->azResult[p->nData++] = z;
147429 }else if( (int)p->nColumn!=nCol ){
147430 sqlite3_free(p->zErrMsg);
147431 p->zErrMsg = sqlite3_mprintf(
147434 p->rc = SQLITE_ERROR;
147450 p->azResult[p->nData++] = z;
147452 p->nRow++;
147457 p->rc = SQLITE_NOMEM_BKPT;
147497 db->errCode = SQLITE_NOMEM;
147513 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
147526 db->errCode = SQLITE_NOMEM;
147545 azResult--;
147578 pTriggerStep = pTriggerStep->pNext;
147580 sqlite3ExprDelete(db, pTmp->pWhere);
147581 sqlite3ExprListDelete(db, pTmp->pExprList);
147582 sqlite3SelectDelete(db, pTmp->pSelect);
147583 sqlite3IdListDelete(db, pTmp->pIdList);
147584 sqlite3UpsertDelete(db, pTmp->pUpsert);
147585 sqlite3SrcListDelete(db, pTmp->pFrom);
147586 sqlite3DbFree(db, pTmp->zSpan);
147597 ** are already attached to pTab->pTrigger. But there might be additional
147599 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
147604 ** pTab as well as the triggers lised in pTab->pTrigger.
147611 assert( pParse->disableTriggers==0 );
147612 pTmpSchema = pParse->db->aDb[1].pSchema;
147613 p = sqliteHashFirst(&pTmpSchema->trigHash);
147614 pList = pTab->pTrigger;
147617 if( pTrig->pTabSchema==pTab->pSchema
147618 && pTrig->table
147619 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
147620 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
147622 pTrig->pNext = pList;
147624 }else if( pTrig->op==TK_RETURNING ){
147626 assert( pParse->db->pVtabCtx==0 );
147628 assert( pParse->bReturning );
147629 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
147630 pTrig->table = pTab->zName;
147631 pTrig->pTabSchema = pTab->pSchema;
147632 pTrig->pNext = pList;
147640 printf("Triggers for %s:", pTab->zName);
147641 for(pX=pList; pX; pX=pX->pNext){
147642 printf(" %s", pX->zName);
147655 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
147674 sqlite3 *db = pParse->db; /* The database connection */
147679 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
147685 if( pName2->n>0 ){
147698 if( !pTableName || db->mallocFailed ){
147702 /* A long-standing parser bug is that this syntax was allowed:
147710 if( db->init.busy && iDb!=1 ){
147711 sqlite3DbFree(db, pTableName->a[0].zDatabase);
147712 pTableName->a[0].zDatabase = 0;
147721 if( db->init.busy==0 && pName2->n==0 && pTab
147722 && pTab->pSchema==db->aDb[1].pSchema ){
147727 if( db->mallocFailed ) goto trigger_cleanup;
147728 assert( pTableName->nSrc==1 );
147747 assert( db->mallocFailed );
147750 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
147755 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
147759 assert( !db->init.busy );
147761 VVA_ONLY( pParse->ifNotExists = 1; )
147768 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
147778 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
147783 " trigger on table: %S", pTableName->a);
147789 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147791 const char *zDb = db->aDb[iTabDb].zDbSName;
147792 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
147794 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
147815 pTrigger->zName = zName;
147817 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
147818 pTrigger->pSchema = db->aDb[iDb].pSchema;
147819 pTrigger->pTabSchema = pTab->pSchema;
147820 pTrigger->op = (u8)op;
147821 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
147823 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
147824 pTrigger->pWhen = pWhen;
147827 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
147829 pTrigger->pColumns = pColumns;
147831 assert( pParse->pNewTrigger==0 );
147832 pParse->pNewTrigger = pTrigger;
147839 if( !pParse->pNewTrigger ){
147842 assert( pParse->pNewTrigger==pTrigger );
147847 if( db->init.iDb==1 ){
147850 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
147854 ** "orphaned trigger" - a trigger whose associated table is missing.
147856 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
147858 db->init.orphanTrigger = 1;
147872 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
147874 sqlite3 *db = pParse->db; /* The database */
147879 pParse->pNewTrigger = 0;
147880 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
147881 zName = pTrig->zName;
147882 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
147883 pTrig->step_list = pStepList;
147885 pStepList->pTrig = pTrig;
147886 pStepList = pStepList->pNext;
147888 sqlite3TokenInit(&nameToken, pTrig->zName);
147890 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
147891 || sqlite3FixExpr(&sFix, pTrig->pWhen)
147898 assert( !db->init.busy );
147899 pParse->pNewTrigger = pTrig;
147907 if( !db->init.busy ){
147912 ** are read-only, and the trigger makes a change to a shadow table,
147913 ** then raise an error - do not allow the trigger to be created. */
147916 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
147917 if( pStep->zTarget!=0
147918 && sqlite3ShadowTableName(db, pStep->zTarget)
147922 pTrig->zName, pStep->zTarget);
147932 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
147937 db->aDb[iDb].zDbSName, zName,
147938 pTrig->table, z);
147945 if( db->init.busy ){
147947 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
147953 }else if( pLink->pSchema==pLink->pTabSchema ){
147955 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
147957 pLink->pNext = pTab->pTrigger;
147958 pTab->pTrigger = pLink;
147964 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
147997 pTriggerStep->op = TK_SELECT;
147998 pTriggerStep->pSelect = pSelect;
147999 pTriggerStep->orconf = OE_Default;
148000 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
148008 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
148017 sqlite3 *db = pParse->db;
148020 if( pParse->nErr ) return 0;
148021 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
148024 memcpy(z, pName->z, pName->n);
148026 pTriggerStep->zTarget = z;
148027 pTriggerStep->op = op;
148028 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
148030 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
148053 sqlite3 *db = pParse->db;
148056 assert(pSelect != 0 || db->mallocFailed);
148061 pTriggerStep->pSelect = pSelect;
148064 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
148066 pTriggerStep->pIdList = pColumn;
148067 pTriggerStep->pUpsert = pUpsert;
148068 pTriggerStep->orconf = orconf;
148070 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
148091 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
148098 sqlite3 *db = pParse->db;
148104 pTriggerStep->pExprList = pEList;
148105 pTriggerStep->pWhere = pWhere;
148106 pTriggerStep->pFrom = pFrom;
148111 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
148112 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
148113 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
148115 pTriggerStep->orconf = orconf;
148135 sqlite3 *db = pParse->db;
148141 pTriggerStep->pWhere = pWhere;
148144 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
148146 pTriggerStep->orconf = OE_Default;
148156 if( pTrigger==0 || pTrigger->bReturning ) return;
148157 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
148158 sqlite3DbFree(db, pTrigger->zName);
148159 sqlite3DbFree(db, pTrigger->table);
148160 sqlite3ExprDelete(db, pTrigger->pWhen);
148161 sqlite3IdListDelete(db, pTrigger->pColumns);
148178 sqlite3 *db = pParse->db;
148180 if( db->mallocFailed ) goto drop_trigger_cleanup;
148185 assert( pName->nSrc==1 );
148186 zDb = pName->a[0].zDatabase;
148187 zName = pName->a[0].zName;
148189 for(i=OMIT_TEMPDB; i<db->nDb; i++){
148193 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
148198 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
148202 pParse->checkSchema = 1;
148216 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
148226 sqlite3 *db = pParse->db;
148229 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
148230 assert( iDb>=0 && iDb<db->nDb );
148232 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
148236 const char *zDb = db->aDb[iDb].zDbSName;
148239 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
148251 db->aDb[iDb].zDbSName, pTrigger->zName
148254 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
148266 pHash = &(db->aDb[iDb].pSchema->trigHash);
148269 if( pTrigger->pSchema==pTrigger->pTabSchema ){
148273 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
148275 *pp = (*pp)->pNext;
148282 db->mDbFlags |= DBFLAG_SchemaChange;
148298 for(e=0; e<pEList->nExpr; e++){
148299 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
148308 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
148309 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
148332 || (pList->bReturning && pList->pNext==0) );
148335 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
148336 && pTab->pTrigger!=0
148341 if( pList==pTab->pTrigger ){
148345 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
148346 p->pNext = 0;
148350 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
148351 mask |= p->tr_tm;
148352 }else if( p->op==TK_RETURNING ){
148356 p->op = op;
148363 p->tr_tm = TRIGGER_BEFORE;
148365 p->tr_tm = TRIGGER_AFTER;
148367 mask |= p->tr_tm;
148368 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
148371 mask |= p->tr_tm;
148373 p = p->pNext;
148390 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
148391 || pParse->disableTriggers
148400 ** Convert the pStep->zTarget string into a SrcList and return a pointer
148413 sqlite3 *db = pParse->db;
148415 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
148417 assert( pSrc==0 || pSrc->nSrc==1 );
148420 Schema *pSchema = pStep->pTrig->pSchema;
148421 pSrc->a[0].zName = zName;
148422 if( pSchema!=db->aDb[1].pSchema ){
148423 pSrc->a[0].pSchema = pSchema;
148425 if( pStep->pFrom ){
148426 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
148427 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
148453 if( pTerm->op==TK_ASTERISK ) return 1;
148454 if( pTerm->op!=TK_DOT ) return 0;
148455 assert( pTerm->pRight!=0 );
148456 assert( pTerm->pLeft!=0 );
148457 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
148474 sqlite3 *db = pParse->db;
148477 for(i=0; i<pList->nExpr; i++){
148478 Expr *pOldExpr = pList->a[i].pExpr;
148482 for(jj=0; jj<pTab->nCol; jj++){
148484 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
148485 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
148487 if( !db->mallocFailed ){
148488 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
148489 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
148490 pItem->fg.eEName = ENAME_NAME;
148496 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
148497 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
148498 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
148499 pItem->fg.eEName = pList->a[i].fg.eEName;
148509 ** is generated in-line.
148517 Vdbe *v = pParse->pVdbe;
148518 sqlite3 *db = pParse->db;
148525 assert( pParse->bReturning );
148526 assert( db->pParse==pParse );
148527 pReturning = pParse->u1.pReturning;
148528 assert( pTrigger == &(pReturning->retTrig) );
148531 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
148535 sFrom.a[0].iCursor = -1;
148537 if( pParse->nErr==0 ){
148538 assert( db->mallocFailed==0 );
148542 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
148543 if( pParse->nErr==0 ){
148546 if( pReturning->nRetCol==0 ){
148547 pReturning->nRetCol = pNew->nExpr;
148548 pReturning->iRetCur = pParse->nTab++;
148553 pParse->eTriggerOp = pTrigger->op;
148554 pParse->pTriggerTab = pTab;
148556 && ALWAYS(!db->mallocFailed)
148559 int nCol = pNew->nExpr;
148560 int reg = pParse->nMem+1;
148561 pParse->nMem += nCol+2;
148562 pReturning->iRetReg = reg;
148564 Expr *pCol = pNew->a[i].pExpr;
148565 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
148572 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
148573 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
148577 pParse->eTriggerOp = 0;
148578 pParse->pTriggerTab = 0;
148593 Vdbe *v = pParse->pVdbe;
148594 sqlite3 *db = pParse->db;
148596 assert( pParse->pTriggerTab && pParse->pToplevel );
148599 for(pStep=pStepList; pStep; pStep=pStep->pNext){
148610 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
148611 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
148613 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
148614 assert( pParse->okConstFactor==0 );
148617 if( pStep->zSpan ){
148619 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
148624 switch( pStep->op ){
148628 sqlite3ExprListDup(db, pStep->pExprList, 0),
148629 sqlite3ExprDup(db, pStep->pWhere, 0),
148630 pParse->eOrconf, 0, 0, 0
148638 sqlite3SelectDup(db, pStep->pSelect, 0),
148639 sqlite3IdListDup(db, pStep->pIdList),
148640 pParse->eOrconf,
148641 sqlite3UpsertDup(db, pStep->pUpsert)
148649 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
148654 default: assert( pStep->op==TK_SELECT ); {
148656 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
148687 ** Parse context structure pFrom has just been used to create a sub-vdbe
148692 assert( pFrom->zErrMsg==0 || pFrom->nErr );
148693 assert( pTo->zErrMsg==0 || pTo->nErr );
148694 if( pTo->nErr==0 ){
148695 pTo->zErrMsg = pFrom->zErrMsg;
148696 pTo->nErr = pFrom->nErr;
148697 pTo->rc = pFrom->rc;
148699 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
148704 ** Create and populate a new TriggerPrg object with a sub-program
148714 sqlite3 *db = pParse->db; /* Database handle */
148718 NameContext sNC; /* Name context for sub-vdbe */
148719 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
148721 Parse sSubParse; /* Parse context for sub-vdbe */
148723 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
148724 assert( pTop->pVdbe );
148728 ** list of the top-level Parse object sooner rather than later. */
148731 pPrg->pNext = pTop->pTriggerPrg;
148732 pTop->pTriggerPrg = pPrg;
148733 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
148735 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
148736 pPrg->pTrigger = pTrigger;
148737 pPrg->orconf = orconf;
148738 pPrg->aColmask[0] = 0xffffffff;
148739 pPrg->aColmask[1] = 0xffffffff;
148742 ** trigger sub-program. */
148748 sSubParse.zAuthContext = pTrigger->zName;
148749 sSubParse.eTriggerOp = pTrigger->op;
148750 sSubParse.nQueryLoop = pParse->nQueryLoop;
148751 sSubParse.prepFlags = pParse->prepFlags;
148756 pTrigger->zName, onErrorText(orconf),
148757 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
148758 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
148759 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
148760 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
148761 pTab->zName
148764 if( pTrigger->zName ){
148765 sqlite3VdbeChangeP4(v, -1,
148766 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
148772 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
148774 if( pTrigger->pWhen ){
148775 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
148776 if( db->mallocFailed==0
148785 /* Code the trigger program into the sub-vdbe. */
148786 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
148788 /* Insert an OP_Halt at the end of the sub-program. */
148793 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
148796 if( pParse->nErr==0 ){
148797 assert( db->mallocFailed==0 );
148798 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
148800 pProgram->nMem = sSubParse.nMem;
148801 pProgram->nCsr = sSubParse.nTab;
148802 pProgram->token = (void *)pTrigger;
148803 pPrg->aColmask[0] = sSubParse.oldmask;
148804 pPrg->aColmask[1] = sSubParse.newmask;
148816 ** Return a pointer to a TriggerPrg object containing the sub-program for
148830 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
148836 for(pPrg=pRoot->pTriggerPrg;
148837 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
148838 pPrg=pPrg->pNext
148844 pParse->db->errByteOffset = -1;
148867 assert( pPrg || pParse->nErr );
148870 ** is a pointer to the sub-vdbe containing the trigger program. */
148872 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
148874 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
148875 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
148877 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
148879 /* Set the P5 operand of the OP_Program instruction to non-zero if
148881 ** invocation is disallowed if (a) the sub-program is really a trigger,
148896 ** operation on pTab, this function is a no-op.
148901 ** (a copy of pTab->nCol), then registers are populated as follows:
148904 ** ------------------------------------------------------
148906 ** reg+1 OLD.* value of left-most column of pTab
148908 ** reg+N OLD.* value of right-most column of pTab
148910 ** reg+N+2 NEW.* value of left-most column of pTab
148912 ** reg+N+N+1 NEW.* value of right-most column of pTab
148945 for(p=pTrigger; p; p=p->pNext){
148950 assert( p->pSchema!=0 );
148951 assert( p->pTabSchema!=0 );
148952 assert( p->pSchema==p->pTabSchema
148953 || p->pSchema==pParse->db->aDb[1].pSchema );
148960 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
148961 && p->tr_tm==tr_tm
148962 && checkColumnOverlap(p->pColumns, pChanges)
148964 if( !p->bReturning ){
148974 ** Triggers may access values stored in the old.* or new.* pseudo-table.
148975 ** This function returns a 32-bit bitmask indicating which columns of the
148980 ** Bit 0 of the returned mask is set if the left-most column of the
149015 for(p=pTrigger; p; p=p->pNext){
149016 if( p->op==op
149017 && (tr_tm&p->tr_tm)
149018 && checkColumnOverlap(p->pColumns,pChanges)
149020 if( p->bReturning ){
149026 mask |= pPrg->aColmask[isNew];
149071 ** i-th column of table pTab. This routine sets the P4 parameter of the
149077 ** command. If the latter, then the row-records in the table btree on disk
149080 ** If the former, then all row-records are guaranteed to include a value
149094 ** If column as REAL affinity and the table is an ordinary b-tree table
149102 assert( pTab->nCol>i );
149103 pCol = &pTab->aCol[i];
149104 if( pCol->iDflt ){
149108 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
149109 assert( i<pTab->nCol );
149112 pCol->affinity, &pValue);
149118 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
149127 ** and false if not. This is an optimization. False-positives are a
149128 ** performance degradation, but false-negatives can result in a corrupt
149131 ** aXRef[j] will be non-negative if column j of the original table is
149141 i16 iIdxCol = pIdx->aiColumn[iCol];
149147 assert( pIdx->aColExpr!=0 );
149148 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
149149 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
149157 ** to be unchanged. This is an optimization. False-positives are a
149158 ** performance degradation, but false-negatives can result in a corrupt
149161 ** aXRef[j] will be non-negative if column j of the original table is
149170 if( pIdx->pPartIdxWhere==0 ) return 0;
149171 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
149179 ** table in the source-list (pSrc->a[0]).
149183 if( pRet ) pRet->iColumn = iCol+1;
149191 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
149199 ** SELECT <other-columns>, pChanges FROM pTabList
149201 ** GROUP BY <other-columns>
149207 ** the <other-columns> in the query above are is determined by the type
149208 ** of table pTabList->a[0].pTab.
149211 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
149215 ** If the table is actually a view, then <other-columns> are all columns of
149219 ** If the table is a virtual or ordinary intkey table, then <other-columns>
149222 ** rowid value in <other-columns> is used as the integer key, and the
149242 sqlite3 *db = pParse->db;
149243 Table *pTab = pTabList->a[0].pTab;
149263 assert( pTabList->nSrc>1 );
149265 pSrc->a[0].fg.notCte = 1;
149266 pSrc->a[0].iCursor = -1;
149267 pSrc->a[0].pTab->nTabRef--;
149268 pSrc->a[0].pTab = 0;
149271 for(i=0; i<pPk->nKeyCol; i++){
149272 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
149282 for(i=0; i<pTab->nCol; i++){
149295 assert( pChanges!=0 || pParse->db->mallocFailed );
149297 for(i=0; i<pChanges->nExpr; i++){
149299 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
149307 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
149309 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
149347 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
149348 ** an expression for the i-th column of the table.
149349 ** aXRef[i]==-1 if the i-th column is not changed. */
149355 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
149357 NameContext sNC; /* The name-context to resolve expressions in */
149379 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
149391 db = pParse->db;
149392 assert( db->pParse==pParse );
149393 if( pParse->nErr ){
149396 assert( db->mallocFailed==0 );
149402 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
149424 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
149430 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
149433 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
149458 iBaseCur = iDataCur = pParse->nTab++;
149461 testcase( pPk!=0 && pPk!=pTab->pIndex );
149462 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
149464 iDataCur = pParse->nTab;
149466 pParse->nTab++;
149470 iDataCur = pUpsert->iDataCur;
149471 iIdxCur = pUpsert->iIdxCur;
149472 pParse->nTab = iBaseCur;
149474 pTabList->a[0].iCursor = iDataCur;
149479 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
149481 aRegIdx = aXRef+pTab->nCol;
149485 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
149487 /* Initialize the name-context */
149505 for(i=0; i<pChanges->nExpr; i++){
149506 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
149509 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
149512 for(j=0; j<pTab->nCol; j++){
149513 if( pTab->aCol[j].hName==hCol
149514 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
149516 if( j==pTab->iPKey ){
149518 pRowidExpr = pChanges->a[i].pExpr;
149520 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
149524 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
149525 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
149526 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
149529 pTab->aCol[j].zCnName);
149537 if( j>=pTab->nCol ){
149538 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
149539 j = -1;
149541 pRowidExpr = pChanges->a[i].pExpr;
149544 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
149545 pParse->checkSchema = 1;
149552 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
149553 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
149554 db->aDb[iDb].zDbSName);
149558 aXRef[j] = -1;
149572 ** is non-negative, so the value of aXRef[] for generated columns can be
149573 ** set to any non-negative number. We use 99999 so that the value is
149576 if( pTab->tabFlags & TF_HasGenerated ){
149578 testcase( pTab->tabFlags & TF_HasVirtual );
149579 testcase( pTab->tabFlags & TF_HasStored );
149582 for(i=0; i<pTab->nCol; i++){
149584 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
149586 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
149602 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
149611 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
149616 reg = ++pParse->nMem;
149617 pParse->nMem += pIdx->nColumn;
149620 for(i=0; i<pIdx->nKeyCol; i++){
149622 reg = ++pParse->nMem;
149623 pParse->nMem += pIdx->nColumn;
149624 if( onError==OE_Default && pIdx->onError==OE_Replace ){
149634 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
149641 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
149650 ** two-pass update algorithm. */
149651 assert( aRegIdx[nAllIdx]==pParse->nMem );
149653 regOldRowid = regNewRowid = ++pParse->nMem;
149655 regOld = pParse->nMem + 1;
149656 pParse->nMem += pTab->nCol;
149659 regNewRowid = ++pParse->nMem;
149661 regNew = pParse->nMem + 1;
149662 pParse->nMem += pTab->nCol;
149667 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
149704 if( (db->flags&SQLITE_CountRows)!=0
149705 && !pParse->pTriggerTab
149706 && !pParse->nested
149707 && !pParse->bReturning
149710 regRowCount = ++pParse->nMem;
149716 iEph = pParse->nTab++;
149720 nPk = pPk ? pPk->nKeyCol : 0;
149721 iPk = pParse->nMem+1;
149722 pParse->nMem += nPk;
149723 pParse->nMem += nChangeFrom;
149724 regKey = ++pParse->nMem;
149726 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
149727 iEph = pParse->nTab++;
149728 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
149733 pKeyInfo->nAllField = nEphCol;
149767 ** Do not consider a single-pass strategy for a multi-row update if
149777 if( !pParse->nested
149789 /* A one-pass strategy that might update more than one row may not
149805 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
149815 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
149819 aRegIdx[nAllIdx] = ++pParse->nMem;
149831 assert( pPk->aiColumn[i]>=0 );
149833 pPk->aiColumn[i], iPk+i);
149857 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
149858 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
149861 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
149939 /* Compute the old pre-UPDATE content of the row being changed, if that
149946 for(i=0; i<pTab->nCol; i++){
149947 u32 colFlags = pTab->aCol[i].colFlags;
149980 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
149981 if( i==pTab->iPKey ){
149983 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
149984 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
149989 int nOff = (isView ? pTab->nCol : nPk);
149993 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
150011 if( pTab->tabFlags & TF_HasGenerated ){
150012 testcase( pTab->tabFlags & TF_HasVirtual );
150013 testcase( pTab->tabFlags & TF_HasStored );
150027 /* The row-trigger may have deleted the row being updated. In this
150029 ** required. This behavior - what happens when the row being updated
150030 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
150041 /* After-BEFORE-trigger-reload-loop:
150047 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
150050 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
150051 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
150052 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
150053 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
150058 if( pTab->tabFlags & TF_HasGenerated ){
150059 testcase( pTab->tabFlags & TF_HasVirtual );
150060 testcase( pTab->tabFlags & TF_HasStored );
150092 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
150105 ** to invoke the pre-update hook.
150108 ** pre-update hook. If the caller invokes preupdate_new(), the returned
150122 if( !pParse->nested ){
150163 /* Nothing to do at end-of-loop for a single-pass */
150177 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
150215 ** There are two possible strategies - the default and the special
150243 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
150246 sqlite3 *db = pParse->db; /* Database connection */
150249 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
150253 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
150262 ephemTab = pParse->nTab++;
150264 regArg = pParse->nMem + 1;
150265 pParse->nMem += nArg;
150266 if( pSrc->nSrc>1 ){
150280 assert( pPk->nKeyCol==1 );
150281 iPk = pPk->aiColumn[0];
150283 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
150290 for(i=0; i<pTab->nCol; i++){
150293 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
150304 regRec = ++pParse->nMem;
150305 regRowid = ++pParse->nMem;
150314 for(i=0; i<pTab->nCol; i++){
150315 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
150317 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
150335 assert( pPk->nKeyCol==1 );
150336 iPk = pPk->aiColumn[0];
150347 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
150358 ** accept no-change records with serial_type 10 */
150369 if( pSrc->nSrc==1 ){
150402 ** 2018-04-12
150423 Upsert *pNext = p->pNextUpsert;
150424 sqlite3ExprListDelete(db, p->pUpsertTarget);
150425 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
150426 sqlite3ExprListDelete(db, p->pUpsertSet);
150427 sqlite3ExprDelete(db, p->pUpsertWhere);
150428 sqlite3DbFree(db, p->pToFree);
150444 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
150445 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
150446 sqlite3ExprListDup(db, p->pUpsertSet, 0),
150447 sqlite3ExprDup(db, p->pUpsertWhere, 0),
150448 sqlite3UpsertDup(db, p->pNextUpsert)
150473 pNew->pUpsertTarget = pTarget;
150474 pNew->pUpsertTargetWhere = pTargetWhere;
150475 pNew->pUpsertSet = pSet;
150476 pNew->pUpsertWhere = pWhere;
150477 pNew->isDoUpdate = pSet!=0;
150478 pNew->pNextUpsert = pNext;
150485 ** symbols in the conflict-target.
150499 ExprList *pTarget; /* The conflict-target clause */
150500 Expr *pTerm; /* One term of the conflict-target clause */
150505 assert( pTabList->nSrc==1 );
150506 assert( pTabList->a[0].pTab!=0 );
150508 assert( pUpsert->pUpsertTarget!=0 );
150510 /* Resolve all symbolic names in the conflict-target clause, which
150511 ** includes both the list of columns and the optional partial-index
150517 for(; pUpsert && pUpsert->pUpsertTarget;
150518 pUpsert=pUpsert->pNextUpsert, nClause++){
150519 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
150521 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
150525 pTab = pTabList->a[0].pTab;
150526 pTarget = pUpsert->pUpsertTarget;
150527 iCursor = pTabList->a[0].iCursor;
150529 && pTarget->nExpr==1
150530 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
150531 && pTerm->iColumn==XN_ROWID
150533 /* The conflict-target is the rowid of the primary table */
150534 assert( pUpsert->pUpsertIdx==0 );
150542 ** prior to comparing against the conflict-target expression.
150548 sCol[1].iTable = pTabList->a[0].iCursor;
150551 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
150554 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
150555 if( pIdx->pPartIdxWhere ){
150556 if( pUpsert->pUpsertTargetWhere==0 ) continue;
150557 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
150558 pIdx->pPartIdxWhere, iCursor)!=0 ){
150562 nn = pIdx->nKeyCol;
150565 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
150566 if( pIdx->aiColumn[ii]==XN_EXPR ){
150567 assert( pIdx->aColExpr!=0 );
150568 assert( pIdx->aColExpr->nExpr>ii );
150569 assert( pIdx->bHasExpr );
150570 pExpr = pIdx->aColExpr->a[ii].pExpr;
150571 if( pExpr->op!=TK_COLLATE ){
150577 sCol[1].iColumn = pIdx->aiColumn[ii];
150581 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
150595 pUpsert->pUpsertIdx = pIdx;
150598 if( pUpsert->pUpsertIdx==0 ){
150600 if( nClause==0 && pUpsert->pNextUpsert==0 ){
150621 pNext = pUpsert->pNextUpsert;
150623 if( pNext->pUpsertTarget==0 ) return 1;
150624 if( pNext->pUpsertIdx==0 ) return 1;
150637 && pUpsert->pUpsertTarget!=0
150638 && pUpsert->pUpsertIdx!=pIdx
150640 pUpsert = pUpsert->pNextUpsert;
150649 ** In this case parameter iCur is a cursor open on the table b-tree that
150655 Parse *pParse, /* The parsing and code-generating context */
150661 Vdbe *v = pParse->pVdbe;
150662 sqlite3 *db = pParse->db;
150670 iDataCur = pUpsert->iDataCur;
150682 int nPk = pPk->nKeyCol;
150683 int iPk = pParse->nMem+1;
150684 pParse->nMem += nPk;
150687 assert( pPk->aiColumn[i]>=0 );
150688 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
150690 VdbeComment((v, "%s.%s", pIdx->zName,
150691 pTab->aCol[pPk->aiColumn[i]].zCnName));
150702 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
150704 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
150706 for(i=0; i<pTab->nCol; i++){
150707 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
150708 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
150711 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
150712 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
150756 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
150828 if( pParse->nErr ) goto build_vacuum_end;
150837 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
150838 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
150841 iDb = sqlite3FindDb(pParse->db, pNm);
150848 iIntoReg = ++pParse->nMem;
150855 sqlite3ExprDelete(pParse->db, pInto);
150871 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
150872 u64 saved_flags; /* Saved value of db->flags */
150873 i64 saved_nChange; /* Saved value of db->nChange */
150874 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
150875 u32 saved_openFlags; /* Saved value of db->openFlags */
150885 if( !db->autoCommit ){
150887 return SQLITE_ERROR; /* IMP: R-12218-18073 */
150889 if( db->nVdbeActive>1 ){
150890 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
150891 return SQLITE_ERROR; /* IMP: R-15610-35227 */
150893 saved_openFlags = db->openFlags;
150896 sqlite3SetString(pzErrMsg, db, "non-text filename");
150900 db->openFlags &= ~SQLITE_OPEN_READONLY;
150901 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
150907 ** restored before returning. Then set the writable-schema flag, and
150909 saved_flags = db->flags;
150910 saved_mDbFlags = db->mDbFlags;
150911 saved_nChange = db->nChange;
150912 saved_nTotalChange = db->nTotalChange;
150913 saved_mTrace = db->mTrace;
150914 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
150915 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
150916 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
150918 db->mTrace = 0;
150920 zDbMain = db->aDb[iDb].zDbSName;
150921 pMain = db->aDb[iDb].pBt;
150930 ** An optimisation would be to use a non-journaled pager.
150938 nDb = db->nDb;
150940 db->openFlags = saved_openFlags;
150942 assert( (db->nDb-1)==nDb );
150943 pDb = &db->aDb[nDb];
150944 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
150945 pTemp = pDb->pBt;
150949 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
150954 db->mDbFlags |= DBFLAG_VacuumInto;
150956 /* For a VACUUM INTO, the pager-flags are set to the same values as
150959 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
150963 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
150969 ** to ensure that we do not try to change the page-size on a WAL database.
150981 db->nextPagesize = 0;
150985 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
150986 || NEVER(db->mallocFailed)
150993 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
151000 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
151014 db->init.iDb = 0;
151027 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
151028 db->mDbFlags &= ~DBFLAG_Vacuum;
151047 ** both transactions are closed by this block - the main database
151101 /* Restore the original value of db->flags */
151102 db->init.iDb = 0;
151103 db->mDbFlags = saved_mDbFlags;
151104 db->flags = saved_flags;
151105 db->nChange = saved_nChange;
151106 db->nTotalChange = saved_nTotalChange;
151107 db->mTrace = saved_mTrace;
151108 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
151117 db->autoCommit = 1;
151120 sqlite3BtreeClose(pDb->pBt);
151121 pDb->pBt = 0;
151122 pDb->pSchema = 0;
151125 /* This both clears the schemas and reduces the size of the db->aDb[]
151196 pMod->zName = zCopy;
151197 pMod->pModule = pModule;
151198 pMod->pAux = pAux;
151199 pMod->xDestroy = xDestroy;
151200 pMod->pEpoTab = 0;
151201 pMod->nRefModule = 1;
151203 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
151231 sqlite3_mutex_enter(db->mutex);
151235 sqlite3_mutex_leave(db->mutex);
151241 ** External API function used to create a new virtual-table module.
151256 ** External API function used to create a new virtual-table module.
151272 ** External API to drop all virtual-table modules, except those named
151280 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
151285 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
151288 createModule(db, pMod->zName, 0, 0, 0);
151298 assert( pMod->nRefModule>0 );
151299 pMod->nRefModule--;
151300 if( pMod->nRefModule==0 ){
151301 if( pMod->xDestroy ){
151302 pMod->xDestroy(pMod->pAux);
151304 assert( pMod->pEpoTab==0 );
151318 pVTab->nRef++;
151323 ** pTab is a pointer to a Table structure representing a virtual-table.
151325 ** this virtual-table, if one has been created, or NULL otherwise.
151330 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
151335 ** Decrement the ref-count on a virtual table object. When the ref-count
151339 sqlite3 *db = pVTab->db;
151342 assert( pVTab->nRef>0 );
151343 assert( db->eOpenState==SQLITE_STATE_OPEN
151344 || db->eOpenState==SQLITE_STATE_ZOMBIE );
151346 pVTab->nRef--;
151347 if( pVTab->nRef==0 ){
151348 sqlite3_vtab *p = pVTab->pVtab;
151350 p->pModule->xDisconnect(p);
151352 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
151359 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
151362 ** connection db is left in the p->u.vtab.p list.
151369 pVTable = p->u.vtab.p;
151370 p->u.vtab.p = 0;
151376 ** database connection that may have an entry in the p->u.vtab.p list.
151378 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
151381 sqlite3 *db2 = pVTable->db;
151382 VTable *pNext = pVTable->pNext;
151386 p->u.vtab.p = pRet;
151387 pRet->pNext = 0;
151389 pVTable->pNext = db2->pDisconnect;
151390 db2->pDisconnect = pVTable;
151402 ** list in p->pVTab. It also decrements the VTable ref count. This is
151405 ** be being used by other shared-cache connections).
151412 assert( sqlite3_mutex_held(db->mutex) );
151414 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
151415 if( (*ppVTab)->db==db ){
151417 *ppVTab = pVTab->pNext;
151429 ** shared b-tree databases opened using connection db are held by the
151439 ** or, if the virtual table is stored in a non-sharable database, then
151443 ** by multiple threads. It is thread-safe.
151446 VTable *p = db->pDisconnect;
151449 assert( sqlite3_mutex_held(db->mutex) );
151452 db->pDisconnect = 0;
151455 VTable *pNext = p->pNext;
151463 ** Clear any and all virtual-table information from the Table record.
151467 ** Since it is a virtual-table, the Table structure contains a pointer
151479 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
151480 if( p->u.vtab.azArg ){
151482 for(i=0; i<p->u.vtab.nArg; i++){
151483 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
151485 sqlite3DbFree(db, p->u.vtab.azArg);
151490 ** Add a new module argument to pTable->u.vtab.azArg[].
151491 ** The string is not copied - the pointer is stored. The
151498 sqlite3 *db = pParse->db;
151501 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
151502 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
151503 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
151505 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
151509 int i = pTable->u.vtab.nArg++;
151512 pTable->u.vtab.azArg = azModuleArg;
151532 pTable = pParse->pNewTable;
151534 assert( 0==pTable->pIndex );
151535 pTable->eTabType = TABTYP_VTAB;
151537 db = pParse->db;
151539 assert( pTable->u.vtab.nArg==0 );
151542 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
151543 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
151544 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
151546 pParse->sNameToken.n = (int)(
151547 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
151556 if( pTable->u.vtab.azArg ){
151557 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
151559 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
151560 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
151567 ** in pParse->zArg[] and appends it to the list of arguments on the
151568 ** virtual table currently under construction in pParse->pTable.
151571 if( pParse->sArg.z && pParse->pNewTable ){
151572 const char *z = (const char*)pParse->sArg.z;
151573 int n = pParse->sArg.n;
151574 sqlite3 *db = pParse->db;
151575 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
151584 Table *pTab = pParse->pNewTable; /* The table being constructed */
151585 sqlite3 *db = pParse->db; /* The database connection */
151590 pParse->sArg.z = 0;
151591 if( pTab->u.vtab.nArg<1 ) return;
151599 if( !db->init.busy ){
151610 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
151612 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
151618 ** The VM register number pParse->regRowid holds the rowid of an
151622 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151627 db->aDb[iDb].zDbSName,
151628 pTab->zName,
151629 pTab->zName,
151631 pParse->regRowid
151637 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
151641 iReg = ++pParse->nMem;
151642 sqlite3VdbeLoadString(v, iReg, pTab->zName);
151645 /* If we are rereading the sqlite_schema table create the in-memory
151648 Schema *pSchema = pTab->pSchema;
151649 const char *zName = pTab->zName;
151652 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
151658 pParse->pNewTable = 0;
151668 pParse->sArg.z = 0;
151669 pParse->sArg.n = 0;
151677 Token *pArg = &pParse->sArg;
151678 if( pArg->z==0 ){
151679 pArg->z = p->z;
151680 pArg->n = p->n;
151682 assert(pArg->z <= p->z);
151683 pArg->n = (int)(&p->z[p->n] - pArg->z);
151703 int nArg = pTab->u.vtab.nArg;
151710 azArg = (const char *const*)pTab->u.vtab.azArg;
151712 /* Check that the virtual-table is not already being initialized */
151713 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
151714 if( pCtx->pTab==pTab ){
151716 "vtable constructor called recursively: %s", pTab->zName
151722 zModuleName = sqlite3DbStrDup(db, pTab->zName);
151733 pVTable->db = db;
151734 pVTable->pMod = pMod;
151735 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
151737 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151738 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
151741 assert( &db->pVtabCtx );
151745 sCtx.pPrior = db->pVtabCtx;
151747 db->pVtabCtx = &sCtx;
151748 pTab->nTabRef++;
151749 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
151751 db->pVtabCtx = sCtx.pPrior;
151763 }else if( ALWAYS(pVTable->pVtab) ){
151766 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
151767 pVTable->pVtab->pModule = pMod->pModule;
151768 pMod->nRefModule++;
151769 pVTable->nRef = 1;
151772 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
151779 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
151783 pVTable->pNext = pTab->u.vtab.p;
151784 pTab->u.vtab.p = pVTable;
151786 for(iCol=0; iCol<pTab->nCol; iCol++){
151787 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
151793 && (i==0 || zType[i-1]==' ')
151806 assert(zType[i-1]==' ');
151807 zType[i-1] = '\0';
151809 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
151810 pTab->tabFlags |= TF_HasHidden;
151813 pTab->tabFlags |= oooHidden;
151828 ** This call is a no-op if table pTab is not a virtual table.
151831 sqlite3 *db = pParse->db;
151843 zMod = pTab->u.vtab.azArg[0];
151844 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
151847 const char *zModule = pTab->u.vtab.azArg[0];
151852 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
151855 pParse->rc = rc;
151863 ** Grow the db->aVTrans[] array so that there is room for at least one
151864 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
151870 if( (db->nVTrans%ARRAY_INCR)==0 ){
151873 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
151874 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
151878 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
151879 db->aVTrans = aVTrans;
151891 db->aVTrans[db->nVTrans++] = pVTab;
151909 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
151910 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
151913 zMod = pTab->u.vtab.azArg[0];
151914 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
151920 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
151924 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
151956 sqlite3_mutex_enter(db->mutex);
151957 pCtx = db->pVtabCtx;
151958 if( !pCtx || pCtx->bDeclared ){
151960 sqlite3_mutex_leave(db->mutex);
151963 pTab = pCtx->pTab;
151970 ** schema. Nevertheless, defend against that (turn off db->init.busy)
151972 assert( db->init.busy==0 );
151973 initBusy = db->init.busy;
151974 db->init.busy = 0;
151978 && ALWAYS(!db->mallocFailed)
151982 if( !pTab->aCol ){
151985 pTab->aCol = pNew->aCol;
151986 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
151987 pTab->nNVCol = pTab->nCol = pNew->nCol;
151988 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
151989 pNew->nCol = 0;
151990 pNew->aCol = 0;
151991 assert( pTab->pIndex==0 );
151994 && pCtx->pVTable->pMod->pModule->xUpdate!=0
151995 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
151997 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
151998 ** or else must have a single-column PRIMARY KEY */
152001 pIdx = pNew->pIndex;
152003 assert( pIdx->pNext==0 );
152004 pTab->pIndex = pIdx;
152005 pNew->pIndex = 0;
152006 pIdx->pTable = pTab;
152009 pCtx->bDeclared = 1;
152023 db->init.busy = initBusy;
152027 sqlite3_mutex_leave(db->mutex);
152036 ** This call is a no-op if zTab is not a virtual table.
152042 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
152045 && ALWAYS(pTab->u.vtab.p!=0)
152049 for(p=pTab->u.vtab.p; p; p=p->pNext){
152050 assert( p->pVtab );
152051 if( p->pVtab->nRef>0 ){
152056 xDestroy = p->pMod->pModule->xDestroy;
152057 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
152059 pTab->nTabRef++;
152060 rc = xDestroy(p->pVtab);
152063 assert( pTab->u.vtab.p==p && p->pNext==0 );
152064 p->pVtab = 0;
152065 pTab->u.vtab.p = 0;
152084 if( db->aVTrans ){
152085 VTable **aVTrans = db->aVTrans;
152086 db->aVTrans = 0;
152087 for(i=0; i<db->nVTrans; i++){
152089 sqlite3_vtab *p = pVTab->pVtab;
152092 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
152095 pVTab->iSavepoint = 0;
152099 db->nVTrans = 0;
152108 ** If an error message is available, leave it in p->zErrMsg.
152113 VTable **aVTrans = db->aVTrans;
152115 db->aVTrans = 0;
152116 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
152118 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
152119 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
152124 db->aVTrans = aVTrans;
152158 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
152169 pModule = pVTab->pVtab->pModule;
152171 if( pModule->xBegin ){
152175 for(i=0; i<db->nVTrans; i++){
152176 if( db->aVTrans[i]==pVTab ){
152185 rc = pModule->xBegin(pVTab->pVtab);
152187 int iSvpt = db->nStatement + db->nSavepoint;
152189 if( iSvpt && pModule->xSavepoint ){
152190 pVTab->iSavepoint = iSvpt;
152191 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
152218 assert( iSavepoint>=-1 );
152219 if( db->aVTrans ){
152221 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
152222 VTable *pVTab = db->aVTrans[i];
152223 const sqlite3_module *pMod = pVTab->pMod->pModule;
152224 if( pVTab->pVtab && pMod->iVersion>=2 ){
152229 xMethod = pMod->xSavepoint;
152230 pVTab->iSavepoint = iSavepoint+1;
152233 xMethod = pMod->xRollbackTo;
152236 xMethod = pMod->xRelease;
152239 if( xMethod && pVTab->iSavepoint>iSavepoint ){
152240 u64 savedFlags = (db->flags & SQLITE_Defensive);
152241 db->flags &= ~(u64)SQLITE_Defensive;
152242 rc = xMethod(pVTab->pVtab, iSavepoint);
152243 db->flags |= savedFlags;
152281 if( pExpr->op!=TK_COLUMN ) return pDef;
152283 pTab = pExpr->y.pTab;
152286 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
152288 assert( pVtab->pModule!=0 );
152289 pMod = (sqlite3_module *)pVtab->pModule;
152290 if( pMod->xFindFunction==0 ) return pDef;
152296 ** with an all lower-case function name. Continue in this tradition to
152302 for(i=0; pDef->zName[i]; i++){
152303 unsigned char x = (unsigned char)pDef->zName[i];
152308 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
152316 + sqlite3Strlen30(pDef->zName) + 1);
152321 pNew->zName = (const char*)&pNew[1];
152322 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
152323 pNew->xSFunc = xSFunc;
152324 pNew->pUserData = pArg;
152325 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
152330 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
152333 ** is a no-op.
152341 for(i=0; i<pToplevel->nVtabLock; i++){
152342 if( pTab==pToplevel->apVtabLock[i] ) return;
152344 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
152345 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
152347 pToplevel->apVtabLock = apVtabLock;
152348 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
152350 sqlite3OomFault(pToplevel->db);
152357 ** exist. Return non-zero if either the eponymous virtual table instance
152364 ** instances always exist. They cannot be DROP-ed.
152370 const sqlite3_module *pModule = pMod->pModule;
152374 sqlite3 *db = pParse->db;
152375 if( pMod->pEpoTab ) return 1;
152376 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
152379 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
152380 if( pTab->zName==0 ){
152384 pMod->pEpoTab = pTab;
152385 pTab->nTabRef = 1;
152386 pTab->eTabType = TABTYP_VTAB;
152387 pTab->pSchema = db->aDb[0].pSchema;
152388 assert( pTab->u.vtab.nArg==0 );
152389 pTab->iPKey = -1;
152390 pTab->tabFlags |= TF_Eponymous;
152391 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
152393 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
152394 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
152408 Table *pTab = pMod->pEpoTab;
152413 pTab->tabFlags |= TF_Ephemeral;
152415 pMod->pEpoTab = 0;
152435 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
152436 return (int)aMap[db->vtabOnConflict-1];
152452 sqlite3_mutex_enter(db->mutex);
152453 p = db->pVtabCtx;
152457 assert( p->pTab==0 || IsVirtual(p->pTab) );
152461 p->pVTable->bConstraint = (u8)va_arg(ap, int);
152465 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
152469 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
152473 p->pVTable->bAllSchemas = 1;
152485 sqlite3_mutex_leave(db->mutex);
152494 ** 2015-06-06
152507 ** This file was split off from where.c on 2015-06-06 in order to reduce the
152516 ** 2013-11-12
152585 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
152593 int addrSkip; /* Jump here for next iteration of skip-scan */
152597 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
152598 int addrBignull; /* Jump here for next part of big-null scan */
152608 union { /* Information that depends on pWLoop->wsFlags */
152614 int iBase; /* Base register of multi-key index record */
152618 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
152632 ** prevent a query solution - which is an error) and many terms of the
152634 ** potential way of implementing that FROM-clause term, together with
152650 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
152693 ** correspond to the subquery(s) of OR-clause processing. Only the
152709 ** vector, not a scalar, and because dependencies are many-to-one, not
152710 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
152726 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
152739 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
152770 ** spread out over the non-negative integers. For example, the cursor
152789 int iParent; /* Disable pWC->a[iParent] when this term disabled */
152799 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
152812 #define TERM_OK 0x0040 /* Used during OR-clause processing */
152818 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
152825 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
152836 int k; /* Resume scanning at this->pWC->a[this->k] */
152842 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
152864 int nBase; /* Number of terms through the last non-Virtual */
152902 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
152903 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
152910 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
152911 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
152950 ** routine. The limit is high enough that is should not impact real-world
152988 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
152997 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
153011 ** Private interfaces - callable only by other where.c routines.
153063 int iLevel, /* Which level of pWInfo->a[] should be coded */
153091 ** OR-ed combination of these values can be used when searching for
153103 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
153104 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
153105 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
153106 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
153110 #define WO_OR 0x0200 /* Two or more OR-connected terms */
153111 #define WO_AND 0x0400 /* Two or more AND-connected terms */
153114 #define WO_ROWVAL 0x2000 /* A row-value term */
153117 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
153132 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
153140 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
153145 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
153147 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
153150 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
153151 #define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
153161 ** Return the name of the i-th column of the pIdx index.
153164 i = pIdx->aiColumn[i];
153167 return pIdx->pTable->aCol[i].zCnName;
153182 int iTerm, /* Zero-based index of first term. */
153183 int bAnd, /* Non-zero to append " AND " */
153223 Index *pIndex = pLoop->u.btree.pIndex;
153224 u16 nEq = pLoop->u.btree.nEq;
153225 u16 nSkip = pLoop->nSkip;
153228 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
153237 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
153238 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
153241 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
153242 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
153248 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
153250 ** was defined at compile-time. If it is not a no-op, a single OP_Explain
153264 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
153267 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
153268 Vdbe *v = pParse->pVdbe; /* VM being constructed */
153269 sqlite3 *db = pParse->db; /* Database handle */
153277 pLoop = pLevel->pWLoop;
153278 flags = pLoop->wsFlags;
153282 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
153292 assert( pLoop->u.btree.pIndex!=0 );
153293 pIdx = pLoop->u.btree.pIndex;
153295 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
153310 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
153316 const Table *pTab = pItem->pTab;
153317 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
153339 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
153342 if( pItem->fg.jointype & JT_LEFT ){
153343 sqlite3_str_appendf(&str, " LEFT-JOIN");
153346 if( pLoop->nOut>=10 ){
153348 sqlite3LogEstToInt(pLoop->nOut));
153356 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
153366 ** required and this routine is a no-op.
153377 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
153378 Vdbe *v = pParse->pVdbe; /* VM being constructed */
153379 sqlite3 *db = pParse->db; /* Database handle */
153389 pLoop = pLevel->pWLoop;
153390 if( pLoop->wsFlags & WHERE_IPK ){
153391 const Table *pTab = pItem->pTab;
153392 if( pTab->iPKey>=0 ){
153393 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
153398 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
153399 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
153400 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
153407 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
153409 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
153432 WhereLoop *pLoop = pLvl->pWLoop;
153433 int wsFlags = pLoop->wsFlags;
153436 if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
153437 zObj = pLoop->u.btree.pIndex->zName;
153439 zObj = pSrclist->a[pLvl->iFrom].zName;
153440 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
153443 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
153448 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
153451 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
153499 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
153504 while( (pTerm->wtFlags & TERM_CODED)==0
153505 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
153506 && (pLevel->notReady & pTerm->prereqAll)==0
153508 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
153509 pTerm->wtFlags |= TERM_LIKECOND;
153511 pTerm->wtFlags |= TERM_CODED;
153515 sqlite3DebugPrintf("DISABLE-");
153516 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
153519 if( pTerm->iParent<0 ) break;
153520 pTerm = &pTerm->pWC->a[pTerm->iParent];
153522 pTerm->nChild--;
153523 if( pTerm->nChild!=0 ) break;
153533 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
153540 Vdbe *v = pParse->pVdbe;
153542 assert( pParse->db->mallocFailed );
153552 n--;
153556 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
153557 n--;
153628 sqlite3 *db = pParse->db;
153632 if( db->mallocFailed==0 ){
153633 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
153641 pOrigRhs = pSelect->pEList;
153642 assert( pNew->pLeft!=0 );
153643 assert( ExprUseXList(pNew->pLeft) );
153644 if( pSelect==pNew->x.pSelect ){
153645 pOrigLhs = pNew->pLeft->x.pList;
153647 for(i=iEq; i<pLoop->nLTerm; i++){
153648 if( pLoop->aLTerm[i]->pExpr==pX ){
153650 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
153651 iField = pLoop->aLTerm[i]->u.x.iField - 1;
153652 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
153653 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
153654 pOrigRhs->a[iField].pExpr = 0;
153656 assert( pOrigLhs->a[iField].pExpr!=0 );
153657 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
153658 pOrigLhs->a[iField].pExpr = 0;
153665 pNew->pLeft->x.pList = pLhs;
153667 pSelect->pEList = pRhs;
153668 if( pLhs && pLhs->nExpr==1 ){
153672 Expr *p = pLhs->a[0].pExpr;
153673 pLhs->a[0].pExpr = 0;
153674 sqlite3ExprDelete(db, pNew->pLeft);
153675 pNew->pLeft = p;
153677 if( pSelect->pOrderBy ){
153679 ** iOrderByCol variables. These are set to non-zero when an
153681 ** result-set. Since the result-set of the SELECT statement may
153685 ExprList *pOrderBy = pSelect->pOrderBy;
153686 for(i=0; i<pOrderBy->nExpr; i++){
153687 pOrderBy->a[i].u.x.iOrderByCol = 0;
153715 ** straight-line code. For constraints of the form X IN (...)
153723 int bRev, /* True for reverse-order IN operations */
153726 Expr *pX = pTerm->pExpr;
153727 Vdbe *v = pParse->pVdbe;
153730 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
153732 if( pX->op==TK_EQ || pX->op==TK_IS ){
153733 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
153734 }else if( pX->op==TK_ISNULL ){
153742 WhereLoop *pLoop = pLevel->pWLoop;
153747 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
153748 && pLoop->u.btree.pIndex!=0
153749 && pLoop->u.btree.pIndex->aSortOrder[iEq]
153755 assert( pX->op==TK_IN );
153759 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
153764 for(i=iEq;i<pLoop->nLTerm; i++){
153765 assert( pLoop->aLTerm[i]!=0 );
153766 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
153770 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
153773 Expr *pExpr = pTerm->pExpr;
153774 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
153775 sqlite3 *db = pParse->db;
153777 if( !db->mallocFailed ){
153778 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
153780 pExpr->iTable = iTab;
153784 int n = sqlite3ExprVectorSize(pX->pLeft);
153785 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
153799 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
153800 pLoop->wsFlags |= WHERE_IN_ABLE;
153801 if( pLevel->u.in.nIn==0 ){
153802 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
153804 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
153805 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
153808 i = pLevel->u.in.nIn;
153809 pLevel->u.in.nIn += nEq;
153810 pLevel->u.in.aInLoop =
153811 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
153812 pLevel->u.in.aInLoop,
153813 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
153814 pIn = pLevel->u.in.aInLoop;
153818 for(i=iEq;i<pLoop->nLTerm; i++){
153819 if( pLoop->aLTerm[i]->pExpr==pX ){
153820 int iOut = iReg + i - iEq;
153822 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
153825 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
153829 pIn->iCur = iTab;
153830 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
153832 pIn->iBase = iReg - i;
153833 pIn->nPrefix = i;
153835 pIn->nPrefix = 0;
153838 pIn->eEndLoopOp = OP_Noop;
153844 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
153845 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
153847 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
153849 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
153852 pLevel->u.in.nIn = 0;
153854 sqlite3DbFree(pParse->db, aiMap);
153865 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
153867 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
153868 || (pTerm->eOperator & WO_EQUIV)==0
153889 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
153890 ** The only thing it does is allocate the pLevel->iMem memory cell and
153929 u16 nSkip; /* Number of left-most columns to skip */
153930 Vdbe *v = pParse->pVdbe; /* The vm under construction */
153940 pLoop = pLevel->pWLoop;
153941 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
153942 nEq = pLoop->u.btree.nEq;
153943 nSkip = pLoop->nSkip;
153944 pIdx = pLoop->u.btree.pIndex;
153949 regBase = pParse->nMem + 1;
153950 nReg = pLoop->u.btree.nEq + nExtraReg;
153951 pParse->nMem += nReg;
153953 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
153954 assert( zAff!=0 || pParse->db->mallocFailed );
153957 int iIdxCur = pLevel->iIdxCur;
153958 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
153962 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
153964 assert( pLevel->addrSkip==0 );
153965 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
153972 testcase( pIdx->aiColumn[j]==XN_EXPR );
153982 pTerm = pLoop->aLTerm[j];
153986 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
153987 testcase( pTerm->wtFlags & TERM_VIRTUAL );
153999 pTerm = pLoop->aLTerm[j];
154000 if( pTerm->eOperator & WO_IN ){
154001 if( pTerm->pExpr->flags & EP_xIsSelect ){
154008 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
154009 Expr *pRight = pTerm->pExpr->pRight;
154010 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
154011 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
154014 if( pParse->nErr==0 ){
154015 assert( pParse->db->mallocFailed==0 );
154045 ** becomes a no-op.
154052 if( pTerm->wtFlags & TERM_LIKEOPT ){
154054 assert( pLevel->iLikeRepCntr>0 );
154057 assert( pOp->opcode==OP_String8
154058 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
154059 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
154060 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
154083 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
154086 struct CCurHint *pHint = pWalker->u.pCCurHint;
154087 assert( pHint->pIdx!=0 );
154088 if( pExpr->op==TK_COLUMN
154089 && pExpr->iTable==pHint->iTabCur
154090 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
154092 pWalker->eCode = 1;
154099 ** should be included in the cursor-hint for a table that is on the rhs
154100 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
154113 if( pExpr->op==TK_IS
154114 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
154115 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
154117 pWalker->eCode = 1;
154118 }else if( pExpr->op==TK_FUNCTION ){
154121 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
154122 pWalker->eCode = 1;
154150 struct CCurHint *pHint = pWalker->u.pCCurHint;
154151 if( pExpr->op==TK_COLUMN ){
154152 if( pExpr->iTable!=pHint->iTabCur ){
154153 reg = ++pWalker->pParse->nMem; /* Register for column value */
154154 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
154155 pExpr->op = TK_REGISTER;
154156 pExpr->iTable = reg;
154157 }else if( pHint->pIdx!=0 ){
154158 pExpr->iTable = pHint->iIdxCur;
154159 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
154160 assert( pExpr->iColumn>=0 );
154162 }else if( pExpr->pAggInfo ){
154164 reg = ++pWalker->pParse->nMem; /* Register for column value */
154165 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
154166 pExpr->op = TK_REGISTER;
154167 pExpr->iTable = reg;
154168 }else if( pExpr->op==TK_TRUEFALSE ){
154169 /* Do not walk disabled expressions. tag-20230504-1 */
154182 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
154184 Parse *pParse = pWInfo->pParse;
154185 sqlite3 *db = pParse->db;
154186 Vdbe *v = pParse->pVdbe;
154188 WhereLoop *pLoop = pLevel->pWLoop;
154197 iCur = pLevel->iTabCur;
154198 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
154200 sHint.iIdxCur = pLevel->iIdxCur;
154201 sHint.pIdx = pLoop->u.btree.pIndex;
154205 pWC = &pWInfo->sWC;
154206 for(i=0; i<pWC->nBase; i++){
154207 pTerm = &pWC->a[i];
154208 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
154209 if( pTerm->prereqAll & pLevel->notReady ) continue;
154213 ** from the cursor-hint.
154233 if( pTabItem->fg.jointype & JT_LEFT ){
154234 Expr *pExpr = pTerm->pExpr;
154236 || pExpr->w.iJoin!=pTabItem->iCursor
154240 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
154244 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
154247 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
154250 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
154251 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
154252 if( j<pLoop->nLTerm ) continue;
154255 /* No subqueries or non-deterministic functions allowed */
154256 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
154263 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
154268 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
154272 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
154279 # define codeCursorHint(A,B,C,D) /* No-op */
154283 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
154294 ** However, if the scan currently being coded is a branch of an OR-loop and
154311 int iCur, /* Cursor for IPK b-tree */
154314 Parse *pParse = pWInfo->pParse; /* Parse context */
154315 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
154318 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
154320 pWInfo->bDeferredSeek = 1;
154322 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
154323 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
154326 Table *pTab = pIdx->pTable;
154327 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
154329 ai[0] = pTab->nCol;
154330 for(i=0; i<pIdx->nColumn-1; i++){
154332 assert( pIdx->aiColumn[i]<pTab->nCol );
154333 x1 = pIdx->aiColumn[i];
154338 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
154357 Vdbe *v = pParse->pVdbe;
154359 assert( p->op==TK_SELECT );
154361 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
154368 pList = p->x.pList;
154369 assert( nReg<=pList->nExpr );
154371 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
154375 assert( nReg==1 || pParse->nErr );
154394 while( pTruth->op==TK_AND ){
154395 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
154396 pTruth = pTruth->pRight;
154398 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
154400 if( pTerm->wtFlags & TERM_CODED ) continue;
154401 pExpr = pTerm->pExpr;
154403 pTerm->wtFlags |= TERM_CODED;
154413 ** inner-loop Bloom filters, then evaluate those filters now, before the
154419 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
154426 int iLevel, /* Which level of pWInfo->a[] should be coded */
154430 while( ++iLevel < pWInfo->nLevel ){
154431 WhereLevel *pLevel = &pWInfo->a[iLevel];
154432 WhereLoop *pLoop = pLevel->pWLoop;
154433 if( pLevel->regFilter==0 ) continue;
154434 if( pLevel->pWLoop->nSkip ) continue;
154435 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
154436 ** vvvvv--' pLevel->regFilter if this were true. */
154437 if( NEVER(pLoop->prereq & notReady) ) continue;
154438 assert( pLevel->addrBrk==0 );
154439 pLevel->addrBrk = addrNxt;
154440 if( pLoop->wsFlags & WHERE_IPK ){
154441 WhereTerm *pTerm = pLoop->aLTerm[0];
154444 assert( pTerm->pExpr!=0 );
154445 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154448 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
154449 VdbeCoverage(pParse->pVdbe);
154450 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
154452 VdbeCoverage(pParse->pVdbe);
154454 u16 nEq = pLoop->u.btree.nEq;
154458 assert( pLoop->wsFlags & WHERE_INDEXED );
154459 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
154462 sqlite3DbFree(pParse->db, zStartAff);
154463 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
154465 VdbeCoverage(pParse->pVdbe);
154467 pLevel->regFilter = 0;
154468 pLevel->addrBrk = 0;
154473 ** Generate code for the start of the iLevel-th loop in the WHERE clause
154480 int iLevel, /* Which level of pWInfo->a[] should be coded */
154501 pWC = &pWInfo->sWC;
154502 db = pParse->db;
154503 pLoop = pLevel->pWLoop;
154504 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
154505 iCur = pTabItem->iCursor;
154506 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
154507 bRev = (pWInfo->revMask>>iLevel)&1;
154508 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
154512 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
154520 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
154522 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
154537 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
154538 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
154544 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
154545 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
154547 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
154548 pLevel->iLeftJoin = ++pParse->nMem;
154549 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
154550 VdbeComment((v, "init LEFT JOIN no-match flag"));
154555 for(j=iLevel; j>0; j--){
154556 if( pWInfo->a[j].iLeftJoin ) break;
154557 if( pWInfo->a[j].pRJ ) break;
154559 addrHalt = pWInfo->a[j].addrBrk;
154561 /* Special case of a FROM clause subquery implemented as a co-routine */
154562 if( pTabItem->fg.viaCoroutine ){
154563 int regYield = pTabItem->regReturn;
154564 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
154565 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
154567 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
154568 pLevel->op = OP_Goto;
154572 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
154573 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
154578 int nConstraint = pLoop->nLTerm;
154581 addrNotFound = pLevel->addrBrk;
154584 pTerm = pLoop->aLTerm[j];
154586 if( pTerm->eOperator & WO_IN ){
154587 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
154588 int iTab = pParse->nTab++;
154589 int iCache = ++pParse->nMem;
154590 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
154594 addrNotFound = pLevel->addrNxt;
154597 Expr *pRight = pTerm->pExpr->pRight;
154599 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
154600 && pLoop->u.vtab.bOmitOffset
154602 assert( pTerm->eOperator==WO_AUX );
154603 assert( pWInfo->pSelect!=0 );
154604 assert( pWInfo->pSelect->iOffset>0 );
154605 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
154610 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
154613 pLoop->u.vtab.idxStr,
154614 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
154616 pLoop->u.vtab.needFree = 0;
154618 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
154619 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
154620 pLevel->p1 = iCur;
154621 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
154622 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
154623 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
154626 pTerm = pLoop->aLTerm[j];
154627 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
154631 if( (pTerm->eOperator & WO_IN)!=0
154632 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
154633 && !db->mallocFailed
154638 int iIn; /* IN loop corresponding to the j-th constraint */
154645 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
154646 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
154647 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
154648 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
154650 testcase( pOp->opcode==OP_Rowid );
154651 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
154660 if( !db->mallocFailed ){
154661 int iFld = pTerm->u.x.iField;
154662 Expr *pLeft = pTerm->pExpr->pLeft;
154665 assert( pLeft->op==TK_VECTOR );
154667 assert( iFld<=pLeft->x.pList->nExpr );
154668 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
154670 pCompare->pLeft = pLeft;
154672 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
154674 pRight->iTable = iReg+j+2;
154676 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
154679 pCompare->pLeft = 0;
154687 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
154695 if( (pLoop->wsFlags & WHERE_IPK)!=0
154696 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
154703 assert( pLoop->u.btree.nEq==1 );
154704 pTerm = pLoop->aLTerm[0];
154706 assert( pTerm->pExpr!=0 );
154707 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154708 iReleaseReg = ++pParse->nMem;
154711 addrNxt = pLevel->addrNxt;
154712 if( pLevel->regFilter ){
154715 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
154722 pLevel->op = OP_Noop;
154723 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
154724 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
154735 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
154736 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
154762 assert( (pStart->wtFlags & TERM_VNULL)==0 );
154763 testcase( pStart->wtFlags & TERM_VIRTUAL );
154764 pX = pStart->pExpr;
154766 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
154767 if( sqlite3ExprIsVector(pX->pRight) ){
154769 codeExprOrVector(pParse, pX->pRight, r1, 1);
154770 testcase( pX->op==TK_GT );
154771 testcase( pX->op==TK_GE );
154772 testcase( pX->op==TK_LT );
154773 testcase( pX->op==TK_LE );
154774 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
154775 assert( pX->op!=TK_GT || op==OP_SeekGE );
154776 assert( pX->op!=TK_GE || op==OP_SeekGE );
154777 assert( pX->op!=TK_LT || op==OP_SeekLE );
154778 assert( pX->op!=TK_LE || op==OP_SeekLE );
154780 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
154782 op = aMoveOp[(pX->op - TK_GT)];
154786 VdbeCoverageIf(v, pX->op==TK_GT);
154787 VdbeCoverageIf(v, pX->op==TK_LE);
154788 VdbeCoverageIf(v, pX->op==TK_LT);
154789 VdbeCoverageIf(v, pX->op==TK_GE);
154798 pX = pEnd->pExpr;
154800 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
154801 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
154802 testcase( pEnd->wtFlags & TERM_VIRTUAL );
154803 memEndValue = ++pParse->nMem;
154804 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
154805 if( 0==sqlite3ExprIsVector(pX->pRight)
154806 && (pX->op==TK_LT || pX->op==TK_GT)
154812 if( 0==sqlite3ExprIsVector(pX->pRight) ){
154817 pLevel->op = bRev ? OP_Prev : OP_Next;
154818 pLevel->p1 = iCur;
154819 pLevel->p2 = start;
154820 assert( pLevel->p5==0 );
154822 iRowidReg = ++pParse->nMem;
154831 }else if( pLoop->wsFlags & WHERE_INDEXED ){
154836 ** left-most columns of the index. It may also contain
154839 ** the right-most column can be an inequality - the rest must
154879 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
154880 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
154881 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
154897 int regBignull = 0; /* big-null flag register */
154900 pIdx = pLoop->u.btree.pIndex;
154901 iIdxCur = pLevel->iIdxCur;
154902 assert( nEq>=pLoop->nSkip );
154908 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
154909 pRangeStart = pLoop->aLTerm[j++];
154910 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
154912 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
154913 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
154915 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
154916 pRangeEnd = pLoop->aLTerm[j++];
154917 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
154919 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
154921 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
154922 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
154923 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
154925 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
154929 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
154931 pLevel->iLikeRepCntr <<=1;
154932 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
154936 j = pIdx->aiColumn[nEq];
154937 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
154942 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
154945 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
154948 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
154951 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
154952 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
154956 testcase( pLoop->nSkip>0 );
154959 pLevel->regBignull = regBignull = ++pParse->nMem;
154960 if( pLevel->iLeftJoin ){
154963 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
154970 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
154976 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
154992 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
154994 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
154995 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
154996 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
154997 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
154998 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
154999 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
155005 Expr *pRight = pRangeStart->pExpr->pRight;
155008 if( (pRangeStart->wtFlags & TERM_VNULL)==0
155018 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
155035 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
155036 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
155037 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
155043 VdbeComment((v, "NULL-scan pass ctr"));
155045 if( pLevel->regFilter ){
155046 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
155054 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
155065 (pIdx->aiRowLogEst[0]+9)/10);
155090 nConstraint-startEq);
155104 assert( pLevel->p2==0 );
155106 Expr *pRight = pRangeEnd->pExpr->pRight;
155110 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
155120 assert( pParse->db->mallocFailed );
155123 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
155141 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
155146 /* Except, skip the end-of-range check while doing the NULL-scan */
155148 VdbeComment((v, "If NULL-scan 2nd pass"));
155160 /* During a NULL-scan, check to see if we have reached the end of
155166 VdbeComment((v, "If NULL-scan 1st pass"));
155177 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
155182 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
155183 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
155186 }else if( HasRowid(pIdx->pTable) ){
155189 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
155190 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
155191 for(j=0; j<pPk->nKeyCol; j++){
155192 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
155196 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
155199 if( pLevel->iLeftJoin==0 ){
155204 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
155207 if( pIdx->pPartIdxWhere ){
155208 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
155211 testcase( pIdx->pPartIdxWhere );
155213 ** The OR-optimization doesn't work for the right hand table of
155215 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
155219 if( pLoop->wsFlags & WHERE_ONEROW ){
155220 pLevel->op = OP_Noop;
155222 pLevel->op = OP_Prev;
155224 pLevel->op = OP_Next;
155226 pLevel->p1 = iIdxCur;
155227 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
155228 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
155229 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
155231 assert( pLevel->p5==0 );
155237 if( pLoop->wsFlags & WHERE_MULTI_OR ){
155276 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
155281 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
155282 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
155284 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
155286 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
155294 Table *pTab = pTabItem->pTab;
155296 pTerm = pLoop->aLTerm[0];
155298 assert( pTerm->eOperator & WO_OR );
155299 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
155300 pOrWc = &pTerm->u.pOrInfo->wc;
155301 pLevel->op = OP_Return;
155302 pLevel->p1 = regReturn;
155308 if( pWInfo->nLevel>1 ){
155311 nNotReady = pWInfo->nLevel - iLevel - 1;
155313 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
155315 pOrTab->nAlloc = (u8)(nNotReady + 1);
155316 pOrTab->nSrc = pOrTab->nAlloc;
155317 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
155318 origSrc = pWInfo->pTabList->a;
155320 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
155323 pOrTab = pWInfo->pTabList;
155334 ** correct response for the end-of-loop code (the OP_Return) is to
155338 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
155340 regRowset = ++pParse->nMem;
155344 regRowset = pParse->nTab++;
155345 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
155348 regRowid = ++pParse->nMem;
155358 ** the "interesting" terms of z - terms that did not originate in the
155366 ** 2022-02-04: Do not push down slices of a row-value comparison.
155368 ** the initialization of the right-hand operand of the vector comparison
155372 ** 2022-03-03: Do not push down expressions that involve subqueries.
155373 ** The subquery might get coded as a subroutine. Any table-references
155374 ** in the subquery might be resolved to index-references for the index on
155377 ** index-references will not work. tag-20220303a
155380 if( pWC->nTerm>1 ){
155382 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
155383 Expr *pExpr = pWC->a[iTerm].pExpr;
155384 if( &pWC->a[iTerm] == pTerm ) continue;
155385 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
155386 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
155387 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
155388 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
155391 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
155392 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
155400 ** prevents sqlite3PExpr() from applying the AND short-circuit
155408 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
155410 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
155411 for(ii=0; ii<pOrWc->nTerm; ii++){
155412 WhereTerm *pOrTerm = &pOrWc->a[ii];
155413 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
155414 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
155415 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
155422 if( db->mallocFailed ){
155427 pAndExpr->pLeft = pOrExpr;
155432 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
155435 assert( pSubWInfo || pParse->nErr );
155439 pParse, pOrTab, &pSubWInfo->a[0], 0
155441 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
155443 /* This is the sub-WHERE clause body. First skip over
155444 ** duplicate rows from prior sub-WHERE clauses, and record the
155446 ** row will be skipped in subsequent sub-WHERE clauses.
155448 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
155449 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
155451 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
155457 int nPk = pPk->nKeyCol;
155464 int iCol = pPk->aiColumn[iPk];
155476 ** the temp table. And if iSet is -1, assume that there is no
155499 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
155502 /* The pSubWInfo->untestedTerms flag means that this OR term
155507 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
155509 /* If all of the OR-connected terms are optimized using the same
155515 ** uses an index, and this is either the first OR-connected term
155521 pSubLoop = pSubWInfo->a[0].pWLoop;
155522 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
155523 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
155524 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
155525 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
155527 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
155528 pCov = pSubLoop->u.btree.pIndex;
155533 pWInfo->bDeferredSeek = 1;
155544 assert( pLevel->pWLoop==pLoop );
155545 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
155546 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
155547 pLevel->u.pCoveringIdx = pCov;
155548 if( pCov ) pLevel->iIdxCur = iCovCur;
155550 pAndExpr->pLeft = 0;
155554 sqlite3VdbeGoto(v, pLevel->addrBrk);
155559 ** loop. The byte-code formatter will use that P2 value as a hint to
155561 ** See tag-20220407a in vdbe.c and shell.c */
155562 assert( pLevel->op==OP_Return );
155563 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
155565 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
155577 if( pTabItem->fg.isRecursive ){
155579 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
155580 pLevel->op = OP_Noop;
155583 pLevel->op = aStep[bRev];
155584 pLevel->p1 = iCur;
155585 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
155588 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
155593 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
155605 ** sub-queries.
155613 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
155616 testcase( pTerm->wtFlags & TERM_VIRTUAL );
155617 testcase( pTerm->wtFlags & TERM_CODED );
155618 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155619 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
155620 testcase( pWInfo->untestedTerms==0
155621 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
155622 pWInfo->untestedTerms = 1;
155625 pE = pTerm->pExpr;
155627 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
155630 ** join processing. tag-20220513a */
155632 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
155636 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
155637 if( m & pLevel->notReady ){
155643 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
155647 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
155652 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
155661 u32 x = pLevel->iLikeRepCntr;
155672 pWC->nTerm-j, pTerm, iLoop));
155676 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
155681 pTerm->wtFlags |= TERM_CODED;
155694 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
155697 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155698 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
155699 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
155700 if( pTerm->leftCursor!=iCur ) continue;
155701 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
155702 pE = pTerm->pExpr;
155706 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
155710 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
155711 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155712 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
155715 if( pAlt->wtFlags & (TERM_CODED) ) continue;
155716 if( (pAlt->eOperator & WO_IN)
155717 && ExprUseXSelect(pAlt->pExpr)
155718 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
155722 testcase( pAlt->eOperator & WO_EQ );
155723 testcase( pAlt->eOperator & WO_IS );
155724 testcase( pAlt->eOperator & WO_IN );
155726 sEAlt = *pAlt->pExpr;
155727 sEAlt.pLeft = pE->pLeft;
155729 pAlt->wtFlags |= TERM_CODED;
155735 if( pLevel->pRJ ){
155740 WhereRightJoin *pRJ = pLevel->pRJ;
155742 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
155747 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
155750 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
155755 nPk = pPk->nKeyCol;
155758 int iCol = pPk->aiColumn[iPk];
155762 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
155764 VdbeComment((v, "match against %s", pTab->zName));
155766 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
155767 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
155776 if( pLevel->iLeftJoin ){
155777 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
155778 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
155780 if( pLevel->pRJ==0 ){
155785 if( pLevel->pRJ ){
155788 ** be in-line with the rest of the code. But at the end, a separate
155792 WhereRightJoin *pRJ = pLevel->pRJ;
155793 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
155794 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
155795 assert( pParse->withinRJSubrtn < 255 );
155796 pParse->withinRJSubrtn++;
155801 ** appropriate WHERE clause constraint checks. tag-20220513a.
155804 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
155805 testcase( pTerm->wtFlags & TERM_VIRTUAL );
155806 testcase( pTerm->wtFlags & TERM_CODED );
155807 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155808 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
155809 assert( pWInfo->untestedTerms );
155812 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
155813 assert( pTerm->pExpr );
155814 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
155815 pTerm->wtFlags |= TERM_CODED;
155821 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
155827 iLevel, (u64)pLevel->notReady);
155830 return pLevel->notReady;
155834 ** Generate the code for the loop that finds all non-matched terms
155842 Parse *pParse = pWInfo->pParse;
155843 Vdbe *v = pParse->pVdbe;
155844 WhereRightJoin *pRJ = pLevel->pRJ;
155846 WhereClause *pWC = &pWInfo->sWC;
155848 WhereLoop *pLoop = pLevel->pWLoop;
155849 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155854 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
155855 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
155856 pRJ->regReturn);
155859 mAll |= pWInfo->a[k].pWLoop->maskSelf;
155860 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
155861 iIdxCur = pWInfo->a[k].iIdxCur;
155866 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
155867 mAll |= pLoop->maskSelf;
155868 for(k=0; k<pWC->nTerm; k++){
155869 WhereTerm *pTerm = &pWC->a[k];
155870 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
155871 && pTerm->eOperator!=WO_ROWVAL
155875 if( pTerm->prereqAll & ~mAll ) continue;
155876 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
155878 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
155885 assert( pParse->withinRJSubrtn < 100 );
155886 pParse->withinRJSubrtn++;
155890 int iCur = pLevel->iTabCur;
155891 int r = ++pParse->nMem;
155895 Table *pTab = pTabItem->pTab;
155897 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
155902 nPk = pPk->nKeyCol;
155903 pParse->nMem += nPk - 1;
155905 int iCol = pPk->aiColumn[iPk];
155909 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
155911 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
155914 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
155917 sqlite3ExprDelete(pParse->db, pSubWhere);
155919 assert( pParse->withinRJSubrtn>0 );
155920 pParse->withinRJSubrtn--;
155926 ** 2015-06-08
155953 sqlite3WhereClauseClear(&p->wc);
155961 sqlite3WhereClauseClear(&p->wc);
155968 ** The index in pWC->a[] of the new WhereTerm is returned on success.
155971 ** the db->mallocFailed flag so that higher-level functions can detect it.
155973 ** This routine will increase the size of the pWC->a[] array as necessary.
155982 ** the pWC->a[] array.
155988 if( pWC->nTerm>=pWC->nSlot ){
155989 WhereTerm *pOld = pWC->a;
155990 sqlite3 *db = pWC->pWInfo->pParse->db;
155991 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
155992 if( pWC->a==0 ){
155996 pWC->a = pOld;
155999 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
156000 pWC->nSlot = pWC->nSlot*2;
156002 pTerm = &pWC->a[idx = pWC->nTerm++];
156003 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
156005 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
156007 pTerm->truthProb = 1;
156009 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
156010 pTerm->wtFlags = wtFlags;
156011 pTerm->pWC = pWC;
156012 pTerm->iParent = -1;
156013 memset(&pTerm->eOperator, 0,
156014 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
156036 if( pExpr->pLeft->op==TK_VECTOR
156037 || pExpr->pRight->op==TK_VECTOR
156038 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
156039 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
156041 pExpr->flags ^= EP_Commuted;
156043 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
156044 if( pExpr->op>=TK_GT ){
156049 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
156050 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
156068 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
156069 c = (u16)(WO_EQ<<(op-TK_EQ));
156107 int cnt; /* Number of non-wildcard prefix characters */
156109 sqlite3 *db = pParse->db; /* Database connection */
156121 pList = pExpr->x.pList;
156122 pLeft = pList->a[1].pExpr;
156124 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
156125 op = pRight->op;
156126 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
156127 Vdbe *pReprepare = pParse->pReprepare;
156128 int iCol = pRight->iColumn;
156133 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
156134 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
156137 z = (u8*)pRight->u.zToken;
156149 ** with a wildcard and if (2) the non-wildcard prefix does not end with
156156 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
156168 zNew = pPrefix->u.zToken;
156184 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
156185 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
156186 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
156187 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
156188 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
156190 if( pLeft->op!=TK_COLUMN
156193 && ALWAYS(pLeft->y.pTab)
156194 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
156200 if( iTo==1 && zNew[0]=='-' ){
156203 zNew[iTo-1]++;
156205 zNew[iTo-1]--;
156220 Vdbe *v = pParse->pVdbe;
156221 sqlite3VdbeSetVarmask(v, pRight->iColumn);
156223 if( *pisComplete && pRight->u.zToken[1] ){
156232 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
156254 ** ----------------------- ---------------------------------
156282 if( pExpr->op==TK_FUNCTION ){
156297 pList = pExpr->x.pList;
156298 if( pList==0 || pList->nExpr!=2 ){
156302 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
156304 ** the left-hand side operand in their in-fix form.
156309 pCol = pList->a[1].pExpr;
156310 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
156314 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
156316 *ppRight = pList->a[0].pExpr;
156329 ** Historically, xFindFunction expected to see lower-case function
156333 pCol = pList->a[0].pExpr;
156334 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
156335 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
156341 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
156343 assert( pVtab->pModule!=0 );
156345 pMod = (sqlite3_module *)pVtab->pModule;
156346 if( pMod->xFindFunction!=0 ){
156347 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
156350 *ppRight = pList->a[1].pExpr;
156356 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
156358 Expr *pLeft = pExpr->pLeft;
156359 Expr *pRight = pExpr->pRight;
156360 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
156364 assert( pRight==0 || pRight->op!=TK_COLUMN
156365 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
156372 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
156373 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
156374 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
156387 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
156388 pDerived->w.iJoin = pBase->w.iJoin;
156396 pWC->a[iChild].iParent = iParent;
156397 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
156398 pWC->a[iParent].nChild++;
156402 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
156407 if( pTerm->eOperator!=WO_AND ){
156410 if( N<pTerm->u.pAndInfo->wc.nTerm ){
156411 return &pTerm->u.pAndInfo->wc.a[N];
156418 ** two subterms are in disjunction - they are OR-ed together.
156428 ** x<y OR x=y --> x<=y
156429 ** x=y OR x=y --> x=y
156430 ** x<=y OR x<y --> x<=y
156434 ** x<y OR x>y --> x!=y
156442 u16 eOp = pOne->eOperator | pTwo->eOperator;
156448 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
156449 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
156450 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
156453 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
156454 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
156455 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
156456 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
156458 if( (eOp & (eOp-1))!=0 ){
156466 db = pWC->pWInfo->pParse->db;
156467 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
156469 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
156470 pNew->op = op;
156477 ** Analyze a term that consists of two or more OR-connected
156490 ** The term being analyzed must have two or more of OR-connected subterms.
156491 ** A single subterm might be a set of AND-connected sub-subterms.
156529 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
156566 int idxTerm /* Index of the OR-term to be analyzed */
156568 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
156569 Parse *pParse = pWInfo->pParse; /* Parser context */
156570 sqlite3 *db = pParse->db; /* Database connection */
156571 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
156572 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
156575 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
156585 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
156586 assert( pExpr->op==TK_OR );
156587 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
156589 pTerm->wtFlags |= TERM_ORINFO;
156590 pOrWc = &pOrInfo->wc;
156591 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
156595 if( db->mallocFailed ) return;
156596 assert( pOrWc->nTerm>=2 );
156603 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
156604 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
156606 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
156614 pOrTerm->u.pAndInfo = pAndInfo;
156615 pOrTerm->wtFlags |= TERM_ANDINFO;
156616 pOrTerm->eOperator = WO_AND;
156617 pOrTerm->leftCursor = -1;
156618 pAndWC = &pAndInfo->wc;
156619 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
156620 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
156621 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
156623 pAndWC->pOuter = pWC;
156624 if( !db->mallocFailed ){
156625 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
156626 assert( pAndTerm->pExpr );
156627 if( allowedOp(pAndTerm->pExpr->op)
156628 || pAndTerm->eOperator==WO_AUX
156630 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
156636 }else if( pOrTerm->wtFlags & TERM_COPIED ){
156641 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
156642 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
156643 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
156644 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
156647 if( (pOrTerm->eOperator & WO_EQ)==0 ){
156659 pOrInfo->indexable = indexable;
156660 pTerm->eOperator = WO_OR;
156661 pTerm->leftCursor = -1;
156663 pWC->hasOr = 1;
156666 /* For a two-way OR, attempt to implementation case 2.
156668 if( indexable && pOrWc->nTerm==2 ){
156671 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
156674 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
156685 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
156688 ** something other than == on a column in the single table. The 1-bit
156692 ** sure the same column is used on all terms. The 2-bit case is when
156703 int iColumn = -1; /* Column index on lhs of IN operator */
156704 int iCursor = -1; /* Table cursor common to all terms */
156715 pOrTerm = pOrWc->a;
156716 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
156717 assert( pOrTerm->eOperator & WO_EQ );
156718 pOrTerm->wtFlags &= ~TERM_OK;
156719 if( pOrTerm->leftCursor==iCursor ){
156720 /* This is the 2-bit case and we are on the second iteration and
156725 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
156726 pOrTerm->leftCursor))==0 ){
156731 testcase( pOrTerm->wtFlags & TERM_COPIED );
156732 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
156733 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
156736 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156737 iColumn = pOrTerm->u.x.leftColumn;
156738 iCursor = pOrTerm->leftCursor;
156739 pLeft = pOrTerm->pExpr->pLeft;
156747 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
156755 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
156756 assert( pOrTerm->eOperator & WO_EQ );
156757 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156758 if( pOrTerm->leftCursor!=iCursor ){
156759 pOrTerm->wtFlags &= ~TERM_OK;
156760 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
156761 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
156766 /* If the right-hand side is also a column, then the affinities
156770 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
156771 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
156775 pOrTerm->wtFlags |= TERM_OK;
156791 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
156792 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
156793 assert( pOrTerm->eOperator & WO_EQ );
156794 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156795 assert( pOrTerm->leftCursor==iCursor );
156796 assert( pOrTerm->u.x.leftColumn==iColumn );
156797 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
156798 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
156799 pLeft = pOrTerm->pExpr->pLeft;
156808 pNew->x.pList = pList;
156812 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
156840 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
156841 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
156843 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
156844 aff2 = sqlite3ExprAffinity(pExpr->pRight);
156852 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
156863 SrcList *pSrc = pS->pSrc;
156864 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
156865 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
156866 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
156867 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
156868 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
156871 for(i=0; i<pSrc->nSrc; i++){
156872 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
156873 if( pSrc->a[i].fg.isUsing==0 ){
156874 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
156876 if( pSrc->a[i].fg.isTabFunc ){
156877 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
156881 pS = pS->pPrior;
156892 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
156903 int j /* Start looking with the j-th pFrom entry */
156909 iCur = pFrom->a[j].iCursor;
156910 for(pIdx=pFrom->a[j].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
156911 if( pIdx->aColExpr==0 ) continue;
156912 for(i=0; i<pIdx->nKeyCol; i++){
156913 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
156914 assert( pIdx->bHasExpr );
156915 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
156916 && pExpr->op!=TK_STRING
156924 }while( ++j < pFrom->nSrc );
156941 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
156943 pExpr = pExpr->x.pList->a[0].pExpr;
156946 if( pExpr->op==TK_COLUMN ){
156947 aiCurCol[0] = pExpr->iTable;
156948 aiCurCol[1] = pExpr->iColumn;
156952 for(i=0; i<pFrom->nSrc; i++){
156954 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
156955 if( pIdx->aColExpr ){
156987 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
156991 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
156997 int op; /* Top-level operator. pExpr->op */
156998 Parse *pParse = pWInfo->pParse; /* Parsing context */
156999 sqlite3 *db = pParse->db; /* Database connection */
157003 if( db->mallocFailed ){
157006 assert( pWC->nTerm > idxTerm );
157007 pTerm = &pWC->a[idxTerm];
157008 pMaskSet = &pWInfo->sMaskSet;
157009 pExpr = pTerm->pExpr;
157011 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
157012 pMaskSet->bVarSelect = 0;
157013 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
157014 op = pExpr->op;
157016 assert( pExpr->pRight==0 );
157019 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
157021 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
157023 prereqAll = prereqLeft | pTerm->prereqRight;
157025 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
157026 if( pExpr->pLeft==0
157028 || pExpr->x.pList!=0
157032 prereqAll = prereqLeft | pTerm->prereqRight;
157035 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
157046 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
157049 extraRight = x-1; /* ON clause terms may not be used with an index
157062 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
157069 pTerm->prereqAll = prereqAll;
157070 pTerm->leftCursor = -1;
157071 pTerm->iParent = -1;
157072 pTerm->eOperator = 0;
157075 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
157076 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
157077 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
157079 if( pTerm->u.x.iField>0 ){
157081 assert( pLeft->op==TK_VECTOR );
157083 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
157087 pTerm->leftCursor = aiCurCol[0];
157088 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157089 pTerm->u.x.leftColumn = aiCurCol[1];
157090 pTerm->eOperator = operatorMask(op) & opMask;
157092 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
157099 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
157100 assert( pTerm->u.x.iField==0 );
157101 if( pTerm->leftCursor>=0 ){
157104 if( db->mallocFailed ){
157110 pNew = &pWC->a[idxNew];
157112 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
157113 pTerm = &pWC->a[idxTerm];
157114 pTerm->wtFlags |= TERM_COPIED;
157117 pTerm->eOperator |= WO_EQUIV;
157124 pNew->wtFlags |= exprCommute(pParse, pDup);
157125 pNew->leftCursor = aiCurCol[0];
157126 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157127 pNew->u.x.leftColumn = aiCurCol[1];
157129 pNew->prereqRight = prereqLeft | extraRight;
157130 pNew->prereqAll = prereqAll;
157131 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
157138 pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */
157139 pExpr->u.zToken = "false";
157141 pTerm->prereqAll = 0;
157142 pTerm->eOperator = 0;
157162 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
157167 pList = pExpr->x.pList;
157169 assert( pList->nExpr==2 );
157174 sqlite3ExprDup(db, pExpr->pLeft, 0),
157175 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
157180 pTerm = &pWC->a[idxTerm];
157190 else if( pExpr->op==TK_OR ){
157191 assert( pWC->op==TK_AND );
157193 pTerm = &pWC->a[idxTerm];
157202 else if( pExpr->op==TK_NOTNULL ){
157203 if( pExpr->pLeft->op==TK_COLUMN
157204 && pExpr->pLeft->iColumn>=0
157208 Expr *pLeft = pExpr->pLeft;
157219 pNewTerm = &pWC->a[idxNew];
157220 pNewTerm->prereqRight = 0;
157221 pNewTerm->leftCursor = pLeft->iTable;
157222 pNewTerm->u.x.leftColumn = pLeft->iColumn;
157223 pNewTerm->eOperator = WO_GT;
157225 pTerm = &pWC->a[idxTerm];
157226 pTerm->wtFlags |= TERM_COPIED;
157227 pNewTerm->prereqAll = pTerm->prereqAll;
157243 ** for LIKE) then the lower-bound is made all uppercase and the upper-
157247 else if( pExpr->op==TK_FUNCTION
157248 && pWC->op==TK_AND
157252 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
157261 pLeft = pExpr->x.pList->a[1].pExpr;
157267 /* Convert the lower bound to upper-case and the upper bound to
157268 ** lower-case (upper-case is less than lower-case in ASCII) so that
157271 if( noCase && !pParse->db->mallocFailed ){
157274 pTerm->wtFlags |= TERM_LIKE;
157275 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
157276 pStr1->u.zToken[i] = sqlite3Toupper(c);
157277 pStr2->u.zToken[i] = sqlite3Tolower(c);
157281 if( !db->mallocFailed ){
157283 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
157292 if( c=='A'-1 ) isComplete = 0;
157314 pTerm = &pWC->a[idxTerm];
157322 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
157323 ** new terms for each component comparison - "a = ?" and "b = ?". The
157328 ** is not a sub-select.
157330 ** tag-20220128a
157332 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
157333 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
157334 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
157335 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
157336 || (pExpr->pRight->flags & EP_xIsSelect)==0)
157337 && pWC->op==TK_AND
157343 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
157344 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
157346 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
157351 pTerm = &pWC->a[idxTerm];
157352 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
157353 pTerm->eOperator = WO_ROWVAL;
157356 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
157365 else if( pExpr->op==TK_IN
157366 && pTerm->u.x.iField==0
157367 && pExpr->pLeft->op==TK_VECTOR
157369 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values))
157371 && pExpr->x.pSelect->pWin==0
157373 && pWC->op==TK_AND
157376 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
157379 pWC->a[idxNew].u.x.iField = i+1;
157395 else if( pWC->op==TK_AND ){
157398 while( res-- > 0 ){
157411 pNewExpr->w.iJoin = pExpr->w.iJoin;
157415 pNewTerm = &pWC->a[idxNew];
157416 pNewTerm->prereqRight = prereqExpr;
157417 pNewTerm->leftCursor = pLeft->iTable;
157418 pNewTerm->u.x.leftColumn = pLeft->iColumn;
157419 pNewTerm->eOperator = WO_AUX;
157420 pNewTerm->eMatchOp = eOp2;
157422 pTerm = &pWC->a[idxTerm];
157423 pTerm->wtFlags |= TERM_COPIED;
157424 pNewTerm->prereqAll = pTerm->prereqAll;
157434 testcase( pTerm!=&pWC->a[idxTerm] );
157435 pTerm = &pWC->a[idxTerm];
157436 pTerm->prereqRight |= extraRight;
157463 pWC->op = op;
157466 if( pE2->op!=op ){
157469 sqlite3WhereSplit(pWC, pE2->pLeft, op);
157470 sqlite3WhereSplit(pWC, pE2->pRight, op);
157477 ** where-clause passed as the first argument. The value for the term
157492 Parse *pParse = pWC->pWInfo->pParse;
157493 sqlite3 *db = pParse->db;
157501 pVal->u.iValue = iVal;
157506 pVal->iTable = iReg;
157513 pTerm = &pWC->a[idx];
157514 pTerm->leftCursor = iCsr;
157515 pTerm->eOperator = WO_AUX;
157516 pTerm->eMatchOp = eMatchOp;
157539 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
157540 if( p->pGroupBy==0
157541 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
157542 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
157544 ExprList *pOrderBy = p->pOrderBy;
157545 int iCsr = p->pSrc->a[0].iCursor;
157549 for(ii=0; ii<pWC->nTerm; ii++){
157550 if( pWC->a[ii].wtFlags & TERM_CODED ){
157552 ** other, subsequent terms. It can be ignored. See tag-20220128a */
157553 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
157554 assert( pWC->a[ii].eOperator==WO_ROWVAL );
157557 if( pWC->a[ii].nChild ){
157559 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
157564 if( pWC->a[ii].leftCursor!=iCsr ) return;
157569 for(ii=0; ii<pOrderBy->nExpr; ii++){
157570 Expr *pExpr = pOrderBy->a[ii].pExpr;
157571 if( pExpr->op!=TK_COLUMN ) return;
157572 if( pExpr->iTable!=iCsr ) return;
157573 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
157577 /* All conditions are met. Add the terms to the where-clause object. */
157578 assert( p->pLimit->op==TK_LIMIT );
157579 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
157581 if( p->iOffset>0 ){
157582 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
157595 pWC->pWInfo = pWInfo;
157596 pWC->hasOr = 0;
157597 pWC->pOuter = 0;
157598 pWC->nTerm = 0;
157599 pWC->nBase = 0;
157600 pWC->nSlot = ArraySize(pWC->aStatic);
157601 pWC->a = pWC->aStatic;
157610 sqlite3 *db = pWC->pWInfo->pParse->db;
157611 assert( pWC->nTerm>=pWC->nBase );
157612 if( pWC->nTerm>0 ){
157613 WhereTerm *a = pWC->a;
157614 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
157617 /* Verify that every term past pWC->nBase is virtual */
157618 for(i=pWC->nBase; i<pWC->nTerm; i++){
157619 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
157623 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
157624 if( a->wtFlags & TERM_DYNAMIC ){
157625 sqlite3ExprDelete(db, a->pExpr);
157627 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
157628 if( a->wtFlags & TERM_ORINFO ){
157629 assert( (a->wtFlags & TERM_ANDINFO)==0 );
157630 whereOrInfoDelete(db, a->u.pOrInfo);
157632 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
157633 whereAndInfoDelete(db, a->u.pAndInfo);
157648 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
157653 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
157658 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
157664 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
157671 ** "no-inline" function to avoid the stack push overhead in the
157679 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
157680 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
157681 if( p->pRight ){
157682 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
157683 assert( p->x.pList==0 );
157685 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
157686 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
157687 }else if( p->x.pList ){
157688 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
157691 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
157692 assert( p->y.pWin!=0 );
157693 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
157694 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
157695 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
157701 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
157702 return sqlite3WhereGetMask(pMaskSet, p->iTable);
157704 assert( p->op!=TK_IF_NULL_ROW );
157716 for(i=0; i<pList->nExpr; i++){
157717 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
157737 for(i=pWC->nTerm-1; i>=0; i--){
157743 ** For table-valued-functions, transform the function arguments into
157759 if( pItem->fg.isTabFunc==0 ) return;
157760 pTab = pItem->pTab;
157762 pArgs = pItem->u1.pFuncArg;
157764 for(j=k=0; j<pArgs->nExpr; j++){
157767 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
157768 if( k>=pTab->nCol ){
157769 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
157770 pTab->zName, j);
157773 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
157775 pColRef->iTable = pItem->iCursor;
157776 pColRef->iColumn = k++;
157778 pColRef->y.pTab = pTab;
157779 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
157781 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
157783 if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){
157784 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
157785 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
157788 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
157791 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
157847 return pWInfo->nRowOut;
157855 return pWInfo->eDistinct;
157867 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
157871 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
157873 ** inner-most loop did not fit within the sorter, then we can skip all
157876 ** loop - the loop immediately outside the inner-most.
157883 ** be the continuation for the second-inner-most loop. If the ORDER BY
157885 ** be the continuation for the inner-most loop.
157888 ** inner-most loop, in the sense that a correct answer will result.
157895 if( !pWInfo->bOrderedInnerLoop ){
157897 ** continuation of the inner-most loop. */
157898 return pWInfo->iContinue;
157900 pInner = &pWInfo->a[pWInfo->nLevel-1];
157901 assert( pInner->addrNxt!=0 );
157902 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
157907 ** the aggregate-step call to min() or max(), check to see if any
157919 if( !pWInfo->bOrderedInnerLoop ) return;
157920 if( pWInfo->nOBSat==0 ) return;
157921 for(i=pWInfo->nLevel-1; i>=0; i--){
157922 pInner = &pWInfo->a[i];
157923 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
157924 sqlite3VdbeGoto(v, pInner->addrNxt);
157928 sqlite3VdbeGoto(v, pWInfo->iBreak);
157936 assert( pWInfo->iContinue!=0 );
157937 return pWInfo->iContinue;
157945 return pWInfo->iBreak;
157952 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
157959 ** Either value may be -1, indicating that cursor is not used.
157962 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
157966 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
157968 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
157970 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
157974 return pWInfo->eOnePass;
157982 return pWInfo->bDeferredSeek;
157989 pDest->n = pSrc->n;
157990 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
158003 LogEst rRun, /* Run-cost of the new entry */
158008 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
158009 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
158012 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
158016 if( pSet->n<N_OR_COST ){
158017 p = &pSet->a[pSet->n++];
158018 p->nOut = nOut;
158020 p = pSet->a;
158021 for(i=1; i<pSet->n; i++){
158022 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
158024 if( p->rRun<=rRun ) return 0;
158027 p->prereq = prereq;
158028 p->rRun = rRun;
158029 if( p->nOut>nOut ) p->nOut = nOut;
158039 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
158040 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
158041 assert( iCursor>=-1 );
158042 if( pMaskSet->ix[0]==iCursor ){
158045 for(i=1; i<pMaskSet->n; i++){
158046 if( pMaskSet->ix[i]==iCursor ){
158057 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
158059 pBlock->pNext = pWInfo->pMemToFree;
158060 pBlock->sz = nByte;
158061 pWInfo->pMemToFree = pBlock;
158070 pOldBlk--;
158071 assert( pOldBlk->sz<nByte );
158072 memcpy(pNew, pOld, pOldBlk->sz);
158082 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
158086 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
158087 pMaskSet->ix[pMaskSet->n++] = iCursor;
158091 ** If the right-hand branch of the expression is a TK_COLUMN, then return
158092 ** a pointer to the right-hand branch. Otherwise, return NULL.
158095 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
158096 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
158109 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
158111 WhereClause *pWC; /* Shorthand for pScan->pWC */
158113 int k = pScan->k; /* Where to start scanning */
158115 assert( pScan->iEquiv<=pScan->nEquiv );
158116 pWC = pScan->pWC;
158118 iColumn = pScan->aiColumn[pScan->iEquiv-1];
158119 iCur = pScan->aiCur[pScan->iEquiv-1];
158123 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
158124 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
158125 if( pTerm->leftCursor==iCur
158126 && pTerm->u.x.leftColumn==iColumn
158128 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
158129 pScan->pIdxExpr,iCur)==0)
158130 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
158132 if( (pTerm->eOperator & WO_EQUIV)!=0
158133 && pScan->nEquiv<ArraySize(pScan->aiCur)
158134 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
158137 for(j=0; j<pScan->nEquiv; j++){
158138 if( pScan->aiCur[j]==pX->iTable
158139 && pScan->aiColumn[j]==pX->iColumn ){
158143 if( j==pScan->nEquiv ){
158144 pScan->aiCur[j] = pX->iTable;
158145 pScan->aiColumn[j] = pX->iColumn;
158146 pScan->nEquiv++;
158149 if( (pTerm->eOperator & pScan->opMask)!=0 ){
158151 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
158153 Parse *pParse = pWC->pWInfo->pParse;
158154 pX = pTerm->pExpr;
158155 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
158158 assert(pX->pLeft);
158160 if( pColl==0 ) pColl = pParse->db->pDfltColl;
158161 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
158165 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
158166 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
158167 && pX->op==TK_COLUMN
158168 && pX->iTable==pScan->aiCur[0]
158169 && pX->iColumn==pScan->aiColumn[0]
158171 testcase( pTerm->eOperator & WO_IS );
158174 pScan->pWC = pWC;
158175 pScan->k = k+1;
158179 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
158180 pTerm, pScan->nEquiv);
158181 for(ii=0; ii<pScan->nEquiv; ii++){
158183 pScan->aiCur[ii], pScan->aiColumn[ii]);
158192 pWC = pWC->pOuter;
158195 if( pScan->iEquiv>=pScan->nEquiv ) break;
158196 pWC = pScan->pOrigWC;
158198 pScan->iEquiv++;
158205 ** It is factored out into a separate tail-recursion subroutine so that
158206 ** the normal whereScanInit() routine, which is a high-runner, does not
158210 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
158241 pScan->pOrigWC = pWC;
158242 pScan->pWC = pWC;
158243 pScan->pIdxExpr = 0;
158244 pScan->idxaff = 0;
158245 pScan->zCollName = 0;
158246 pScan->opMask = opMask;
158247 pScan->k = 0;
158248 pScan->aiCur[0] = iCur;
158249 pScan->nEquiv = 1;
158250 pScan->iEquiv = 1;
158253 iColumn = pIdx->aiColumn[j];
158254 if( iColumn==pIdx->pTable->iPKey ){
158257 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
158258 pScan->zCollName = pIdx->azColl[j];
158260 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
158261 pScan->zCollName = pIdx->azColl[j];
158262 pScan->aiColumn[0] = XN_EXPR;
158268 pScan->aiColumn[0] = iColumn;
158279 ** Search for terms matching the iColumn-th column of pIdx
158280 ** rather than the iColumn-th column of table iCur.
158284 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
158291 ** then try for the one with no dependencies on <expr> - in other words where
158294 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
158312 if( (p->prereqRight & notReady)==0 ){
158313 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
158314 testcase( p->eOperator & WO_IS );
158325 ** This function searches pList for an entry that matches the iCol-th column
158328 ** If such an expression is found, its index in pList->a[] is returned. If
158329 ** no expression is found, -1 is returned.
158339 const char *zColl = pIdx->azColl[iCol];
158341 for(i=0; i<pList->nExpr; i++){
158342 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
158344 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
158345 && p->iColumn==pIdx->aiColumn[iCol]
158346 && p->iTable==iBase
158348 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
158349 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
158355 return -1;
158359 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
158364 assert( iCol>=0 && iCol<pIdx->nColumn );
158365 j = pIdx->aiColumn[iCol];
158367 return pIdx->pTable->aCol[j].notNull;
158368 }else if( j==(-1) ){
158371 assert( j==(-2) );
158378 ** Return true if the DISTINCT expression-list passed as the third argument
158382 ** DISTINCT list are collectively unique and individually non-null.
158395 /* If there is more than one table or sub-select in the FROM clause of
158398 if( pTabList->nSrc!=1 ) return 0;
158399 iBase = pTabList->a[0].iCursor;
158400 pTab = pTabList->a[0].pTab;
158403 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
158404 ** current SELECT is a correlated sub-query.
158406 for(i=0; i<pDistinct->nExpr; i++){
158407 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
158409 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
158410 if( p->iTable==iBase && p->iColumn<0 ) return 1;
158421 ** comparison and select-list expressions must match those of the index.
158426 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
158428 if( pIdx->pPartIdxWhere ) continue;
158429 for(i=0; i<pIdx->nKeyCol; i++){
158435 if( i==pIdx->nKeyCol ){
158449 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
158456 ** opcodes into OP_Copy when the table is being accessed via co-routine
158469 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
158471 Vdbe *v = pParse->pVdbe;
158474 if( pParse->db->mallocFailed ) return;
158476 if( pOp->p1!=iTabCur ) continue;
158477 if( pOp->opcode==OP_Column ){
158478 pOp->opcode = OP_Copy;
158479 pOp->p1 = pOp->p2 + iRegister;
158480 pOp->p2 = pOp->p3;
158481 pOp->p3 = 0;
158482 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
158483 }else if( pOp->opcode==OP_Rowid ){
158484 pOp->opcode = OP_Sequence;
158485 pOp->p1 = iAutoidxCur;
158488 pOp->opcode = OP_Null;
158489 pOp->p3 = 0;
158500 ** are no-ops.
158506 for(i=0; i<p->nConstraint; i++){
158510 p->aConstraint[i].iColumn,
158511 p->aConstraint[i].iTermOffset,
158512 p->aConstraint[i].op,
158513 p->aConstraint[i].usable,
158516 for(i=0; i<p->nOrderBy; i++){
158519 p->aOrderBy[i].iColumn,
158520 p->aOrderBy[i].desc);
158526 for(i=0; i<p->nConstraint; i++){
158529 p->aConstraintUsage[i].argvIndex,
158530 p->aConstraintUsage[i].omit);
158532 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
158533 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
158534 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
158535 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
158536 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
158554 ** not-NULL condition on the left table of the RIGHT JOIN.
158560 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
158561 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
158562 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
158563 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
158564 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
158565 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
158566 || pTerm->pExpr->w.iJoin != pSrc->iCursor
158570 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
158571 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
158592 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
158593 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
158594 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
158595 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
158600 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
158601 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158602 if( pTerm->u.x.leftColumn<0 ) return 0;
158603 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
158604 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
158605 testcase( pTerm->pExpr->op==TK_IS );
158631 if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){
158632 Table *pTab = pIdx->pTable;
158636 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
158637 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
158638 assert( pIdx->nColumn>1 );
158639 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID );
158640 for(ii=0; ii<(pIdx->nColumn-1); ii++){
158642 int iCol = pIdx->aiColumn[ii];
158644 zName = pTab->aCol[iCol].zCnName;
158650 sqlite3OomFault(pParse->db);
158676 WhereTerm *pWCEnd; /* End of pWC->a[] */
158685 int mxBitCol; /* Maximum column in pSrc->colUsed */
158705 v = pParse->pVdbe;
158712 pTabList = pWC->pWInfo->pTabList;
158713 pSrc = &pTabList->a[pLevel->iFrom];
158714 pTable = pSrc->pTab;
158715 pWCEnd = &pWC->a[pWC->nTerm];
158716 pLoop = pLevel->pWLoop;
158718 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
158719 Expr *pExpr = pTerm->pExpr;
158723 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
158724 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom)
158727 sqlite3ExprDup(pParse->db, pExpr, 0));
158732 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158733 iCol = pTerm->u.x.leftColumn;
158734 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
158736 testcase( iCol==BMS-1 );
158739 "automatic index on %s(%s)", pTable->zName,
158740 pTable->aCol[iCol].zCnName);
158744 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
158747 pLoop->aLTerm[nKeyCol++] = pTerm;
158752 assert( nKeyCol>0 || pParse->db->mallocFailed );
158753 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
158754 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
158768 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
158770 mxBitCol = MIN(BMS-1,pTable->nCol);
158771 testcase( pTable->nCol==BMS-1 );
158772 testcase( pTable->nCol==BMS-2 );
158776 if( pSrc->colUsed & MASKBIT(BMS-1) ){
158777 nKeyCol += pTable->nCol - BMS + 1;
158781 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
158783 pLoop->u.btree.pIndex = pIdx;
158784 pIdx->zName = "auto-index";
158785 pIdx->pTable = pTable;
158788 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
158792 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158793 iCol = pTerm->u.x.leftColumn;
158794 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
158795 testcase( iCol==BMS-1 );
158798 Expr *pX = pTerm->pExpr;
158800 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
158802 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
158803 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
158805 if( ALWAYS(pX->pLeft!=0)
158806 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT
158818 assert( (u32)n==pLoop->u.btree.nEq );
158824 pIdx->aiColumn[n] = i;
158825 pIdx->azColl[n] = sqlite3StrBINARY;
158829 if( pSrc->colUsed & MASKBIT(BMS-1) ){
158830 for(i=BMS-1; i<pTable->nCol; i++){
158831 pIdx->aiColumn[n] = i;
158832 pIdx->azColl[n] = sqlite3StrBINARY;
158837 pIdx->aiColumn[n] = XN_ROWID;
158838 pIdx->azColl[n] = sqlite3StrBINARY;
158842 assert( pLevel->iIdxCur>=0 );
158843 pLevel->iIdxCur = pParse->nTab++;
158844 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
158846 VdbeComment((v, "for %s", pTable->zName));
158847 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){
158848 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
158849 pLevel->regFilter = ++pParse->nMem;
158850 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
158854 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] );
158855 if( pSrc->fg.viaCoroutine ){
158856 int regYield = pSrc->regReturn;
158858 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSrc->addrFillSub);
158861 VdbeComment((v, "next row of %s", pSrc->pTab->zName));
158863 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
158868 pLoop->wsFlags |= WHERE_PARTIALIDX;
158872 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
158874 if( pLevel->regFilter ){
158875 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
158876 regBase, pLoop->u.btree.nEq);
158879 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
158882 if( pSrc->fg.viaCoroutine ){
158884 testcase( pParse->db->mallocFailed );
158885 assert( pLevel->iIdxCur>0 );
158886 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
158887 pSrc->regResult, pLevel->iIdxCur);
158889 pSrc->fg.viaCoroutine = 0;
158891 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
158899 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
158902 sqlite3ExprDelete(pParse->db, pPartial);
158919 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
158928 int iLevel, /* Index in pWInfo->a[] that is pLevel */
158937 Parse *pParse = pWInfo->pParse; /* Parsing context */
158938 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
158939 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
158943 saved_pIdxEpr = pParse->pIdxEpr;
158944 pParse->pIdxEpr = 0;
158948 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
158959 iCur = pLevel->iTabCur;
158960 pLevel->regFilter = ++pParse->nMem;
158963 ** to zero-filled blob of at least 80K bits, but maybe more if the
158965 ** measure the size of the table at run-time using OP_Count with
158970 pTabList = pWInfo->pTabList;
158971 iSrc = pLevel->iFrom;
158972 pItem = &pTabList->a[iSrc];
158974 pTab = pItem->pTab;
158976 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
158982 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
158985 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
158986 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
158987 Expr *pExpr = pTerm->pExpr;
158988 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
158991 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
158994 if( pLoop->wsFlags & WHERE_IPK ){
158997 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
159000 Index *pIdx = pLoop->u.btree.pIndex;
159001 int n = pLoop->u.btree.nEq;
159005 assert( pIdx->pTable==pItem->pTab );
159008 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
159012 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
159015 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
159016 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
159017 while( ++iLevel < pWInfo->nLevel ){
159019 pLevel = &pWInfo->a[iLevel];
159020 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
159021 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
159022 pLoop = pLevel->pWLoop;
159024 if( pLoop->prereq & notReady ) continue;
159025 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
159028 /* This is a candidate for bloom-filter pull-down (early evaluation).
159035 }while( iLevel < pWInfo->nLevel );
159037 pParse->pIdxEpr = saved_pIdxEpr;
159056 Parse *pParse = pWInfo->pParse;
159067 ExprList *pOrderBy = pWInfo->pOrderBy;
159070 pTab = pSrc->pTab;
159078 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
159079 pTerm->wtFlags &= ~TERM_OK;
159080 if( pTerm->leftCursor != pSrc->iCursor ) continue;
159081 if( pTerm->prereqRight & mUnusable ) continue;
159082 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
159083 testcase( pTerm->eOperator & WO_IN );
159084 testcase( pTerm->eOperator & WO_ISNULL );
159085 testcase( pTerm->eOperator & WO_IS );
159086 testcase( pTerm->eOperator & WO_ALL );
159087 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
159088 if( pTerm->wtFlags & TERM_VNULL ) continue;
159090 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
159091 assert( pTerm->u.x.leftColumn>=XN_ROWID );
159092 assert( pTerm->u.x.leftColumn<pTab->nCol );
159093 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
159099 pTerm->wtFlags |= TERM_OK;
159108 int n = pOrderBy->nExpr;
159110 Expr *pExpr = pOrderBy->a[i].pExpr;
159119 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
159121 /* First case - a direct column references without a COLLATE operator */
159122 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
159123 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
159127 /* 2nd case - a column reference with a COLLATE operator. Only match
159129 if( pExpr->op==TK_COLLATE
159130 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
159131 && pE2->iTable==pSrc->iCursor
159135 assert( pExpr->u.zToken!=0 );
159136 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
159137 pExpr->iColumn = pE2->iColumn;
159138 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
159139 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
159141 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
159149 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
159150 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
159151 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
159159 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
159168 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
159171 pIdxInfo->aConstraint = pIdxCons;
159172 pIdxInfo->aOrderBy = pIdxOrderBy;
159173 pIdxInfo->aConstraintUsage = pUsage;
159174 pHidden->pWC = pWC;
159175 pHidden->pParse = pParse;
159176 pHidden->eDistinct = eDistinct;
159177 pHidden->mIn = 0;
159178 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
159180 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
159181 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
159183 op = pTerm->eOperator & WO_ALL;
159185 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
159186 pHidden->mIn |= SMASKBIT32(j);
159191 pIdxCons[j].op = pTerm->eMatchOp;
159208 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
159211 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
159223 pIdxInfo->nConstraint = j;
159225 Expr *pExpr = pOrderBy->a[i].pExpr;
159227 assert( pExpr->op==TK_COLUMN
159228 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
159229 && pExpr->iColumn==pExpr->pLeft->iColumn) );
159230 pIdxOrderBy[j].iColumn = pExpr->iColumn;
159231 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
159234 pIdxInfo->nOrderBy = j;
159249 assert( pHidden->pParse!=0 );
159250 assert( pHidden->pParse->db==db );
159251 for(i=0; i<pIdxInfo->nConstraint; i++){
159252 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
159253 pHidden->aRhs[i] = 0;
159271 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
159275 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
159279 pParse->db->nSchemaLock++;
159280 rc = pVtab->pModule->xBestIndex(pVtab, p);
159281 pParse->db->nSchemaLock--;
159286 sqlite3OomFault(pParse->db);
159287 }else if( !pVtab->zErrMsg ){
159290 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
159293 if( pTab->u.vtab.p->bAllSchemas ){
159296 sqlite3_free(pVtab->zErrMsg);
159297 pVtab->zErrMsg = 0;
159312 ** into the aSample[] array - it is an index into a virtual set of samples
159323 IndexSample *aSample = pIdx->aSample;
159337 assert( pIdx->nSample>0 );
159338 assert( pRec->nField>0 );
159385 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
159386 nField = pIdx->nKeyCol;
159388 nField = pIdx->nColumn;
159390 nField = MIN(pRec->nField, nField);
159392 iSample = pIdx->nSample * nField;
159404 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
159410 pRec->nField = n;
159413 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
159416 iLower = aSample[iSamp].anLt[n-1];
159418 res = -1;
159421 iCol = n-1;
159430 if( pParse->db->mallocFailed==0 ){
159433 assert( i<pIdx->nSample );
159434 assert( iCol==nField-1 );
159435 pRec->nField = nField;
159437 || pParse->db->mallocFailed
159440 /* Unless i==pIdx->nSample, indicating that pRec is larger than
159443 assert( i<=pIdx->nSample && i>=0 );
159444 pRec->nField = iCol+1;
159445 assert( i==pIdx->nSample
159447 || pParse->db->mallocFailed );
159452 ** If (i>0), then pRec must also be greater than sample (i-1). */
159454 pRec->nField = iCol;
159456 || pParse->db->mallocFailed || CORRUPT_DB );
159459 pRec->nField = nField;
159460 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
159461 || pParse->db->mallocFailed || CORRUPT_DB );
159469 assert( iCol==nField-1 );
159474 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
159477 if( i>=pIdx->nSample ){
159478 iUpper = pIdx->nRowEst0;
159486 iGap = iUpper - iLower;
159494 aStat[1] = pIdx->aAvgEq[nField-1];
159497 /* Restore the pRec->nField value before returning. */
159498 pRec->nField = nField;
159517 if( pTerm->truthProb<=0 ){
159518 nRet += pTerm->truthProb;
159519 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
159520 nRet -= 20; assert( 20==sqlite3LogEst(4) );
159532 assert( iCol>=0 && iCol<pIdx->nColumn );
159533 if( !pIdx->zColAff ){
159536 assert( pIdx->zColAff[iCol]!=0 );
159537 return pIdx->zColAff[iCol];
159545 ** range-scan on a skip-scan index. For example:
159550 ** Value pLoop->nOut is currently set to the estimated number of rows
159561 ** N is the total number of samples, the pLoop->nOut value is adjusted
159564 ** nOut = nOut * ( min(U - L, 1) / N )
159585 Index *p = pLoop->u.btree.pIndex;
159586 int nEq = pLoop->u.btree.nEq;
159587 sqlite3 *db = pParse->db;
159588 int nLower = -1;
159589 int nUpper = p->nSample+1;
159598 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
159600 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
159604 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
159605 nUpper = p2 ? 0 : p->nSample;
159611 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
159612 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
159622 nDiff = (nUpper - nLower);
159631 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
159632 pLoop->nOut -= nAdjust;
159634 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
159635 nLower, nUpper, nAdjust*-1, pLoop->nOut));
159665 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
159673 ** left-most column of the index). Or, if the query is:
159698 int nOut = pLoop->nOut;
159702 Index *p = pLoop->u.btree.pIndex;
159703 int nEq = pLoop->u.btree.nEq;
159705 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
159706 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
159708 if( nEq==pBuilder->nRecValid ){
159709 UnpackedRecord *pRec = pBuilder->pRec;
159711 int nBtm = pLoop->u.btree.nBtm;
159712 int nTop = pLoop->u.btree.nTop;
159717 ** key-prefix formed by the nEq values matched against the nEq left-most
159731 ** The number of rows between the two bounds is then just iUpper-iLower.
159735 int iLwrIdx = -2; /* aSample[] for the lower bound */
159736 int iUprIdx = -1; /* aSample[] for the upper bound */
159739 testcase( pRec->nField!=pBuilder->nRecValid );
159740 pRec->nField = pBuilder->nRecValid;
159745 iUpper = p->nRowEst0;
159747 /* Note: this call could be optimized away - since the same values must
159754 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
159755 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
159756 assert( p->aSortOrder!=0 );
159757 if( p->aSortOrder[nEq] ){
159766 Expr *pExpr = pLower->pExpr->pRight;
159773 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
159775 nOut--;
159783 Expr *pExpr = pUpper->pExpr->pRight;
159790 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
159792 nOut--;
159797 pBuilder->pRec = pRec;
159800 nNew = sqlite3LogEst(iUpper - iLower);
159805 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
159826 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 );
159831 ** has an application-defined likelihood(), assume the range is
159832 ** reduced by an additional 75%. This means that, by default, an open-ended
159836 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
159837 nNew -= 20;
159840 nOut -= (pLower!=0) + (pUpper!=0);
159844 if( pLoop->nOut>nOut ){
159846 pLoop->nOut, nOut));
159849 pLoop->nOut = (LogEst)nOut;
159857 ** the histogram data. This only works when x is the left-most
159864 ** non-zero.
159877 Index *p = pBuilder->pNew->u.btree.pIndex;
159878 int nEq = pBuilder->pNew->u.btree.nEq;
159879 UnpackedRecord *pRec = pBuilder->pRec;
159885 assert( nEq<=p->nColumn );
159886 assert( p->aSample!=0 );
159887 assert( p->nSample>0 );
159888 assert( pBuilder->nRecValid<nEq );
159892 if( pBuilder->nRecValid<(nEq-1) ){
159898 if( nEq>=p->nColumn ){
159903 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
159904 pBuilder->pRec = pRec;
159907 pBuilder->nRecValid = nEq;
159911 p->zName, nEq-1, (int)a[1]));
159921 ** an IN constraint where the right-hand side of the IN operator
159928 ** non-zero.
159941 Index *p = pBuilder->pNew->u.btree.pIndex;
159942 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
159943 int nRecValid = pBuilder->nRecValid;
159949 assert( p->aSample!=0 );
159950 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
159952 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
159954 pBuilder->nRecValid = nRecValid;
159962 assert( pBuilder->nRecValid==nRecValid );
159974 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
159979 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
159980 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
159981 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
159982 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
159983 if( pTerm->eOperator & WO_SINGLE ){
159984 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
159986 pTerm->leftCursor, pTerm->u.x.leftColumn);
159987 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
159989 pTerm->u.pOrInfo->indexable);
159991 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
159994 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
159995 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
159999 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
160000 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
160002 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
160003 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
160005 if( pTerm->iParent>=0 ){
160006 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
160009 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
160020 for(i=0; i<pWC->nTerm; i++){
160021 sqlite3WhereTermPrint(&pWC->a[i], i);
160031 WhereInfo *pWInfo = pWC->pWInfo;
160032 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
160033 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
160034 Table *pTab = pItem->pTab;
160035 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
160036 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
160037 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
160039 pItem->zAlias ? pItem->zAlias : pTab->zName);
160040 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
160042 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
160044 int i = sqlite3Strlen30(zName) - 1;
160045 while( zName[i]!='_' ) i--;
160048 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
160054 if( p->u.vtab.idxStr ){
160056 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
160058 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
160060 sqlite3DebugPrintf(" %-19s", z);
160063 if( p->wsFlags & WHERE_SKIPSCAN ){
160064 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
160066 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
160068 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
160069 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
160071 for(i=0; i<p->nLTerm; i++){
160072 sqlite3WhereTermPrint(p->aLTerm[i], i);
160083 p->aLTerm = p->aLTermSpace;
160084 p->nLTerm = 0;
160085 p->nLSlot = ArraySize(p->aLTermSpace);
160086 p->wsFlags = 0;
160093 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
160094 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
160095 sqlite3_free(p->u.vtab.idxStr);
160096 p->u.vtab.needFree = 0;
160097 p->u.vtab.idxStr = 0;
160098 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
160099 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
160100 sqlite3DbFreeNN(db, p->u.btree.pIndex);
160101 p->u.btree.pIndex = 0;
160111 if( p->aLTerm!=p->aLTermSpace ){
160112 sqlite3DbFreeNN(db, p->aLTerm);
160113 p->aLTerm = p->aLTermSpace;
160114 p->nLSlot = ArraySize(p->aLTermSpace);
160117 p->nLTerm = 0;
160118 p->wsFlags = 0;
160122 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
160126 if( p->nLSlot>=n ) return SQLITE_OK;
160128 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
160130 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
160131 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
160132 p->aLTerm = paNew;
160133 p->nLSlot = n;
160142 if( pFrom->nLTerm > pTo->nLSlot
160143 && whereLoopResize(db, pTo, pFrom->nLTerm)
160149 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
160150 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
160151 pFrom->u.vtab.needFree = 0;
160152 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
160153 pFrom->u.btree.pIndex = 0;
160173 sqlite3WhereClauseClear(&pWInfo->sWC);
160174 while( pWInfo->pLoops ){
160175 WhereLoop *p = pWInfo->pLoops;
160176 pWInfo->pLoops = p->pNextLoop;
160179 while( pWInfo->pMemToFree ){
160180 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
160181 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
160182 pWInfo->pMemToFree = pNext;
160201 ** was added because if X uses skip-scan less than Y it still might
160204 ** than a non-covering index even if it is a proper subset.
160211 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
160214 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
160215 if( pY->nSkip > pX->nSkip ) return 0;
160216 for(i=pX->nLTerm-1; i>=0; i--){
160217 if( pX->aLTerm[i]==0 ) continue;
160218 for(j=pY->nLTerm-1; j>=0; j--){
160219 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
160223 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
160224 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
160245 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
160246 for(; p; p=p->pNextLoop){
160247 if( p->iTab!=pTemplate->iTab ) continue;
160248 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
160253 pTemplate->rRun, pTemplate->nOut,
160254 MIN(p->rRun, pTemplate->rRun),
160255 MIN(p->nOut - 1, pTemplate->nOut)));
160256 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
160257 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
160262 pTemplate->rRun, pTemplate->nOut,
160263 MAX(p->rRun, pTemplate->rRun),
160264 MAX(p->nOut + 1, pTemplate->nOut)));
160265 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
160266 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
160290 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
160291 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
160300 assert( p->rSetup==0 || pTemplate->rSetup==0
160301 || p->rSetup==pTemplate->rSetup );
160305 ** rSetup. Call this SETUP-INVARIANT */
160306 assert( p->rSetup>=pTemplate->rSetup );
160308 /* Any loop using an appliation-defined index (or PRIMARY KEY or
160310 ** than an automatic index. Unless it is a skip-scan. */
160311 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
160312 && (pTemplate->nSkip)==0
160313 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
160314 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
160315 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
160325 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
160326 && p->rSetup<=pTemplate->rSetup /* (2a) */
160327 && p->rRun<=pTemplate->rRun /* (2b) */
160328 && p->nOut<=pTemplate->nOut /* (2c) */
160338 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
160339 && p->rRun>=pTemplate->rRun /* (2a) */
160340 && p->nOut>=pTemplate->nOut /* (2b) */
160342 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
160358 ** If pBuilder->pOrSet is not NULL then we care about only the
160360 ** information is gathered in the pBuilder->pOrSet object. This special
160363 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
160375 WhereInfo *pWInfo = pBuilder->pWInfo;
160376 sqlite3 *db = pWInfo->pParse->db;
160380 if( pBuilder->iPlanLimit==0 ){
160382 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
160385 pBuilder->iPlanLimit--;
160387 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
160389 /* If pBuilder->pOrSet is defined, then only keep track of the costs
160392 if( pBuilder->pOrSet!=0 ){
160393 if( pTemplate->nLTerm ){
160395 u16 n = pBuilder->pOrSet->n;
160398 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
160399 pTemplate->nOut);
160402 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
160403 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
160412 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
160420 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
160436 sqlite3WhereLoopPrint(p, pBuilder->pWC);
160441 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
160449 p->pNextLoop = 0;
160454 WhereLoop **ppTail = &p->pNextLoop;
160461 *ppTail = pToDel->pNextLoop;
160465 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
160472 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
160473 Index *pIndex = p->u.btree.pIndex;
160474 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
160475 p->u.btree.pIndex = 0;
160494 ** TODO --> Perhaps this is something that could be improved by better
160498 ** value corresponds to -1 in LogEst notation, so this means decrement
160505 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
160506 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
160516 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
160518 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
160520 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
160521 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
160523 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
160524 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
160525 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
160526 for(j=pLoop->nLTerm-1; j>=0; j--){
160527 pX = pLoop->aLTerm[j];
160530 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
160533 sqlite3ProgressCheck(pWC->pWInfo->pParse);
160534 if( pLoop->maskSelf==pTerm->prereqAll ){
160538 ** "self-culling".
160540 ** 2022-03-24: Self-culling only applies if either the extra terms
160541 ** are straight comparison operators that are non-true with NULL
160544 if( (pTerm->eOperator & 0x3f)!=0
160545 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
160548 pLoop->wsFlags |= WHERE_SELFCULL;
160551 if( pTerm->truthProb<=0 ){
160554 pLoop->nOut += pTerm->truthProb;
160558 pLoop->nOut--;
160559 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
160560 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
160562 Expr *pRight = pTerm->pExpr->pRight;
160564 testcase( pTerm->pExpr->op==TK_IS );
160565 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
160571 pTerm->wtFlags |= TERM_HEURTRUTH;
160578 if( pLoop->nOut > nRow-iReduce ){
160579 pLoop->nOut = nRow - iReduce;
160607 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
160610 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
160619 assert( ExprUseXList(pTerm->pExpr->pLeft) );
160620 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
160621 pRhs = pTerm->pExpr->pRight;
160623 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
160625 pRhs = pRhs->x.pList->a[i].pExpr;
160632 if( pLhs->op!=TK_COLUMN
160633 || pLhs->iTable!=iCur
160634 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
160635 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
160640 testcase( pLhs->iColumn==XN_ROWID );
160642 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
160647 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
160654 ** compiled with -DSQLITE_ENABLE_COSTMULT
160663 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
160666 ** When this function is called, pBuilder->pNew->nOut contains the
160671 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
160680 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
160681 Parse *pParse = pWInfo->pParse; /* Parsing context */
160682 sqlite3 *db = pParse->db; /* Database connection malloc context */
160687 Bitmask saved_prereq; /* Original value of pNew->prereq */
160688 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
160689 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
160690 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
160691 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
160692 u16 saved_nSkip; /* Original value of pNew->nSkip */
160693 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
160694 LogEst saved_nOut; /* Original value of pNew->nOut */
160700 pNew = pBuilder->pNew;
160701 assert( db->mallocFailed==0 || pParse->nErr>0 );
160702 if( pParse->nErr ){
160703 return pParse->rc;
160706 pProbe->pTable->zName,pProbe->zName,
160707 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
160709 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
160710 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
160711 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
160714 assert( pNew->u.btree.nBtm==0 );
160717 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
160719 assert( pNew->u.btree.nEq<pProbe->nColumn );
160720 assert( pNew->u.btree.nEq<pProbe->nKeyCol
160721 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
160723 saved_nEq = pNew->u.btree.nEq;
160724 saved_nBtm = pNew->u.btree.nBtm;
160725 saved_nTop = pNew->u.btree.nTop;
160726 saved_nSkip = pNew->nSkip;
160727 saved_nLTerm = pNew->nLTerm;
160728 saved_wsFlags = pNew->wsFlags;
160729 saved_prereq = pNew->prereq;
160730 saved_nOut = pNew->nOut;
160731 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
160733 pNew->rSetup = 0;
160734 rSize = pProbe->aiRowLogEst[0];
160737 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
160742 int nRecValid = pBuilder->nRecValid;
160744 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
160749 if( pTerm->prereqRight & pNew->maskSelf ) continue;
160753 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
160755 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
160760 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
160761 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
160763 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
160765 pNew->wsFlags = saved_wsFlags;
160766 pNew->u.btree.nEq = saved_nEq;
160767 pNew->u.btree.nBtm = saved_nBtm;
160768 pNew->u.btree.nTop = saved_nTop;
160769 pNew->nLTerm = saved_nLTerm;
160770 if( pNew->nLTerm>=pNew->nLSlot
160771 && whereLoopResize(db, pNew, pNew->nLTerm+1)
160773 break; /* OOM while trying to enlarge the pNew->aLTerm array */
160775 pNew->aLTerm[pNew->nLTerm++] = pTerm;
160776 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
160779 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
160780 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
160781 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
160785 Expr *pExpr = pTerm->pExpr;
160796 for(i=0; i<pNew->nLTerm-1; i++){
160797 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
160799 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
160801 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
160803 if( pProbe->hasStat1 && rLogSize>=10 ){
160810 ** the left-most index column, M==N.
160820 ** with the index, as using an index has better worst-case behavior.
160825 M = pProbe->aiRowLogEst[saved_nEq];
160827 /* TUNING v----- 10 to bias toward indexed IN */
160828 x = M + logK + 10 - (nIn + rLogSize);
160837 " nInMul=%d) prefers skip-scan\n",
160839 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
160848 pNew->wsFlags |= WHERE_COLUMN_IN;
160850 int iCol = pProbe->aiColumn[saved_nEq];
160851 pNew->wsFlags |= WHERE_COLUMN_EQ;
160852 assert( saved_nEq==pNew->u.btree.nEq );
160854 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
160856 if( iCol==XN_ROWID || pProbe->uniqNotNull
160857 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
160859 pNew->wsFlags |= WHERE_ONEROW;
160861 pNew->wsFlags |= WHERE_UNQ_WANTED;
160864 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
160866 pNew->wsFlags |= WHERE_COLUMN_NULL;
160869 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
160874 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
160875 pNew->u.btree.nBtm = nVecLen;
160878 if( pTerm->wtFlags & TERM_LIKEOPT ){
160882 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
160883 assert( pTop->wtFlags & TERM_LIKEOPT );
160884 assert( pTop->eOperator==WO_LT );
160885 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
160886 pNew->aLTerm[pNew->nLTerm++] = pTop;
160887 pNew->wsFlags |= WHERE_TOP_LIMIT;
160888 pNew->u.btree.nTop = 1;
160894 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
160895 pNew->u.btree.nTop = nVecLen;
160897 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
160898 pNew->aLTerm[pNew->nLTerm-2] : 0;
160902 /* At this point pNew->nOut is set to the number of rows expected to
160906 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
160907 assert( pNew->nOut==saved_nOut );
160908 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
160913 int nEq = ++pNew->u.btree.nEq;
160916 assert( pNew->nOut==saved_nOut );
160917 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
160920 pNew->nOut += pTerm->truthProb;
160921 pNew->nOut -= nIn;
160926 && pProbe->nSample
160927 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
160928 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
160931 Expr *pExpr = pTerm->pExpr;
160936 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
160938 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
160943 pNew->nOut = sqlite3LogEst(nOut);
160947 ** See tag-202002240-1 */
160948 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
160957 pTerm->wtFlags |= TERM_HIGHTRUTH;
160958 if( pTerm->wtFlags & TERM_HEURTRUTH ){
160962 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
160965 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
160966 pNew->nOut -= nIn;
160972 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
160977 pNew->nOut += 10;
160984 ** it to pNew->rRun, which is currently set to the cost of the index
160985 ** seek only. Then, if this is a non-covering index, add the cost of
160987 assert( pSrc->pTab->szTabRow>0 );
160988 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
160989 /* The pProbe->szIdxRow is low for an IPK table since the interior
160991 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
160992 ** under-estimate the scanning cost. */
160993 rCostIdx = pNew->nOut + 16;
160995 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
160997 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
160998 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
160999 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
161001 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
161003 nOutUnadjusted = pNew->nOut;
161004 pNew->rRun += nInMul + nIn;
161005 pNew->nOut += nInMul + nIn;
161006 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
161009 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
161010 pNew->nOut = saved_nOut;
161012 pNew->nOut = nOutUnadjusted;
161015 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
161016 && pNew->u.btree.nEq<pProbe->nColumn
161017 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
161018 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
161020 if( pNew->u.btree.nEq>3 ){
161025 pNew->nOut = saved_nOut;
161027 pBuilder->nRecValid = nRecValid;
161030 pNew->prereq = saved_prereq;
161031 pNew->u.btree.nEq = saved_nEq;
161032 pNew->u.btree.nBtm = saved_nBtm;
161033 pNew->u.btree.nTop = saved_nTop;
161034 pNew->nSkip = saved_nSkip;
161035 pNew->wsFlags = saved_wsFlags;
161036 pNew->nOut = saved_nOut;
161037 pNew->nLTerm = saved_nLTerm;
161039 /* Consider using a skip-scan if there are no WHERE clause constraints
161040 ** available for the left-most terms of the index, and if the average
161041 ** number of repeats in the left-most terms is at least 18.
161051 && saved_nEq+1<pProbe->nKeyCol
161052 && saved_nEq==pNew->nLTerm
161053 && pProbe->noSkipScan==0
161054 && pProbe->hasStat1!=0
161056 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
161057 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
161060 pNew->u.btree.nEq++;
161061 pNew->nSkip++;
161062 pNew->aLTerm[pNew->nLTerm++] = 0;
161063 pNew->wsFlags |= WHERE_SKIPSCAN;
161064 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
161065 pNew->nOut -= nIter;
161066 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
161067 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
161070 pNew->nOut = saved_nOut;
161071 pNew->u.btree.nEq = saved_nEq;
161072 pNew->nSkip = saved_nSkip;
161073 pNew->wsFlags = saved_wsFlags;
161077 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
161098 if( pIndex->bUnordered ) return 0;
161099 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
161100 for(ii=0; ii<pOB->nExpr; ii++){
161101 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
161103 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
161104 if( pExpr->iColumn<0 ) return 1;
161105 for(jj=0; jj<pIndex->nKeyCol; jj++){
161106 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
161108 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
161109 for(jj=0; jj<pIndex->nKeyCol; jj++){
161110 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
161111 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
161134 pParse = pWC->pWInfo->pParse;
161135 while( pWhere->op==TK_AND ){
161136 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
161137 pWhere = pWhere->pRight;
161139 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
161140 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
161142 pExpr = pTerm->pExpr;
161143 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
161146 && (pTerm->wtFlags & TERM_VNULL)==0
161164 for(i=0; i<pIdx->nColumn; i++){
161165 if( pIdx->aiColumn[i]==XN_EXPR
161166 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
161186 ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
161188 ** If the Expr node references the table with cursor pCk->iTabCur, then
161189 ** make sure that column is covered by the index pCk->pIdx. We know that
161190 ** all columns less than 63 (really BMS-1) are covered, so we don't need
161193 ** If the index does not cover the column, then set pWalk->eCode to
161194 ** non-zero and return WRC_Abort to stop the search.
161199 ** If pCk->pIdx contains indexed expressions and one of those expressions
161209 pCk = pWalk->u.pCovIdxCk;
161210 pIdx = pCk->pIdx;
161211 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
161212 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
161213 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
161214 pIdx = pWalk->u.pCovIdxCk->pIdx;
161215 aiColumn = pIdx->aiColumn;
161216 nColumn = pIdx->nColumn;
161218 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
161220 pCk->bUnidx = 1;
161222 }else if( pIdx->bHasExpr
161223 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
161224 pCk->bExpr = 1;
161232 ** pIdx is an index that covers all of the low-number columns used by
161233 ** pWInfo->pSelect (columns from 0 through 62) or an index that has
161262 if( pWInfo->pSelect==0 ){
161267 if( pIdx->bHasExpr==0 ){
161268 for(i=0; i<pIdx->nColumn; i++){
161269 if( pIdx->aiColumn[i]>=BMS-1 ) break;
161271 if( i>=pIdx->nColumn ){
161286 sqlite3WalkSelect(&w, pWInfo->pSelect);
161299 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
161300 ** a b-tree table, not a virtual table.
161302 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
161307 ** cost = nRow * 3.0 // full-table scan
161309 ** cost = nRow * (K+3.0) // scan of non-covering index
161316 ** the index b-tree:
161319 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
161323 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
161328 ** log(nRow) factor is omitted from a non-covering index scan in order to
161329 ** bias the scoring in favor of using an index, since the worst-case
161330 ** performance of using an index is far better than the worst-case performance
161341 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
161352 pNew = pBuilder->pNew;
161353 pWInfo = pBuilder->pWInfo;
161354 pTabList = pWInfo->pTabList;
161355 pSrc = pTabList->a + pNew->iTab;
161356 pTab = pSrc->pTab;
161357 pWC = pBuilder->pWC;
161358 assert( !IsVirtual(pSrc->pTab) );
161360 if( pSrc->fg.isIndexedBy ){
161361 assert( pSrc->fg.isCte==0 );
161363 pProbe = pSrc->u2.pIBIndex;
161365 pProbe = pTab->pIndex;
161381 aiRowEstPk[0] = pTab->nRowLogEst;
161383 pFirst = pSrc->pTab->pIndex;
161384 if( pSrc->fg.notIndexed==0 ){
161391 rSize = pTab->nRowLogEst;
161395 if( !pBuilder->pOrSet /* Not part of an OR optimization */
161396 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
161397 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
161398 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
161399 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
161401 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
161402 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
161403 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
161405 /* Generate auto-index WhereLoops */
161408 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
161410 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
161411 if( pTerm->prereqRight & pNew->maskSelf ) continue;
161413 pNew->u.btree.nEq = 1;
161414 pNew->nSkip = 0;
161415 pNew->u.btree.pIndex = 0;
161416 pNew->nLTerm = 1;
161417 pNew->aLTerm[0] = pTerm;
161418 /* TUNING: One-time cost for computing the automatic index is
161421 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
161426 pNew->rSetup = rLogSize + rSize;
161427 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
161428 pNew->rSetup += 28;
161430 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
161433 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
161434 if( pNew->rSetup<0 ) pNew->rSetup = 0;
161439 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
161440 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
161441 pNew->wsFlags = WHERE_AUTO_INDEX;
161442 pNew->prereq = mPrereq | pTerm->prereqRight;
161452 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
161454 if( pProbe->pPartIdxWhere!=0
161455 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
161456 pProbe->pPartIdxWhere)
161458 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
161461 if( pProbe->bNoQuery ) continue;
161462 rSize = pProbe->aiRowLogEst[0];
161463 pNew->u.btree.nEq = 0;
161464 pNew->u.btree.nBtm = 0;
161465 pNew->u.btree.nTop = 0;
161466 pNew->nSkip = 0;
161467 pNew->nLTerm = 0;
161468 pNew->iSortIdx = 0;
161469 pNew->rSetup = 0;
161470 pNew->prereq = mPrereq;
161471 pNew->nOut = rSize;
161472 pNew->u.btree.pIndex = pProbe;
161473 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
161476 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
161477 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
161479 pNew->wsFlags = WHERE_IPK;
161482 pNew->iSortIdx = b ? iSortIdx : 0;
161485 ** since index lookups have better worst-case performance if our
161496 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
161498 pNew->rRun = rSize + 16;
161500 if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
161501 pNew->wsFlags |= WHERE_VIEWSCAN;
161503 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
161506 pNew->nOut = rSize;
161510 if( pProbe->isCovering ){
161512 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
161514 m = pSrc->colUsed & pProbe->colNotIdxed;
161515 pNew->wsFlags = WHERE_INDEXED;
161516 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
161517 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
161520 ("-> %s is not a covering index"
161521 " according to whereIsCoveringIndex()\n", pProbe->zName));
161525 pNew->wsFlags |= isCov;
161528 ("-> %s is a covering expression index"
161529 " according to whereIsCoveringIndex()\n", pProbe->zName));
161533 ("-> %s might be a covering expression index"
161534 " according to whereIsCoveringIndex()\n", pProbe->zName));
161539 ("-> %s a covering index according to bitmasks\n",
161540 pProbe->zName, m==0 ? "is" : "is not"));
161541 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
161548 || pProbe->pPartIdxWhere!=0
161549 || pSrc->fg.isIndexedBy
161551 && pProbe->bUnordered==0
161552 && (pProbe->szIdxRow<pTab->szTabRow)
161553 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
161555 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
161558 pNew->iSortIdx = b ? iSortIdx : 0;
161563 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
161565 /* If this is a non-covering index scan, add in the cost of
161572 int iCur = pSrc->iCursor;
161573 WhereClause *pWC2 = &pWInfo->sWC;
161574 for(ii=0; ii<pWC2->nTerm; ii++){
161575 WhereTerm *pTerm = &pWC2->a[ii];
161576 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
161581 if( pTerm->truthProb<=0 ){
161582 nLookup += pTerm->truthProb;
161584 nLookup--;
161585 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
161589 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
161591 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
161593 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
161594 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
161596 ** positioned to the correct row during the right-join no-match
161601 pNew->nOut = rSize;
161606 pBuilder->bldFlags1 = 0;
161608 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
161609 /* If a non-unique index is used, or if a prefix of the key for
161610 ** unique index is used (making the index functionally non-unique)
161613 pTab->tabFlags |= TF_StatsUsed;
161616 sqlite3Stat4ProbeFree(pBuilder->pRec);
161617 pBuilder->nRecValid = 0;
161618 pBuilder->pRec = 0;
161630 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
161631 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
161632 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
161637 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
161665 WhereClause *pWC = pBuilder->pWC;
161668 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
161672 WhereLoop *pNew = pBuilder->pNew;
161673 Parse *pParse = pBuilder->pWInfo->pParse;
161674 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
161675 int nConstraint = pIdxInfo->nConstraint;
161679 pNew->prereq = mPrereq;
161683 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
161685 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
161686 pIdxCons->usable = 0;
161687 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
161688 && (pTerm->eOperator & mExclude)==0
161691 pIdxCons->usable = 1;
161697 assert( pIdxInfo->needToFreeIdxStr==0 );
161698 pIdxInfo->idxStr = 0;
161699 pIdxInfo->idxNum = 0;
161700 pIdxInfo->orderByConsumed = 0;
161701 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
161702 pIdxInfo->estimatedRows = 25;
161703 pIdxInfo->idxFlags = 0;
161704 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
161705 pHidden->mHandleIn = 0;
161708 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
161715 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
161721 mxTerm = -1;
161722 assert( pNew->nLSlot>=nConstraint );
161723 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
161724 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
161725 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
161728 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
161730 int j = pIdxCons->iTermOffset;
161733 || j>=pWC->nTerm
161734 || pNew->aLTerm[iTerm]!=0
161735 || pIdxCons->usable==0
161737 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
161738 testcase( pIdxInfo->needToFreeIdxStr );
161741 testcase( iTerm==nConstraint-1 );
161743 testcase( j==pWC->nTerm-1 );
161744 pTerm = &pWC->a[j];
161745 pNew->prereq |= pTerm->prereqRight;
161746 assert( iTerm<pNew->nLSlot );
161747 pNew->aLTerm[iTerm] = pTerm;
161754 pNew->u.vtab.omitMask |= 1<<iTerm;
161758 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
161759 pNew->u.vtab.bOmitOffset = 1;
161762 if( SMASKBIT32(i) & pHidden->mHandleIn ){
161763 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
161764 }else if( (pTerm->eOperator & WO_IN)!=0 ){
161770 pIdxInfo->orderByConsumed = 0;
161771 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
161782 if( pIdxInfo->needToFreeIdxStr ){
161783 sqlite3_free(pIdxInfo->idxStr);
161784 pIdxInfo->idxStr = 0;
161785 pIdxInfo->needToFreeIdxStr = 0;
161793 pNew->nLTerm = mxTerm+1;
161795 if( pNew->aLTerm[i]==0 ){
161796 /* The non-zero argvIdx values must be contiguous. Raise an
161798 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
161799 testcase( pIdxInfo->needToFreeIdxStr );
161803 assert( pNew->nLTerm<=pNew->nLSlot );
161804 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
161805 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
161806 pIdxInfo->needToFreeIdxStr = 0;
161807 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
161808 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
161809 pIdxInfo->nOrderBy : 0);
161810 pNew->rSetup = 0;
161811 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
161812 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
161816 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
161817 pNew->wsFlags |= WHERE_ONEROW;
161819 pNew->wsFlags &= ~WHERE_ONEROW;
161822 if( pNew->u.vtab.needFree ){
161823 sqlite3_free(pNew->u.vtab.idxStr);
161824 pNew->u.vtab.needFree = 0;
161828 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
161851 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
161853 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
161854 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
161855 if( pX->pLeft ){
161856 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
161858 zRet = (pC ? pC->zName : sqlite3StrBINARY);
161871 if( m & pHidden->mIn ){
161873 pHidden->mHandleIn &= ~m;
161875 pHidden->mHandleIn |= m;
161886 ** on the right-hand-side of constraint iCons.
161896 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
161897 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
161899 if( pH->aRhs[iCons]==0 ){
161900 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
161902 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
161903 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
161907 pVal = pH->aRhs[iCons];
161911 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
161912 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
161923 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
161924 return pHidden->eDistinct;
161930 ** prepared is read-only, then just start read transactions on all
161934 ** This is used by the (built-in) sqlite_dbpage virtual table.
161937 int nDb = pParse->db->nDb;
161942 if( DbMaskNonZero(pParse->writeMask) ){
161951 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
161972 ** mUnusable should always be configured as "not-usable" for xBestIndex.
161993 pWInfo = pBuilder->pWInfo;
161994 pParse = pWInfo->pParse;
161995 pWC = pBuilder->pWC;
161996 pNew = pBuilder->pNew;
161997 pSrc = &pWInfo->pTabList->a[pNew->iTab];
161998 assert( IsVirtual(pSrc->pTab) );
162001 pNew->rSetup = 0;
162002 pNew->wsFlags = WHERE_VIRTUALTABLE;
162003 pNew->nLTerm = 0;
162004 pNew->u.vtab.needFree = 0;
162005 nConstraint = p->nConstraint;
162006 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
162007 freeIndexInfo(pParse->db, p);
162012 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
162029 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
162042 mBestNoIn = pNew->prereq & ~mPrereq;
162057 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
162068 if( pNew->prereq==mPrereq ){
162094 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
162095 freeIndexInfo(pParse->db, p);
162096 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
162110 WhereInfo *pWInfo = pBuilder->pWInfo;
162121 pWC = pBuilder->pWC;
162122 pWCEnd = pWC->a + pWC->nTerm;
162123 pNew = pBuilder->pNew;
162125 pItem = pWInfo->pTabList->a + pNew->iTab;
162126 iCur = pItem->iCursor;
162128 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
162129 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
162131 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
162132 if( (pTerm->eOperator & WO_OR)!=0
162133 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
162135 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
162136 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
162144 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
162145 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
162146 if( (pOrTerm->eOperator & WO_AND)!=0 ){
162147 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
162148 }else if( pOrTerm->leftCursor==iCur ){
162149 tempWC.pWInfo = pWC->pWInfo;
162161 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
162162 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
162168 if( IsVirtual(pItem->pTab) ){
162199 pNew->nLTerm = 1;
162200 pNew->aLTerm[0] = pTerm;
162201 pNew->wsFlags = WHERE_MULTI_OR;
162202 pNew->rSetup = 0;
162203 pNew->iSortIdx = 0;
162204 memset(&pNew->u, 0, sizeof(pNew->u));
162207 ** of all sub-scans required by the OR-scan. However, due to rounding
162208 ** errors, it may be that the cost of the OR-scan is equal to its
162209 ** most expensive sub-scan. Add the smallest possible penalty
162216 ** the planner may elect to "OR" together a full-table scan and an
162218 pNew->rRun = sSum.a[i].rRun + 1;
162219 pNew->nOut = sSum.a[i].nOut;
162220 pNew->prereq = sSum.a[i].prereq;
162223 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
162233 WhereInfo *pWInfo = pBuilder->pWInfo;
162237 SrcList *pTabList = pWInfo->pTabList;
162239 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
162240 sqlite3 *db = pWInfo->pParse->db;
162248 pNew = pBuilder->pNew;
162251 assert( pNew->nLTerm==0 );
162252 assert( pNew->wsFlags==0 );
162253 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
162254 assert( pNew->aLTerm!=0 );
162256 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
162257 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
162259 pNew->iTab = iTab;
162260 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
162261 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
162263 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
162271 ** prevent FROM-clause terms from moving from the right side of
162275 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
162277 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
162282 if( IsVirtual(pItem->pTab) ){
162285 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
162286 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
162295 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
162298 mPrior |= pNew->maskSelf;
162299 if( rc || db->mallocFailed ){
162329 ** pOrderBy terms must be matched in strict left-to-right order.
162336 u16 nLoop, /* Number of entries in pPath->aLoop[] */
162337 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
162343 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
162359 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
162362 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
162366 ** We say the WhereLoop is "one-row" if it generates no more than one
162367 ** row of output. A WhereLoop is one-row if all of the following are true:
162370 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
162371 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
162373 ** We say the WhereLoop is "order-distinct" if the set of columns from
162375 ** row of the WhereLoop. Every one-row WhereLoop is automatically
162376 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
162377 ** is not order-distinct. To be order-distinct is not quite the same as being
162379 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
162380 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
162384 ** automatically order-distinct.
162390 nOrderBy = pOrderBy->nExpr;
162391 testcase( nOrderBy==BMS-1 );
162392 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
162394 obDone = MASKBIT(nOrderBy)-1;
162402 if( iLoop>0 ) ready |= pLoop->maskSelf;
162404 pLoop = pPath->aLoop[iLoop];
162409 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
162410 if( pLoop->u.vtab.isOrdered
162417 pLoop->u.btree.nDistinctCol = 0;
162419 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
162428 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
162430 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
162431 if( pOBExpr->iTable!=iCur ) continue;
162432 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
162435 if( pTerm->eOperator==WO_IN ){
162441 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
162442 if( j>=pLoop->nLTerm ) continue;
162444 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
162445 Parse *pParse = pWInfo->pParse;
162446 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
162447 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
162449 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
162452 testcase( pTerm->pExpr->op==TK_IS );
162457 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
162458 if( pLoop->wsFlags & WHERE_IPK ){
162462 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
162465 nKeyCol = pIndex->nKeyCol;
162466 nColumn = pIndex->nColumn;
162467 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
162468 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
162469 || !HasRowid(pIndex->pTable));
162470 /* All relevant terms of the index must also be non-NULL in order
162473 ** made at tag-20210426-1 below */
162475 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
162486 assert( j>=pLoop->u.btree.nEq
162487 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
162489 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
162490 u16 eOp = pLoop->aLTerm[j]->eOperator;
162495 ** the loop need to be marked as not order-distinct because it can
162514 ** j<pLoop->u.btree.nEq constraint above. Any equality other
162517 Expr *pX = pLoop->aLTerm[j]->pExpr;
162518 for(i=j+1; i<pLoop->u.btree.nEq; i++){
162519 if( pLoop->aLTerm[i]->pExpr==pX ){
162520 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
162529 ** (revIdx) for the j-th column of the index.
162532 iColumn = pIndex->aiColumn[j];
162533 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
162534 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
162541 ** WhereLoop is not well-ordered. tag-20210426-1
162545 && j>=pLoop->u.btree.nEq
162546 && pIndex->pTable->aCol[iColumn].notNull==0
162555 /* Find the ORDER BY term that corresponds to the j-th column
162561 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
162567 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
162568 if( pOBExpr->iTable!=iCur ) continue;
162569 if( pOBExpr->iColumn!=iColumn ) continue;
162571 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
162577 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
162578 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
162581 pLoop->u.btree.nDistinctCol = j+1;
162591 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
162596 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
162601 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
162602 if( j==pLoop->u.btree.nEq ){
162603 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
162627 } /* end-if not one-row */
162631 orderDistinctMask |= pLoop->maskSelf;
162636 p = pOrderBy->a[i].pExpr;
162637 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
162644 } /* End the loop over all WhereLoops from outer-most down to inner-most */
162647 for(i=nOrderBy-1; i>0; i--){
162648 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
162653 return -1;
162660 ** BY clause - and so any order that groups rows as required satisfies the
162677 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
162678 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
162681 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
162682 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
162683 return pWInfo->sorted;
162691 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
162692 if( pLast ) zName[i++] = pLast->cId;
162714 ** Or, if the order-by clause has X terms but only the last Y
162715 ** terms are out of order, then block-sorting will reduce the
162731 assert( pWInfo->pSelect!=0 );
162732 assert( pWInfo->pSelect->pEList!=0 );
162734 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
162738 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
162746 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
162751 if( pWInfo->iLimit<nRow ){
162752 nRow = pWInfo->iLimit;
162754 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
162757 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
162764 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
162766 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
162796 pParse = pWInfo->pParse;
162797 nLoop = pWInfo->nLevel;
162799 ** For 2-way joins, the 5 best paths are followed.
162802 assert( nLoop<=pWInfo->pTabList->nSrc );
162803 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
162810 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
162813 nOrderBy = pWInfo->pOrderBy->nExpr;
162819 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
162825 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
162826 pFrom->aLoop = pX;
162831 ** is either zero - meaning it has not yet been initialized - or the
162846 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
162854 ** -1, indicating that the result set may or may not be ordered,
162856 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
162865 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
162871 Bitmask revMask; /* Mask of rev-order loops for (..) */
162873 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
162874 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
162875 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
162886 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
162887 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
162888 nOut = pFrom->nRow + pWLoop->nOut;
162889 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
162890 isOrdered = pFrom->isOrdered;
162894 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
162897 revMask = pFrom->revLoop;
162912 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
162913 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
162917 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
162920 /* TUNING: A full-scan of a VIEW or subquery in the outer loop
162922 if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){
162923 rCost += -10;
162924 nOut += -30;
162928 ** mxChoice best-so-far paths.
162930 ** First look for an existing path among best-so-far paths
162934 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
162935 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
162936 ** of legal values for isOrdered, -1..64.
162939 if( pTo->maskLoop==maskNew
162940 && ((pTo->isOrdered^isOrdered)&0x80)==0
162942 testcase( jj==nTo-1 );
162947 /* None of the existing best-so-far paths match the candidate. */
162952 ** paths currently in the best-so-far buffer. So discard
162956 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
162964 ** needs to be added to the set of best-so-far paths. */
162975 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
162981 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
162987 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
162989 if( pTo->rCost<rCost
162990 || (pTo->rCost==rCost
162991 && (pTo->nRow<nOut
162992 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
162999 "Skip %s cost=%-3d,%3d,%3d order=%c",
163002 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
163003 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
163004 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
163008 testcase( pTo->rCost==rCost );
163011 testcase( pTo->rCost==rCost+1 );
163017 "Update %s cost=%-3d,%3d,%3d order=%c",
163020 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
163021 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
163022 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
163027 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
163028 pTo->revLoop = revMask;
163029 pTo->nRow = nOut;
163030 pTo->rCost = rCost;
163031 pTo->rUnsorted = rUnsorted;
163032 pTo->isOrdered = isOrdered;
163033 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
163034 pTo->aLoop[iLoop] = pWLoop;
163040 if( pTo->rCost>mxCost
163041 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
163043 mxCost = pTo->rCost;
163044 mxUnsorted = pTo->rUnsorted;
163054 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
163056 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
163057 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
163058 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
163059 if( pTo->isOrdered>0 ){
163060 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
163077 sqlite3StackFreeNN(pParse->db, pSpace);
163084 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
163086 assert( pWInfo->nLevel==nLoop );
163089 WhereLevel *pLevel = pWInfo->a + iLoop;
163090 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
163091 pLevel->iFrom = pWLoop->iTab;
163092 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
163094 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
163095 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
163096 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
163100 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
163101 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
163102 if( rc==pWInfo->pResultSet->nExpr ){
163103 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
163106 pWInfo->bOrderedInnerLoop = 0;
163107 if( pWInfo->pOrderBy ){
163108 pWInfo->nOBSat = pFrom->isOrdered;
163109 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
163110 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
163111 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
163113 if( pWInfo->pSelect->pOrderBy
163114 && pWInfo->nOBSat > pWInfo->pSelect->pOrderBy->nExpr ){
163115 pWInfo->nOBSat = pWInfo->pSelect->pOrderBy->nExpr;
163118 pWInfo->revMask = pFrom->revLoop;
163119 if( pWInfo->nOBSat<=0 ){
163120 pWInfo->nOBSat = 0;
163122 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
163127 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
163128 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
163131 if( rc==pWInfo->pOrderBy->nExpr ){
163132 pWInfo->bOrderedInnerLoop = 1;
163133 pWInfo->revMask = m;
163138 && pWInfo->nOBSat==1
163139 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
163141 pWInfo->bOrderedInnerLoop = 1;
163144 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
163145 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
163148 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
163149 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
163151 assert( pWInfo->sorted==0 );
163152 if( nOrder==pWInfo->pOrderBy->nExpr ){
163153 pWInfo->sorted = 1;
163154 pWInfo->revMask = revMask;
163160 pWInfo->nRowOut = pFrom->nRow;
163163 sqlite3StackFreeNN(pParse->db, pSpace);
163171 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
163174 ** Return non-zero on success, if this query can be handled by this
163175 ** no-frills query planner. Return zero if this query needs the
163176 ** general-purpose query planner.
163190 pWInfo = pBuilder->pWInfo;
163191 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
163192 assert( pWInfo->pTabList->nSrc>=1 );
163193 pItem = pWInfo->pTabList->a;
163194 pTab = pItem->pTab;
163196 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
163197 testcase( pItem->fg.isIndexedBy );
163198 testcase( pItem->fg.notIndexed );
163201 iCur = pItem->iCursor;
163202 pWC = &pWInfo->sWC;
163203 pLoop = pBuilder->pNew;
163204 pLoop->wsFlags = 0;
163205 pLoop->nSkip = 0;
163206 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
163207 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
163209 testcase( pTerm->eOperator & WO_IS );
163210 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
163211 pLoop->aLTerm[0] = pTerm;
163212 pLoop->nLTerm = 1;
163213 pLoop->u.btree.nEq = 1;
163215 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
163217 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
163219 assert( pLoop->aLTermSpace==pLoop->aLTerm );
163221 || pIdx->pPartIdxWhere!=0
163222 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
163224 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
163225 for(j=0; j<pIdx->nKeyCol; j++){
163227 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
163229 testcase( pTerm->eOperator & WO_IS );
163230 pLoop->aLTerm[j] = pTerm;
163232 if( j!=pIdx->nKeyCol ) continue;
163233 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
163234 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
163235 pLoop->wsFlags |= WHERE_IDX_ONLY;
163237 pLoop->nLTerm = j;
163238 pLoop->u.btree.nEq = j;
163239 pLoop->u.btree.pIndex = pIdx;
163241 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
163245 if( pLoop->wsFlags ){
163246 pLoop->nOut = (LogEst)1;
163247 pWInfo->a[0].pWLoop = pLoop;
163248 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
163249 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
163250 pWInfo->a[0].iTabCur = iCur;
163251 pWInfo->nRowOut = 1;
163252 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
163253 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
163254 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
163256 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
163258 pLoop->cId = '0';
163274 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
163275 pWalker->eCode = 0;
163282 ** Return true if the expression contains no non-deterministic SQL
163283 ** functions. Do not consider non-deterministic SQL functions that are
163284 ** part of sub-select statements.
163307 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
163308 p->cId = zLabel[i%(sizeof(zLabel)-1)];
163328 ** 5) The table must not have an inner-join ON or USING clause if there is
163332 ** the right-most table of a subquery that was flattened into the
163333 ** main query and that subquery was the right-hand operand of an
163363 assert( pWInfo->nLevel>=2 );
163364 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
163368 assert( pWInfo->pResultSet!=0 );
163369 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
163371 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
163372 if( pWInfo->pOrderBy ){
163373 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
163375 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
163376 for(i=pWInfo->nLevel-1; i>=1; i--){
163380 pLoop = pWInfo->a[i].pWLoop;
163381 pItem = &pWInfo->pTabList->a[pLoop->iTab];
163382 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
163383 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
163384 && (pLoop->wsFlags & WHERE_ONEROW)==0
163388 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
163389 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
163390 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
163391 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
163392 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
163393 || pTerm->pExpr->w.iJoin!=pItem->iCursor
163399 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
163400 && pTerm->pExpr->w.iJoin==pItem->iCursor
163406 WHERETRACE(0xffffffff, ("-> drop loop %c not used\n", pLoop->cId));
163407 notReady &= ~pLoop->maskSelf;
163408 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
163409 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
163410 pTerm->wtFlags |= TERM_CODED;
163413 if( i!=pWInfo->nLevel-1 ){
163414 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
163415 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
163417 pWInfo->nLevel--;
163418 assert( pWInfo->nLevel>0 );
163432 ** (3) Bloom-filter processing is not disabled. (Checked by the
163447 assert( pWInfo->nLevel>=2 );
163448 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
163449 for(i=0; i<pWInfo->nLevel; i++){
163450 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
163452 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
163453 Table *pTab = pItem->pTab;
163454 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
163455 pTab->tabFlags |= TF_StatsUsed;
163457 && (pLoop->wsFlags & reqFlags)==reqFlags
163458 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
163459 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
163461 if( nSearch > pTab->nRowLogEst ){
163462 testcase( pItem->fg.jointype & JT_LEFT );
163463 pLoop->wsFlags |= WHERE_BLOOMFILTER;
163464 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
163466 "-> use Bloom-filter on loop %c because there are ~%.1e "
163468 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
163469 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
163472 nSearch += pLoop->nOut;
163478 ** free the Parse->pIdxEpr list when the Parse object is destroyed.
163482 while( pParse->pIdxEpr!=0 ){
163483 IndexedExpr *p = pParse->pIdxEpr;
163484 pParse->pIdxEpr = p->pIENext;
163485 sqlite3ExprDelete(db, p->pExpr);
163496 ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
163502 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
163503 Index *pIdx, /* The index-on-expression that contains the expressions */
163510 assert( pIdx->bHasExpr );
163511 pTab = pIdx->pTable;
163512 for(i=0; i<pIdx->nColumn; i++){
163514 int j = pIdx->aiColumn[i];
163517 pExpr = pIdx->aColExpr->a[i].pExpr;
163518 testcase( pTabItem->fg.jointype & JT_LEFT );
163519 testcase( pTabItem->fg.jointype & JT_RIGHT );
163520 testcase( pTabItem->fg.jointype & JT_LTORJ );
163521 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
163522 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
163523 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
163529 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
163531 p->pIENext = pParse->pIdxEpr;
163534 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
163538 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
163539 p->iDataCur = pTabItem->iCursor;
163540 p->iIdxCur = iIdxCur;
163541 p->iIdxCol = i;
163542 p->bMaybeNullRow = bMaybeNullRow;
163543 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
163544 p->aff = pIdx->zColAff[i];
163547 p->zIdxName = pIdx->zName;
163549 pParse->pIdxEpr = p;
163550 if( p->pIENext==0 ){
163575 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
163579 ** end |-- by sqlite3WhereEnd()
163586 ** scanning through all values on the right-hand side of the IN.
163589 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
163599 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
163609 ** inner loops (or around the "..." if the test occurs within the inner-
163658 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
163662 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
163679 db = pParse->db;
163683 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
163684 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
163689 testcase( pTabList->nSrc==BMS );
163690 if( pTabList->nSrc>BMS ){
163700 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
163705 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
163706 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
163709 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
163711 if( db->mallocFailed ){
163716 pWInfo->pParse = pParse;
163717 pWInfo->pTabList = pTabList;
163718 pWInfo->pOrderBy = pOrderBy;
163720 pWInfo->pWhere = pWhere;
163722 pWInfo->pResultSet = pResultSet;
163723 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
163724 pWInfo->nLevel = nTabList;
163725 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
163726 pWInfo->wctrlFlags = wctrlFlags;
163727 pWInfo->iLimit = iAuxArg;
163728 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
163729 pWInfo->pSelect = pSelect;
163730 memset(&pWInfo->nOBSat, 0,
163731 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
163732 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
163733 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
163734 pMaskSet = &pWInfo->sMaskSet;
163735 pMaskSet->n = 0;
163736 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
163738 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
163740 sWLB.pWC = &pWInfo->sWC;
163745 sWLB.pNew->cId = '*';
163751 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
163752 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
163757 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
163761 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
163767 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
163770 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
163774 ** Note that bitmasks are created for all pTabList->nSrc tables in
163776 ** equal to pTabList->nSrc but might be shortened to 1 if the
163781 createMask(pMaskSet, pTabList->a[ii].iCursor);
163782 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
163783 }while( (++ii)<pTabList->nSrc );
163787 for(ii=0; ii<pTabList->nSrc; ii++){
163788 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
163797 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
163798 if( pSelect && pSelect->pLimit ){
163799 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
163801 if( pParse->nErr ) goto whereBeginError;
163803 /* The False-WHERE-Term-Bypass optimization:
163812 ** right-hand side of a LEFT or FULL JOIN.
163815 ** (4) If the expression contains non-deterministic functions
163816 ** that are not within a sub-select. This is not required
163820 ** WHERE random()>0; -- eval random() once per row
163821 ** WHERE (SELECT random())>0; -- eval random() just once overall
163829 for(ii=0; ii<sWLB.pWC->nBase; ii++){
163830 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
163832 if( pT->wtFlags & TERM_VIRTUAL ) continue;
163833 pX = pT->pExpr;
163835 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) );
163836 if( pT->prereqAll==0 /* Conditions (1) and (2) */
163839 && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 )
163841 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL);
163842 pT->wtFlags |= TERM_CODED;
163851 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
163852 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
163854 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
163857 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
163858 pWInfo->pOrderBy = pResultSet;
163881 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
163902 while( pWInfo->pLoops ){
163903 WhereLoop *p = pWInfo->pLoops;
163904 pWInfo->pLoops = p->pNextLoop;
163914 if( db->mallocFailed ) goto whereBeginError;
163915 if( pWInfo->pOrderBy ){
163916 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
163917 if( db->mallocFailed ) goto whereBeginError;
163920 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
163921 pWInfo->revMask = ALLBITS;
163923 if( pParse->nErr ){
163926 assert( db->mallocFailed==0 );
163929 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
163930 if( pWInfo->nOBSat>0 ){
163931 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
163933 switch( pWInfo->eDistinct ){
163948 for(ii=0; ii<pWInfo->nLevel; ii++){
163949 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
163957 ** This query optimization is factored out into a separate "no-inline"
163960 ** some C-compiler optimizers from in-lining the
163962 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
163965 if( pWInfo->nLevel>=2
163971 nTabList = pWInfo->nLevel;
163978 if( pWInfo->nLevel>=2
163986 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
163991 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
163994 ** to use a one-pass algorithm, determine if this is appropriate.
163996 ** A one-pass approach can be used if the caller has requested one
164000 ** * the caller has indicated that a one-pass approach can be used
164009 ** use a one-pass approach, and this is not set accurately for scans
164012 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
164014 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
164016 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
164019 && !IsVirtual(pTabList->a[0].pTab)
164022 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
164023 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
164027 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
164035 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
164040 pTabItem = &pTabList->a[pLevel->iFrom];
164041 pTab = pTabItem->pTab;
164042 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
164043 pLoop = pLevel->pWLoop;
164044 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
164048 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
164050 int iCur = pTabItem->iCursor;
164056 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
164058 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
164061 if( pWInfo->eOnePass!=ONEPASS_OFF ){
164063 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
164065 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
164066 assert( pTabItem->iCursor==pLevel->iTabCur );
164067 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
164068 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
164069 if( pWInfo->eOnePass==ONEPASS_OFF
164070 && pTab->nCol<BMS
164071 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
164072 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
164077 Bitmask b = pTabItem->colUsed;
164080 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
164081 assert( n<=pTab->nCol );
164084 if( pLoop->u.btree.pIndex!=0 && (pTab->tabFlags & TF_WithoutRowid)==0 ){
164092 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
164093 (const u8*)&pTabItem->colUsed, P4_INT64);
164096 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
164098 if( pLoop->wsFlags & WHERE_INDEXED ){
164099 Index *pIx = pLoop->u.btree.pIndex;
164103 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
164107 /* This is one term of an OR-optimization using the PRIMARY KEY of a
164109 iIndexCur = pLevel->iTabCur;
164111 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
164112 Index *pJ = pTabItem->pTab->pIndex;
164117 pJ = pJ->pNext;
164120 pWInfo->aiCurOnePass[1] = iIndexCur;
164125 iIndexCur = pParse->nTab++;
164126 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
164130 pLevel->iIdxCur = iIndexCur;
164132 assert( pIx->pSchema==pTab->pSchema );
164135 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
164137 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
164138 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
164139 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
164140 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
164141 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
164142 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
164146 VdbeComment((v, "%s", pIx->zName));
164151 for(ii=0; ii<pIx->nColumn; ii++){
164152 jj = pIx->aiColumn[ii];
164155 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
164165 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
164166 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
164168 WhereRightJoin *pRJ = pLevel->pRJ;
164169 pRJ->iMatch = pParse->nTab++;
164170 pRJ->regBloom = ++pParse->nMem;
164171 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
164172 pRJ->regReturn = ++pParse->nMem;
164173 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
164174 assert( pTab==pTabItem->pTab );
164177 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
164178 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
164180 pInfo->aColl[0] = 0;
164181 pInfo->aSortFlags[0] = 0;
164186 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
164189 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
164193 pWInfo->nOBSat = 0;
164194 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
164197 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
164198 if( db->mallocFailed ) goto whereBeginError;
164208 if( pParse->nErr ) goto whereBeginError;
164209 pLevel = &pWInfo->a[ii];
164210 wsFlags = pLevel->pWLoop->wsFlags;
164211 pSrc = &pTabList->a[pLevel->iFrom];
164212 if( pSrc->fg.isMaterialized ){
164213 if( pSrc->fg.isCorrelated ){
164214 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
164217 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
164221 assert( pTabList == pWInfo->pTabList );
164225 constructAutomaticIndex(pParse, &pWInfo->sWC, notReady, pLevel);
164230 if( db->mallocFailed ) goto whereBeginError;
164235 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
164237 pWInfo->iContinue = pLevel->addrCont;
164244 VdbeModuleComment((v, "Begin WHERE-core"));
164245 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
164251 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
164264 # define OpcodeRewriteTrace(D,K,P) /* no-op */
164272 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
164284 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
164285 if( pOp->p1!=iCur ) continue;
164286 if( pOp->opcode==OP_Close ) return 0;
164287 if( pOp->opcode==OP_OpenRead ) return 1;
164288 if( pOp->opcode==OP_OpenWrite ) return 1;
164289 if( pOp->opcode==OP_OpenDup ) return 1;
164290 if( pOp->opcode==OP_OpenAutoindex ) return 1;
164291 if( pOp->opcode==OP_OpenEphemeral ) return 1;
164302 Parse *pParse = pWInfo->pParse;
164303 Vdbe *v = pParse->pVdbe;
164307 SrcList *pTabList = pWInfo->pTabList;
164308 sqlite3 *db = pParse->db;
164314 VdbeModuleComment((v, "End WHERE-core"));
164315 for(i=pWInfo->nLevel-1; i>=0; i--){
164317 pLevel = &pWInfo->a[i];
164318 if( pLevel->pRJ ){
164321 WhereRightJoin *pRJ = pLevel->pRJ;
164322 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
164323 pLevel->addrCont = 0;
164324 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
164325 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
164329 pLoop = pLevel->pWLoop;
164330 if( pLevel->op!=OP_Noop ){
164335 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
164336 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
164337 && (pLoop->wsFlags & WHERE_INDEXED)!=0
164338 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
164339 && (n = pLoop->u.btree.nDistinctCol)>0
164340 && pIdx->aiRowLogEst[n]>=36
164342 int r1 = pParse->nMem+1;
164345 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
164347 pParse->nMem += n+1;
164348 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
164349 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
164352 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
164356 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
164357 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
164358 sqlite3VdbeChangeP5(v, pLevel->p5);
164360 VdbeCoverageIf(v, pLevel->op==OP_Next);
164361 VdbeCoverageIf(v, pLevel->op==OP_Prev);
164362 VdbeCoverageIf(v, pLevel->op==OP_VNext);
164363 if( pLevel->regBignull ){
164364 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
164365 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
164371 }else if( pLevel->addrCont ){
164372 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
164374 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
164377 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
164378 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
164379 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
164380 || pParse->db->mallocFailed );
164381 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
164382 if( pIn->eEndLoopOp!=OP_Noop ){
164383 if( pIn->nPrefix ){
164385 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
164386 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
164387 if( pLevel->iLeftJoin ){
164388 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
164393 ** return the null-row. So, if the cursor is not open yet,
164396 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
164401 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
164403 pIn->iBase, pIn->nPrefix);
164409 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
164412 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
164414 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
164415 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
164417 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
164420 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
164421 if( pLevel->pRJ ){
164422 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
164425 if( pLevel->addrSkip ){
164426 sqlite3VdbeGoto(v, pLevel->addrSkip);
164427 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
164428 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
164429 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
164432 if( pLevel->addrLikeRep ){
164433 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
164434 pLevel->addrLikeRep);
164438 if( pLevel->iLeftJoin ){
164439 int ws = pLoop->wsFlags;
164440 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
164443 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
164444 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
164447 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
164450 Index *pIx = pLevel->u.pCoveringIdx;
164451 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
164452 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
164455 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
164457 if( pLevel->op==OP_Return ){
164458 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
164460 sqlite3VdbeGoto(v, pLevel->addrFirst);
164464 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
164465 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
164468 assert( pWInfo->nLevel<=pTabList->nSrc );
164469 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
164473 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
164474 Table *pTab = pTabItem->pTab;
164476 pLoop = pLevel->pWLoop;
164482 if( pLevel->pRJ ){
164487 /* For a co-routine, change all OP_Column references to the table of
164488 ** the co-routine into OP_Copy of result contained in a register.
164491 if( pTabItem->fg.viaCoroutine ){
164492 testcase( pParse->db->mallocFailed );
164493 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
164494 pTabItem->regResult, 0);
164509 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
164510 pIdx = pLoop->u.btree.pIndex;
164511 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
164512 pIdx = pLevel->u.pCoveringIdx;
164515 && !db->mallocFailed
164517 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
164520 last = pWInfo->iEndWhere;
164522 if( pIdx->bHasExpr ){
164523 IndexedExpr *p = pParse->pIdxEpr;
164525 if( p->iIdxCur==pLevel->iIdxCur ){
164528 sqlite3DebugPrintf("Disable pParse->pIdxEpr term {%d,%d}\n",
164529 p->iIdxCur, p->iIdxCol);
164530 if( sqlite3WhereTrace & 0x5000 ) sqlite3ShowExpr(p->pExpr);
164533 p->iDataCur = -1;
164534 p->iIdxCur = -1;
164536 p = p->pIENext;
164539 k = pLevel->addrBody + 1;
164541 if( db->flags & SQLITE_VdbeAddopTrace ){
164542 printf("TRANSLATE cursor %d->%d in opcode range %d..%d\n",
164543 pLevel->iTabCur, pLevel->iIdxCur, k, last-1);
164546 pOp = sqlite3VdbeGetOp(v, k - 1);
164547 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
164548 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
164549 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
164552 pLastOp = pOp + (last - k);
164555 if( pOp->p1!=pLevel->iTabCur ){
164556 /* no-op */
164557 }else if( pOp->opcode==OP_Column
164559 || pOp->opcode==OP_Offset
164562 int x = pOp->p2;
164563 assert( pIdx->pTable==pTab );
164565 if( pOp->opcode==OP_Offset ){
164571 x = pPk->aiColumn[x];
164579 pOp->p2 = x;
164580 pOp->p1 = pLevel->iIdxCur;
164584 ** reference. Verify that this is harmless - that the
164588 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
164589 || cursorIsOpen(v,pOp->p1,k)
164590 || pOp->opcode==OP_Offset
164593 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
164594 || cursorIsOpen(v,pOp->p1,k)
164598 }else if( pOp->opcode==OP_Rowid ){
164599 pOp->p1 = pLevel->iIdxCur;
164600 pOp->opcode = OP_IdxRowid;
164602 }else if( pOp->opcode==OP_IfNullRow ){
164603 pOp->p1 = pLevel->iIdxCur;
164611 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
164619 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
164623 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
164625 pParse->withinRJSubrtn -= nRJ;
164674 ** the sub-query.
164679 ** select-list and ORDER BY expressions in the parent query are
164680 ** selected by the sub-query. For the purposes of the transformation,
164712 ** sub-query, which is always implemented as a co-routine. It then calls
164717 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
164718 ** When the sub-routine is invoked:
164720 ** * The results of all window-functions for the row are stored
164732 ** BUILT-IN WINDOW FUNCTIONS
164734 ** This implementation features the following built-in window functions:
164748 ** These are the same built-in window functions supported by Postgres.
164751 ** be implemented using an API, built-in window functions are much more
164754 ** As such, some built-in window functions use the same API as aggregate
164765 ** As well as some of the built-in window functions, aggregate window
164772 ** Implementation of built-in window function row_number(). Assumes that the
164803 ** Implementation of built-in window function dense_rank(). Assumes that
164815 if( p ) p->nStep = 1;
164823 if( p->nStep ){
164824 p->nValue++;
164825 p->nStep = 0;
164827 sqlite3_result_int64(pCtx, p->nValue);
164832 ** Implementation of built-in window function nth_value(). This
164833 ** implementation is used in "slow mode" only - when the EXCLUDE clause
164864 p->nStep++;
164865 if( iVal==p->nStep ){
164866 p->pValue = sqlite3_value_dup(apArg[0]);
164867 if( !p->pValue ){
164878 pCtx, "second argument to nth_value must be a positive integer", -1
164884 if( p && p->pValue ){
164885 sqlite3_result_value(pCtx, p->pValue);
164886 sqlite3_value_free(p->pValue);
164887 p->pValue = 0;
164900 if( p && p->pValue==0 ){
164901 p->pValue = sqlite3_value_dup(apArg[0]);
164902 if( !p->pValue ){
164912 if( p && p->pValue ){
164913 sqlite3_result_value(pCtx, p->pValue);
164914 sqlite3_value_free(p->pValue);
164915 p->pValue = 0;
164922 ** Implementation of built-in window function rank(). Assumes that
164935 p->nStep++;
164936 if( p->nValue==0 ){
164937 p->nValue = p->nStep;
164947 sqlite3_result_int64(pCtx, p->nValue);
164948 p->nValue = 0;
164953 ** Implementation of built-in window function percent_rank(). Assumes that
164968 p->nTotal++;
164980 p->nStep++;
164986 p->nValue = p->nStep;
164987 if( p->nTotal>1 ){
164988 double r = (double)p->nValue / (double)(p->nTotal-1);
164998 ** Implementation of built-in window function cume_dist(). Assumes that
165013 p->nTotal++;
165025 p->nStep++;
165031 double r = (double)(p->nStep) / (double)(p->nTotal);
165061 if( p->nTotal==0 ){
165062 p->nParam = sqlite3_value_int64(apArg[0]);
165063 if( p->nParam<=0 ){
165065 pCtx, "argument of ntile must be a positive integer", -1
165069 p->nTotal++;
165081 p->iRow++;
165086 if( p && p->nParam>0 ){
165087 int nSize = (p->nTotal / p->nParam);
165089 sqlite3_result_int64(pCtx, p->iRow+1);
165091 i64 nLarge = p->nTotal - p->nParam*nSize;
165093 i64 iRow = p->iRow;
165095 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
165100 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
165127 sqlite3_value_free(p->pVal);
165128 p->pVal = sqlite3_value_dup(apArg[0]);
165129 if( p->pVal==0 ){
165132 p->nVal++;
165146 p->nVal--;
165147 if( p->nVal==0 ){
165148 sqlite3_value_free(p->pVal);
165149 p->pVal = 0;
165156 if( p && p->pVal ){
165157 sqlite3_result_value(pCtx, p->pVal);
165163 if( p && p->pVal ){
165164 sqlite3_result_value(pCtx, p->pVal);
165165 sqlite3_value_free(p->pVal);
165166 p->pVal = 0;
165171 ** Static names for the built-in window function names. These static
165176 ** if( pFuncDef->zName==row_valueName ){ ... }
165191 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
165192 ** for built-in window functions that never call those interfaces.
165209 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
165220 ** no-op routines for their methods */
165238 ** Register those built-in window functions that are not also aggregates.
165263 for(p=pList; p; p=p->pNextWin){
165264 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
165285 ** * If the function is a built-in window function that requires the
165286 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
165295 if( pWin->zName && pWin->eFrmType==0 ){
165296 Window *p = windowFind(pParse, pList, pWin->zName);
165298 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
165299 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
165300 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
165301 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
165302 pWin->eStart = p->eStart;
165303 pWin->eEnd = p->eEnd;
165304 pWin->eFrmType = p->eFrmType;
165305 pWin->eExclude = p->eExclude;
165309 if( (pWin->eFrmType==TK_RANGE)
165310 && (pWin->pStart || pWin->pEnd)
165311 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
165317 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
165318 sqlite3 *db = pParse->db;
165319 if( pWin->pFilter ){
165341 if( pFunc->zName==aUp[i].zFunc ){
165342 sqlite3ExprDelete(db, pWin->pStart);
165343 sqlite3ExprDelete(db, pWin->pEnd);
165344 pWin->pEnd = pWin->pStart = 0;
165345 pWin->eFrmType = aUp[i].eFrmType;
165346 pWin->eStart = aUp[i].eStart;
165347 pWin->eEnd = aUp[i].eEnd;
165348 pWin->eExclude = 0;
165349 if( pWin->eStart==TK_FOLLOWING ){
165350 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
165357 pWin->pWFunc = pFunc;
165370 Select *pSubSelect; /* Current sub-select, if any */
165375 ** this function appends to the output expression-list and updates
165379 struct WindowRewrite *p = pWalker->u.pRewrite;
165380 Parse *pParse = pWalker->pParse;
165382 assert( p->pWin!=0 );
165384 /* If this function is being called from within a scalar sub-select
165388 ** to the scalar sub-select. */
165389 if( p->pSubSelect ){
165390 if( pExpr->op!=TK_COLUMN ){
165393 int nSrc = p->pSrc->nSrc;
165396 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
165402 switch( pExpr->op ){
165409 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
165410 if( pExpr->y.pWin==pWin ){
165411 assert( pWin->pOwner==pExpr );
165421 int iCol = -1;
165422 if( pParse->db->mallocFailed ) return WRC_Abort;
165423 if( p->pSub ){
165425 for(i=0; i<p->pSub->nExpr; i++){
165426 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
165433 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
165434 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
165435 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
165437 if( p->pSub ){
165438 int f = pExpr->flags & EP_Collate;
165441 sqlite3ExprDelete(pParse->db, pExpr);
165445 pExpr->op = TK_COLUMN;
165446 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
165447 pExpr->iTable = p->pWin->iEphCsr;
165448 pExpr->y.pTab = p->pTab;
165449 pExpr->flags = f;
165451 if( pParse->db->mallocFailed ) return WRC_Abort;
165455 default: /* no-op */
165462 struct WindowRewrite *p = pWalker->u.pRewrite;
165463 Select *pSave = p->pSubSelect;
165467 p->pSubSelect = pSelect;
165469 p->pSubSelect = pSave;
165476 ** Iterate through each expression in expression-list pEList. For each:
165483 ** Append the node to output expression-list (*ppSub). And replace it
165484 ** with a TK_COLUMN that reads the (N-1)th element of table
165485 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
165494 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
165519 ** Append a copy of each expression in expression-list pAppend to
165530 int nInit = pList ? pList->nExpr : 0;
165531 for(i=0; i<pAppend->nExpr; i++){
165532 sqlite3 *db = pParse->db;
165533 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
165534 if( db->mallocFailed ){
165543 pSub->op = TK_NULL;
165544 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
165545 pSub->u.zToken = 0;
165549 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
165558 ** then we have to increase the Expr->op2 values of those nodes
165564 if( pExpr->op==TK_AGG_FUNCTION
165565 && pExpr->op2>=pWalker->walkerDepth
165567 pExpr->op2++;
165573 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
165575 sqlite3ErrorMsg(pWalker->pParse,
165576 "misuse of aggregate: %s()", pExpr->u.zToken);
165583 ** any SQL window functions, this function is a no-op. Otherwise, it
165590 if( p->pWin
165591 && p->pPrior==0
165592 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
165596 sqlite3 *db = pParse->db;
165598 SrcList *pSrc = p->pSrc;
165599 Expr *pWhere = p->pWhere;
165600 ExprList *pGroupBy = p->pGroupBy;
165601 Expr *pHaving = p->pHaving;
165604 ExprList *pSublist = 0; /* Expression list for sub-query */
165605 Window *pMWin = p->pWin; /* Main window object */
165610 u32 selFlags = p->selFlags;
165618 if( (p->selFlags & SF_Aggregate)==0 ){
165621 sqlite3WalkExprList(&w, p->pOrderBy);
165624 p->pSrc = 0;
165625 p->pWhere = 0;
165626 p->pGroupBy = 0;
165627 p->pHaving = 0;
165628 p->selFlags &= ~SF_Aggregate;
165629 p->selFlags |= SF_WinRewrite;
165631 /* Create the ORDER BY clause for the sub-select. This is the concatenation
165634 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
165635 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
165636 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
165637 int nSave = pSort->nExpr;
165638 pSort->nExpr = p->pOrderBy->nExpr;
165639 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
165640 sqlite3ExprListDelete(db, p->pOrderBy);
165641 p->pOrderBy = 0;
165643 pSort->nExpr = nSave;
165649 pMWin->iEphCsr = pParse->nTab++;
165650 pParse->nTab += 3;
165652 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
165653 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
165654 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
165657 ** sub-select expression list. They are required to figure out where
165659 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
165660 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
165663 ** sub-select expression list. Also allocate two registers for each
165664 ** window function - one for the accumulator, another for interim
165666 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165668 assert( ExprUseXList(pWin->pOwner) );
165669 assert( pWin->pWFunc!=0 );
165670 pArgs = pWin->pOwner->x.pList;
165671 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
165673 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
165674 pWin->bExprArgs = 1;
165676 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
165679 if( pWin->pFilter ){
165680 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
165683 pWin->regAccum = ++pParse->nMem;
165684 pWin->regResult = ++pParse->nMem;
165685 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
165704 ("New window-function subquery in FROM clause of (%u/%p)\n",
165705 p->selId, p));
165706 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
165707 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
165710 if( p->pSrc ){
165712 p->pSrc->a[0].pSelect = pSub;
165713 p->pSrc->a[0].fg.isCorrelated = 1;
165714 sqlite3SrcListAssignCursors(pParse, p->pSrc);
165715 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
165717 pSub->selFlags |= (selFlags & SF_Aggregate);
165720 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
165725 pTab->tabFlags |= TF_Ephemeral;
165726 p->pSrc->a[0].pTab = pTab;
165737 if( db->mallocFailed ) rc = SQLITE_NOMEM;
165741 ** result-set or ORDER BY clause of the SELECT statement p. */
165745 assert( rc==SQLITE_OK || pParse->nErr!=0 );
165754 if( p->ppThis ){
165755 *p->ppThis = p->pNextWin;
165756 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
165757 p->ppThis = 0;
165767 sqlite3ExprDelete(db, p->pFilter);
165768 sqlite3ExprListDelete(db, p->pPartition);
165769 sqlite3ExprListDelete(db, p->pOrderBy);
165770 sqlite3ExprDelete(db, p->pEnd);
165771 sqlite3ExprDelete(db, p->pStart);
165772 sqlite3DbFree(db, p->zName);
165773 sqlite3DbFree(db, p->zBase);
165783 Window *pNext = p->pNextWin;
165791 ** value should be a non-negative integer. If the value is not a
165792 ** constant, change it to NULL. The fact that it is then a non-negative
165799 sqlite3ExprDelete(pParse->db, pExpr);
165800 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
165855 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
165857 pWin->eFrmType = eType;
165858 pWin->eStart = eStart;
165859 pWin->eEnd = eEnd;
165860 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
165863 pWin->eExclude = eExclude;
165864 pWin->bImplicitFrame = bImplicitFrame;
165865 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
165866 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
165870 sqlite3ExprDelete(pParse->db, pEnd);
165871 sqlite3ExprDelete(pParse->db, pStart);
165877 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
165878 ** equivalent nul-terminated string.
165888 pWin->pPartition = pPartition;
165889 pWin->pOrderBy = pOrderBy;
165891 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
165894 sqlite3ExprListDelete(pParse->db, pPartition);
165895 sqlite3ExprListDelete(pParse->db, pOrderBy);
165903 ** stored in the linked list starting at pWin->pNextWin. This function
165908 if( pWin->zBase ){
165909 sqlite3 *db = pParse->db;
165910 Window *pExist = windowFind(pParse, pList, pWin->zBase);
165914 if( pWin->pPartition ){
165916 }else if( pExist->pOrderBy && pWin->pOrderBy ){
165918 }else if( pExist->bImplicitFrame==0 ){
165923 "cannot override %s of window: %s", zErr, pWin->zBase
165926 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
165927 if( pExist->pOrderBy ){
165928 assert( pWin->pOrderBy==0 );
165929 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
165931 sqlite3DbFree(db, pWin->zBase);
165932 pWin->zBase = 0;
165943 assert( p->op==TK_FUNCTION );
165945 p->y.pWin = pWin;
165947 pWin->pOwner = p;
165948 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
165954 sqlite3WindowDelete(pParse->db, pWin);
165959 ** Possibly link window pWin into the list at pSel->pWin (window functions
165966 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
165967 pWin->pNextWin = pSel->pWin;
165968 if( pSel->pWin ){
165969 pSel->pWin->ppThis = &pWin->pNextWin;
165971 pSel->pWin = pWin;
165972 pWin->ppThis = &pSel->pWin;
165974 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
165975 pSel->selFlags |= SF_MultiPart;
165994 if( p1->eFrmType!=p2->eFrmType ) return 1;
165995 if( p1->eStart!=p2->eStart ) return 1;
165996 if( p1->eEnd!=p2->eEnd ) return 1;
165997 if( p1->eExclude!=p2->eExclude ) return 1;
165998 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
165999 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
166000 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
166003 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
166007 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
166017 ** to begin iterating through the sub-query results. It is used to allocate
166021 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
166022 Window *pMWin = pSelect->pWin;
166026 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
166027 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
166028 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
166029 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
166033 if( pMWin->pPartition ){
166034 int nExpr = pMWin->pPartition->nExpr;
166035 pMWin->regPart = pParse->nMem+1;
166036 pParse->nMem += nExpr;
166037 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
166040 pMWin->regOne = ++pParse->nMem;
166041 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
166043 if( pMWin->eExclude ){
166044 pMWin->regStartRowid = ++pParse->nMem;
166045 pMWin->regEndRowid = ++pParse->nMem;
166046 pMWin->csrApp = pParse->nTab++;
166047 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
166048 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
166049 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
166053 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166054 FuncDef *p = pWin->pWFunc;
166055 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
166065 assert( ExprUseXList(pWin->pOwner) );
166066 pList = pWin->pOwner->x.pList;
166068 pWin->csrApp = pParse->nTab++;
166069 pWin->regApp = pParse->nMem+1;
166070 pParse->nMem += 3;
166071 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
166072 assert( pKeyInfo->aSortFlags[0]==0 );
166073 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
166075 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
166077 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
166079 else if( p->zName==nth_valueName || p->zName==first_valueName ){
166080 /* Allocate two registers at pWin->regApp. These will be used to
166082 pWin->regApp = pParse->nMem+1;
166083 pWin->csrApp = pParse->nTab++;
166084 pParse->nMem += 2;
166085 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
166087 else if( p->zName==leadName || p->zName==lagName ){
166088 pWin->csrApp = pParse->nTab++;
166089 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
166104 ** code to check that the value is a non-negative integer and throws an
166109 "frame starting offset must be a non-negative integer",
166110 "frame ending offset must be a non-negative integer",
166112 "frame starting offset must be a non-negative number",
166113 "frame ending offset must be a non-negative number",
166151 ** Return the number of arguments passed to the window-function associated
166156 assert( ExprUseXList(pWin->pOwner) );
166157 pList = pWin->pOwner->x.pList;
166158 return (pList ? pList->nExpr : 0);
166181 ** in an array of accumulator registers - one for each window function
166188 ** order to reduce memory requirements - it would always be safe just
166203 ** Consider a window-frame similar to the following:
166223 ** Depending on the window-frame in question, all three cursors may not
166251 Window *pMWin = p->pMWin;
166252 ExprList *pOrderBy = pMWin->pOrderBy;
166254 Vdbe *v = sqlite3GetVdbe(p->pParse);
166255 ExprList *pPart = pMWin->pPartition;
166256 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
166258 for(i=0; i<pOrderBy->nExpr; i++){
166266 ** xInverse (if bInverse is non-zero) for each window function in the
166267 ** linked list starting at pMWin. Or, for built-in window functions
166278 ** already populated with all columns from the current row of the sub-query.
166280 ** If argument regPartSize is non-zero, then it is a register containing the
166290 Parse *pParse = p->pParse;
166293 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166294 FuncDef *pFunc = pWin->pWFunc;
166296 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
166299 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
166306 if( i!=1 || pFunc->zName!=nth_valueName ){
166307 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
166309 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
166314 if( pMWin->regStartRowid==0
166315 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
166316 && (pWin->eStart!=TK_UNBOUNDED)
166321 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
166322 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
166323 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
166324 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
166326 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
166328 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
166329 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
166332 }else if( pWin->regApp ){
166333 assert( pFunc->zName==nth_valueName
166334 || pFunc->zName==first_valueName
166337 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
166338 }else if( pFunc->xSFunc!=noopStepFunc ){
166340 if( pWin->pFilter ){
166342 assert( ExprUseXList(pWin->pOwner) );
166343 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
166344 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
166346 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
166352 if( pWin->bExprArgs ){
166356 assert( ExprUseXList(pWin->pOwner) );
166357 nArg = pWin->pOwner->x.pList->nExpr;
166359 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
166363 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
166364 pOp->p1 = csr;
166368 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
166371 assert( ExprUseXList(pWin->pOwner) );
166372 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
166376 bInverse, regArg, pWin->regAccum);
166379 if( pWin->bExprArgs ){
166397 ** pMWin. Or, for built-in window-functions that do not use the standard
166401 Parse *pParse = p->pParse;
166402 Window *pMWin = p->pMWin;
166406 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166407 if( pMWin->regStartRowid==0
166408 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
166409 && (pWin->eStart!=TK_UNBOUNDED)
166411 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
166412 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
166414 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
166415 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
166416 }else if( pWin->regApp ){
166417 assert( pMWin->regStartRowid==0 );
166421 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
166422 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
166423 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
166424 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
166426 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
166427 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
166435 ** p->pMWin list by doing a full scan of the current window frame. Store the
166441 Parse *pParse = p->pParse;
166442 Window *pMWin = p->pMWin;
166443 Vdbe *v = p->pVdbe;
166459 csr = pMWin->csrApp;
166460 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
166472 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
166473 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
166475 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166476 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
166479 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
166483 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
166486 if( pMWin->eExclude==TK_CURRENT ){
166489 }else if( pMWin->eExclude!=TK_NO ){
166494 if( pMWin->pOrderBy ){
166495 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
166497 if( pMWin->eExclude==TK_TIES ){
166514 windowAggStep(p, pMWin, csr, 0, p->regArg);
166519 sqlite3VdbeJumpHere(v, addrNext-1);
166533 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
166537 ** for per-row processing is only generated for the following built-in window
166546 Window *pMWin = p->pMWin;
166547 Vdbe *v = p->pVdbe;
166549 if( pMWin->regStartRowid ){
166552 Parse *pParse = p->pParse;
166555 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166556 FuncDef *pFunc = pWin->pWFunc;
166557 assert( ExprUseXList(pWin->pOwner) );
166558 if( pFunc->zName==nth_valueName
166559 || pFunc->zName==first_valueName
166561 int csr = pWin->csrApp;
166564 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
166566 if( pFunc->zName==nth_valueName ){
166567 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
166572 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
166573 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
166577 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
166581 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
166582 int nArg = pWin->pOwner->x.pList->nExpr;
166583 int csr = pWin->csrApp;
166586 int iEph = pMWin->iEphCsr;
166589 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
166591 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
166595 int val = (pFunc->zName==leadName ? 1 : -1);
166598 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
166600 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
166607 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
166613 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
166619 ** any equivalent initialization required by any built-in window functions
166627 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166628 FuncDef *pFunc = pWin->pWFunc;
166629 assert( pWin->regAccum );
166630 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
166632 if( pMWin->regStartRowid==0 ){
166633 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
166634 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
166635 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
166638 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
166639 assert( pWin->eStart!=TK_UNBOUNDED );
166640 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
166641 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
166645 regArg = pParse->nMem+1;
166646 pParse->nMem += nArg;
166656 if( pMWin->regStartRowid ) return 1;
166657 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
166658 FuncDef *pFunc = pWin->pWFunc;
166659 if( (pFunc->zName==nth_valueName)
166660 || (pFunc->zName==first_valueName)
166661 || (pFunc->zName==leadName)
166662 || (pFunc->zName==lagName)
166672 ** pOrderBy->nExpr. This function generates code to compare the two
166689 int nVal = pOrderBy->nExpr;
166697 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
166712 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
166714 ** If the sort-order for the ORDER BY term in the window is DESC, then the
166716 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
166720 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
166730 int regVal, /* Register containing non-negative number */
166734 Parse *pParse = p->pParse;
166736 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
166739 int regString = ++pParse->nMem; /* Reg. for constant value '' */
166745 /* Read the peer-value from each cursor into a register */
166750 assert( pOrderBy && pOrderBy->nExpr==1 );
166751 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
166761 reg1, (arith==OP_Add ? "+" : "-"), regVal,
166784 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
166799 default: assert( op==OP_Lt ); /* no-op */ break;
166810 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
166813 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
166816 ** reg1 = reg1 +/- regVal
166819 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
166836 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
166865 Parse *pParse = p->pParse;
166866 Window *pMWin = p->pMWin;
166868 Vdbe *v = p->pVdbe;
166870 int bPeer = (pMWin->eFrmType!=TK_ROWS);
166875 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
166877 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
166883 if( pMWin->eFrmType==TK_RANGE ){
166887 if( pMWin->eStart==TK_FOLLOWING ){
166889 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
166893 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
166898 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
166907 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
166918 if( pMWin->eStart==pMWin->eEnd && regCountdown
166919 && pMWin->eFrmType==TK_RANGE
166924 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
166925 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
166928 }else if( p->regRowid ){
166929 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
166930 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
166935 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
166940 csr = p->current.csr;
166941 reg = p->current.reg;
166946 csr = p->start.csr;
166947 reg = p->start.reg;
166948 if( pMWin->regStartRowid ){
166949 assert( pMWin->regEndRowid );
166950 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
166952 windowAggStep(p, pMWin, csr, 1, p->regArg);
166958 csr = p->end.csr;
166959 reg = p->end.reg;
166960 if( pMWin->regStartRowid ){
166961 assert( pMWin->regEndRowid );
166962 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
166964 windowAggStep(p, pMWin, csr, 0, p->regArg);
166969 if( op==p->eDelete ){
166987 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
166990 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
167012 pNew->zName = sqlite3DbStrDup(db, p->zName);
167013 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
167014 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
167015 pNew->pWFunc = p->pWFunc;
167016 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
167017 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
167018 pNew->eFrmType = p->eFrmType;
167019 pNew->eEnd = p->eEnd;
167020 pNew->eStart = p->eStart;
167021 pNew->eExclude = p->eExclude;
167022 pNew->regResult = p->regResult;
167023 pNew->regAccum = p->regAccum;
167024 pNew->iArgCol = p->iArgCol;
167025 pNew->iEphCsr = p->iEphCsr;
167026 pNew->bExprArgs = p->bExprArgs;
167027 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
167028 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
167029 pNew->pOwner = pOwner;
167030 pNew->bImplicitFrame = p->bImplicitFrame;
167045 for(pWin=p; pWin; pWin=pWin->pNextWin){
167048 pp = &((*pp)->pNextWin);
167064 sqlite3 *db = pParse->db;
167066 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
167078 ** and invoke the sub-routine at instruction addrGosub once for each row.
167108 ** if( (regEnd--)<=0 ){
167110 ** if( (regStart--)<=0 ){
167121 ** if( (regStart--)<=0 ){
167127 ** The pseudo-code above uses the following shorthand:
167142 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
167161 ** if( (regEnd--)<=0 ){
167165 ** if( (regStart--)<=0 ){
167171 ** if( (regEnd--)<=0 ){
167187 ** regStart = regEnd - <expr1>
167190 ** if( (regEnd--)<=0 ){
167193 ** if( (regStart--)<=0 ){
167201 ** if( (regEnd--)<=0 ){
167205 ** if( (regStart--)<=0 ){
167217 ** This is optimized of course - branches that will never be taken and
167238 ** if( (regStart--)<=0 ){
167254 ** To handle this case, the pseudo-code programs depicted above are modified
167274 ** of the outer loop - the one started by sqlite3WhereBegin().
167416 Window *pMWin = p->pWin;
167417 ExprList *pOrderBy = pMWin->pOrderBy;
167420 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
167421 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
167432 WindowCodeArg s; /* Context object for sub-routines */
167437 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
167438 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
167440 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
167441 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
167443 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
167444 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
167445 || pMWin->eExclude==TK_NO
167457 s.current.csr = pMWin->iEphCsr;
167463 ** are four options - they may never be deleted (eDelete==0), they may
167468 switch( pMWin->eStart ){
167470 if( pMWin->eFrmType!=TK_RANGE
167471 && windowExprGtZero(pParse, pMWin->pStart)
167478 if( pMWin->eEnd==TK_PRECEDING ){
167479 if( pMWin->eFrmType!=TK_RANGE
167480 && windowExprGtZero(pParse, pMWin->pEnd)
167494 /* Allocate registers for the array of values from the sub-query, the
167497 regNew = pParse->nMem+1;
167498 pParse->nMem += nInput;
167499 regRecord = ++pParse->nMem;
167500 s.regRowid = ++pParse->nMem;
167505 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
167506 regStart = ++pParse->nMem;
167508 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
167509 regEnd = ++pParse->nMem;
167515 if( pMWin->eFrmType!=TK_ROWS ){
167516 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
167517 regNewPeer = regNew + pMWin->nBufferCol;
167518 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
167519 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
167520 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
167521 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
167522 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
167525 /* Load the column values for the row returned by the sub-select
167538 if( pMWin->pPartition ){
167540 ExprList *pPart = pMWin->pPartition;
167541 int nPart = pPart->nExpr;
167542 int regNewPart = regNew + pMWin->nBufferCol;
167545 regFlushPart = ++pParse->nMem;
167546 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
167552 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
167558 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
167565 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
167566 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
167569 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
167570 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
167573 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
167574 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
167585 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
167586 assert( pMWin->eEnd==TK_FOLLOWING );
167590 if( pMWin->eStart!=TK_UNBOUNDED ){
167596 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
167597 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
167598 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
167599 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
167610 if( pMWin->eStart==TK_FOLLOWING ){
167612 if( pMWin->eEnd!=TK_UNBOUNDED ){
167613 if( pMWin->eFrmType==TK_RANGE ){
167627 if( pMWin->eEnd==TK_PRECEDING ){
167628 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
167636 if( pMWin->eEnd!=TK_UNBOUNDED ){
167637 if( pMWin->eFrmType==TK_RANGE ){
167667 if( pMWin->pPartition ){
167675 if( pMWin->eEnd==TK_PRECEDING ){
167676 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
167680 }else if( pMWin->eStart==TK_FOLLOWING ){
167686 if( pMWin->eFrmType==TK_RANGE ){
167691 if( pMWin->eEnd==TK_UNBOUNDED ){
167696 assert( pMWin->eEnd==TK_FOLLOWING );
167721 if( pMWin->pPartition ){
167722 if( pMWin->regStartRowid ){
167723 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
167724 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
167738 ** 2001-09-15
167752 ** That input file is processed by Lemon to generate a C-language
167754 ** this comment as part of the translated C-code. Edits should be made
167761 ** Disable all error recovery processing in the parser push-down
167814 sqlite3 *db = pParse->db;
167815 pParse->disableLookaside++;
167835 sqlite3ExprListDelete(pParse->db, pOrderBy);
167836 sqlite3ExprDelete(pParse->db, pLimit);
167842 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
167848 if( p->pPrior ){
167852 pLoop->pNext = pNext;
167853 pLoop->selFlags |= SF_Compound;
167855 pLoop = pLoop->pPrior;
167858 if( pLoop->pOrderBy || pLoop->pLimit ){
167860 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
167861 sqlite3SelectOpName(pNext->op));
167865 if( (p->selFlags & SF_MultiValue)==0 &&
167866 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
167879 pSelect->pWith = pWith;
167882 sqlite3WithDelete(pParse->db, pWith);
167890 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
167893 p->op = (u8)op;
167894 p->affExpr = 0;
167895 p->flags = EP_Leaf;
167897 /* p->iAgg = -1; // Not required */
167898 p->pLeft = p->pRight = 0;
167899 p->pAggInfo = 0;
167900 memset(&p->x, 0, sizeof(p->x));
167901 memset(&p->y, 0, sizeof(p->y));
167902 p->op2 = 0;
167903 p->iTable = 0;
167904 p->iColumn = 0;
167905 p->u.zToken = (char*)&p[1];
167906 memcpy(p->u.zToken, t.z, t.n);
167907 p->u.zToken[t.n] = 0;
167908 p->w.iOfst = (int)(t.z - pParse->zTail);
167909 if( sqlite3Isquote(p->u.zToken[0]) ){
167913 p->nHeight = 1;
167926 sqlite3 *db = pParse->db;
167927 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
167928 pA->op = (u8)op;
167929 sqlite3ExprDelete(db, pA->pRight);
167930 pA->pRight = 0;
167948 && pParse->db->init.busy==0
167951 pIdToken->n, pIdToken->z);
168154 ** that represent terminal and non-terminal symbols.
168160 ** (also known as: "terminal symbols") have fall-back
168164 ** YYACTIONTYPE is the data type used for "action codes" - numbers
168169 ** value associated with a terminal or non-terminal
168172 ** Each non-terminal can have a different minor type.
168194 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
168195 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
168198 ** YY_NO_ACTION The yy_action[] code for no-op
168244 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
168245 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
168262 /* Define the yytestcase() macro to be a no-op if is not already defined
168287 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
168296 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
168309 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
168321 ** shifting non-terminals after a reduce.
168833 #define YY_REDUCE_MIN (-271)
168836 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
168837 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
168838 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
168839 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
168840 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
168841 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
168842 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
168843 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
168849 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
168850 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
168851 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
168852 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
168853 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
168854 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
168857 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
168858 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
168938 /********** End of lemon-generated parsing tables *****************************/
169161 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
169164 YYMINORTYPE minor; /* The user-supplied minor token value. This
169174 int yyhwm; /* High-water mark of the stack */
169972 newSize = p->yystksz*2 + 100;
169973 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
169974 if( p->yystack==&p->yystk0 ){
169976 if( pNew ) pNew[0] = p->yystk0;
169978 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
169981 p->yystack = pNew;
169982 p->yytos = &p->yystack[idx];
169986 yyTracePrompt, p->yystksz, newSize);
169989 p->yystksz = newSize;
170010 yypParser->yyhwm = 0;
170013 yypParser->yytos = NULL;
170014 yypParser->yystack = NULL;
170015 yypParser->yystksz = 0;
170017 yypParser->yystack = &yypParser->yystk0;
170018 yypParser->yystksz = 1;
170022 yypParser->yyerrcnt = -1;
170024 yypParser->yytos = yypParser->yystack;
170025 yypParser->yystack[0].stateno = 0;
170026 yypParser->yystack[0].major = 0;
170028 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
170073 ** terminal or non-terminal is destroyed. This can happen
170088 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
170103 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
170120 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
170129 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
170134 sqlite3WithDelete(pParse->db, (yypminor->yy521));
170140 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
170146 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
170155 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
170161 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
170166 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
170173 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
170189 assert( pParser->yytos!=0 );
170190 assert( pParser->yytos > pParser->yystack );
170191 yytos = pParser->yytos--;
170196 yyTokenName[yytos->major]);
170199 yy_destructor(pParser, yytos->major, &yytos->minor);
170207 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
170209 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
170240 return pParser->yyhwm;
170246 ** is in state X and has a lookahead token Y. In a well-tested
170283 ** look-ahead token iLookAhead.
170286 YYCODETYPE iLookAhead, /* The look-ahead token */
170324 int j = i - iLookAhead + YYWILDCARD;
170347 ** Find the appropriate action for a parser given the non-terminal
170348 ** look-ahead token iLookAhead.
170352 YYCODETYPE iLookAhead /* The look-ahead token */
170387 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
170406 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
170410 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
170411 yyNewState - YY_MIN_REDUCE);
170429 yypParser->yytos++;
170431 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
170432 yypParser->yyhwm++;
170433 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
170437 if( yypParser->yytos>yypParser->yystackEnd ){
170438 yypParser->yytos--;
170443 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
170445 yypParser->yytos--;
170452 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
170454 yytos = yypParser->yytos;
170455 yytos->stateno = yyNewState;
170456 yytos->major = yyMajor;
170457 yytos->minor.yy0 = yyMinor;
170461 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
170870 ** of symbols on the right-hand side of that rule. */
170872 -1, /* (0) explain ::= EXPLAIN */
170873 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
170874 -1, /* (2) cmdx ::= cmd */
170875 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
170877 -1, /* (5) transtype ::= DEFERRED */
170878 -1, /* (6) transtype ::= IMMEDIATE */
170879 -1, /* (7) transtype ::= EXCLUSIVE */
170880 -2, /* (8) cmd ::= COMMIT|END trans_opt */
170881 -2, /* (9) cmd ::= ROLLBACK trans_opt */
170882 -2, /* (10) cmd ::= SAVEPOINT nm */
170883 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
170884 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
170885 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
170886 -1, /* (14) createkw ::= CREATE */
170888 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
170889 -1, /* (17) temp ::= TEMP */
170891 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
170892 -2, /* (20) create_table_args ::= AS select */
170894 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
170895 -2, /* (23) table_option ::= WITHOUT nm */
170896 -1, /* (24) table_option ::= nm */
170897 -2, /* (25) columnname ::= nm typetoken */
170899 -4, /* (27) typetoken ::= typename LP signed RP */
170900 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
170901 -2, /* (29) typename ::= typename ID|STRING */
170904 -2, /* (32) ccons ::= CONSTRAINT nm */
170905 -3, /* (33) ccons ::= DEFAULT scantok term */
170906 -4, /* (34) ccons ::= DEFAULT LP expr RP */
170907 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
170908 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
170909 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
170910 -3, /* (38) ccons ::= NOT NULL onconf */
170911 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
170912 -2, /* (40) ccons ::= UNIQUE onconf */
170913 -4, /* (41) ccons ::= CHECK LP expr RP */
170914 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
170915 -1, /* (43) ccons ::= defer_subclause */
170916 -2, /* (44) ccons ::= COLLATE ID|STRING */
170917 -3, /* (45) generated ::= LP expr RP */
170918 -4, /* (46) generated ::= LP expr RP ID */
170920 -1, /* (48) autoinc ::= AUTOINCR */
170922 -2, /* (50) refargs ::= refargs refarg */
170923 -2, /* (51) refarg ::= MATCH nm */
170924 -3, /* (52) refarg ::= ON INSERT refact */
170925 -3, /* (53) refarg ::= ON DELETE refact */
170926 -3, /* (54) refarg ::= ON UPDATE refact */
170927 -2, /* (55) refact ::= SET NULL */
170928 -2, /* (56) refact ::= SET DEFAULT */
170929 -1, /* (57) refact ::= CASCADE */
170930 -1, /* (58) refact ::= RESTRICT */
170931 -2, /* (59) refact ::= NO ACTION */
170932 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
170933 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
170935 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
170936 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
170938 -1, /* (66) tconscomma ::= COMMA */
170939 -2, /* (67) tcons ::= CONSTRAINT nm */
170940 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
170941 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
170942 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
170943-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
170946 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
170948 -2, /* (76) orconf ::= OR resolvetype */
170949 -1, /* (77) resolvetype ::= IGNORE */
170950 -1, /* (78) resolvetype ::= REPLACE */
170951 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
170952 -2, /* (80) ifexists ::= IF EXISTS */
170954 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
170955 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
170956 -1, /* (84) cmd ::= select */
170957 -3, /* (85) select ::= WITH wqlist selectnowith */
170958 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
170959 -1, /* (87) select ::= selectnowith */
170960 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
170961 -1, /* (89) multiselect_op ::= UNION */
170962 -2, /* (90) multiselect_op ::= UNION ALL */
170963 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
170964-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
170965-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
170966 -4, /* (94) values ::= VALUES LP nexprlist RP */
170967 -5, /* (95) values ::= values COMMA LP nexprlist RP */
170968 -1, /* (96) distinct ::= DISTINCT */
170969 -1, /* (97) distinct ::= ALL */
170972 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
170973 -3, /* (101) selcollist ::= sclp scanpt STAR */
170974 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
170975 -2, /* (103) as ::= AS nm */
170978 -2, /* (106) from ::= FROM seltablist */
170979 -2, /* (107) stl_prefix ::= seltablist joinop */
170981 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
170982 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
170983 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
170984 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
170985 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
170987 -2, /* (115) dbnm ::= DOT nm */
170988 -1, /* (116) fullname ::= nm */
170989 -3, /* (117) fullname ::= nm DOT nm */
170990 -1, /* (118) xfullname ::= nm */
170991 -3, /* (119) xfullname ::= nm DOT nm */
170992 -5, /* (120) xfullname ::= nm DOT nm AS nm */
170993 -3, /* (121) xfullname ::= nm AS nm */
170994 -1, /* (122) joinop ::= COMMA|JOIN */
170995 -2, /* (123) joinop ::= JOIN_KW JOIN */
170996 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
170997 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
170998 -2, /* (126) on_using ::= ON expr */
170999 -4, /* (127) on_using ::= USING LP idlist RP */
171002 -3, /* (130) indexed_by ::= INDEXED BY nm */
171003 -2, /* (131) indexed_by ::= NOT INDEXED */
171005 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
171006 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
171007 -3, /* (135) sortlist ::= expr sortorder nulls */
171008 -1, /* (136) sortorder ::= ASC */
171009 -1, /* (137) sortorder ::= DESC */
171011 -2, /* (139) nulls ::= NULLS FIRST */
171012 -2, /* (140) nulls ::= NULLS LAST */
171015 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
171017 -2, /* (145) having_opt ::= HAVING expr */
171019 -2, /* (147) limit_opt ::= LIMIT expr */
171020 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
171021 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
171022 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
171024 -2, /* (152) where_opt ::= WHERE expr */
171026 -2, /* (154) where_opt_ret ::= WHERE expr */
171027 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
171028 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
171029 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
171030 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
171031 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
171032 -3, /* (160) setlist ::= nm EQ expr */
171033 -5, /* (161) setlist ::= LP idlist RP EQ expr */
171034 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
171035 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
171037 -2, /* (165) upsert ::= RETURNING selcollist */
171038-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
171039 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
171040 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
171041 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
171042 -2, /* (170) returning ::= RETURNING selcollist */
171043 -2, /* (171) insert_cmd ::= INSERT orconf */
171044 -1, /* (172) insert_cmd ::= REPLACE */
171046 -3, /* (174) idlist_opt ::= LP idlist RP */
171047 -3, /* (175) idlist ::= idlist COMMA nm */
171048 -1, /* (176) idlist ::= nm */
171049 -3, /* (177) expr ::= LP expr RP */
171050 -1, /* (178) expr ::= ID|INDEXED|JOIN_KW */
171051 -3, /* (179) expr ::= nm DOT nm */
171052 -5, /* (180) expr ::= nm DOT nm DOT nm */
171053 -1, /* (181) term ::= NULL|FLOAT|BLOB */
171054 -1, /* (182) term ::= STRING */
171055 -1, /* (183) term ::= INTEGER */
171056 -1, /* (184) expr ::= VARIABLE */
171057 -3, /* (185) expr ::= expr COLLATE ID|STRING */
171058 -6, /* (186) expr ::= CAST LP expr AS typetoken RP */
171059 -5, /* (187) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP */
171060 -4, /* (188) expr ::= ID|INDEXED|JOIN_KW LP STAR RP */
171061 -6, /* (189) expr ::= ID|INDEXED|JOIN_KW LP distinct exprlist RP filter_over */
171062 -5, /* (190) expr ::= ID|INDEXED|JOIN_KW LP STAR RP filter_over */
171063 -1, /* (191) term ::= CTIME_KW */
171064 -5, /* (192) expr ::= LP nexprlist COMMA expr RP */
171065 -3, /* (193) expr ::= expr AND expr */
171066 -3, /* (194) expr ::= expr OR expr */
171067 -3, /* (195) expr ::= expr LT|GT|GE|LE expr */
171068 -3, /* (196) expr ::= expr EQ|NE expr */
171069 -3, /* (197) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
171070 -3, /* (198) expr ::= expr PLUS|MINUS expr */
171071 -3, /* (199) expr ::= expr STAR|SLASH|REM expr */
171072 -3, /* (200) expr ::= expr CONCAT expr */
171073 -2, /* (201) likeop ::= NOT LIKE_KW|MATCH */
171074 -3, /* (202) expr ::= expr likeop expr */
171075 -5, /* (203) expr ::= expr likeop expr ESCAPE expr */
171076 -2, /* (204) expr ::= expr ISNULL|NOTNULL */
171077 -3, /* (205) expr ::= expr NOT NULL */
171078 -3, /* (206) expr ::= expr IS expr */
171079 -4, /* (207) expr ::= expr IS NOT expr */
171080 -6, /* (208) expr ::= expr IS NOT DISTINCT FROM expr */
171081 -5, /* (209) expr ::= expr IS DISTINCT FROM expr */
171082 -2, /* (210) expr ::= NOT expr */
171083 -2, /* (211) expr ::= BITNOT expr */
171084 -2, /* (212) expr ::= PLUS|MINUS expr */
171085 -3, /* (213) expr ::= expr PTR expr */
171086 -1, /* (214) between_op ::= BETWEEN */
171087 -2, /* (215) between_op ::= NOT BETWEEN */
171088 -5, /* (216) expr ::= expr between_op expr AND expr */
171089 -1, /* (217) in_op ::= IN */
171090 -2, /* (218) in_op ::= NOT IN */
171091 -5, /* (219) expr ::= expr in_op LP exprlist RP */
171092 -3, /* (220) expr ::= LP select RP */
171093 -5, /* (221) expr ::= expr in_op LP select RP */
171094 -5, /* (222) expr ::= expr in_op nm dbnm paren_exprlist */
171095 -4, /* (223) expr ::= EXISTS LP select RP */
171096 -5, /* (224) expr ::= CASE case_operand case_exprlist case_else END */
171097 -5, /* (225) case_exprlist ::= case_exprlist WHEN expr THEN expr */
171098 -4, /* (226) case_exprlist ::= WHEN expr THEN expr */
171099 -2, /* (227) case_else ::= ELSE expr */
171103 -3, /* (231) nexprlist ::= nexprlist COMMA expr */
171104 -1, /* (232) nexprlist ::= expr */
171106 -3, /* (234) paren_exprlist ::= LP exprlist RP */
171107-12, /* (235) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
171108 -1, /* (236) uniqueflag ::= UNIQUE */
171111 -3, /* (239) eidlist_opt ::= LP eidlist RP */
171112 -5, /* (240) eidlist ::= eidlist COMMA nm collate sortorder */
171113 -3, /* (241) eidlist ::= nm collate sortorder */
171115 -2, /* (243) collate ::= COLLATE ID|STRING */
171116 -4, /* (244) cmd ::= DROP INDEX ifexists fullname */
171117 -2, /* (245) cmd ::= VACUUM vinto */
171118 -3, /* (246) cmd ::= VACUUM nm vinto */
171119 -2, /* (247) vinto ::= INTO expr */
171121 -3, /* (249) cmd ::= PRAGMA nm dbnm */
171122 -5, /* (250) cmd ::= PRAGMA nm dbnm EQ nmnum */
171123 -6, /* (251) cmd ::= PRAGMA nm dbnm LP nmnum RP */
171124 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ minus_num */
171125 -6, /* (253) cmd ::= PRAGMA nm dbnm LP minus_num RP */
171126 -2, /* (254) plus_num ::= PLUS INTEGER|FLOAT */
171127 -2, /* (255) minus_num ::= MINUS INTEGER|FLOAT */
171128 -5, /* (256) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
171129-11, /* (257) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
171130 -1, /* (258) trigger_time ::= BEFORE|AFTER */
171131 -2, /* (259) trigger_time ::= INSTEAD OF */
171133 -1, /* (261) trigger_event ::= DELETE|INSERT */
171134 -1, /* (262) trigger_event ::= UPDATE */
171135 -3, /* (263) trigger_event ::= UPDATE OF idlist */
171137 -2, /* (265) when_clause ::= WHEN expr */
171138 -3, /* (266) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
171139 -2, /* (267) trigger_cmd_list ::= trigger_cmd SEMI */
171140 -3, /* (268) trnm ::= nm DOT nm */
171141 -3, /* (269) tridxby ::= INDEXED BY nm */
171142 -2, /* (270) tridxby ::= NOT INDEXED */
171143 -9, /* (271) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
171144 -8, /* (272) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
171145 -6, /* (273) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
171146 -3, /* (274) trigger_cmd ::= scanpt select scanpt */
171147 -4, /* (275) expr ::= RAISE LP IGNORE RP */
171148 -6, /* (276) expr ::= RAISE LP raisetype COMMA nm RP */
171149 -1, /* (277) raisetype ::= ROLLBACK */
171150 -1, /* (278) raisetype ::= ABORT */
171151 -1, /* (279) raisetype ::= FAIL */
171152 -4, /* (280) cmd ::= DROP TRIGGER ifexists fullname */
171153 -6, /* (281) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
171154 -3, /* (282) cmd ::= DETACH database_kw_opt expr */
171156 -2, /* (284) key_opt ::= KEY expr */
171157 -1, /* (285) cmd ::= REINDEX */
171158 -3, /* (286) cmd ::= REINDEX nm dbnm */
171159 -1, /* (287) cmd ::= ANALYZE */
171160 -3, /* (288) cmd ::= ANALYZE nm dbnm */
171161 -6, /* (289) cmd ::= ALTER TABLE fullname RENAME TO nm */
171162 -7, /* (290) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
171163 -6, /* (291) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
171164 -1, /* (292) add_column_fullname ::= fullname */
171165 -8, /* (293) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
171166 -1, /* (294) cmd ::= create_vtab */
171167 -4, /* (295) cmd ::= create_vtab LP vtabarglist RP */
171168 -8, /* (296) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
171170 -1, /* (298) vtabargtoken ::= ANY */
171171 -3, /* (299) vtabargtoken ::= lp anylist RP */
171172 -1, /* (300) lp ::= LP */
171173 -2, /* (301) with ::= WITH wqlist */
171174 -3, /* (302) with ::= WITH RECURSIVE wqlist */
171175 -1, /* (303) wqas ::= AS */
171176 -2, /* (304) wqas ::= AS MATERIALIZED */
171177 -3, /* (305) wqas ::= AS NOT MATERIALIZED */
171178 -6, /* (306) wqitem ::= nm eidlist_opt wqas LP select RP */
171179 -1, /* (307) wqlist ::= wqitem */
171180 -3, /* (308) wqlist ::= wqlist COMMA wqitem */
171181 -1, /* (309) windowdefn_list ::= windowdefn */
171182 -3, /* (310) windowdefn_list ::= windowdefn_list COMMA windowdefn */
171183 -5, /* (311) windowdefn ::= nm AS LP window RP */
171184 -5, /* (312) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
171185 -6, /* (313) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
171186 -4, /* (314) window ::= ORDER BY sortlist frame_opt */
171187 -5, /* (315) window ::= nm ORDER BY sortlist frame_opt */
171188 -1, /* (316) window ::= frame_opt */
171189 -2, /* (317) window ::= nm frame_opt */
171191 -3, /* (319) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
171192-6, /* (320) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
171193 -1, /* (321) range_or_rows ::= RANGE|ROWS|GROUPS */
171194 -1, /* (322) frame_bound_s ::= frame_bound */
171195 -2, /* (323) frame_bound_s ::= UNBOUNDED PRECEDING */
171196 -1, /* (324) frame_bound_e ::= frame_bound */
171197 -2, /* (325) frame_bound_e ::= UNBOUNDED FOLLOWING */
171198 -2, /* (326) frame_bound ::= expr PRECEDING|FOLLOWING */
171199 -2, /* (327) frame_bound ::= CURRENT ROW */
171201 -2, /* (329) frame_exclude_opt ::= EXCLUDE frame_exclude */
171202 -2, /* (330) frame_exclude ::= NO OTHERS */
171203 -2, /* (331) frame_exclude ::= CURRENT ROW */
171204 -1, /* (332) frame_exclude ::= GROUP|TIES */
171205 -2, /* (333) window_clause ::= WINDOW windowdefn_list */
171206 -2, /* (334) filter_over ::= filter_clause over_clause */
171207 -1, /* (335) filter_over ::= over_clause */
171208 -1, /* (336) filter_over ::= filter_clause */
171209 -4, /* (337) over_clause ::= OVER LP window RP */
171210 -2, /* (338) over_clause ::= OVER nm */
171211 -5, /* (339) filter_clause ::= FILTER LP WHERE expr RP */
171212 -1, /* (340) input ::= cmdlist */
171213 -2, /* (341) cmdlist ::= cmdlist ecmd */
171214 -1, /* (342) cmdlist ::= ecmd */
171215 -1, /* (343) ecmd ::= SEMI */
171216 -2, /* (344) ecmd ::= cmdx SEMI */
171217 -3, /* (345) ecmd ::= explain cmdx SEMI */
171219 -1, /* (347) trans_opt ::= TRANSACTION */
171220 -2, /* (348) trans_opt ::= TRANSACTION nm */
171221 -1, /* (349) savepoint_opt ::= SAVEPOINT */
171223 -2, /* (351) cmd ::= create_table create_table_args */
171224 -1, /* (352) table_option_set ::= table_option */
171225 -4, /* (353) columnlist ::= columnlist COMMA columnname carglist */
171226 -2, /* (354) columnlist ::= columnname carglist */
171227 -1, /* (355) nm ::= ID|INDEXED|JOIN_KW */
171228 -1, /* (356) nm ::= STRING */
171229 -1, /* (357) typetoken ::= typename */
171230 -1, /* (358) typename ::= ID|STRING */
171231 -1, /* (359) signed ::= plus_num */
171232 -1, /* (360) signed ::= minus_num */
171233 -2, /* (361) carglist ::= carglist ccons */
171235 -2, /* (363) ccons ::= NULL onconf */
171236 -4, /* (364) ccons ::= GENERATED ALWAYS AS generated */
171237 -2, /* (365) ccons ::= AS generated */
171238 -2, /* (366) conslist_opt ::= COMMA conslist */
171239 -3, /* (367) conslist ::= conslist tconscomma tcons */
171240 -1, /* (368) conslist ::= tcons */
171242 -1, /* (370) defer_subclause_opt ::= defer_subclause */
171243 -1, /* (371) resolvetype ::= raisetype */
171244 -1, /* (372) selectnowith ::= oneselect */
171245 -1, /* (373) oneselect ::= values */
171246 -2, /* (374) sclp ::= selcollist COMMA */
171247 -1, /* (375) as ::= ID|STRING */
171248 -1, /* (376) indexed_opt ::= indexed_by */
171250 -1, /* (378) expr ::= term */
171251 -1, /* (379) likeop ::= LIKE_KW|MATCH */
171252 -1, /* (380) case_operand ::= expr */
171253 -1, /* (381) exprlist ::= nexprlist */
171254 -1, /* (382) nmnum ::= plus_num */
171255 -1, /* (383) nmnum ::= nm */
171256 -1, /* (384) nmnum ::= ON */
171257 -1, /* (385) nmnum ::= DELETE */
171258 -1, /* (386) nmnum ::= DEFAULT */
171259 -1, /* (387) plus_num ::= INTEGER|FLOAT */
171261 -3, /* (389) foreach_clause ::= FOR EACH ROW */
171262 -1, /* (390) trnm ::= nm */
171264 -1, /* (392) database_kw_opt ::= DATABASE */
171267 -1, /* (395) kwcolumn_opt ::= COLUMNKW */
171268 -1, /* (396) vtabarglist ::= vtabarg */
171269 -3, /* (397) vtabarglist ::= vtabarglist COMMA vtabarg */
171270 -2, /* (398) vtabarg ::= vtabarg vtabargtoken */
171272 -4, /* (400) anylist ::= anylist LP anylist RP */
171273 -2, /* (401) anylist ::= anylist ANY */
171286 ** only called from one place, optimizing compilers will in-line it, which
171303 yymsp = yypParser->yytos;
171317 { pParse->explain = 1; }
171320 { pParse->explain = 2; }
171326 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
171335 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
171339 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
171358 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[
171375 {yymsp[-2].minor.yy394 = 1;}
171378 {yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
171382 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
171388 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
171395 {yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
171396 yymsp[-2].minor.yy285 = yylhsminor.yy285;
171401 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
171403 yymsp[-1].minor.yy285 = 0;
171420 {sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
171429 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
171434 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
171438 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
171454 {pParse->constraintName = yymsp[0].minor.yy0;}
171457 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
171460 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
171463 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
171468 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
171476 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
171485 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
171492 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
171495 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
171504 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
171507 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
171513 { yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
171516 { yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy2…
171519 { yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
171522 { yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
171525 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
171528 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
171531 { yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
171534 { yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
171537 { yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
171540 { yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
171543 { yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
171546 {yymsp[-2].minor.yy394 = 0;}
171551 {yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
171558 {yymsp[-1].minor.yy394 = 1;}
171561 {yymsp[-1].minor.yy394 = 0;}
171564 {pParse->constraintName.n = 0;}
171567 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
171570 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
171574 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
171578 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
171587 {yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
171598 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
171603 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
171608 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
171615 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
171619 {yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
171622 {yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
171630 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
171636 Select *pLhs = yymsp[-2].minor.yy47;
171637 if( pRhs && pRhs->pPrior ){
171646 pRhs->op = (u8)yymsp[-1].minor.yy394;
171647 pRhs->pPrior = pLhs;
171648 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
171649 pRhs->selFlags &= ~SF_MultiValue;
171650 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
171652 sqlite3SelectDelete(pParse->db, pLhs);
171654 yymsp[-2].minor.yy47 = pRhs;
171659 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
171662 {yymsp[-1].minor.yy394 = TK_ALL;}
171666-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].min…
171671-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].min…
171672 if( yymsp[-9].minor.yy47 ){
171673 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
171675 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
171681 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
171686 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
171687 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
171688 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
171690 pRight->op = TK_ALL;
171691 pRight->pPrior = pLeft;
171692 yymsp[-4].minor.yy47 = pRight;
171694 yymsp[-4].minor.yy47 = pLeft;
171714 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528…
171715 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
171716 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
171721 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
171722 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
171728 Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0);
171730 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
171737 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
171745 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
171746 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
171751 …if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0) ) yymsp[-1].minor.yy131->a[yyms…
171756 …yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor…
171761 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor…
171762 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
171767 …yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor…
171768 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
171773 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
171778 …if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0…
171779 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
171780 }else if( ALWAYS(yymsp[-3].minor.yy131!=0) && yymsp[-3].minor.yy131->nSrc==1 ){
171781 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
171782 if( yymsp[-5].minor.yy131 ){
171783 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
171784 SrcItem *pOld = yymsp[-3].minor.yy131->a;
171785 pNew->zName = pOld->zName;
171786 pNew->zDatabase = pOld->zDatabase;
171787 pNew->pSelect = pOld->pSelect;
171788 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
171789 pNew->fg.isNestedFrom = 1;
171791 if( pOld->fg.isTabFunc ){
171792 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
171793 pOld->u1.pFuncArg = 0;
171794 pOld->fg.isTabFunc = 0;
171795 pNew->fg.isTabFunc = 1;
171797 pOld->zName = pOld->zDatabase = 0;
171798 pOld->pSelect = 0;
171800 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
171803 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
171804 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom,0);
171805 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
171816 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
171822 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
171823 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
171825 yymsp[-2].minor.yy131 = yylhsminor.yy131;
171828 {yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
171831 {yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
171835 …yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
171836 …if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
171841 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
171842 …if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
171849 {yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
171852 {yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
171855 {yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
171858 {yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
171861 {yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
171867 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
171870 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
171874 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
171878 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
171879 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
171884 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
171885 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
171899 {yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
171902 {yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
171918 {yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
171921 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,0);}
171924 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
171927 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
171931 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
171932 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
171936 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
171939 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
171943 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
171944 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
171945 if( yymsp[-1].minor.yy131 ){
171946 SrcList *pFromClause = yymsp[-1].minor.yy131;
171947 if( pFromClause->nSrc>1 ){
171955 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
171957 …sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].mi…
171962 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
171963 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
171968 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
171974 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
171976 yymsp[-2].minor.yy322 = yylhsminor.yy322;
171980 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
171985 …sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5…
171990 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
171997 { yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
172000 { yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,…
172003 { yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0…
172006 { yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
172009 { yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy5…
172018 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
172021 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
172024 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
172027 {yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
172030 {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
172034 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
172038 yymsp[-2].minor.yy528 = yylhsminor.yy528;
172042 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0);
172043 Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
172051 yymsp[-4].minor.yy528 = yylhsminor.yy528;
172055 {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
172059 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
172060 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
172073 ** in the virtual machine. #N is the N-th register. */
172074 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
172076 if( pParse->nested==0 ){
172081 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
172088 …yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.…
172093 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
172094 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
172099 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[
172101 yymsp[-4].minor.yy528 = yylhsminor.yy528;
172105 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
172107 yymsp[-3].minor.yy528 = yylhsminor.yy528;
172111 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[
172114 yymsp[-5].minor.yy528 = yylhsminor.yy528;
172118 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
172121 yymsp[-4].minor.yy528 = yylhsminor.yy528;
172131 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
172132 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
172133 if( yymsp[-4].minor.yy528 ){
172134 yymsp[-4].minor.yy528->x.pList = pList;
172135 if( ALWAYS(pList->nExpr) ){
172136 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
172139 sqlite3ExprListDelete(pParse->db, pList);
172144 {yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
172153 {yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy5…
172156 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
172161 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
172162 yymsp[-1].minor.yy0.n &= 0x7fffffff;
172164 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
172165 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
172166 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy528, 0);
172167 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
172173 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
172174 yymsp[-3].minor.yy0.n &= 0x7fffffff;
172175 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
172176 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
172178 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
172179 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy528, 0);
172180 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
172184 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
172187 {yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy528,0);}
172191 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
172192 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL);
172197 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
172198 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL);
172203 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
172204 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL);
172209 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
172210 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL);
172215 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwri…
172219 …yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
172220 /*A-overwrites-B*/
172225 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
172227 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
172229 yymsp[-2].minor.yy528 = yylhsminor.yy528;
172237 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
172239 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy528, 0);
172240 if( yymsp[-4].minor.yy528 ){
172241 yymsp[-4].minor.yy528->x.pList = pList;
172243 sqlite3ExprListDelete(pParse->db, pList);
172245 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
172250 if( yymsp[-1].minor.yy322==0 ){
172259 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
172260 …yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false…
172261 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
172263 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
172264 …if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=T…
172265 yymsp[-1].minor.yy322->a[0].pExpr = 0;
172266 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
172268 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy528, pRHS);
172269 }else if( yymsp[-1].minor.yy322->nExpr==1 && pRHS->op==TK_SELECT ){
172270 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
172271 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pRHS->x.pSelect);
172272 pRHS->x.pSelect = 0;
172273 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
172275 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
172276 if( yymsp[-4].minor.yy528==0 ){
172277 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
172278 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
172279 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
172280 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
172283 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
172286 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
172287 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
172290 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
172296 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
172297 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
172302 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
172303 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
172304 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
172309 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
172312 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
172313 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
172314 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
172320 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
172321 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
172326 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy528, 0);
172327 if( yymsp[-4].minor.yy528 ){
172328 …yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
172329 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
172331 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
172332 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
172338 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
172339 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
172344 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
172345 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
172349 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
172352 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
172356 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
172360 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
172361 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy3…
172362 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_…
172363 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
172364 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
172377 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
172382 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
172386 {sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
172392 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
172395 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
172398 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
172401 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
172404 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
172407 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
172412 all.z = yymsp[-3].minor.yy0.z;
172413 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
172414 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
172419-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].min…
172420 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
172424 { yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
172427 { yymsp[-1].minor.yy394 = TK_INSTEAD;}
172434 {yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
172437 {yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
172445 { yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
172449 assert( yymsp[-2].minor.yy33!=0 );
172450 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
172451 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
172456 assert( yymsp[-1].minor.yy33!=0 );
172457 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
172462 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
172483 …tep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy…
172484 yymsp[-8].minor.yy33 = yylhsminor.yy33;
172488-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy4…
172490 yymsp[-7].minor.yy33 = yylhsminor.yy33;
172493 …sminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[
172494 yymsp[-5].minor.yy33 = yylhsminor.yy33;
172497 …ggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*y…
172498 yymsp[-2].minor.yy33 = yylhsminor.yy33;
172502 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
172503 if( yymsp[-3].minor.yy528 ){
172504 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
172510 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
172511 if( yymsp[-5].minor.yy528 ) {
172512 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
172524 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
172529 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
172541 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
172547 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
172551 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
172556 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
172557 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
172562 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
172573 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
172584 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
172603 {yymsp[-1].minor.yy516 = M10d_Yes;}
172606 {yymsp[-2].minor.yy516 = M10d_No;}
172610 …sp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].…
172615 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
172620 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
172630 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
172631 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
172634 yymsp[-2].minor.yy41 = yylhsminor.yy41;
172638 if( ALWAYS(yymsp[-1].minor.yy41) ){
172639 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
172641 yylhsminor.yy41 = yymsp[-1].minor.yy41;
172643 yymsp[-4].minor.yy41 = yylhsminor.yy41;
172647 …yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, y…
172652 …e3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp…
172654 yymsp[-5].minor.yy41 = yylhsminor.yy41;
172658 …yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322…
172663 …1 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.…
172665 yymsp[-4].minor.yy41 = yylhsminor.yy41;
172676 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
172678 yymsp[-1].minor.yy41 = yylhsminor.yy41;
172687 …ylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yy…
172689 yymsp[-2].minor.yy41 = yylhsminor.yy41;
172693 …wAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yy…
172695 yymsp[-5].minor.yy41 = yylhsminor.yy41;
172705 {yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
172706 yymsp[-1].minor.yy595 = yylhsminor.yy595;
172709 {yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
172710 yymsp[-1].minor.yy595 = yylhsminor.yy595;
172716 {yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
172720 {yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
172723 {yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
172726 { yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
172731 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
172733 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
172737 yymsp[-1].minor.yy41 = yylhsminor.yy41;
172741 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
172743 yylhsminor.yy41->eFrmType = TK_FILTER;
172744 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
172746 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
172753 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
172754 assert( yymsp[-3].minor.yy41!=0 );
172759 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
172760 if( yymsp[-1].minor.yy41 ){
172761 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
172766 { yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
172848 yypParser->yytos = yymsp;
172849 yymsp->stateno = (YYACTIONTYPE)yyact;
172850 yymsp->major = (YYCODETYPE)yygoto;
172869 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
172917 yypParser->yyerrcnt = -1;
172919 assert( yypParser->yytos==yypParser->yystack );
172941 ** <li> An option argument of a grammar-specified type.
172965 assert( yypParser->yytos!=0 );
172970 yyact = yypParser->yytos->stateno;
172978 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
172984 assert( yypParser->yytos>=yypParser->yystack );
172985 assert( yyact==yypParser->yytos->stateno );
172988 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
172998 yypParser->yytos[yysize].stateno);
173012 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
173013 yypParser->yyhwm++;
173014 assert( yypParser->yyhwm ==
173015 (int)(yypParser->yytos - yypParser->yystack));
173019 if( yypParser->yytos>=yypParser->yystackEnd ){
173024 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
173036 yypParser->yyerrcnt--;
173040 yypParser->yytos--;
173074 if( yypParser->yyerrcnt<0 ){
173077 yymx = yypParser->yytos->major;
173088 while( yypParser->yytos > yypParser->yystack ){
173089 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
173094 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
173098 yypParser->yyerrcnt = -1;
173105 yypParser->yyerrcnt = 3;
173108 yyact = yypParser->yytos->stateno;
173130 if( yypParser->yyerrcnt<=0 ){
173133 yypParser->yyerrcnt = 3;
173138 yypParser->yyerrcnt = -1;
173150 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
173151 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
173190 ** individual tokens and sends those tokens one-by-one over to the
173215 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
173220 #define CC_SLASH 16 /* '/'. / or c-style comment */
173279 ** lower-case ASCII equivalent. On ASCII machines, this is just
173280 ** an upper-to-lower case map. On EBCDIC machines we also need
173333 ** might be implemented more directly using a hand-written hash table.
173391 /* aKWHash[i] is the hash value for the i-th keyword */
173405 ** then the i-th keyword has no more hash collisions. Otherwise,
173406 ** the next keyword with the same hash is aKWHash[i]-1. */
173421 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
173437 ** the text for the i-th keyword. */
173452 /* aKWCode[i] is the parser symbol code for the i-th keyword */
173614 /* Check to see if z[0..n-1] is a keyword. If it is, write the
173621 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
173817 ** For ASCII, any character with the high-order bit set is
173818 ** allowed in an identifier. For 7-bit characters,
173848 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
173890 ** impossible to call a window-function without a FILTER clause.
173937 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
173951 if( z[1]=='-' ){
173953 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
173989 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
174115 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
174231 void *pEngine; /* The LEMON-generated LALR(1) parser */
174234 int lastTokenParsed = -1; /* type of the previous token */
174235 sqlite3 *db = pParse->db; /* The database connection */
174239 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
174241 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
174244 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
174245 if( db->nVdbeActive==0 ){
174246 AtomicStore(&db->u1.isInterrupted, 0);
174248 pParse->rc = SQLITE_OK;
174249 pParse->zTail = zSql;
174251 if( db->flags & SQLITE_ParserTrace ){
174268 assert( pParse->pNewTable==0 );
174269 assert( pParse->pNewTrigger==0 );
174270 assert( pParse->nVar==0 );
174271 assert( pParse->pVList==0 );
174272 pParentParse = db->pParse;
174273 db->pParse = pParse;
174276 mxSqlLen -= n;
174278 pParse->rc = SQLITE_TOOBIG;
174279 pParse->nErr++;
174291 if( AtomicLoad(&db->u1.isInterrupted) ){
174292 pParse->rc = SQLITE_INTERRUPT;
174293 pParse->nErr++;
174330 pParse->sLastToken.z = zSql;
174331 pParse->sLastToken.n = n;
174332 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
174335 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
174336 if( pParse->rc!=SQLITE_OK ) break;
174351 if( db->mallocFailed ){
174352 pParse->rc = SQLITE_NOMEM_BKPT;
174354 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
174355 if( pParse->zErrMsg==0 ){
174356 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
174358 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
174361 pParse->zTail = zSql;
174363 sqlite3_free(pParse->apVtabLock);
174366 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
174367 /* If the pParse->declareVtab flag is set, do not delete any table
174368 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
174371 sqlite3DeleteTable(db, pParse->pNewTable);
174373 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
174374 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
174376 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
174377 db->pParse = pParentParse;
174378 assert( nErr==0 || pParse->rc!=SQLITE_OK );
174389 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
174395 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
174407 int prevType = 0; /* Previous non-whitespace token */
174415 tokenType = -1;
174419 for(i=0; zSql[i] && pStr->accError==0; i+=n){
174447 iStartIN = pStr->nChar;
174455 assert( pStr->nChar>=(u32)iStartIN );
174456 pStr->nChar = iStartIN+1;
174460 nParen--;
174466 j = pStr->nChar;
174490 while( j<pStr->nChar ){
174491 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
174502 j = pStr->nChar;
174504 while( j<pStr->nChar ){
174505 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
174549 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
174578 ** (0) INVALID We have not yet seen a non-whitespace character.
174676 case '/': { /* C-style comments */
174688 case '-': { /* SQL-style comments from "--" to end of line */
174689 if( zSql[1]!='-' ){
174698 case '[': { /* Microsoft-style identifiers in [...] */
174705 case '`': /* Grave-accent quoted symbols used by MySQL */
174706 case '"': /* single- and double-quoted strings */
174767 zSql += nId-1;
174784 ** above, except that the parameter is required to be UTF-16 encoded, not
174785 ** UTF-8.
174797 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
174935 ** This is an extension initializer that is a no-op and always
174936 ** succeeds, except that it fails if the fault-simulation is set
174964 ** built-in extensions.
175004 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
175010 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
175015 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
175023 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
175028 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
175030 ** the SQLITE_THREADSAFE compile-time option being set to 0.
175036 ** this variable being set to non-zero will cause OSTRACE macros to emit
175083 ** This routine is a no-op except on its very first call for the process,
175099 ** * Calls to this routine from Y must block until the outer-most
175120 ** combination, the work-around is to set the correct pointer
175121 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
175125 ** to sqlite3_initialize() should be a no-op. But the initialization
175148 ** malloc subsystem - this implies that the allocation of a static
175185 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
175232 sqlite3GlobalConfig.nRefInitMutex--;
175249 u64 x = (((u64)1)<<63)-1;
175260 ** compile-time option.
175278 ** when this routine is invoked, then this routine is a harmless no-op.
175327 ** the SQLite library at run-time.
175360 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
175362 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
175363 ** Single-thread. */
175369 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
175371 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
175372 ** Multi-thread. */
175378 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
175380 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
175387 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
175394 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
175403 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
175406 ** low-level memory allocation routines to be used in place of the memory
175412 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
175422 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
175433 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
175434 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
175443 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
175455 /* no-op */
175465 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
175473 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
175484 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
175489 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
175490 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
175505 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
175515 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
175552 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
175553 ** can be changed at start-time using the
175558 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
175559 ** argument of type int. If non-zero, then URI handling is globally
175568 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
175586 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
175592 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
175593 ** negative, then that argument is changed to its compile-time default.
175595 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
175597 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
175598 ** compile-time option.
175610 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
175612 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
175672 int nBig; /* Number of full-size slots */
175673 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
175682 if( db->lookaside.bMalloced ){
175683 sqlite3_free(db->lookaside.pStart);
175688 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
175696 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
175705 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
175708 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
175717 db->lookaside.pStart = pStart;
175718 db->lookaside.pInit = 0;
175719 db->lookaside.pFree = 0;
175720 db->lookaside.sz = (u16)sz;
175721 db->lookaside.szTrue = (u16)sz;
175728 p->pNext = db->lookaside.pInit;
175729 db->lookaside.pInit = p;
175733 db->lookaside.pSmallInit = 0;
175734 db->lookaside.pSmallFree = 0;
175735 db->lookaside.pMiddle = p;
175737 p->pNext = db->lookaside.pSmallInit;
175738 db->lookaside.pSmallInit = p;
175743 db->lookaside.pEnd = p;