• Home
  • Raw
  • Download

Lines Matching +full:vm +full:- +full:backends

17 ** language. The code for the "sqlite3" command-line shell is also in a
21 ** 2019.09.02-Complete codec logic for encryption and decryption.
52 ** NO_TEST - The branches on this line are not
57 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
61 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
65 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
70 ** 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
218 ** so the GCC_VERSION macro will be set to a correct non-zero value even
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 "2022-12-28 14:03:47 df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c…
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
1493 ** whether or not there is a database client in another process with a wal-mode
1495 ** (void*) argument passed with this file-control should be a pointer to a
1499 ** the database is not a wal-mode db, or if there is no such connection in any
1509 ** database is not a temp db, then this file-control purges the contents
1510 ** of the in-memory page cache. If there is an open transaction, or if
1511 ** the db is a temp-db, it is a no-op, not an error.
1589 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1632 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1650 ** consist of a single "-" character followed by no more than
1651 ** 11 alphanumeric and/or "-" characters.
1666 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1686 ** the open of a journal file a no-op. Writes to this journal would
1687 ** also be no-ops, and any attempt to read the journal would return
1689 ** file will be doing page-aligned sector reads and writes in a random
1728 ** flag is never actually used and is not implemented in the built-in
1731 ** non-zero error code if there is an I/O error or if the name of
1733 ** is returned, then non-zero or zero is written into *pResOut to indicate
1747 ** of good-quality randomness into zOut. The return value is
1755 ** a 24-hour day).
1781 void *pAppData; /* Pointer to application-specific data */
1891 ** are harmless no-ops.)^
1896 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1919 ** compile-time option, then the automatic calls to sqlite3_initialize()
1928 ** The sqlite3_os_init() routine does operating-system specific
1944 ** (using the [SQLITE_OS_OTHER=1] compile-time
1946 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1975 ** implementation of an application-defined [sqlite3_os_init()].
1985 ** then this routine returns a non-zero [error code].
1999 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2012 ** and low-level memory allocation routines.
2024 ** Note that SQLite comes with several [built-in memory allocators]
2029 ** memory allocator that simulates memory out-of-memory conditions in
2094 ** non-zero [error code] if a discontinued or unsupported configuration option
2100 ** [threading mode] to Single-thread. In other words, it disables
2103 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2105 ** value of Single-thread and so [sqlite3_config()] will return
2111 ** [threading mode] to Multi-thread. In other words, it disables
2115 ** are enabled so that SQLite will be safe to use in a multi-threaded
2118 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2119 ** it is not possible to set the Multi-thread [threading mode] and
2134 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2143 ** alternative low-level memory allocation routines to be used in place of
2171 ** disabled, the following SQLite interfaces become non-operational:
2192 ** This configuration option is a no-op if an application-defined page
2195 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2203 ** argument must be either a NULL pointer or a pointer to an 8-byte
2210 ** ^If pMem is NULL and N is non-zero, then each database connection
2213 ** of -1024*N bytes if N is negative, . ^If additional
2226 ** An 8-byte aligned pointer to the memory,
2233 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2241 ** The argument specifies alternative low-level mutex routines to be used
2245 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2258 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2291 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2293 ** passed through as the first parameter to the application-defined logger
2301 ** In a multi-threaded application, the application-defined logger
2306 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2323 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2324 ** if that compile-time option is omitted.
2334 ** They are retained for backwards compatibility but are now no-ops.
2340 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2356 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2363 ** compile-time maximum mmap size set by the
2364 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2366 ** changed to its compile-time default.
2371 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2372 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2388 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2397 ** becomes the [statement journal] spill-to-disk threshold.
2400 ** Or if the threshold is -1, statement journals are always held
2406 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2411 ** of type (int) - the new value of the sorter-reference size threshold.
2415 ** of a table column that its values are likely to be very large - larger
2416 ** than the configured sorter-reference size threshold - then a reference
2422 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2427 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2430 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2432 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2433 ** compile-time option is not set, then the default maximum is 1073741824.
2444 ** passed through as the first parameter to the application-defined corruption
2447 ** In a multi-threaded application, the application-defined corruption
2464 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2465 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2493 ** non-zero [error code] if a discontinued or unsupported configuration option
2509 ** must be aligned to an 8-byte boundary. ^If the second argument to
2545 ** triggers in the main database schema or in the schemas of ATTACH-ed
2562 ** views in the main database schema or in the schemas of ATTACH-ed
2569 ** [FTS3] full-text search engine extension.
2584 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2586 ** When the first argument to this interface is 1, then only the C-API is
2588 ** this interface is 0, then both the C-API and the SQL function are disabled.
2589 ** If the first argument is -1, then no changes are made to state of either the
2590 ** C-API or the SQL function.
2613 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2616 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2617 ** have been disabled - 0 if they are not disabled, 1 if they are.
2640 ** behavior. The first parameter passed to this operation is an integer -
2644 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2698 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2707 ** the legacy [double-quoted string literal] misfeature for DML statements
2709 ** default value of this setting is determined by the [-DSQLITE_DQS]
2710 ** compile-time option.
2716 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2718 ** default value of this setting is determined by the [-DSQLITE_DQS]
2719 ** compile-time option.
2746 ** created database file to have a schema format version number (the 4-byte
2803 ** has a unique 64-bit signed
2884 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2906 ** any changes performed by sub-triggers, as the sqlite3_changes()
2907 ** value will be saved and restored after each sub-trigger has run.)^
2975 ** CAPI3REF: Interrupt A Long-Running Query
2981 ** or Ctrl-C where the user wants a long query operation to halt
3006 ** SQL statements is a no-op and has no effect on SQL statements
3014 ** These routines are useful during command-line input to determine if the
3020 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3035 ** then the return value from sqlite3_complete16() will be non-zero
3038 ** The input to [sqlite3_complete()] must be a zero-terminated
3039 ** UTF-8 string.
3041 ** The input to [sqlite3_complete16()] must be a zero-terminated
3042 ** UTF-16 string in native byte order.
3049 ** KEYWORDS: {busy-handler callback} {busy handler}
3071 ** ^If the callback returns non-zero, then another attempt
3147 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3149 ** to zero-terminated strings that contain the names of the columns.
3151 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3163 ** -----------------------
3185 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3227 ** These routines are work-alikes of the "printf()" family of functions
3231 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3232 ** See the [built-in printf()] documentation for details.
3255 ** guarantees that the buffer is always zero-terminated. ^The first
3258 ** written will be n-1 characters.
3262 ** See also: [built-in printf()], [printf() SQL function]
3274 ** does not include operating-system specific [VFS] implementation. The
3285 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3286 ** of a signed 32-bit integer.
3291 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3296 ** might result if sqlite3_free() is called with a non-NULL pointer that
3316 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3317 ** of a 32-bit signed integer.
3332 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3356 ** routines, which form the built-in memory allocation subsystem.
3361 ** value of [sqlite3_memory_used()] since the high-water mark
3368 ** ^The memory high-water mark is reset to the current value of
3371 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3378 ** CAPI3REF: Pseudo-Random Number Generator
3380 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3383 ** the built-in random() and randomblob() SQL functions. This interface allows
3394 ** non-NULL P then the pseudo-randomness is generated
3401 ** CAPI3REF: Compile-Time Authorization Callbacks
3432 ** to the callback are either NULL pointers or zero-terminated strings
3460 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3479 ** statement might be re-prepared during [sqlite3_step()] due to a
3525 ** is the name of the inner-most trigger or view that is responsible for
3527 ** top-level SQL code.
3577 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3581 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3583 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3588 ** the original statement text and an estimate of wall-clock time
3608 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3630 ** interface by using the X argument when X begins with "--" and invoking
3637 ** X argument points to a 64-bit integer which is the estimated of
3667 ** M argument should be the bitwise OR-ed combination of
3717 ** ^If the progress callback returns non-zero, the operation is
3734 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3735 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3746 ** ^The default encoding will be UTF-8 for databases created using
3748 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3762 ** <dd>The database is opened in read-only mode. If the database does not
3784 ** <dd>The database will be opened as an in-memory database. The database
3785 ** is named by the "filename" argument for the purposes of cache-sharing,
3790 ** <dd>The new database connection will use the "multi-thread"
3808 ** this option is a no-op.
3834 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3845 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3846 ** is created for the connection. ^This in-memory database will vanish when
3854 ** on-disk database will be created. ^This private database will be
3864 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3887 ** SQLite and its built-in [VFSes] interpret the
3902 ** ^If "ro" is specified, then the database is opened for read-only
3905 ** "rw", then the database is opened for read-write (but not create)
3909 ** set to "memory" then a pure [in-memory database] that never reads
3936 ** read-only media. ^When immutable is set, SQLite assumes that the
3938 ** privilege, and so the database is opened read-only and all locking
3963 ** <tr><td style="white-space:nowrap">
3967 ** necessary - space characters can be used literally
3970 ** Open file "data.db" in the current directory for read-only access.
3971 ** Regardless of whether or not shared-cache mode is enabled by
3973 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3974 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3975 ** that uses dot-files in place of posix advisory locking.
3983 ** percent sign - "%" - followed by exactly two hexadecimal digits
3985 ** URI filename are interpreted, they are encoded using UTF-8 and all
3987 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
3991 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3993 ** characters must be converted to UTF-8 prior to passing them into
4003 const char *filename, /* Database filename (UTF-8) */
4007 const void *filename, /* Database filename (UTF-16) */
4011 const char *filename, /* Database filename (UTF-8) */
4048 ** case or if the value begins with a non-zero number. The
4056 ** 64-bit signed integer and returns that integer, or D if P does not
4061 ** the value) of the N-th query parameter for filename F, or a NULL
4063 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4122 ** ^If X is the name of a rollback or WAL-mode journal file that is
4128 ** only. It is not a general-purpose interface.
4172 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4208 ** change the value of the error code. The error-code preserving
4219 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4220 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4226 ** ^The sqlite3_errstr() interface returns the English-language text
4227 ** that describes the [result code], as UTF-8.
4236 ** SQL, then the sqlite3_error_offset() function returns -1.
4271 ** The life-cycle of a prepared statement object usually goes like this:
4286 ** CAPI3REF: Run-time Limits
4299 ** set at compile-time by a C preprocessor macro called
4308 ** simply invoke this interface with the third parameter set to -1.
4310 ** Run-time limits are intended for use in applications that manage
4323 ** New run-time limit categories may be added in future releases.
4328 ** CAPI3REF: Run-Time Limit Categories
4332 ** that can be lowered at run-time using [sqlite3_limit()].
4418 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4441 ** To execute an SQL statement, it must first be compiled into a byte-code
4450 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4451 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4452 ** as a convenience. The UTF-16 interfaces work by converting the
4453 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4460 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4462 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4463 ** and sqlite3_prepare16_v3() use UTF-16.
4469 ** If the caller knows that the supplied string is nul-terminated, then
4472 ** the nul-terminator.
4522 ** ^The specific value of a WHERE-clause [parameter] might influence the
4523 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4525 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4537 const char *zSql, /* SQL statement, UTF-8 encoded */
4544 const char *zSql, /* SQL statement, UTF-8 encoded */
4551 const char *zSql, /* SQL statement, UTF-8 encoded */
4559 const void *zSql, /* SQL statement, UTF-16 encoded */
4566 const void *zSql, /* SQL statement, UTF-16 encoded */
4573 const void *zSql, /* SQL statement, UTF-16 encoded */
4584 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4588 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4591 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4607 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4608 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4619 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4631 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4635 ** Note that [application-defined SQL functions] or
4639 ** change the database file through side-effects:
4665 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4667 ** read-only no-op if the table already exists, but
4692 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4729 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4740 ** implementation of [application-defined SQL functions] are protected.
4758 ** is always first parameter to [application-defined SQL functions].
4759 ** The application-defined SQL function implementation will pass this
4809 ** it should be a pointer to well-formed UTF8 text.
4811 ** it should be a pointer to well-formed UTF16 text.
4813 ** it should be a pointer to a well-formed unicode string that is
4817 ** [[byte-order determination rules]] ^The byte-order of
4818 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4835 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4875 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4877 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4949 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
4960 ** ^If the value N is out of range or if the N-th parameter is
4962 ** always in UTF-8 encoding even if the named parameter was
4963 ** originally specified as UTF-16 in [sqlite3_prepare16()],
4980 ** name must be given in UTF-8 even if the original statement
4981 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5022 ** interface returns a pointer to a zero-terminated UTF-8 string
5023 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5024 ** UTF-16 string. ^The first parameter is the [prepared statement]
5035 ** (for example during a conversion from UTF-8 to UTF-16) then a
5054 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5063 ** ^The names returned are the original un-aliased names of the
5069 ** ^The left-most column is column 0 for these routines.
5078 ** UTF-16 encoded strings and the other functions return UTF-8.
5081 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5105 ** ^The returned string is always UTF-8 encoded.
5118 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5168 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5170 ** the VM. More information may be found by calling [sqlite3_errmsg()].
5194 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5227 ** will return non-zero if previous call to [sqlite3_step](P) returned
5229 ** where it always returns zero since each step of that multi-step
5243 ** <li> 64-bit signed integer
5244 ** <li> 64-bit IEEE floating point number
5277 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5278 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5279 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5280 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5285 ** or a UTF-8 TEXT result in bytes
5287 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5339 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5341 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5342 ** the string to UTF-8 and then returns the number of bytes.
5344 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5348 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5350 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5351 ** the string to UTF-16 and then returns the number of bytes.
5353 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5364 ** even empty strings, are always zero-terminated. ^The return
5365 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5381 ** [application-defined SQL functions] or [virtual tables], not within
5382 ** top-level application code.
5400 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5421 ** sqlite3_column_text16() is called. A zero-terminator might
5423 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5425 ** to UTF-16.</li>
5426 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5428 ** to UTF-8.</li>
5431 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5462 ** fail if an out-of-memory error occurs during a format conversion.
5463 ** Only the following subset of interfaces are subject to out-of-memory
5474 ** If an out-of-memory error occurs, then the return value from these
5476 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5510 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5525 ** object back to its initial state, ready to be re-executed.
5564 ** connection then application-defined SQL functions must be added
5568 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5569 ** representation, exclusive of the zero-terminator. ^Note that the name
5570 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5576 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5579 ** parameter is less than -1 or greater than 127 then the behavior is
5598 ** deterministic. The built-in [random()] SQL function is an example of a
5609 ** all application-defined SQL functions that do not need to be
5623 ** pointers to C-language functions that implement the SQL function or
5633 ** C-language callbacks that implement the new function. xStep and xFinal
5634 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5636 ** non-NULL, in which case the new function may be used as either an aggregate
5639 ** [user-defined window functions|available here].
5654 ** SQL function is used. ^A function implementation with a non-negative
5663 ** ^Built-in functions may be overloaded by new application-defined functions.
5665 ** ^An application-defined function is permitted to call other
5720 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5721 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5722 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5749 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5753 ** for all [application-defined SQL functions], and especially for functions
5754 ** that have side-effects or that could potentially leak sensitive
5774 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5776 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5778 ** security-adverse side-effects and information-leaks.
5783 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5785 ** functions. However, if it is not specified for a user-defined window
5786 ** function, then any sub-types belonging to arguments passed to the window
5825 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5826 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5828 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5829 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5831 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5832 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5835 ** or a UTF-8 TEXT in bytes
5837 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5855 ** implement [application-defined SQL functions] and [virtual tables].
5865 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5866 ** in the native byte-order of the host machine. ^The
5868 ** extract UTF-16 strings as big-endian and little-endian respectively.
5919 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5934 ** fail if an out-of-memory error occurs during a format conversion.
5935 ** Only the following subset of interfaces are subject to out-of-memory
5948 ** If an out-of-memory error occurs, then the return value from these
5950 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5977 ** an [application-defined SQL function] argument V. The subtype
5997 ** then sqlite3_value_free(V) is a harmless no-op.
6058 ** the application-defined function is running.
6078 ** These functions may be used by (non-aggregate) SQL functions to
6082 ** of where this might be useful is in a regular-expression matching
6091 ** value to the application-defined function. ^N is zero for the left-most
6096 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6097 ** argument of the application-defined function. ^Subsequent
6121 ** function parameters that are compile-time constants, including literal
6124 ** The value of the N parameter to these interfaces should be non-negative.
6151 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6167 ** an application-defined function to be the BLOB whose content is pointed
6172 ** interfaces set the result of the application-defined function to be
6176 ** an application-defined function to be a floating point value specified
6184 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6185 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6186 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6191 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6209 ** of the application-defined function to be the 32-bit signed integer
6212 ** of the application-defined function to be the 64-bit signed integer
6216 ** of the application-defined function to be NULL.
6220 ** set the return value of the application-defined function to be
6221 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6222 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6224 ** application-defined function to be a text string in an encoding
6234 ** is non-negative, then as many bytes (not characters) of the text
6235 ** pointed to by the 2nd parameter are taken as the application-defined
6236 ** function result. If the 3rd parameter is non-negative, then it
6243 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6259 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6261 ** byte-order specified by the BOM. ^The byte-order specified by
6262 ** the BOM at the beginning of the text overrides the byte-order
6265 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6276 ** the application-defined function to be a copy of the
6287 ** also associates the host-language pointer P or type T with that
6289 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6297 ** than the one containing the application-defined function that received
6329 ** the result from the [application-defined SQL function] with
6345 ** ^The name of the collation is a UTF-8 string
6347 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6410 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6453 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6454 ** the names are passed as UTF-16 in machine native byte order.
6455 ** ^A call to either function replaces the existing collation-needed callback.
6552 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6594 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6595 ** &nbsp; TemporaryFolder->Path->Data();
6598 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6611 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6650 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6655 ** the current directory on the sub-platforms of Win32 where that concept is
6659 ** UTF-8 or UTF-16, respectively.
6678 ** CAPI3REF: Test For Auto-Commit Mode
6682 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6686 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6688 ** If certain kinds of errors occur on a statement within a multi-statement
6719 ** for the N-th database on database connection D, or a NULL pointer of N is
6721 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6729 ** remember the string long-term should make their own copy. Applications that
6731 ** threads should mutex-protect calls to this API and should make their own
6743 ** connection D, or if database N is a temporary or in-memory database, then
6748 ** is [DETACH]-ed or until the database connection closes.
6769 ** CAPI3REF: Determine if a database is read-only
6773 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6792 ** a valid schema, then -1 is returned.
6858 ** ^If the callback on a commit hook function returns non-zero,
6880 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6882 ** hook returning non-zero, just as it would be with any other rollback.
6902 ** the schema-name of the attached database that is being autovacuumed,
6910 ** <p>^If there are multiple ATTACH-ed database files that are being
7025 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7026 ** compile-time option is recommended because the
7052 ** shared cache mode should be enabled per-database connection via
7056 ** 32-bit integer is atomic.
7058 ** See Also: [SQLite Shared-Cache Mode]
7066 ** of heap memory by deallocating non-essential memory allocations
7068 ** pages to improve performance is an example of non-essential memory.
7071 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7085 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7120 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7144 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7145 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7178 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7182 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7249 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7261 ** with various operating-system specific extensions added.
7270 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7309 ** [extension loading] while evaluating user-entered SQL, the following API
7317 ** ^This interface enables or disables both the C-API
7320 ** to enable or disable only the C-API.)^
7360 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7389 ** The interface to the virtual-table mechanism is currently considered
7393 ** When the virtual-table mechanism stabilizes, we will declare the
7467 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7479 ** expr on the right-hand side can be evaluated (and thus the constraint
7494 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7500 ** non-zero.
7504 ** the right-hand side of the corresponding aConstraint[] is evaluated
7505 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7534 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7565 int iColumn; /* Column constrained. -1 for ROWID */
7568 int iTermOffset; /* Used internally - xBestIndex should ignore */
7610 ** ^The left-hand operand of the operator is given by the corresponding
7611 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7614 ** operators have no left-hand operand, and so for those operators the
7623 ** The right-hand operands for each constraint might be accessible using
7624 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7626 ** in the input SQL. If the right-hand operand is another column or an
7630 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7635 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7798 ** The interface to the virtual-table mechanism defined above (back up
7803 ** When the virtual-table mechanism stabilizes, we will declare the
7840 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7842 ** read-only access.
7878 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7894 ** and the built-in [zeroblob] SQL function may be used to create a
7895 ** zero-filled blob to read or write using the incremental-blob interface.
7925 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7947 ** ^If the blob handle being closed was opened for read-write access, and if
7948 ** the database is in auto-commit mode and there are no other open read-write
7956 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7983 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8012 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8061 ** ^Names are zero-terminated UTF-8 strings.
8092 ** is selected automatically at compile-time. The following
8103 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8108 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8145 ** cases where it really needs one. If a faster non-recursive mutex
8192 ** behave as no-ops.
8205 ** An instance of this structure defines the low-level routines
8255 ** xMutexInit() must be no-ops.
8301 ** the routine should return 1. This seems counter-intuitive since
8305 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8321 ** next. Applications that override the built-in mutex logic must be
8337 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8352 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8358 ** CAPI3REF: Low-Level Control Of Database Files
8470 ** by enclosing in double-quotes) so as not to confuse the parser.
8475 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8478 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8484 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8496 ** <li> Put all identifier names inside double-quotes. This is the official
8507 ** compile-time options. For example, "VACUUM" is not a keyword if
8508 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8519 ** An instance of the sqlite3_str object contains a dynamically-sized
8543 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8583 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8588 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8589 ** S must contain at least N non-zero bytes of content. To append a
8590 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8594 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8597 ** single-byte character C onto the end of [sqlite3_str] object X.
8623 ** [SQLITE_NOMEM] following any out-of-memory error, or
8630 ** zero-termination byte.
8665 ** SQLITE_OK on success and a non-zero [error code] on failure.
8668 ** be represented by a 32-bit integer, then the values returned by
8686 ** These integer constants designate various run-time status parameters
8694 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8779 ** non-zero [error code] on failure.
8795 ** The [sqlite3_db_status()] interface will return a non-zero error code
8805 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8813 ** Only the high-water value is meaningful;
8821 ** Only the high-water value is meaningful;
8844 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8884 ** to disk all at once. When pages spill mid-transaction, that introduces
8955 ** A non-zero value in this counter may indicate an opportunity to
8961 ** A non-zero value in this counter may indicate an opportunity to
8990 ** step was bypassed because a Bloom filter returned not-found. The
9060 ** The built-in page cache is recommended for most uses.
9075 ** built-in default page cache is used instead of the application defined
9107 ** false if it is used for an in-memory database. The cache implementation
9118 ** suggested maximum cache-size (number of pages stored by) the cache
9164 ** as its second argument. If the third parameter, discard, is non-zero,
9198 ** is not obligated to free any memory, but well-behaved implementations should
9257 ** for copying in-memory databases to or from persistent files.
9263 ** ^The source database is read-locked only while it is being read;
9297 ** there is already a read or read-write transaction open on the
9329 ** <li> the destination database was opened read-only, or
9330 ** <li> the destination database is using write-ahead-log journaling
9332 ** <li> the destination database is an in-memory database and the
9336 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9337 ** the [sqlite3_busy_handler | busy-handler function]
9339 ** busy-handler returns non-zero before the lock is available, then
9360 ** sqlite3_backup_step(), the source database may be modified mid-way
9377 ** active write-transaction on the destination database is rolled back.
9384 ** ^If an out-of-memory condition or IO error occurred during any prior
9453 ** ^When running in shared-cache mode, a database operation may fail with
9454 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9455 ** individual tables within the shared-cache cannot be obtained. See
9456 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9460 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9464 ** ^Shared-cache locks are released when a database connection concludes
9468 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9478 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9484 ** ^If the blocked connection is attempting to obtain a write-lock on a
9485 ** shared-cache table, and more than one other connection currently holds
9486 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9489 ** ^(There may be at most one unlock-notify callback registered by a
9491 ** blocked connection already has a registered unlock-notify callback,
9494 ** unlock-notify callback is canceled. ^The blocked connections
9495 ** unlock-notify callback may also be canceled by closing the blocked
9498 ** The unlock-notify callback is not reentrant. If an application invokes
9499 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9507 ** When an unlock-notify callback is registered, the application provides a
9511 ** an unlock-notify callback is a pointer to an array of void* pointers,
9515 ** more than one blocked connection that has registered for an unlock-notify
9525 ** Assuming that after registering for an unlock-notify callback a
9536 ** unlock-notify callback is registered. The system is said to be in
9537 ** a deadlocked state if connection A has registered for an unlock-notify
9539 ** B has itself registered for an unlock-notify callback when connection
9542 ** registered for an unlock-notify callback on the conclusion of connection
9554 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9555 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9575 ** and extensions to compare the contents of two buffers containing UTF-8
9576 ** strings in a case-independent fashion, using the same definition of "case
9592 ** Note that this routine returns zero on a match and non-zero if the strings
9609 ** insensitive - equivalent upper and lower case ASCII characters match
9615 ** Note that this routine returns zero on a match and non-zero if the strings
9639 ** a fixed-length buffer on the stack. If the log message is longer than
9646 ** CAPI3REF: Write-Ahead Log Commit Hook
9653 ** the associated write-lock on the database released)^, so the implementation
9659 ** ^The third parameter is the name of the database that was written to -
9660 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9661 ** is the number of pages currently in the write-ahead log file,
9672 ** A single database handle may have at most a single write-ahead log callback
9674 ** previously registered write-ahead log callback. ^The return value is
9687 ** CAPI3REF: Configure an auto-checkpoint
9694 ** more frames in the [write-ahead log] file. ^Passing zero or
9709 ** ^Every new [database connection] defaults to having the auto-checkpoint
9725 ** [write-ahead log] for database X on [database connection] D to be
9726 ** transferred into the database file and for the write-ahead log to
9752 ** in the log were checkpointed. ^The [busy-handler callback]
9759 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9768 ** [busy-handler callback])
9781 ** the log file or to -1 if the checkpoint could not run because
9785 ** was called) or to -1 if the checkpoint could not run due to an error or
9793 ** busy-handler configured, it will not be invoked in this case.
9797 ** obtained immediately, and a busy-handler is configured, it is invoked and
9798 ** the writer lock retried until either the busy-handler returns 0 or the lock
9799 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9800 ** database readers as described above. ^If the busy-handler returns 0 before
9803 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9819 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9895 ** If X is non-zero, then the virtual table implementation guarantees
9981 ** [xUpdate] method understands as a "no-change" value.
10122 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10123 ** only sees a single value from the right-hand side of the IN operator
10127 ** table to see all values on the right-hand of the IN operator all at
10132 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10133 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10135 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10136 ** by which the virtual table can ask SQLite if all-at-once processing
10142 ** the IN operator all-at-once, respectively. ^Thus when the third
10143 ** parameter (F) is non-negative, this interface is the mechanism by
10152 ** (non-zero), that means that the constraint is an IN operator
10153 ** that can be processed all-at-once. ^If the constraint is not an IN
10154 ** operator or cannot be processed all-at-once, then the interface returns
10157 ** ^(All-at-once processing of the IN operator is selected if both of the
10161 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10163 ** use the N-th constraint.
10166 ** non-negative had F>=1.
10170 ** the traditional one-at-a-time processing strategy for the IN constraint.
10171 ** ^If both conditions are true, then the argvIndex-th parameter to the
10174 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10180 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10190 ** a parameter that was previously selected for all-at-once IN constraint
10193 ** 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".
10303 ** set to the total number of times that the X-th loop has run.</dd>
10307 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10312 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10319 ** to a zero-terminated UTF-8 string containing the name of the index or table
10320 ** used for the X-th loop.
10324 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10325 ** description for the X-th loop.
10329 ** "select-id" for the X-th loop. The select-id identifies which query or
10330 ** subquery the loop is part of. The main query has a select-id of zero.
10331 ** The select-id is the same value as is output in the first column
10353 ** compile-time option.
10361 ** Loops are numbered starting from zero. ^If idx is out of range - less than
10363 ** the statement - a non-zero value is returned and the variable that pOut
10368 ** as if the loop did not exist - it returns non-zero and leave the variable
10381 ** CAPI3REF: Zero Scan-Status Counters
10386 ** This API is only available if the library is built with pre-processor
10392 ** CAPI3REF: Flush caches to disk mid-transaction
10395 ** ^If a write-transaction is open on [database connection] D when the
10397 ** pages in the pager-cache that are not currently in use are written out
10401 ** interface flushes caches for all schemas - "main", "temp", and
10406 ** immediately and there is a busy-handler callback configured, it is invoked
10414 ** example an IO error or out-of-memory condition), then processing is
10425 ** CAPI3REF: The pre-update hook.
10429 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10501 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10502 ** triggers; or 2 for changes resulting from triggers called by top-level
10506 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10511 ** pre-update hook is being invoked for some other reason, including a
10512 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10538 ** CAPI3REF: Low-level system error code
10543 ** The return value is OS-dependent. For example, on unix systems, after
10583 ** If there is not already a read-transaction open on schema S when
10615 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10649 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10650 ** SQLITE_IOERR error code - is returned, then the final state of the
10664 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10681 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10748 ** For an ordinary on-disk database file, the serialization is just a
10749 ** copy of the disk file. For an in-memory database or a "TEMP" database,
10786 ** Zero or more of the following constants can be OR-ed together for
10790 ** a pointer to contiguous in-memory database that it is currently using,
10792 ** a contiguous in-memory database, then this option causes
10794 ** using a contiguous in-memory database if it has been initialized by a
10804 ** reopen S as an in-memory database based on the serialization contained
10808 ** permitted to add content to the in-memory database as long as the total
10860 ** should be treated as read-only.
10864 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10904 /* The double-precision datatype used by RTree depends on the
10905 ** SQLITE_RTREE_INT_ONLY compile-time option.
10915 ** R-Tree geometry query as follows:
10940 ** Register a 2nd-generation geometry callback named zScore that can be
10941 ** used as part of an R-Tree geometry query as follows:
11047 ** is not possible for an application to register a pre-update hook on a
11050 ** which a pre-update hook is already defined. The results of attempting
11118 ** disabled - it does not. A newly created session object is enabled.
11125 ** no-op, and may be used to query the current state of the session.
11202 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11208 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11212 ** conflict-handler callback) then the X'' value is returned. The application
11260 ** database row along with the updated values for each updated non-primary-key
11269 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11271 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11284 ** are sorted in the same order in which they were attached (or auto-attached)
11298 ** recorded once - the first time a row with said primary key is inserted,
11305 ** The session object therefore accumulates two types of records - those
11323 ** found and one or more of the non-primary key fields have been
11340 ** This may appear to have some counter-intuitive effects if a single row
11356 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11378 ** does not have a primary key, this function is a no-op (but does not return
11398 ** used to update the table in database zFrom (call this the "from-table")
11400 ** object (call this the "to-table"). Specifically:
11403 ** <li> For each row (primary key) that exists in the to-table but not in
11404 ** the from-table, an INSERT record is added to the session object.
11406 ** <li> For each row (primary key) that exists in the to-table but not in
11407 ** the from-table, a DELETE record is added to the session object.
11410 ** different non-PK values in each, an UPDATE record is added to the
11455 ** Because the non-primary key "old.*" fields are omitted, no
11474 ** Return non-zero if no changes to attached tables have been recorded by
11482 ** are restored. However, if this function returns non-zero, then it is
11569 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11593 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11606 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11609 ** or until the conflict-handler function returns.
11656 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11665 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11696 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11730 ** conflict-handler callback by [sqlite3changeset_apply()] with either
11741 ** "conflicting row" associated with the current conflict-handler callback
11779 ** function with an iterator passed to a conflict-handler by
11787 ** returned. This is to allow the following pattern (pseudo-code):
11940 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
11941 ** <tr><th style="white-space:pre">Existing Change </th>
11942 ** <th style="white-space:pre">New Change </th>
11987 ** returned. Or, if an out-of-memory condition occurs during processing, this
12045 ** Otherwise, if the return value is non-zero or the xFilter argument to
12081 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12085 ** returned by each invocation of the conflict-handler function. Refer to
12094 ** stored in all non-primary key columns also match the values stored in
12098 ** the non-primary key fields contains a value different from the original
12099 ** row value stored in the changeset, the conflict-handler function is
12102 ** only the values of those non-primary key fields are compared against
12103 ** the current database contents - any trailing database table columns
12107 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12112 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12131 ** This includes the case where the INSERT operation is re-attempted because
12139 ** stored in all modified non-primary key columns also match the values
12143 ** the modified non-primary key fields contains a value different from an
12144 ** original row value stored in the changeset, the conflict-handler function
12146 ** UPDATE changes only contain values for non-primary key fields that are
12148 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12151 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12155 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12173 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12250 ** Values that may be passed as the second argument to a conflict-handler.
12257 ** (non primary-key) fields modified by the update do not contain the
12418 ** combined on a per-field basis, not per-row. This means that in the
12502 ** corresponding non-streaming API functions:
12504 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12505 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12515 ** Non-streaming functions that accept changesets (or patchsets) as input
12520 ** low-memory environment is required to handle very large changesets, the
12784 ** non-negative but less than the number of columns in the table, return
12799 ** non-negative but less than the number of columns in the table, set
12813 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12911 ** If the bClear argument is non-zero, then the auxiliary data is cleared
12933 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12935 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12941 ** modify this structure directly - it should only be used as shown above
12949 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12963 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12965 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12974 ** xPhraseFirstColumn() set iCol to -1).
13043 ** The second and third arguments are an array of nul-terminated strings
13061 ** by argument pText. pText may or may not be nul-terminated. The first
13069 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13074 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13078 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13083 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13116 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13150 ** still appears to contain just two phrases - "(first OR 1st)"
13161 ** when tokenizing query text (it should not - to do so would be
13208 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13309 ** autoconf-based build
13337 ** The hard limit is the ability of a 32-bit signed integer
13338 ** to count the size: 2^31-1 or 2147483647.
13356 ** tell you that in a well-normalized database, you usually should
13414 ** The suggested maximum number of in-memory pages to use for
13417 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13419 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13420 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13423 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13437 ** counted using a signed 8-bit integer which has a maximum value of 127
13449 ** as a signed 32-bit integer can hold.
13456 ** imposed by the use of 16-bit offsets within each page.
13462 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13463 ** compiled with the default page-size limit will not be able to rollback
13486 ** device characteristics (sector-size and atomic write() support),
13503 ** This value can be lowered (or raised) at run-time using that the
13534 #pragma warn -rch /* unreachable code */
13535 #pragma warn -ccc /* Condition is always true or false */
13536 #pragma warn -aus /* Assigned value is never used */
13537 #pragma warn -csu /* Comparing signed and unsigned */
13538 #pragma warn -spa /* Suspicious pointer arithmetic */
13542 ** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13546 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13579 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13592 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13593 #else /* Generates a warning - but it always works */
13633 ** level of threadsafety. 2 means the library is multithreaded - multiple
13641 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13649 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13655 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13662 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13691 # error "Two or more of the following compile-time configuration options\
13729 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13794 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13806 ** of SQLite to unexpected behavior - to make the code "self-healing"
13812 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
13898 ** Return true (non-zero) if the input is an integer that is too large
13899 ** to fit in 32-bits. This macro is used inside of various testcase()
13900 ** macros to verify that we have tested SQLite for large-file support.
13927 ** This is the header file for the generic hash-table implementation
13938 ** The internals of this structure are intended to be opaque -- client
13945 ** All elements of the hash table are on a single doubly-linked list.
13949 ** the global doubly-linked list. The contents of the bucket are the
13950 ** element pointed to plus the next _ht.count-1 elements in the list.
13969 ** structure. All elements are stored on a single doubly-linked list.
14000 #define sqliteHashFirst(H) ((H)->first)
14001 #define sqliteHashNext(E) ((E)->next)
14002 #define sqliteHashData(E) ((E)->data)
14003 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14004 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14009 #define sqliteHashCount(H) ((H)->count)
14219 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14224 ** substitute integer for floating-point
14255 ** the VDBE-level file format changes. The following macros define the
14266 ** changed at run-time using a pragma.
14274 ** on the command-line
14304 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14306 ** The default value of "20" was chosen to minimize the run-time of the
14307 ** speedtest1 test program with options: --shrink-memory --reprepare
14321 ** The compile-time options SQLITE_MMAP_READWRITE and
14334 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14365 ** types can be conveniently redefined at compile-type. Like this:
14367 ** cc '-DUINTPTR_TYPE=long long int' ...
14407 typedef sqlite_int64 i64; /* 8-byte signed integer */
14408 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14409 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14410 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14411 typedef INT16_TYPE i16; /* 2-byte signed integer */
14412 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14413 typedef INT8_TYPE i8; /* 1-byte signed integer */
14421 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14426 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
14427 ** can be used at compile-time if desired.
14430 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14432 typedef u32 tRowcnt; /* 32-bit is the default */
14436 ** Estimated quantities used for query planning are stored as 16-bit
14438 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14447 ** 1 -> 0 20 -> 43 10000 -> 132
14448 ** 2 -> 10 25 -> 46 25000 -> 146
14449 ** 3 -> 16 100 -> 66 1000000 -> 199
14450 ** 4 -> 20 1000 -> 99 1048576 -> 200
14451 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14456 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14499 ** and whether or not that determination is run-time or compile-time.
14501 ** For best performance, an attempt is made to guess at the byte-order
14502 ** using C-preprocessor macros. If that is unsuccessful, or if
14503 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14504 ** at run-time.
14539 ** Constants for the largest and smallest possible 64-bit signed integers.
14540 ** These macros are designed to work correctly on both 32-bit and 64-bit
14545 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14549 ** to force 8-byte alignment on 64-bit architectures.
14554 ** pointers in size, and so it is a no-op on systems where the pointer
14570 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14575 ** underlying malloc() implementation might return us 4-byte aligned
14576 ** pointers. In that case, only verify 4-byte alignment.
14579 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14581 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14593 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14613 ** default MMAP_SIZE is specified at compile-time, make sure that it does
14637 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14658 ** An instance of the following structure is used to store the busy-handler
14663 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14697 ** The root-page of the schema table.
14716 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14735 ** directly, we use its constant as a key to lookup the run-time allocated
14737 ** for the run-time allocated buffer.
14740 ** macros become no-ops and have zero performance impact.
14838 ** Changing this from a 64-bit to a 32-bit type limits the number of
14860 #define ALLBITS ((Bitmask)-1)
14861 #define TOPBIT (((Bitmask)1)<<(BMS-1))
14890 ** This header file (together with is companion C source-code file
14894 ** This header file is #include-ed by sqliteInt.h and thus ends up
14902 ** necessary pre-processor macros for it.
14918 ** This file contains pre-processor directives related to operating system
14939 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15002 ** a no-op
15028 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15030 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15031 ** Mcafee started using SQLite in their anti-virus product and it
15077 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15185 ** sqlite3_malloc() to obtain space for the file-handle structure.
15221 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15244 ** is devoted to storing a super-journal name - there are no more pages to
15248 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15249 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15257 #define PAGER_MEMORY 0x0002 /* In-memory database */
15262 #define PAGER_LOCKINGMODE_QUERY -1
15273 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15278 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15279 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15285 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15307 ** that make up the Pager sub-system API. See source code comments for
15462 ** This header file defines the interface that the sqlite B-Tree file
15475 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15482 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15483 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15496 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15511 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15512 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15566 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15569 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15572 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15573 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15593 ** For example, the free-page-count field is located at byte offset 36 of
15594 ** the database file header. The incr-vacuum-flag field is located at
15598 ** It is a read-only number computed by the pager. But we merge it with
15611 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15625 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15626 ** column of the b-tree of the cursor. The Expr tree will not contain
15627 ** any function calls nor subqueries nor references to b-trees other than
15630 ** The design of the _RANGE hint is aid b-tree implementations that try
15631 ** to prefetch content from remote machines - to provide those
15656 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15661 ** For read-only cursors the wrFlag argument is always zero. For read-write
15672 ** by this, the native b-tree engine of SQLite, but it is available to
15674 ** b-tree system. For alternative storage engines in which a delete of
15678 ** and DELETE operations as no-ops, and any READ operation against a
15681 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
15687 int wrFlag, /* 1 for writing. 0 for read-only */
15826 ** Enter and Leave procedures no-ops.
15913 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15944 u32 iSrcLine; /* Source-code line that generated this opcode
15952 ** A sub-routine used to implement a trigger program.
15955 VdbeOp *aOp; /* Array of opcodes for sub-program */
15961 SubProgram *pNext; /* Next sub-program already visited */
15981 #define P4_STATIC (-1) /* Pointer to a static string */
15982 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15983 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15984 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15985 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
15987 #define P4_FREE_IF_LE (-6)
15988 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
15989 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
15990 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
15991 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
15992 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
15993 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
15994 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
15995 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
15996 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
15997 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16098 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16099 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16100 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16129 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16146 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16163 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16199 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16275 ** Additional non-public SQLITE_PREPARE_* flags
16324 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16329 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16406 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16443 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16448 ** // in distingishing equal and not-equal.
16451 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16452 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16460 ** is responsible for keeping track of this and reporting byte-code branches
16590 /* Modify the page-size after the cache has been created. */
16612 /* Change a page number. Used by incr-vacuum. */
16654 /* Set and get the suggested cache-size for the specified pager-cache.
16657 ** the total number of pages cached by purgeable pager-caches to the sum
16658 ** of the suggested cache-sizes.
16665 /* Set or get the suggested spill-size for the specified pager-cache.
16667 ** The spill-size is the minimum number of pages in cache before the cache
16731 ** at start-time.
16733 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
16736 ** start-time.
16738 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16740 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16757 ** If this is a no-op implementation, implement everything as macros.
16778 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16798 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16799 ** In other words, the zero-based numbers are used for all external interfaces
16800 ** and the one-based values are used internally.
16828 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16857 ** Db.pSchema->flags field.
16859 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16860 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16861 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16862 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16865 ** Allowed values for the DB.pSchema->flags field.
16885 ** Lookaside malloc is a set of fixed-size buffers that can be used
16907 ** in a performance-critical path. sz should be set by to szTrue whenever
16912 ** come off of pFree first, then pInit as a fallback. This dual-list
16913 ** allows use to compute a high-water mark - the maximum number of allocations
16914 ** outstanding at any point in the past - by subtracting the number of
16917 ** Enhancement on 2019-12-12: Two-size-lookaside
16921 ** The two-size-lookaside enhancement breaks up the lookaside allocation
16922 ** into two pools: One of 128-byte slots and the other of the default size
16923 ** (1200-byte) slots. Allocations are filled from the small-pool first,
16924 ** failing over to the full-size pool if that does not work. Thus more
16941 void *pMiddle; /* First byte past end of full-size buffers and
16946 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
16952 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16953 #define EnableLookaside db->lookaside.bDisable--;\
16954 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16956 /* Size of the smaller allocations in two-size lookside */
16964 ** A hash table for built-in function definitions. (Application-defined
17054 Db *aDb; /* All backends */
17055 int nDb; /* Number of backends currently in use */
17060 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17068 u8 autoCommit; /* The auto-commit flag. */
17072 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17078 u8 noSharedCache; /* True if no shared-cache backends */
17126 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17160 Db aDbStatic[2]; /* Static space for the 2 default backends */
17164 int nSavepoint; /* Number of non-transaction savepoints */
17165 int nStatement; /* Number of nested statement-transactions */
17203 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17204 #define ENC(db) ((db)->enc)
17209 ** C-compilers still do not accept LL integer literals.
17233 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
17252 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17253 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17274 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17295 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17299 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17301 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17309 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17319 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17320 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17326 #define ConstFactorOk(P) ((P)->okConstFactor)
17341 ** structure. For global built-in functions (ex: substr(), max(), count())
17343 ** For per-connection application-defined functions, a pointer to this
17344 ** structure is held in the db->aHash hash table.
17346 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17347 ** field is used by per-connection app-def functions.
17350 i8 nArg; /* Number of arguments. -1 means unlimited */
17354 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17357 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17366 ** This structure encapsulates a user-function destructor callback (as
17403 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17406 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17407 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17408 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17409 /* 0x0200 -- available for reuse */
17410 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17414 ** single query - might change over time */
17415 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17416 /* 0x8000 -- available for reuse */
17417 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17420 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17422 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17423 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17426 /* Identifier numbers for each in-line function */
17444 ** as the user-data (sqlite3_user_data()) for the function. If
17455 ** zName is the name of a function that is implemented by in-line
17461 ** zName is the name of a test-only function implemented by in-line
17470 ** a single query. The iArg is ignored. The user-data is always set
17474 ** For math-library functions. xPtr is an arbitrary pointer.
17479 ** ignored and the user-data for these functions is set to an
17480 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17498 ** available as the function user-data (sqlite3_user_data()). The
17562 char *zName; /* Savepoint name (nul-terminated) */
17623 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17654 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17674 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17686 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17739 ** database connections, even when the rest of the in-memory database
17749 ** database schema are initially stored in a linked-list pointed to by
17756 ** When an in-memory Table object is deleted (for example when the
17806 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17837 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17838 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17848 #define TF_Readonly 0x00000001 /* Read-only system table */
17859 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
17860 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
17875 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
17876 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
17884 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17886 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
17894 ** only works for non-virtual tables (ordinary tables and views) and is
17899 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17900 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17902 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17911 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17912 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17927 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17930 ** from-table == child-table
17931 ** to-table == parent-table
17934 ** which is attached to the from-table. The to-table need not exist when
17935 ** the from-table is created. The existence of the to-table is not checked.
17949 /* EV: R-30323-21917 */
18015 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18043 ** pKeyInfo->nField.
18047 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18051 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18052 ** multiple entries in the b-tree with the same key (when only looking
18053 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18058 ** get and equal results when comparing this structure to a b-tree record.
18062 ** b-tree.
18065 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18104 ** algorithm to employ when an attempt is made to insert a non-unique
18111 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18113 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18114 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18124 ** number (it cannot - the database page is not allocated until the VDBE
18149 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18160 IndexSample *aSample; /* Samples of the left-most key */
18161 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18162 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18176 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18179 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18184 #define XN_ROWID (-1) /* Indexed column is the rowid */
18185 #define XN_EXPR (-2) /* Indexed column is an expression */
18217 const char *z; /* Text of the token. Not NULL-terminated! */
18240 int sortingIdxPTab; /* Cursor number of pseudo-table */
18268 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18269 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18270 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18275 ** the option is available (at compile-time).
18290 ** to represent the greater-than-or-equal-to operator in the expression
18304 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18323 ** subquery gives a constant result, then iTable is -1. If the subquery
18328 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18359 int iValue; /* Non-negative integer value if EP_IntValue */
18384 ** TK_TRIGGER: 1 -> new, 0 -> old
18389 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18392 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18456 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18457 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18458 #define ExprSetProperty(E,P) (E)->flags|=(P)
18459 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18460 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18461 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18466 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18467 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18468 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18469 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18470 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18471 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18472 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18481 ** processes but is a no-op for delivery.
18484 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18485 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18486 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18506 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18516 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18532 ** ---------- -------------------------
18591 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18600 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18611 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18622 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18623 ** contains more than 63 columns and the 64-th or later column is used.
18641 int regResult; /* Registers holding results of a co-routine */
18646 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18647 unsigned isCorrelated :1; /* True if sub-query is correlated */
18649 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18655 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18667 ExprList *pFuncArg; /* Arguments to table-valued-function */
18717 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18720 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18723 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18752 ** NameContexts can be nested. When resolving names, the inner-most
18768 ExprList *pEList; /* Optional list of result-set columns */
18809 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
18817 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18818 ** conflict-target clause.) The pUpsertTargetWhere is the optional
18922 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
18928 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
18936 ** identified by pDest->iSDParm.
18938 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
18940 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18945 ** the side-effects of functions.
18952 ** in register pDest->iSDParm then abandon the rest
18956 ** row of result as the key in table pDest->iSDParm.
18957 ** Apply the affinity pDest->affSdst before storing
18960 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18966 ** SRT_Coroutine Generate a co-routine that returns a new row of
18968 ** of the co-routine is stored in register pDest->iSDParm
18969 ** and the result row is stored in pDest->nDest registers
18970 ** starting with pDest->iSdst.
18972 ** SRT_Table Store results in temporary table pDest->iSDParm.
18978 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18979 ** But also use temporary table pDest->iSDParm+1 as
18983 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
18987 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18989 ** index at pDest->iSDParm+1 hold all prior stores.
18992 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18993 ** table is an intkey table - in this case the first
18995 ** key. If (pDest->iSDParm>0), then the table is an index
18996 ** table. (pDest->iSDParm) is the number of key columns in
19008 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
19014 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19042 ** the code generator needs. We have to keep per-table autoincrement
19061 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19142 ** The nTableLock and aTableLock variables are only used if the shared-cache
19143 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19144 ** used to store the set of table-locks required by the statement being
19174 ** of the base register during check-constraint eval */
19185 int nMaxArg; /* Max args passed to user function by sub-program */
19189 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19234 int nHeight; /* Expression tree height of current sub-select */
19239 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
19246 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19268 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19270 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19279 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19285 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19291 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19315 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19319 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19323 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19358 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19359 the <column-list> is stored here */
19378 ** that is a part of a trigger-program.
19383 ** the first step of the trigger-program.
19390 ** orconf -> stores the ON CONFLICT algorithm
19391 ** pSelect -> The content to be inserted - either a SELECT statement or
19393 ** zTarget -> Dequoted name of the table to insert into.
19394 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19395 ** statement, then this stores the column-names to be
19397 ** pUpsert -> The ON CONFLICT clauses for an Upsert
19400 ** zTarget -> Dequoted name of the table to delete from.
19401 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19405 ** zTarget -> Dequoted name of the table to update.
19406 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19408 ** pExprList -> A list of the columns to update and the expressions to update
19413 ** pSelect -> The SELECT statement
19416 ** pExprList -> The list of expressions that follow the RETURNING keyword.
19432 TriggerStep *pNext; /* Next in the link-list */
19433 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19462 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19466 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19492 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19495 ** be used on trunk check-ins. They are a temporary mechanism available
19500 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
19502 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
19517 u8 bUseCis; /* Use covering indices for full-scans */
19521 int neverCorrupt; /* Database is always well-formed */
19524 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19525 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19526 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19527 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19537 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19539 /* The above might be initialized to non-zero. The following need to always
19570 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19574 /* vvvv--- must be last ---vvv */
19582 ** the assert is only valid on a well-formed database. Instead of:
19594 ** things that are always true for well-formed databases.
19599 ** Context pointer passed down through the tree-walk.
19640 const char *zType; /* Type of the container - used for error messages */
19641 const Token *pName; /* Name of the container - used for error messages */
19668 ** Return code from the parse-tree walking primitives and their
19729 ** data structures on sqlite3DebugPrintf() using a tree-like view.
19746 ** (2) All window functions in a single SELECT form a linked-list
19816 ** Assuming zIn points to the first byte of a UTF-8 character,
19817 ** advance zIn to point to the first byte of the next UTF-8 character.
19868 for (; i < bufSize && (i * 2 < encodeBufSize - 1); i++) { in sqlite3base16Encode()
19879 ** routines that report the line-number on which the error originated
19906 # define SQLITE_CORRUPT_REPORT_PGNO(context) SQLITE_CORRUPT_PGNO((context)->pgno,(context))
19926 ** The ctype.h header is needed for non-ASCII systems. It is also
20060 # define MAN754 ((((u64)1)<<52)-1)
20200 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20201 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20210 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20477 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20478 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20540 ** Routines to read and write variable-length integers. These used to
20755 ** The interface to the LEMON-generated parser
20816 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20895 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20897 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20899 ** provided (enforcement of FK constraints requires the triggers sub-system).
20950 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
21003 ** sqlite3IoTrace is a pointer to a printf-like routine used to
21036 ** passed back to non-lookaside free() routines. Asserts such as the
21037 ** example above are placed on the non-lookaside free() routines to verify
21040 ** All of this is no-op for a production build. It only comes into
21041 ** play when the SQLITE_MEMDEBUG compile-time option is used.
21048 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21102 ** all of the platform-specific files (os_*.c) and is #included into those
21114 ** switch. The following code should catch this problem at compile-time.
21128 ** high-performance timing routines.
21144 ** This file contains inline asm code for retrieving "high-performance"
21151 ** The following routine only works on pentium-class (or newer) processors.
21153 ** processor and returns that value. This can be used for high-res
21224 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21248 || sqlite3_io_error_pending-- == 1 ) \
21263 sqlite3_diskfull_pending--; \
21310 ** This file implements routines used to report what compile-time options
21313 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21317 ** autoconf-based build
21331 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21337 ** An array of names of all compile-time options. This array should
21338 ** be sorted A-Z.
21341 ** only a handful of compile-time options, so most times this array is usually
21379 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21383 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21385 "COMPILER=gcc-" __VERSION__,
22102 /* An array to map all upper-case characters into their corresponding
22103 ** lower-case character.
22105 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22145 /* All of the upper-to-lower conversion data is above. The following
22162 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22163 ** be undefined behavior. That's goofy, but the C-standards people thought
22171 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22172 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22173 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22176 ** The following 256 byte lookup table is used to support SQLites built-in
22189 ** case. i.e. if the character is a lower-case ASCII character.
22190 ** If x is a lower-case ASCII character, then its upper-case equivalent
22191 ** is (x - 0x20). Therefore toupper() can be implemented as:
22198 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22200 ** non-ASCII UTF character. Hence the test for whether or not a character is
22209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22241 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22245 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22246 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22248 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22263 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22264 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22265 ** that compile-time option is omitted.
22271 # error "Compile-time disabling of covering index scan using the\
22272 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22288 ** before 3.12.0). -1 means always keep the entire statement journal in
22298 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22300 ** and N is the number of slots. The lookaside-configuration can be
22301 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22302 ** or at run-time for an individual database connection using
22305 ** With the two-size-lookaside enhancement, less lookaside is required.
22306 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22307 ** and 93 128-byte slots, which is more lookaside than is available
22308 ** using the older 1200,100 configuration without two-size-lookaside.
22319 /* The default maximum size of an in-memory database created using
22393 ** Hash table for global functions - functions common to all
22395 ** read-only.
22414 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22421 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
22578 ** * A b-tree cursor
22579 ** - In the main database or in an ephemeral database
22580 ** - On either an index or a table
22583 ** * A one-row "pseudotable" stored in a single register
22588 i8 iDb; /* Index of cursor database in db->aDb[] */
22597 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22647 /* Return true if P is a null-only cursor
22650 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
22659 ** When a sub-program is executed (OP_Program), a structure of this type
22662 ** values stored in the Vdbe struct. When the sub-program is finished,
22664 ** restoring the state of the VM to as it was before the sub-program
22671 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
22681 Vdbe *v; /* VM this frame belongs to */
22701 i64 nDbChange; /* Value of db->nChange */
22736 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22755 ** UPDATE no-change flag set
22761 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22765 ** length Mem.n. Zero-terminated if
22821 /* Return TRUE if Mem X contains dynamically allocated content - anything
22825 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
22831 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
22834 ** True if Mem X is a NULL-nochng type.
22837 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
22838 && (X)->n==0 && (X)->u.nZero==0)
22849 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
22855 ** of this structure. All such structures associated with a single VM
22857 ** when the VM is halted (if not before).
22884 Vdbe *pVdbe; /* The VM that owns this context */
22906 int iSelectID; /* The "Select-ID" for this loop */
22911 /* The DblquoteStr object holds the text of a double-quoted
22915 ** list is consulted for each double-quoted identifier to see if the
22944 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
22963 i64 startTime; /* Time when query started - used for profiling */
22974 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
22976 bft changeCntOn:1; /* True to update the change-counter */
22980 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
22986 DblquoteStr *pDblStr; /* List of double-quoted string literals */
22990 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
22992 u32 expmask; /* Binding to these vars invalidates VM */
22993 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
23041 ** set of values on the right-hand side of an IN constraint.
23057 ** single-byte varint.
23212 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23257 ** we have to locate the state vector at run-time. In the more common
23287 ** The value of N is added to the current status value and the high-water
23291 ** mark is unchanged. N must be non-negative for StatusDown().
23311 wsdStat.nowValue[op] -= N;
23383 p = p->pNext;
23393 u32 nInit = countLookasideSlots(db->lookaside.pInit);
23394 u32 nFree = countLookasideSlots(db->lookaside.pFree);
23396 nInit += countLookasideSlots(db->lookaside.pSmallInit);
23397 nFree += countLookasideSlots(db->lookaside.pSmallFree);
23399 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
23400 return db->lookaside.nSlot - (nInit+nFree);
23410 int *pHighwater, /* Write high-water mark here */
23411 int resetFlag /* Reset high-water mark if true */
23419 sqlite3_mutex_enter(db->mutex);
23424 LookasideSlot *p = db->lookaside.pFree;
23426 while( p->pNext ) p = p->pNext;
23427 p->pNext = db->lookaside.pInit;
23428 db->lookaside.pInit = db->lookaside.pFree;
23429 db->lookaside.pFree = 0;
23432 p = db->lookaside.pSmallFree;
23434 while( p->pNext ) p = p->pNext;
23435 p->pNext = db->lookaside.pSmallInit;
23436 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
23437 db->lookaside.pSmallFree = 0;
23450 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
23451 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
23453 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
23455 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
23470 for(i=0; i<db->nDb; i++){
23471 Btree *pBt = db->aDb[i].pBt;
23497 db->pnBytesFreed = &nByte;
23498 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23499 db->lookaside.pEnd = db->lookaside.pStart;
23500 for(i=0; i<db->nDb; i++){
23501 Schema *pSchema = db->aDb[i].pSchema;
23506 pSchema->tblHash.count
23507 + pSchema->trigHash.count
23508 + pSchema->idxHash.count
23509 + pSchema->fkeyHash.count
23511 nByte += sqlite3_msize(pSchema->tblHash.ht);
23512 nByte += sqlite3_msize(pSchema->trigHash.ht);
23513 nByte += sqlite3_msize(pSchema->idxHash.ht);
23514 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
23516 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
23519 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
23524 db->pnBytesFreed = 0;
23525 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23542 db->pnBytesFreed = &nByte;
23543 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23544 db->lookaside.pEnd = db->lookaside.pStart;
23545 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
23548 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23549 db->pnBytesFreed = 0;
23551 *pHighwater = 0; /* IMP: R-64479-57858 */
23573 for(i=0; i<db->nDb; i++){
23574 if( db->aDb[i].pBt ){
23575 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
23579 *pHighwater = 0; /* IMP: R-42420-56072 */
23580 /* IMP: R-54100-20147 */
23581 /* IMP: R-29431-39229 */
23586 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
23591 *pHighwater = 0; /* IMP: R-11967-56545 */
23592 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
23600 sqlite3_mutex_leave(db->mutex);
23620 ** There is only one exported symbol in this file - the function
23629 ** 1970-01-01 00:00:00 is JD 2440587.5
23630 ** 2000-01-01 00:00:00 is JD 2451544.5
23632 ** This implementation requires years to be expressed as a 4-digit number
23633 ** which means that only dates between 0000-01-01 and 9999-12-31 can
23639 ** use the julian calendar for dates prior to 1582-10-15 and for some
23647 ** ISBN 0-943396-61-1
23648 ** Willmann-Bell, Inc
23694 ** of a four-character format specifiers ABCD is:
23708 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23709 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23710 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23711 ** the 2-digit day which is the last integer in the set.
23724 char N = zFormat[0] - '0';
23725 char min = zFormat[1] - '0';
23730 max = aMx[zFormat[2] - 'a'];
23733 while( N-- ){
23737 val = val*10 + *zDate - '0';
23754 ** Parse a timezone extension on the end of a date-time.
23757 ** (+/-)HH:MM
23764 ** of change in p->tz and return 0. If a parser error occurs,
23765 ** return non-zero.
23774 p->tz = 0;
23776 if( c=='-' ){
23777 sgn = -1;
23791 p->tz = sgn*(nMn + nHr*60);
23794 p->tzSet = 1;
23822 ms = ms*10.0 + *zDate - '0';
23831 p->validJD = 0;
23832 p->rawS = 0;
23833 p->validHMS = 1;
23834 p->h = h;
23835 p->m = m;
23836 p->s = s + ms;
23838 p->validTZ = (p->tz!=0)?1:0;
23847 p->isError = 1;
23851 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
23852 ** that the YYYY-MM-DD is according to the Gregorian calendar.
23859 if( p->validJD ) return;
23860 if( p->validYMD ){
23861 Y = p->Y;
23862 M = p->M;
23863 D = p->D;
23865 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
23869 if( Y<-4713 || Y>9999 || p->rawS ){
23874 Y--;
23878 B = 2 - A + (A/4);
23881 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23882 p->validJD = 1;
23883 if( p->validHMS ){
23884 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
23885 if( p->validTZ ){
23886 p->iJD -= p->tz*60000;
23887 p->validYMD = 0;
23888 p->validHMS = 0;
23889 p->validTZ = 0;
23897 ** YYYY-MM-DD HH:MM:SS.FFF
23898 ** YYYY-MM-DD HH:MM:SS
23899 ** YYYY-MM-DD HH:MM
23900 ** YYYY-MM-DD
23903 ** on success and 1 if the input string is not a well-formed
23909 if( zDate[0]=='-' ){
23915 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
23923 p->validHMS = 0;
23927 p->validJD = 0;
23928 p->validYMD = 1;
23929 p->Y = neg ? -Y : Y;
23930 p->M = M;
23931 p->D = D;
23932 if( p->validTZ ){
23944 p->iJD = sqlite3StmtCurrentTime(context);
23945 if( p->iJD>0 ){
23946 p->validJD = 1;
23957 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23960 p->s = r;
23961 p->rawS = 1;
23963 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
23964 p->validJD = 1;
23974 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23978 ** In the first form, the +/-HH:MM is always optional. The fractional
24003 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
24026 if( p->validYMD ) return;
24027 if( !p->validJD ){
24028 p->Y = 2000;
24029 p->M = 1;
24030 p->D = 1;
24031 }else if( !validJulianDay(p->iJD) ){
24035 Z = (int)((p->iJD + 43200000)/86400000);
24036 A = (int)((Z - 1867216.25)/36524.25);
24037 A = Z + 1 + A - (A/4);
24039 C = (int)((B - 122.1)/365.25);
24041 E = (int)((B-D)/30.6001);
24043 p->D = B - D - X1;
24044 p->M = E<14 ? E-1 : E-13;
24045 p->Y = p->M>2 ? C - 4716 : C - 4715;
24047 p->validYMD = 1;
24055 if( p->validHMS ) return;
24057 s = (int)((p->iJD + 43200000) % 86400000);
24058 p->s = s/1000.0;
24059 s = (int)p->s;
24060 p->s -= s;
24061 p->h = s/3600;
24062 s -= p->h*3600;
24063 p->m = s/60;
24064 p->s += s - p->m*60;
24065 p->rawS = 0;
24066 p->validHMS = 1;
24081 p->validYMD = 0;
24082 p->validHMS = 0;
24083 p->validTZ = 0;
24093 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24107 ** using whatever operating-system specific localtime facility that
24109 ** non-zero on any kind of error.
24111 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24114 ** invoked in place of the OS-defined localtime() function.
24116 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24182 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
24183 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
24185 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
24192 iYearDiff = (2000 + x.Y%4) - x.Y;
24196 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
24199 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
24202 sqlite3_result_error(pCtx, "local time unavailable", -1);
24205 p->Y = sLocal.tm_year + 1900 - iYearDiff;
24206 p->M = sLocal.tm_mon + 1;
24207 p->D = sLocal.tm_mday;
24208 p->h = sLocal.tm_hour;
24209 p->m = sLocal.tm_min;
24210 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
24211 p->validYMD = 1;
24212 p->validHMS = 1;
24213 p->validJD = 0;
24214 p->rawS = 0;
24215 p->validTZ = 0;
24216 p->isError = 0;
24226 ** Where NNN is an arbitrary floating-point number and "days" can be one
24244 ** Process a modifier to a date-time stamp. The modifiers are
24285 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
24286 if( !p->rawS || p->validJD ){
24288 p->rawS = 0;
24289 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
24290 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
24292 r = p->s*1000.0 + 210866760000000.0;
24294 p->iJD = (sqlite3_int64)(r + 0.5);
24295 p->validJD = 1;
24296 p->rawS = 0;
24306 ** Always interpret the prior number as a julian-day value. If this
24312 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
24313 if( p->validJD && p->rawS ){
24315 p->rawS = 0;
24337 ** Treat the current value of p->s as the number of
24340 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
24341 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
24342 r = p->s*1000.0 + 210866760000000.0;
24345 p->iJD = (sqlite3_int64)(r + 0.5);
24346 p->validJD = 1;
24347 p->rawS = 0;
24353 if( p->tzSet==0 ){
24360 iGuess = iOrigJD = p->iJD;
24365 iGuess -= iErr;
24371 iErr = new.iJD - iOrigJD;
24374 p->iJD = iGuess;
24375 p->validJD = 1;
24376 p->tzSet = 1;
24389 ** date is already on the appropriate weekday, this is a no-op.
24396 p->validTZ = 0;
24397 p->validJD = 0;
24399 Z = ((p->iJD + 129600000)/86400000) % 7;
24400 if( Z>n ) Z -= 7;
24401 p->iJD += (n - Z)*86400000;
24415 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
24418 p->validHMS = 1;
24419 p->h = p->m = 0;
24420 p->s = 0.0;
24421 p->rawS = 0;
24422 p->validTZ = 0;
24423 p->validJD = 0;
24425 p->D = 1;
24428 p->M = 1;
24429 p->D = 1;
24437 case '-':
24456 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
24468 tx.iJD -= 43200000;
24470 tx.iJD -= day*86400000;
24471 if( z[0]=='-' ) tx.iJD = -tx.iJD;
24474 p->iJD += tx.iJD;
24485 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
24488 rRounder = r<0 ? -0.5 : +0.5;
24492 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
24499 p->M += (int)r;
24500 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24501 p->Y += x;
24502 p->M -= x*12;
24503 p->validJD = 0;
24504 r -= (int)r;
24511 p->Y += y;
24512 p->validJD = 0;
24513 r -= (int)r;
24518 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
24534 ** Process time function arguments. argv[0] is a date-time stamp.
24571 if( p->isError || !validJulianDay(p->iJD) ) return 1;
24602 ** the unix epoch of 1970-01-01 00:00:00 GMT.
24612 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
24619 ** Return YYYY-MM-DD HH:MM:SS
24632 if( Y<0 ) Y = -Y;
24637 zBuf[5] = '-';
24640 zBuf[8] = '-';
24655 zBuf[0] = '-';
24695 ** Return YYYY-MM-DD
24708 if( Y<0 ) Y = -Y;
24713 zBuf[5] = '-';
24716 zBuf[8] = '-';
24721 zBuf[0] = '-';
24736 ** %H hour 00-24
24737 ** %j day of year 000-366
24739 ** %m month 01-12
24740 ** %M minute 00-59
24741 ** %s seconds since 1970-01-01
24742 ** %S seconds 00-59
24743 ** %w day of week 0-6 sunday==0
24744 ** %W week of year 00-53
24745 ** %Y year 0000-9999
24762 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
24764 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
24770 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24796 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
24800 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
24819 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
24846 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24895 ** If the library is compiled to omit the full-scale date and time
24901 ** This function uses the C-library functions time(), gmtime()
24903 ** as the user-data for the function.
24922 t = iT/1000 - 10000*(sqlite3_int64)21086676;
24933 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
24946 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
24947 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ),
24948 PURE_DATE(date, -1, 0, 0, dateFunc ),
24949 PURE_DATE(time, -1, 0, 0, timeFunc ),
24950 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
24951 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
24957 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
24958 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
24990 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
25008 ** from within OsOpen()), but some third-party implementations may.
25048 if( pId->pMethods ){
25049 pId->pMethods->xClose(pId);
25050 pId->pMethods = 0;
25055 return id->pMethods->xRead(id, pBuf, amt, offset);
25059 return id->pMethods->xWrite(id, pBuf, amt, offset);
25062 return id->pMethods->xTruncate(id, size);
25066 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
25070 return id->pMethods->xFileSize(id, pSize);
25075 return id->pMethods->xLock(id, lockType);
25079 return id->pMethods->xUnlock(id, lockType);
25083 return id->pMethods->xCheckReservedLock(id, pResOut);
25095 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
25105 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
25113 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
25120 return id->pMethods->xFileControl(id, op, pArg);
25123 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
25127 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
25131 if( NEVER(id->pMethods==0) ) return 0;
25132 return id->pMethods->xDeviceCharacteristics(id);
25136 return id->pMethods->xShmLock(id, offset, n, flags);
25139 id->pMethods->xShmBarrier(id);
25142 return id->pMethods->xShmUnmap(id, deleteFlag);
25152 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
25160 return id->pMethods->xFetch(id, iOff, iAmt, pp);
25163 return id->pMethods->xUnfetch(id, iOff, p);
25166 /* No-op stubs to use when memory-mapped I/O is disabled */
25194 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
25195 assert( rc==SQLITE_OK || pFile->pMethods==0 );
25201 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK;
25210 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
25220 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
25225 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */
25226 return pVfs->xDlOpen(pVfs, zPath);
25229 pVfs->xDlError(pVfs, nByte, zBufOut);
25232 return pVfs->xDlSym(pVfs, pHdle, zSym);
25235 pVfs->xDlClose(pVfs, pHandle);
25245 return pVfs->xRandomness(pVfs, nByte, zBufOut);
25250 return pVfs->xSleep(pVfs, nMicro);
25253 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
25257 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
25263 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
25264 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
25267 rc = pVfs->xCurrentTime(pVfs, &r);
25282 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
25340 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
25342 if( strcmp(zVfs, pVfs->zName)==0 ) break;
25354 /* No-op */
25356 vfsList = pVfs->pNext;
25359 while( p->pNext && p->pNext!=pVfs ){
25360 p = p->pNext;
25362 if( p->pNext==pVfs ){
25363 p->pNext = pVfs->pNext;
25387 pVfs->pNext = vfsList;
25390 pVfs->pNext = vfsList->pNext;
25391 vfsList->pNext = pVfs;
25433 ** Most malloc failures are non-benign. After they occur, SQLite
25457 ** we have to locate the state vector at run-time. In the more common
25487 ** indicates that subsequent malloc failures are non-benign.
25518 ** This file contains a no-op memory allocation drivers for use when
25528 ** used when no other memory allocator is specified using compile-time
25534 ** No-op versions of all memory allocation routines
25547 ** Populate the low-level memory allocation function pointers in
25580 ** This file contains low-level memory allocation drivers for when
25581 ** SQLite will use the standard C-library malloc/realloc/free interface
25584 ** This file contains implementations of the low-level memory allocation
25592 ** C-preprocessor macro summary:
25599 ** a different name, using a separate -D
25615 ** used when no other memory allocator is specified using compile-time
25635 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
25640 ** Use standard C library malloc and free on non-Apple systems.
25657 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
25691 ** For this low-level routine, we are guaranteed that nByte>0 because
25725 ** For this low-level routine, we already know that pPrior!=0 since
25727 ** by higher-level routines.
25735 p--;
25752 p--;
25761 ** For this low-level interface, we know that pPrior!=0. Cases where
25762 ** pPrior==0 while have been intercepted by higher-level routine and
25764 ** cases where nByte<=0 will have been intercepted by higher-level
25780 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
25781 p--;
25841 ** Populate the low-level memory allocation function pointers in
25874 ** This file contains low-level memory allocation drivers for when
25875 ** SQLite will use the standard C-library malloc/realloc/free interface
25880 ** This file contains implementations of the low-level memory allocation
25906 ** ------------------------------------------------------------------------
25908 ** ------------------------------------------------------------------------
25992 if( i>NCSIZE-1 ){
25993 i = NCSIZE - 1;
26002 mem.nCurrent[i]--;
26020 p--;
26021 assert( p->iForeGuard==(int)FOREGUARD );
26022 nReserve = ROUND8(p->iSize);
26030 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
26043 return (int)pHdr->iSize;
26076 ** Fill a buffer with pseudo-random bytes. This is used to preset
26085 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
26090 nByte -= 4;
26092 while( nByte-- > 0 ){
26093 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
26121 pHdr->pNext = 0;
26122 pHdr->pPrev = mem.pLast;
26124 mem.pLast->pNext = pHdr;
26129 pHdr->iForeGuard = FOREGUARD;
26130 pHdr->eType = MEMTYPE_HEAP;
26131 pHdr->nBacktraceSlots = mem.nBacktrace;
26132 pHdr->nTitle = mem.nTitle;
26135 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
26136 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
26139 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
26142 pHdr->nBacktrace = 0;
26147 pHdr->iSize = nByte;
26152 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
26170 pBt -= pHdr->nBacktraceSlots;
26172 if( pHdr->pPrev ){
26173 assert( pHdr->pPrev->pNext==pHdr );
26174 pHdr->pPrev->pNext = pHdr->pNext;
26177 mem.pFirst = pHdr->pNext;
26179 if( pHdr->pNext ){
26180 assert( pHdr->pNext->pPrev==pHdr );
26181 pHdr->pNext->pPrev = pHdr->pPrev;
26184 mem.pLast = pHdr->pPrev;
26187 z -= pHdr->nTitle;
26188 adjustStats((int)pHdr->iSize, -1);
26189 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
26190 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
26208 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
26212 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
26213 if( nByte>pOldHdr->iSize ){
26214 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
26222 ** Populate the low-level memory allocation function pointers in
26246 assert( pHdr->iForeGuard==FOREGUARD );
26247 pHdr->eType = eType;
26265 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26266 if( (pHdr->eType&eType)==0 ){
26287 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26288 if( (pHdr->eType&eType)!=0 ){
26317 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
26326 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26328 pBt -= pHdr->nBacktraceSlots;
26329 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
26348 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26350 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
26352 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
26353 if( pHdr->nBacktrace ){
26356 pBt -= pHdr->nBacktraceSlots;
26357 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
26362 for(i=0; i<NCSIZE-1; i++){
26368 if( mem.nAlloc[NCSIZE-1] ){
26370 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
26371 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
26423 ** mean that the library will use a memory-pool by default, just that
26464 ** two fields form a double-linked list of chunks of related sizes.
26501 ** True if we are evaluating an out-of-memory callback.
26529 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26562 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26564 size = mem3.aPool[i-1].u.hdr.size4x/4;
26565 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26568 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
26597 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26598 size = mem3.aPool[i-1].u.hdr.size4x/4;
26599 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26602 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
26641 ** size parameters for check-out and return a pointer to the
26648 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
26649 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
26650 x = mem3.aPool[i-1].u.hdr.size4x;
26651 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
26652 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
26653 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
26665 if( nBlock>=mem3.szKeyBlk-1 ){
26675 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
26677 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
26678 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
26679 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
26680 mem3.szKeyBlk -= nBlock;
26681 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
26682 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26683 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26713 size = mem3.aPool[i-1].u.hdr.size4x;
26717 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
26718 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
26723 size = i + size/4 - prev;
26724 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
26725 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
26726 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
26766 i = mem3.aiSmall[nBlock-2];
26768 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
26774 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
26807 for(i=0; i<MX_SMALL-1; i++){
26834 i = p - mem3.aPool;
26835 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
26836 size = mem3.aPool[i-1].u.hdr.size4x/4;
26838 mem3.aPool[i-1].u.hdr.size4x &= ~1;
26839 mem3.aPool[i+size-1].u.hdr.prevSize = size;
26840 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
26845 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
26846 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
26847 mem3.iKeyBlk -= size;
26850 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26851 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26852 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26854 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26855 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
26857 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
26858 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26859 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26866 ** size returned omits the 8-byte header overhead. This only
26873 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
26874 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
26884 return ((n+11)&~7) - 4;
26924 if( nBytes<=nOld && nBytes>=nOld-128 ){
26953 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
26999 size = mem3.aPool[i-1].u.hdr.size4x;
27005 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
27010 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
27016 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
27018 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
27022 for(i=0; i<MX_SMALL-1; i++){
27027 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
27036 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
27041 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
27042 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
27058 ** Populate the low-level memory allocation function pointers in
27119 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
27128 ** N >= M*(1 + log2(n)/2) - n + 1
27157 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
27192 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
27223 ** structures, return a pointer to the idx-th such link.
27237 next = MEM5LINK(i)->next;
27238 prev = MEM5LINK(i)->prev;
27242 MEM5LINK(prev)->next = next;
27245 MEM5LINK(next)->prev = prev;
27260 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
27261 MEM5LINK(i)->prev = -1;
27264 MEM5LINK(x)->prev = i;
27286 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
27341 iBin--;
27352 mem5.totalExcess += iFullSz - nByte;
27379 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
27381 /* Check that the pointer pOld points to a valid, non-free block. */
27383 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
27388 assert( iBlock+size-1<(u32)mem5.nBlock );
27391 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
27396 mem5.currentCount--;
27397 mem5.currentOut -= size*mem5.szAtom;
27406 iBuddy = iBlock - size;
27468 ** memsys5Round(). Hence nBytes is always a non-negative power
27477 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
27500 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
27522 ** Examples: memsys5Log(1) -> 0
27523 ** memsys5Log(2) -> 1
27524 ** memsys5Log(4) -> 2
27525 ** memsys5Log(5) -> 3
27526 ** memsys5Log(8) -> 3
27527 ** memsys5Log(9) -> 4
27531 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
27556 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
27574 mem5.aiFreelist[ii] = -1;
27578 for(ii=LOGMAX; ii>=0; ii--){
27628 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
27719 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
27730 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
27740 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
27743 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
27774 p->iType = iType;
27777 if( iType-2>=ArraySize(staticMutexes) ){
27782 p = &staticMutexes[iType-2];
27785 if( p->mutex==0 ){
27786 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
27787 if( p->mutex==0 ){
27807 if( ((CheckMutex*)p)->iType<2 )
27811 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
27826 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
27827 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
27831 "illegal multi-threaded access to database connection"
27834 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
27842 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
27850 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
27880 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
27881 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
27909 pTo->xMutexInit = pFrom->xMutexInit;
27910 pTo->xMutexEnd = pFrom->xMutexEnd;
27911 pTo->xMutexFree = pFrom->xMutexFree;
27912 pTo->xMutexEnter = pFrom->xMutexEnter;
27913 pTo->xMutexTry = pFrom->xMutexTry;
27914 pTo->xMutexLeave = pFrom->xMutexLeave;
27915 pTo->xMutexHeld = pFrom->xMutexHeld;
27916 pTo->xMutexNotheld = pFrom->xMutexNotheld;
27918 pTo->xMutexAlloc = pFrom->xMutexAlloc;
28007 ** this function is a no-op.
28051 ** here are place-holders. Applications can substitute working
28052 ** mutex routines at start-time using the
28125 return p==0 || p->cnt>0;
28129 return p==0 || p->cnt==0;
28144 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
28151 pNew->id = id;
28152 pNew->cnt = 0;
28158 if( id-2<0 || id-2>=ArraySize(aStatic) ){
28163 pNew = &aStatic[id-2];
28164 pNew->id = id;
28176 assert( p->cnt==0 );
28177 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
28199 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28200 p->cnt++;
28204 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28205 p->cnt++;
28218 p->cnt--;
28219 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28241 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
28242 ** is used regardless of the run-time threadsafety setting.
28282 ** home-grown mutexes. Encapsulate these conditions into a single #define.
28325 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
28331 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
28334 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
28387 ** cases where it really needs one. If a faster non-recursive mutex
28428 pthread_mutex_init(&p->mutex, 0);
28434 pthread_mutex_init(&p->mutex, &recursiveAttr);
28438 p->id = SQLITE_MUTEX_RECURSIVE;
28446 pthread_mutex_init(&p->mutex, 0);
28448 p->id = SQLITE_MUTEX_FAST;
28455 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
28460 p = &staticMutexes[iType-2];
28465 assert( p==0 || p->id==iType );
28477 assert( p->nRef==0 );
28479 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
28482 pthread_mutex_destroy(&p->mutex);
28504 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28509 ** is atomic - that it cannot be deceived into thinking self
28510 ** and p->owner are equal if p->owner changes between two values
28512 ** This implementation also assumes a coherent cache - that
28519 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28520 p->nRef++;
28522 pthread_mutex_lock(&p->mutex);
28523 assert( p->nRef==0 );
28524 p->owner = self;
28525 p->nRef = 1;
28529 /* Use the built-in recursive mutexes if they are available.
28531 pthread_mutex_lock(&p->mutex);
28533 assert( p->nRef>0 || p->owner==0 );
28534 p->owner = pthread_self();
28535 p->nRef++;
28540 if( p->trace ){
28541 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28547 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28552 ** is atomic - that it cannot be deceived into thinking self
28553 ** and p->owner are equal if p->owner changes between two values
28555 ** This implementation also assumes a coherent cache - that
28562 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28563 p->nRef++;
28565 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
28566 assert( p->nRef==0 );
28567 p->owner = self;
28568 p->nRef = 1;
28575 /* Use the built-in recursive mutexes if they are available.
28577 if( pthread_mutex_trylock(&p->mutex)==0 ){
28579 p->owner = pthread_self();
28580 p->nRef++;
28589 if( rc==SQLITE_OK && p->trace ){
28590 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28605 p->nRef--;
28606 if( p->nRef==0 ) p->owner = 0;
28608 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
28611 if( p->nRef==0 ){
28612 pthread_mutex_unlock(&p->mutex);
28615 pthread_mutex_unlock(&p->mutex);
28619 if( p->trace ){
28620 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28725 ** Determine if we are dealing with Windows CE - which has a much reduced
28753 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
28808 return p->nRef!=0 && p->owner==GetCurrentThreadId();
28812 return p->nRef==0 || p->owner!=tid;
28857 static int winMutex_isNt = -1; /* <0 means "need to query" */
28935 ** cases where it really needs one. If a faster non-recursive mutex
28961 p->id = iType;
28964 p->trace = 1;
28968 InitializeCriticalSectionEx(&p->mutex, 0, 0);
28970 InitializeCriticalSection(&p->mutex);
28977 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
28982 p = &winMutex_staticMutexes[iType-2];
28985 InterlockedCompareExchange(&p->trace, 1, 0);
28991 assert( p==0 || p->id==iType );
29003 assert( p->nRef==0 && p->owner==0 );
29004 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
29005 DeleteCriticalSection(&p->mutex);
29031 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
29036 EnterCriticalSection(&p->mutex);
29038 assert( p->nRef>0 || p->owner==0 );
29039 p->owner = tid;
29040 p->nRef++;
29041 if( p->trace ){
29042 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
29043 tid, p->id, p, p->trace, p->nRef));
29054 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
29068 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
29073 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
29075 p->owner = tid;
29076 p->nRef++;
29084 if( p->trace ){
29085 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
29086 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
29104 assert( p->nRef>0 );
29105 assert( p->owner==tid );
29106 p->nRef--;
29107 if( p->nRef==0 ) p->owner = 0;
29108 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
29111 LeaveCriticalSection(&p->mutex);
29113 if( p->trace ){
29114 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
29115 tid, p->id, p, p->trace, p->nRef));
29162 ** Attempt to release up to n bytes of non-essential memory currently
29163 ** held by SQLite. An example of non-essential memory is memory used to
29170 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
29171 ** is a no-op returning zero if SQLite is not compiled with
29213 ** no-op.
29228 ** Set the soft heap-size limit for the library. An argument of
29229 ** zero disables the limit. A negative argument is a no-op used to
29244 if( rc ) return -1;
29259 excess = sqlite3_memory_used() - n;
29269 ** Set the hard heap-size limit for the library. An argument of zero
29270 ** disables the hard heap limit. A negative argument is a no-op used
29284 if( rc ) return -1;
29319 ** Return true if the heap is currently under memory pressure - in other
29377 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
29387 if( nUsed >= mem0.alarmThreshold - nFull ){
29392 if( nUsed >= mem0.hardLimit - nFull ){
29423 ** This provides a 256-byte safety margin for defense against 32-bit
29452 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
29479 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
29495 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
29497 return db->lookaside.szTrue;
29512 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
29514 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29515 assert( sqlite3_mutex_held(db->mutex) );
29519 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29520 assert( sqlite3_mutex_held(db->mutex) );
29521 return db->lookaside.szTrue;
29537 if( p==0 ) return; /* IMP: R-49053-54554 */
29553 ** *db->pnBytesFreed.
29556 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
29561 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29562 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29565 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29568 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29570 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29572 assert( db->pnBytesFreed==0 );
29576 pBuf->pNext = db->lookaside.pSmallFree;
29577 db->lookaside.pSmallFree = pBuf;
29581 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29583 assert( db->pnBytesFreed==0 );
29585 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29587 pBuf->pNext = db->lookaside.pFree;
29588 db->lookaside.pFree = pBuf;
29592 if( db->pnBytesFreed ){
29605 assert( sqlite3_mutex_held(db->mutex) );
29607 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29609 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29611 assert( db->pnBytesFreed==0 );
29615 pBuf->pNext = db->lookaside.pSmallFree;
29616 db->lookaside.pSmallFree = pBuf;
29620 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29622 assert( db->pnBytesFreed==0 );
29624 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29626 pBuf->pNext = db->lookaside.pFree;
29627 db->lookaside.pFree = pBuf;
29631 if( db->pnBytesFreed ){
29641 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29654 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
29657 sqlite3_free(pOld); /* IMP: R-26507-47431 */
29665 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
29675 nDiff = nNew - nOld;
29677 mem0.alarmThreshold-nDiff ){
29679 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
29693 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
29699 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
29711 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
29755 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
29764 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
29792 assert( sqlite3_mutex_held(db->mutex) );
29793 assert( db->pnBytesFreed==0 );
29794 if( n>db->lookaside.sz ){
29795 if( !db->lookaside.bDisable ){
29796 db->lookaside.anStat[1]++;
29797 }else if( db->mallocFailed ){
29804 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
29805 db->lookaside.pSmallFree = pBuf->pNext;
29806 db->lookaside.anStat[0]++;
29808 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
29809 db->lookaside.pSmallInit = pBuf->pNext;
29810 db->lookaside.anStat[0]++;
29815 if( (pBuf = db->lookaside.pFree)!=0 ){
29816 db->lookaside.pFree = pBuf->pNext;
29817 db->lookaside.anStat[0]++;
29819 }else if( (pBuf = db->lookaside.pInit)!=0 ){
29820 db->lookaside.pInit = pBuf->pNext;
29821 db->lookaside.anStat[0]++;
29824 db->lookaside.anStat[2]++;
29828 assert( sqlite3_mutex_held(db->mutex) );
29829 assert( db->pnBytesFreed==0 );
29830 if( db->mallocFailed ){
29847 assert( sqlite3_mutex_held(db->mutex) );
29848 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
29850 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
29854 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
29855 if( n<=db->lookaside.szTrue ) return p;
29864 if( db->mallocFailed==0 ){
29880 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
29940 n = (int)(zEnd - zStart);
29941 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
29955 ** Call this routine to record the fact that an OOM (out-of-memory) error
29956 ** has happened. This routine will set db->mallocFailed, and also
29968 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
29969 db->mallocFailed = 1;
29970 if( db->nVdbeExec>0 ){
29971 AtomicStore(&db->u1.isInterrupted, 1);
29974 if( db->pParse ){
29976 sqlite3ErrorMsg(db->pParse, "out of memory");
29977 db->pParse->rc = SQLITE_NOMEM_BKPT;
29978 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
29979 pParse->nErr++;
29980 pParse->rc = SQLITE_NOMEM;
29989 ** db->mallocFailed flag as necessary.
29995 if( db->mallocFailed && db->nVdbeExec==0 ){
29996 db->mallocFailed = 0;
29997 AtomicStore(&db->u1.isInterrupted, 0);
29998 assert( db->lookaside.bDisable>0 );
30007 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
30012 return rc & db->errMask;
30024 ** If an OOM as occurred, then the connection error-code (the value
30029 ** Otherwise the read (and possible write) of db->mallocFailed
30033 assert( sqlite3_mutex_held(db->mutex) );
30034 if( db->mallocFailed || rc ){
30037 return rc & db->errMask;
30048 ** This file contains code for a set of "printf"-like routines. These
30059 #define etRADIX 0 /* non-decimal integer types. %x %o */
30075 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
30076 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
30083 ** An "etByte" is an 8-bit unsigned value.
30112 static const char aPrefix[] = "-x0\000X0";
30151 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
30152 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
30170 ** 16 (the number of significant digits in a 64-bit float) '0' is
30177 (*cnt)--;
30181 *val = (*val - d)*10.0;
30191 p->accError = eError;
30192 if( p->mxAlloc ) sqlite3_str_reset(p);
30193 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
30200 if( p->nArg<=p->nUsed ) return 0;
30201 return sqlite3_value_int64(p->apArg[p->nUsed++]);
30204 if( p->nArg<=p->nUsed ) return 0.0;
30205 return sqlite3_value_double(p->apArg[p->nUsed++]);
30208 if( p->nArg<=p->nUsed ) return 0;
30209 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
30223 if( pAccum->accError ) return 0;
30224 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
30228 z = sqlite3DbMallocRaw(pAccum->db, n);
30245 ** Hard limit on the precision of floating-point conversions.
30265 etByte flag_leftjustify; /* True if "-" flag is present */
30275 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
30295 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30298 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
30312 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
30325 precision = -1;
30328 case '-': flag_leftjustify = 1; break;
30348 unsigned wx = c - '0';
30350 wx = wx*10 + c - '0';
30362 fmt--;
30374 width = width >= -2147483647 ? -width : 0;
30396 precision = precision >= -2147483647 ? -precision : -1;
30402 px = px*10 + c - '0';
30414 --fmt;
30429 xtype = infop->type;
30440 ** flag_leftjustify TRUE if a '-' is present or if the
30445 ** always non-negative. Zero is the default.
30447 ** is -1.
30452 assert( precision>=(-1) );
30463 if( infop->flags & FLAG_SIGNED ){
30478 testcase( v==(-1) );
30481 prefix = '-';
30501 if( flag_zeropad && precision<width-(prefix!=0) ){
30502 precision = width-(prefix!=0);
30504 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
30515 bufpt = &zOut[nOut-1];
30522 *(--bufpt) = zOrd[x*2+1];
30523 *(--bufpt) = zOrd[x*2];
30526 const char *cset = &aDigits[infop->charset];
30527 u8 base = infop->base;
30529 *(--bufpt) = cset[longvalue%base];
30533 length = (int)(&zOut[nOut-1]-bufpt);
30535 *(--bufpt) = '0'; /* Zero pad */
30539 int nn = (length - 1)/3; /* Number of "," to insert */
30540 int ix = (length - 1)%3 + 1;
30541 bufpt -= nn;
30544 ix--;
30547 nn--;
30552 if( prefix ) *(--bufpt) = prefix; /* Add sign */
30553 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
30556 pre = &aPrefix[infop->prefix];
30557 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
30559 length = (int)(&zOut[nOut-1]-bufpt);
30579 realvalue = -realvalue;
30580 prefix = '-';
30584 if( xtype==etGENERIC && precision>0 ) precision--;
30588 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
30594 ex = -1023 + (int)((u>>52)&0x7ff);
30595 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
30611 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
30612 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
30632 if( exp<-4 || exp>precision ){
30635 precision = precision - exp;
30665 for(; e2>=0; e2--){
30675 for(e2++; e2<0; precision--, e2++){
30680 while( (precision--)>0 ){
30685 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
30687 if( bufpt[-1]=='.' ){
30691 *(--bufpt) = 0;
30697 *(bufpt++) = aDigits[infop->charset];
30699 *(bufpt++) = '-'; exp = -exp;
30715 length = (int)(bufpt-zOut);
30722 int nPad = width - length;
30723 for(i=width; i>=nPad; i--){
30724 bufpt[i] = bufpt[i-nPad];
30727 while( nPad-- ) bufpt[i++] = '0';
30734 *(va_arg(ap,int*)) = pAccum->nChar;
30780 width -= precision-1;
30782 sqlite3_str_appendchar(pAccum, width-1, ' ');
30785 while( precision-- > 1 ){
30803 if( pAccum->nChar==0
30804 && pAccum->mxAlloc
30807 && pAccum->accError==0
30812 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30813 pAccum->zText = bufpt;
30814 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
30815 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
30816 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
30827 while( precision-- > 0 && z[0] ){
30830 length = (int)(z - (unsigned char*)bufpt);
30839 /* Adjust width to account for extra bytes in UTF-8 characters */
30840 int ii = length - 1;
30841 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
30866 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
30893 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30898 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
30899 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
30905 if( pToken && pToken->n ){
30906 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
30907 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
30915 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30918 if( pItem->zAlias && !flag_altform2 ){
30919 sqlite3_str_appendall(pAccum, pItem->zAlias);
30920 }else if( pItem->zName ){
30921 if( pItem->zDatabase ){
30922 sqlite3_str_appendall(pAccum, pItem->zDatabase);
30925 sqlite3_str_appendall(pAccum, pItem->zName);
30926 }else if( pItem->zAlias ){
30927 sqlite3_str_appendall(pAccum, pItem->zAlias);
30929 Select *pSel = pItem->pSelect;
30931 if( pSel->selFlags & SF_NestedFrom ){
30932 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
30934 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
30953 width -= length;
30963 sqlite3DbFree(pAccum->db, zExtra);
30982 if( db->errByteOffset!=(-2) ) return;
30983 pParse = db->pParse;
30985 zText =pParse->zTail;
30989 db->errByteOffset = (int)(z-zText);
30999 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
31001 pExpr = pExpr->pLeft;
31004 db->errByteOffset = pExpr->w.iOfst;
31016 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
31017 if( p->accError ){
31018 testcase(p->accError==SQLITE_TOOBIG);
31019 testcase(p->accError==SQLITE_NOMEM);
31022 if( p->mxAlloc==0 ){
31024 return p->nAlloc - p->nChar - 1;
31026 char *zOld = isMalloced(p) ? p->zText : 0;
31027 i64 szNew = p->nChar;
31029 if( szNew+p->nChar<=p->mxAlloc ){
31032 szNew += p->nChar;
31034 if( szNew > p->mxAlloc ){
31039 p->nAlloc = (int)szNew;
31041 if( p->db ){
31042 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
31044 zNew = sqlite3Realloc(zOld, p->nAlloc);
31047 assert( p->zText!=0 || p->nChar==0 );
31048 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
31049 p->zText = zNew;
31050 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
31051 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
31065 testcase( p->nChar + (i64)N > 0x7fffffff );
31066 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
31069 while( (N--)>0 ) p->zText[p->nChar++] = c;
31076 ** This is a helper routine to sqlite3_str_append() that does special-case
31083 memcpy(&p->zText[p->nChar], z, N);
31084 p->nChar += N;
31094 assert( p->zText!=0 || p->nChar==0 || p->accError );
31096 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
31097 if( p->nChar+N >= p->nAlloc ){
31100 assert( p->zText );
31101 p->nChar += N;
31102 memcpy(&p->zText[p->nChar-N], z, N);
31107 ** Append the complete text of zero-terminated string z[] to the p string.
31115 ** Finish off a string by making sure it is zero-terminated.
31121 assert( p->mxAlloc>0 && !isMalloced(p) );
31122 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
31124 memcpy(zText, p->zText, p->nChar+1);
31125 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
31129 p->zText = zText;
31133 if( p->zText ){
31134 p->zText[p->nChar] = 0;
31135 if( p->mxAlloc>0 && !isMalloced(p) ){
31139 return p->zText;
31147 if( p->accError ){
31148 sqlite3_result_error_code(pCtx, p->accError);
31151 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
31183 return p ? p->accError : SQLITE_NOMEM;
31188 return p ? p->nChar : 0;
31193 if( p==0 || p->nChar==0 ) return 0;
31194 p->zText[p->nChar] = 0;
31195 return p->zText;
31203 sqlite3DbFree(p->db, p->zText);
31204 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
31206 p->nAlloc = 0;
31207 p->nChar = 0;
31208 p->zText = 0;
31216 ** memory is used if not NULL. db->mallocFailed is set appropriately
31226 p->zText = zBase;
31227 p->db = db;
31228 p->nAlloc = n;
31229 p->mxAlloc = mx;
31230 p->nChar = 0;
31231 p->accError = 0;
31232 p->printfFlags = 0;
31240 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
31249 ** %-conversion extensions.
31257 db->aLimit[SQLITE_LIMIT_LENGTH]);
31269 ** %-conversion extensions.
31282 ** %-conversion extensions.
31306 ** %-conversion extensions.
31360 ** stack space on small-stack systems when logging is disabled.
31422 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31435 ** 2015-06-08
31467 p->iLevel++;
31470 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
31479 p->iLevel--;
31480 if( p->iLevel<0 ){
31497 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
31498 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
31500 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
31536 int colMoreToFollow = i<(nCol - 1);
31551 printf(" X-%s", z);
31559 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND");
31570 ** Generate a human-readable description of a WITH clause.
31575 if( pWith->nCte==0 ) return;
31576 if( pWith->pOuter ){
31577 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
31581 if( pWith->nCte>0 ){
31583 for(i=0; i<pWith->nCte; i++){
31586 const struct Cte *pCte = &pWith->a[i];
31588 sqlite3_str_appendf(&x, "%s", pCte->zName);
31589 if( pCte->pCols && pCte->pCols->nExpr>0 ){
31592 for(j=0; j<pCte->pCols->nExpr; j++){
31593 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
31598 if( pCte->eM10d!=M10d_Any ){
31600 pCte->eM10d==M10d_No ? "NOT " : "");
31602 if( pCte->pUse ){
31603 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
31604 pCte->pUse->nUse);
31607 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
31608 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
31616 ** Generate a human-readable description of a SrcList object.
31621 for(i=0; i<pSrc->nSrc; i++){
31622 const SrcItem *pItem = &pSrc->a[i];
31628 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
31629 if( pItem->pTab ){
31631 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
31633 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
31634 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
31635 }else if( pItem->fg.jointype & JT_LEFT ){
31636 sqlite3_str_appendf(&x, " LEFT-JOIN");
31637 }else if( pItem->fg.jointype & JT_RIGHT ){
31638 sqlite3_str_appendf(&x, " RIGHT-JOIN");
31639 }else if( pItem->fg.jointype & JT_CROSS ){
31640 sqlite3_str_appendf(&x, " CROSS-JOIN");
31642 if( pItem->fg.jointype & JT_LTORJ ){
31645 if( pItem->fg.fromDDL ){
31648 if( pItem->fg.isCte ){
31649 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
31651 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
31655 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
31657 if( pItem->pSelect ) n++;
31658 if( pItem->fg.isTabFunc ) n++;
31659 if( pItem->fg.isUsing ) n++;
31660 if( pItem->fg.isUsing ){
31661 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
31663 if( pItem->pSelect ){
31664 if( pItem->pTab ){
31665 Table *pTab = pItem->pTab;
31666 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
31668 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
31669 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
31671 if( pItem->fg.isTabFunc ){
31672 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
31679 ** Generate a human-readable description of a Select object.
31685 sqlite3TreeViewLine(pView, "nil-SELECT");
31689 if( p->pWith ){
31690 sqlite3TreeViewWith(pView, p->pWith, 1);
31695 if( p->selFlags & SF_WhereBegin ){
31700 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
31701 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
31702 p->selId, p, p->selFlags,
31703 (int)p->nSelectRow
31707 if( p->pPrior ){
31711 if( p->pSrc && p->pSrc->nSrc ) n++;
31712 if( p->pWhere ) n++;
31713 if( p->pGroupBy ) n++;
31714 if( p->pHaving ) n++;
31715 if( p->pOrderBy ) n++;
31716 if( p->pLimit ) n++;
31718 if( p->pWin ) n++;
31719 if( p->pWinDefn ) n++;
31722 if( p->pEList ){
31723 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
31725 n--;
31727 if( p->pWin ){
31729 sqlite3TreeViewPush(&pView, (n--)>0);
31730 sqlite3TreeViewLine(pView, "window-functions");
31731 for(pX=p->pWin; pX; pX=pX->pNextWin){
31732 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
31737 if( p->pSrc && p->pSrc->nSrc ){
31738 sqlite3TreeViewPush(&pView, (n--)>0);
31740 sqlite3TreeViewSrcList(pView, p->pSrc);
31743 if( p->pWhere ){
31744 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
31745 sqlite3TreeViewExpr(pView, p->pWhere, 0);
31748 if( p->pGroupBy ){
31749 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
31751 if( p->pHaving ){
31752 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
31753 sqlite3TreeViewExpr(pView, p->pHaving, 0);
31757 if( p->pWinDefn ){
31759 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
31760 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
31761 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
31766 if( p->pOrderBy ){
31767 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
31769 if( p->pLimit ){
31770 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
31771 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
31772 if( p->pLimit->pRight ){
31773 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
31774 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
31779 if( p->pPrior ){
31781 switch( p->op ){
31788 p = p->pPrior;
31832 ** Generate a human-readable explanation for a Window object
31837 if( pWin->pFilter ){
31839 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
31843 if( pWin->zName ){
31844 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
31848 if( pWin->zBase ) nElement++;
31849 if( pWin->pOrderBy ) nElement++;
31850 if( pWin->eFrmType ) nElement++;
31851 if( pWin->eExclude ) nElement++;
31852 if( pWin->zBase ){
31853 sqlite3TreeViewPush(&pView, (--nElement)>0);
31854 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
31857 if( pWin->pPartition ){
31858 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
31860 if( pWin->pOrderBy ){
31861 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
31863 if( pWin->eFrmType ){
31866 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
31867 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
31869 pWin->bImplicitFrame ? " (implied)" : "");
31870 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
31871 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
31872 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
31875 if( pWin->eExclude ){
31878 switch( pWin->eExclude ){
31884 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
31898 ** Generate a human-readable explanation for a Window Function object
31904 pWin->pWFunc->zName, pWin->pWFunc->nArg);
31911 ** Generate a human-readable explanation of an expression tree.
31923 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31927 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31929 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
31932 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
31944 switch( pExpr->op ){
31947 pExpr->iTable, pExpr->iColumn, zFlgs);
31951 if( pExpr->iTable<0 ){
31954 if( pExpr->op2 ){
31955 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
31960 pExpr->iColumn, zFlgs, zOp2);
31964 pExpr->iTable, pExpr->iColumn,
31965 pExpr->y.pTab, zFlgs);
31968 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31973 if( pExpr->flags & EP_IntValue ){
31974 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
31976 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
31983 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31989 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
32004 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
32011 pExpr->u.zToken, pExpr->iColumn);
32015 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
32020 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
32027 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
32028 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32065 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
32067 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
32068 assert( pExpr->pRight );
32069 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
32070 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
32077 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
32078 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32085 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
32090 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
32091 pExpr->u.zToken, zFlgs);
32092 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32105 pFarg = pExpr->x.pList;
32107 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
32113 if( pExpr->op==TK_AGG_FUNCTION ){
32115 pExpr->op2, pExpr->u.zToken, zFlgs,
32116 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
32117 pExpr->iAgg, pExpr->pAggInfo);
32118 }else if( pExpr->op2!=0 ){
32121 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
32123 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
32124 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
32125 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
32126 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
32128 pExpr->u.zToken, zFlgs, zOp2);
32130 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
32145 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
32146 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32151 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
32152 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32158 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
32159 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
32162 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
32167 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32169 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32171 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32184 ** X is stored in pExpr->pLeft.
32185 ** Y is stored in pExpr->pList->a[0].pExpr.
32186 ** Z is stored in pExpr->pList->a[1].pExpr.
32190 pX = pExpr->pLeft;
32192 assert( pExpr->x.pList->nExpr==2 );
32193 pY = pExpr->x.pList->a[0].pExpr;
32194 pZ = pExpr->x.pList->a[1].pExpr;
32203 ** to a column in the new.* or old.* pseudo-tables available to
32205 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
32206 ** is set to the column of the pseudo-table to read, or to -1 to
32210 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
32215 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32217 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32223 switch( pExpr->affExpr ){
32230 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
32236 pExpr->iTable, pExpr->iColumn, zFlgs);
32237 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32243 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
32248 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
32249 pExpr->iColumn, pExpr->iTable-1,
32250 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
32251 assert( ExprUseXSelect(pExpr->pLeft) );
32252 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
32256 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
32257 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32264 tmp.op = pExpr->op2;
32269 if( pExpr->iColumn<=0 ){
32273 pExpr->iColumn-1);
32278 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
32284 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32285 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32288 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32295 ** Generate a human-readable explanation of an expression list.
32308 for(i=0; i<pList->nExpr; i++){
32309 int j = pList->a[i].u.x.iOrderByCol;
32310 char *zName = pList->a[i].zEName;
32311 int moreToFollow = i<pList->nExpr - 1;
32317 switch( pList->a[i].fg.eEName ){
32322 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
32323 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
32324 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
32325 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
32338 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
32357 ** Generate a human-readable explanation of an id-list.
32370 for(i=0; i<pList->nId; i++){
32371 char *zName = pList->a[i].zName;
32372 int moreToFollow = i<pList->nId - 1;
32376 if( pList->eU4==EU4_NONE ){
32378 }else if( pList->eU4==EU4_IDX ){
32379 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
32381 assert( pList->eU4==EU4_EXPR );
32382 if( pList->a[i].u4.pExpr==0 ){
32386 sqlite3TreeViewPush(&pView, i<pList->nId-1);
32387 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
32407 ** Generate a human-readable explanation of a list of Upsert objects
32418 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
32420 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
32421 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
32422 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
32423 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
32424 if( pUpsert->pUpsertWhere ){
32425 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32426 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
32430 pUpsert = pUpsert->pNextUpsert;
32437 ** Generate a human-readable diagram of the data structure that go
32459 sqlite3TreeViewPush(&pView, (--n)>0);
32464 sqlite3TreeViewPush(&pView, (--n)>0);
32470 sqlite3TreeViewPush(&pView, (--n)>0);
32476 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32479 sqlite3TreeViewPush(&pView, (--n)>0);
32485 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32493 ** Generate a human-readable diagram of the data structure that go
32526 sqlite3TreeViewPush(&pView, (--n)>0);
32531 sqlite3TreeViewPush(&pView, (--n)>0);
32537 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
32540 sqlite3TreeViewPush(&pView, (--n)>0);
32541 sqlite3TreeViewLine(pView, "DATA-SOURCE");
32546 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
32549 sqlite3TreeViewPush(&pView, (--n)>0);
32555 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32563 ** Generate a human-readable diagram of the data structure that go
32598 sqlite3TreeViewPush(&pView, (--n)>0);
32603 sqlite3TreeViewPush(&pView, (--n)>0);
32609 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
32612 sqlite3TreeViewPush(&pView, (--n)>0);
32618 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32621 sqlite3TreeViewPush(&pView, (--n)>0);
32627 sqlite3TreeViewPush(&pView, (--n)>0);
32633 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32641 ** Show a human-readable graph of a TriggerStep
32652 moreToFollow || (showFullList && pStep->pNext!=0));
32654 if( cnt++ && pStep->pNext==0 ){
32658 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
32659 }while( showFullList && (pStep = pStep->pNext)!=0 );
32664 ** Show a human-readable graph of a Trigger
32675 moreToFollow || (showFullList && pTrigger->pNext!=0));
32677 if( cnt++ && pTrigger->pNext==0 ){
32681 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
32683 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
32685 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
32692 ** These simplified versions of the tree-view routines omit unnecessary
32738 ** This file contains code to implement a pseudo-random number
32741 ** Random numbers are used by some of the database backends in order
32757 /* The RFC-7539 ChaCha20 block function
32759 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
32788 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32790 ** we have to locate the state vector at run-time. In the more common
32842 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N);
32843 wsdPrng.n -= N;
32848 N -= wsdPrng.n;
32899 ** This file presents a simple cross-platform threading interface for
32912 ** single-threaded if desired.
32924 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
32947 /* This routine is never used in single-threaded mode */
32954 p->xTask = xTask;
32955 p->pIn = pIn;
32963 rc = pthread_create(&p->tid, 0, xTask, pIn);
32966 p->done = 1;
32967 p->pOut = xTask(pIn);
32979 if( p->done ){
32980 *ppOut = p->pOut;
32983 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
32996 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
33022 assert( p->id==GetCurrentThreadId() );
33024 assert( p->xTask!=0 );
33025 p->pResult = p->xTask(p->pIn);
33052 p->xTask = xTask;
33053 p->pIn = pIn;
33054 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
33055 if( p->tid==0 ){
33059 if( p->xTask==0 ){
33060 p->id = GetCurrentThreadId();
33061 p->pResult = xTask(pIn);
33076 if( p->xTask==0 ){
33077 /* assert( p->id==GetCurrentThreadId() ); */
33079 assert( p->tid==0 );
33081 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
33082 rc = sqlite3Win32Wait((HANDLE)p->tid);
33084 bRc = CloseHandle((HANDLE)p->tid);
33087 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
33096 /********************************* Single-Threaded **************************/
33125 p->xTask = xTask;
33126 p->pIn = pIn;
33128 p->xTask = 0;
33129 p->pResult = xTask(pIn);
33140 if( p->xTask ){
33141 *ppOut = p->xTask(p->pIn);
33143 *ppOut = p->pResult;
33159 /****************************** End Single-Threaded *************************/
33175 ** This file contains routines used to translate between UTF-8,
33176 ** UTF-16, UTF-16BE, and UTF-16LE.
33178 ** Notes on UTF-8:
33180 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
33187 ** Notes on UTF-16: (with wwww+1==uuuuu)
33189 ** Word-0 Word-1 Value
33195 ** 0xff 0xfe little-endian utf-16 follows
33196 ** 0xfe 0xff big-endian utf-16 follows
33213 ** a multi-byte UTF8 character.
33252 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33253 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33264 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33265 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33272 ** Translate a single UTF-8 character. Return the unicode value.
33279 ** Notes On Invalid UTF-8:
33281 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
33282 ** be encoded as a multi-byte character. Any multi-byte character that
33286 ** If a multi-byte character attempts to encode a value between
33290 ** byte of a character are interpreted as single-byte characters
33294 ** * This routine accepts over-length UTF8 encodings
33295 ** for unicode values 0x80 and greater. It does not change over-length
33301 c = sqlite3Utf8Trans1[c-0xc0]; \
33315 ** For this routine, we assume the UTF8 string is always zero-terminated.
33319 c = sqlite3Utf8Trans1[c-0xc0];
33353 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
33354 assert( pMem->flags&MEM_Str );
33355 assert( pMem->enc!=desiredEnc );
33356 assert( pMem->enc!=0 );
33357 assert( pMem->n>=0 );
33369 /* If the translation is between UTF-16 little and big endian, then
33373 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
33381 zIn = (u8*)pMem->z;
33382 zTerm = &zIn[pMem->n&~1];
33389 pMem->enc = desiredEnc;
33395 /* When converting from UTF-16, the maximum growth results from
33396 ** translating a 2-byte character to a 4-byte UTF-8 character.
33398 ** nul-terminator.
33400 pMem->n &= ~1;
33401 len = 2 * (sqlite3_int64)pMem->n + 1;
33403 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
33404 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
33406 ** nul-terminator.
33408 len = 2 * (sqlite3_int64)pMem->n + 2;
33417 zIn = (u8*)pMem->z;
33418 zTerm = &zIn[pMem->n];
33419 zOut = sqlite3DbMallocRaw(pMem->db, len);
33425 if( pMem->enc==SQLITE_UTF8 ){
33427 /* UTF-8 -> UTF-16 Little-endian */
33434 /* UTF-8 -> UTF-16 Big-endian */
33440 pMem->n = (int)(z - zOut);
33444 if( pMem->enc==SQLITE_UTF16LE ){
33445 /* UTF-16 Little-endian -> UTF-8 */
33457 zIn -= 2;
33474 /* UTF-16 Big-endian -> UTF-8 */
33486 zIn -= 2;
33503 pMem->n = (int)(z - zOut);
33506 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
33508 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
33510 pMem->flags = c;
33511 pMem->enc = desiredEnc;
33512 pMem->z = (char*)zOut;
33513 pMem->zMalloc = pMem->z;
33514 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
33532 ** This routine checks for a byte-order mark at the beginning of the
33533 ** UTF-16 string stored in *pMem. If one is present, it is removed and
33535 ** byte-swapping, it just sets Mem.enc appropriately.
33544 assert( pMem->n>=0 );
33545 if( pMem->n>1 ){
33546 u8 b1 = *(u8 *)pMem->z;
33547 u8 b2 = *(((u8 *)pMem->z) + 1);
33559 pMem->n -= 2;
33560 memmove(pMem->z, &pMem->z[2], pMem->n);
33561 pMem->z[pMem->n] = '\0';
33562 pMem->z[pMem->n+1] = '\0';
33563 pMem->flags |= MEM_Term;
33564 pMem->enc = bom;
33572 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
33585 zTerm = (const u8*)(-1);
33595 /* This test function is not currently used by the automated test-suite.
33600 ** Translate UTF-8 to UTF-8.
33602 ** This has the effect of making sure that the string is well-formed
33603 ** UTF-8. Miscoded characters are removed.
33605 ** The translation is done in-place and aborted if the output
33620 return (int)(zOut - zStart);
33626 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
33627 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
33638 if( db->mallocFailed ){
33642 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
33643 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
33644 assert( m.z || db->mallocFailed );
33649 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
33651 ** in pZ. nChar must be non-negative.
33665 return (int)(z-(unsigned char const *)zIn)
33666 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
33685 n = (int)(z-zBuf);
33694 assert( (z-zBuf)==n );
33731 ** sqlite3FaultSim() function only returns non-zero during testing.
33733 ** During testing, if the test harness has set a fault-sim callback using
33735 ** each call to sqlite3FaultSim() is relayed to that application-supplied
33736 ** callback and the integer return value form the application-supplied
33774 ** lower 30 bits of a 32-bit signed integer.
33789 ** The column type is an extra string stored after the zero-terminator on
33793 if( pCol->colFlags & COLFLAG_HASTYPE ){
33794 return pCol->zCnName + strlen(pCol->zCnName) + 1;
33795 }else if( pCol->eCType ){
33796 assert( pCol->eCType<=SQLITE_N_STDTYPE );
33797 return (char*)sqlite3StdType[pCol->eCType-1];
33804 ** Helper function for sqlite3Error() - called rarely. Broken out into
33809 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33820 db->errCode = err_code;
33821 if( err_code || db->pErr ){
33824 db->errByteOffset = -1;
33834 db->errCode = SQLITE_OK;
33835 db->errByteOffset = -1;
33836 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33847 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
33857 ** assumed to be encoded in UTF-8.
33865 db->errCode = err_code;
33869 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
33875 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
33880 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
33892 sqlite3 *db = pParse->db;
33894 assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
33895 db->errByteOffset = -2;
33899 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
33900 if( db->suppressErr ){
33902 if( db->mallocFailed ){
33903 pParse->nErr++;
33904 pParse->rc = SQLITE_NOMEM;
33907 pParse->nErr++;
33908 sqlite3DbFree(db, pParse->zErrMsg);
33909 pParse->zErrMsg = zMsg;
33910 pParse->rc = SQLITE_ERROR;
33911 pParse->pWith = 0;
33922 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
33923 pParse->rc = errCode;
33924 pParse->nErr++;
33929 ** Convert an SQL-style quoted string into a normal string by removing
33930 ** the quote characters. The conversion is done in-place. If the
33932 ** is a no-op.
33934 ** The input string must be zero-terminated. A new zero-terminator
33937 ** The return value is -1 if no dequoting occurs or the length of the
33941 ** 2002-02-14: This routine is extended to remove MS-Access style
33942 ** brackets from around identifiers. For example: "[a-b-c]" becomes
33943 ** "a-b-c".
33969 assert( sqlite3Isquote(p->u.zToken[0]) );
33970 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
33971 sqlite3Dequote(p->u.zToken);
33978 ** "abc" -> abc
33979 ** "ab""cd" -> (not possible because of the interior "")
33983 ** is always a no-op.
33987 if( p->n<2 ) return;
33988 if( !sqlite3Isquote(p->z[0]) ) return;
33989 for(i=1; i<p->n-1; i++){
33990 if( sqlite3Isquote(p->z[i]) ) return;
33992 p->n -= 2;
33993 p->z++;
34000 p->z = z;
34001 p->n = sqlite3Strlen30(z);
34004 /* Convenient short-hand */
34011 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
34013 ** the contents of two buffers containing UTF-8 strings in a
34014 ** case-independent fashion, using the same definition of "case
34019 return zRight ? -1 : 0;
34036 c = (int)UpperToLower[c] - (int)UpperToLower[x];
34047 return zRight ? -1 : 0;
34053 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
34054 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
34058 ** Compute an 8-bit hash on a string that is insensitive to case differences
34071 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
34114 ** uses the encoding enc. The string is not necessarily zero-terminated.
34122 ** -1 => Not a valid number, but has a valid prefix which
34127 ** [+-]digits[E[+-]digits]
34128 ** [+-]digits.[digits][E[+-]digits]
34129 ** [+-].digits[E[+-]digits]
34151 int eValid = 1; /* True exponent is either not used or is well-formed */
34154 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
34170 for(i=3-enc; i<length && z[i]==0; i+=2){}
34171 if( i<length ) eType = -100;
34181 if( *z=='-' ){
34182 sign = -1;
34190 s = s*10 + (*z - '0');
34192 if( s>=((LARGEST_INT64-9)/10) ){
34193 /* skip non-significant significand digits
34207 if( s<((LARGEST_INT64-9)/10) ){
34208 s = s*10 + (*z - '0');
34209 d--;
34226 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
34229 if( *z=='-' ){
34230 esign = -1;
34237 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
34250 esign = -1;
34251 e *= -1;
34258 result = sign<0 ? -(double)0 : (double)0;
34266 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
34268 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
34271 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
34274 e--;
34278 s = sign<0 ? -s : s;
34280 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
34284 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
34285 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
34286 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
34319 /* return true if number and no extra non-whitespace chracters after */
34323 return -1;
34336 ** Render an signed 64-bit integer as text. Store the result in zOut[].
34345 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
34349 i = sizeof(zTemp)-2;
34350 zTemp[sizeof(zTemp)-1] = 0;
34352 zTemp[i--] = (x%10) + '0';
34355 if( v<0 ) zTemp[i--] = '-';
34356 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
34360 ** Compare the 19-character string zNum against the text representation
34371 ** will return -8.
34379 c = (zNum[i*incr]-pow63[i])*10;
34382 c = zNum[18*incr] - '8';
34383 testcase( c==(-1) );
34391 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
34396 ** -1 Not even a prefix of the input text looks like an integer
34397 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34398 ** 1 Excess non-space text after the integer value
34399 ** 2 Integer too large for a 64-bit signed integer or is malformed
34403 ** The string is not necessarily zero-terminated. The encoding is
34412 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
34423 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
34430 if( *zNum=='-' ){
34440 u = u*10 + c - '0';
34447 ** from clang and -fsanitize=undefined. This test and assignment make
34449 ** them, but we must appaise the undefined-behavior pharisees. */
34452 *pNum = -(i64)u;
34458 rc = -1;
34459 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
34465 rc = 1; /* Extra non-space text after the integer */
34476 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
34490 assert( u-1==LARGEST_INT64 );
34498 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
34499 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
34504 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34506 ** 2 Integer too large for a 64-bit signed integer or is malformed
34521 return (z[k]==0 && k-i<=16) ? 0 : 2;
34530 ** If zNum represents an integer that will fit in 32-bits, then set
34535 ** Any non-numeric characters that following zNum are ignored.
34537 ** input number to be zero-terminated.
34543 if( zNum[0]=='-' ){
34570 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
34577 ** 2^31 -> 2147483648
34583 testcase( v-neg==2147483647 );
34584 if( v-neg>2147483647 ){
34588 v = -v;
34595 ** Return a 32-bit integer value extracted from a string. If the
34605 ** Try to convert z into an unsigned 32-bit integer. Return true on
34614 v = v*10 + z[i] - '0';
34623 ** The variable-length integer encoding is as follows:
34630 ** 7 bits - A
34631 ** 14 bits - BA
34632 ** 21 bits - BBA
34633 ** 28 bits - BBBA
34634 ** 35 bits - BBBBA
34635 ** 42 bits - BBBBBA
34636 ** 49 bits - BBBBBBA
34637 ** 56 bits - BBBBBBBA
34638 ** 64 bits - BBBBBBBBC
34642 ** Write a 64-bit variable-length integer to memory starting at p[0].
34646 ** A variable-length integer consists of the lower 7 bits of each byte
34657 for(i=7; i>=0; i--){
34670 for(i=0, j=n-1; j>=0; j--, i++){
34702 ** Read a 64-bit variable-length integer from memory starting at p[0].
34845 b = p[-4];
34856 ** Read a 32-bit variable-length integer from memory starting at p[0].
34859 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
34863 ** single-byte case. All code should use the MACRO version as
34864 ** this function assumes the single-byte case has already been handled.
34869 /* The 1-byte case. Overwhelmingly the most common. Handled inline
34882 /* The 2-byte case */
34895 /* The 3-byte case */
34910 /* A 32-bit varint is used to store size information in btrees.
34911 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
34912 ** A 3-byte varint is sufficient, for example, to record the size
34913 ** of a 1048569-byte BLOB or string.
34915 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
34916 ** rare larger cases can be handled by the slower 64-bit varint
34924 n = sqlite3GetVarint(p-2, &v64);
34936 ** unrolling for the 3- and 4-byte varint cases. This code is
34969 ** slow) general-purpose sqlite3GetVarint() routine to extract the
34975 p -= 4;
34986 ** 64-bit integer.
34996 ** Read or write a four-byte big-endian integer value.
35063 n--;
35106 eOpenState = db->eOpenState;
35119 eOpenState = db->eOpenState;
35132 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
35133 ** the other 64-bit signed integer at *pA and store the result in *pA.
35143 testcase( iB==-1 ); testcase( iB==0 );
35145 testcase( iA>0 && LARGEST_INT64 - iA == iB );
35146 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
35147 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
35149 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
35150 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
35151 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
35163 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
35165 *pA -= iB;
35168 return sqlite3AddInt64(pA, -iB);
35186 if( -iA>LARGEST_INT64/-iB ) return 1;
35195 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
35196 ** if the integer has a value of -2147483648, return +2147483647
35201 return -x;
35206 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
35212 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
35217 ** test.db-journal => test.nal
35218 ** test.db-wal => test.wal
35219 ** test.db-shm => test.shm
35220 ** test.db-mj7f3319fa => test.9fa
35229 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
35230 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
35248 5, 5, 5, /* 12-14 */
35249 4, 4, 4, 4, /* 15-18 */
35250 3, 3, 3, 3, 3, 3, /* 19-24 */
35251 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
35256 return a+x[a-b];
35260 return b+x[b-a];
35273 while( x<8 ){ y -= 10; x <<= 1; }
35276 int i = 60 - __builtin_clzll(x);
35280 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
35284 return a[x&7] + y - 10;
35298 e = (a>>52) - 1022;
35309 if( n>=5 ) n -= 2;
35310 else if( n>=1 ) n -= 1;
35312 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
35319 ** db->mallocFailed flag is set.
35332 ** is always zero-terminated.
35431 ** This is the implementation of generic hash-tables
35444 pNew->first = 0;
35445 pNew->count = 0;
35446 pNew->htsize = 0;
35447 pNew->ht = 0;
35458 elem = pH->first;
35459 pH->first = 0;
35460 sqlite3_free(pH->ht);
35461 pH->ht = 0;
35462 pH->htsize = 0;
35464 HashElem *next_elem = elem->next;
35468 pH->count = 0;
35477 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
35480 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
35498 pHead = pEntry->count ? pEntry->chain : 0;
35499 pEntry->count++;
35500 pEntry->chain = pNew;
35505 pNew->next = pHead;
35506 pNew->prev = pHead->prev;
35507 if( pHead->prev ){ pHead->prev->next = pNew; }
35508 else { pH->first = pNew; }
35509 pHead->prev = pNew;
35511 pNew->next = pH->first;
35512 if( pH->first ){ pH->first->prev = pNew; }
35513 pNew->prev = 0;
35514 pH->first = pNew;
35533 if( new_size==pH->htsize ) return 0;
35549 sqlite3_free(pH->ht);
35550 pH->ht = new_ht;
35551 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
35553 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
35554 unsigned int h = strHash(elem->pKey) % new_size;
35555 next_elem = elem->next;
35576 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
35578 h = strHash(pKey) % pH->htsize;
35579 pEntry = &pH->ht[h];
35580 elem = pEntry->chain;
35581 count = pEntry->count;
35584 elem = pH->first;
35585 count = pH->count;
35588 while( count-- ){
35590 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
35593 elem = elem->next;
35607 if( elem->prev ){
35608 elem->prev->next = elem->next;
35610 pH->first = elem->next;
35612 if( elem->next ){
35613 elem->next->prev = elem->prev;
35615 if( pH->ht ){
35616 pEntry = &pH->ht[h];
35617 if( pEntry->chain==elem ){
35618 pEntry->chain = elem->next;
35620 assert( pEntry->count>0 );
35621 pEntry->count--;
35624 pH->count--;
35625 if( pH->count==0 ){
35626 assert( pH->first==0 );
35627 assert( pH->count==0 );
35639 return findElementWithHash(pH, pKey, 0)->data;
35664 if( elem->data ){
35665 void *old_data = elem->data;
35669 elem->data = data;
35670 elem->pKey = pKey;
35677 new_elem->pKey = pKey;
35678 new_elem->data = data;
35679 pH->count++;
35680 if( pH->count>=10 && pH->count > 2*pH->htsize ){
35681 if( rehash(pH, pH->count*2) ){
35682 assert( pH->htsize>0 );
35683 h = strHash(pKey) % pH->htsize;
35686 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
35763 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35764 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35765 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35794 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
35811 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
35828 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
35864 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
35899 ** 2022-09-06
35942 ** VFS - the database and the rollback journal.
35951 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
36064 /* Forward declarations for the low-level storage engine
36080 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
36084 ** underlying key/value store to use - either "local" or "session".
36086 ** Both zKey and zData are zero-terminated pure text strings.
36100 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
36113 ** this routine is a no-op.
36119 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
36130 ** not counting the final zero terminator. Return -1 if the key does
36150 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36151 return -1;
36157 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
36166 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36167 return -1;
36169 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
36172 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
36199 ** for JavaScript-side implementations in WASM builds. In such builds
36228 ** ---------------
36230 ** * Non-zero bytes are encoded as upper-case hexadecimal
36232 ** * A sequence of one or more zero-bytes that are not at the
36233 ** beginning of the buffer are encoded as a little-endian
36234 ** base-26 number using a..z. "a" means 0. "b" means 1,
36238 ** of hexadecimal and base-26 numbers, it is always clear where
36250 /* A sequence of 1 or more zeros is stored as a little-endian
36251 ** base-26 number using a..z as the digits. So one zero is "b".
36257 i += k-1;
36269 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36270 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36271 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36272 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36273 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36274 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36275 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36276 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36278 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36279 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36280 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36281 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36282 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36283 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36284 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36285 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36308 n += (c - 'a')*mult;
36312 if( j+n>nOut ) return -1;
36329 ** Decode a complete journal file. Allocate space in pFile->aJrnl
36330 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
36333 ** The first few characters of the text encoding will be a little-endian
36334 ** base-26 number (digits a..z) that is the total number of bytes
36335 ** in the decoded journal file image. This base-26 number is followed
36337 ** separator is required to act as a terminator for the base-26 number.
36340 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36341 const char *zTxt, /* Text encoding. Zero-terminated */
36349 n += (zTxt[i] - 'a')*mult;
36352 sqlite3_free(pFile->aJrnl);
36353 pFile->aJrnl = sqlite3_malloc64( n );
36354 if( pFile->aJrnl==0 ){
36355 pFile->nJrnl = 0;
36358 pFile->nJrnl = n;
36359 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36360 if( n<pFile->nJrnl ){
36361 sqlite3_free(pFile->aJrnl);
36362 pFile->aJrnl = 0;
36363 pFile->nJrnl = 0;
36373 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36379 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36385 ** Close an kvvfs-file.
36390 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36391 pFile->isJournal ? "journal" : "db"));
36392 sqlite3_free(pFile->aJrnl);
36397 ** Read from the -journal file.
36406 assert( pFile->isJournal );
36407 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36408 if( pFile->aJrnl==0 ){
36409 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36416 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36419 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36421 if( iOfst+iAmt>pFile->nJrnl ){
36424 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36444 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36449 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36452 pFile->szPage = iAmt;
36458 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36466 n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36478 memset(zBuf+n, 0, iAmt-n);
36486 ** Write into the -journal file.
36496 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36498 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36499 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36503 pFile->aJrnl = aNew;
36504 if( pFile->nJrnl<iOfst ){
36505 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36507 pFile->nJrnl = iEnd;
36509 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36526 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36528 assert( (iAmt & (iAmt-1))==0 );
36529 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36530 pFile->szPage = iAmt;
36534 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36537 if( iOfst+iAmt > pFile->szDb ){
36538 pFile->szDb = iOfst + iAmt;
36544 ** Truncate an kvvfs-file.
36548 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36550 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36551 sqlite3_free(pFile->aJrnl);
36552 pFile->aJrnl = 0;
36553 pFile->nJrnl = 0;
36558 if( pFile->szDb>size
36559 && pFile->szPage>0
36560 && (size % pFile->szPage)==0
36564 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36565 pgno = 1 + size/pFile->szPage;
36566 pgnoMax = 2 + pFile->szDb/pFile->szPage;
36569 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36572 pFile->szDb = size;
36579 ** Sync an kvvfs-file.
36585 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36586 if( pFile->nJrnl<=0 ){
36589 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36593 n = pFile->nJrnl;
36600 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36601 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36610 ** Return the current file-size of an kvvfs-file.
36614 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36615 *pSize = pFile->nJrnl;
36620 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36621 if( pFile->szDb>=0 ){
36622 *pSize = pFile->szDb;
36630 ** Lock an kvvfs-file.
36634 assert( !pFile->isJournal );
36635 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36638 pFile->szDb = kvvfsReadFileSize(pFile);
36644 ** Unlock an kvvfs-file.
36648 assert( !pFile->isJournal );
36649 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36651 pFile->szDb = -1;
36657 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36666 ** File control method. For custom operations on an kvvfs-file.
36677 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36678 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36687 ** Return the sector-size in bytes for an kvvfs-file.
36694 ** Return the device characteristic flags supported by an kvvfs-file.
36718 pFile->isJournal = 0;
36719 pFile->base.pMethods = &kvvfs_db_io_methods;
36721 if( strcmp(zName, "local-journal")==0
36722 || strcmp(zName, "session-journal")==0
36724 pFile->isJournal = 1;
36725 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36730 pFile->zClass = "session";
36732 pFile->zClass = "local";
36734 pFile->aJrnl = 0;
36735 pFile->nJrnl = 0;
36736 pFile->szPage = -1;
36737 pFile->szDb = -1;
36743 ** ensure the file-system modifications are synced to disk before
36747 if( strcmp(zPath, "local-journal")==0 ){
36750 if( strcmp(zPath, "session-journal")==0 ){
36767 if( strcmp(zPath, "local-journal")==0 ){
36770 if( strcmp(zPath, "session-journal")==0 ){
36803 if( nOut<nPath+1 ) nPath = nOut - 1;
36855 ** This routine is called initialize the KV-vfs as the default VFS.
36878 #define SHM_DMS_IDX (WAL_LOCK_NUM - 1)
36924 ** This file contains the VFS implementation for unix-like operating systems
36930 ** use flock(), dot-files, various proprietary locking schemas, or simply
36940 ** * General-purpose declarations and utility functions.
36944 ** + for no-op locks
36945 ** + for dot-file locks
36953 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
36966 ** 3. Dot-file locking,
37025 ** -DHAVE_GETHOSTUUID=0
37026 ** -DHAVE_GETHOSTUUID=1
37029 ** -DSQLITE_ENABLE_LOCKING_STYLE.
37067 ** If we are to be thread-safe, include the pthreads header.
37088 ** Maximum supported path-length.
37110 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
37139 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
37166 ** transaction counter in bytes 24-27 of database files are updated
37231 ** The threadid macro resolves to the thread-id or to 0. Used for
37252 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
37253 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
37261 ** Linux-specific IOCTL magic numbers used for controlling F2FS
37302 ** which always has the same well-defined interface.
37335 ** Many system calls are accessed through pointer-to-functions so that
37364 ** DJGPP. But it is DOS - what did you expect?
37511 ** log if they come from non-root processes. So avoid calling fchown() if
37591 int i = -1;
37595 for(i=0; i<ArraySize(aSyscall)-1; i++){
37623 ** The m parameter will be non-zero only when creating -wal, -journal,
37624 ** and -shm files. We want those files to have *exactly* the same
37626 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
37651 fd = -1;
37716 ** integer lock-type.
37735 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
37736 ** command-line option on the compiler. This code is normally
37752 if( p->l_type==F_RDLCK ){
37754 }else if( p->l_type==F_WRLCK ){
37756 }else if( p->l_type==F_UNLCK ){
37761 assert( p->l_whence==SEEK_SET );
37765 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
37766 (int)p->l_pid, s);
37767 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
37780 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
37800 /* On Android, ftruncate() always uses 32-bit offsets, even if
37861 ** a performance-critical path, so it is sufficient to put these
37886 ** Changes are made in-place. Return the new name length.
37888 ** The original filename is in z[0..n-1]. Return the number of
37893 while( n>1 && z[n-1]=='/' ){ n--; }
37902 while( j>0 && z[j-1]!='/' ){ j--; }
37903 if( j>0 ){ j--; }
37934 pNew->zCanonicalName = (char*)&pNew[1];
37935 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
37936 n = vxworksSimplifyName(pNew->zCanonicalName, n);
37943 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
37944 if( pCandidate->nName==n
37945 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
37948 pCandidate->nRef++;
37955 pNew->nRef = 1;
37956 pNew->nName = n;
37957 pNew->pNext = vxworksFileList;
37969 assert( pId->nRef>0 );
37970 pId->nRef--;
37971 if( pId->nRef==0 ){
37973 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
37975 *pp = pId->pNext;
38033 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
38061 ** on linux - with NPTL a lock created by thread A can override locks
38062 ** in thread B. But there is no way to know at compile-time which
38064 ** compile-time whether or not thread A can override locks on thread B.
38065 ** One has to do a run-time check to discover the behavior of the
38085 ** sizes ino_t at only 32-bits instead of 64-bits. (See
38086 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
38087 ** To work around this, always allocate 64-bits for the inode number.
38088 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
38155 assert( pFile->pInode );
38156 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
38159 assert( pFile->pInode );
38160 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
38166 ** This function - unixLogErrorAtLine(), is only ever called via the macro
38171 ** errno and, if possible, the human-readable equivalent from strerror() or
38177 ** failed (e.g. "unlink", "open") and the associated file-system path,
38191 ** the strerror() function to obtain the human-readable error message
38214 strerror_r(iErrno, aErr, sizeof(aErr)-1);
38220 /* Non-threadsafe build, use strerror(). */
38226 "os_unix.c:%d: (%d) %s(%s) - %s",
38249 pFile ? pFile->zPath : 0, lineno);
38254 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
38258 pFile->lastErrno = error;
38262 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
38265 unixInodeInfo *pInode = pFile->pInode;
38269 for(p=pInode->pUnused; p; p=pNext){
38270 pNext = p->pNext;
38271 robust_close(pFile, p->fd, __LINE__);
38274 pInode->pUnused = 0;
38284 unixInodeInfo *pInode = pFile->pInode;
38288 pInode->nRef--;
38289 if( pInode->nRef==0 ){
38290 assert( pInode->pShmNode==0 );
38291 sqlite3_mutex_enter(pInode->pLockMutex);
38293 sqlite3_mutex_leave(pInode->pLockMutex);
38294 if( pInode->pPrev ){
38295 assert( pInode->pPrev->pNext==pInode );
38296 pInode->pPrev->pNext = pInode->pNext;
38299 inodeList = pInode->pNext;
38301 if( pInode->pNext ){
38302 assert( pInode->pNext->pPrev==pInode );
38303 pInode->pNext->pPrev = pInode->pPrev;
38305 sqlite3_mutex_free(pInode->pLockMutex);
38327 struct stat statbuf; /* Low-level file information */
38332 /* Get low-level information about the file that we can used to
38335 fd = pFile->h;
38340 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
38343 … sqlite3_log(SQLITE_IOERR, "findInodeInfo-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc);
38350 ** incorrectly for zero-size files. See ticket #3260. To work
38359 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
38364 … sqlite3_log(SQLITE_IOERR, "findInodeInfo-osWrite, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc);
38372 …sqlite3_log(SQLITE_IOERR, "findInodeInfo-msdos-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno…
38382 fileId.pId = pFile->pId;
38388 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
38389 pInode = pInode->pNext;
38397 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
38399 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38400 if( pInode->pLockMutex==0 ){
38405 pInode->nRef = 1;
38407 pInode->pNext = inodeList;
38408 pInode->pPrev = 0;
38409 if( inodeList ) inodeList->pPrev = pInode;
38412 pInode->nRef++;
38423 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
38426 return pFile->pInode!=0 &&
38427 (osStat(pFile->zPath, &buf)!=0
38428 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
38447 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
38449 rc = osFstat(pFile->h, &buf);
38451 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
38455 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
38459 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
38463 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
38472 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38483 assert( pFile->eFileLock<=SHARED_LOCK );
38484 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38487 if( pFile->pInode->eFileLock>SHARED_LOCK ){
38494 if( !reserved && !pFile->pInode->bProcessLock ){
38500 if( osFcntl(pFile->h, F_GETLK, &lock) ){
38509 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38510 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
38520 ** Set a posix-advisory-lock.
38524 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
38529 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
38540 int tm = pFile->iBusyTimeout;
38550 tm--;
38558 ** Attempt to set a system-lock on the file pFile. The lock is
38561 ** If the pFile was opened read/write from unix-excl, then the only lock
38564 ** operations become no-ops. Locking operations still happen internally,
38569 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
38570 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
38571 ** and is read-only.
38573 ** Zero is returned if the call completes successfully, or -1 if a call
38578 unixInodeInfo *pInode = pFile->pInode;
38580 assert( sqlite3_mutex_held(pInode->pLockMutex) );
38581 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
38582 if( pInode->bProcessLock==0 ){
38584 assert( pInode->nLock==0 );
38589 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
38591 pInode->bProcessLock = 1;
38592 pInode->nLock++;
38597 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
38603 ** Lock the file with the lock specified by parameter eFileLock - one
38617 ** UNLOCKED -> SHARED
38618 ** SHARED -> RESERVED
38619 ** SHARED -> (PENDING) -> EXCLUSIVE
38620 ** RESERVED -> (PENDING) -> EXCLUSIVE
38621 ** PENDING -> EXCLUSIVE
38629 ** lock primitives (called read-locks and write-locks below, to avoid
38638 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
38639 ** byte'. If this is successful, 'shared byte range' is read-locked
38642 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
38644 ** Windows95 is now pretty much extinct, but this work-around for the
38645 ** lack of shared-locks on Windows95 lives on, for backwards
38649 ** A RESERVED lock is implemented by grabbing a write-lock on the
38653 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
38661 ** implemented by obtaining a write-lock on the entire 'shared byte
38662 ** range'. Since all other locks require a read-lock on one of the bytes
38673 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
38674 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38675 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
38682 if( pFile->eFileLock>=eFileLock ){
38683 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
38694 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
38696 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
38698 /* This mutex is needed because pFile->pInode is shared across threads
38700 pInode = pFile->pInode;
38701 sqlite3_mutex_enter(pInode->pLockMutex);
38706 if( (pFile->eFileLock!=pInode->eFileLock &&
38707 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
38719 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
38721 assert( pFile->eFileLock==0 );
38722 assert( pInode->nShared>0 );
38723 pFile->eFileLock = SHARED_LOCK;
38724 pInode->nShared++;
38725 pInode->nLock++;
38738 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
38757 assert( pInode->nShared==0 );
38758 assert( pInode->eFileLock==0 );
38761 /* Now get the read-lock */
38785 pFile->eFileLock = SHARED_LOCK;
38786 pInode->nLock++;
38787 pInode->nShared = 1;
38789 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
38799 assert( 0!=pFile->eFileLock );
38823 /* Set up the transaction-counter change checking flags when
38829 && pFile->eFileLock<=SHARED_LOCK
38832 pFile->transCntrChng = 0;
38833 pFile->dbUpdate = 0;
38834 pFile->inNormalWrite = 1;
38840 pFile->eFileLock = eFileLock;
38841 pInode->eFileLock = eFileLock;
38843 pFile->eFileLock = PENDING_LOCK;
38844 pInode->eFileLock = PENDING_LOCK;
38848 sqlite3_mutex_leave(pInode->pLockMutex);
38849 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
38859 unixInodeInfo *pInode = pFile->pInode;
38860 UnixUnusedFd *p = pFile->pPreallocatedUnused;
38862 p->pNext = pInode->pUnused;
38863 pInode->pUnused = p;
38864 pFile->h = -1;
38865 pFile->pPreallocatedUnused = 0;
38873 ** the requested locking level, this routine is a no-op.
38888 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
38889 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
38893 if( pFile->eFileLock<=eFileLock ){
38896 pInode = pFile->pInode;
38897 sqlite3_mutex_enter(pInode->pLockMutex);
38898 assert( pInode->nShared!=0 );
38899 if( pFile->eFileLock>SHARED_LOCK ){
38900 assert( pInode->eFileLock==pFile->eFileLock );
38911 pFile->inNormalWrite = 0;
38915 ** before establishing the readlock - to avoid a race condition we downgrade
38931 off_t divSize = SHARED_SIZE - 1;
38937 if( unixFileLock(pFile, &lock)==(-1) ){
38947 if( unixFileLock(pFile, &lock)==(-1) ){
38958 lock.l_len = SHARED_SIZE-divSize;
38959 if( unixFileLock(pFile, &lock)==(-1) ){
38990 pInode->eFileLock = SHARED_LOCK;
39002 pInode->nShared--;
39003 if( pInode->nShared==0 ){
39008 pInode->eFileLock = NO_LOCK;
39012 pInode->eFileLock = NO_LOCK;
39013 pFile->eFileLock = NO_LOCK;
39020 pInode->nLock--;
39021 assert( pInode->nLock>=0 );
39022 if( pInode->nLock==0 ) closePendingFds(pFile);
39026 sqlite3_mutex_leave(pInode->pLockMutex);
39028 pFile->eFileLock = eFileLock;
39038 ** the requested locking level, this routine is a no-op.
39042 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
39067 if( pFile->h>=0 ){
39068 robust_close(pFile, pFile->h, __LINE__);
39069 pFile->h = -1;
39072 if( pFile->pId ){
39073 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39074 osUnlink(pFile->pId->zCanonicalName);
39076 vxworksReleaseFileId(pFile->pId);
39077 pFile->pId = 0;
39081 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
39082 osUnlink(pFile->zPath);
39083 sqlite3_free(*(char**)&pFile->zPath);
39084 pFile->zPath = 0;
39087 OSTRACE(("CLOSE %-3d\n", pFile->h));
39088 OpenCounter(-1);
39089 sqlite3_free(pFile->pPreallocatedUnused);
39100 unixInodeInfo *pInode = pFile->pInode;
39111 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
39112 sqlite3_mutex_enter(pInode->pLockMutex);
39113 if( pInode->nLock ){
39116 ** descriptor to pInode->pUnused list. It will be automatically closed
39121 sqlite3_mutex_leave(pInode->pLockMutex);
39123 assert( pFile->pShm==0 );
39133 ****************************** No-op Locking **********************************
39139 ** This locking mode is appropriate for use on read-only databases
39140 ** (ex: databases that are burned into CD-ROM, for example.) It can
39170 /******************* End of the no-op lock implementation *********************
39174 ************************* Begin dot-file Locking ******************************
39204 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39219 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
39220 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
39226 ** Lock the file with the lock specified by parameter eFileLock - one
39240 ** UNLOCKED -> SHARED
39241 ** SHARED -> RESERVED
39242 ** SHARED -> (PENDING) -> EXCLUSIVE
39243 ** RESERVED -> (PENDING) -> EXCLUSIVE
39244 ** PENDING -> EXCLUSIVE
39254 char *zLockFile = (char *)pFile->lockingContext;
39261 if( pFile->eFileLock > NO_LOCK ){
39262 pFile->eFileLock = eFileLock;
39289 pFile->eFileLock = eFileLock;
39298 ** the requested locking level, this routine is a no-op.
39304 char *zLockFile = (char *)pFile->lockingContext;
39308 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
39309 pFile->eFileLock, osGetpid(0)));
39312 /* no-op if possible */
39313 if( pFile->eFileLock==eFileLock ){
39321 pFile->eFileLock = SHARED_LOCK;
39338 pFile->eFileLock = NO_LOCK;
39349 sqlite3_free(pFile->lockingContext);
39352 /****************** End of the dot-file lock implementation *******************
39360 ** flock() locking is like dot-file locking in that the various
39361 ** fine-grain locking levels supported by SQLite are collapsed into
39388 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39401 if( pFile->eFileLock>SHARED_LOCK ){
39408 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
39411 lrc = robust_flock(pFile->h, LOCK_UN);
39430 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
39443 ** Lock the file with the lock specified by parameter eFileLock - one
39457 ** UNLOCKED -> SHARED
39458 ** SHARED -> RESERVED
39459 ** SHARED -> (PENDING) -> EXCLUSIVE
39460 ** RESERVED -> (PENDING) -> EXCLUSIVE
39461 ** PENDING -> EXCLUSIVE
39479 if (pFile->eFileLock > NO_LOCK) {
39480 pFile->eFileLock = eFileLock;
39486 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
39495 pFile->eFileLock = eFileLock;
39497 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
39513 ** the requested locking level, this routine is a no-op.
39519 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
39520 pFile->eFileLock, osGetpid(0)));
39523 /* no-op if possible */
39524 if( pFile->eFileLock==eFileLock ){
39530 pFile->eFileLock = eFileLock;
39535 if( robust_flock(pFile->h, LOCK_UN) ){
39540 sqlite3_log(SQLITE_IOERR_UNLOCK, "IOERR_UNLOCK fd[%d], eFileLock[%d]", pFile->h, eFileLock);
39544 pFile->eFileLock = NO_LOCK;
39568 ** Semaphore locking is like dot-lock and flock in that it really only
39578 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39591 if( pFile->eFileLock>SHARED_LOCK ){
39597 sem_t *pSem = pFile->pInode->pSem;
39599 if( sem_trywait(pSem)==-1 ){
39606 reserved = (pFile->eFileLock < SHARED_LOCK);
39613 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
39620 ** Lock the file with the lock specified by parameter eFileLock - one
39634 ** UNLOCKED -> SHARED
39635 ** SHARED -> RESERVED
39636 ** SHARED -> (PENDING) -> EXCLUSIVE
39637 ** RESERVED -> (PENDING) -> EXCLUSIVE
39638 ** PENDING -> EXCLUSIVE
39650 sem_t *pSem = pFile->pInode->pSem;
39655 if (pFile->eFileLock > NO_LOCK) {
39656 pFile->eFileLock = eFileLock;
39662 if( sem_trywait(pSem)==-1 ){
39668 pFile->eFileLock = eFileLock;
39679 ** the requested locking level, this routine is a no-op.
39683 sem_t *pSem = pFile->pInode->pSem;
39687 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
39688 pFile->eFileLock, osGetpid(0)));
39691 /* no-op if possible */
39692 if( pFile->eFileLock==eFileLock ){
39698 pFile->eFileLock = eFileLock;
39703 if ( sem_post(pSem)==-1 ) {
39711 pFile->eFileLock = NO_LOCK;
39744 ** on Apple Macintosh computers - both OS9 and OSX.
39746 ** Third-party implementations of AFP are available. But this code here
39773 ** This is a utility for setting or clearing a bit-range lock on an
39792 pb.fd = pFile->h;
39795 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
39798 if ( err==-1 ) {
39821 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39833 context = (afpLockingContext *) pFile->lockingContext;
39834 if( context->reserved ){
39838 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
39840 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39848 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
39852 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
39862 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
39863 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
39870 ** Lock the file with the lock specified by parameter eFileLock - one
39884 ** UNLOCKED -> SHARED
39885 ** SHARED -> RESERVED
39886 ** SHARED -> (PENDING) -> EXCLUSIVE
39887 ** RESERVED -> (PENDING) -> EXCLUSIVE
39888 ** PENDING -> EXCLUSIVE
39896 unixInodeInfo *pInode = pFile->pInode;
39897 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
39900 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
39901 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
39902 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
39908 if( pFile->eFileLock>=eFileLock ){
39909 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
39919 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
39921 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
39923 /* This mutex is needed because pFile->pInode is shared across threads
39925 pInode = pFile->pInode;
39926 sqlite3_mutex_enter(pInode->pLockMutex);
39931 if( (pFile->eFileLock!=pInode->eFileLock &&
39932 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
39943 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
39945 assert( pFile->eFileLock==0 );
39946 assert( pInode->nShared>0 );
39947 pFile->eFileLock = SHARED_LOCK;
39948 pInode->nShared++;
39949 pInode->nLock++;
39958 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
39961 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
39975 assert( pInode->nShared==0 );
39976 assert( pInode->eFileLock==0 );
39979 /* Now get the read-lock SHARED_LOCK */
39982 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
39983 lrc1 = afpSetLock(context->dbPath, pFile,
39984 SHARED_FIRST+pInode->sharedByte, 1, 1);
39986 lrc1Errno = pFile->lastErrno;
39989 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40001 pFile->eFileLock = SHARED_LOCK;
40002 pInode->nLock++;
40003 pInode->nShared = 1;
40005 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
40015 assert( 0!=pFile->eFileLock );
40016 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
40018 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
40020 context->reserved = 1;
40029 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
40030 pInode->sharedByte, 1, 0)) ){
40033 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
40035 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
40036 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
40054 pFile->eFileLock = eFileLock;
40055 pInode->eFileLock = eFileLock;
40057 pFile->eFileLock = PENDING_LOCK;
40058 pInode->eFileLock = PENDING_LOCK;
40062 sqlite3_mutex_leave(pInode->pLockMutex);
40063 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
40073 ** the requested locking level, this routine is a no-op.
40079 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
40082 int h = pFile->h;
40086 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
40087 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
40091 if( pFile->eFileLock<=eFileLock ){
40094 pInode = pFile->pInode;
40095 sqlite3_mutex_enter(pInode->pLockMutex);
40096 assert( pInode->nShared!=0 );
40097 if( pFile->eFileLock>SHARED_LOCK ){
40098 assert( pInode->eFileLock==pFile->eFileLock );
40100 SimulateIOError( h=(-1) )
40112 assert( pFile->inNormalWrite==0
40113 || pFile->dbUpdate==0
40114 || pFile->transCntrChng==1 );
40115 pFile->inNormalWrite = 0;
40118 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
40119 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
40120 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
40121 /* only re-establish the shared lock if necessary */
40122 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40123 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
40128 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
40129 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
40131 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
40132 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
40134 context->reserved = 0;
40137 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
40138 pInode->eFileLock = SHARED_LOCK;
40147 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40148 pInode->nShared--;
40149 if( pInode->nShared==0 ){
40151 SimulateIOError( h=(-1) )
40154 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
40157 pInode->eFileLock = NO_LOCK;
40158 pFile->eFileLock = NO_LOCK;
40162 pInode->nLock--;
40163 assert( pInode->nLock>=0 );
40164 if( pInode->nLock==0 ) closePendingFds(pFile);
40168 sqlite3_mutex_leave(pInode->pLockMutex);
40170 pFile->eFileLock = eFileLock;
40185 if( pFile->pInode ){
40186 unixInodeInfo *pInode = pFile->pInode;
40187 sqlite3_mutex_enter(pInode->pLockMutex);
40188 if( pInode->nLock ){
40191 ** descriptor to pInode->aPending. It will be automatically closed when
40196 sqlite3_mutex_leave(pInode->pLockMutex);
40199 sqlite3_free(pFile->lockingContext);
40209 ** is available. If you don't compile for a mac, then the "unix-afp"
40224 ** the requested locking level, this routine is a no-op.
40240 **************** Non-locking sqlite3_file methods *****************************
40270 assert( id->h>2 );
40273 got = osPread(id->h, pBuf, cnt, offset);
40274 SimulateIOError( got = -1 );
40276 got = osPread64(id->h, pBuf, cnt, offset);
40277 SimulateIOError( got = -1 );
40279 newOffset = lseek(id->h, offset, SEEK_SET);
40280 SimulateIOError( newOffset = -1 );
40283 return -1;
40285 got = osRead(id->h, pBuf, cnt);
40294 cnt -= got;
40301 OSTRACE(("READ %-3d %5d %7lld %llu\n",
40302 id->h, got+prior, offset-prior, TIMER_ELAPSED));
40323 /* If this is a database file (not a journal, super-journal or temp
40326 assert( pFile->pPreallocatedUnused==0
40335 if( offset<pFile->mmapSize ){
40336 if( offset+amt <= pFile->mmapSize ){
40337 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
40340 int nCopy = pFile->mmapSize - offset;
40341 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
40343 amt -= nCopy;
40353 /* pFile->lastErrno has been set by seekAndRead().
40360 switch( pFile->lastErrno ){
40370 …sqlite3_log(SQLITE_IOERR_CORRUPTFS, "unixRead-EDEVERR, fd:[%d], amt[%d], got[%d], offset[%lld]", p…
40375 …sqlite3_log(SQLITE_IOERR_READ, "unixRead-got<0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->…
40380 /* Unread parts of the buffer must be zero-filled */
40381 memset(&((char*)pBuf)[got], 0, amt-got);
40387 ** Attempt to seek the file-descriptor passed as the first argument to
40389 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
40415 SimulateIOError( iSeek = -1 );
40417 rc = -1;
40425 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
40433 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
40440 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
40459 /* If this is a database file (not a journal, super-journal or temp
40462 assert( pFile->pPreallocatedUnused==0
40470 ** doing a hot-journal rollback or a write to some file other than a
40475 if( pFile->inNormalWrite ){
40476 pFile->dbUpdate = 1; /* The database has been modified */
40483 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
40484 pFile->transCntrChng = 1; /* The transaction counter has changed */
40493 if( offset<pFile->mmapSize ){
40494 if( offset+amt <= pFile->mmapSize ){
40495 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
40498 int nCopy = pFile->mmapSize - offset;
40499 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
40501 amt -= nCopy;
40508 amt -= wrote;
40512 SimulateIOError(( wrote=(-1), amt=1 ));
40516 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
40521 pFile->h,
40525 pFile->lastErrno);
40533 pFile->h,
40537 pFile->lastErrno);
40559 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
40629 ** no-op. But go ahead and call fstat() to validate the file
40662 if( rc==-1 && errno==ENOTSUP ){
40668 if( OS_VXWORKS && rc!= -1 ){
40681 ** The directory file descriptor is used for only one thing - to
40690 ** replace this routine with a harmless no-op. To make this routine
40691 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
40699 int fd = -1;
40703 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
40712 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
40727 ** has been created by fsync-ing the directory that contains the file.
40732 ** will not roll back - possibly leading to database corruption.
40752 OSTRACE(("SYNC %-3d\n", pFile->h));
40753 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
40757 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
40761 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
40764 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
40766 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
40768 rc = osOpenDirectory(pFile->zPath, &dirfd);
40776 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
40790 /* If the user has configured a chunk-size for this file, truncate the
40795 if( pFile->szChunk>0 ){
40796 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
40799 rc = robust_ftruncate(pFile->h, nByte);
40802 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
40806 ** doing a hot-journal rollback or a write to some file other than a
40809 ** when restoring a database using the backup API from a zero-length
40812 if( pFile->inNormalWrite && nByte==0 ){
40813 pFile->transCntrChng = 1;
40822 if( nByte<pFile->mmapSize ){
40823 pFile->mmapSize = nByte;
40838 rc = osFstat(((unixFile*)id)->h, &buf);
40846 /* When opening a zero-size database, the findInodeInfo() procedure
40848 ** in the OS-X msdos filesystem. In order to avoid problems with upper
40860 ** Handler for proxy-locking file-control verbs. Defined below in the
40868 ** file-control operation. Enlarge the database to nBytes in size
40869 ** (rounded up to the next chunk-size). If the database is already
40870 ** nBytes or larger, this routine is a no-op.
40873 if( pFile->szChunk>0 ){
40877 if( osFstat(pFile->h, &buf) ){
40881 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
40890 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
40895 "fcntlSizeHint-fallocate, fd[%d], bufSize[%lld], nSize[%lld] err[%d]",
40896 pFile->h,
40907 ** at offset (nSize-1), to set the size of the file correctly.
40911 int nBlk = buf.st_blksize; /* File-system block size */
40915 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
40918 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
40919 if( iWrite>=nSize ) iWrite = nSize - 1;
40924 "fcntlSizeHint-seekAndWrite, fd[%d], nWrite[%d], nSize[%d], nBlk[%d], iWrite[%lld]",
40925 pFile->h,
40939 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
40941 if( pFile->szChunk<=0 ){
40942 if( robust_ftruncate(pFile->h, nByte) ){
40944 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
40958 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
40960 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
40964 *pArg = (pFile->ctrlFlags & mask)!=0;
40966 pFile->ctrlFlags &= ~mask;
40968 pFile->ctrlFlags |= mask;
40986 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
40989 … sqlite3_log(SQLITE_IOERR_BEGIN_ATOMIC, "unixFileControl-begin, fd[%d], op[%d]", pFile->h, op);
40995 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
40998 … sqlite3_log(SQLITE_IOERR_COMMIT_ATOMIC, "unixFileControl-commit, fd[%d], op[%d]", pFile->h, op);
41004 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
41007 …sqlite3_log(SQLITE_IOERR_ROLLBACK_ATOMIC, "unixFileControl-rollback, fd[%d], op[%d]", pFile->h, op…
41015 *(int*)pArg = pFile->eFileLock;
41019 *(int*)pArg = pFile->lastErrno;
41023 pFile->szChunk = *(int *)pArg;
41042 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
41046 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
41048 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
41059 int iOld = pFile->iBusyTimeout;
41060 pFile->iBusyTimeout = *(int*)pArg;
41075 ** 64-bit type. */
41080 *(i64*)pArg = pFile->mmapSizeMax;
41081 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
41082 pFile->mmapSizeMax = newLimit;
41083 if( pFile->mmapSize>0 ){
41085 rc = unixMapfile(pFile, -1);
41098 ((unixFile*)id)->dbUpdate = 0;
41122 ** If pFd->sectorSize is non-zero when this function is called, it is a
41123 ** no-op. Otherwise, the values of pFd->sectorSize and
41124 ** pFd->deviceCharacteristics are set according to the file-system
41132 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
41133 if( pFd->sectorSize==0 ){
41139 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
41141 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
41146 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
41147 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
41150 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41157 if( pFile->sectorSize == 0 ){
41160 /* Set defaults for non-supported filesystems */
41161 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41162 pFile->deviceCharacteristics = 0;
41163 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
41168 pFile->sectorSize = fsInfo.f_bsize;
41169 pFile->deviceCharacteristics =
41177 pFile->sectorSize = fsInfo.f_bsize;
41178 pFile->deviceCharacteristics =
41180 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
41187 pFile->sectorSize = fsInfo.f_bsize;
41188 pFile->deviceCharacteristics =
41196 pFile->sectorSize = fsInfo.f_bsize;
41197 pFile->deviceCharacteristics =
41199 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41204 pFile->sectorSize = fsInfo.f_bsize;
41205 pFile->deviceCharacteristics =
41207 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41212 pFile->deviceCharacteristics =
41221 if( pFile->sectorSize % 512 != 0 ){
41222 pFile->deviceCharacteristics = 0;
41223 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41234 ** if two files are created in the same file-system directory (i.e.
41241 return pFd->sectorSize;
41250 ** words, after a power-loss event, parts of the file that were never
41251 ** written might end up being altered.) However, non-PSOW behavior is very,
41254 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
41260 return pFd->deviceCharacteristics;
41288 ** When multiple threads all reference the same wal-index, each thread
41290 ** of this unixShmNode object. In other words, each wal-index is opened
41305 ** The following fields are read-only after the object is created:
41319 int szRegion; /* Size of shared-memory regions */
41321 u8 isReadonly; /* True if read-only */
41323 char **apRegion; /* Array of mapped shared-memory regions */
41326 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
41340 ** are read-only thereafter:
41345 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
41351 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
41365 ** wal-mode transactions in other processes on database file pFile. If
41374 if( pFile->pShm){
41375 unixShmNode *pShmNode = pFile->pShm->pShmNode;
41382 f.l_len = SQLITE_SHM_NLOCK - 3;
41384 sqlite3_mutex_enter(pShmNode->pShmMutex);
41385 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
41390 sqlite3_mutex_leave(pShmNode->pShmMutex);
41398 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
41400 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
41409 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
41414 pShmNode = pFile->pInode->pShmNode;
41415 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
41416 assert( pShmNode->nRef>0 || unixMutexHeld() );
41424 if( pShmNode->hShm>=0 ){
41431 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
41432 if( res==-1 ){
41434 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
41444 OSTRACE(("SHM-LOCK "));
41445 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
41449 pShmNode->exclMask &= ~mask;
41450 pShmNode->sharedMask &= ~mask;
41452 OSTRACE(("read-lock %d ok", ofst));
41453 pShmNode->exclMask &= ~mask;
41454 pShmNode->sharedMask |= mask;
41457 OSTRACE(("write-lock %d ok", ofst));
41458 pShmNode->exclMask |= mask;
41459 pShmNode->sharedMask &= ~mask;
41465 OSTRACE(("read-lock failed"));
41468 OSTRACE(("write-lock %d failed", ofst));
41471 OSTRACE((" - afterwards %03x,%03x\n",
41472 pShmNode->sharedMask, pShmNode->exclMask));
41482 ** current system page-size.
41485 ** to use 64KB pages - in this case each mapping must cover at least two
41491 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
41499 ** This is not a VFS shared-memory method; it is a utility function called
41500 ** by VFS shared-memory methods.
41503 unixShmNode *p = pFd->pInode->pShmNode;
41505 if( p && ALWAYS(p->nRef==0) ){
41508 assert( p->pInode==pFd->pInode );
41509 sqlite3_mutex_free(p->pShmMutex);
41510 for(i=0; i<p->nRegion; i+=nShmPerMap){
41511 if( p->hShm>=0 ){
41512 osMunmap(p->apRegion[i], p->szRegion);
41514 sqlite3_free(p->apRegion[i]);
41517 sqlite3_free(p->apRegion);
41518 if( p->hShm>=0 ){
41519 robust_close(pFd, p->hShm, __LINE__);
41520 p->hShm = -1;
41522 p->pInode->pShmNode = 0;
41534 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
41543 ** and proceed with opening the *-shm file.
41547 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
41554 ** EXCLUSIVE failed just before truncating the *-shm file, then this
41555 ** process might open and use the *-shm file without truncating it.
41556 ** And if the *-shm file has been corrupted by a power failure or
41562 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
41565 if( pShmNode->isReadonly ){
41566 pShmNode->isUnlocked = 1;
41572 /* The first connection to attach must truncate the -shm file. We
41574 ** -shm header size) rather than 0 as a system debugging aid, to
41575 ** help detect if a -shm file truncation is legitimate or is the work
41577 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
41578 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
41597 ** Open a shared-memory area associated with open database file pDbFd.
41600 ** The file used to implement shared-memory is in the same directory
41602 ** file with the "-shm" suffix added. For example, if the database file
41604 ** for shared memory will be called "/home/user1/config.db-shm".
41610 ** database to end up using different files for shared memory -
41611 ** meaning that their memory would not really be shared - resulting
41613 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
41614 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
41618 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
41621 ** When opening a new shared-memory file, if no other instances of that
41625 ** If the original database file (pDbFd) is using the "unix-excl" VFS
41643 assert( pDbFd->pShm==0 );
41650 pInode = pDbFd->pInode;
41651 pShmNode = pInode->pShmNode;
41655 const char *zBasePath = pDbFd->zPath;
41659 ** a new *-shm file is created, an attempt will be made to create it
41662 if( osFstat(pDbFd->h, &sStat) ){
41678 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
41681 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
41684 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
41685 sqlite3FileSuffix3(pDbFd->zPath, zShm);
41687 pShmNode->hShm = -1;
41688 pDbFd->pInode->pShmNode = pShmNode;
41689 pShmNode->pInode = pDbFd->pInode;
41691 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
41692 if( pShmNode->pShmMutex==0 ){
41698 if( pInode->bProcessLock==0 ){
41699 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
41700 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
41703 if( pShmNode->hShm<0 ){
41704 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
41706 if( pShmNode->hShm<0 ){
41710 pShmNode->isReadonly = 1;
41717 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
41725 p->pShmNode = pShmNode;
41727 p->id = pShmNode->nextShmId++;
41729 pShmNode->nRef++;
41730 pDbFd->pShm = p;
41737 ** at pShmNode->pFirst. This must be done while holding the
41738 ** pShmNode->pShmMutex.
41740 sqlite3_mutex_enter(pShmNode->pShmMutex);
41741 p->pNext = pShmNode->pFirst;
41742 pShmNode->pFirst = p;
41743 sqlite3_mutex_leave(pShmNode->pShmMutex);
41756 ** shared-memory associated with the database file fd. Shared-memory regions
41757 ** are numbered starting from zero. Each shared-memory region is szRegion
41762 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
41765 ** bExtend is non-zero and the requested shared-memory region has not yet
41768 ** If the shared-memory region has already been allocated or is allocated by
41787 /* If the shared-memory file has not yet been opened, open it now. */
41788 if( pDbFd->pShm==0 ){
41793 p = pDbFd->pShm;
41794 pShmNode = p->pShmNode;
41795 sqlite3_mutex_enter(pShmNode->pShmMutex);
41796 if( pShmNode->isUnlocked ){
41799 pShmNode->isUnlocked = 0;
41801 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
41802 assert( pShmNode->pInode==pDbFd->pInode );
41803 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
41804 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
41809 if( pShmNode->nRegion<nReqRegion ){
41814 pShmNode->szRegion = szRegion;
41816 if( pShmNode->hShm>=0 ){
41818 ** Check to see if it has been allocated (i.e. if the wal-index file is
41821 if( osFstat(pShmNode->hShm, &sStat) ){
41849 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
41850 const char *zFile = pShmNode->zFilename;
41861 pShmNode->apRegion, nReqRegion*sizeof(char *)
41867 pShmNode->apRegion = apNew;
41868 while( pShmNode->nRegion<nReqRegion ){
41872 if( pShmNode->hShm>=0 ){
41874 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
41875 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
41878 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
41891 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
41893 pShmNode->nRegion += nShmPerMap;
41898 if( pShmNode->nRegion>iRegion ){
41899 *pp = pShmNode->apRegion[iRegion];
41903 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
41904 sqlite3_mutex_leave(pShmNode->pShmMutex);
41909 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
41919 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
41922 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
41925 if( pX->exclMask & (1<<i) ){
41927 aLock[i] = -1;
41928 }else if( pX->sharedMask & (1<<i) ){
41935 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
41936 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
41941 ** Change the lock state for a shared-memory segment.
41961 p = pDbFd->pShm;
41964 …sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShm, fd[%d], ofst[%d], n[%d], flags[%d]", pDbFd->h…
41968 pShmNode = p->pShmNode;
41971 …sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShmNode, fd[%d], ofst[%d], n[%d], flags[%d]", pDbF…
41975 aLock = pShmNode->aLock;
41976 int *aLockTid = pShmNode->aLockTid;
41977 assert( pShmNode==pDbFd->pInode->pShmNode );
41978 assert( pShmNode->pInode==pDbFd->pInode );
41986 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
41987 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
42003 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
42005 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
42006 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
42007 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
42011 mask = (1<<(ofst+n)) - (1<<ofst);
42013 sqlite3_mutex_enter(pShmNode->pShmMutex);
42017 if( (p->exclMask|p->sharedMask) & mask ){
42022 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
42032 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
42034 aLock[ofst]--;
42039 p->exclMask &= ~mask;
42040 p->sharedMask &= ~mask;
42046 assert( (p->exclMask & (1<<ofst))==0 );
42047 if( (p->sharedMask & mask)==0 ){
42057 p->sharedMask |= mask;
42067 assert( (p->sharedMask & mask)==0 );
42068 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
42075 ** also update the in-memory values. */
42080 assert( (p->sharedMask & mask)==0 );
42081 p->exclMask |= mask;
42083 aLock[ii] = -1;
42091 sqlite3_mutex_leave(pShmNode->pShmMutex);
42092 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
42093 p->id, osGetpid(0), p->sharedMask, p->exclMask));
42107 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
42108 assert( fd->pMethods->xLock==nolockLock
42116 ** Close a connection to shared-memory. Delete the underlying
42120 ** routine is a harmless no-op.
42124 int deleteFlag /* Delete shared-memory if true */
42127 unixShmNode *pShmNode; /* The underlying shared-memory file */
42132 p = pDbFd->pShm;
42134 pShmNode = p->pShmNode;
42136 assert( pShmNode==pDbFd->pInode->pShmNode );
42137 assert( pShmNode->pInode==pDbFd->pInode );
42141 sqlite3_mutex_enter(pShmNode->pShmMutex);
42142 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
42143 *pp = p->pNext;
42147 pDbFd->pShm = 0;
42148 sqlite3_mutex_leave(pShmNode->pShmMutex);
42150 /* If pShmNode->nRef has reached 0, then close the underlying
42151 ** shared-memory file, too */
42154 assert( pShmNode->nRef>0 );
42155 pShmNode->nRef--;
42156 if( pShmNode->nRef==0 ){
42157 if( deleteFlag && pShmNode->hShm>=0 ){
42158 osUnlink(pShmNode->zFilename);
42180 assert( pFd->nFetchOut==0 );
42181 if( pFd->pMapRegion ){
42182 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
42183 pFd->pMapRegion = 0;
42184 pFd->mmapSize = 0;
42185 pFd->mmapSizeActual = 0;
42209 int h = pFd->h; /* File descriptor open on db file */
42210 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
42211 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
42215 assert( pFd->nFetchOut==0 );
42216 assert( nNew>pFd->mmapSize );
42217 assert( nNew<=pFd->mmapSizeMax );
42219 assert( pFd->mmapSizeActual>=pFd->mmapSize );
42223 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
42228 i64 nReuse = pFd->mmapSize;
42231 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
42237 osMunmap(pReq, nOrig-nReuse);
42244 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
42247 osMunmap(pNew, nNew - nReuse);
42269 unixLogError(SQLITE_OK, zErr, pFd->zPath);
42274 pFd->mmapSizeMax = 0;
42276 pFd->pMapRegion = (void *)pNew;
42277 pFd->mmapSize = pFd->mmapSizeActual = nNew;
42281 ** Memory map or remap the file opened by file-descriptor pFd (if the file
42284 ** outstanding xFetch() references to it, this function is a no-op.
42286 ** If parameter nByte is non-negative, then it is the requested size of
42297 assert( nMap>=0 || pFd->nFetchOut==0 );
42298 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42299 if( pFd->nFetchOut>0 ) return SQLITE_OK;
42302 struct stat statbuf; /* Low-level file information */
42303 if( osFstat(pFd->h, &statbuf) ){
42308 if( nMap>pFd->mmapSizeMax ){
42309 nMap = pFd->mmapSizeMax;
42312 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42313 if( nMap!=pFd->mmapSize ){
42340 if( pFd->mmapSizeMax>0 ){
42341 if( pFd->pMapRegion==0 ){
42342 int rc = unixMapfile(pFd, -1);
42345 if( pFd->mmapSize >= iOff+nAmt ){
42346 *pp = &((u8 *)pFd->pMapRegion)[iOff];
42347 pFd->nFetchOut++;
42355 ** If the third argument is non-NULL, then this function releases a
42372 assert( (p==0)==(pFd->nFetchOut==0) );
42375 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
42378 pFd->nFetchOut--;
42383 assert( pFd->nFetchOut>=0 );
42401 ** of "finder" functions. A finder-function is used to locate the appropriate
42404 ** the correct finder-function for that VFS.
42407 ** object. The only interesting finder-function is autolockIoFinder, which
42411 ** For finder-function F, two objects are created:
42413 ** (1) The real finder-function named "FImpt()".
42420 ** directly at the finder-function since C90 rules prevent a void*
42537 ** The proxy locking method is a "super-method" in the sense that it
42539 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
42608 if( statfs(filePath, &fsInfo) != -1 ){
42620 ** Test byte-range lock using fcntl(). If the call succeeds,
42621 ** assume that the file-system supports POSIX style locks.
42627 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42667 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42705 assert( pNew->pInode==NULL );
42710 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
42711 pNew->h = h;
42712 pNew->pVfs = pVfs;
42713 pNew->zPath = zFilename;
42714 pNew->ctrlFlags = (u8)ctrlFlags;
42716 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
42720 pNew->ctrlFlags |= UNIXFILE_PSOW;
42722 if( strcmp(pVfs->zName,"unix-excl")==0 ){
42723 pNew->ctrlFlags |= UNIXFILE_EXCL;
42727 pNew->pId = vxworksFindFileId(zFilename);
42728 if( pNew->pId==0 ){
42737 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
42742 pNew->lockingContext = (void*)zFilename;
42752 rc = findInodeInfo(pNew, &pNew->pInode);
42765 ** handle h - as it is guaranteed that no posix locks will be released
42773 h = -1;
42784 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
42791 pCtx->dbPath = zFilename;
42792 pCtx->reserved = 0;
42795 rc = findInodeInfo(pNew, &pNew->pInode);
42797 sqlite3_free(pNew->lockingContext);
42799 h = -1;
42820 pNew->lockingContext = zLockFile;
42829 rc = findInodeInfo(pNew, &pNew->pInode);
42830 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
42831 char *zSemName = pNew->pInode->aSemName;
42834 pNew->pId->zCanonicalName);
42837 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
42838 if( pNew->pInode->pSem == SEM_FAILED ){
42840 pNew->pInode->aSemName[0] = '\0';
42851 h = -1;
42853 pNew->ctrlFlags |= UNIXFILE_DELETE;
42859 pId->pMethods = pLockingStyle;
42912 ** pVfs->mxPathname bytes.
42919 /* It's odd to simulate an io-error here, but really this is just
42920 ** using the io-error infrastructure to test that SQLite handles this
42935 zBuf[nBuf-2] = 0;
42938 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
42950 ** Routine to transform a unixFile into a proxy-locking unixFile.
42951 ** Implementation in the proxy-lock division, but used by unixOpen()
42959 ** file (not a journal or super-journal file) identified by pathname
42965 ** other file descriptor open on the same file is holding a file-lock.
42971 ** such file descriptor is located, -1 is returned.
42989 ** ignored and -1 is returned. The caller will try to open a new file
42998 while( pInode && (pInode->fileId.dev!=sStat.st_dev
42999 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
43000 pInode = pInode->pNext;
43004 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
43005 sqlite3_mutex_enter(pInode->pLockMutex);
43007 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
43010 *pp = pUnused->pNext;
43012 sqlite3_mutex_leave(pInode->pLockMutex);
43052 ** this function queries the file-system for the permissions on the
43081 ** "<path to db>-journal"
43082 ** "<path to db>-wal"
43083 ** "<path to db>-journalNN"
43084 ** "<path to db>-walNN"
43090 ** a '-' character. However in 8+3 filename mode, or if a corrupt
43091 ** rollback journal specifies a super-journal with a goofy name, then
43092 ** the '-' might be missing or the '-' might be the first character in
43095 nDb = sqlite3Strlen30(zPath) - 1;
43097 if( zPath[nDb]=='-' ){
43103 nDb--;
43132 ** ReadWrite() -> (READWRITE | CREATE)
43133 ** ReadOnly() -> (READONLY)
43134 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
43136 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
43150 int fd = -1; /* File descriptor returned by open() */
43169 /* If creating a super- or main-file journal, this function will open
43170 ** a file-descriptor on the directory too. The first time unixSync()
43197 /* The main DB, main journal, WAL file and super-journal are never
43204 /* Assert that the upper layer has set one of the "file-type" flags. */
43231 fd = pUnused->fd;
43238 p->pPreallocatedUnused = pUnused;
43240 /* Database filenames are double-zero terminated if they are not
43248 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
43254 /* Generated temporary filenames are always double-zero terminated
43275 assert( !p->pPreallocatedUnused );
43280 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
43288 /* Failed to open the file for read/write access. Try read-only. */
43305 ** the case. The chown() system call will be a no-op if the current
43324 if( p->pPreallocatedUnused ){
43325 p->pPreallocatedUnused->fd = fd;
43326 p->pPreallocatedUnused->flags =
43345 p->openFlags = openFlags;
43350 if( fstatfs(fd, &fsInfo) == -1 ){
43359 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43362 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43378 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
43383 ** never use proxy, NULL means use proxy for non-local files only. */
43396 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
43414 sqlite3_free(p->pPreallocatedUnused);
43432 if( osUnlink(zPath)==(-1) ){
43497 ** If the last component of the pathname in z[0]..z[j-1] is something
43505 for(i=j-1; i>0 && z[i-1]!='/'; i--){}
43507 if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0;
43508 *pJ = i-1;
43528 if( osGetcwd(zOut, nOut-2)==0 ){
43535 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
43540 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
43581 ** is stored as a nul-terminated string in the buffer pointed to by
43585 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
43603 assert( pVfs->mxPathname==MAX_PATHNAME );
43606 /* It's odd to simulate an io-error here, but really this is just
43607 ** using the io-error infrastructure to test that SQLite handles this
43637 nByte = osReadlink(zIn, zDel, nOut-1);
43643 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
43703 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
43706 ** use dlsym() with -pedantic-errors?
43714 ** This work-around is unlikely to work correctly on any system where
43743 ** errors. The reports issued by valgrind are incorrect - we would
43745 ** uninitialized space in zBuf - but valgrind errors tend to worry
43808 ** The following variable, if set to a non-zero value, is interpreted as
43872 ** low-level error message when operating-system problems come up
43891 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
43893 ** meta-layer over top of the primitive locking implemented above. For
43896 ** been defined - so that the primitive locking methods are available
43901 ** The default locking schemes in SQLite use byte-range locks on the
43906 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
43914 ** SHARED_RANGE 0x40000002 -> 0x40000200
43918 ** the read cache when byte-range locks are present. Enabling the read
43921 ** close-to-open semantics for ensuring cache coherency
43925 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
43934 ** -----------------
43966 ** -----------------------
43976 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
43977 ** by taking an sqlite-style shared lock on the conch file, reading the
43981 ** is patterned after the database file name as ".<databasename>-conch".
43989 ** The proxy file - a single-byte file used for all advisory file locks
44005 ** ---------------------
44009 ** Database files accessed on non-local file systems are
44054 int conchHeld; /* 1 if the conch is held, -1 if lockless */
44090 if( lPath[len-1]!='/' ){
44119 if( lockPath[i] == '/' && (i - start > 0) ){
44121 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
44122 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
44154 int fd = -1;
44163 ** 2. if that fails, and this is a lock file (not-conch), try creating
44165 ** 3. if that fails, try to open the file read-only
44170 fd = pUnused->fd;
44199 sqlite3_log(SQLITE_IOERR_LOCK, "proxyCreateUnixFile-EIO, fd[%d]", fd);
44212 pNew->openFlags = openFlags;
44216 pUnused->fd = fd;
44217 pUnused->flags = openFlags;
44218 pNew->pPreallocatedUnused = pUnused;
44276 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
44288 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44289 unixFile *conchFile = pCtx->conchFile;
44292 char *cPath = pCtx->conchFilePath;
44296 int fd = -1;
44297 int rc = -1;
44300 /* create a new path by replace the trailing '-conch' with '-break' */
44303 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
44308 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
44329 robust_close(pFile, conchFile->h, __LINE__);
44330 conchFile->h = fd;
44331 conchFile->openFlags = O_RDWR | O_CREAT;
44348 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44349 unixFile *conchFile = pCtx->conchFile;
44356 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44366 if( osFstat(conchFile->h, &buf) ){
44369 …CK, "proxyConchLock pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno);
44388 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
44392 …yConchLock tries 2, pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno);
44413 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
44416 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44426 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
44432 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44434 if( pCtx->conchHeld!=0 ){
44437 unixFile *conchFile = pCtx->conchFile;
44450 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
44451 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44467 storeLastErrno(pFile, conchFile->lastErrno);
44469 …akeConch pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, conchFile->lastEr…
44482 ** retry with a new auto-generated path
44490 if( !pCtx->lockProxyPath ){
44491 /* for auto-named local lock file, just check the host ID and we'll
44495 size_t pathLen = (readLen - PROXY_PATHINDEX);
44498 pathLen=MAXPATHLEN-1;
44508 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
44509 readLen-PROXY_PATHINDEX)
44517 if( (conchFile->openFlags&O_RDWR) == 0 ){
44523 if( !pCtx->lockProxyPath ){
44524 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
44533 futimes(conchFile->h, NULL);
44535 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
44551 if( pCtx->lockProxyPath!=NULL ){
44552 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
44558 robust_ftruncate(conchFile->h, writeSize);
44560 full_fsync(conchFile->h,0,0);
44566 int err = osFstat(pFile->h, &buf);
44572 osFchmod(conchFile->h, cmode);
44575 rc = osFchmod(conchFile->h, cmode);
44576 }while( rc==(-1) && errno==EINTR );
44592 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
44595 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
44596 if( rc==SQLITE_OK && pFile->openFlags ){
44598 if( pFile->h>=0 ){
44599 robust_close(pFile, pFile->h, __LINE__);
44601 pFile->h = -1;
44602 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
44605 pFile->h = fd;
44611 if( rc==SQLITE_OK && !pCtx->lockProxy ){
44612 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
44613 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
44616 ** so try again via auto-naming
44628 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
44629 if( !pCtx->lockProxyPath ){
44635 pCtx->conchHeld = 1;
44637 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
44639 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
44640 afpCtx->dbPath = pCtx->lockProxyPath;
44643 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44645 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
44648 } while (1); /* in case we need to retry the :auto: lock file -
44661 pCtx = (proxyLockingContext *)pFile->lockingContext;
44662 conchFile = pCtx->conchFile;
44663 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
44664 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44666 if( pCtx->conchHeld>0 ){
44667 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44669 pCtx->conchHeld = 0;
44670 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
44688 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
44700 for( i=(len-1); i>=0; i-- ){
44712 /* append the "-conch" suffix to the file */
44713 memcpy(&conchPath[i+1], "-conch", 7);
44720 /* Takes a fully configured proxy locking-style unix file and switches
44724 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44725 char *oldPath = pCtx->lockProxyPath;
44728 if( pFile->eFileLock!=NO_LOCK ){
44737 unixFile *lockProxy = pCtx->lockProxy;
44738 pCtx->lockProxy=NULL;
44739 pCtx->conchHeld = 0;
44741 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
44746 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
44761 if( pFile->pMethod == &afpIoMethods ){
44764 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44765 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
44769 if( pFile->pMethod == &dotlockIoMethods ){
44772 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
44773 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
44776 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44777 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
44787 ** ->lockingContext
44788 ** ->pMethod
44796 if( pFile->eFileLock!=NO_LOCK ){
44806 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
44815 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
44817 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
44818 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
44820 ** (c) the file system is read-only, then enable no-locking access.
44828 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
44830 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
44835 pCtx->conchHeld = -1; /* read only FS/ lockless */
44841 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
44845 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
44846 if( pCtx->dbPath==NULL ){
44854 pCtx->oldLockingContext = pFile->lockingContext;
44855 pFile->lockingContext = pCtx;
44856 pCtx->pOldMethod = pFile->pMethod;
44857 pFile->pMethod = &proxyIoMethods;
44859 if( pCtx->conchFile ){
44860 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
44861 sqlite3_free(pCtx->conchFile);
44863 sqlite3DbFree(0, pCtx->lockProxyPath);
44864 sqlite3_free(pCtx->conchFilePath);
44867 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
44881 if( pFile->pMethod == &proxyIoMethods ){
44882 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44884 if( pCtx->lockProxyPath ){
44885 *(const char **)pArg = pCtx->lockProxyPath;
44897 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
44900 /* turn off proxy locking - not supported. If support is added for
44906 /* turn off proxy locking - already off - NOOP */
44913 (proxyLockingContext*)pFile->lockingContext;
44915 || (pCtx->lockProxyPath &&
44916 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
44939 ** above this point are all utilities. The lock-related methods of the
44940 ** proxy-locking sqlite3_io_method object follow.
44947 ** to a non-zero value otherwise *pResOut is set to zero. The return value
44954 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44955 if( pCtx->conchHeld>0 ){
44956 unixFile *proxy = pCtx->lockProxy;
44957 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
44966 ** Lock the file with the lock specified by parameter eFileLock - one
44980 ** UNLOCKED -> SHARED
44981 ** SHARED -> RESERVED
44982 ** SHARED -> (PENDING) -> EXCLUSIVE
44983 ** RESERVED -> (PENDING) -> EXCLUSIVE
44984 ** PENDING -> EXCLUSIVE
44993 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44994 if( pCtx->conchHeld>0 ){
44995 unixFile *proxy = pCtx->lockProxy;
44996 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
44997 pFile->eFileLock = proxy->eFileLock;
45011 ** the requested locking level, this routine is a no-op.
45017 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45018 if( pCtx->conchHeld>0 ){
45019 unixFile *proxy = pCtx->lockProxy;
45020 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
45021 pFile->eFileLock = proxy->eFileLock;
45035 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
45036 unixFile *lockProxy = pCtx->lockProxy;
45037 unixFile *conchFile = pCtx->conchFile;
45041 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
45043 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
45046 pCtx->lockProxy = 0;
45049 if( pCtx->conchHeld ){
45053 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
45057 sqlite3DbFree(0, pCtx->lockProxyPath);
45058 sqlite3_free(pCtx->conchFilePath);
45059 sqlite3DbFree(0, pCtx->dbPath);
45061 pFile->lockingContext = pCtx->oldLockingContext;
45062 pFile->pMethod = pCtx->pOldMethod;
45064 return pFile->pMethod->xClose(id);
45084 ** This routine registers all VFS implementations for unix-like operating
45101 ** when compiling with -pedantic-errors on GCC.)
45104 ** finder-function. The finder-function returns a pointer to the
45107 ** macro for addition information on finder-functions.
45155 UNIXVFS("unix-none", nolockIoFinder ),
45156 UNIXVFS("unix-dotfile", dotlockIoFinder ),
45157 UNIXVFS("unix-excl", posixIoFinder ),
45159 UNIXVFS("unix-namedsem", semIoFinder ),
45162 UNIXVFS("unix-posix", posixIoFinder ),
45165 UNIXVFS("unix-flock", flockIoFinder ),
45168 UNIXVFS("unix-afp", afpIoFinder ),
45169 UNIXVFS("unix-nfs", nfsIoFinder ),
45170 UNIXVFS("unix-proxy", proxyIoFinder ),
45175 /* Double-check that the aSyscall[] array has been constructed
45201 ** READ-0 UNIX_SHM_BASE+3 123
45202 ** READ-1 UNIX_SHM_BASE+4 124
45203 ** READ-2 UNIX_SHM_BASE+5 125
45204 ** READ-3 UNIX_SHM_BASE+6 126
45205 ** READ-4 UNIX_SHM_BASE+7 127
45208 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
45222 ** This routine is a no-op for unix.
45276 ** based on the sub-platform)?
45284 ** based on the sub-platform)?
45372 ** characters, so we allocate 4 bytes per character assuming worst-case of
45373 ** 4-bytes-per-character for UTF8.
45396 ** Returns non-zero if the character should be treated as a directory
45460 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
45473 typedef struct winShm winShm; /* A connection to shared-memory */
45474 typedef struct winShmNode winShmNode; /* A region of shared-memory */
45532 BOOL bNoLock; /* Non-zero if locking is disabled. */
45546 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
45556 * If this is non-zero, an isolated heap will be created by the native Win32
45562 * WARNING: It is important to note that when this setting is non-zero and the
45574 * This is the maximum possible initial size of the Win32-specific heap, in
45582 * This is the extra space for the initial size of the Win32-specific heap,
45595 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
45602 * Win32-specific heap. It cannot be negative.
45608 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
45614 * initial size of the Win32-specific heap to exceed the maximum amount
45623 * The initial size of the Win32-specific heap. This value may be zero.
45632 * The maximum size of the Win32-specific heap. This value may be zero.
45648 ** The winMemData structure stores information required by the Win32-specific
45732 ** Many system calls are accessed through pointer-to-functions so that
46349 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
46461 int i = -1;
46465 for(i=0; i<ArraySize(aSyscall)-1; i++){
46480 ** "pnLargest" argument, if non-zero, will be used to return the size of the
46570 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
46571 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
46572 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
46638 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
46666 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
46691 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
46786 if( n==(SIZE_T)-1 ){
46808 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46809 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46812 if( !pWinMemData->hHeap ){
46820 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
46822 if( !pWinMemData->hHeap ){
46829 pWinMemData->bOwned = TRUE;
46830 assert( pWinMemData->bOwned );
46833 pWinMemData->hHeap = osGetProcessHeap();
46834 if( !pWinMemData->hHeap ){
46839 pWinMemData->bOwned = FALSE;
46840 assert( !pWinMemData->bOwned );
46842 assert( pWinMemData->hHeap!=0 );
46843 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46845 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46857 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46858 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46860 if( pWinMemData->hHeap ){
46861 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46863 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46865 if( pWinMemData->bOwned ){
46866 if( !osHeapDestroy(pWinMemData->hHeap) ){
46868 osGetLastError(), (void*)pWinMemData->hHeap);
46870 pWinMemData->bOwned = FALSE;
46872 pWinMemData->hHeap = NULL;
46877 ** Populate the low-level memory allocation function pointers in
46904 ** Convert a UTF-8 string to Microsoft Unicode.
46912 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
46920 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
46930 ** Convert a Microsoft Unicode string to UTF-8.
46938 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
46946 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
46966 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
46975 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
46985 ** Convert a Microsoft Unicode string to a multi-byte character string,
46995 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
47003 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
47013 ** Convert a multi-byte character string to UTF-8.
47031 ** Convert a UTF-8 string to a multi-byte character string.
47146 ** it accepts a UTF-8 string.
47190 ** it accepts a UTF-16 string.
47223 ** is zero if the error message fits in the buffer, or non-zero
47303 ** This function - winLogErrorAtLine() - is only ever called via the macro
47308 ** error code and, if possible, the human-readable equivalent from
47314 ** failed and the associated file-system path, if any.
47334 "os_win.c:%d: (%lu) %s(%s) - %s",
47343 ** will be retried following a locking error - probably caused by
47360 ** non-zero if the error code is transient in nature and the operation
47448 y.tm_year = pTm.wYear - 1900;
47449 y.tm_mon = pTm.wMonth - 1;
47463 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
47497 memset(&pFile->local, 0, sizeof(pFile->local));
47507 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
47508 if (!pFile->hMutex){
47509 pFile->lastErrno = osGetLastError();
47511 return winLogError(SQLITE_IOERR, pFile->lastErrno,
47516 winceMutexAcquire(pFile->hMutex);
47519 ** case-sensitive, take advantage of that by uppercasing the mutex name
47523 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
47528 ** must be zero-initialized */
47537 if( pFile->hShared ){
47538 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
47541 if( !pFile->shared ){
47542 pFile->lastErrno = osGetLastError();
47543 winLogError(SQLITE_IOERR, pFile->lastErrno,
47546 osCloseHandle(pFile->hShared);
47547 pFile->hShared = NULL;
47552 if( pFile->hShared==NULL ){
47554 pFile->lastErrno = lastErrno;
47555 winLogError(SQLITE_IOERR, pFile->lastErrno,
47559 winceMutexRelease(pFile->hMutex);
47560 osCloseHandle(pFile->hMutex);
47561 pFile->hMutex = NULL;
47567 memset(pFile->shared, 0, sizeof(winceLock));
47570 winceMutexRelease(pFile->hMutex);
47578 if (pFile->hMutex){
47580 winceMutexAcquire(pFile->hMutex);
47584 if (pFile->local.nReaders){
47585 pFile->shared->nReaders --;
47587 if (pFile->local.bReserved){
47588 pFile->shared->bReserved = FALSE;
47590 if (pFile->local.bPending){
47591 pFile->shared->bPending = FALSE;
47593 if (pFile->local.bExclusive){
47594 pFile->shared->bExclusive = FALSE;
47597 /* De-reference and close our copy of the shared memory handle */
47598 osUnmapViewOfFile(pFile->shared);
47599 osCloseHandle(pFile->hShared);
47602 winceMutexRelease(pFile->hMutex);
47603 osCloseHandle(pFile->hMutex);
47604 pFile->hMutex = NULL;
47624 if (!pFile->hMutex) return TRUE;
47625 winceMutexAcquire(pFile->hMutex);
47630 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
47631 pFile->shared->bExclusive = TRUE;
47632 pFile->local.bExclusive = TRUE;
47637 /* Want a read-only lock? */
47640 if (pFile->shared->bExclusive == 0){
47641 pFile->local.nReaders ++;
47642 if (pFile->local.nReaders == 1){
47643 pFile->shared->nReaders ++;
47653 if (pFile->shared->bPending == 0) {
47654 pFile->shared->bPending = TRUE;
47655 pFile->local.bPending = TRUE;
47663 if (pFile->shared->bReserved == 0) {
47664 pFile->shared->bReserved = TRUE;
47665 pFile->local.bReserved = TRUE;
47670 winceMutexRelease(pFile->hMutex);
47690 if (!pFile->hMutex) return TRUE;
47691 winceMutexAcquire(pFile->hMutex);
47696 if (pFile->local.bExclusive){
47698 pFile->local.bExclusive = FALSE;
47699 pFile->shared->bExclusive = FALSE;
47704 else if (pFile->local.nReaders){
47707 pFile->local.nReaders --;
47708 if (pFile->local.nReaders == 0)
47710 pFile->shared->nReaders --;
47719 if (pFile->local.bPending){
47720 pFile->local.bPending = FALSE;
47721 pFile->shared->bPending = FALSE;
47728 if (pFile->local.bReserved) {
47729 pFile->local.bReserved = FALSE;
47730 pFile->shared->bReserved = FALSE;
47735 winceMutexRelease(pFile->hMutex);
47815 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
47821 ** Otherwise, set pFile->lastErrno and return non-zero.
47830 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
47836 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
47842 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
47846 pFile->lastErrno = lastErrno;
47847 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47848 "winSeekFile", pFile->zPath);
47849 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47853 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
47864 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
47867 pFile->lastErrno = osGetLastError();
47868 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47869 "winSeekFile", pFile->zPath);
47870 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47874 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
47902 assert( pFile->pShm==0 );
47904 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
47906 osGetCurrentProcessId(), pFile, pFile->h));
47913 rc = osCloseHandle(pFile->h);
47919 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
47920 if( pAppData==NULL || !pAppData->bNoLock ){
47924 if( pFile->zDeleteOnClose ){
47927 osDeleteFileW(pFile->zDeleteOnClose)==0
47928 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
47933 sqlite3_free(pFile->zDeleteOnClose);
47937 pFile->h = NULL;
47939 OpenCounter(-1);
47941 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
47944 "winClose", pFile->zPath);
47971 pFile->h, pBuf, amt, offset, pFile->locktype));
47976 if( offset<pFile->mmapSize ){
47977 if( offset+amt <= pFile->mmapSize ){
47978 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
47979 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
47980 osGetCurrentProcessId(), pFile, pFile->h));
47983 int nCopy = (int)(pFile->mmapSize - offset);
47984 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
47986 amt -= nCopy;
47995 osGetCurrentProcessId(), pFile, pFile->h));
47998 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
48003 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
48008 pFile->lastErrno = lastErrno;
48010 osGetCurrentProcessId(), pFile, pFile->h));
48011 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
48012 "winRead", pFile->zPath);
48016 /* Unread parts of the buffer must be zero-filled */
48017 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
48019 osGetCurrentProcessId(), pFile, pFile->h));
48024 osGetCurrentProcessId(), pFile, pFile->h));
48049 pFile->h, pBuf, amt, offset, pFile->locktype));
48054 if( offset<pFile->mmapSize ){
48055 if( offset+amt <= pFile->mmapSize ){
48056 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
48057 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48058 osGetCurrentProcessId(), pFile, pFile->h));
48061 int nCopy = (int)(pFile->mmapSize - offset);
48062 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
48064 amt -= nCopy;
48092 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
48094 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
48110 nRem -= nWrite;
48113 pFile->lastErrno = lastErrno;
48119 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
48120 || ( pFile->lastErrno==ERROR_DISK_FULL )){
48122 osGetCurrentProcessId(), pFile, pFile->h));
48123 return winLogError(SQLITE_FULL, pFile->lastErrno,
48124 "winWrite1", pFile->zPath);
48127 osGetCurrentProcessId(), pFile, pFile->h));
48128 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
48129 "winWrite2", pFile->zPath);
48134 osGetCurrentProcessId(), pFile, pFile->h));
48147 if( pFile->nFetchOut>0 ){
48148 /* File truncation is a no-op if there are outstanding memory mapped
48154 ** No real harm comes of this - the database file is not corrupted,
48158 ** The only feasible work-around is to defer the truncation until after
48159 ** all references to memory-mapped content are closed. That is doable,
48162 ** now to simply make trancations a no-op if there are pending reads. We
48172 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
48174 /* If the user has configured a chunk-size for this file, truncate the
48179 if( pFile->szChunk>0 ){
48180 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
48184 if( pFile->pMapRegion ){
48185 oldMmapSize = pFile->mmapSize;
48192 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
48194 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48195 "winTruncate1", pFile->zPath);
48196 }else if( 0==osSetEndOfFile(pFile->h) &&
48198 pFile->lastErrno = lastErrno;
48199 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48200 "winTruncate2", pFile->zPath);
48206 winMapfile(pFile, -1);
48214 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
48260 osGetCurrentProcessId(), pFile, pFile->h, flags,
48261 pFile->locktype));
48273 ** no-op
48276 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48277 osGetCurrentProcessId(), pFile, pFile->h));
48281 if( pFile->pMapRegion ){
48282 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
48283 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48285 pFile, pFile->pMapRegion));
48287 pFile->lastErrno = osGetLastError();
48288 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48290 pFile, pFile->pMapRegion));
48291 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
48292 "winSync1", pFile->zPath);
48296 rc = osFlushFileBuffers(pFile->h);
48300 osGetCurrentProcessId(), pFile, pFile->h));
48303 pFile->lastErrno = osGetLastError();
48305 osGetCurrentProcessId(), pFile, pFile->h));
48306 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
48307 "winSync2", pFile->zPath);
48322 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
48327 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
48331 pFile->lastErrno = osGetLastError();
48332 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48333 "winFileSize", pFile->zPath);
48342 lowerBits = osGetFileSize(pFile->h, &upperBits);
48346 pFile->lastErrno = lastErrno;
48347 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48348 "winFileSize", pFile->zPath);
48353 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
48395 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48402 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
48404 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
48412 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
48413 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48414 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48418 pFile->lastErrno = osGetLastError();
48421 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
48431 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48433 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48437 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48441 pFile->lastErrno = lastErrno;
48442 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
48443 "winUnlockReadLock", pFile->zPath);
48445 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
48450 ** Lock the file with the lock specified by parameter locktype - one
48464 ** UNLOCKED -> SHARED
48465 ** SHARED -> RESERVED
48466 ** SHARED -> (PENDING) -> EXCLUSIVE
48467 ** RESERVED -> (PENDING) -> EXCLUSIVE
48468 ** PENDING -> EXCLUSIVE
48478 int newLocktype; /* Set pFile->locktype to this value before exiting */
48485 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48491 if( pFile->locktype>=locktype ){
48492 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
48496 /* Do not allow any kind of write-lock on a read-only database
48498 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
48504 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
48506 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
48512 newLocktype = pFile->locktype;
48513 if( pFile->locktype==NO_LOCK
48514 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
48517 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48520 ** around problems caused by indexing and/or anti-virus software on
48526 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
48527 pFile->h, cnt, res));
48529 pFile->lastErrno = lastErrno;
48531 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
48532 pFile->h, cnt, sqlite3ErrName(rc)));
48546 assert( pFile->locktype==NO_LOCK );
48558 assert( pFile->locktype==SHARED_LOCK );
48559 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
48577 assert( pFile->locktype>=SHARED_LOCK );
48579 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
48593 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48602 pFile->lastErrno = lastErrno;
48604 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
48605 pFile->h, locktype, newLocktype));
48607 pFile->locktype = (u8)newLocktype;
48609 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48616 ** non-zero, otherwise zero.
48623 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
48626 if( pFile->locktype>=RESERVED_LOCK ){
48628 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
48630 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
48632 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48635 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
48638 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
48639 pFile->h, pResOut, *pResOut));
48648 ** the requested locking level, this routine is a no-op.
48661 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48662 type = pFile->locktype;
48664 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48669 "winUnlock", pFile->zPath);
48673 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48679 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48681 pFile->locktype = (u8)locktype;
48683 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48688 ****************************** No-op Locking **********************************
48694 ** This locking mode is appropriate for use on read-only databases
48695 ** (ex: databases that are burned into CD-ROM, for example.) It can
48722 /******************* End of the no-op lock implementation *********************
48727 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
48729 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
48733 *pArg = (pFile->ctrlFlags & mask)!=0;
48735 pFile->ctrlFlags &= ~mask;
48737 pFile->ctrlFlags |= mask;
48752 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
48755 *(int*)pArg = pFile->locktype;
48756 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48760 *(int*)pArg = (int)pFile->lastErrno;
48761 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48765 pFile->szChunk = *(int *)pArg;
48766 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48770 if( pFile->szChunk>0 ){
48781 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48784 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48789 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48794 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48798 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
48799 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48814 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48819 *phFile = pFile->h;
48820 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48826 HANDLE hOldFile = pFile->h;
48827 pFile->h = *phFile;
48830 hOldFile, pFile->h));
48836 int rc = winGetTempname(pFile->pVfs, &zTFile);
48840 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48853 ** least a 64-bit type. */
48858 *(i64*)pArg = pFile->mmapSizeMax;
48859 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
48860 pFile->mmapSizeMax = newLimit;
48861 if( pFile->mmapSize>0 ){
48863 rc = winMapfile(pFile, -1);
48866 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48871 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
48881 ** if two files are created in the same file-system directory (i.e.
48896 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
48938 ** log-summary, each thread has its own winFile object, but they all
48940 ** log-summary is opened only once per process.
48948 ** The following fields are read-only after the object is created:
48963 int szRegion; /* Size of shared-memory regions */
48965 u8 isReadonly; /* True if read-only */
48994 ** are read-only thereafter:
48999 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
49026 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
49034 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
49036 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
49037 pFile->hFile.h, lockType, ofst, nByte));
49039 /* Release/Acquire the system-level lock */
49041 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
49046 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
49052 pFile->lastErrno = osGetLastError();
49056 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
49057 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
49058 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
49070 ** This is not a VFS shared-memory method; it is a utility function called
49071 ** by VFS shared-memory methods.
49077 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
49081 if( p->nRef==0 ){
49083 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
49084 for(i=0; i<p->nRegion; i++){
49085 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
49086 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
49089 bRc = osCloseHandle(p->aRegion[i].hMap);
49090 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
49094 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
49096 winClose((sqlite3_file *)&p->hFile);
49102 winDelete(pVfs, p->zFilename, 0);
49106 *pp = p->pNext;
49107 sqlite3_free(p->aRegion);
49110 pp = &p->pNext;
49122 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
49128 if( pShmNode->isReadonly ){
49129 pShmNode->isUnlocked = 1;
49132 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
49135 "winLockSharedMemory", pShmNode->zFilename);
49147 ** Open the shared-memory area associated with database file pDbFd.
49149 ** When opening a new shared-memory file, if no other instances of that
49160 assert( pDbFd->pShm==0 ); /* Not previously opened */
49167 nName = sqlite3Strlen30(pDbFd->zPath);
49173 pNew->zFilename = (char*)&pNew[1];
49174 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
49175 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
49181 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
49185 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
49195 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
49196 pShmNode->pNext = winShmNodeList;
49200 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
49201 if( pShmNode->mutex==0 ){
49207 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
49212 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
49213 (sqlite3_file*)&pShmNode->hFile,
49217 pShmNode->zFilename);
49220 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
49227 p->pShmNode = pShmNode;
49229 p->id = pShmNode->nextShmId++;
49231 pShmNode->nRef++;
49232 pDbFd->pShm = p;
49239 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
49242 sqlite3_mutex_enter(pShmNode->mutex);
49243 p->pNext = pShmNode->pFirst;
49244 pShmNode->pFirst = p;
49245 sqlite3_mutex_leave(pShmNode->mutex);
49251 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
49259 ** Close a connection to shared-memory. Delete the underlying
49266 winFile *pDbFd; /* Database holding shared-memory */
49268 winShmNode *pShmNode; /* The underlying shared-memory file */
49272 p = pDbFd->pShm;
49274 pShmNode = p->pShmNode;
49278 sqlite3_mutex_enter(pShmNode->mutex);
49279 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
49280 *pp = p->pNext;
49284 pDbFd->pShm = 0;
49285 sqlite3_mutex_leave(pShmNode->mutex);
49287 /* If pShmNode->nRef has reached 0, then close the underlying
49288 ** shared-memory file, too */
49290 assert( pShmNode->nRef>0 );
49291 pShmNode->nRef--;
49292 if( pShmNode->nRef==0 ){
49293 winShmPurge(pDbFd->pVfs, deleteFlag);
49301 ** Change the lock state for a shared-memory segment.
49310 winShm *p = pDbFd->pShm; /* The shared memory being locked */
49317 pShmNode = p->pShmNode;
49328 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
49330 sqlite3_mutex_enter(pShmNode->mutex);
49335 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49337 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
49338 allMask |= pX->sharedMask;
49341 /* Unlock the system-level locks */
49350 p->exclMask &= ~mask;
49351 p->sharedMask &= ~mask;
49360 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49361 if( (pX->exclMask & mask)!=0 ){
49365 allShared |= pX->sharedMask;
49379 p->sharedMask |= mask;
49385 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49386 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
49398 assert( (p->sharedMask & mask)==0 );
49399 p->exclMask |= mask;
49403 sqlite3_mutex_leave(pShmNode->mutex);
49404 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
49405 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
49420 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
49427 ** shared-memory associated with the database file fd. Shared-memory regions
49428 ** are numbered starting from zero. Each shared-memory region is szRegion
49433 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
49436 ** isWrite is non-zero and the requested shared-memory region has not yet
49439 ** If the shared-memory region has already been allocated or is allocated by
49452 winShm *pShm = pDbFd->pShm;
49461 pShm = pDbFd->pShm;
49464 pShmNode = pShm->pShmNode;
49466 sqlite3_mutex_enter(pShmNode->mutex);
49467 if( pShmNode->isUnlocked ){
49470 pShmNode->isUnlocked = 0;
49472 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
49474 if( pShmNode->nRegion<=iRegion ){
49477 sqlite3_int64 sz; /* Current size of wal-index file */
49479 pShmNode->szRegion = szRegion;
49482 ** Check to see if it has been allocated (i.e. if the wal-index file is
49485 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
49488 "winShmMap1", pDbFd->zPath);
49496 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
49500 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
49503 "winShmMap2", pDbFd->zPath);
49510 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
49516 pShmNode->aRegion = apNew;
49518 if( pShmNode->isReadonly ){
49523 while( pShmNode->nRegion<=iRegion ){
49524 HANDLE hMap = NULL; /* file-mapping handle */
49528 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
49532 hMap = osCreateFileMappingW(pShmNode->hFile.h,
49536 hMap = osCreateFileMappingA(pShmNode->hFile.h,
49540 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
49541 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
49544 int iOffset = pShmNode->nRegion*szRegion;
49548 iOffset - iOffsetShift, szRegion + iOffsetShift
49552 0, iOffset - iOffsetShift, szRegion + iOffsetShift
49555 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
49556 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
49560 pShmNode->lastErrno = osGetLastError();
49561 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
49562 "winShmMap3", pDbFd->zPath);
49567 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
49568 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
49569 pShmNode->nRegion++;
49574 if( pShmNode->nRegion>iRegion ){
49577 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
49582 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
49583 sqlite3_mutex_leave(pShmNode->mutex);
49600 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
49602 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
49603 pFile->mmapSize, pFile->mmapSizeMax));
49604 if( pFile->pMapRegion ){
49605 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
49606 pFile->lastErrno = osGetLastError();
49607 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
49609 pFile->pMapRegion));
49610 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49611 "winUnmapfile1", pFile->zPath);
49613 pFile->pMapRegion = 0;
49614 pFile->mmapSize = 0;
49616 if( pFile->hMap!=NULL ){
49617 if( !osCloseHandle(pFile->hMap) ){
49618 pFile->lastErrno = osGetLastError();
49619 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
49620 osGetCurrentProcessId(), pFile, pFile->hMap));
49621 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49622 "winUnmapfile2", pFile->zPath);
49624 pFile->hMap = NULL;
49626 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49632 ** Memory map or remap the file opened by file-descriptor pFd (if the file
49635 ** outstanding xFetch() references to it, this function is a no-op.
49637 ** If parameter nByte is non-negative, then it is the requested size of
49651 assert( nMap>=0 || pFd->nFetchOut==0 );
49652 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
49655 if( pFd->nFetchOut>0 ) return SQLITE_OK;
49660 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
49665 if( nMap>pFd->mmapSizeMax ){
49666 nMap = pFd->mmapSizeMax;
49668 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
49670 if( nMap==0 && pFd->mmapSize>0 ){
49673 if( nMap!=pFd->mmapSize ){
49680 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
49686 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
49688 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
49692 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
49696 if( pFd->hMap==NULL ){
49697 pFd->lastErrno = osGetLastError();
49698 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49699 "winMapfile1", pFd->zPath);
49701 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
49708 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
49710 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
49713 osCloseHandle(pFd->hMap);
49714 pFd->hMap = NULL;
49715 pFd->lastErrno = osGetLastError();
49716 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49717 "winMapfile2", pFd->zPath);
49719 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
49723 pFd->pMapRegion = pNew;
49724 pFd->mmapSize = nMap;
49727 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49755 if( pFd->mmapSizeMax>0 ){
49756 if( pFd->pMapRegion==0 ){
49757 int rc = winMapfile(pFd, -1);
49764 if( pFd->mmapSize >= iOff+nAmt ){
49765 assert( pFd->pMapRegion!=0 );
49766 *pp = &((u8 *)pFd->pMapRegion)[iOff];
49767 pFd->nFetchOut++;
49778 ** If the third argument is non-NULL, then this function releases a
49794 assert( (p==0)==(pFd->nFetchOut==0) );
49797 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
49803 pFd->nFetchOut--;
49807 ** is unnecessary can be omitted - potentially improving
49812 assert( pFd->nFetchOut>=0 );
49900 ** supports for filenames into UTF-8. Space to hold the result is
49919 ** Convert a UTF-8 filename into whatever form the underlying
49939 ** This function returns non-zero if the specified UTF-8 string buffer
49947 if( winIsDirSep(zBuf[nLen-1]) ){
49986 /* It's odd to simulate an io-error here, but really this is just
49987 ** using the io-error infrastructure to test that SQLite handles this
49995 nMax = pVfs->mxPathname; nBuf = nMax + 2;
49998 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50006 nDir = nMax - (nPre + 15);
50011 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
50017 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50059 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50072 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50080 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
50087 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
50093 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50111 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50117 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50129 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50139 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50144 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
50154 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50168 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50186 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50190 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
50195 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
50201 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
50247 const char *zName, /* Name of the file (UTF-8) */
50264 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
50306 /* The main DB, main journal, WAL file and super-journal are never
50313 /* Assert that the upper layer has set one of the "file-type" flags. */
50322 pFile->h = INVALID_HANDLE_VALUE;
50344 /* Database filenames are double-zero terminated if they are not
50490 pFile->lastErrno = lastErrno;
50491 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
50508 pAppData = (winVfsAppData*)pVfs->pAppData;
50513 && ((pAppData==NULL) || !pAppData->bNoLock)
50519 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
50524 pFile->zDeleteOnClose = zConverted;
50532 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
50533 pFile->pVfs = pVfs;
50534 pFile->h = h;
50536 pFile->ctrlFlags |= WINFILE_RDONLY;
50541 pFile->ctrlFlags |= WINFILE_PSOW;
50543 pFile->lastErrno = NO_ERROR;
50544 pFile->zPath = zName;
50546 pFile->hMap = NULL;
50547 pFile->pMapRegion = 0;
50548 pFile->mmapSize = 0;
50549 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
50705 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
50751 ** Returns non-zero if the specified path name starts with the "long path"
50762 ** Returns non-zero if the specified path name starts with a drive letter
50772 ** Returns non-zero if the specified path name should be used verbatim. If
50773 ** non-zero is returned from this function, the calling function must simply
50774 ** use the provided path name verbatim -OR- resolve it into a full path name
50810 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
50836 assert( nFull>=pVfs->mxPathname );
50844 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50850 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
50860 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50866 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50872 zRelative, zOut, pVfs->mxPathname+1)<0 ){
50882 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
50901 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50904 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
50910 /* It's odd to simulate an io-error here, but really this is just
50911 ** using the io-error infrastructure to test that SQLite handles this
50923 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50984 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
51015 int nFull = pVfs->mxPathname+1;
51090 for(j=0, k=p->i; j<sz; j++){
51091 p->a[k++] ^= x[j];
51092 if( k>=p->na ) k = 0;
51094 p->i = k;
51095 p->nXor += sz;
51166 ** The following variable, if set to a non-zero value, is interpreted as
51185 /* FILETIME structure is a 64-bit value representing the number of
51186 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
51193 /* 2^32 - to avoid use of LL and warnings in gcc */
51243 ** buffer with a nul-terminated UTF-8 encoded error message
51249 ** is zero if the error message fits in the buffer, or non-zero
51250 ** otherwise (if the message was truncated). If non-zero is returned,
51251 ** then it is not necessary to include the nul-terminator character
51308 "win32-longpath", /* zName */
51333 "win32-none", /* zName */
51358 "win32-longpath-none", /* zName */
51379 /* Double-check that the aSyscall[] array has been constructed
51432 ** 2016-09-07
51443 ** This file implements an in-memory VFS. A database is held as a contiguous
51459 /* Access to a lower-level VFS that (might) implement dynamic loading,
51462 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
51522 ** File-scope variables for holding the memdb files that are accessible
51626 sqlite3_mutex_enter(p->pMutex);
51629 sqlite3_mutex_leave(p->pMutex);
51636 ** Close an memdb-file.
51641 MemStore *p = ((MemFile*)pFile)->pStore;
51642 if( p->zFName ){
51651 if( p->nRef==1 ){
51652 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
51665 p->nRef--;
51666 if( p->nRef<=0 ){
51667 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
51668 sqlite3_free(p->aData);
51671 sqlite3_mutex_free(p->pMutex);
51680 ** Read data from an memdb-file.
51688 MemStore *p = ((MemFile*)pFile)->pStore;
51690 if( iOfst+iAmt>p->sz ){
51692 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
51696 memcpy(zBuf, p->aData+iOfst, iAmt);
51706 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
51709 if( newSz>p->szMax ){
51713 if( newSz>p->szMax ) newSz = p->szMax;
51714 pNew = sqlite3Realloc(p->aData, newSz);
51716 p->aData = pNew;
51717 p->szAlloc = newSz;
51722 ** Write data to an memdb-file.
51730 MemStore *p = ((MemFile*)pFile)->pStore;
51732 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51738 if( iOfst+iAmt>p->sz ){
51740 if( iOfst+iAmt>p->szAlloc
51746 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
51747 p->sz = iOfst+iAmt;
51749 memcpy(p->aData+iOfst, z, iAmt);
51755 ** Truncate an memdb-file.
51762 MemStore *p = ((MemFile*)pFile)->pStore;
51765 if( size>p->sz ){
51769 p->sz = size;
51776 ** Sync an memdb-file.
51785 ** Return the current file-size of an memdb-file.
51788 MemStore *p = ((MemFile*)pFile)->pStore;
51790 *pSize = p->sz;
51796 ** Lock an memdb-file.
51800 MemStore *p = pThis->pStore;
51802 if( eLock<=pThis->eLock ) return SQLITE_OK;
51805 assert( p->nWrLock==0 || p->nWrLock==1 );
51806 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
51807 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
51809 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51814 assert( pThis->eLock==SQLITE_LOCK_NONE );
51815 if( p->nWrLock>0 ){
51818 p->nRdLock++;
51825 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51826 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
51827 if( p->nWrLock>0 ){
51830 p->nWrLock = 1;
51838 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51839 if( p->nRdLock>1 ){
51841 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
51842 p->nWrLock = 1;
51848 if( rc==SQLITE_OK ) pThis->eLock = eLock;
51854 ** Unlock an memdb-file.
51858 MemStore *p = pThis->pStore;
51859 if( eLock>=pThis->eLock ) return SQLITE_OK;
51864 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
51865 p->nWrLock--;
51868 if( pThis->eLock>SQLITE_LOCK_SHARED ){
51869 p->nWrLock--;
51871 p->nRdLock--;
51874 pThis->eLock = eLock;
51882 ** occur on an in-memory database.
51892 ** File control method. For custom operations on an memdb-file.
51895 MemStore *p = ((MemFile*)pFile)->pStore;
51899 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
51904 if( iLimit<p->sz ){
51906 iLimit = p->szMax;
51908 iLimit = p->sz;
51911 p->szMax = iLimit;
51921 ** Return the sector-size in bytes for an memdb-file.
51929 ** Return the device characteristic flags supported by an memdb-file.
51939 /* Fetch a page of a memory-mapped file */
51946 MemStore *p = ((MemFile*)pFile)->pStore;
51948 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
51951 p->nMmap++;
51952 *pp = (void*)(p->aData + iOfst);
51958 /* Release a memory-mapped page */
51960 MemStore *p = ((MemFile*)pFile)->pStore;
51964 p->nMmap--;
51993 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
52015 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
52016 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52017 p->zFName = (char*)&p[1];
52018 memcpy(p->zFName, zName, szName+1);
52019 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
52020 if( p->pMutex==0 ){
52021 memdb_g.nMemStore--;
52026 p->nRef = 1;
52030 p->nRef++;
52039 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
52040 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
52042 pFile->pStore = p;
52046 pFd->pMethods = &memdb_io_methods;
52051 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
52055 ** ensure the file-system modifications are synced to disk before
52102 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
52107 ** utf-8 string describing the most recent error encountered associated
52111 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
52118 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
52125 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
52133 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
52141 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
52149 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
52154 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
52157 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
52169 if( p->base.pMethods!=&memdb_io_methods ) return 0;
52170 pStore = p->pStore;
52172 if( pStore->zFName!=0 ) p = 0;
52203 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52206 if( piSize ) *piSize = -1;
52209 MemStore *pStore = p->pStore;
52210 assert( pStore->pMutex==0 );
52211 if( piSize ) *piSize = pStore->sz;
52213 pOut = pStore->aData;
52215 pOut = sqlite3_malloc64( pStore->sz );
52216 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
52220 pBt = db->aDb[iDb].pBt;
52224 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
52243 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
52283 sqlite3_mutex_enter(db->mutex);
52284 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52295 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
52299 db->init.iDb = (u8)iDb;
52300 db->init.reopenMemdb = 1;
52302 db->init.reopenMemdb = 0;
52311 MemStore *pStore = p->pStore;
52312 pStore->aData = pData;
52314 pStore->sz = szDb;
52315 pStore->szAlloc = szBuf;
52316 pStore->szMax = szBuf;
52317 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
52318 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
52320 pStore->mFlags = mFlags;
52329 sqlite3_mutex_leave(db->mutex);
52341 sz = pLower->szOsFile;
52366 ** This file implements an object that represents a fixed-length
52370 ** journalled during a transaction, or which pages have the "dont-write"
52399 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
52416 ** sub-dividing and re-hashing. */
52421 ** no fewer collisions than the no-op *1. */
52441 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
52450 u32 nSet; /* Number of bits that are set - only valid for aHash
52474 p->iSize = iSize;
52480 ** Check to see if the i-th bit is set. Return true or false.
52486 i--;
52487 if( i>=p->iSize ) return 0;
52488 while( p->iDivisor ){
52489 u32 bin = i/p->iDivisor;
52490 i = i%p->iDivisor;
52491 p = p->u.apSub[bin];
52496 if( p->iSize<=BITVEC_NBIT ){
52497 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
52500 while( p->u.aHash[h] ){
52501 if( p->u.aHash[h]==i ) return 1;
52512 ** Set the i-th bit. Return 0 on success and an error code if
52515 ** This routine might cause sub-bitmaps to be allocated. Failing
52516 ** to get the memory needed to hold the sub-bitmap is the only
52527 assert( i<=p->iSize );
52528 i--;
52529 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
52530 u32 bin = i/p->iDivisor;
52531 i = i%p->iDivisor;
52532 if( p->u.apSub[bin]==0 ){
52533 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
52534 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
52536 p = p->u.apSub[bin];
52538 if( p->iSize<=BITVEC_NBIT ){
52539 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
52545 /* worring about sub-dividing and re-hashing. */
52546 if( !p->u.aHash[h] ){
52547 if (p->nSet<(BITVEC_NINT-1)) {
52556 if( p->u.aHash[h]==i ) return SQLITE_OK;
52559 } while( p->u.aHash[h] );
52564 if( p->nSet>=BITVEC_MXHASH ){
52567 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
52571 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52572 memset(p->u.apSub, 0, sizeof(p->u.apSub));
52573 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
52583 p->nSet++;
52584 p->u.aHash[h] = i;
52589 ** Clear the i-th bit.
52597 i--;
52598 while( p->iDivisor ){
52599 u32 bin = i/p->iDivisor;
52600 i = i%p->iDivisor;
52601 p = p->u.apSub[bin];
52606 if( p->iSize<=BITVEC_NBIT ){
52607 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
52611 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52612 memset(p->u.aHash, 0, sizeof(p->u.aHash));
52613 p->nSet = 0;
52616 u32 h = BITVEC_HASH(aiValues[j]-1);
52617 p->nSet++;
52618 while( p->u.aHash[h] ){
52622 p->u.aHash[h] = aiValues[j];
52633 if( p->iDivisor ){
52636 sqlite3BitvecDestroy(p->u.apSub[i]);
52647 return p->iSize;
52689 ** If a memory allocation error occurs, return -1.
52694 int rc = -1;
52717 i = aOp[pc+2] - 1;
52729 if( (--aOp[pc+1]) > 0 ) nx = 0;
52745 ** match (rc==0). Change rc to non-zero if a discrepancy
52750 + (sqlite3BitvecSize(pBitvec) - sz);
52796 ** such that p was added to the list more recently than p->pDirtyNext.
52801 ** page to eject from the cache mid-transaction. It is better to eject
52841 pPg = (PgHdr*)pLower->pExtra;
52842 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
52843 a = (unsigned char *)pLower->pBuf;
52853 if( pCache->pCache==0 ) return;
52857 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
52860 if( ((PgHdr*)pLower)->pPage==0 ){
52861 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
52878 for(p=pCache->pDirty; p; p=p->pDirtyNext){
52898 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
52899 pCache = pPg->pCache;
52901 if( pPg->flags & PGHDR_CLEAN ){
52902 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
52905 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
52906 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
52907 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
52908 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
52912 if( pPg->flags & PGHDR_WRITEABLE ){
52913 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
52951 PCache *p = pPage->pCache;
52955 pPage->pgno));
52957 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
52958 assert( pPage->pDirtyPrev || pPage==p->pDirty );
52961 if( p->pSynced==pPage ){
52962 p->pSynced = pPage->pDirtyPrev;
52965 if( pPage->pDirtyNext ){
52966 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
52968 assert( pPage==p->pDirtyTail );
52969 p->pDirtyTail = pPage->pDirtyPrev;
52971 if( pPage->pDirtyPrev ){
52972 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
52978 assert( pPage==p->pDirty );
52979 p->pDirty = pPage->pDirtyNext;
52980 assert( p->bPurgeable || p->eCreate==2 );
52981 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
52982 assert( p->bPurgeable==0 || p->eCreate==1 );
52983 p->eCreate = 2;
52988 pPage->pDirtyPrev = 0;
52989 pPage->pDirtyNext = p->pDirty;
52990 if( pPage->pDirtyNext ){
52991 assert( pPage->pDirtyNext->pDirtyPrev==0 );
52992 pPage->pDirtyNext->pDirtyPrev = pPage;
52994 p->pDirtyTail = pPage;
52995 if( p->bPurgeable ){
52996 assert( p->eCreate==2 );
52997 p->eCreate = 1;
53000 p->pDirty = pPage;
53006 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
53007 if( !p->pSynced
53008 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
53010 p->pSynced = pPage;
53018 ** being used for an in-memory database, this function is a no-op.
53021 if( p->pCache->bPurgeable ){
53022 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
53023 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
53024 pcacheDump(p->pCache);
53029 ** Compute the number of pages of cache requested. p->szCache is the
53033 if( p->szCache>=0 ){
53034 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
53036 return p->szCache;
53039 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
53043 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
53056 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
53057 ** built-in default page cache is used instead of the application defined
53066 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
53097 p->szPage = 1;
53098 p->szExtra = szExtra;
53100 p->bPurgeable = bPurgeable;
53101 p->eCreate = 2;
53102 p->xStress = xStress;
53103 p->pStress = pStress;
53104 p->szCache = 100;
53105 p->szSpill = 1;
53115 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
53116 if( pCache->szPage ){
53119 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
53120 pCache->bPurgeable
53124 if( pCache->pCache ){
53125 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53127 pCache->pCache = pNew;
53128 pCache->szPage = szPage;
53167 assert( pCache->pCache!=0 );
53169 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
53178 eCreate = createFlag & pCache->eCreate;
53180 assert( createFlag==0 || pCache->eCreate==eCreate );
53181 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
53182 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
53206 if( pCache->eCreate==2 ) return 0;
53208 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
53209 /* Find a dirty page to write-out and recycle. First try to find a
53210 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
53218 for(pPg=pCache->pSynced;
53219 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
53220 pPg=pPg->pDirtyPrev
53222 pCache->pSynced = pPg;
53224 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
53230 "spill page %d making room for %d - cache used: %d/%d",
53231 pPg->pgno, pgno,
53232 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
53235 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
53236 rc = pCache->xStress(pCache->pStress, pPg);
53243 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
53263 pPgHdr = (PgHdr*)pPage->pExtra;
53264 assert( pPgHdr->pPage==0 );
53265 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
53266 pPgHdr->pPage = pPage;
53267 pPgHdr->pData = pPage->pBuf;
53268 pPgHdr->pExtra = (void *)&pPgHdr[1];
53269 memset(pPgHdr->pExtra, 0, 8);
53270 pPgHdr->pCache = pCache;
53271 pPgHdr->pgno = pgno;
53272 pPgHdr->flags = PGHDR_CLEAN;
53290 pPgHdr = (PgHdr *)pPage->pExtra;
53292 if( !pPgHdr->pPage ){
53295 pCache->nRefSum++;
53296 pPgHdr->nRef++;
53306 assert( p->nRef>0 );
53307 p->pCache->nRefSum--;
53308 if( (--p->nRef)==0 ){
53309 if( p->flags&PGHDR_CLEAN ){
53322 assert(p->nRef>0);
53324 p->nRef++;
53325 p->pCache->nRefSum++;
53334 assert( p->nRef==1 );
53336 if( p->flags&PGHDR_DIRTY ){
53339 p->pCache->nRefSum--;
53340 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
53348 assert( p->nRef>0 );
53350 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
53351 p->flags &= ~PGHDR_DONT_WRITE;
53352 if( p->flags & PGHDR_CLEAN ){
53353 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
53354 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
53355 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
53369 assert( (p->flags & PGHDR_DIRTY)!=0 );
53370 assert( (p->flags & PGHDR_CLEAN)==0 );
53372 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53373 p->flags |= PGHDR_CLEAN;
53374 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
53376 if( p->nRef==0 ){
53386 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
53387 while( (p = pCache->pDirty)!=0 ){
53397 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
53398 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53399 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53401 pCache->pSynced = pCache->pDirtyTail;
53409 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53410 p->flags &= ~PGHDR_NEED_SYNC;
53412 pCache->pSynced = pCache->pDirtyTail;
53419 PCache *pCache = p->pCache;
53421 assert( p->nRef>0 );
53424 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
53425 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
53427 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
53428 assert( pXPage->nRef==0 );
53429 pXPage->nRef++;
53430 pCache->nRefSum++;
53433 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
53434 p->pgno = newPgno;
53435 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
53451 if( pCache->pCache ){
53455 for(p=pCache->pDirty; p; p=pNext){
53456 pNext = p->pDirtyNext;
53461 assert( p->pgno>0 );
53462 if( p->pgno>pgno ){
53463 assert( p->flags&PGHDR_DIRTY );
53467 if( pgno==0 && pCache->nRefSum ){
53469 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
53471 ** pCache->nRefSum>0 */
53472 memset(pPage1->pBuf, 0, pCache->szPage);
53476 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
53484 assert( pCache->pCache!=0 );
53486 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53505 if( pA->pgno<pB->pgno ){
53506 pTail->pDirty = pA;
53508 pA = pA->pDirty;
53510 pTail->pDirty = pB;
53514 pTail->pDirty = pB;
53516 pB = pB->pDirty;
53518 pTail->pDirty = pA;
53543 pIn = p->pDirty;
53544 p->pDirty = 0;
53545 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
53554 if( NEVER(i==N_SORT_BUCKET-1) ){
53574 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53575 p->pDirty = p->pDirtyNext;
53577 return pcacheSortDirtyList(pCache->pDirty);
53587 return pCache->nRefSum;
53594 return p->nRef;
53601 assert( pCache->pCache!=0 );
53602 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
53607 ** Get the suggested cache-size value.
53615 ** Set the suggested cache-size value.
53618 assert( pCache->pCache!=0 );
53619 pCache->szCache = mxPage;
53620 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
53625 ** Set the suggested cache-spill value. Make no changes if if the
53626 ** argument is zero. Return the effective cache-spill size, which will
53631 assert( p->pCache!=0 );
53634 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
53636 p->szSpill = mxPage;
53639 if( res<p->szSpill ) res = p->szSpill;
53647 assert( pCache->pCache!=0 );
53648 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
53653 ** in the page-cache hierarchy.
53665 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
53674 return (pCache->pDirty!=0);
53686 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
53714 ** -------------------------------------------------------------
53716 ** -------------------------------------------------------------
53732 ** size can vary according to architecture, compile-time options, and
53738 ** allocations. However, the btree layer needs a small (16-byte) overrun
53753 ** (1) The general-purpose memory allocator - sqlite3Malloc()
53754 ** (2) Global page-cache memory provided using sqlite3_config() with
53756 ** (3) PCache-local bulk allocation.
53766 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
53770 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
53786 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
53796 ** but causes a 2-byte gap in the structure for most architectures (since
53797 ** pointers must be either 4 or 8-byte aligned). As this structure is located
53799 ** corrupt, code at the b-tree layer may overread the page buffer and
53805 ** The pLruNext and pLruPrev pointers form a double-linked circular list
53826 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
53827 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
53855 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
53861 ** open database file (including each in-memory database and each
53876 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
53894 PgHdr1 *pFree; /* List of unused pcache-local pages */
53895 void *pBulk; /* Bulk memory used by pcache-local */
53929 ** reading because (1) most platforms read a 32-bit integer atomically and
53946 # define pcache1EnterMutex(X) assert((X)->mutex==0)
53947 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
53950 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
53951 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
53961 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
53980 while( n-- ){
53982 p->pNext = pcache1.pFree;
53991 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
53992 ** true if pCache->pFree ends up containing one or more free pages.
53999 if( pCache->nMax<3 ) return 0;
54002 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
54004 szBulk = -1024 * (i64)pcache1.nInitPage;
54006 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
54007 szBulk = pCache->szAlloc*(i64)pCache->nMax;
54009 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
54012 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
54014 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
54015 pX->page.pBuf = zBulk;
54016 pX->page.pExtra = &pX[1];
54017 pX->isBulkLocal = 1;
54018 pX->isAnchor = 0;
54019 pX->pNext = pCache->pFree;
54020 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
54021 pCache->pFree = pX;
54022 zBulk += pCache->szAlloc;
54023 }while( --nBulk );
54025 return pCache->pFree!=0;
54044 pcache1.pFree = pcache1.pFree->pNext;
54045 pcache1.nFreeSlot--;
54082 pSlot->pNext = pcache1.pFree;
54129 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54130 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
54131 assert( pCache->pFree!=0 );
54132 p = pCache->pFree;
54133 pCache->pFree = p->pNext;
54134 p->pNext = 0;
54141 assert( pCache->pGroup==&pcache1.grp );
54142 pcache1LeaveMutex(pCache->pGroup);
54145 pPg = pcache1Alloc(pCache->szAlloc);
54148 pcache1EnterMutex(pCache->pGroup);
54151 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
54152 p->page.pBuf = pPg;
54153 p->page.pExtra = &p[1];
54154 p->isBulkLocal = 0;
54155 p->isAnchor = 0;
54156 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
54158 (*pCache->pnPurgeable)++;
54168 pCache = p->pCache;
54169 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
54170 if( p->isBulkLocal ){
54171 p->pNext = pCache->pFree;
54172 pCache->pFree = p;
54174 pcache1Free(p->page.pBuf);
54176 (*pCache->pnPurgeable)--;
54214 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
54235 assert( sqlite3_mutex_held(p->pGroup->mutex) );
54237 nNew = p->nHash*2;
54242 pcache1LeaveMutex(p->pGroup);
54243 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
54245 if( p->nHash ){ sqlite3EndBenignMalloc(); }
54246 pcache1EnterMutex(p->pGroup);
54248 for(i=0; i<p->nHash; i++){
54250 PgHdr1 *pNext = p->apHash[i];
54252 unsigned int h = pPage->iKey % nNew;
54253 pNext = pPage->pNext;
54254 pPage->pNext = apNew[h];
54258 sqlite3_free(p->apHash);
54259 p->apHash = apNew;
54260 p->nHash = nNew;
54267 ** LRU list, then this function is a no-op.
54274 assert( pPage->pLruNext );
54275 assert( pPage->pLruPrev );
54276 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
54277 pPage->pLruPrev->pLruNext = pPage->pLruNext;
54278 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
54279 pPage->pLruNext = 0;
54280 /* pPage->pLruPrev = 0;
54282 assert( pPage->isAnchor==0 );
54283 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
54284 pPage->pCache->nRecyclable--;
54298 PCache1 *pCache = pPage->pCache;
54301 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54302 h = pPage->iKey % pCache->nHash;
54303 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
54304 *pp = (*pp)->pNext;
54306 pCache->nPage--;
54315 PGroup *pGroup = pCache->pGroup;
54317 assert( sqlite3_mutex_held(pGroup->mutex) );
54318 while( pGroup->nPurgeable>pGroup->nMaxPage
54319 && (p=pGroup->lru.pLruPrev)->isAnchor==0
54321 assert( p->pCache->pGroup==pGroup );
54326 if( pCache->nPage==0 && pCache->pBulk ){
54327 sqlite3_free(pCache->pBulk);
54328 pCache->pBulk = pCache->pFree = 0;
54343 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
54345 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54346 assert( pCache->iMaxKey >= iLimit );
54347 assert( pCache->nHash > 0 );
54348 if( pCache->iMaxKey - iLimit < pCache->nHash ){
54353 h = iLimit % pCache->nHash;
54354 iStop = pCache->iMaxKey % pCache->nHash;
54355 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
54359 h = pCache->nHash/2;
54360 iStop = h - 1;
54365 assert( h<pCache->nHash );
54366 pp = &pCache->apHash[h];
54368 if( pPage->iKey>=iLimit ){
54369 pCache->nPage--;
54370 *pp = pPage->pNext;
54374 pp = &pPage->pNext;
54379 h = (h+1) % pCache->nHash;
54381 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
54398 ** private PGroup (mode-1). pcache1.separateCache is false if the single
54399 ** PGroup in pcache1.grp is used for all page caches (mode-2).
54401 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
54403 ** * Use a unified cache in single-threaded applications that have
54404 ** configured a start-time buffer for use as page-cache memory using
54405 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
54408 ** * Otherwise use separate caches (mode-1)
54462 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
54470 pGroup->mxPinned = 10;
54475 if( pGroup->lru.isAnchor==0 ){
54476 pGroup->lru.isAnchor = 1;
54477 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
54479 pCache->pGroup = pGroup;
54480 pCache->szPage = szPage;
54481 pCache->szExtra = szExtra;
54482 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
54483 pCache->bPurgeable = (bPurgeable ? 1 : 0);
54486 pCache->nMin = 10;
54487 pGroup->nMinPage += pCache->nMin;
54488 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54489 pCache->pnPurgeable = &pGroup->nPurgeable;
54491 pCache->pnPurgeable = &pCache->nPurgeableDummy;
54494 if( pCache->nHash==0 ){
54511 if( pCache->bPurgeable ){
54512 PGroup *pGroup = pCache->pGroup;
54515 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
54516 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
54518 pGroup->nMaxPage += (n - pCache->nMax);
54519 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54520 pCache->nMax = n;
54521 pCache->n90pct = pCache->nMax*9/10;
54534 if( pCache->bPurgeable ){
54535 PGroup *pGroup = pCache->pGroup;
54538 savedMaxPage = pGroup->nMaxPage;
54539 pGroup->nMaxPage = 0;
54541 pGroup->nMaxPage = savedMaxPage;
54552 pcache1EnterMutex(pCache->pGroup);
54553 n = pCache->nPage;
54554 pcache1LeaveMutex(pCache->pGroup);
54573 PGroup *pGroup = pCache->pGroup;
54577 assert( pCache->nPage >= pCache->nRecyclable );
54578 nPinned = pCache->nPage - pCache->nRecyclable;
54579 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
54580 assert( pCache->n90pct == pCache->nMax*9/10 );
54582 nPinned>=pGroup->mxPinned
54583 || nPinned>=pCache->n90pct
54584 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
54589 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
54590 assert( pCache->nHash>0 && pCache->apHash );
54593 if( pCache->bPurgeable
54594 && !pGroup->lru.pLruPrev->isAnchor
54595 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
54598 pPage = pGroup->lru.pLruPrev;
54602 pOther = pPage->pCache;
54603 if( pOther->szAlloc != pCache->szAlloc ){
54607 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
54619 unsigned int h = iKey % pCache->nHash;
54620 pCache->nPage++;
54621 pPage->iKey = iKey;
54622 pPage->pNext = pCache->apHash[h];
54623 pPage->pCache = pCache;
54624 pPage->pLruNext = 0;
54625 /* pPage->pLruPrev = 0;
54627 *(void **)pPage->page.pExtra = 0;
54628 pCache->apHash[h] = pPage;
54629 if( iKey>pCache->iMaxKey ){
54630 pCache->iMaxKey = iKey;
54646 ** For a non-purgeable cache (a cache used as the storage for an in-memory
54649 ** a non-purgeable cache.
54692 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
54704 pPage = pCache->apHash[iKey % pCache->nHash];
54705 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
54733 pcache1EnterMutex(pCache->pGroup);
54735 assert( pPage==0 || pCache->iMaxKey>=iKey );
54736 pcache1LeaveMutex(pCache->pGroup);
54750 assert( pCache->bPurgeable || createFlag!=1 );
54751 assert( pCache->bPurgeable || pCache->nMin==0 );
54752 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
54753 assert( pCache->nMin==0 || pCache->bPurgeable );
54754 assert( pCache->nHash>0 );
54756 if( pCache->pGroup->mutex ){
54778 PGroup *pGroup = pCache->pGroup;
54780 assert( pPage->pCache==pCache );
54786 assert( pPage->pLruNext==0 );
54789 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
54793 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
54794 pPage->pLruPrev = &pGroup->lru;
54795 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
54797 pCache->nRecyclable++;
54800 pcache1LeaveMutex(pCache->pGroup);
54816 assert( pPage->iKey==iOld );
54817 assert( pPage->pCache==pCache );
54820 pcache1EnterMutex(pCache->pGroup);
54823 hOld = iOld%pCache->nHash;
54824 pp = &pCache->apHash[hOld];
54826 pp = &(*pp)->pNext;
54828 *pp = pPage->pNext;
54831 hNew = iNew%pCache->nHash;
54832 pPage->iKey = iNew;
54833 pPage->pNext = pCache->apHash[hNew];
54834 pCache->apHash[hNew] = pPage;
54835 if( iNew>pCache->iMaxKey ){
54836 pCache->iMaxKey = iNew;
54839 pcache1LeaveMutex(pCache->pGroup);
54851 pcache1EnterMutex(pCache->pGroup);
54852 if( iLimit<=pCache->iMaxKey ){
54854 pCache->iMaxKey = iLimit-1;
54856 pcache1LeaveMutex(pCache->pGroup);
54866 PGroup *pGroup = pCache->pGroup;
54867 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
54869 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
54870 assert( pGroup->nMaxPage >= pCache->nMax );
54871 pGroup->nMaxPage -= pCache->nMax;
54872 assert( pGroup->nMinPage >= pCache->nMin );
54873 pGroup->nMinPage -= pCache->nMin;
54874 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54877 sqlite3_free(pCache->pBulk);
54878 sqlite3_free(pCache->apHash);
54938 && p->isAnchor==0
54940 nFree += pcache1MemSize(p->page.pBuf);
54964 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
55024 ** a non-zero batch number, it will see all prior INSERTs.
55052 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
55110 p->pChunk = 0;
55111 p->db = db;
55112 p->pEntry = 0;
55113 p->pLast = 0;
55114 p->pForest = 0;
55115 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
55116 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
55117 p->rsFlags = ROWSET_SORTED;
55118 p->iBatch = 0;
55131 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
55132 pNextChunk = pChunk->pNextChunk;
55133 sqlite3DbFree(p->db, pChunk);
55135 p->pChunk = 0;
55136 p->nFresh = 0;
55137 p->pEntry = 0;
55138 p->pLast = 0;
55139 p->pForest = 0;
55140 p->rsFlags = ROWSET_SORTED;
55150 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
55158 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
55163 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
55167 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
55171 pNew->pNextChunk = p->pChunk;
55172 p->pChunk = pNew;
55173 p->pFresh = pNew->aEntry;
55174 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
55176 p->nFresh--;
55177 return p->pFresh++;
55191 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
55195 pEntry->v = rowid;
55196 pEntry->pRight = 0;
55197 pLast = p->pLast;
55199 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
55202 p->rsFlags &= ~ROWSET_SORTED;
55204 pLast->pRight = pEntry;
55206 p->pEntry = pEntry;
55208 p->pLast = pEntry;
55227 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
55228 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
55229 if( pA->v<=pB->v ){
55230 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
55231 pA = pA->pRight;
55233 pTail->pRight = pB;
55237 pTail = pTail->pRight = pB;
55238 pB = pB->pRight;
55240 pTail->pRight = pA;
55258 pNext = pIn->pRight;
55259 pIn->pRight = 0;
55287 if( pIn->pLeft ){
55289 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
55290 p->pRight = pIn;
55294 if( pIn->pRight ){
55295 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
55299 assert( (*ppLast)->pRight==0 );
55322 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
55326 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
55330 pLeft = rowSetNDeepTree(ppList, iDepth-1);
55332 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
55337 p->pLeft = pLeft;
55338 *ppList = p->pRight;
55339 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
55342 *ppList = p->pRight;
55343 p->pLeft = p->pRight = 0;
55359 pList = p->pRight;
55360 p->pLeft = p->pRight = 0;
55364 pList = p->pRight;
55365 p->pLeft = pLeft;
55366 p->pRight = rowSetNDeepTree(&pList, iDepth);
55386 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
55389 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55390 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55391 p->pEntry = rowSetEntrySort(p->pEntry);
55393 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
55397 if( p->pEntry ){
55398 *pRowid = p->pEntry->v;
55399 p->pEntry = p->pEntry->pRight;
55400 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
55415 ** on pRowSet->pEntry, then sort those entries into the forest at
55416 ** pRowSet->pForest so that they can be tested.
55422 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
55427 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
55428 p = pRowSet->pEntry;
55430 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
55431 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55435 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55436 ppPrevTree = &pTree->pRight;
55437 if( pTree->pLeft==0 ){
55438 pTree->pLeft = rowSetListToTree(p);
55442 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
55443 pTree->pLeft = 0;
55450 pTree->v = 0;
55451 pTree->pRight = 0;
55452 pTree->pLeft = rowSetListToTree(p);
55455 pRowSet->pEntry = 0;
55456 pRowSet->pLast = 0;
55457 pRowSet->rsFlags |= ROWSET_SORTED;
55459 pRowSet->iBatch = iBatch;
55465 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55466 p = pTree->pLeft;
55468 if( p->v<iRowid ){
55469 p = p->pRight;
55470 }else if( p->v>iRowid ){
55471 p = p->pLeft;
55517 ** This header file defines the interface to the write-ahead logging
55557 /* Connection to a write-ahead log (WAL) file.
55562 /* Open and close a connection to a write-ahead log. */
55570 ** snapshot is like a read-transaction. It is the state of the database
55579 /* Read a page from the write-ahead log, if it is present. */
55606 Wal *pWal, /* Write-ahead log connection */
55630 /* Return true if the argument is non-NULL and the WAL module is using
55631 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
55632 ** WAL module is using shared-memory, return false.
55646 ** stored in each frame (i.e. the db page-size when the WAL was created).
55713 ** (6) If a super-journal file is used, then all writes to the database file
55714 ** are synced prior to the super-journal being deleted.
55730 ** method is a no-op, but that does not change the fact the SQLite will
55741 ** (11) A database file is well-formed at the beginning and at the conclusion
55765 ** to print out file-descriptors.
55768 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
55771 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
55779 ** OPEN <------+------+
55782 ** +---------> READER-------+ |
55785 ** |<-------WRITER_LOCKED------> ERROR
55788 ** |<------WRITER_CACHEMOD-------->|
55791 ** |<-------WRITER_DBMOD---------->|
55794 ** +<------WRITER_FINISHED-------->+
55799 ** OPEN -> READER [sqlite3PagerSharedLock]
55800 ** READER -> OPEN [pager_unlock]
55802 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
55803 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
55804 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
55805 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
55806 ** WRITER_*** -> READER [pager_end_transaction]
55808 ** WRITER_*** -> ERROR [pager_error]
55809 ** ERROR -> OPEN [pager_unlock]
55815 ** state - the file may or may not be locked and the database size is
55825 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
55826 ** was) in exclusive-locking mode, a user-level read transaction is
55830 ** it opens a read-transaction on the database and returns to state
55831 ** OPEN after the read-transaction is completed. However a connection
55833 ** this state even after the read-transaction is closed. The only way
55837 ** * A read transaction may be active (but a write-transaction cannot).
55839 ** * The dbSize variable may be trusted (even if a user-level read
55843 ** * Even if a read-transaction is not open, it is guaranteed that
55844 ** there is no hot-journal in the file-system.
55848 ** The pager moves to this state from READER when a write-transaction
55850 ** required to start a write-transaction are held, but no actual
55865 ** * If the connection is open in rollback-mode, a RESERVED or greater
55867 ** * If the connection is open in WAL-mode, a WAL write transaction
55906 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
55922 ** The ERROR state is entered when an IO or disk-full error (including
55924 ** difficult to be sure that the in-memory pager state (cache contents,
55925 ** db size etc.) are consistent with the contents of the file-system.
55927 ** Temporary pager files may enter the ERROR state, but in-memory pagers
55931 ** the contents of the page-cache may be left in an inconsistent state.
55943 ** page-cache and any other in-memory state at the same time. Everything
55944 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
55945 ** when a read-transaction is next opened on the pager (transitioning
55961 ** In other cases, the error is returned to the b-tree layer. The b-tree
55965 ** Condition (3) is necessary because it can be triggered by a read-only
55967 ** code were simply returned to the user, the b-tree layer would not
55969 ** read-only statement cannot leave the pager in an internally inconsistent
55975 ** * The pager is not an in-memory pager.
55985 ** state. There are two exceptions: immediately after exclusive-mode has
56001 ** following locking-states, according to the lock currently held on
56009 ** pagerUnlockDb() take a conservative approach - eLock is always updated
56020 ** from ERROR to OPEN state. At this point there may be a hot-journal file
56021 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
56025 ** of hot-journal detection.
56031 ** a hot-journal may be mistaken for a journal being created by an active
56038 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
56039 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
56040 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
56054 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
56056 if( P->xCodec==0 ){ O=(char*)D; }else \
56057 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
56059 # define CODEC1(P,D,N,X,E) /* NO-OP */
56079 ** set to 0. If a journal-header is written into the main journal while
56082 ** journal before the journal-header. This is required during savepoint
56091 Pgno iSubRec; /* Index of first record in sub-journal */
56116 ** For a real on-disk database, the current lock held on the database file -
56119 ** For a temporary or in-memory database (neither of which require any
56131 ** This boolean variable is used to make sure that the change-counter
56132 ** (the 4-byte header field at byte offset 24 of the database file) is
56135 ** It is set to true when the change-counter field is updated, which
56140 ** updating the change-counter is omitted for the current transaction.
56143 ** need only update the change-counter once, for the first transaction
56149 ** (or may not) specify a super-journal name to be written into the
56152 ** Whether or not a journal file contains a super-journal pointer affects
56155 ** If a journal file does not contain a super-journal pointer, it is
56157 ** it does contain a super-journal pointer the journal file is finalized
56161 ** Journal files that contain super-journal pointers cannot be finalized
56162 ** simply by overwriting the first journal-header with zeroes, as the
56163 ** super-journal pointer could interfere with hot-journal rollback of any
56173 ** This variables control the behavior of cache-spills (calls made by
56175 ** to the file-system in order to free up memory).
56187 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
56188 ** is larger than the database page-size in order to prevent a journal sync
56193 ** This is a boolean variable. If true, then any required sub-journal
56194 ** is opened as an in-memory journal file. If false, then in-memory
56195 ** sub-journals are only used for in-memory pager files.
56198 ** write-transaction is opened.
56209 ** is not an integer multiple of the page-size, the value stored in
56210 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
56212 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
56215 ** During a write-transaction, if pages with page-numbers greater than
56226 ** Throughout a write-transaction, dbFileSize contains the size of
56228 ** write-transaction is first opened, and updated when VFS calls are made
56248 ** write-transaction is opened (at the same time as dbFileSize and
56251 ** size-hint passed to the method call. See pager_write_pagelist() for
56259 ** sub-codes.
56285 u8 readOnly; /* True for a read-only database */
56287 u8 memVfs; /* VFS-implemented memory database */
56300 u8 changeCountDone; /* Set after incrementing the change-counter */
56301 u8 setSuper; /* Super-jrnl name is written into jrnl */
56302 u8 doNotSpill; /* Do not spill the cache when non-zero */
56303 u8 subjInMemory; /* True to use in-memory sub-journals */
56311 int nRec; /* Pages journalled since last j-header written */
56312 u32 cksumInit; /* Quasi-random value added to every checksum */
56313 u32 nSubRec; /* Number of records written to sub-journal */
56317 sqlite3_file *sjfd; /* File descriptor for sub-journal */
56330 ** End of the routinely-changing class members
56333 u16 nExtra; /* Add this many bytes to each in-memory page */
56360 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
56361 char *zWal; /* File name for write-ahead log */
56378 ** a non-testing build. These variables are not thread-safe.
56397 ** written, semi-random garbage data might appear in the journal
56404 ** of a 32-bit checksum on each page of data. The checksum covers both
56405 ** the page number and the pPager->pageSize bytes of data for the page.
56406 ** This cksum is initialized to a 32-bit random value that appears in the
56422 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
56428 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
56431 ** The macro MEMDB is true if we are dealing with an in-memory database.
56439 # define MEMDB pPager->memDb
56444 ** interfaces to access the database using memory-mapped I/O.
56447 # define USEFETCH(x) ((x)->bUseFetch)
56454 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
56458 ** if( isOpen(pPager->jfd) ){ ...
56462 ** if( pPager->jfd->pMethods ){ ...
56464 #define isOpen(pFd) ((pFd)->pMethods!=0)
56469 ** by the b-tree layer. This is the case if:
56476 if( pPager->fd->pMethods==0 ) return 0;
56477 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
56479 if( pPager->xCodec!=0 ) return 0;
56482 if( pPager->pWal ){
56485 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
56494 # define pagerUseWal(x) ((x)->pWal!=0)
56516 assert( p->eState==PAGER_OPEN
56517 || p->eState==PAGER_READER
56518 || p->eState==PAGER_WRITER_LOCKED
56519 || p->eState==PAGER_WRITER_CACHEMOD
56520 || p->eState==PAGER_WRITER_DBMOD
56521 || p->eState==PAGER_WRITER_FINISHED
56522 || p->eState==PAGER_ERROR
56525 /* Regardless of the current state, a temp-file connection always behaves
56527 ** the change-counter field, so the changeCountDone flag is always set.
56529 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
56530 assert( p->tempFile==0 || pPager->changeCountDone );
56532 /* If the useJournal flag is clear, the journal-mode must be "OFF".
56533 ** And if the journal-mode is "OFF", the journal file must not be open.
56535 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
56536 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
56538 /* Check that MEMDB implies noSync. And an in-memory journal. Since
56539 ** this means an in-memory pager performs no IO at all, it cannot encounter
56541 ** a journal file. (although the in-memory journal implementation may
56543 ** is therefore not possible for an in-memory pager to enter the ERROR
56547 assert( !isOpen(p->fd) );
56548 assert( p->noSync );
56549 assert( p->journalMode==PAGER_JOURNALMODE_OFF
56550 || p->journalMode==PAGER_JOURNALMODE_MEMORY
56552 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
56559 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
56560 assert( p->eLock!=PENDING_LOCK );
56562 switch( p->eState ){
56565 assert( pPager->errCode==SQLITE_OK );
56566 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
56570 assert( pPager->errCode==SQLITE_OK );
56571 assert( p->eLock!=UNKNOWN_LOCK );
56572 assert( p->eLock>=SHARED_LOCK );
56576 assert( p->eLock!=UNKNOWN_LOCK );
56577 assert( pPager->errCode==SQLITE_OK );
56579 assert( p->eLock>=RESERVED_LOCK );
56581 assert( pPager->dbSize==pPager->dbOrigSize );
56582 assert( pPager->dbOrigSize==pPager->dbFileSize );
56583 assert( pPager->dbOrigSize==pPager->dbHintSize );
56584 assert( pPager->setSuper==0 );
56588 assert( p->eLock!=UNKNOWN_LOCK );
56589 assert( pPager->errCode==SQLITE_OK );
56596 assert( p->eLock>=RESERVED_LOCK );
56597 assert( isOpen(p->jfd)
56598 || p->journalMode==PAGER_JOURNALMODE_OFF
56599 || p->journalMode==PAGER_JOURNALMODE_WAL
56602 assert( pPager->dbOrigSize==pPager->dbFileSize );
56603 assert( pPager->dbOrigSize==pPager->dbHintSize );
56607 assert( p->eLock==EXCLUSIVE_LOCK );
56608 assert( pPager->errCode==SQLITE_OK );
56610 assert( p->eLock>=EXCLUSIVE_LOCK );
56611 assert( isOpen(p->jfd)
56612 || p->journalMode==PAGER_JOURNALMODE_OFF
56613 || p->journalMode==PAGER_JOURNALMODE_WAL
56614 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56616 assert( pPager->dbOrigSize<=pPager->dbHintSize );
56620 assert( p->eLock==EXCLUSIVE_LOCK );
56621 assert( pPager->errCode==SQLITE_OK );
56623 assert( isOpen(p->jfd)
56624 || p->journalMode==PAGER_JOURNALMODE_OFF
56625 || p->journalMode==PAGER_JOURNALMODE_WAL
56626 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56635 assert( pPager->errCode!=SQLITE_OK );
56636 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
56669 , p->zFilename
56670 , p->eState==PAGER_OPEN ? "OPEN" :
56671 p->eState==PAGER_READER ? "READER" :
56672 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
56673 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
56674 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
56675 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
56676 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
56677 , (int)p->errCode
56678 , p->eLock==NO_LOCK ? "NO_LOCK" :
56679 p->eLock==RESERVED_LOCK ? "RESERVED" :
56680 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
56681 p->eLock==SHARED_LOCK ? "SHARED" :
56682 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
56683 , p->exclusiveMode ? "exclusive" : "normal"
56684 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
56685 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
56686 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
56687 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
56688 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
56689 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
56690 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
56691 , p->journalOff, p->journalHdr
56692 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
56711 if( pPager->errCode ){
56712 pPager->xGet = getPageError;
56716 && pPager->xCodec==0
56719 pPager->xGet = getPageMMap;
56722 pPager->xGet = getPageNormal;
56727 ** Return true if it is necessary to write page *pPg into the sub-journal.
56728 ** A page needs to be written into the sub-journal if there exists one
56731 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
56732 ** * The bit corresponding to the page-number is not set in
56736 Pager *pPager = pPg->pPager;
56738 Pgno pgno = pPg->pgno;
56740 for(i=0; i<pPager->nSavepoint; i++){
56741 p = &pPager->aSavepoint[i];
56742 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
56743 for(i=i+1; i<pPager->nSavepoint; i++){
56744 pPager->aSavepoint[i].bTruncateOnRelease = 0;
56757 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
56762 ** Read a 32-bit integer from the given file descriptor. Store the integer
56766 ** All values are stored on disk as big-endian.
56778 ** Write a 32-bit integer into a string buffer in big-endian byte order.
56784 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
56805 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
56808 if( isOpen(pPager->fd) ){
56809 assert( pPager->eLock>=eLock );
56810 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
56811 if( pPager->eLock!=UNKNOWN_LOCK ){
56812 pPager->eLock = (u8)eLock;
56816 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
56834 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
56835 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
56836 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
56837 pPager->eLock = (u8)eLock;
56845 ** This function determines whether or not the atomic-write or
56846 ** atomic-batch-write optimizations can be used with this pager. The
56847 ** atomic-write optimization can be used if:
56857 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
56858 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
56870 assert( isOpen(pPager->fd) );
56871 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
56877 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
56878 return -1;
56884 int nSector = pPager->sectorSize;
56885 int szPage = pPager->pageSize;
56907 ** Return a 32-bit hash of the page data for pPage.
56918 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
56921 pPage->pageHash = pager_pagehash(pPage);
56927 ** that the page is either dirty or still matches the calculated page-hash.
56931 Pager *pPager = pPg->pPager;
56932 assert( pPager->eState!=PAGER_ERROR );
56933 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
56945 ** This function attempts to read a super-journal file name from the
56948 ** used to store a super-journal file name at the end of a journal file.
56952 ** enough space to write the super-journal name). If the super-journal
56954 ** nul-terminator), then this is handled as if no super-journal name
56957 ** If a super-journal file name is present at the end of the journal
56959 ** nul-terminator byte is appended to the buffer following the
56960 ** super-journal file name.
56962 ** If it is determined that no super-journal file name is present
56970 u32 len; /* Length in bytes of super-journal name */
56979 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
56981 || len>szJ-16
56983 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
56984 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
56986 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
56991 /* See if the checksum matches the super-journal name */
56993 cksum -= zSuper[u];
56997 ** containing the super-journal filename is corrupted. This means
56999 ** super-journal filename.
57011 ** following the value in pPager->journalOff, assuming a sector
57012 ** size of pPager->sectorSize bytes.
57017 ** ---------------------------------------
57026 i64 c = pPager->journalOff;
57028 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
57032 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
57039 ** This function is a no-op if the journal file has not been written to
57042 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
57044 ** zero the 28-byte header at the start of the journal file. In either case,
57045 ** if the pager is not in no-sync mode, sync the journal file immediately
57048 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
57059 assert( isOpen(pPager->jfd) );
57060 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
57061 if( pPager->journalOff ){
57062 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
57066 rc = sqlite3OsTruncate(pPager->jfd, 0);
57069 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
57071 if( rc==SQLITE_OK && !pPager->noSync ){
57072 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
57083 rc = sqlite3OsFileSize(pPager->jfd, &sz);
57085 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
57098 ** - 8 bytes: Magic identifying journal format.
57099 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
57100 ** - 4 bytes: Random number used for page hash.
57101 ** - 4 bytes: Initial database page count.
57102 ** - 4 bytes: Sector size used by the process that wrote this journal.
57103 ** - 4 bytes: Database page size.
57105 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
57109 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
57110 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
57114 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57124 for(ii=0; ii<pPager->nSavepoint; ii++){
57125 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
57126 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
57130 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
57133 ** Write the nRec Field - the number of page records that follow this
57136 ** if in full-sync mode), the zero is overwritten with the true number
57146 ** * When the pager is in no-sync mode. Corruption can follow a
57152 assert( isOpen(pPager->fd) || pPager->noSync );
57153 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
57154 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
57162 /* The random check-hash initializer */
57163 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
57164 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
57166 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
57168 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
57171 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
57179 nHeader-(sizeof(aJournalMagic)+20));
57190 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
57193 ** The loop is required here in case the sector-size is larger than the
57199 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
57200 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
57201 assert( pPager->journalHdr <= pPager->journalOff );
57202 pPager->journalOff += nHeader;
57212 ** pPager->journalOff. See comments above function writeJournalHdr() for
57217 ** database before the transaction began, in pages. Also, pPager->cksumInit
57236 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57242 pPager->journalOff = journalHdrOffset(pPager);
57243 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
57246 iHdrOff = pPager->journalOff;
57253 if( isHot || iHdrOff!=pPager->journalHdr ){
57254 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
57263 /* Read the first three 32-bit fields of the journal header: The nRec
57264 ** field, the checksum-initializer and the database size at the start
57267 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
57268 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
57269 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
57274 if( pPager->journalOff==0 ){
57275 u32 iPageSize; /* Page-size field of journal header */
57276 u32 iSectorSize; /* Sector-size field of journal header */
57278 /* Read the page-size and sector-size journal header fields. */
57279 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
57280 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
57285 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
57290 iPageSize = pPager->pageSize;
57293 /* Check that the values read from the page-size and sector-size fields
57300 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
57302 /* If the either the page-size or sector-size in the journal-header is
57303 ** invalid, then the process that wrote the journal-header must have
57310 /* Update the page-size to match the value read from the journal.
57314 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
57317 /* Update the assumed sector-size to match the value used by
57323 pPager->sectorSize = iSectorSize;
57326 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
57332 ** Write the supplied super-journal name into the journal file for pager
57333 ** pPager at the current location. The super-journal name must be the last
57334 ** thing written to a journal file. If the pager is in full-sync mode, the
57339 ** + N bytes: super-journal filename in utf-8.
57340 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
57341 ** + 4 bytes: super-journal name checksum.
57344 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
57345 ** name, where each byte is interpreted as a signed 8-bit integer.
57348 ** this call is a no-op.
57357 assert( pPager->setSuper==0 );
57361 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57362 || !isOpen(pPager->jfd)
57366 pPager->setSuper = 1;
57367 assert( pPager->journalHdr <= pPager->journalOff );
57374 /* If in full-sync mode, advance to the next disk sector before writing
57375 ** the super-journal name. This is in case the previous page written to
57378 if( pPager->fullSync ){
57379 pPager->journalOff = journalHdrOffset(pPager);
57381 iHdrOff = pPager->journalOff;
57383 /* Write the super-journal data to the end of the journal file. If
57386 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
57387 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
57388 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
57389 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
57390 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
57395 pPager->journalOff += (nSuper+20);
57397 /* If the pager is in peristent-journal mode, then the physical
57398 ** journal-file may extend past the end of the super-journal name
57400 ** dangerous because the code to rollback a hot-journal file
57401 ** will not be able to find the super-journal name to determine
57407 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
57408 && jrnlSize>pPager->journalOff
57410 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
57416 ** Discard the entire contents of the in-memory page-cache.
57419 pPager->iDataVersion++;
57420 sqlite3BackupRestart(pPager->pBackup);
57421 sqlite3PcacheClear(pPager->pPCache);
57425 ** Return the pPager->iDataVersion value
57428 return pPager->iDataVersion;
57433 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
57438 for(ii=0; ii<pPager->nSavepoint; ii++){
57439 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
57441 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
57442 sqlite3OsClose(pPager->sjfd);
57444 sqlite3_free(pPager->aSavepoint);
57445 pPager->aSavepoint = 0;
57446 pPager->nSavepoint = 0;
57447 pPager->nSubRec = 0;
57459 for(ii=0; ii<pPager->nSavepoint; ii++){
57460 PagerSavepoint *p = &pPager->aSavepoint[ii];
57461 if( pgno<=p->nOrig ){
57462 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
57471 ** This function is a no-op if the pager is in exclusive mode and not
57475 ** If the pager is not in exclusive-access mode, the database file is
57476 ** completely unlocked. If the file is unlocked and the file-system does
57482 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
57483 ** or not, any journal file left in the file-system will be treated
57484 ** as a hot-journal and rolled back the next time a read-transaction
57489 assert( pPager->eState==PAGER_READER
57490 || pPager->eState==PAGER_OPEN
57491 || pPager->eState==PAGER_ERROR
57494 sqlite3BitvecDestroy(pPager->pInJournal);
57495 pPager->pInJournal = 0;
57499 assert( !isOpen(pPager->jfd) );
57500 sqlite3WalEndReadTransaction(pPager->pWal);
57501 pPager->eState = PAGER_OPEN;
57502 }else if( !pPager->exclusiveMode ){
57504 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
57518 || 1!=(pPager->journalMode & 5)
57520 sqlite3OsClose(pPager->jfd);
57529 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
57530 pPager->eLock = UNKNOWN_LOCK;
57534 ** without clearing the error code. This is intentional - the error
57537 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
57538 pPager->eState = PAGER_OPEN;
57544 ** normal and exclusive-locking mode.
57546 assert( pPager->errCode==SQLITE_OK || !MEMDB );
57547 if( pPager->errCode ){
57548 if( pPager->tempFile==0 ){
57550 pPager->changeCountDone = 0;
57551 pPager->eState = PAGER_OPEN;
57553 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
57555 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57556 pPager->errCode = SQLITE_OK;
57560 pPager->journalOff = 0;
57561 pPager->journalHdr = 0;
57562 pPager->setSuper = 0;
57569 ** the error-code about to be returned by a pager API function. The
57573 ** IOERR sub-codes, the pager enters the ERROR state and the error code
57577 ** The ERROR state indicates that the contents of the pager-cache
57579 ** the contents of the pager-cache. If a transaction was active when
57582 ** it were a hot-journal).
57588 pPager->errCode==SQLITE_FULL ||
57589 pPager->errCode==SQLITE_OK ||
57590 (pPager->errCode & 0xff)==SQLITE_IOERR
57593 pPager->errCode = rc;
57594 pPager->eState = PAGER_ERROR;
57610 ** * For non-TEMP databases, always sync to disk. This is necessary
57619 if( pPager->tempFile==0 ) return 1;
57621 if( !isOpen(pPager->fd) ) return 0;
57622 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
57628 ** after rollback of a hot-journal, or if an error occurs while opening
57629 ** the journal file or writing the very first journal-header of a
57634 ** exclusive than a RESERVED lock, it is a no-op.
57640 ** transaction. Nor will it be considered to be a hot-journal by this
57643 ** the current journal-mode (Pager.journalMode value), as follows:
57647 ** in-memory journal.
57666 ** If running in non-exclusive rollback mode, the lock on the file is
57684 ** is no write-transaction active but a RESERVED or greater lock is
57687 ** 1. After a successful hot-journal rollback, it is called with
57692 ** read-transaction, this function is called with eState==PAGER_READER
57693 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
57696 assert( pPager->eState!=PAGER_ERROR );
57697 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
57702 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
57703 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57705 if( isOpen(pPager->jfd) ){
57709 if( sqlite3JournalIsInMemory(pPager->jfd) ){
57710 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
57711 sqlite3OsClose(pPager->jfd);
57712 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
57713 if( pPager->journalOff==0 ){
57716 rc = sqlite3OsTruncate(pPager->jfd, 0);
57717 if( rc==SQLITE_OK && pPager->fullSync ){
57723 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
57726 pPager->journalOff = 0;
57727 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
57728 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
57730 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
57731 pPager->journalOff = 0;
57734 ** a hot-journal was just rolled back. In this case the journal
57736 ** the database file, it will do so using an in-memory journal.
57738 int bDelete = !pPager->tempFile;
57739 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
57740 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
57741 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57742 || pPager->journalMode==PAGER_JOURNALMODE_WAL
57744 sqlite3OsClose(pPager->jfd);
57746 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
57752 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
57753 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
57756 p->pageHash = 0;
57762 sqlite3BitvecDestroy(pPager->pInJournal);
57763 pPager->pInJournal = 0;
57764 pPager->nRec = 0;
57767 sqlite3PcacheCleanAll(pPager->pPCache);
57769 sqlite3PcacheClearWritable(pPager->pPCache);
57771 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
57775 /* Drop the WAL write-lock, if any. Also, if the connection was in
57779 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
57781 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
57782 /* This branch is taken when committing a transaction in rollback-journal
57788 assert( pPager->eLock==EXCLUSIVE_LOCK );
57789 rc = pager_truncate(pPager, pPager->dbSize);
57793 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
57797 if( !pPager->exclusiveMode
57798 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
57802 pPager->eState = PAGER_READER;
57803 pPager->setSuper = 0;
57814 ** call to pager_unlock() will discard all in-memory pages, unlock
57816 ** means that there is a hot-journal left in the file-system, the next
57826 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
57828 if( pPager->eState>=PAGER_WRITER_LOCKED ){
57832 }else if( !pPager->exclusiveMode ){
57833 assert( pPager->eState==PAGER_READER );
57841 ** Parameter aData must point to a buffer of pPager->pageSize bytes
57843 ** page of data and the current value of pPager->cksumInit.
57846 ** random initial value (pPager->cksumInit) and every 200th byte
57847 ** of the page data, starting with byte offset (pPager->pageSize%200).
57848 ** Each byte is interpreted as an 8-bit unsigned integer.
57860 u32 cksum = pPager->cksumInit; /* Checksum value to return */
57861 int i = pPager->pageSize-200; /* Loop counter */
57864 i -= 200;
57875 if( pPager->xCodecSizeChng ){
57876 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
57877 (int)pPager->nReserve);
57881 # define pagerReportSize(X) /* No-op if we do not support a codec */
57891 if( pDest->nReserve!=pSrc->nReserve ){
57892 pDest->nReserve = pSrc->nReserve;
57900 ** from the sub-journal (if isMainJrnl==0) and playback that page.
57904 ** The main rollback journal uses checksums - the statement journal does
57907 ** If the page number of the page record read from the (sub-)journal file
57917 ** If the page record is successfully read from the (sub-)journal file
57919 ** while reading the record from the (sub-)journal file or while writing
57921 ** is successfully read from the (sub-)journal file but appears to be
57925 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
57939 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
57951 ** the codec. It is false for pure in-memory journals. */
57952 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
57957 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
57958 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
57960 aData = pPager->pTmpSpace;
57966 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
57967 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
57968 ** only reads from the main journal, not the sub-journal.
57970 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
57971 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
57973 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
57975 /* Read the page number and page data from the journal or sub-journal
57978 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
57981 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
57983 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
57994 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
57998 rc = read32bits(jfd, (*pOffset)-4, &cksum);
58014 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
58015 pPager->nReserve = ((u8*)aData)[20];
58022 ** An exception to the above rule: If the database is in no-sync mode
58032 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
58049 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
58059 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
58061 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
58062 (isMainJrnl?"main-journal":"sub-journal")
58065 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
58067 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
58069 if( isOpen(pPager->fd)
58070 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58073 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
58074 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
58078 ** This is usually safe even for an encrypted database - as the data
58080 ** is if the data was just read from an in-memory sub-journal. In that
58086 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
58090 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
58092 if( pgno>pPager->dbFileSize ){
58093 pPager->dbFileSize = pgno;
58095 if( pPager->pBackup ){
58099 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
58103 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
58107 ** the database and the page is not in-memory, there is a potential
58108 ** problem. When the page is next fetched by the b-tree layer, it
58114 ** if the page is on the free-list at the start of the transaction, then
58117 ** The solution is to add an in-memory page to the cache containing
58118 ** the data just read from the sub-journal. Mark the page as dirty
58119 ** and if the pager requires a journal-sync, then mark the page as
58120 ** requiring a journal-sync before it is written.
58123 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
58124 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
58126 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
58127 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
58139 pData = pPg->pData;
58140 memcpy(pData, (u8*)aData, pPager->pageSize);
58141 pPager->xReiniter(pPg);
58151 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
58156 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
58164 ** Parameter zSuper is the name of a super-journal file. A single journal
58165 ** file that referred to the super-journal file has just been rolled back.
58166 ** This routine checks if it is possible to delete the super-journal file,
58172 ** When a super-journal file is created, it is populated with the names
58173 ** of all of its child journals, one after another, formatted as utf-8
58175 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
58178 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
58180 ** A super-journal file may only be deleted once all of its child
58183 ** This function reads the contents of the super-journal file into
58188 ** * if the child journal contains a reference to super-journal
58194 ** the file-system using sqlite3OsDelete().
58202 ** the entire contents of the super-journal file. This could be
58203 ** a couple of kilobytes or so - potentially larger than the page
58207 sqlite3_vfs *pVfs = pPager->pVfs;
58209 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
58210 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
58211 char *zSuperJournal = 0; /* Contents of super-journal file */
58212 i64 nSuperJournal; /* Size of super-journal file */
58214 char *zSuperPtr; /* Space to hold super-journal filename */
58219 ** If successful, open the super-journal file for reading.
58221 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
58228 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
58232 /* Load the entire super-journal file into space obtained from
58234 ** sufficient space (in zSuperPtr) to hold the names of super-journal
58235 ** files extracted from regular rollback-journals.
58239 nSuperPtr = pVfs->mxPathname+1;
58254 while( (zJournal-zSuperJournal)<nSuperJournal ){
58261 /* One of the journals pointed to by the super-journal exists.
58262 ** Open it and check if it points at the super-journal. If
58263 ** so, return without deleting the super-journal file.
58283 /* We have a match. Do not delete the super-journal file. */
58306 ** file in the file-system. This only happens when committing a transaction,
58307 ** or rolling back a transaction (including rolling back a hot-journal).
58310 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
58311 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
58326 assert( pPager->eState!=PAGER_ERROR );
58327 assert( pPager->eState!=PAGER_READER );
58329 if( isOpen(pPager->fd)
58330 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58333 int szPage = pPager->pageSize;
58334 assert( pPager->eLock==EXCLUSIVE_LOCK );
58336 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
58340 rc = sqlite3OsTruncate(pPager->fd, newSize);
58342 char *pTmp = pPager->pTmpSpace;
58344 testcase( (newSize-szPage) == currentSize );
58345 testcase( (newSize-szPage) > currentSize );
58346 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
58347 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
58350 pPager->dbFileSize = nPage;
58358 ** Return a sanitized version of the sector-size of OS file pFile. The
58377 ** super-journal pointers within created journal files.
58381 ** Otherwise, for non-temporary files, the effective sector size is
58388 ** pPager->sectorSize is to define the "blast radius" of bytes that
58396 assert( isOpen(pPager->fd) || pPager->tempFile );
58398 if( pPager->tempFile
58399 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
58405 pPager->sectorSize = 512;
58407 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
58418 ** (2) 4 byte big-endian integer which is the number of valid page records
58421 ** (3) 4 byte big-endian integer which is the initial value for the
58425 ** (5) 4 byte big-endian integer which is the sector size. The header
58427 ** (6) 4 byte big-endian integer which is the page size.
58431 ** + pPager->pageSize bytes of data.
58448 ** no-sync option for the journal. A power failure could lead to corruption
58452 ** If the file opened as the journal file is not a well-formed
58458 ** If an I/O or malloc() error occurs, the journal-file is not deleted
58469 sqlite3_vfs *pVfs = pPager->pVfs;
58476 char *zSuper = 0; /* Name of super-journal file if any */
58479 u32 savedPageSize = pPager->pageSize;
58484 assert( isOpen(pPager->jfd) );
58485 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
58490 /* Read the super-journal name from the journal, if it is present.
58491 ** If a super-journal file name is specified, but the file is not
58497 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
58501 zSuper = pPager->pTmpSpace;
58502 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58510 pPager->journalOff = 0;
58532 ** working in no-sync mode. This means that the rest of the journal
58537 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
58538 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
58556 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
58557 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
58563 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
58568 pPager->dbSize = mxPg;
58569 if( pPager->mxPgno<mxPg ){
58570 pPager->mxPgno = mxPg;
58582 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
58587 pPager->journalOff = szJ;
58613 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
58617 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
58621 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
58625 ** malloc error that occurred after the change-counter was updated but
58626 ** before the transaction was committed, then the change-counter
58629 ** update the change-counter at all. This may lead to cache inconsistency
58633 pPager->changeCountDone = pPager->tempFile;
58636 /* Leave 4 bytes of space before the super-journal filename in memory.
58640 zSuper = &pPager->pTmpSpace[4];
58641 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58645 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58654 /* If there was a super-journal and this routine will return success,
58655 ** see if it is possible to delete the super-journal.
58657 assert( zSuper==&pPager->pTmpSpace[4] );
58658 memset(&zSuper[-4], 0, 4);
58664 nPlayback, pPager->zJournal);
58679 ** pPg->pData. A shared lock or greater must be held on the database
58689 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
58695 assert( pPager->eState>=PAGER_READER && !MEMDB );
58696 assert( isOpen(pPager->fd) );
58699 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame); // find in wal-index
58703 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
58707 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
58708 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
58714 if( pPg->pgno==1 ){
58721 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
58728 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
58730 u8 *dbFileVers = &((u8*)pPg->pData)[24];
58731 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
58734 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
58737 PAGER_INCR(pPager->nRead);
58738 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
58740 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
58746 ** Update the value of the change-counter at offsets 24 and 92 in
58750 ** routine which only updates the change-counter if the update is actually
58751 ** needed, as determined by the pPager->changeCountDone state variable.
58758 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
58759 put32bits(((char*)pPg->pData)+24, change_counter);
58764 put32bits(((char*)pPg->pData)+92, change_counter);
58765 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
58794 pPager->xReiniter(pPg);
58808 sqlite3BackupRestart(pPager->pBackup);
58827 pPager->dbSize = pPager->dbOrigSize;
58828 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
58829 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58831 PgHdr *pNext = pList->pDirty;
58832 rc = pagerUndoCallback((void *)pPager, pList->pgno);
58858 assert( pPager->pWal );
58862 for(p=pList; p && p->pDirty; p=p->pDirty){
58863 assert( p->pgno < p->pDirty->pgno );
58867 assert( pList->pDirty==0 || isCommit );
58875 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
58876 if( p->pgno<=nTruncate ){
58877 ppNext = &p->pDirty;
58885 pPager->aStat[PAGER_STAT_WRITE] += nList;
58887 if( pList->pgno==1 ) pager_write_changecounter(pList);
58888 rc = sqlite3WalFrames(pPager->pWal,
58889 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
58891 if( rc==SQLITE_OK && pPager->pBackup ){
58892 for(p=pList; p; p=p->pDirty){
58893 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
58898 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58899 for(p=pList; p; p=p->pDirty){
58920 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
58927 sqlite3WalEndReadTransaction(pPager->pWal);
58929 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
58932 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
58951 /* Query the WAL sub-system for the database size. The WalDbsize()
58954 ** available from the WAL sub-system if the log file is empty or
58957 assert( pPager->eState==PAGER_OPEN );
58958 assert( pPager->eLock>=SHARED_LOCK );
58959 assert( isOpen(pPager->fd) );
58960 assert( pPager->tempFile==0 );
58961 nPage = sqlite3WalDbsize(pPager->pWal);
58964 ** WAL sub-system, determine the page count based on the size of
58966 ** integer multiple of the page-size, round up the result.
58968 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
58970 int rc = sqlite3OsFileSize(pPager->fd, &n);
58974 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
58981 if( nPage>pPager->mxPgno ){
58982 pPager->mxPgno = (Pgno)nPage;
58991 ** Check if the *-wal file that corresponds to the database opened by pPager
58992 ** exists if the database is not empy, or verify that the *-wal file does
58995 ** If the database is not empty and the *-wal file exists, open the pager
58996 ** in WAL mode. If the database is empty or if no *-wal file exists and
59004 ** a WAL on a none-empty database, this ensures there is no race condition
59010 assert( pPager->eState==PAGER_OPEN );
59011 assert( pPager->eLock>=SHARED_LOCK );
59013 if( !pPager->tempFile ){
59016 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
59025 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
59027 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
59030 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
59031 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
59041 ** the entire super-journal file. The case pSavepoint==NULL occurs when
59045 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
59058 ** * Pages are then played back from the sub-journal file, starting
59072 ** (or transaction). No page with a page-number greater than this value
59077 i64 iHdrOff; /* End of first segment of main-journal records */
59081 assert( pPager->eState!=PAGER_ERROR );
59082 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59086 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
59095 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
59096 pPager->changeCountDone = pPager->tempFile;
59102 /* Use pPager->journalOff as the effective size of the main rollback
59105 ** past pPager->journalOff is off-limits to us.
59107 szJ = pPager->journalOff;
59113 ** greater than the current database size (pPager->dbSize) but those
59118 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
59119 pPager->journalOff = pSavepoint->iOffset;
59120 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
59121 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59125 pPager->journalOff = 0;
59130 ** of the main journal file. Continue to skip out-of-range pages and
59133 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
59141 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
59146 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
59148 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
59150 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
59151 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
59155 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
59157 /* Finally, rollback pages from the sub-journal. Page that were
59159 ** will be skipped. Out-of-range pages are also skipped.
59163 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
59166 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
59168 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
59169 assert( offset==(i64)ii*(4+pPager->pageSize) );
59177 pPager->journalOff = szJ;
59184 ** Change the maximum number of in-memory pages that are allowed
59188 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
59192 ** Change the maximum number of in-memory pages that are allowed
59196 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
59204 sqlite3_file *fd = pPager->fd;
59205 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
59207 sz = pPager->szMmap;
59208 pPager->bUseFetch = (sz>0);
59210 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
59219 pPager->szMmap = szMmap;
59227 sqlite3PcacheShrink(pPager->pPCache);
59249 ** database (with some additional information - the nRec field
59250 ** of the journal header - being written in between the two
59260 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
59271 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
59287 if( pPager->tempFile ){
59288 pPager->noSync = 1;
59289 pPager->fullSync = 0;
59290 pPager->extraSync = 0;
59292 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
59293 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
59294 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
59296 if( pPager->noSync ){
59297 pPager->syncFlags = 0;
59299 pPager->syncFlags = SQLITE_SYNC_FULL;
59301 pPager->syncFlags = SQLITE_SYNC_NORMAL;
59303 pPager->walSyncFlags = (pPager->syncFlags<<2);
59304 if( pPager->fullSync ){
59305 pPager->walSyncFlags |= pPager->syncFlags;
59307 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
59308 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
59311 pPager->doNotSpill &= ~SPILLFLAG_OFF;
59313 pPager->doNotSpill |= SPILLFLAG_OFF;
59355 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
59363 ** The pager invokes the busy-handler if sqlite3OsLock() returns
59364 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
59368 ** (which occurs during hot-journal rollback). Summary:
59371 ** --------------------------------------------------------
59372 ** NO_LOCK -> SHARED_LOCK | Yes
59373 ** SHARED_LOCK -> RESERVED_LOCK | No
59374 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
59375 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
59377 ** If the busy-handler callback returns non-zero, the lock is
59383 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
59387 pPager->xBusyHandler = xBusyHandler;
59388 pPager->pBusyHandlerArg = pBusyHandlerArg;
59389 ap = (void **)&pPager->xBusyHandler;
59392 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
59400 ** is a no-op. The value returned is the error state error code (i.e.
59401 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
59410 ** * the database is either not an in-memory database or it is
59411 ** an in-memory database that currently consists of zero pages.
59435 ** is a no-op for that case anyhow.
59440 if( (pPager->memDb==0 || pPager->dbSize==0)
59441 && sqlite3PcacheRefCount(pPager->pPCache)==0
59442 && pageSize && pageSize!=(u32)pPager->pageSize
59447 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
59448 rc = sqlite3OsFileSize(pPager->fd, &nByte);
59451 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
59463 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
59466 sqlite3PageFree(pPager->pTmpSpace);
59467 pPager->pTmpSpace = pNew;
59468 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
59469 pPager->pageSize = pageSize;
59470 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
59476 *pPageSize = pPager->pageSize;
59478 if( nReserve<0 ) nReserve = pPager->nReserve;
59480 pPager->nReserve = (i16)nReserve;
59495 return pPager->pTmpSpace;
59507 pPager->mxPgno = mxPage;
59509 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
59510 /* assert( pPager->mxPgno>=pPager->dbSize ); */
59514 return pPager->mxPgno;
59522 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
59531 sqlite3_io_error_pending = -1;
59558 assert( isOpen(pPager->fd) || pPager->tempFile );
59566 if( isOpen(pPager->fd) ){
59568 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
59577 ** This function may only be called when a read-transaction is open on
59580 ** However, if the file is between 1 and <page-size> bytes in size, then
59584 assert( pPager->eState>=PAGER_READER );
59585 assert( pPager->eState!=PAGER_WRITER_FINISHED );
59586 *pnPage = (int)pPager->dbSize;
59592 ** a similar or greater lock is already held, this function is a no-op
59607 /* Check that this is either a no-op (because the requested lock is
59608 ** already held), or one of the transitions that the busy-handler
59612 assert( (pPager->eLock>=locktype)
59613 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
59614 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
59619 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
59625 ** following is true for all dirty pages currently in the page-cache:
59631 ** be necessary to write the current content out to the sub-journal.
59640 ** sub-journal rolled back the content could not be restored and the
59646 Pager *pPager = pPg->pPager;
59647 assert( pPg->flags&PGHDR_DIRTY );
59648 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
59649 Pgno pgno = pPg->pgno;
59651 for(i=0; i<pPg->pPager->nSavepoint; i++){
59652 PagerSavepoint *p = &pPager->aSavepoint[i];
59653 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
59658 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
59665 ** Truncate the in-memory database file image to nPage pages. This
59676 assert( pPager->dbSize>=nPage || CORRUPT_DB );
59677 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
59678 pPager->dbSize = nPage;
59693 ** This function is called before attempting a hot-journal rollback. It
59694 ** syncs the journal file to disk, then sets pPager->journalHdr to the
59698 ** Syncing a hot-journal to disk before attempting to roll it back ensures
59699 ** that if a power-failure occurs during the rollback, the process that
59708 if( !pPager->noSync ){
59709 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
59712 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
59736 if( pPager->pMmapFreelist ){
59737 *ppPage = p = pPager->pMmapFreelist;
59738 pPager->pMmapFreelist = p->pDirty;
59739 p->pDirty = 0;
59740 assert( pPager->nExtra>=8 );
59741 memset(p->pExtra, 0, 8);
59743 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
59745 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
59748 p->pExtra = (void *)&p[1];
59749 p->flags = PGHDR_MMAP;
59750 p->nRef = 1;
59751 p->pPager = pPager;
59754 assert( p->pExtra==(void *)&p[1] );
59755 assert( p->pPage==0 );
59756 assert( p->flags==PGHDR_MMAP );
59757 assert( p->pPager==pPager );
59758 assert( p->nRef==1 );
59760 p->pgno = pgno;
59761 p->pData = pData;
59762 pPager->nMmapOut++;
59773 Pager *pPager = pPg->pPager;
59774 pPager->nMmapOut--;
59775 pPg->pDirty = pPager->pMmapFreelist;
59776 pPager->pMmapFreelist = pPg;
59778 assert( pPager->fd->pMethods->iVersion>=3 );
59779 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
59788 for(p=pPager->pMmapFreelist; p; p=pNext){
59789 pNext = p->pDirty;
59796 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
59803 if( pPager->tempFile ) return SQLITE_OK;
59804 if( pPager->dbSize==0 ) return SQLITE_OK;
59805 assert( pPager->zFilename && pPager->zFilename[0] );
59806 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
59808 /* If the HAS_MOVED file-control is unimplemented, assume that the file
59834 u8 *pTmp = (u8*)pPager->pTmpSpace;
59840 /* pPager->errCode = 0; */
59841 pPager->exclusiveMode = 0;
59845 assert( db || pPager->pWal==0 );
59846 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
59851 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
59852 pPager->pWal = 0;
59867 ** back or finalize it. The next database user will have to do hot-journal
59870 if( isOpen(pPager->jfd) ){
59879 sqlite3OsClose(pPager->jfd);
59880 sqlite3OsClose(pPager->fd);
59882 sqlite3PcacheClose(pPager->pPCache);
59885 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
59888 assert( !pPager->aSavepoint && !pPager->pInJournal );
59889 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
59900 return pPg->pgno;
59914 ** disk and can be restored in the event of a hot-journal rollback.
59916 ** If the Pager.noSync flag is set, then this function is a no-op.
59917 ** Otherwise, the actions required depend on the journal-mode and the
59918 ** device characteristics of the file-system, as follows:
59920 ** * If the journal file is an in-memory journal file, no action need
59926 ** been written following it. If the pager is operating in full-sync
59932 ** Or, in pseudo-code:
59934 ** if( NOT <in-memory journal> ){
59936 ** if( <full-sync mode> ) xSync(<journal file>);
59949 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59950 || pPager->eState==PAGER_WRITER_DBMOD
59959 if( !pPager->noSync ){
59960 assert( !pPager->tempFile );
59961 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
59962 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
59963 assert( isOpen(pPager->jfd) );
59967 ** that wrote to this database was operating in persistent-journal
59970 ** file happens to be a journal-header (written as part of the
59971 ** previous connection's transaction), and a crash or power-failure
59975 ** hot-journal rollback following recovery. It may roll back all
59977 ** out-of-date data that follows it. Database corruption.
59993 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
59996 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
59999 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
60006 ** full-synchronous mode, sync the journal first. This ensures that
60016 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
60019 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
60022 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
60024 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
60031 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
60032 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
60037 pPager->journalHdr = pPager->journalOff;
60039 pPager->nRec = 0;
60044 pPager->journalHdr = pPager->journalOff;
60052 sqlite3PcacheClearSyncFlags(pPager->pPCache);
60053 pPager->eState = PAGER_WRITER_DBMOD;
60061 ** in-memory pages in the list to the database file. The argument may
60063 ** a no-op.
60070 ** If the pager is a temp-file pager and the actual file-system file
60095 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
60096 assert( pPager->eLock==EXCLUSIVE_LOCK );
60097 assert( isOpen(pPager->fd) || pList->pDirty==0 );
60099 /* If the file is a temp-file has not yet been opened, open it now. It
60101 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
60103 if( !isOpen(pPager->fd) ){
60104 assert( pPager->tempFile && rc==SQLITE_OK );
60105 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
60111 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
60113 && pPager->dbHintSize<pPager->dbSize
60114 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
60116 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
60117 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
60118 pPager->dbHintSize = pPager->dbSize;
60122 Pgno pgno = pList->pgno;
60126 ** make the file smaller (presumably by auto-vacuum code). Do not write
60132 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
60133 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
60136 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
60137 if( pList->pgno==1 ) pager_write_changecounter(pList);
60140 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
60143 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
60150 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
60152 if( pgno>pPager->dbFileSize ){
60153 pPager->dbFileSize = pgno;
60155 pPager->aStat[PAGER_STAT_WRITE]++;
60158 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
60168 pList = pList->pDirty;
60175 ** Ensure that the sub-journal file is open. If it is already open, this
60176 ** function is a no-op.
60184 if( !isOpen(pPager->sjfd) ){
60189 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
60190 nStmtSpill = -1;
60192 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
60198 ** Append a record of the current state of page pPg to the sub-journal.
60200 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
60204 ** error code if the attempt to write to the sub-journal fails, or
60210 Pager *pPager = pPg->pPager;
60211 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
60213 /* Open the sub-journal, if it has not already been opened */
60214 assert( pPager->useJournal );
60215 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
60216 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
60219 || pPg->pgno>pPager->dbOrigSize
60223 /* If the sub-journal was opened successfully (or was already open),
60226 void *pData = pPg->pData;
60227 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
60231 if( !pPager->subjInMemory ){
60232 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
60236 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
60237 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
60239 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
60244 pPager->nSubRec++;
60245 assert( pPager->nSavepoint>0 );
60246 rc = addToSavepointBitvecs(pPager, pPg->pgno);
60261 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
60281 assert( pPg->pPager==pPager );
60282 assert( pPg->flags&PGHDR_DIRTY );
60300 if( NEVER(pPager->errCode) ) return SQLITE_OK;
60301 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
60302 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
60303 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
60304 if( pPager->doNotSpill
60305 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
60306 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
60311 pPager->aStat[PAGER_STAT_SPILL]++;
60312 pPg->pDirty = 0;
60322 if( pPager->tempFile==0 ){
60323 rc = sqlite3JournalCreate(pPager->jfd);
60329 if( pPg->flags&PGHDR_NEED_SYNC
60330 || pPager->eState==PAGER_WRITER_CACHEMOD
60337 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
60344 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
60355 int rc = pPager->errCode;
60357 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
60360 PgHdr *pNext = pList->pDirty;
60361 if( pList->nRef==0 ){
60377 ** If zFilename is NULL then a randomly-named temporary file is created
60381 ** This can be used to implement an in-memory database.
60407 int nExtra, /* Extra bytes append to each in-memory page */
60415 int tempFile = 0; /* True for temp files (incl. in-memory files) */
60416 int memDb = 0; /* True if this is an in-memory file */
60422 int readOnly = 0; /* True if this is a read-only file */
60433 /* Figure out how much space is required for each journal file-handle
60434 ** (there are two of them, the main journal and the sub-journal). */
60458 nPathname = pVfs->mxPathname+1;
60481 nUriByte = (int)(&z[1] - zUri);
60483 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
60485 ** the database being opened will be more than pVfs->mxPathname
60488 ** check for a hot-journal before reading.
60504 ** Database file handle (pVfs->szOsFile bytes)
60505 ** Sub-journal file handle (journalFileSize bytes)
60515 ** Some 3rd-party software, over which we have no control, depends on
60519 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
60522 ** filename format expected by 3rd-party software should be as follows:
60524 ** - Main Database Path
60525 ** - \0
60526 ** - Multiple URI components consisting of:
60527 ** - Key
60528 ** - \0
60529 ** - Value
60530 ** - \0
60531 ** - \0
60532 ** - Journal Path
60533 ** - \0
60534 ** - WAL Path (zWALName)
60535 ** - \0
60545 ROUND8(pVfs->szOsFile) + /* The main db file */
60563 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
60564 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
60565 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60566 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60567 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
60572 pPager->zFilename = (char*)pPtr;
60585 pPager->zJournal = (char*)pPtr;
60587 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
60589 sqlite3FileSuffix3(zFilename,pPager->zJournal);
60590 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
60593 pPager->zJournal = 0;
60599 pPager->zWal = (char*)pPtr;
60601 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
60603 sqlite3FileSuffix3(zFilename, pPager->zWal);
60604 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
60607 pPager->zWal = 0;
60613 pPager->pVfs = pVfs;
60614 pPager->vfsFlags = vfsFlags;
60620 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
60623 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
60636 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
60640 if( szPageDflt<pPager->sectorSize ){
60641 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
60644 szPageDflt = (u32)pPager->sectorSize;
60661 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
60663 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
60673 ** This branch is also run for an in-memory database. An in-memory
60674 ** database is the same as a temp-file that is never written out to
60675 ** disk and uses an in-memory rollback journal.
60681 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
60682 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
60683 pPager->noLock = 1; /* Do no locking */
60691 assert( pPager->memDb==0 );
60692 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
60701 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
60707 sqlite3OsClose(pPager->fd);
60708 sqlite3PageFree(pPager->pTmpSpace);
60713 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
60714 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
60716 pPager->useJournal = (u8)useJournal;
60717 /* pPager->stmtOpen = 0; */
60718 /* pPager->stmtInUse = 0; */
60719 /* pPager->nRef = 0; */
60720 /* pPager->stmtSize = 0; */
60721 /* pPager->stmtJSize = 0; */
60722 /* pPager->nPage = 0; */
60723 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
60724 /* pPager->state = PAGER_UNLOCK; */
60725 /* pPager->errMask = 0; */
60726 pPager->tempFile = (u8)tempFile;
60730 pPager->exclusiveMode = (u8)tempFile;
60731 pPager->changeCountDone = pPager->tempFile;
60732 pPager->memDb = (u8)memDb;
60733 pPager->readOnly = (u8)readOnly;
60734 assert( useJournal || pPager->tempFile );
60735 pPager->noSync = pPager->tempFile;
60736 if( pPager->noSync ){
60737 assert( pPager->fullSync==0 );
60738 assert( pPager->extraSync==0 );
60739 assert( pPager->syncFlags==0 );
60740 assert( pPager->walSyncFlags==0 );
60742 pPager->fullSync = 1;
60743 pPager->extraSync = 0;
60744 pPager->syncFlags = SQLITE_SYNC_NORMAL;
60745 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
60747 /* pPager->pFirst = 0; */
60748 /* pPager->pFirstSynced = 0; */
60749 /* pPager->pLast = 0; */
60750 pPager->nExtra = (u16)nExtra;
60751 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
60752 assert( isOpen(pPager->fd) || tempFile );
60755 pPager->journalMode = PAGER_JOURNALMODE_OFF;
60757 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
60759 /* pPager->xBusyHandler = 0; */
60760 /* pPager->pBusyHandlerArg = 0; */
60761 pPager->xReiniter = xReinit;
60763 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
60764 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
60777 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
60778 zName--;
60780 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
60781 return pPager->fd;
60788 ** the file-system for the given pager. A hot journal is one that
60789 ** needs to be played back. According to this function, a hot-journal
60803 ** This routine does not check if there is a super-journal filename
60804 ** at the end of the file. If there is, and that super-journal file
60806 ** case this routine will return a false-positive. The pager_playback()
60810 ** If a hot-journal file is found to exist, *pExists is set to 1 and
60811 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
60813 ** to determine whether or not a hot-journal file exists, the IO error
60817 sqlite3_vfs * const pVfs = pPager->pVfs;
60820 int jrnlOpen = !!isOpen(pPager->jfd);
60822 assert( pPager->useJournal );
60823 assert( isOpen(pPager->fd) );
60824 assert( pPager->eState==PAGER_OPEN );
60826 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
60832 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
60842 ** in fact there is none. This results in a false-positive which will
60845 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
60849 assert( pPager->tempFile==0 );
60863 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
60864 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
60871 ** at least one non-zero bytes at the start of the journal file.
60877 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
60881 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
60886 sqlite3OsClose(pPager->jfd);
60913 ** has been successfully called. If a shared-lock is already held when
60914 ** this function is called, it is a no-op.
60921 ** the SHARED lock, the file-system is checked for a hot-journal,
60922 ** which is played back if present. Following any hot-journal
60924 ** the 'change-counter' field of the database file header and
60927 ** 2) If the pager is running in exclusive-mode, and there are currently
60934 ** occurs while locking the database, checking for a hot-journal file or
60940 /* This routine is only called from b-tree and only when there are no
60944 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
60946 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
60947 assert( pPager->errCode==SQLITE_OK );
60949 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
60950 int bHotJournal = 1; /* True if there exists a hot journal-file */
60953 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
60958 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
60965 if( pPager->eLock<=SHARED_LOCK ){
60972 if( pPager->readOnly ){
60982 ** hot-journal back.
61000 ** in exclusive-access mode the file descriptor will be kept open
61001 ** and possibly used for a transaction later on. Also, write-access
61008 ** may mean that the pager was in the error-state when this
61011 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61012 sqlite3_vfs * const pVfs = pPager->pVfs;
61015 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
61019 assert( !pPager->tempFile );
61020 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
61021 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61024 sqlite3OsClose(pPager->jfd);
61031 ** playing back the hot-journal so that we don't end up with
61037 if( isOpen(pPager->jfd) ){
61041 rc = pager_playback(pPager, !pPager->tempFile);
61042 pPager->eState = PAGER_OPEN;
61044 }else if( !pPager->exclusiveMode ){
61050 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
61069 assert( pPager->eState==PAGER_OPEN );
61070 assert( (pPager->eLock==SHARED_LOCK)
61071 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
61075 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
61076 /* The shared-lock has just been acquired then check to
61080 ** single unnecessary sqlite3OsRead() call at the start-up.
61084 ** a 32-bit counter that is incremented with each change. The
61092 char dbFileVers[sizeof(pPager->dbFileVers)];
61095 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
61103 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
61113 sqlite3OsUnfetch(pPager->fd, 0, 0);
61118 /* If there is a WAL file in the file-system, open this database in WAL
61119 ** mode. Otherwise, the following function call is a no-op.
61123 assert( pPager->pWal==0 || rc==SQLITE_OK );
61132 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
61133 rc = pagerPagecount(pPager, &pPager->dbSize);
61140 assert( pPager->eState==PAGER_OPEN );
61142 pPager->eState = PAGER_READER;
61143 pPager->hasHeldSharedLock = 1;
61154 ** nothing to rollback, so this routine is a no-op.
61157 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
61158 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
61171 ** getPageNormal() -- The normal getter
61172 ** getPageError() -- Used if the pager is in an error state
61173 ** getPageMmap() -- Used if memory-mapped I/O is enabled
61195 ** a) When reading a free-list leaf page from the database, and
61213 ** to find a page in the in-memory cache first. If the page is not already
61215 ** just returns 0. This routine acquires a read-lock the first time it
61231 assert( pPager->errCode==SQLITE_OK );
61232 assert( pPager->eState>=PAGER_READER );
61234 assert( pPager->hasHeldSharedLock==1 );
61238 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPager->dbSize, 0, CORRUPT_TYPE_UNKOWN,
61239 -1, 0, zMsg, NULL);
61242 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
61245 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
61252 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
61254 assert( pPg->pgno==pgno );
61255 assert( pPg->pPager==pPager || pPg->pPager==0 );
61258 if( pPg->pPager && !noContent ){
61262 pPager->aStat[PAGER_STAT_HIT]++;
61275 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPager->dbSize, pgno, CORRUPT_TYPE_UNKOWN,
61276 -1, 0, zMsg, NULL);
61281 pPg->pPager = pPager;
61283 assert( !isOpen(pPager->fd) || !MEMDB );
61284 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
61285 if( pgno>pPager->mxPgno ){
61290 /* Failure to set the bits in the InJournal bit-vectors is benign.
61297 if( pgno<=pPager->dbOrigSize ){
61298 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
61305 memset(pPg->pData, 0, pPager->pageSize);
61308 assert( pPg->pPager==pPager );
61309 pPager->aStat[PAGER_STAT_MISS]++;
61330 /* The page getter for when memory-mapped I/O is enabled */
61341 /* It is acceptable to use a read-only (mmap) page for any page except
61342 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
61344 ** temporary or in-memory database. */
61346 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
61351 assert( pPager->xCodec==0 );
61360 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPager->dbSize, 0, CORRUPT_TYPE_UNKOWN,
61361 -1, 0, zMsg, NULL);
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);
61467 TESTONLY( Pager *pPager = pPg->pPager; )
61469 if( pPg->flags & PGHDR_MMAP ){
61470 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
61476 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
61484 assert( pPg->pgno==1 );
61485 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
61486 pPager = pPg->pPager;
61497 ** to the start of it. If there are active savepoints, open the sub-journal
61515 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
61517 assert( pPager->eState==PAGER_WRITER_LOCKED );
61519 assert( pPager->pInJournal==0 );
61521 /* If already in the error state, this function is a no-op. But on
61524 if( NEVER(pPager->errCode) ) return pPager->errCode;
61526 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61527 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
61528 if( pPager->pInJournal==0 ){
61533 if( !isOpen(pPager->jfd) ){
61534 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
61535 sqlite3MemJournalOpen(pPager->jfd);
61540 if( pPager->tempFile ){
61554 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
61558 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61563 ** the sub-journal if necessary.
61567 pPager->nRec = 0;
61568 pPager->journalOff = 0;
61569 pPager->setSuper = 0;
61570 pPager->journalHdr = 0;
61576 sqlite3BitvecDestroy(pPager->pInJournal);
61577 pPager->pInJournal = 0;
61578 pPager->journalOff = 0;
61580 assert( pPager->eState==PAGER_WRITER_LOCKED );
61581 pPager->eState = PAGER_WRITER_CACHEMOD;
61588 ** Begin a write-transaction on the specified pager object. If a
61589 ** write-transaction has already been opened, this function is a no-op.
61596 ** If the subjInMemory argument is non-zero, then any sub-journal opened
61597 ** within this transaction will be opened as an in-memory file. This
61598 ** has no effect if the sub-journal is already opened (as it may be when
61600 ** sub-journal. If the subjInMemory argument is zero, then any required
61601 ** sub-journal is implemented in-memory if pPager is an in-memory database,
61607 if( pPager->errCode ) return pPager->errCode;
61608 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
61609 pPager->subjInMemory = (u8)subjInMemory;
61611 if( pPager->eState==PAGER_READER ){
61612 assert( pPager->pInJournal==0 );
61618 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
61624 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
61629 ** The busy-handler is not invoked if another connection already
61630 ** holds the write-lock. If possible, the upper layer will call it.
61632 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
61637 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
61654 ** transactions may copy data from the sub-journal into the database
61658 pPager->eState = PAGER_WRITER_LOCKED;
61659 pPager->dbHintSize = pPager->dbSize;
61660 pPager->dbFileSize = pPager->dbSize;
61661 pPager->dbOrigSize = pPager->dbSize;
61662 pPager->journalOff = 0;
61665 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
61666 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
61678 Pager *pPager = pPg->pPager;
61682 i64 iOff = pPager->journalOff;
61687 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
61689 assert( pPager->journalHdr<=pPager->journalOff );
61690 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
61694 ** page in the block above, set the need-sync flag for the page.
61700 pPg->flags |= PGHDR_NEED_SYNC;
61702 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
61704 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
61706 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
61709 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
61710 pPager->journalOff, pPager->pageSize));
61713 PAGERID(pPager), pPg->pgno,
61714 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
61716 pPager->journalOff += 8 + pPager->pageSize;
61717 pPager->nRec++;
61718 assert( pPager->pInJournal!=0 );
61719 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
61722 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
61729 ** main journal or sub-journal as required. If the page is written into
61735 Pager *pPager = pPg->pPager;
61738 /* This routine is not called unless a write-transaction has already
61742 assert( pPager->eState==PAGER_WRITER_LOCKED
61743 || pPager->eState==PAGER_WRITER_CACHEMOD
61744 || pPager->eState==PAGER_WRITER_DBMOD
61747 assert( pPager->errCode==0 );
61748 assert( pPager->readOnly==0 );
61752 ** obtained the necessary locks to begin the write-transaction, but the
61760 if( pPager->eState==PAGER_WRITER_LOCKED ){
61764 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
61774 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
61775 if( pPager->pInJournal!=0
61776 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
61779 if( pPg->pgno<=pPager->dbOrigSize ){
61785 if( pPager->eState!=PAGER_WRITER_DBMOD ){
61786 pPg->flags |= PGHDR_NEED_SYNC;
61789 PAGERID(pPager), pPg->pgno,
61790 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
61794 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
61799 pPg->flags |= PGHDR_WRITEABLE;
61804 if( pPager->nSavepoint>0 ){
61809 if( pPager->dbSize<pPg->pgno ){
61810 pPager->dbSize = pPg->pgno;
61833 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
61834 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
61841 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
61842 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
61844 /* This trick assumes that both the page-size and sector-size are
61848 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
61850 nPageCount = pPager->dbSize;
61851 if( pPg->pgno>nPageCount ){
61852 nPage = (pPg->pgno - pg1)+1;
61853 }else if( (pg1+nPagePerSector-1)>nPageCount ){
61854 nPage = nPageCount+1-pg1;
61859 assert(pg1<=pPg->pgno);
61860 assert((pg1+nPage)>pPg->pgno);
61865 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
61870 if( pPage->flags&PGHDR_NEED_SYNC ){
61877 if( pPage->flags&PGHDR_NEED_SYNC ){
61895 pPage->flags |= PGHDR_NEED_SYNC;
61901 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
61902 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
61914 ** fit on a single disk sector. In this case all co-resident pages
61921 Pager *pPager = pPg->pPager;
61922 assert( (pPg->flags & PGHDR_MMAP)==0 );
61923 assert( pPager->eState>=PAGER_WRITER_LOCKED );
61925 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
61926 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
61928 }else if( pPager->errCode ){
61929 return pPager->errCode;
61930 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
61931 assert( pPager->tempFile==0 );
61945 return pPg->flags & PGHDR_WRITEABLE;
61963 ** This optimization cannot be used with a temp-file, as the page may
61970 Pager *pPager = pPg->pPager;
61971 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
61972 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
61973 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
61974 pPg->flags |= PGHDR_DONT_WRITE;
61975 pPg->flags &= ~PGHDR_WRITEABLE;
61976 testcase( pPg->flags & PGHDR_NEED_SYNC );
61983 ** change-counter, stored as a 4-byte big-endian integer starting at
61987 ** But this only happens if the pPager->changeCountDone flag is false.
61997 ** The isDirectMode flag may only be non-zero if the library was compiled
61999 ** if isDirect is non-zero, then the database file is updated directly
62006 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62007 || pPager->eState==PAGER_WRITER_DBMOD
62012 ** atomic-write optimization is enabled in this build, then isDirect
62016 ** The idea is that if the atomic-write optimization is not
62029 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
62032 assert( !pPager->tempFile && isOpen(pPager->fd) );
62039 ** operating in direct-mode, make page 1 writable. When not in
62041 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
62054 assert( pPager->dbFileSize>0 );
62055 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
62057 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
62058 pPager->aStat[PAGER_STAT_WRITE]++;
62061 /* Update the pager's copy of the change-counter. Otherwise, the
62063 ** flushed (as the change-counter values will not match). */
62065 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
62066 pPager->changeCountDone = 1;
62069 pPager->changeCountDone = 1;
62080 ** Sync the database file to disk. This is a no-op for in-memory databases
62083 ** If successful, or if called on a pager for which it is a no-op, this
62089 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
62091 if( rc==SQLITE_OK && !pPager->noSync ){
62093 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
62099 ** This function may only be called while a write-transaction is active in
62100 ** rollback. If the connection is in WAL mode, this call is a no-op.
62110 int rc = pPager->errCode;
62113 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62114 || pPager->eState==PAGER_WRITER_DBMOD
62115 || pPager->eState==PAGER_WRITER_LOCKED
62127 ** of a super-journal file that should be written into the individual
62129 ** super-journal (a single database transaction).
62133 ** * The database file change-counter is updated,
62134 ** * the journal is synced (unless the atomic-write optimization is used),
62141 ** delete the super-journal file if specified).
62146 ** If the final parameter - noSync - is true, then the database file itself
62153 const char *zSuper, /* If not NULL, the super-journal name */
62158 assert( pPager->eState==PAGER_WRITER_LOCKED
62159 || pPager->eState==PAGER_WRITER_CACHEMOD
62160 || pPager->eState==PAGER_WRITER_DBMOD
62161 || pPager->eState==PAGER_ERROR
62166 if( NEVER(pPager->errCode) ) return pPager->errCode;
62172 pPager->zFilename, zSuper, pPager->dbSize));
62175 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
62177 assert( MEMDB==0 || pPager->tempFile );
62178 assert( isOpen(pPager->fd) || pPager->tempFile );
62180 /* If this is an in-memory db, or no pages have been written to, or this
62181 ** function has already been called, it is mostly a no-op. However, any
62183 sqlite3BackupRestart(pPager->pBackup);
62188 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62191 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
62194 pList->pDirty = 0;
62198 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
62202 sqlite3PcacheCleanAll(pPager->pPCache);
62205 /* The bBatch boolean is true if the batch-atomic-write commit method
62206 ** should be used. No rollback journal is created if batch-atomic-write
62210 sqlite3_file *fd = pPager->fd;
62213 && !pPager->noSync
62214 && sqlite3JournalIsInMemory(pPager->jfd);
62220 /* The following block updates the change-counter. Exactly how it
62221 ** does this depends on whether or not the atomic-update optimization
62225 ** * The file-system supports the atomic-write property for
62226 ** blocks of size page-size, and
62227 ** * This commit is not part of a multi-file transaction, and
62232 ** counter in 'indirect-mode'. If the optimization is compiled in but
62235 ** pager_incr_changecounter() to update the change-counter in indirect
62239 ** then call pager_incr_changecounter() to update the change-counter
62245 assert( isOpen(pPager->jfd)
62246 || pPager->journalMode==PAGER_JOURNALMODE_OFF
62247 || pPager->journalMode==PAGER_JOURNALMODE_WAL
62249 if( !zSuper && isOpen(pPager->jfd)
62250 && pPager->journalOff==jrnlBufferSize(pPager)
62251 && pPager->dbSize>=pPager->dbOrigSize
62252 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
62254 /* Update the db file change counter via the direct-write method. The
62255 ** following call will modify the in-memory representation of page 1
62257 ** directly to the database file. Because of the atomic-write
62258 ** property of the host file-system, this is safe.
62262 rc = sqlite3JournalCreate(pPager->jfd);
62271 rc = sqlite3JournalCreate(pPager->jfd);
62280 /* Write the super-journal name into the journal file. If a
62281 ** super-journal file name has already been written to the journal file,
62282 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
62288 ** If the atomic-update optimization is being used, this sync will not
62291 ** Because the change-counter page was just modified, unless the
62292 ** atomic-update optimization is used it is almost certain that the
62296 ** xSync() call will be changed to a no-op by the OS anyhow.
62301 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62316 rc = sqlite3JournalCreate(pPager->jfd);
62318 sqlite3OsClose(pPager->jfd);
62323 sqlite3OsClose(pPager->jfd);
62335 sqlite3PcacheCleanAll(pPager->pPCache);
62340 ** last page in the db image moved to the free-list. In this case the
62343 if( pPager->dbSize>pPager->dbFileSize ){
62344 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
62345 assert( pPager->eState==PAGER_WRITER_DBMOD );
62360 pPager->eState = PAGER_WRITER_FINISHED;
62369 ** synced to disk. The journal file still exists in the file-system
62371 ** be used as a hot-journal and the current transaction rolled back.
62375 ** for hot-journal rollback. Once this is done the transaction is
62387 if( NEVER(pPager->errCode) ) return pPager->errCode;
62388 pPager->iDataVersion++;
62390 assert( pPager->eState==PAGER_WRITER_LOCKED
62391 || pPager->eState==PAGER_WRITER_FINISHED
62392 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
62397 ** this transaction, the pager is running in exclusive-mode and is
62398 ** using persistent journals, then this function is a no-op.
62402 ** a hot-journal during hot-journal rollback, 0 changes will be made
62407 if( pPager->eState==PAGER_WRITER_LOCKED
62408 && pPager->exclusiveMode
62409 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
62411 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
62412 pPager->eState = PAGER_READER;
62417 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
62423 ** transaction are reverted and the current write-transaction is closed.
62433 ** in-memory cache pages to the state they were in when the transaction
62442 ** In WAL mode, all cache-entries containing data modified within the
62444 ** their pre-transaction state by re-reading data from the database or
62451 /* PagerRollback() is a no-op if called in READER or OPEN state. If
62456 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
62457 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
62461 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
62462 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
62464 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
62465 int eState = pPager->eState;
62472 pPager->errCode = SQLITE_ABORT;
62473 pPager->eState = PAGER_ERROR;
62481 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
62494 ** Return TRUE if the database file is opened read-only. Return FALSE
62498 return pPager->readOnly;
62506 return sqlite3PcacheRefCount(pPager->pPCache);
62515 int perPageSize = pPager->pageSize + pPager->nExtra
62517 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
62519 + pPager->pageSize;
62535 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
62536 a[1] = sqlite3PcachePagecount(pPager->pPCache);
62537 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
62538 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
62539 a[4] = pPager->eState;
62540 a[5] = pPager->errCode;
62541 a[6] = pPager->aStat[PAGER_STAT_HIT];
62542 a[7] = pPager->aStat[PAGER_STAT_MISS];
62543 a[8] = 0; /* Used to be pPager->nOvfl */
62544 a[9] = pPager->nRead;
62545 a[10] = pPager->aStat[PAGER_STAT_WRITE];
62558 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
62574 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
62575 *pnVal += pPager->aStat[eStat];
62577 pPager->aStat[eStat] = 0;
62582 ** Return true if this is an in-memory or temp-file backed pager.
62585 return pPager->tempFile || pPager->memVfs;
62592 ** equal to nSavepoint, then this function is a no-op.
62595 ** occurs while opening the sub-journal file, then an IO error code is
62600 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
62604 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62606 assert( nSavepoint>nCurrent && pPager->useJournal );
62613 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
62618 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
62619 pPager->aSavepoint = aNew;
62623 aNew[ii].nOrig = pPager->dbSize;
62624 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
62625 aNew[ii].iOffset = pPager->journalOff;
62629 aNew[ii].iSubRec = pPager->nSubRec;
62630 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
62636 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
62638 pPager->nSavepoint = ii+1;
62640 assert( pPager->nSavepoint==nSavepoint );
62645 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62648 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
62668 ** (the first created). A value of (Pager.nSavepoint-1) means operate
62670 ** (Pager.nSavepoint-1), then this function is a no-op.
62687 int rc = pPager->errCode;
62696 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
62705 for(ii=nNew; ii<pPager->nSavepoint; ii++){
62706 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
62708 pPager->nSavepoint = nNew;
62710 /* Truncate the sub-journal so that it only includes the parts
62713 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
62714 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
62715 /* Only truncate if it is an in-memory sub-journal. */
62716 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
62717 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
62718 rc = sqlite3OsTruncate(pPager->sjfd, sz);
62721 pPager->nSubRec = pRel->iSubRec;
62725 ** If this is a temp-file, it is possible that the journal file has
62729 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
62730 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
62741 pPager->journalMode==PAGER_JOURNALMODE_OFF
62742 && pPager->eState>=PAGER_WRITER_CACHEMOD
62744 pPager->errCode = SQLITE_ABORT;
62745 pPager->eState = PAGER_ERROR;
62757 ** Except, if the pager is in-memory only, then return an empty string if
62761 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
62762 ** participate in shared-cache.
62769 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
62776 return pPager->pVfs;
62785 return pPager->fd;
62794 return pPager->jfd;
62796 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
62804 return pPager->zJournal;
62818 if( pPager->xCodecFree ){
62819 pPager->xCodecFree(pPager->pCodec);
62823 pPager->xCodec = pPager->memDb ? 0 : xCodec;
62824 pPager->xCodecSizeChng = xCodecSizeChng;
62825 pPager->xCodecFree = xCodecFree;
62826 pPager->pCodec = pCodec;
62831 return pPager->pCodec;
62843 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
62859 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
62867 ** If the fourth argument, isCommit, is non-zero, then this page is being
62877 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
62881 assert( pPg->nRef>0 );
62882 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62883 || pPager->eState==PAGER_WRITER_DBMOD
62887 /* In order to be able to rollback, an in-memory database must journal
62890 assert( pPager->tempFile || !MEMDB );
62891 if( pPager->tempFile ){
62898 ** sub-journal now. This is required to handle the following scenario:
62906 ** If page X were not written to the sub-journal here, it would not
62910 ** subjournalPage() may need to allocate space to store pPg->pgno into
62914 if( (pPg->flags & PGHDR_DIRTY)!=0
62921 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
62922 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
62924 /* If the journal needs to be sync()ed before page pPg->pgno can
62925 ** be written to, store pPg->pgno in local variable needSyncPgno.
62928 ** the journal needs to be sync()ed before database page pPg->pgno
62931 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
62932 needSyncPgno = pPg->pgno;
62933 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
62934 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
62935 assert( pPg->flags&PGHDR_DIRTY );
62938 /* If the cache contains a page with page-number pgno, remove it
62943 pPg->flags &= ~PGHDR_NEED_SYNC;
62945 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
62947 if( NEVER(pPgOld->nRef>1) ){
62950 …rintf(sizeof(zMsg), zMsg, "page(%u) should be no references, ref cnt:%d", pgno, (int)pPgOld->nRef);
62951 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPager->dbSize, pgno, CORRUPT_TYPE_UNKOWN,
62952 -1, 0, zMsg, NULL);
62955 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
62956 if( pPager->tempFile ){
62957 /* Do not discard pages from an in-memory database since we might
62959 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
62965 origPgno = pPg->pgno;
62969 /* For an in-memory database, make sure the original page continues
62973 if( pPager->tempFile && pPgOld ){
62979 /* If needSyncPgno is non-zero, then the journal file needs to be
62981 ** Currently, no such page exists in the page-cache and the
62983 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
62986 ** If the attempt to load the page into the page-cache fails, (due
62996 if( needSyncPgno<=pPager->dbOrigSize ){
62997 assert( pPager->pTmpSpace!=0 );
62998 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
63002 pPgHdr->flags |= PGHDR_NEED_SYNC;
63018 assert( pPg->pgno!=iNew );
63019 pPg->flags = flags;
63027 assert( pPg->nRef>0 || pPg->pPager->memDb );
63028 return pPg->pData;
63036 return pPg->pExtra;
63040 ** Get/set the locking-mode for this pager. Parameter eMode must be one
63043 ** the locking-mode is set to the value specified.
63047 ** locking-mode.
63055 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
63056 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
63057 pPager->exclusiveMode = (u8)eMode;
63059 return (int)pPager->exclusiveMode;
63063 ** Set the journal-mode for this pager. Parameter eMode must be one of:
63075 ** * An in-memory database can only have its journal_mode set to _OFF
63080 ** The returned indicate the current (possibly updated) journal-mode.
63083 u8 eOld = pPager->journalMode; /* Prior journalmode */
63097 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
63099 /* Do allow the journalmode of an in-memory database to be set to
63112 assert( pPager->eState!=PAGER_ERROR );
63113 pPager->journalMode = (u8)eMode;
63126 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
63127 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
63136 sqlite3OsClose(pPager->jfd);
63137 if( pPager->eLock>=RESERVED_LOCK ){
63138 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63141 int state = pPager->eState;
63146 if( pPager->eState==PAGER_READER ){
63152 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
63159 assert( state==pPager->eState );
63162 sqlite3OsClose(pPager->jfd);
63167 return (int)pPager->journalMode;
63174 return (int)pPager->journalMode;
63184 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
63185 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
63190 ** Get/set the size-limit used for persistent journal files.
63192 ** Setting the size limit to -1 means no limit is enforced.
63193 ** An attempt to set a limit smaller than -1 is a no-op.
63196 if( iLimit>=-1 ){
63197 pPager->journalSizeLimit = iLimit;
63198 sqlite3WalLimit(pPager->pWal, iLimit);
63200 return pPager->journalSizeLimit;
63204 ** Return a pointer to the pPager->pBackup variable. The backup module
63210 return &pPager->pBackup;
63215 ** Unless this is an in-memory or temporary database, clear the pager cache.
63218 assert( MEMDB==0 || pPager->tempFile );
63219 if( pPager->tempFile==0 ) pager_reset(pPager);
63240 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
63252 if( pPager->pWal ){
63253 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
63254 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
63255 pPager->pBusyHandlerArg,
63256 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
63264 return sqlite3WalCallback(pPager->pWal);
63269 ** primitives necessary for write-ahead logging.
63272 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
63273 if( pPager->noLock ) return 0;
63274 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
63284 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63297 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
63298 ** lock on the database file and use heap-memory to store the wal-index
63299 ** in. Otherwise, use the normal shared-memory.
63304 assert( pPager->pWal==0 && pPager->tempFile==0 );
63305 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63307 /* If the pager is already in exclusive-mode, the WAL module will use
63308 ** heap-memory for the wal-index instead of the VFS shared-memory
63312 if( pPager->exclusiveMode ){
63321 rc = sqlite3WalOpen(pPager->pVfs,
63322 pPager->fd, pPager->zWal, pPager->exclusiveMode,
63323 pPager->journalSizeLimit, &pPager->pWal
63337 ** file (not a temp file or an in-memory database), and the WAL file
63343 ** If the pager is open on a temp-file (or in-memory database), or if
63349 int *pbOpen /* OUT: Set to true if call is a no-op */
63354 assert( pPager->eState==PAGER_OPEN || pbOpen );
63355 assert( pPager->eState==PAGER_READER || !pbOpen );
63357 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
63359 if( !pPager->tempFile && !pPager->pWal ){
63363 sqlite3OsClose(pPager->jfd);
63367 pPager->journalMode = PAGER_JOURNALMODE_WAL;
63368 pPager->eState = PAGER_OPEN;
63389 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
63391 /* If the log file is not already open, but does exist in the file-system,
63395 if( !pPager->pWal ){
63401 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
63410 ** the database file, the log and log-summary files will be deleted.
63412 if( rc==SQLITE_OK && pPager->pWal ){
63416 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
63417 pPager->pageSize, (u8*)pPager->pTmpSpace);
63418 pPager->pWal = 0;
63420 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
63428 ** If pager pPager is a wal-mode database not in exclusive locking mode,
63435 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
63436 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
63447 sqlite3WalDb(pPager->pWal, db);
63459 if( pPager->pWal ){
63460 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
63475 if( pPager->pWal ){
63476 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
63489 if( pPager->pWal ){
63490 rc = sqlite3WalSnapshotRecover(pPager->pWal);
63511 if( pPager->pWal ){
63512 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
63524 assert( pPager->pWal );
63525 sqlite3WalSnapshotUnlock(pPager->pWal);
63533 ** A read-lock must be held on the pager when this function is called. If
63540 assert( pPager->eState>=PAGER_READER );
63541 return sqlite3WalFramesize(pPager->pWal);
63561 ** This file contains the implementation of a write-ahead log (WAL) used in
63564 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
63583 ** big-endian 32-bit unsigned integer values:
63589 ** 16: Salt-1, random integer incremented with each checkpoint
63590 ** 20: Salt-2, a different random integer changing with each ckpt
63591 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
63592 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
63594 ** Immediately following the wal-header are zero or more frames. Each
63595 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
63596 ** of page data. The frame-header is six big-endian 32-bit unsigned
63602 ** 8: Salt-1 (copied from the header)
63603 ** 12: Salt-2 (copied from the header)
63604 ** 16: Checksum-1.
63605 ** 20: Checksum-2.
63610 ** (1) The salt-1 and salt-2 values in the frame-header match
63611 ** salt values in the wal-header
63613 ** (2) The checksum values in the final 8 bytes of the frame-header
63618 ** The checksum is computed using 32-bit big-endian integers if the
63620 ** is computed using little-endian if the magic number is 0x377f0682.
63622 ** big-endian format regardless of which byte order is used to compute
63624 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
63627 ** for i from 0 to n-1 step 2:
63634 ** of the sequence being summed.) The s1 value spans all 32-bit
63637 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
63638 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
63639 ** The VFS.xSync operations serve as write barriers - all writes launched
63643 ** After each checkpoint, the salt-1 value is incremented and the salt-2
63672 ** data structure called the wal-index is maintained to expedite the
63675 ** WAL-INDEX FORMAT
63677 ** Conceptually, the wal-index is shared memory, though VFS implementations
63678 ** might choose to implement the wal-index using a mmapped file. Because
63679 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
63683 ** In the default unix and windows implementation, the wal-index is a mmapped
63684 ** file whose name is the database name with a "-shm" suffix added. For that
63685 ** reason, the wal-index is sometimes called the "shm" file.
63687 ** The wal-index is transient. After a crash, the wal-index can (and should
63689 ** to either truncate or zero the header of the wal-index when the last
63690 ** connection to it closes. Because the wal-index is transient, it can
63691 ** use an architecture-specific format; it does not have to be cross-platform.
63693 ** as big endian, the wal-index can store multi-byte values in the native
63696 ** The purpose of the wal-index is to answer this question quickly: Given
63701 ** The wal-index consists of a header region, followed by an one or
63704 ** The wal-index header contains the total number of frames within the WAL
63710 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
63712 ** wal-index. The values are:
63717 ** Each index block contains two sections, a page-mapping that contains the
63718 ** database page number associated with each wal frame, and a hash-table
63720 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
63721 ** for the first index block) 32-bit page numbers. The first entry in the
63722 ** first index-block contains the database page number corresponding to the
63727 ** The last index block in a wal-index usually contains less than the full
63728 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
63730 ** allocated size of the page-mapping array - the page-mapping array merely
63734 ** can be found by scanning the page-mapping sections of each index block
63740 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
63745 ** 1-based index of an entry in the mapping section of the same
63746 ** index block. Let K be the 1-based index of the largest entry in
63761 ** wrap-around.) Because the hash table is never more than half full,
63766 ** current index block. Otherwise the iMax-th mapping entry of the
63784 ** and to the wal-index) might be using a different value K1, where K1>K0.
63790 ** in the first place - which is what reader one wants. Meanwhile, the
63813 ** The maximum (and only) versions of the wal and wal-index formats
63817 ** values in the wal-header are correct and (b) the version field is not
63820 ** Similarly, if a client successfully reads a wal-index header (i.e. the
63822 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
63837 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
63847 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
63857 ** The following object holds a copy of the wal-index header content.
63859 ** The actual header in the wal-index consists of two copies of this
63866 ** Or it can be 1 to represent a 65536-byte page. The latter case was
63870 u32 iVersion; /* Wal-index version */
63874 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
63884 ** A copy of the following object occurs in the wal-index immediately
63907 ** holds read-lock K, then the value in aReadMark[K] is no greater than
63910 ** a special case; its value is never used and it exists as a place-holder
63939 ** We assume that 32-bit loads are atomic and so no locks are needed in
63952 ** This is a schematic view of the complete 136-byte header of the
63953 ** wal-index file (also known as the -shm file):
63955 ** +-----------------------------+
63957 ** +-----------------------------+ |
63959 ** +-----------------------------+ |
63961 ** +-------+-------+-------------+ |
63963 ** +-------+-------+-------------+ |
63965 ** +-----------------------------+ | WalIndexHdr object
63967 ** +-----------------------------+ |
63970 ** +-----------------------------+ |
63973 ** +-----------------------------+ |
63976 ** +-----------------------------+
63978 ** +-----------------------------+ |
63980 ** +-----------------------------+ |
63982 ** +-------+-------+-------------+ |
63984 ** +-------+-------+-------------+ | Second copy of the
63986 ** +-----------------------------+ |
63988 ** +-----------------------------+ |
63991 ** +-----------------------------+ |
63994 ** +-----------------------------+ |
63997 ** +-----------------------------+
63999 ** +-----------------------------+
64005 ** +-------+-------+------+------+
64007 ** +-------+-------+------+------+ ) 8 lock bytes
64009 ** +-------+-------+------+------+
64011 ** +-----------------------------+
64013 ** +-----------------------------+
64018 ** only support mandatory file-locks, we do not read or write data
64031 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
64032 ** big-endian format in the first 4 bytes of a WAL file.
64035 ** file are calculated by treating all data as an array of 32-bit
64036 ** big-endian words. Otherwise, they are calculated by interpreting
64037 ** all data as 32-bit little-endian words.
64042 ** Return the offset of frame iFrame in the write-ahead log file,
64044 ** is to the start of the write-ahead log frame-header.
64047 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
64051 ** An open write-ahead log file is represented by an instance of the
64062 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
64064 i16 readLock; /* Which read lock is being held. -1 for none */
64066 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
64073 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
64074 WalIndexHdr hdr; /* Wal-index header for current transaction */
64078 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
64105 ** Each page of the wal-index mapping contains a hash-table made up of
64119 ** walIteratorInit() - Create a new iterator,
64120 ** walIteratorNext() - Step an iterator,
64121 ** walIteratorFree() - Free an iterator.
64134 } aSegment[1]; /* One for every 32KB page in the wal-index */
64138 ** Define the parameters of the hash tables in the wal-index file. There
64139 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
64140 ** wal-index.
64142 ** Changing any of these constants will alter the wal-index format and
64150 ** The block of page numbers associated with the first hash-table in a
64151 ** wal-index is smaller than usual. This is so that there is a complete
64152 ** hash-table on each aligned 32KB page of the wal-index.
64154 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
64156 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
64162 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
64163 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
64166 ** If the wal-index is currently smaller the iPage pages then the size
64167 ** of the wal-index might be increased, but only if it is safe to do
64168 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
64169 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
64173 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
64177 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
64186 /* Enlarge the pWal->apWiData[] array if required */
64187 if( pWal->nWiData<=iPage ){
64190 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
64195 memset((void*)&apNew[pWal->nWiData], 0,
64196 sizeof(u32*)*(iPage+1-pWal->nWiData));
64197 pWal->apWiData = apNew;
64198 pWal->nWiData = iPage+1;
64202 assert( pWal->apWiData[iPage]==0 );
64203 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
64204 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
64205 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
64207 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
64208 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
64210 assert( pWal->apWiData[iPage]!=0
64212 || (pWal->writeLock==0 && iPage==0) );
64213 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
64217 pWal->readOnly |= WAL_SHM_RDONLY;
64224 *ppPage = pWal->apWiData[iPage];
64233 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
64240 ** Return a pointer to the WalCkptInfo structure in the wal-index.
64243 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64244 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
64248 ** Return a pointer to the WalIndexHdr structure in the wal-index.
64251 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64252 return (volatile WalIndexHdr*)pWal->apWiData[0];
64256 ** The argument to this macro must be of type u32. On a little-endian
64258 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
64260 ** of the input value as a little-endian integer.
64277 int nativeCksum, /* True for native byte-order, false for non-native */
64320 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64321 sqlite3OsShmBarrier(pWal->pDbFd);
64326 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
64328 ** might give false-positive TSAN warnings.
64330 ** See tag-20200519-1.
64339 ** Write the header information in pWal->hdr into the wal-index.
64341 ** The checksum on pWal->hdr is updated before it is written.
64347 assert( pWal->writeLock );
64348 pWal->hdr.isInit = 1;
64349 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
64350 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
64351 /* Possible TSAN false-positive. See tag-20200519-1 */
64352 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64354 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64359 ** supplied by the caller. A frame-header is made up of a series of
64360 ** 4-byte big-endian integers, as follows:
64365 ** 8: Salt-1 (copied from the wal-header)
64366 ** 12: Salt-2 (copied from the wal-header)
64367 ** 16: Checksum-1.
64368 ** 20: Checksum-2.
64371 Wal *pWal, /* The write-ahead log */
64373 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
64377 int nativeCksum; /* True for native byte-order checksums */
64378 u32 *aCksum = pWal->hdr.aFrameCksum;
64382 if( pWal->iReCksum==0 ){
64383 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
64385 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64387 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64402 Wal *pWal, /* The write-ahead log */
64408 int nativeCksum; /* True for native byte-order checksums */
64409 u32 *aCksum = pWal->hdr.aFrameCksum;
64413 /* A frame is only valid if the salt values in the frame-header
64414 ** match the salt values in the wal-header.
64416 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
64428 ** all prior frams, the first 16 bytes of this frame-header,
64429 ** and the frame-data matches the checksum in the last 8
64430 ** bytes of this frame-header.
64432 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64434 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64458 return "WRITE-LOCK";
64460 return "CKPT-LOCK";
64462 return "RECOVER-LOCK";
64465 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
64466 lockIdx-WAL_READ_LOCK(0));
64475 ** A lock cannot be moved directly between shared and exclusive - it must go
64478 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
64482 if( pWal->exclusiveMode ) return SQLITE_OK;
64483 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64485 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
64487 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64491 if( pWal->exclusiveMode ) return;
64492 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64494 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
64498 if( pWal->exclusiveMode ) return SQLITE_OK;
64499 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64501 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
64503 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64507 if( pWal->exclusiveMode ) return;
64508 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64510 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
64516 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
64521 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
64522 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
64525 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
64530 ** of a page hash table in the wal-index. This becomes the return value
64535 volatile ht_slot *aHash; /* Start of the wal-index hash table */
64542 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
64545 ** Set output variable pLoc->aHash to point to the start of the hash table
64546 ** in the wal-index file. Set pLoc->iZero to one less than the frame
64549 ** (pLoc->iZero+N) in the log.
64551 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
64552 ** first frame indexed by the hash table, frame (pLoc->iZero).
64561 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
64564 if( pLoc->aPgno ){
64565 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
64567 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
64568 pLoc->iZero = 0;
64570 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
64579 ** Return the number of the wal-index page that contains the hash-table
64580 ** and page-number array that contain entries corresponding to WAL frame
64581 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
64585 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
64602 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
64604 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
64609 ** than pWal->hdr.mxFrame.
64611 ** This function is called whenever pWal->hdr.mxFrame is decreased due
64614 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
64616 ** pWal->hdr.mxFrame advances to the point where those hash tables are
64625 assert( pWal->writeLock );
64626 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
64627 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
64628 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
64630 if( pWal->hdr.mxFrame==0 ) return;
64632 /* Obtain pointers to the hash-table and page-number array containing
64633 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
64634 ** that the page said hash-table and array reside on is already mapped.(1)
64636 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
64637 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
64638 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
64639 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
64641 /* Zero all hash-table entries that correspond to frame numbers greater
64642 ** than pWal->hdr.mxFrame.
64644 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
64653 ** frame numbers greater than pWal->hdr.mxFrame.
64655 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
64678 ** Set an entry in the wal-index that will map database page number
64683 WalHashLoc sLoc; /* Wal-index hash table location */
64687 /* Assuming the wal-index file was successfully mapped, populate the
64692 int idx; /* Value to write to hash-table slot */
64695 idx = iFrame - sLoc.iZero;
64698 /* If this is the first entry to be added to this hash-table, zero the
64702 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
64711 ** the hash-table before writing any new entries.
64713 if( sLoc.aPgno[idx-1] ){
64715 assert( !sLoc.aPgno[idx-1] );
64718 /* Write the aPgno[] array entry and the hash-table slot. */
64721 if( (nCollide--)==0 ){
64724 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pWal->hdr.nPage, iPage, CORRUPT_TYPE_FRAME_…
64725 -1, 0, zMsg, NULL);
64729 sLoc.aPgno[idx-1] = iPage;
64745 ** thing to check, so only do this occasionally - not on every
64767 ** Recover the wal-index by reading the write-ahead log file.
64770 ** wal-index to prevent other threads/processes from doing anything
64771 ** with the WAL or wal-index while recovery is running. The
64788 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
64791 assert( pWal->writeLock );
64792 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
64793 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64801 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
64803 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
64810 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
64818 u32 iPg; /* Current 32KB wal-index page */
64822 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
64835 || szPage&(szPage-1)
64841 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
64842 pWal->szPage = szPage;
64843 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
64844 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
64847 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
64848 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
64850 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
64851 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
64875 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
64880 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
64885 pWal->apWiData[iPg] = aPrivate;
64893 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64900 /* If nTruncate is non-zero, this is a commit record. */
64902 pWal->hdr.mxFrame = iFrame;
64903 pWal->hdr.nPage = nTruncate;
64904 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
64907 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
64908 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
64911 pWal->apWiData[iPg] = aShare;
64923 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
64952 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
64953 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
64956 /* Reset the checkpoint-header. This is safe because this thread is
64958 ** checkpointers. Then set the values of read-mark slots 1 through N.
64961 pInfo->nBackfill = 0;
64962 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
64963 pInfo->aReadMark[0] = 0;
64967 if( i==1 && pWal->hdr.mxFrame ){
64968 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
64970 pInfo->aReadMark[i] = READMARK_NOT_USED;
64983 if( pWal->hdr.nPage ){
64986 pWal->hdr.mxFrame, pWal->zWalName
64993 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64998 ** Close an open wal-index.
65001 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
65003 for(i=0; i<pWal->nWiData; i++){
65004 sqlite3_free((void *)pWal->apWiData[i]);
65005 pWal->apWiData[i] = 0;
65008 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
65009 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
65020 ** client from unlinking the WAL or wal-index file. If another process
65029 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
65032 int bNoShm, /* True to run in heap-memory mode */
65044 ** of these constants would result in an incompatible on-disk format
65045 ** for the -shm file. Any change that causes one of these asserts to
65049 ** This table also serves as a helpful cross-reference when trying to
65050 ** interpret hex dumps of the -shm file.
65090 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
65095 pRet->pVfs = pVfs;
65096 pRet->pWalFd = (sqlite3_file *)&pRet[1];
65097 pRet->pDbFd = pDbFd;
65098 pRet->readLock = -1;
65099 pRet->mxWalSize = mxWalSize;
65100 pRet->zWalName = zWalName;
65101 pRet->syncHeader = 1;
65102 pRet->padToSectorBoundary = 1;
65103 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
65105 /* Open file handle on the write-ahead log file. */
65107 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
65109 pRet->readOnly = WAL_RDONLY;
65114 sqlite3OsClose(pRet->pWalFd);
65118 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
65120 pRet->padToSectorBoundary = 0;
65132 if( pWal ) pWal->mxWalSize = iLimit;
65154 iMin = p->iPrior;
65156 for(i=p->nSegment-1; i>=0; i--){
65157 struct WalSegment *pSegment = &p->aSegment[i];
65158 while( pSegment->iNext<pSegment->nEntry ){
65159 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
65163 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
65167 pSegment->iNext++;
65171 *piPage = p->iPrior = iRet;
65199 const u32 *aContent, /* Pages in wal - keys for the sort */
65263 ht_slot *aList; /* Pointer to sub-list content */
65271 struct Sublist aSub[13]; /* Array of sub-lists */
65275 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
65284 assert( p->aList && p->nList<=(1<<iSub) );
65285 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
65286 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65297 assert( p->nList<=(1<<iSub) );
65298 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
65299 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65309 assert( aContent[aList[i]] > aContent[aList[i-1]] );
65325 ** nBackfill or earlier may be included - excluding them is an optimization
65341 ht_slot *aTmp; /* Temp space used by merge-sort */
65347 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
65348 iLast = pWal->hdr.mxFrame;
65353 + (nSegment-1)*sizeof(struct WalSegment)
65360 p->nSegment = nSegment;
65362 /* Allocate temporary space used by the merge-sort routine. This block
65382 nEntry = (int)(iLast - sLoc.iZero);
65384 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
65386 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
65393 p->aSegment[i].iZero = sLoc.iZero;
65394 p->aSegment[i].nEntry = nEntry;
65395 p->aSegment[i].aIndex = aIndex;
65396 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
65413 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
65418 if( pWal->db ){
65419 int tmout = pWal->db->busyTimeout;
65423 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
65436 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
65450 assert( pWal->readLock<0 || bLock==0 );
65452 assert( pWal->db );
65456 pWal->writeLock = 1;
65460 }else if( pWal->writeLock ){
65462 pWal->writeLock = 0;
65471 pWal->db = db;
65495 ** busy-handler function. Invoke it and retry the lock until either the
65496 ** lock is successfully obtained or the busy-handler returns 0.
65519 ** The cache of the wal-index header must be valid to call this function.
65520 ** Return the page-size in bytes used by the database.
65523 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65532 ** file - there are no readers that may attempt to read a frame from
65535 ** This function updates the shared-memory structures so that the next
65540 ** new wal-index header. It should be passed a pseudo-random value (i.e.
65546 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
65547 pWal->nCkpt++;
65548 pWal->hdr.mxFrame = 0;
65550 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
65552 AtomicStore(&pInfo->nBackfill, 0);
65553 pInfo->nBackfillAttempted = 0;
65554 pInfo->aReadMark[1] = 0;
65555 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
65556 assert( pInfo->aReadMark[0]==0 );
65570 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
65576 ** in the WAL and can be recovered following a power-loss or hard reset.
65583 ** This routine uses and updates the nBackfill field of the wal-index header.
65602 int szPage; /* Database page-size */
65615 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65617 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65626 mxSafeFrame = pWal->hdr.mxFrame;
65627 mxPage = pWal->hdr.nPage;
65629 u32 y = AtomicLoad(pInfo->aReadMark+i);
65631 assert( y<=pWal->hdr.mxFrame );
65636 AtomicStore(pInfo->aReadMark+i, iMark);
65648 if( pInfo->nBackfill<mxSafeFrame ){
65649 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
65656 u32 nBackfill = pInfo->nBackfill;
65658 pInfo->nBackfillAttempted = mxSafeFrame;
65661 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
65669 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
65670 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
65672 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
65675 ** maximum size of the pending-byte page (65536 bytes), then
65680 nSize, pWal->hdr.mxFrame, szPage, nReq);
65682 -1, 0, zMsg, NULL);
65685 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
65695 if( AtomicLoad(&db->u1.isInterrupted) ){
65696 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
65704 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
65706 iOffset = (iDbpage-1)*(i64)szPage;
65710 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
65713 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
65717 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
65718 i64 szDb = pWal->hdr.nPage*(i64)szPage;
65720 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
65722 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
65726 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
65749 assert( pWal->writeLock );
65750 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65756 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
65757 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
65761 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
65767 ** wal-index header in shared memory, as all subsequent reader or
65771 ** the wal-index header do not match the contents of the
65772 ** file-system. To avoid this, update the wal-index header to
65775 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
65777 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65795 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
65797 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
65801 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
65817 int isDelete = 0; /* True to unlink wal and wal-index files */
65820 ** ordinary, rollback-mode locking methods, this guarantees that the
65823 ** the wal and wal-index files.
65828 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
65830 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
65831 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65837 int bPersist = -1;
65839 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
65843 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
65846 }else if( pWal->mxWalSize>=0 ){
65850 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
65860 sqlite3OsClose(pWal->pWalFd);
65863 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
65867 sqlite3_free((void *)pWal->apWiData);
65874 ** Try to read the wal-index header. Return 0 on success and 1 if
65877 ** The wal-index is in shared memory. Another thread or process might
65884 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
65887 ** If the checksum cannot be verified return non-zero. If the header
65895 /* The first page of the wal-index must be mapped at this point. */
65896 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65901 ** from the file. If this happens, return non-zero.
65903 ** tag-20200519-1:
65904 ** There are two copies of the header at the beginning of the wal-index.
65908 ** give false-positive warnings about these accesses because the tools do not
65909 ** account for the double-read and the memory barrier. The use of mutexes
65915 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
65923 return 1; /* Malformed header - probably all zeros */
65925 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
65930 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
65932 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
65933 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65934 testcase( pWal->szPage<=32768 );
65935 testcase( pWal->szPage>=65536 );
65946 #define WAL_RETRY (-1)
65949 ** Read the wal-index header from the wal-index and into pWal->hdr.
65950 ** If the wal-header appears to be corrupt, try to reconstruct the
65951 ** wal-index from the WAL before returning.
65953 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
65954 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
65957 ** If the wal-index header is successfully read, return SQLITE_OK.
65963 volatile u32 *page0; /* Chunk of wal-index containing header */
65965 /* Ensure that page 0 of the wal-index (the page that contains the
65966 ** wal-index header) is mapped. Return early if an error occurs here.
65973 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
65975 ** confirm that another write-capable connection has the shared-memory
65976 ** open, and hence the content of the shared-memory is unreliable,
65977 ** since the shared-memory might be inconsistent with the WAL file
65980 assert( pWal->writeLock==0 );
65981 assert( pWal->readOnly & WAL_SHM_RDONLY );
65982 pWal->bShmUnreliable = 1;
65983 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
65986 return rc; /* Any other non-OK return is just an error */
65989 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
65993 assert( page0!=0 || pWal->writeLock==0 );
65995 /* If the first page of the wal-index has been mapped, try to read the
65996 ** wal-index header immediately, without holding any lock. This usually
65997 ** works, but may fail if the wal-index header is corrupt or currently
66006 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
66013 int bWriteLock = pWal->writeLock;
66015 pWal->writeLock = 1;
66019 /* If the wal-index header is still malformed even while holding
66028 pWal->writeLock = 0;
66037 ** sure the wal-index was not constructed with some future format that
66040 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
66043 if( pWal->bShmUnreliable ){
66046 pWal->bShmUnreliable = 0;
66047 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
66054 pWal->exclusiveMode = WAL_NORMAL_MODE;
66061 ** Open a transaction in a connection where the shared-memory is read-only
66062 ** and where we cannot verify that there is a separate write-capable connection
66063 ** on hand to keep the shared-memory up-to-date with the WAL file.
66065 ** This can happen, for example, when the shared-memory is implemented by
66066 ** memory-mapping a *-shm file, where a prior writer has shut down and
66067 ** left the *-shm file on disk, and now the present connection is trying
66068 ** to use that database but lacks write permission on the *-shm file.
66073 ** The *-wal file has been read and an appropriate wal-index has been
66074 ** constructed in pWal->apWiData[] using heap memory instead of shared
66081 ** WAL_RETRY, then the heap memory wal-index has been discarded and
66083 ** beginning (including attempting to map the *-shm file).
66096 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
66098 assert( pWal->bShmUnreliable );
66099 assert( pWal->readOnly & WAL_SHM_RDONLY );
66100 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66110 pWal->readLock = 0;
66112 /* Check to see if a separate writer has attached to the shared-memory area,
66113 ** thus making the shared-memory "reliable" again. Do this by invoking
66117 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
66118 ** cause the heap-memory WAL-index to be discarded and the actual
66130 ** even if some external agent does a "chmod" to make the shared-memory
66134 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
66135 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
66141 /* We reach this point only if the real shared-memory is still unreliable.
66142 ** Assume the in-memory WAL-index substitute is correct and load it
66143 ** into pWal->hdr.
66145 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
66150 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
66155 /* If the wal file is too small to contain a wal-header and the
66156 ** wal-index header has mxFrame==0, then it must be safe to proceed
66162 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
66167 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
66171 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
66173 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
66180 assert( (pWal->szPage & (pWal->szPage-1))==0 );
66181 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
66182 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
66191 ** wal file since the heap-memory wal-index was created. If so, the
66192 ** heap-memory wal-index is discarded and WAL_RETRY returned to
66194 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
66195 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
66196 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
66204 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
66208 /* If nTruncate is non-zero, then a complete transaction has been
66216 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
66217 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
66223 for(i=0; i<pWal->nWiData; i++){
66224 sqlite3_free((void*)pWal->apWiData[i]);
66225 pWal->apWiData[i] = 0;
66227 pWal->bShmUnreliable = 0;
66246 ** to make a copy of the wal-index header into pWal->hdr. If the
66247 ** wal-index header has changed, *pChanged is set to 1 (as an indication
66249 ** flushed.) When useWal==1, the wal-index header is assumed to already
66260 ** bad luck when there is lots of contention for the wal-index, but that
66264 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
66265 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
66268 ** including frame number aReadMark[pWal->readLock]. The reader will
66269 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
66270 ** Or if pWal->readLock==0, then the reader will ignore the WAL
66273 ** this routine will always set pWal->readLock>0 on success.
66275 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
66286 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
66293 assert( pWal->readLock<0 ); /* Not currently locked */
66296 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
66304 ** paged out or take a page-fault that is time-consuming to resolve,
66318 VVA_ONLY( pWal->lockError = 1; )
66321 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
66325 sqlite3OsSleep(pWal->pVfs, nDelay);
66330 if( pWal->bShmUnreliable==0 ){
66342 if( pWal->apWiData[0]==0 ){
66347 ** code that determines whether or not the shared-memory region
66364 else if( pWal->bShmUnreliable ){
66369 assert( pWal->nWiData>0 );
66370 assert( pWal->apWiData[0]!=0 );
66372 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
66374 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
66384 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
66402 pWal->readLock = 0;
66412 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
66416 mxFrame = pWal->hdr.mxFrame;
66418 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
66419 mxFrame = pWal->pSnapshot->mxFrame;
66423 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
66430 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
66436 AtomicStore(pInfo->aReadMark+i,mxFrame);
66448 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
66457 /* Now that the read-lock has been obtained, check that neither the
66458 ** value in the aReadMark[] array or the contents of the wal-index
66461 ** It is necessary to check that the wal-index header did not change
66462 ** between the time it was read and when the shared-lock was obtained
66465 ** that occur later in the log than pWal->hdr.mxFrame may have been
66468 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
66471 ** Before checking that the live wal-index header has not changed
66474 ** to read any frames earlier than minFrame from the wal file - they
66478 ** nBackfill and checking that the wal-header in shared-memory still
66479 ** matches the one cached in pWal->hdr, it is guaranteed that the
66480 ** checkpointer that set nBackfill was not working with a wal-index
66481 ** header newer than that cached in pWal->hdr. If it were, that could
66483 ** a version of page X that lies before pWal->minFrame (call that version
66486 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
66489 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
66491 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
66493 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
66494 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
66500 assert( mxReadMark<=pWal->hdr.mxFrame );
66501 pWal->readLock = (i16)mxI;
66515 ** This is only really safe if the file-system is such that any page
66529 assert( pWal->readLock>=0 );
66533 int szPage = (int)pWal->szPage;
66536 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
66543 u32 i = pInfo->nBackfillAttempted;
66544 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
66552 assert( i - sLoc.iZero - 1 >=0 );
66553 pgno = sLoc.aPgno[i-sLoc.iZero-1];
66554 iDbOff = (i64)(pgno-1) * szPage;
66558 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
66561 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
66569 pInfo->nBackfillAttempted = i-1;
66587 ** it takes a snapshot of the state of the WAL and wal-index for the current
66589 ** Other threads might append new content to the WAL and wal-index but
66602 WalIndexHdr *pSnapshot = pWal->pSnapshot;
66605 assert( pWal->ckptLock==0 );
66609 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66617 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
66620 ** before checking pInfo->nBackfillAttempted. */
66628 pWal->ckptLock = 1;
66642 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66644 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
66645 ** is populated with the wal-index header corresponding to the head
66653 ** pSnapshot->mxFrame into the database file. Note that the
66658 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
66659 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
66663 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
66664 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
66667 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
66668 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
66670 assert( pWal->readLock>0 );
66671 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
66677 /* A client using a non-current snapshot may not ignore any frames
66683 pWal->minFrame = 1;
66692 if( pWal->ckptLock ){
66695 pWal->ckptLock = 0;
66703 ** read-lock.
66707 if( pWal->readLock>=0 ){
66708 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
66709 pWal->readLock = -1;
66727 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
66732 assert( pWal->readLock>=0 || pWal->lockError );
66734 /* If the "last page" field of the wal-index header snapshot is 0, then
66736 ** in this case as an optimization. Likewise, if pWal->readLock==0,
66740 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
66750 ** that adds entries to the wal-index (and possibly to this hash
66754 ** read transaction was opened may have been written incorrectly -
66761 ** if we had exclusive access to the hash-table:
66764 ** This condition filters out normal hash-table collisions.
66768 ** table after the current read-transaction had started.
66770 iMinHash = walFramePage(pWal->minFrame);
66771 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
66786 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
66790 if( (nCollide--)==0 ){
66792 sqlite3_snprintf(sizeof(zMsg), zMsg, "mis-match page(%u) to map into WAL", pgno);
66793 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pWal->hdr.nPage, pgno, CORRUPT_TYPE_FRAME_W…
66794 -1, 0, zMsg, NULL);
66804 ** of the wal-index file content. Make sure the results agree with the
66809 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
66810 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
66837 sz = pWal->hdr.szPage;
66843 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
66850 if( pWal && ALWAYS(pWal->readLock>=0) ){
66851 return pWal->hdr.nPage;
66874 /* If the write-lock is already held, then it was obtained before the
66875 ** read-transaction was even opened, making this call a no-op.
66877 if( pWal->writeLock ){
66878 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
66885 assert( pWal->readLock>=0 );
66886 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
66888 if( pWal->readOnly ){
66899 pWal->writeLock = 1;
66905 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
66907 pWal->writeLock = 0;
66919 if( pWal->writeLock ){
66921 pWal->writeLock = 0;
66922 pWal->iReCksum = 0;
66923 pWal->truncateOnCommit = 0;
66930 ** function moves the write-pointer back to the start of the transaction.
66942 if( ALWAYS(pWal->writeLock) ){
66943 Pgno iMax = pWal->hdr.mxFrame;
66946 /* Restore the clients cache of the wal-index header to the state it
66949 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
66951 for(iFrame=pWal->hdr.mxFrame+1;
66957 ** (b) has an outstanding reference, then xUndo is either a no-op
66969 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
66981 assert( pWal->writeLock );
66982 aWalData[0] = pWal->hdr.mxFrame;
66983 aWalData[1] = pWal->hdr.aFrameCksum[0];
66984 aWalData[2] = pWal->hdr.aFrameCksum[1];
66985 aWalData[3] = pWal->nCkpt;
66997 assert( pWal->writeLock );
66998 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
67000 if( aWalData[3]!=pWal->nCkpt ){
67001 /* This savepoint was opened immediately after the write-transaction
67006 aWalData[3] = pWal->nCkpt;
67009 if( aWalData[0]<pWal->hdr.mxFrame ){
67010 pWal->hdr.mxFrame = aWalData[0];
67011 pWal->hdr.aFrameCksum[0] = aWalData[1];
67012 pWal->hdr.aFrameCksum[1] = aWalData[2];
67024 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
67028 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
67035 if( pWal->readLock==0 ){
67037 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
67038 if( pInfo->nBackfill>0 ){
67041 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67046 ** wal-index header to reflect this.
67049 ** at this point. But updating the actual wal-index header is also
67053 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
67059 pWal->readLock = -1;
67075 ** the next fsync should occur - passed from sqlite3WalFrames() into
67088 ** Do a sync when crossing the p->iSyncPoint boundary.
67101 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
67102 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
67103 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
67106 iAmt -= iFirstAmt;
67108 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
67109 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
67112 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
67127 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
67128 if( pPage->pgno==1 ){
67129 rc = checkHeaderValid(pPage->pPager, pPage->pData, "walWrite");
67135 pData = pPage->pData;
67137 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
67141 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
67154 const int szPage = pWal->szPage;/* Database page size */
67157 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
67167 ** wal-file header. Otherwise, read them from the frame header of the
67169 assert( pWal->iReCksum>0 );
67170 if( pWal->iReCksum==1 ){
67173 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
67175 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
67176 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
67177 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
67179 iRead = pWal->iReCksum;
67180 pWal->iReCksum = 0;
67183 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
67190 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
67199 ** Write a set of frames to the log. The caller must hold the write-lock
67204 int szPage, /* Database page-size in bytes */
67222 assert( pWal->writeLock );
67229 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
67231 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
67236 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
67237 iFirst = pLive->mxFrame+1;
67241 ** log file, instead of appending to it at pWal->hdr.mxFrame.
67251 iFrame = pWal->hdr.mxFrame;
67253 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
67254 u32 aCksum[2]; /* Checksum for wal-header */
67259 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
67260 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
67261 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
67262 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
67266 pWal->szPage = szPage;
67267 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
67268 pWal->hdr.aFrameCksum[0] = aCksum[0];
67269 pWal->hdr.aFrameCksum[1] = aCksum[1];
67270 pWal->truncateOnCommit = 1;
67272 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
67273 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
67280 ** an out-of-order write following a WAL restart could result in
67285 if( pWal->syncHeader ){
67286 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67290 assert( (int)pWal->szPage==szPage );
67294 w.pFd = pWal->pWalFd;
67302 for(p=pList; p; p=p->pDirty){
67307 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
67309 if( iFirst && (p->pDirty || isCommit==0) ){
67311 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
67316 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
67317 pWal->iReCksum = iWrite;
67322 pData = p->pData;
67324 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
67326 p->flags &= ~PGHDR_WAL_APPEND;
67333 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
67338 p->flags |= PGHDR_WAL_APPEND;
67342 if( isCommit && pWal->iReCksum ){
67363 if( pWal->padToSectorBoundary ){
67364 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
67365 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
67386 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
67387 i64 sz = pWal->mxWalSize;
67388 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
67392 pWal->truncateOnCommit = 0;
67395 /* Append data to the wal-index. It is not necessary to lock the
67396 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
67400 iFrame = pWal->hdr.mxFrame;
67401 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
67402 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
67404 rc = walIndexAppend(pWal, iFrame, p->pgno);
67409 nExtra--;
67410 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
67415 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
67418 pWal->hdr.mxFrame = iFrame;
67420 pWal->hdr.iChange++;
67421 pWal->hdr.nPage = nTruncate;
67423 /* If this is a commit, update the wal-index header too. */
67426 pWal->iCallback = iFrame;
67443 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
67459 int isChanged = 0; /* True if a new wal-index header is loaded */
67463 assert( pWal->ckptLock==0 );
67464 assert( pWal->writeLock==0 );
67466 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67470 if( pWal->readOnly ) return SQLITE_READONLY;
67474 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
67478 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
67480 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
67483 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
67491 pWal->ckptLock = 1;
67493 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
67497 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
67498 ** immediately, and a busy-handler is configured, it is invoked and the
67499 ** writer lock retried until either the busy-handler returns 0 or the
67506 pWal->writeLock = 1;
67516 /* Read the wal-index header. */
67521 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
67522 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
67529 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
67531 …sqlite3_snprintf(sizeof(zMsg), zMsg, "mis-match between pageSize=%d and bufferSize=%d, mxFrame=%u",
67532 walPagesize(pWal),nBuf,pWal->hdr.mxFrame);
67533 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pWal->hdr.nPage, 0, CORRUPT_TYPE_FRAME_WAL,
67534 -1, 0, zMsg, NULL);
67542 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
67543 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
67548 /* If a new wal-index header was loaded before the checkpoint was
67549 ** performed, then the pager-cache associated with pWal is now
67550 ** out of date. So zero the cached wal-index header to ensure that
67554 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
67562 if( pWal->ckptLock ){
67564 pWal->ckptLock = 0;
67570 sqlite3OsCurrentTimeInt64(db->pVfs, &g_lastCkptTime);
67587 ret = pWal->iCallback;
67588 pWal->iCallback = 0;
67599 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
67601 ** transition out of exclusive-mode is successful, return 1. This
67606 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
67608 ** WAL is already in exclusive-locking mode - meaning that this
67609 ** routine is a no-op. The pager must already hold the exclusive lock
67612 ** If op is negative, then do a dry-run of the op==1 case but do
67619 assert( pWal->writeLock==0 );
67620 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
67622 /* pWal->readLock is usually set, but might be -1 if there was a
67623 ** prior error while attempting to acquire are read-lock. This cannot
67626 ** upgrade to exclusive-mode following such an error.
67628 assert( pWal->readLock>=0 || pWal->lockError );
67629 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
67632 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
67633 pWal->exclusiveMode = WAL_NORMAL_MODE;
67634 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
67635 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67637 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67643 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
67644 assert( pWal->readLock>=0 );
67645 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
67646 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67649 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67655 ** Return true if the argument is non-NULL and the WAL module is using
67656 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
67657 ** WAL module is using shared-memory, return false.
67660 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
67673 assert( pWal->readLock>=0 && pWal->writeLock==0 );
67675 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
67683 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
67690 /* Try to open on pSnapshot when the next read-transaction starts
67696 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
67700 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
67709 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
67710 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
67711 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
67712 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
67732 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
67733 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
67757 ** read-lock. This function returns the database page-size if it is known,
67761 assert( pWal==0 || pWal->readLock>=0 );
67762 return (pWal ? pWal->szPage : 0);
67769 return pWal->pWalFd;
67806 ** This file implements an external (disk-based) database using BTrees.
67810 ** "Sorting And Searching", pages 473-480. Addison-Wesley
67816 ** ----------------------------------------------------------------
67817 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
67818 ** ----------------------------------------------------------------
67823 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
67845 ** page, or a pointer-map page.
67864 ** 40 60 15 4-byte meta values passed to higher layers
67869 ** 52 4 Largest root-page (auto/incr_vacuum)
67870 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
67873 ** 68 4 Application-ID
67875 ** 92 4 The version-valid-for number
67878 ** All of the integer values are big-endian (most significant byte first).
67886 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
67901 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
67904 ** |----------------|
67906 ** |----------------|
67908 ** |----------------|
67912 ** |----------------|
67915 ** |----------------| ^ Grows upwards
67918 ** |----------------|
67937 ** The cell pointer array contains zero or more 2-byte numbers which are
67968 ** appears first. A variable-length integer may not be more than 9 bytes long.
67970 ** allows a 64-bit integer to be encoded in 9 bytes.
67993 ** filled with data (pagesize - 4 bytes). The last page can have as little
68016 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
68023 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
68034 ** You can change this value at compile-time by specifying a
68035 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
68036 ** header must be exactly 16 bytes including the zero-terminator so
68048 ** first byte of on-disk image of every BTree page.
68058 ** is derived from the raw on-disk page content.
68065 ** stored in MemPage.pBt->mutex.
68069 u8 intKey; /* True if table b-trees. False for index b-trees */
68082 int nFree; /* Number of free bytes on the page. -1 for unknown */
68085 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
68086 ** non-overflow cell */
68090 u8 *aDataEnd; /* One byte past the end of the entire page - not just
68092 ** corruption-induced buffer overflow. */
68148 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
68162 ** If the shared-data extension is enabled, there may be multiple users
68174 # error wrong numeric code for no-transaction
68177 # error wrong numeric code for read-transaction
68180 # error wrong numeric code for write-transaction
68203 ** If a BtShared client fails to obtain a write-lock on a database
68204 ** table (because there exists one or more read-locks on the table),
68205 ** the shared-cache enters 'pending-lock' state and isPending is
68208 ** The shared-cache leaves the 'pending lock' state when either of
68214 ** while in the 'pending-lock' state, no connection may start a new
68217 ** This feature is included to help prevent writer-starvation.
68226 u8 autoVacuum; /* True if auto-vacuum is enabled */
68227 u8 incrVacuum; /* True if incr-vacuum is enabled */
68231 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
68234 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
68235 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
68244 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
68245 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
68249 BtLock *pLock; /* List of locks held on this shared-btree struct */
68265 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
68267 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
68279 u16 nSize; /* Size of the cell content on the main b-tree page */
68283 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
68286 ** root-node and 3 for all other internal nodes.
68295 ** b-tree within a database file.
68305 ** found at self->pBt->mutex.
68313 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
68314 ** sqlite3BtreePrevious() is no-op if skipNext<0.
68339 u8 curIntKey; /* Value of apPage[0]->intKey */
68341 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
68344 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
68370 ** Cursor is valid except that the Cursor.skipNext field is non-zero
68372 ** operation should be a no-op.
68397 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
68400 ** These macros define the location of the pointer-map entry for a
68405 ** PTRMAP_PAGENO returns the database page number of the pointer-map
68409 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
68411 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
68415 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
68432 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
68435 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
68443 ** overflow pages. The page-number identifies the previous
68446 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
68459 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
68460 assert( p->pBt->inTransaction>=p->inTrans );
68465 ** if the database supports auto-vacuum or not. Because it is used
68471 #define ISAUTOVACUUM (pBt->autoVacuum)
68482 ** the database. As the integrity-check proceeds, for each page used in
68483 ** the database the corresponding bit is set. This allows integrity-check to
68490 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
68500 u32 *heap; /* Min-heap used for analyzing cell coverage */
68505 ** Routines to read or write a two- and four-byte big-endian integer values.
68514 ** two-byte aligned address. get2bytea() is only used for accessing the
68533 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
68535 ** p->locked boolean to true.
68538 assert( p->locked==0 );
68539 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
68540 assert( sqlite3_mutex_held(p->db->mutex) );
68542 sqlite3_mutex_enter(p->pBt->mutex);
68543 p->pBt->db = p->db;
68544 p->locked = 1;
68548 ** Release the BtShared mutex associated with B-Tree handle p and
68549 ** clear the p->locked boolean.
68552 BtShared *pBt = p->pBt;
68553 assert( p->locked==1 );
68554 assert( sqlite3_mutex_held(pBt->mutex) );
68555 assert( sqlite3_mutex_held(p->db->mutex) );
68556 assert( p->db==pBt->db );
68558 sqlite3_mutex_leave(pBt->mutex);
68559 p->locked = 0;
68569 ** and this routine is a no-op. The underlying mutex is non-recursive.
68574 ** by all database connections. The p->pNext is a list of other
68577 ** p, then first unlock all of the others on p->pNext, then wait
68586 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
68587 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
68588 assert( p->pNext==0 || p->pNext->db==p->db );
68589 assert( p->pPrev==0 || p->pPrev->db==p->db );
68590 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
68593 assert( !p->locked || p->wantToLock>0 );
68594 assert( p->sharable || p->wantToLock==0 );
68597 assert( sqlite3_mutex_held(p->db->mutex) );
68601 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
68603 if( !p->sharable ) return;
68604 p->wantToLock++;
68605 if( p->locked ) return;
68622 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
68623 p->pBt->db = p->db;
68624 p->locked = 1;
68633 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68634 assert( pLater->sharable );
68635 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
68636 assert( !pLater->locked || pLater->wantToLock>0 );
68637 if( pLater->locked ){
68642 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68643 if( pLater->wantToLock ){
68654 assert( sqlite3_mutex_held(p->db->mutex) );
68655 if( p->sharable ){
68656 assert( p->wantToLock>0 );
68657 p->wantToLock--;
68658 if( p->wantToLock==0 ){
68667 ** B-Tree is not marked as sharable.
68672 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
68673 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
68674 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
68675 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
68677 return (p->sharable==0 || p->locked);
68689 ** There is a corresponding leave-all procedures.
68700 assert( sqlite3_mutex_held(db->mutex) );
68701 for(i=0; i<db->nDb; i++){
68702 p = db->aDb[i].pBt;
68703 if( p && p->sharable ){
68708 db->noSharedCache = skipOk;
68711 if( db->noSharedCache==0 ) btreeEnterAll(db);
68716 assert( sqlite3_mutex_held(db->mutex) );
68717 for(i=0; i<db->nDb; i++){
68718 p = db->aDb[i].pBt;
68723 if( db->noSharedCache==0 ) btreeLeaveAll(db);
68735 if( !sqlite3_mutex_held(db->mutex) ){
68738 for(i=0; i<db->nDb; i++){
68740 p = db->aDb[i].pBt;
68741 if( p && p->sharable &&
68742 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
68753 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
68757 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
68765 if( db->pVfs==0 && db->nDb==0 ) return 1;
68767 assert( iDb>=0 && iDb<db->nDb );
68768 if( !sqlite3_mutex_held(db->mutex) ) return 0;
68770 p = db->aDb[iDb].pBt;
68772 return p->sharable==0 || p->locked==1;
68780 ** these two routines, all mutex operations are no-ops in that case and
68784 ** the ones below, are no-ops and are null #defines in btree.h.
68788 p->pBt->db = p->db;
68792 for(i=0; i<db->nDb; i++){
68793 Btree *p = db->aDb[i].pBt;
68795 p->pBt->db = p->db;
68810 sqlite3BtreeEnter(pCur->pBtree);
68814 sqlite3BtreeLeave(pCur->pBtree);
68834 ** This file implements an external (disk-based) database using BTrees.
68858 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
68863 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
68866 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
68879 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
68924 ** shared-cache table level locks. If the library is compiled with the
68925 ** shared-cache feature disabled, then there is only ever one user
68927 ** So define the lock related functions as no-ops.
68942 u64 n = pBt->nSeek;
68943 pBt->nSeek = 0;
68950 if( pager==NULL || pager->pCodec ){
68962 if( iDbpage==1 && db->aDb ){
68963 Btree *p = db->aDb[0].pBt;
68964 if( p && p->pBt ){
68978 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
68986 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
68997 # define SQLITE_CORRUPT_PAGE(context,pMemPage) SQLITE_CORRUPT_PGNO((pMemPage)->pgno,(context))
69009 ** For example, when writing to a table with root-page iRoot via
69022 ** hold a write-lock on the schema table (root page 1). This is also
69027 Pgno iRoot, /* Root page of b-tree */
69028 int isIndex, /* True if iRoot is the root of an index b-tree */
69031 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
69036 ** and has the read-uncommitted flag set, then no lock is required.
69039 if( (pBtree->sharable==0)
69040 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
69047 ** the correct locks are held. So do not bother - just return true.
69050 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
69054 /* Figure out the root-page that the lock should be held on. For table
69055 ** b-trees, this is just the root page of the b-tree being read or
69056 ** written. For index b-trees, it is the root page of the associated
69061 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
69063 if( pIdx->tnum==iRoot ){
69070 iTab = pIdx->pTable->tnum;
69078 /* Search for the required lock. Either a write-lock on root-page iTab, a
69079 ** write-lock on the schema table, or (if the client is reading) a
69080 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
69081 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
69082 if( pLock->pBtree==pBtree
69083 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
69084 && pLock->eLock>=eLockType
69106 ** read-uncommitted flag set, then it is OK for the other object to
69116 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69117 if( p->pgnoRoot==iRoot
69118 && p->pBtree!=pBtree
69119 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
69130 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
69135 BtShared *pBt = p->pBt;
69140 assert( p->db!=0 );
69141 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
69143 /* If requesting a write-lock, then the Btree must have an open write
69147 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
69148 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
69150 /* This routine is a no-op if the shared-cache is not enabled */
69151 if( !p->sharable ){
69158 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
69159 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
69163 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69164 /* The condition (pIter->eLock!=eLock) in the following if(...)
69167 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
69173 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
69174 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
69175 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
69176 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
69178 assert( p==pBt->pWriter );
69179 pBt->btsFlags |= BTS_PENDING;
69190 ** Add a lock on the table with root-page iTable to the shared-btree used
69207 BtShared *pBt = p->pBt;
69213 assert( p->db!=0 );
69215 /* A connection with the read-uncommitted flag set will never try to
69216 ** obtain a read-lock using this function. The only read-lock obtained
69217 ** by a connection in read-uncommitted mode is on the sqlite_schema
69219 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
69221 /* This function should only be called on a sharable b-tree after it
69222 ** has been determined that no other b-tree holds a conflicting lock. */
69223 assert( p->sharable );
69227 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69228 if( pIter->iTable==iTable && pIter->pBtree==p ){
69242 pLock->iTable = iTable;
69243 pLock->pBtree = p;
69244 pLock->pNext = pBt->pLock;
69245 pBt->pLock = pLock;
69249 ** and the requested lock. This means if a write-lock was already held
69250 ** and a read-lock requested, we don't incorrectly downgrade the lock.
69253 if( eLock>pLock->eLock ){
69254 pLock->eLock = eLock;
69271 BtShared *pBt = p->pBt;
69272 BtLock **ppIter = &pBt->pLock;
69275 assert( p->sharable || 0==*ppIter );
69276 assert( p->inTrans>0 );
69280 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
69281 assert( pLock->pBtree->inTrans>=pLock->eLock );
69282 if( pLock->pBtree==p ){
69283 *ppIter = pLock->pNext;
69284 assert( pLock->iTable!=1 || pLock==&p->lock );
69285 if( pLock->iTable!=1 ){
69289 ppIter = &pLock->pNext;
69293 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
69294 if( pBt->pWriter==p ){
69295 pBt->pWriter = 0;
69296 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69297 }else if( pBt->nTransaction==2 ){
69307 pBt->btsFlags &= ~BTS_PENDING;
69312 ** This function changes all write-locks held by Btree p into read-locks.
69315 BtShared *pBt = p->pBt;
69316 if( pBt->pWriter==p ){
69318 pBt->pWriter = 0;
69319 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69320 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
69321 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
69322 pLock->eLock = READ_LOCK;
69340 return sqlite3_mutex_held(p->pBt->mutex);
69344 ** database connetion. This is important in shared-cache mode. If the database
69345 ** connection pointers get out-of-sync, it is possible for routines like
69348 ** statements only and for the purpose of double-checking that the btree code
69349 ** does keep the database connection pointers up-to-date.
69353 return (p->pBtree->db==p->pBt->db);
69361 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
69364 ** Invalidate the overflow page-list cache for all cursors opened
69369 assert( sqlite3_mutex_held(pBt->mutex) );
69370 for(p=pBt->pCursor; p; p=p->pNext){
69383 ** cursors open on any row within the table with root-page pgnoRoot.
69396 assert( pBtree->hasIncrblobCur );
69398 pBtree->hasIncrblobCur = 0;
69399 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69400 if( (p->curFlags & BTCF_Incrblob)!=0 ){
69401 pBtree->hasIncrblobCur = 1;
69402 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
69403 p->eState = CURSOR_INVALID;
69416 ** when a page that previously contained data becomes a free-list leaf
69421 ** free-list leaf pages:
69424 ** a free-list leaf page, the page is not written to the database
69425 ** (as free-list leaf pages contain no meaningful data). Sometimes
69429 ** 2) When a free-list leaf page is reused, its content is not read
69435 ** a page is moved to the free-list and then reused within the same
69437 ** it is moved to the free-list and it is also not journalled when it
69438 ** is extracted from the free-list and reused, then the original data
69443 ** moved to become a free-list leaf page, the corresponding bit is
69444 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
69451 if( !pBt->pHasContent ){
69452 assert( pgno<=pBt->nPage );
69453 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
69454 if( !pBt->pHasContent ){
69458 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
69459 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
69467 ** This function is called when a free-list leaf page is removed from the
69468 ** free-list for reuse. It returns false if it is safe to retrieve the
69469 ** page from the pager layer with the 'no-content' flag set. True otherwise.
69472 Bitvec *p = pBt->pHasContent;
69478 ** invoked at the conclusion of each write-transaction.
69481 sqlite3BitvecDestroy(pBt->pHasContent);
69482 pBt->pHasContent = 0;
69490 if( pCur->iPage>=0 ){
69491 for(i=0; i<pCur->iPage; i++){
69492 releasePageNotNull(pCur->apPage[i]);
69494 releasePageNotNull(pCur->pPage);
69495 pCur->iPage = -1;
69502 ** function saves the current cursor key in variables pCur->nKey and
69503 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
69507 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
69508 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
69509 ** set to point to a malloced buffer pCur->nKey bytes in size containing
69514 assert( CURSOR_VALID==pCur->eState );
69515 assert( 0==pCur->pKey );
69518 if( pCur->curIntKey ){
69520 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
69525 ** up to the size of 1 varint plus 1 8-byte value when the cursor
69529 pCur->nKey = sqlite3BtreePayloadSize(pCur);
69530 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
69532 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
69534 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
69535 pCur->pKey = pKey;
69543 assert( !pCur->curIntKey || !pCur->pKey );
69557 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
69558 assert( 0==pCur->pKey );
69561 if( pCur->curFlags & BTCF_Pinned ){
69564 if( pCur->eState==CURSOR_SKIPNEXT ){
69565 pCur->eState = CURSOR_VALID;
69567 pCur->skipNext = 0;
69573 pCur->eState = CURSOR_REQUIRESEEK;
69576 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
69585 ** the table with root-page iRoot. "Saving the cursor position" means that
69596 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
69606 assert( sqlite3_mutex_held(pBt->mutex) );
69607 assert( pExcept==0 || pExcept->pBt==pBt );
69608 for(p=pBt->pCursor; p; p=p->pNext){
69609 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
69612 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
69627 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
69628 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
69634 testcase( p->iPage>=0 );
69638 p = p->pNext;
69648 sqlite3_free(pCur->pKey);
69649 pCur->pKey = 0;
69650 pCur->eState = CURSOR_INVALID;
69669 KeyInfo *pKeyInfo = pCur->pKeyInfo;
69674 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
69677 pIdxKey->nField,pKeyInfo->nAllField);
69678 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pCur->pBt->nPage, 0, CORRUPT_TYPE_PAGE_BTRE…
69679 -1, 0, zMsg, NULL);
69684 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
69703 assert( pCur->eState>=CURSOR_REQUIRESEEK );
69704 if( pCur->eState==CURSOR_FAULT ){
69705 return pCur->skipNext;
69707 pCur->eState = CURSOR_INVALID;
69711 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
69714 sqlite3_free(pCur->pKey);
69715 pCur->pKey = 0;
69716 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
69717 if( skipNext ) pCur->skipNext = skipNext;
69718 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
69719 pCur->eState = CURSOR_SKIPNEXT;
69726 (p->eState>=CURSOR_REQUIRESEEK ? \
69746 assert( sizeof(pCur->eState)==1 );
69778 assert( pCur->eState!=CURSOR_VALID );
69784 if( pCur->eState!=CURSOR_VALID ){
69808 pCur->hints = x;
69815 ** number for the pointer-map page that contains the entry for the
69825 assert( sqlite3_mutex_held(pBt->mutex) );
69827 nPagesPerMapPage = (pBt->usableSize/5)+1;
69828 iPtrMap = (pgno-2)/nPagesPerMapPage;
69842 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
69843 ** a no-op. If an error occurs, the appropriate error code is written
69855 assert( sqlite3_mutex_held(pBt->mutex) );
69856 /* The super-journal page number must never be used as a pointer map page */
69859 assert( pBt->autoVacuum );
69865 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69879 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, iPtrmap, CORRUPT_TYPE_PAGE_PTR_…
69880 -1, 0, zMsg, NULL);
69888 iPtrmap, key, pBt->usableSize);
69889 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, iPtrmap, CORRUPT_TYPE_PAGE_PTR_…
69890 -1, 0, zMsg, NULL);
69894 assert( offset <= (int)pBt->usableSize-5 );
69898 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
69924 assert( sqlite3_mutex_held(pBt->mutex) );
69927 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69938 iPtrmap, key, pBt->usableSize);
69939 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, iPtrmap, CORRUPT_TYPE_PAGE_PTR_…
69940 -1, 0, zMsg, NULL);
69943 assert( offset <= (int)pBt->usableSize-5 );
69954 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, iPtrmap, CORRUPT_TYPE_PAGE_PTR_…
69973 ** 4-byte child pointer found on interior pages, if there is one.
69978 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69980 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69986 ** on a single B-tree page. Make necessary adjustments to the CellInfo
70007 minLocal = pPage->minLocal;
70008 maxLocal = pPage->maxLocal;
70009 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
70013 pInfo->nLocal = (u16)surplus;
70015 pInfo->nLocal = (u16)minLocal;
70017 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
70026 maxLocal = pPage->maxLocal;
70032 minLocal = pPage->minLocal;
70033 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
70057 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70058 assert( pPage->leaf==0 );
70059 assert( pPage->childPtrSize==4 );
70063 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
70064 pInfo->nPayload = 0;
70065 pInfo->nLocal = 0;
70066 pInfo->pPayload = 0;
70078 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70079 assert( pPage->leaf==0 || pPage->leaf==1 );
70080 assert( pPage->intKeyLeaf );
70081 assert( pPage->childPtrSize==0 );
70102 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
70105 ** This routine is a high-runner.
70135 pInfo->nKey = *(i64*)&iKey;
70136 pInfo->nPayload = nPayload;
70137 pInfo->pPayload = pIter;
70138 testcase( nPayload==pPage->maxLocal );
70139 testcase( nPayload==(u32)pPage->maxLocal+1 );
70140 if( nPayload<=pPage->maxLocal ){
70144 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70145 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70146 pInfo->nLocal = (u16)nPayload;
70159 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70160 assert( pPage->leaf==0 || pPage->leaf==1 );
70161 assert( pPage->intKeyLeaf==0 );
70162 pIter = pCell + pPage->childPtrSize;
70172 pInfo->nKey = nPayload;
70173 pInfo->nPayload = nPayload;
70174 pInfo->pPayload = pIter;
70175 testcase( nPayload==pPage->maxLocal );
70176 testcase( nPayload==(u32)pPage->maxLocal+1 );
70177 if( nPayload<=pPage->maxLocal ){
70181 pInfo->nSize = nPayload + (u16)(pIter - pCell);
70182 if( pInfo->nSize<4 ) pInfo->nSize = 4;
70183 pInfo->nLocal = (u16)nPayload;
70193 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
70201 ** data area of the btree-page. The return number includes the cell
70210 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
70220 pPage->xParseCell(pPage, pCell, &debuginfo);
70232 testcase( nSize==pPage->maxLocal );
70233 testcase( nSize==(u32)pPage->maxLocal+1 );
70234 if( nSize<=pPage->maxLocal ){
70235 nSize += (u32)(pIter - pCell);
70238 int minLocal = pPage->minLocal;
70239 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70240 testcase( nSize==pPage->maxLocal );
70241 testcase( nSize==(u32)pPage->maxLocal+1 );
70242 if( nSize>pPage->maxLocal ){
70245 nSize += 4 + (u16)(pIter - pCell);
70260 pPage->xParseCell(pPage, pCell, &debuginfo);
70265 assert( pPage->childPtrSize==4 );
70268 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
70269 return (u16)(pIter - pCell);
70282 pPage->xParseCell(pPage, pCell, &debuginfo);
70294 /* pIter now points at the 64-bit integer key value, a variable length
70305 testcase( nSize==pPage->maxLocal );
70306 testcase( nSize==(u32)pPage->maxLocal+1 );
70307 if( nSize<=pPage->maxLocal ){
70308 nSize += (u32)(pIter - pCell);
70311 int minLocal = pPage->minLocal;
70312 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70313 testcase( nSize==pPage->maxLocal );
70314 testcase( nSize==(u32)pPage->maxLocal+1 );
70315 if( nSize>pPage->maxLocal ){
70318 nSize += 4 + (u16)(pIter - pCell);
70329 return pPage->xCellSize(pPage, findCell(pPage, iCell));
70337 ** pointer to an overflow page, insert an entry into the pointer-map for
70344 pPage->xParseCell(pPage, pCell, &info);
70347 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
70351 (void)sqlite3base16Encode(pCell, info.nSize - info.nLocal - 4, xBuffer, sizeof(xBuffer));
70353 (int)(pCell - pPage->aData), (int)(pSrc->aDataEnd - pCell), info.nSize, xBuffer);
70354 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70355 pCell - pPage->aData, info.nSize, zMsg, NULL);
70359 ovfl = get4byte(&pCell[info.nSize-4]);
70360 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
70368 ** page so that there are no free-blocks on the free-block list.
70373 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
70374 ** b-tree page so that there are no freeblocks or fragment bytes, all
70380 int pc; /* Address of the i-th cell */
70394 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70395 assert( pPage->pBt!=0 );
70396 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
70397 assert( pPage->nOverflow==0 );
70398 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70399 data = pPage->aData;
70400 hdr = pPage->hdrOffset;
70401 cellOffset = pPage->cellOffset;
70402 nCell = pPage->nCell;
70405 usableSize = pPage->pBt->usableSize;
70410 ** offsets to each pointer in the cell-pointer array than it is to
70414 if( iFree>usableSize-4 ){
70420 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70426 if( iFree2>usableSize-4 ){
70433 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70450 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70460 … "the 1st 2 freeblocks mis-order, 1st block offset:%d, size:%d, 2nd block offset:%d, base16:%s",
70462 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70474 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70478 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
70486 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70492 assert( cbrk+(iFree-top) <= usableSize );
70493 memmove(&data[cbrk], &data[top], iFree-top);
70505 iCellLast = usableSize - 4;
70508 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
70509 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
70512 u8 *pAddr; /* The i-th cell pointer */
70524 … sqlite3_snprintf(sizeof(zMsg), zMsg, "%d-th cell pointer:%d out of range[%d, %d], base16:%s",
70526 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70531 size = pPage->xCellSize(pPage, &src[pc]);
70532 cbrk -= size;
70535 …sqlite3_snprintf(sizeof(zMsg), zMsg, "move %d-th cell from %d using unexpected size:%d", i, pc, si…
70536 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70537 -1, 0, zMsg, NULL);
70550 assert( pPage->nFree>=0 );
70551 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
70555 (int)data[hdr+7], cbrk-iCellFirst, pPage->nFree);
70556 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
70557 -1, 0, zMsg, NULL);
70564 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
70565 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70570 ** Search the free-list on page pPg for space to store a cell nByte bytes in
70572 ** from the free-list.
70574 ** If no suitable space can be found on the free-list, return NULL.
70583 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ // search on B-tree page
70584 const int hdr = pPg->hdrOffset; /* Offset to page header */
70585 u8 * const aData = pPg->aData; /* Page data */
70590 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
70595 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
70596 ** freeblock form a big-endian integer which is the size of the freeblock
70597 ** in bytes, including the 4-byte header. */
70600 if( (x = size - nByte)>=0 ){
70604 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
70608 /* Remove the slot from the free-list. Update the number of
70620 x, pc, nByte, pPg->pBt->usableSize, xBuffer);
70621 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPg->pgno, CORRUPT_TYPE_PAGE_BTREE_LEAF,
70626 /* The slot remains on the free-list. Reduce its size to account
70643 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPg->pgno, CORRUPT_TYPE_PAGE_BTREE_LEAF,
70650 if( pc>maxPC+nByte-4 ){
70653 sqlite3_snprintf(sizeof(zMsg), zMsg, "free slot:%d overflow, end:%d", pc, maxPC+nByte-4);
70654 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPg->pgno, CORRUPT_TYPE_PAGE_BTREE_LEAF,
70655 -1, 0, zMsg, NULL);
70662 ** Allocate nByte bytes of space from within the B-Tree page passed
70663 ** as the first argument. Write into *pIdx the index into pPage->aData[]
70675 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
70676 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
70682 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70683 assert( pPage->pBt );
70684 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70686 assert( pPage->nFree>=nByte );
70687 assert( pPage->nOverflow==0 );
70688 assert( nByte < (int)(pPage->pBt->usableSize-8) );
70690 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
70691 gap = pPage->cellOffset + 2*pPage->nCell;
70693 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
70696 ** However, that integer is too large to be stored in a 2-byte unsigned
70700 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
70702 if( top==0 && pPage->pBt->usableSize==65536 ){
70709 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70726 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
70727 *pIdx = g2 = (int)(pSpace-data);
70731 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70732 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
70747 assert( pPage->nCell>0 || CORRUPT_DB );
70748 assert( pPage->nFree>=0 );
70749 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
70762 top -= nByte;
70764 assert( top+nByte <= (int)pPage->pBt->usableSize );
70770 ** Return a section of the pPage->aData to the freelist.
70771 ** The first byte of the new free block is pPage->aData[iStart]
70790 unsigned char *data = pPage->aData; /* Page content */
70793 assert( pPage->pBt!=0 );
70794 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70795 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
70796 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
70797 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70799 assert( iStart<=pPage->pBt->usableSize-4 );
70804 hdr = pPage->hdrOffset;
70815 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70816 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
70821 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
70826 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70839 nFrag = iFreeBlk - iEnd;
70844 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70845 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
70849 if( iEnd > pPage->pBt->usableSize ){
70854 (int)iFreeBlk, iEnd, pPage->pBt->usableSize, xBuffer);
70855 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70859 iSize = iEnd - iStart;
70874 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70875 CORRUPT_TYPE_PAGE_BTREE_LEAF, iPtr, iPtrEnd - iPtr, zMsg, NULL);
70878 nFrag += iStart - iPtrEnd;
70879 iSize = iEnd - iPtr;
70887 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70888 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
70891 data[hdr+7] -= nFrag;
70903 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70910 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
70920 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
70927 pPage->nFree += iOrigSize;
70944 BtShared *pBt; /* A copy of pPage->pBt */
70946 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
70947 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70948 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
70950 pPage->childPtrSize = 4-4*pPage->leaf;
70951 pBt = pPage->pBt;
70953 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
70954 ** interior table b-tree page. */
70956 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
70957 ** leaf table b-tree page. */
70959 pPage->intKey = 1;
70960 if( pPage->leaf ){
70961 pPage->intKeyLeaf = 1;
70962 pPage->xCellSize = cellSizePtrTableLeaf;
70963 pPage->xParseCell = btreeParseCellPtr;
70965 pPage->intKeyLeaf = 0;
70966 pPage->xCellSize = cellSizePtrNoPayload;
70967 pPage->xParseCell = btreeParseCellPtrNoPayload;
70969 pPage->maxLocal = pBt->maxLeaf;
70970 pPage->minLocal = pBt->minLeaf;
70972 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
70973 ** interior index b-tree page. */
70975 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
70976 ** leaf index b-tree page. */
70978 pPage->intKey = 0;
70979 pPage->intKeyLeaf = 0;
70980 pPage->xCellSize = cellSizePtr;
70981 pPage->xParseCell = btreeParseCellPtrIndex;
70982 pPage->maxLocal = pBt->maxLocal;
70983 pPage->minLocal = pBt->minLocal;
70985 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
70987 pPage->intKey = 0;
70988 pPage->intKeyLeaf = 0;
70989 pPage->xCellSize = cellSizePtr;
70990 pPage->xParseCell = btreeParseCellPtrIndex;
70995 pPage->pgno,
70997 pPage->isInit,
70998 pPage->intKey,
70999 pPage->intKeyLeaf,
71000 pPage->leaf,
71001 pPage->childPtrSize,
71002 pPage->cellOffset,
71003 pPage->nCell,
71004 pPage->hdrOffset,
71005 pPage->minLocal,
71006 pPage->maxLocal,
71011 (void)sqlite3base16Encode(pPage->aData, 8, xBuffer, sizeof(xBuffer));
71013 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71014 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71017 pPage->max1bytePayload = pBt->max1bytePayload;
71023 ** in the pPage->nFree field.
71026 int pc; /* Address of a freeblock within pPage->aData[] */
71028 u8 *data; /* Equal to pPage->aData */
71035 assert( pPage->pBt!=0 );
71036 assert( pPage->pBt->db!=0 );
71037 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71038 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
71039 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
71040 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
71041 assert( pPage->isInit==1 );
71042 assert( pPage->nFree<0 );
71044 usableSize = pPage->pBt->usableSize;
71045 hdr = pPage->hdrOffset;
71046 data = pPage->aData;
71047 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
71051 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
71052 iCellLast = usableSize - 4;
71055 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
71059 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
71063 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
71068 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71069 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71077 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71078 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71091 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71092 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71099 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71100 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71106 ** of the cell-content area plus the number of free bytes within
71107 ** the cell-content area. If this is greater than the usable-size
71109 ** serves to verify that the offset to the start of the cell-content
71117 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71118 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71121 pPage->nFree = (u16)(nFree - iCellFirst);
71134 int pc; /* Address of a freeblock within pPage->aData[] */
71135 u8 *data; /* Equal to pPage->aData */
71139 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
71140 usableSize = pPage->pBt->usableSize;
71141 iCellLast = usableSize - 4;
71142 data = pPage->aData;
71143 cellOffset = pPage->cellOffset;
71144 if( !pPage->leaf ) iCellLast--;
71145 for(i=0; i<pPage->nCell; i++){
71152 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71153 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71156 sz = pPage->xCellSize(pPage, &data[pc]);
71161 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
71162 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
71173 ** not contain a well-formed database page, then return
71175 ** guarantee that the page is well-formed. It only shows that
71179 u8 *data; /* Equal to pPage->aData */
71182 assert( pPage->pBt!=0 );
71183 assert( pPage->pBt->db!=0 );
71184 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71185 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
71186 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
71187 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
71188 assert( pPage->isInit==0 );
71190 pBt = pPage->pBt;
71191 data = pPage->aData + pPage->hdrOffset;
71192 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
71193 ** the b-tree page type. */
71197 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
71198 pPage->maskPage = (u16)(pBt->pageSize - 1);
71199 pPage->nOverflow = 0;
71200 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
71201 pPage->aCellIdx = data + pPage->childPtrSize + 8;
71202 pPage->aDataEnd = pPage->aData + pBt->pageSize;
71203 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
71204 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
71206 pPage->nCell = get2byte(&data[3]);
71207 if( pPage->nCell>MX_CELL(pBt) ){
71211 (int)pPage->nCell, pPage->pgno, (int)pPage->hdrOffset + 3, MX_CELL(pBt));
71212 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno, CORRUPT_TYP…
71213 -1, 0, zMsg, NULL);
71216 testcase( pPage->nCell==MX_CELL(pBt) );
71217 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
71221 assert( pPage->nCell>0
71222 || get2byteNotZero(&data[5])==(int)pBt->usableSize
71224 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
71225 pPage->isInit = 1;
71226 if( pBt->db->flags & SQLITE_CellSizeCk ){
71237 unsigned char *data = pPage->aData;
71238 BtShared *pBt = pPage->pBt;
71239 u8 hdr = pPage->hdrOffset;
71242 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
71243 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71244 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
71245 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71246 assert( sqlite3_mutex_held(pBt->mutex) );
71247 if( pBt->btsFlags & BTS_FAST_SECURE ){
71248 memset(&data[hdr], 0, pBt->usableSize - hdr);
71254 put2byte(&data[hdr+5], pBt->usableSize);
71255 pPage->nFree = (u16)(pBt->usableSize - first);
71257 pPage->cellOffset = first;
71258 pPage->aDataEnd = &data[pBt->pageSize];
71259 pPage->aCellIdx = &data[first];
71260 pPage->aDataOfst = &data[pPage->childPtrSize];
71261 pPage->nOverflow = 0;
71262 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
71263 pPage->maskPage = (u16)(pBt->pageSize - 1);
71264 pPage->nCell = 0;
71265 pPage->isInit = 1;
71275 if( pgno!=pPage->pgno ){
71276 pPage->aData = sqlite3PagerGetData(pDbPage);
71277 pPage->pDbPage = pDbPage;
71278 pPage->pBt = pBt;
71279 pPage->pgno = pgno;
71280 pPage->hdrOffset = pgno==1 ? 100 : 0;
71282 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
71307 assert( sqlite3_mutex_held(pBt->mutex) );
71308 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
71321 assert( sqlite3_mutex_held(pBt->mutex) );
71322 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
71331 ** error, return ((unsigned int)-1).
71334 return pBt->nPage;
71338 return btreePagecount(p->pBt);
71346 ** And if the fetch fails, this routine must decrement pCur->iPage.
71348 ** The page is fetched as read-write unless pCur is not NULL and is
71349 ** a read-only cursor.
71359 int bReadOnly /* True for a read-only page */
71363 assert( sqlite3_mutex_held(pBt->mutex) );
71364 assert( pCur==0 || ppPage==&pCur->pPage );
71365 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
71366 assert( pCur==0 || pCur->iPage>0 );
71371 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pgno, CORRUPT_TYPE_PAGE_BTREE_L…
71372 -1, 0, zMsg, NULL);
71376 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
71381 if( (*ppPage)->isInit==0 ){
71388 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
71389 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
71393 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
71395 …sqlite3_snprintf(sizeof(zMsg), zMsg, "check btree page, nCell:%u, intKey:%u, cursor->curIntKey:%u",
71396 (*ppPage)->nCell, (*ppPage)->intKey, pCur->curIntKey);
71397 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pgno, CORRUPT_TYPE_PAGE_BTREE_L…
71398 -1, 0, zMsg, NULL);
71408 pCur->iPage--;
71409 pCur->pPage = pCur->apPage[pCur->iPage];
71423 assert( pPage->aData );
71424 assert( pPage->pBt );
71425 assert( pPage->pDbPage!=0 );
71426 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71427 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71428 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71429 sqlite3PagerUnrefNotNull(pPage->pDbPage);
71436 assert( pPage->aData );
71437 assert( pPage->pBt );
71438 assert( pPage->pDbPage!=0 );
71439 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
71440 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
71441 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71442 sqlite3PagerUnrefPageOne(pPage->pDbPage);
71462 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
71467 (*ppPage)->isInit = 0;
71487 if( pPage->isInit ){
71488 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71489 pPage->isInit = 0;
71496 ** the call for every page that comes in for re-initing. */
71508 assert( pBt->db );
71509 assert( sqlite3_mutex_held(pBt->db->mutex) );
71510 int rc = sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
71513 DumpLocksByPager(pBt->pPager);
71524 ** be exclusively in memory, or it might use a disk-based memory cache.
71528 ** If zFilename is ":memory:" then an in-memory database is created
71541 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
71558 /* Set the variable isMemdb to true for an in-memory database, or
71559 ** false for a file-based database.
71571 assert( sqlite3_mutex_held(db->mutex) );
71590 p->inTrans = TRANS_NONE;
71591 p->db = db;
71593 p->lock.pBtree = p;
71594 p->lock.iTable = 1;
71605 int nFullPathname = pVfs->mxPathname+1;
71609 p->sharable = 1;
71635 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
71636 assert( pBt->nRef>0 );
71637 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
71638 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
71640 for(iDb=db->nDb-1; iDb>=0; iDb--){
71641 Btree *pExisting = db->aDb[iDb].pBt;
71642 if( pExisting && pExisting->pBt==pBt ){
71650 p->pBt = pBt;
71651 pBt->nRef++;
71665 p->sharable = 1;
71687 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
71690 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
71691 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
71696 pBt->openFlags = (u8)flags;
71697 pBt->db = db;
71698 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
71699 p->pBt = pBt;
71701 pBt->pCursor = 0;
71702 pBt->pPage1 = 0;
71703 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
71705 pBt->btsFlags |= BTS_SECURE_DELETE;
71707 pBt->btsFlags |= BTS_OVERWRITE;
71709 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71710 ** determined by the 2-byte integer located at an offset of 16 bytes from
71712 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
71713 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
71714 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
71715 pBt->pageSize = 0;
71717 /* If the magic name ":memory:" will create an in-memory database, then
71718 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
71721 ** regular file-name. In this case the auto-vacuum applies as per normal.
71724 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
71725 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
71730 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
71731 ** determined by the one-byte unsigned integer found at an offset of 20
71734 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71736 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
71737 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
71740 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
71742 pBt->usableSize = pBt->pageSize - nReserve;
71743 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
71748 pBt->nRef = 1;
71749 if( p->sharable ){
71753 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
71754 if( pBt->mutex==0 ){
71760 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
71772 if( p->sharable ){
71775 for(i=0; i<db->nDb; i++){
71776 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
71777 while( pSib->pPrev ){ pSib = pSib->pPrev; }
71778 if( (uptr)p->pBt<(uptr)pSib->pBt ){
71779 p->pNext = pSib;
71780 p->pPrev = 0;
71781 pSib->pPrev = p;
71783 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
71784 pSib = pSib->pNext;
71786 p->pNext = pSib->pNext;
71787 p->pPrev = pSib;
71788 if( p->pNext ){
71789 p->pNext->pPrev = p;
71791 pSib->pNext = p;
71802 if( pBt && pBt->pPager ){
71803 sqlite3PagerClose(pBt->pPager, 0);
71811 /* If the B-Tree was successfully opened, set the pager-cache size to the
71812 ** default value. Except, when opening on an existing shared pager-cache,
71813 ** do not change the pager-cache size.
71819 pFile = sqlite3PagerFile(pBt->pPager);
71820 if( pFile->pMethods ){
71821 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
71844 assert( sqlite3_mutex_notheld(pBt->mutex) );
71847 pBt->nRef--;
71848 if( pBt->nRef<=0 ){
71850 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
71853 while( ALWAYS(pList) && pList->pNext!=pBt ){
71854 pList=pList->pNext;
71857 pList->pNext = pBt->pNext;
71861 sqlite3_mutex_free(pBt->mutex);
71873 ** Make sure pBt->pTmpSpace points to an allocation of
71874 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
71879 assert( pBt->pTmpSpace==0 );
71882 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
71883 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
71884 if( pBt->pTmpSpace==0 ){
71885 BtCursor *pCur = pBt->pCursor;
71886 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
71891 /* One of the uses of pBt->pTmpSpace is to format cells before
71904 ** left-child pointer to the beginning of a cell.
71906 memset(pBt->pTmpSpace, 0, 8);
71907 pBt->pTmpSpace += 4;
71912 ** Free the pBt->pTmpSpace allocation
71915 if( pBt->pTmpSpace ){
71916 pBt->pTmpSpace -= 4;
71917 sqlite3PageFree(pBt->pTmpSpace);
71918 pBt->pTmpSpace = 0;
71926 BtShared *pBt = p->pBt;
71929 assert( sqlite3_mutex_held(p->db->mutex) );
71935 BtCursor *pCur = pBt->pCursor;
71938 pCur = pCur->pNext;
71939 assert( pTmp->pBtree!=p );
71946 ** The call to sqlite3BtreeRollback() drops any table-locks held by
71952 /* If there are still other outstanding references to the shared-btree
71954 ** up the shared-btree.
71956 assert( p->wantToLock==0 && p->locked==0 );
71957 if( !p->sharable || removeFromSharingList(pBt) ){
71963 assert( !pBt->pCursor );
71964 sqlite3PagerClose(pBt->pPager, p->db);
71965 if( pBt->xFreeSchema && pBt->pSchema ){
71966 pBt->xFreeSchema(pBt->pSchema);
71968 sqlite3DbFree(0, pBt->pSchema);
71974 assert( p->wantToLock==0 );
71975 assert( p->locked==0 );
71976 if( p->pPrev ) p->pPrev->pNext = p->pNext;
71977 if( p->pNext ) p->pNext->pPrev = p->pPrev;
71992 BtShared *pBt = p->pBt;
71993 assert( sqlite3_mutex_held(p->db->mutex) );
71995 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
72011 BtShared *pBt = p->pBt;
72013 assert( sqlite3_mutex_held(p->db->mutex) );
72015 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
72026 BtShared *pBt = p->pBt;
72027 assert( sqlite3_mutex_held(p->db->mutex) );
72029 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
72040 ** is a very low but non-zero probability of damage. Level 3 reduces the
72048 BtShared *pBt = p->pBt;
72049 assert( sqlite3_mutex_held(p->db->mutex) );
72051 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
72080 BtShared *pBt = p->pBt;
72083 pBt->nReserveWanted = nReserve;
72084 x = pBt->pageSize - pBt->usableSize;
72086 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
72092 ((pageSize-1)&pageSize)==0 ){
72094 assert( !pBt->pCursor );
72096 pBt->pageSize = (u32)pageSize;
72099 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
72100 pBt->usableSize = pBt->pageSize - (u16)nReserve;
72101 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72110 return p->pBt->pageSize;
72115 ** may only be called if it is guaranteed that the b-tree mutex is already
72119 ** known that the shared b-tree mutex is held, but the mutex on the
72126 assert( sqlite3_mutex_held(p->pBt->mutex) );
72127 n = p->pBt->pageSize - p->pBt->usableSize;
72138 ** The amount of reserve can only grow - never shrink.
72143 n1 = (int)p->pBt->nReserveWanted;
72158 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
72169 ** newFlag==(-1) No changes
72174 ** freelist leaf pages are not written back to the database. Thus in-page
72188 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
72189 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
72191 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
72197 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
72198 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
72199 ** is disabled. The default value for the auto-vacuum property is
72206 BtShared *pBt = p->pBt;
72211 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
72214 pBt->autoVacuum = av ?1:0;
72215 pBt->incrVacuum = av==2 ?1:0;
72223 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
72233 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
72234 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
72243 ** If the user has not set the safety-level for this database connection
72244 ** using "PRAGMA synchronous", and if the safety-level is not already
72253 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
72254 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
72255 if( pDb->bSyncSet==0
72256 && pDb->safety_level!=safety_level
72257 && pDb!=&db->aDb[1]
72259 pDb->safety_level = safety_level;
72260 sqlite3PagerSetFlags(pBt->pPager,
72261 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
72278 ** well-formed database file, then SQLITE_CORRUPT is returned.
72288 assert( sqlite3_mutex_held(pBt->mutex) );
72289 assert( pBt->pPage1==0 );
72290 rc = sqlite3PagerSharedLock(pBt->pPager);
72298 nPage = get4byte(28+(u8*)pPage1->aData);
72299 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
72300 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
72303 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
72309 u8 *page1 = pPage1->aData;
72311 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
72320 pBt->btsFlags |= BTS_READ_ONLY;
72327 pBt->btsFlags |= BTS_READ_ONLY;
72338 ** may not be the latest version - there may be a newer one in the log
72341 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
72343 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
72359 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
72368 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
72369 ** determined by the 2-byte integer located at an offset of 16 bytes from
72372 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
72374 if( ((pageSize-1)&pageSize)!=0
72380 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72382 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
72386 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
72387 ** determined by the one-byte unsigned integer found at an offset of 20
72389 usableSize = pageSize - page1[20];
72390 if( (u32)pageSize!=pBt->pageSize ){
72392 ** of BtShared.pageSize, we have discovered that the page-size is
72393 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
72395 ** again with the correct page-size.
72398 pBt->usableSize = usableSize;
72399 pBt->pageSize = pageSize;
72401 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
72402 pageSize-usableSize);
72406 if( sqlite3WritableSchema(pBt->db)==0 ){
72413 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
72419 pBt->pageSize = pageSize;
72420 pBt->usableSize = usableSize;
72422 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
72423 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
72429 ** cells can will fit on one page. We assume a 10-byte page header.
72431 ** 2-byte pointer to the cell
72432 ** 4-byte child pointer
72433 ** 9-byte nKey value
72434 ** 4-byte nData value
72435 ** 4-byte overflow page pointer
72436 ** So a cell consists of a 2-byte pointer, a header which is as much as
72440 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
72441 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
72442 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
72443 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
72444 if( pBt->maxLocal>127 ){
72445 pBt->max1bytePayload = 127;
72447 pBt->max1bytePayload = (u8)pBt->maxLocal;
72449 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
72450 pBt->pPage1 = pPage1;
72451 pBt->nPage = nPage;
72456 pBt->pPage1 = 0;
72476 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
72477 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
72478 && pCur->eState!=CURSOR_FAULT ) r++;
72490 ** If there is a transaction in progress, this routine is a no-op.
72493 assert( sqlite3_mutex_held(pBt->mutex) );
72494 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
72495 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
72496 MemPage *pPage1 = pBt->pPage1;
72497 assert( pPage1->aData );
72498 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
72499 pBt->pPage1 = 0;
72514 assert( sqlite3_mutex_held(pBt->mutex) );
72515 if( pBt->nPage>0 ){
72518 pP1 = pBt->pPage1;
72520 data = pP1->aData;
72521 rc = sqlite3PagerWrite(pP1->pDbPage);
72525 data[16] = (u8)((pBt->pageSize>>8)&0xff);
72526 data[17] = (u8)((pBt->pageSize>>16)&0xff);
72529 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
72530 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
72534 memset(&data[24], 0, 100-24);
72536 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72538 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
72539 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
72540 put4byte(&data[36 + 4*4], pBt->autoVacuum);
72541 put4byte(&data[36 + 7*4], pBt->incrVacuum);
72543 pBt->nPage = 1;
72556 p->pBt->nPage = 0;
72557 rc = newDatabase(p->pBt);
72563 ** Attempt to start a new transaction. A write-transaction
72564 ** is started if the second argument is nonzero, otherwise a read-
72568 ** upgraded to exclusive by calling this routine a second time - the
72571 ** A write-transaction must be started before attempting any
72585 ** if there is one. But if there was previously a read-lock, do not
72586 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
72587 ** returned when there is already a read-lock in order to avoid a deadlock.
72598 BtShared *pBt = p->pBt;
72599 Pager *pPager = pBt->pPager;
72605 /* If the btree is already in a write-transaction, or it
72606 ** is already in a read-transaction and a read-transaction
72607 ** is requested, this is a no-op.
72609 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
72612 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
72614 if( (p->db->flags & SQLITE_ResetDatabase)
72617 pBt->btsFlags &= ~BTS_READ_ONLY;
72620 /* Write transactions are not possible on a read-only database */
72621 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
72630 ** on this shared-btree structure and a second write transaction is
72633 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
72634 || (pBt->btsFlags & BTS_PENDING)!=0
72636 pBlock = pBt->pWriter->db;
72639 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
72640 if( pIter->pBtree!=p ){
72641 pBlock = pIter->pBtree->db;
72647 sqlite3ConnectionBlocked(p->db, pBlock);
72654 /* Any read-only or read-write transaction implies a read-lock on
72655 ** page 1. So if some other shared-cache client already has a write-lock
72660 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
72661 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
72663 sqlite3PagerWalDb(pPager, p->db);
72668 if( pBt->pPage1==0 && wrflag ){
72669 assert( pBt->inTransaction==TRANS_NONE );
72675 /* Call lockBtree() until either pBt->pPage1 is populated or
72677 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
72678 ** reading page 1 it discovers that the page-size of the database
72679 ** file is not pBt->pageSize. In this case lockBtree() will update
72680 ** pBt->pageSize to the page-size of the file on disk.
72682 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
72685 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
72688 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
72691 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
72704 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
72712 if( p->inTrans==TRANS_NONE ){
72713 pBt->nTransaction++;
72715 if( p->sharable ){
72716 assert( p->lock.pBtree==p && p->lock.iTable==1 );
72717 p->lock.eLock = READ_LOCK;
72718 p->lock.pNext = pBt->pLock;
72719 pBt->pLock = &p->lock;
72723 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
72724 if( p->inTrans>pBt->inTransaction ){
72725 pBt->inTransaction = p->inTrans;
72728 MemPage *pPage1 = pBt->pPage1;
72730 assert( !pBt->pWriter );
72731 pBt->pWriter = p;
72732 pBt->btsFlags &= ~BTS_EXCLUSIVE;
72733 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
72736 /* If the db-size header field is incorrect (as it may be if an old
72739 ** re-read the database size from page 1 if a savepoint or transaction
72742 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
72743 rc = sqlite3PagerWrite(pPage1->pDbPage);
72745 put4byte(&pPage1->aData[28], pBt->nPage);
72754 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
72759 ** the sub-journal is not already open, then it will be opened here.
72761 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
72773 ** Set the pointer-map entries for all children of page pPage. Also, if
72781 BtShared *pBt = pPage->pBt;
72782 Pgno pgno = pPage->pgno;
72784 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72785 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72787 nCell = pPage->nCell;
72794 if( !pPage->leaf ){
72800 if( !pPage->leaf ){
72801 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72813 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
72816 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
72819 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
72823 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72824 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72827 if( get4byte(pPage->aData)!=iFrom ){
72830 pPage->pgno, get4byte(pPage->aData), iFrom);
72831 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_PTR_MAP,
72835 put4byte(pPage->aData, iTo);
72841 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72843 nCell = pPage->nCell;
72849 pPage->xParseCell(pPage, pCell, &info);
72851 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
72855 (int)(pCell - pPage->aData), info.nSize, pPage->pBt->usableSize);
72856 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
72857 CORRUPT_TYPE_PAGE_PTR_MAP, pCell - pPage->aData, info.nSize, zMsg, NULL);
72860 if( iFrom==get4byte(pCell+info.nSize-4) ){
72861 put4byte(pCell+info.nSize-4, iTo);
72866 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
72870 (int)(pCell - pPage->aData), pPage->pBt->usableSize);
72871 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
72872 CORRUPT_TYPE_PAGE_PTR_MAP, pCell - pPage->aData, 4, zMsg, NULL);
72884 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
72886 …rintf(sizeof(zMsg), zMsg, "missing pointer point to overflow page on btree page(%u)", pPage->pgno);
72887 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
72888 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
72891 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
72903 ** the journal needs to be sync()ed before database page pDbPage->pgno
72911 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
72916 Pgno iDbPage = pDbPage->pgno;
72917 Pager *pPager = pBt->pPager;
72922 assert( sqlite3_mutex_held(pBt->mutex) );
72923 assert( pDbPage->pBt==pBt );
72929 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
72933 pDbPage->pgno = iFreePage;
72935 /* If pDbPage was a btree-page, then it may have child pages and/or cells
72949 Pgno nextOvfl = get4byte(pDbPage->aData);
72967 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
72985 ** Perform a single step of an incremental-vacuum. If successful, return
72990 ** More specifically, this function attempts to re-organize the database so
72996 ** If the bCommit parameter is non-zero, this function assumes that the
72998 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
73002 Pgno nFreeList; /* Number of pages still on the free-list */
73005 assert( sqlite3_mutex_held(pBt->mutex) );
73012 nFreeList = get4byte(&pBt->pPage1->aData[36]);
73024 … sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, PTRMAP_PAGENO(pBt, iLastPg),
73025 CORRUPT_TYPE_PAGE_PTR_MAP, -1, 0, zMsg, NULL);
73031 /* Remove the page from the files free-list. This is not required
73032 ** if bCommit is non-zero. In that case, the free-list will be
73060 ** looping until a free-page located within the first nFin pages
73093 iLastPg--;
73095 pBt->bDoTruncate = 1;
73096 pBt->nPage = iLastPg;
73102 ** The database opened by the first argument is an auto-vacuum database
73104 ** size of the database in pages following an auto-vacuum operation.
73111 nEntry = pBt->usableSize/5;
73112 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
73113 nFin = nOrig - nFree - nPtrmap;
73115 nFin--;
73118 nFin--;
73125 ** A write-transaction must be opened before calling this function.
73134 BtShared *pBt = p->pBt;
73137 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
73138 if( !pBt->autoVacuum ){
73142 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
73154 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73155 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
73167 ** is committed for an auto-vacuum database.
73177 pBt = p->pBt;
73178 pPager = pBt->pPager;
73181 assert( sqlite3_mutex_held(pBt->mutex) );
73183 assert(pBt->autoVacuum);
73184 if( !pBt->incrVacuum ){
73194 ** is either a pointer-map page or the pending-byte page. If one
73200 nFree = get4byte(&pBt->pPage1->aData[36]);
73201 db = p->db;
73202 if( db->xAutovacPages ){
73204 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
73205 if( db->aDb[iDb].pBt==p ) break;
73207 nVac = db->xAutovacPages(
73208 db->pAutovacPagesArg,
73209 db->aDb[iDb].zDbSName,
73212 pBt->pageSize
73228 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
73232 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73234 put4byte(&pBt->pPage1->aData[32], 0);
73235 put4byte(&pBt->pPage1->aData[36], 0);
73237 put4byte(&pBt->pPage1->aData[28], nFin);
73238 pBt->bDoTruncate = 1;
73239 pBt->nPage = nFin;
73255 ** This routine does the first phase of a two-phase commit. This routine
73267 ** This call is a no-op if no write-transaction is currently active on pBt.
73270 ** the name of a super-journal file that should be written into the
73271 ** individual journal file, or is NULL, indicating no super-journal file
73274 ** When this is called, the super-journal should already have been
73278 ** the write-transaction for this database file is to delete the journal.
73282 if( p->inTrans==TRANS_WRITE ){
73283 BtShared *pBt = p->pBt;
73286 if( pBt->autoVacuum ){
73293 if( pBt->bDoTruncate ){
73294 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
73297 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
73308 BtShared *pBt = p->pBt;
73309 sqlite3 *db = p->db;
73313 pBt->bDoTruncate = 0;
73315 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
73317 ** handle, downgrade to a read-only transaction. The other statements
73320 p->inTrans = TRANS_READ;
73326 if( p->inTrans!=TRANS_NONE ){
73328 pBt->nTransaction--;
73329 if( 0==pBt->nTransaction ){
73330 pBt->inTransaction = TRANS_NONE;
73336 p->inTrans = TRANS_NONE;
73346 ** This routine implements the second phase of a 2-phase commit. The
73358 ** is non-zero then this b-tree transaction is part of a multi-file
73360 ** (by deleting a super-journal file) and the caller will ignore this
73362 ** reset the b-tree objects internal state to indicate that the write
73371 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
73375 /* If the handle has a write-transaction open, commit the shared-btrees
73378 if( p->inTrans==TRANS_WRITE ){
73380 BtShared *pBt = p->pBt;
73381 assert( pBt->inTransaction==TRANS_WRITE );
73382 assert( pBt->nTransaction>0 );
73383 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
73388 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
73389 pBt->inTransaction = TRANS_READ;
73423 ** flag is true, then only write-cursors need be tripped - read-only
73427 ** rolled back modified the database schema. In this case b-tree root
73432 ** saving the current position of a read-only cursor, all cursors,
73433 ** including all read-cursors are tripped.
73445 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
73446 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
73447 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
73456 p->eState = CURSOR_FAULT;
73457 p->skipNext = errCode;
73467 ** Set the pBt->nPage field correctly, according to the current
73468 ** state of the database. Assume pBt->pPage1 is valid.
73471 int nPage = get4byte(&pPage1->aData[28]);
73473 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
73474 testcase( pBt->nPage!=(u32)nPage );
73475 pBt->nPage = nPage;
73491 BtShared *pBt = p->pBt;
73510 if( p->inTrans==TRANS_WRITE ){
73513 assert( TRANS_WRITE==pBt->inTransaction );
73514 rc2 = sqlite3PagerRollback(pBt->pPager);
73519 /* The rollback may have destroyed the pPage1->aData value. So
73521 ** sure pPage1->aData is set correctly. */
73527 pBt->inTransaction = TRANS_READ;
73547 ** A statement sub-transaction is implemented as an anonymous savepoint. The
73549 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
73550 ** are no active savepoints and no other statement-transactions open,
73556 BtShared *pBt = p->pBt;
73558 assert( p->inTrans==TRANS_WRITE );
73559 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73561 assert( iStatement>p->db->nSavepoint );
73562 assert( pBt->inTransaction==TRANS_WRITE );
73568 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
73580 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
73587 if( p && p->inTrans==TRANS_WRITE ){
73588 BtShared *pBt = p->pBt;
73590 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
73596 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
73599 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
73600 pBt->nPage = 0;
73603 btreeSetNPage(pBt, pBt->pPage1);
73605 /* pBt->nPage might be zero if the database was corrupt when
73607 assert( CORRUPT_DB || pBt->nPage>0 );
73616 ** iTable. If a read-only cursor is requested, it is assumed that
73617 ** the caller already has at least a read-only transaction open
73618 ** on the database already. If a write-cursor is requested, then
73635 ** 3: The database must be writable (not on read-only media)
73646 ** operations on this cursor can be no-ops and all READ operations can
73647 ** return a null row (2-bytes: 0x01 0x00).
73650 ** root page of a b-tree. If it is not, then the cursor acquired
73659 int wrFlag, /* 1 to write. 0 read-only */
73663 BtShared *pBt = p->pBt; /* Shared b-tree handle */
73673 ** b-tree database, the connection is holding the required table locks,
73681 assert( p->inTrans>TRANS_NONE );
73682 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
73683 assert( pBt->pPage1 && pBt->pPage1->aData );
73684 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
73697 pCur->pgnoRoot = iTable;
73698 pCur->iPage = -1;
73699 pCur->pKeyInfo = pKeyInfo;
73700 pCur->pBtree = p;
73701 pCur->pBt = pBt;
73702 pCur->curFlags = 0;
73705 for(pX=pBt->pCursor; pX; pX=pX->pNext){
73706 if( pX->pgnoRoot==iTable ){
73707 pX->curFlags |= BTCF_Multiple;
73708 pCur->curFlags = BTCF_Multiple;
73711 pCur->eState = CURSOR_INVALID;
73712 pCur->pNext = pBt->pCursor;
73713 pBt->pCursor = pCur;
73715 pCur->curFlags |= BTCF_WriteFlag;
73716 pCur->curPagerFlags = 0;
73717 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
73719 pCur->curPagerFlags = PAGER_GET_READONLY;
73726 int wrFlag, /* 1 to write. 0 read-only */
73739 int wrFlag, /* 1 to write. 0 read-only */
73743 if( p->sharable ){
73755 ** to users so they cannot do the sizeof() themselves - they must call
73768 ** of run-time by skipping the initialization of those elements.
73779 Btree *pBtree = pCur->pBtree;
73781 BtShared *pBt = pCur->pBt;
73783 assert( pBt->pCursor!=0 );
73784 if( pBt->pCursor==pCur ){
73785 pBt->pCursor = pCur->pNext;
73787 BtCursor *pPrev = pBt->pCursor;
73789 if( pPrev->pNext==pCur ){
73790 pPrev->pNext = pCur->pNext;
73793 pPrev = pPrev->pNext;
73798 sqlite3_free(pCur->aOverflow);
73799 sqlite3_free(pCur->pKey);
73800 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
73803 assert( pBtree->sharable==0 );
73808 pCur->pBtree = 0;
73823 if( a->nKey!=b->nKey ) return 0;
73824 if( a->pPayload!=b->pPayload ) return 0;
73825 if( a->nPayload!=b->nPayload ) return 0;
73826 if( a->nLocal!=b->nLocal ) return 0;
73827 if( a->nSize!=b->nSize ) return 0;
73833 btreeParseCell(pCur->pPage, pCur->ix, &info);
73834 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
73840 if( pCur->info.nSize==0 ){
73841 pCur->curFlags |= BTCF_ValidNKey;
73842 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
73851 ** that is currently pointing to a row in a (non-empty) table.
73855 return pCur && pCur->eState==CURSOR_VALID;
73860 return pCur->eState==CURSOR_VALID;
73871 assert( pCur->eState==CURSOR_VALID );
73872 assert( pCur->curIntKey );
73874 return pCur->info.nKey;
73881 assert( (pCur->curFlags & BTCF_Pinned)==0 );
73882 pCur->curFlags |= BTCF_Pinned;
73885 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
73886 pCur->curFlags &= ~BTCF_Pinned;
73896 assert( pCur->eState==CURSOR_VALID );
73898 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
73899 (i64)(pCur->info.pPayload - pCur->pPage->aData);
73908 ** The caller must guarantee that the cursor is pointing to a non-NULL
73914 assert( pCur->eState==CURSOR_VALID );
73916 return pCur->info.nPayload;
73934 assert( pCur->eState==CURSOR_VALID );
73935 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
73941 ** linked list of overflow pages. If possible, it uses the auto-vacuum
73942 ** pointer-map data instead of reading the content of page ovfl to do so.
73954 ** the pointer-map was used to obtain the value for *pPgnoNext), then
73967 assert( sqlite3_mutex_held(pBt->mutex) );
73972 ** autovacuum pointer-map pages. Guess that the next page in
73977 if( pBt->autoVacuum ){
74001 next = get4byte(pPage->aData);
74029 int eOp, /* 0 -> copy from page, 1 -> copy to page */
74062 ** the overflow page-list cache array (BtCursor.aOverflow).
74066 ** Once an overflow page-list cache has been allocated, it must be
74068 ** the cursor is moved to a different row. Additionally, in auto-vacuum
74069 ** mode, the following events may invalidate an overflow page-list cache.
74080 int eOp /* zero to read. non-zero to write. */
74085 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
74086 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
74093 assert( pCur->eState==CURSOR_VALID );
74094 if( pCur->ix>=pPage->nCell ){
74097 pCur->ix, pPage->nCell, pPage->pgno);
74098 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pPage->pgno, CORRUPT_TYPE_PAGE_…
74099 -1, 0, zMsg, NULL);
74105 aPayload = pCur->info.pPayload;
74106 assert( offset+amt <= pCur->info.nPayload );
74108 assert( aPayload > pPage->aData );
74109 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
74112 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
74117 … pCur->info.nLocal, (int)(aPayload - pPage->aData), (int)(pBt->usableSize - pCur->info.nLocal));
74118 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pPage->pgno, CORRUPT_TYPE_PAGE_…
74124 if( offset<pCur->info.nLocal ){
74126 if( a+offset>pCur->info.nLocal ){
74127 a = pCur->info.nLocal - offset;
74129 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
74132 amt -= a;
74134 offset -= pCur->info.nLocal;
74139 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
74142 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
74151 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
74152 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
74153 if( pCur->aOverflow==0
74154 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
74157 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
74162 pCur->aOverflow = aNew;
74165 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
74166 pCur->curFlags |= BTCF_ValidOvfl;
74168 /* If the overflow page-list cache has been allocated and the
74172 if( pCur->aOverflow[offset/ovflSize] ){
74174 nextPage = pCur->aOverflow[iIdx];
74181 /* If required, populate the overflow page-list cache. */
74182 if( nextPage > pBt->nPage ){
74185 (void)sqlite3base16Encode(aPayload + pCur->info.nLocal, 4, xBuffer, sizeof(xBuffer));
74188 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pPage->pgno, CORRUPT_TYPE_PAGE_…
74189 aPayload - pPage->aData + pCur->info.nLocal, 4, zMsg, NULL);
74192 assert( pCur->aOverflow[iIdx]==0
74193 || pCur->aOverflow[iIdx]==nextPage
74195 pCur->aOverflow[iIdx] = nextPage;
74201 ** page-list cache, if any, then fall back to the getOverflowPage()
74204 assert( pCur->curFlags & BTCF_ValidOvfl );
74205 assert( pCur->pBtree->db==pBt->db );
74206 if( pCur->aOverflow[iIdx+1] ){
74207 nextPage = pCur->aOverflow[iIdx+1];
74211 offset -= ovflSize;
74218 a = ovflSize - offset;
74226 ** 3) there are no dirty pages in the page-cache
74227 ** 4) the database is file-backed, and
74232 ** output buffer, bypassing the page-cache altogether. This speeds
74237 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
74238 && &pBuf[-4]>=pBufStart /* (6) */
74240 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
74242 u8 *aWrite = &pBuf[-4];
74245 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
74246 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
74254 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
74265 amt -= a;
74278 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, pPage->pgno, CORRUPT_TYPE_PAGE_…
74279 -1, 0, zMsg, NULL);
74290 ** pCur can be pointing to either a table or an index b-tree.
74292 ** pCur is pointing to an index b-tree then the key section is read.
74304 assert( pCur->eState==CURSOR_VALID );
74305 assert( pCur->iPage>=0 && pCur->pPage );
74322 if ( pCur->eState==CURSOR_INVALID ){
74330 if( pCur->eState==CURSOR_VALID ){
74342 ** the key if index btrees (pPage->intKey==0) and is the data for
74343 ** table btrees (pPage->intKey==1). The number of bytes of available
74363 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
74364 assert( pCur->eState==CURSOR_VALID );
74365 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74367 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
74368 assert( pCur->info.nSize>0 );
74369 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
74370 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
74371 amt = pCur->info.nLocal;
74372 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
74376 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
74379 return (void*)pCur->info.pPayload;
74386 ** b-tree page. Write the number of available bytes into *pAmt.
74406 ** This function returns SQLITE_CORRUPT if the page-header flags field of
74408 ** if an intkey page appears to be the parent of a non-intkey page, or
74409 ** vice-versa).
74413 assert( pCur->eState==CURSOR_VALID );
74414 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
74415 assert( pCur->iPage>=0 );
74416 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
74419 pCur->info.nSize = 0;
74420 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74421 pCur->aiIdx[pCur->iPage] = pCur->ix;
74422 pCur->apPage[pCur->iPage] = pCur->pPage;
74423 pCur->ix = 0;
74424 pCur->iPage++;
74425 return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur,
74426 pCur->curPagerFlags);
74431 ** Page pParent is an internal (non-leaf) tree page. This function
74432 ** asserts that page number iChild is the left-child if the iIdx'th
74434 ** cells in pParent, that page number iChild is the right-child of
74440 assert( iIdx<=pParent->nCell );
74441 if( iIdx==pParent->nCell ){
74442 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
74454 ** pCur->idx is set to the cell index that contains the pointer
74456 ** right-most child page then pCur->idx is set to one more than
74462 assert( pCur->eState==CURSOR_VALID );
74463 assert( pCur->iPage>0 );
74464 assert( pCur->pPage );
74466 pCur->apPage[pCur->iPage-1],
74467 pCur->aiIdx[pCur->iPage-1],
74468 pCur->pPage->pgno
74470 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
74471 pCur->info.nSize = 0;
74472 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74473 pCur->ix = pCur->aiIdx[pCur->iPage-1];
74474 pLeaf = pCur->pPage;
74475 pCur->pPage = pCur->apPage[--pCur->iPage];
74480 ** Move the cursor to point to the root page of its b-tree structure.
74487 ** If the b-tree structure is empty, the cursor state is set to
74493 ** page-header flags indicate that the [virtual] root-page is the expected
74494 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
74496 ** indicating a table b-tree, or if the caller did specify a KeyInfo
74498 ** b-tree).
74508 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
74509 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
74511 if( pCur->iPage>=0 ){
74512 if( pCur->iPage ){
74513 releasePageNotNull(pCur->pPage);
74514 while( --pCur->iPage ){
74515 releasePageNotNull(pCur->apPage[pCur->iPage]);
74517 pRoot = pCur->pPage = pCur->apPage[0];
74520 }else if( pCur->pgnoRoot==0 ){
74521 pCur->eState = CURSOR_INVALID;
74524 assert( pCur->iPage==(-1) );
74525 if( pCur->eState>=CURSOR_REQUIRESEEK ){
74526 if( pCur->eState==CURSOR_FAULT ){
74527 assert( pCur->skipNext!=SQLITE_OK );
74528 return pCur->skipNext;
74532 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
74533 0, pCur->curPagerFlags);
74535 pCur->eState = CURSOR_INVALID;
74538 pCur->iPage = 0;
74539 pCur->curIntKey = pCur->pPage->intKey;
74541 pRoot = pCur->pPage;
74542 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
74544 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
74545 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
74546 ** NULL, the caller expects a table b-tree. If this is not the case,
74551 ** if pCur->iPage>=0). But this is not so if the database is corrupted
74552 ** in such a way that page pRoot is linked into a second b-tree table
74554 assert( pRoot->intKey==1 || pRoot->intKey==0 );
74555 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
74558 pRoot->pgno, pRoot->isInit, ((pCur->pKeyInfo==0)?"==":"!="), pRoot->intKey);
74559 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pCur->pBt->nPage, pRoot->pgno, CORRUPT_TYPE…
74560 -1, 0, zMsg, NULL);
74561 return SQLITE_CORRUPT_PAGE(&context, pCur->pPage);
74565 pCur->ix = 0;
74566 pCur->info.nSize = 0;
74567 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
74569 if( pRoot->nCell>0 ){
74570 pCur->eState = CURSOR_VALID;
74571 }else if( !pRoot->leaf ){
74573 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
74574 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
74575 pCur->eState = CURSOR_VALID;
74578 pCur->eState = CURSOR_INVALID;
74585 ** Move the cursor down to the left-most leaf entry beneath the
74588 ** The left-most leaf is the one with the smallest key - the first
74597 assert( pCur->eState==CURSOR_VALID );
74598 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
74599 assert( pCur->ix<pPage->nCell );
74600 pgno = get4byte(findCell(pPage, pCur->ix));
74607 ** Move the cursor down to the right-most leaf entry beneath the
74610 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
74611 ** finds the right-most entry beneath the *page*.
74613 ** The right-most entry is the one with the largest key - the last
74622 assert( pCur->eState==CURSOR_VALID );
74623 while( !(pPage = pCur->pPage)->leaf ){
74624 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74625 pCur->ix = pPage->nCell;
74629 pCur->ix = pPage->nCell-1;
74630 assert( pCur->info.nSize==0 );
74631 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
74643 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74646 assert( pCur->pPage->nCell>0 );
74650 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74665 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74667 /* If the cursor already points to the last entry, this is a no-op. */
74668 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
74671 ** to the last entry in the b-tree. */
74673 for(ii=0; ii<pCur->iPage; ii++){
74674 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
74676 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
74677 testcase( pCur->ix!=pCur->pPage->nCell-1 );
74678 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
74679 assert( pCur->pPage->leaf );
74687 assert( pCur->eState==CURSOR_VALID );
74691 pCur->curFlags |= BTCF_AtLast;
74693 pCur->curFlags &= ~BTCF_AtLast;
74696 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74735 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74737 assert( pCur->pKeyInfo==0 );
74738 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
74742 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
74743 if( pCur->info.nKey==intKey ){
74747 if( pCur->info.nKey<intKey ){
74748 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
74749 *pRes = -1;
74756 if( pCur->info.nKey+1==intKey ){
74761 if( pCur->info.nKey==intKey ){
74772 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74778 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74779 *pRes = -1;
74784 assert( pCur->pPage );
74785 assert( pCur->pPage->isInit );
74786 assert( pCur->eState==CURSOR_VALID );
74787 assert( pCur->pPage->nCell > 0 );
74788 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
74789 assert( pCur->curIntKey );
74794 MemPage *pPage = pCur->pPage;
74797 /* pPage->nCell must be greater than zero. If this is the root-page
74799 ** not run. If this is not the root-page, then the moveToChild() routine
74801 ** be the right kind (index or table) of b-tree page. Otherwise
74803 assert( pPage->nCell>0 );
74804 assert( pPage->intKey );
74806 upr = pPage->nCell-1;
74808 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
74812 if( pPage->intKeyLeaf ){
74814 if( pCell>=pPage->aDataEnd ){
74817 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
74818 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
74826 if( lwr>upr ){ c = -1; break; }
74828 upr = idx-1;
74832 pCur->ix = (u16)idx;
74833 if( !pPage->leaf ){
74837 pCur->curFlags |= BTCF_ValidNKey;
74838 pCur->info.nKey = nCellKey;
74839 pCur->info.nSize = 0;
74847 assert( lwr==upr+1 || !pPage->leaf );
74848 assert( pPage->isInit );
74849 if( pPage->leaf ){
74850 assert( pCur->ix<pCur->pPage->nCell );
74851 pCur->ix = (u16)idx;
74857 if( lwr>=pPage->nCell ){
74858 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74862 pCur->ix = (u16)lwr;
74867 pCur->info.nSize = 0;
74868 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74873 ** Compare the "idx"-th cell on the page the cursor pCur is currently
74894 MemPage *pPage = pCur->pPage;
74900 if( nCell<=pPage->max1bytePayload ){
74901 /* This branch runs if the record-size field of the cell is a
74903 ** b-tree page. */
74904 testcase( pCell+nCell+1==pPage->aDataEnd );
74907 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
74909 /* The record-size field is a 2 byte varint and the record
74910 ** fits entirely on the main b-tree page. */
74911 testcase( pCell+nCell+2==pPage->aDataEnd );
74922 ** Return true (non-zero) if pCur is current pointing to the last
74927 assert( pCur->eState==CURSOR_VALID );
74928 for(i=0; i<pCur->iPage; i++){
74929 MemPage *pPage = pCur->apPage[i];
74930 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
74958 ** The pIdxKey->eqSeen field is set to 1 if there
74970 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74972 assert( pCur->pKeyInfo!=0 );
74975 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74979 pIdxKey->errCode = 0;
74980 assert( pIdxKey->default_rc==1
74981 || pIdxKey->default_rc==0
74982 || pIdxKey->default_rc==-1
74997 if( pCur->eState==CURSOR_VALID
74998 && pCur->pPage->leaf
75002 if( pCur->ix==pCur->pPage->nCell-1
75003 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
75004 && pIdxKey->errCode==SQLITE_OK
75009 if( pCur->iPage>0
75011 && pIdxKey->errCode==SQLITE_OK
75013 pCur->curFlags &= ~BTCF_ValidOvfl;
75014 if( !pCur->pPage->isInit ){
75019 pIdxKey->errCode = SQLITE_OK;
75025 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
75026 *pRes = -1;
75033 assert( pCur->pPage );
75034 assert( pCur->pPage->isInit );
75035 assert( pCur->eState==CURSOR_VALID );
75036 assert( pCur->pPage->nCell > 0 );
75037 assert( pCur->curIntKey==0 );
75042 MemPage *pPage = pCur->pPage;
75045 /* pPage->nCell must be greater than zero. If this is the root-page
75047 ** not run. If this is not the root-page, then the moveToChild() routine
75049 ** be the right kind (index or table) of b-tree page. Otherwise
75051 assert( pPage->nCell>0 );
75052 assert( pPage->intKey==0 );
75054 upr = pPage->nCell-1;
75060 /* The maximum supported page-size is 65536 bytes. This means that
75061 ** the maximum number of record bytes stored on an index B-Tree
75062 ** page is less than 16384 bytes and may be stored as a 2-byte
75065 ** stored entirely within the b-tree page by inspecting the first
75069 if( nCell<=pPage->max1bytePayload ){
75070 /* This branch runs if the record-size field of the cell is a
75072 ** b-tree page. */
75073 testcase( pCell+nCell+1==pPage->aDataEnd );
75076 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
75078 /* The record-size field is a 2 byte varint and the record
75079 ** fits entirely on the main b-tree page. */
75080 testcase( pCell+nCell+2==pPage->aDataEnd );
75093 u8 * const pCellBody = pCell - pPage->childPtrSize;
75095 pPage->xParseCell(pPage, pCellBody, &pCur->info);
75096 nCell = (int)pCur->info.nKey;
75101 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
75104 nCell, pCur->pBt->usableSize, pCur->pBt->nPage);
75105 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno,
75106 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
75115 pCur->ix = (u16)idx;
75118 pCur->curFlags &= ~BTCF_ValidOvfl;
75127 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
75128 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
75133 upr = idx-1;
75138 pCur->ix = (u16)idx;
75139 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
75146 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
75147 assert( pPage->isInit );
75148 if( pPage->leaf ){
75149 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
75150 pCur->ix = (u16)idx;
75155 if( lwr>=pPage->nCell ){
75156 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
75160 pCur->ix = (u16)lwr;
75165 pCur->info.nSize = 0;
75166 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75183 return (CURSOR_VALID!=pCur->eState);
75196 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
75201 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
75202 if( NEVER(pCur->pPage->leaf==0) ) return -1;
75204 n = pCur->pPage->nCell;
75205 for(i=0; i<pCur->iPage; i++){
75206 n *= pCur->apPage[i]->nCell;
75237 if( pCur->eState!=CURSOR_VALID ){
75238 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
75243 if( CURSOR_INVALID==pCur->eState ){
75246 if( pCur->eState==CURSOR_SKIPNEXT ){
75247 pCur->eState = CURSOR_VALID;
75248 if( pCur->skipNext>0 ) return SQLITE_OK;
75252 pPage = pCur->pPage;
75253 idx = ++pCur->ix;
75254 if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
75258 if( idx>=pPage->nCell ){
75259 if( !pPage->leaf ){
75260 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
75265 if( pCur->iPage==0 ){
75266 pCur->eState = CURSOR_INVALID;
75270 pPage = pCur->pPage;
75271 }while( pCur->ix>=pPage->nCell );
75272 if( pPage->intKey ){
75278 if( pPage->leaf ){
75289 pCur->info.nSize = 0;
75290 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
75291 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
75292 pPage = pCur->pPage;
75293 if( (++pCur->ix)>=pPage->nCell ){
75294 pCur->ix--;
75297 if( pPage->leaf ){
75329 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
75330 assert( pCur->info.nSize==0 );
75331 if( pCur->eState!=CURSOR_VALID ){
75336 if( CURSOR_INVALID==pCur->eState ){
75339 if( CURSOR_SKIPNEXT==pCur->eState ){
75340 pCur->eState = CURSOR_VALID;
75341 if( pCur->skipNext<0 ) return SQLITE_OK;
75345 pPage = pCur->pPage;
75346 assert( pPage->isInit );
75347 if( !pPage->leaf ){
75348 int idx = pCur->ix;
75353 while( pCur->ix==0 ){
75354 if( pCur->iPage==0 ){
75355 pCur->eState = CURSOR_INVALID;
75360 assert( pCur->info.nSize==0 );
75361 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
75363 pCur->ix--;
75364 pPage = pCur->pPage;
75365 if( pPage->intKey && !pPage->leaf ){
75377 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
75378 pCur->info.nSize = 0;
75379 if( pCur->eState!=CURSOR_VALID
75380 || pCur->ix==0
75381 || pCur->pPage->leaf==0
75385 pCur->ix--;
75406 ** anywhere on the free-list, then it is guaranteed to be returned. If
75426 assert( sqlite3_mutex_held(pBt->mutex) );
75427 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
75428 pPage1 = pBt->pPage1;
75430 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
75432 n = get4byte(&pPage1->aData[36]);
75433 testcase( n==mxPage-1 );
75437 (void)sqlite3base16Encode(pPage1->aData, 100, xBuffer, sizeof(xBuffer));
75446 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
75449 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
75450 ** shows that the page 'nearby' is somewhere on the free-list, then
75451 ** the entire-list will be searched for that page.
75458 assert( pBt->autoVacuum );
75470 /* Decrement the free-list count by 1. Set iTrunk to the index of the
75471 ** first free-list trunk page. iPrevTrunk is initially 1.
75473 rc = sqlite3PagerWrite(pPage1->pDbPage);
75475 put4byte(&pPage1->aData[36], n-1);
75478 ** is not true. Otherwise, it runs once for each trunk-page on the
75479 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
75485 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
75488 iTrunk = get4byte(&pPrevTrunk->aData[0]);
75490 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
75493 iTrunk = get4byte(&pPage1->aData[32]);
75500 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(mxPage, (pPrevTrunk ? pPrevTrunk->pgno : 1),
75501 CORRUPT_TYPE_PAGE_FREE_LIST, -1, 0, zMsg, NULL);
75511 assert( pTrunk->aData!=0 );
75512 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
75514 k = get4byte(&pTrunk->aData[4]);
75520 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75525 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
75528 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
75529 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
75533 (void)sqlite3base16Encode(pTrunk->aData, 8, xBuffer, sizeof(xBuffer));
75535 k, (u32)(pBt->usableSize/4 - 2), xBuffer);
75550 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75556 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
75558 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75562 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
75566 ** pointers to free-list leaves. The first leaf becomes a trunk
75570 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
75574 (void)sqlite3base16Encode(pTrunk->aData, 12, xBuffer, sizeof(xBuffer));
75587 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
75592 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
75593 put4byte(&pNewTrunk->aData[4], k-1);
75594 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
75597 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
75598 put4byte(&pPage1->aData[32], iNewTrunk);
75600 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75604 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
75608 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
75614 unsigned char *aData = pTrunk->aData;
75628 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
75630 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
75662 *pPgno, closest+1, k, pTrunk->pgno, n-1));
75663 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75665 if( closest<k-1 ){
75668 put4byte(&aData[4], k-1);
75672 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75689 ** pager layer with the 'no-content' flag set. This prevents the pager
75691 ** current transaction has already run one or more incremental-vacuum
75694 ** not set the no-content flag. This causes the pager to load and journal
75699 ** file on disk. So the effects of disabling the no-content optimization
75703 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
75705 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75707 pBt->nPage++;
75708 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
75711 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
75712 /* If *pPgno refers to a pointer-map page, allocate two new pages
75714 ** becomes a new pointer-map page, the second is used by the caller.
75717 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
75718 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
75719 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
75721 rc = sqlite3PagerWrite(pPg->pDbPage);
75725 pBt->nPage++;
75726 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
75729 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
75730 *pPgno = pBt->nPage;
75735 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75748 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
75749 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
75754 ** This function is used to add page iPage to the database file free-list.
75755 ** It is assumed that the page is not already a part of the free-list.
75766 MemPage *pTrunk = 0; /* Free-list trunk page */
75767 Pgno iTrunk = 0; /* Page number of free-list trunk page */
75768 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
75771 u32 nFree; /* Initial number of pages on free-list */
75773 assert( sqlite3_mutex_held(pBt->mutex) );
75775 assert( !pMemPage || pMemPage->pgno==iPage );
75777 if( iPage<2 || iPage>pBt->nPage ){
75782 sqlite3PagerRef(pPage->pDbPage);
75788 rc = sqlite3PagerWrite(pPage1->pDbPage);
75790 nFree = get4byte(&pPage1->aData[36]);
75791 put4byte(&pPage1->aData[36], nFree+1);
75793 if( pBt->btsFlags & BTS_SECURE_DELETE ){
75798 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
75802 memset(pPage->aData, 0, pPage->pBt->pageSize);
75805 /* If the database supports auto-vacuum, write an entry in the pointer-map
75813 /* Now manipulate the actual database free-list structure. There are two
75814 ** possibilities. If the free-list is currently empty, or if the first
75815 ** trunk page in the free-list is full, then this page will become a
75816 ** new free-list trunk page. Otherwise, it will become a leaf of the
75817 ** first trunk page in the current free-list. This block tests if it
75818 ** is possible to add the page as a new free-list leaf.
75823 iTrunk = get4byte(&pPage1->aData[32]);
75833 nLeaf = get4byte(&pTrunk->aData[4]);
75834 assert( pBt->usableSize>32 );
75835 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
75838 (void)sqlite3base16Encode(pTrunk->aData, 4, xBuffer, sizeof(xBuffer));
75841 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pBt->nPage, iTrunk, CORRUPT_TYPE_PAGE_FREE_…
75846 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
75851 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
75854 ** usableSize/4 - 8 entries will be reported as corrupt. In order
75856 ** we will continue to restrict the number of entries to usableSize/4 - 8
75859 ** to read "usableSize/4-2" instead of "usableSize/4-8".
75861 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
75866 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75868 put4byte(&pTrunk->aData[4], nLeaf+1);
75869 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
75870 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
75871 sqlite3PagerDontWrite(pPage->pDbPage);
75875 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
75881 ** the page being freed as a leaf page of the first trunk in the free-list.
75882 ** Possibly because the free-list is empty, or possibly because the
75883 ** first trunk in the free-list is full. Either way, the page being freed
75884 ** will become the new first trunk page in the free-list.
75889 rc = sqlite3PagerWrite(pPage->pDbPage);
75893 put4byte(pPage->aData, iTrunk);
75894 put4byte(&pPage->aData[4], 0);
75895 put4byte(&pPage1->aData[32], iPage);
75896 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
75900 pPage->isInit = 0;
75908 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
75926 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75927 assert( pInfo->nLocal!=pInfo->nPayload );
75928 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
75929 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
75930 if( pCell + pInfo->nSize > pPage->aDataEnd ){
75934 pPage->pgno, (int)(pCell - pPage->aData), pInfo->nSize, pPage->pBt->usableSize);
75935 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(btreePagecount(pPage->pBt), pPage->pgno,
75936 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
75939 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
75940 pBt = pPage->pBt;
75941 assert( pBt->usableSize > 4 );
75942 ovflPageSize = pBt->usableSize - 4;
75943 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
75945 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
75947 while( nOvfl-- ){
75956 (void)sqlite3base16Encode(pCell + pInfo->nSize - 4, 4, xBuffer, sizeof(xBuffer));
75959 sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(btreePagecount(pBt), pPage->pgno,
75960 CORRUPT_TYPE_PAGE_BTREE_LEAF, -1, 0, zMsg, NULL);
75969 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
75976 ** freePage2() may zero the page contents if secure-delete mode is
75987 sqlite3PagerUnref(pOvfl->pDbPage);
76002 pPage->xParseCell(pPage, pCell, &sInfo); \
76017 ** Note that pCell does not necessary need to point to the pPage->aData
76019 ** be constructed in this temporary area then copied into pPage->aData
76039 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76043 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
76044 || sqlite3PagerIswriteable(pPage->pDbPage) );
76047 nHeader = pPage->childPtrSize;
76048 if( pPage->intKey ){
76049 nPayload = pX->nData + pX->nZero;
76050 pSrc = pX->pData;
76051 nSrc = pX->nData;
76052 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
76054 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
76056 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
76057 nSrc = nPayload = (int)pX->nKey;
76058 pSrc = pX->pKey;
76064 if( nPayload<=pPage->maxLocal ){
76075 memset(pPayload+nSrc, 0, nPayload-nSrc);
76082 mn = pPage->minLocal;
76083 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
76084 testcase( n==pPage->maxLocal );
76085 testcase( n==pPage->maxLocal+1 );
76086 if( n > pPage->maxLocal ) n = mn;
76092 pBt = pPage->pBt;
76109 pPage->xParseCell(pPage, pCell, &info);
76110 assert( nHeader==(int)(info.pPayload - pCell) );
76111 assert( info.nKey==pX->nKey );
76124 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76128 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
76129 || sqlite3PagerIswriteable(pPage->pDbPage) );
76139 nPayload -= n;
76143 nSrc -= n;
76144 spaceLeft -= n;
76148 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
76149 if( pBt->autoVacuum ){
76159 /* If the database supports auto-vacuum, and the second or subsequent
76160 ** overflow page is being allocated, add an entry to the pointer-map
76164 ** to the pointer-map. If we write nothing to this pointer-map slot,
76169 if( pBt->autoVacuum && rc==SQLITE_OK ){
76184 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
76188 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
76189 || sqlite3PagerIswriteable(pPage->pDbPage) );
76194 pPrior = pOvfl->aData;
76196 pPayload = &pOvfl->aData[4];
76197 spaceLeft = pBt->usableSize - 4;
76205 ** Remove the i-th cell from pPage. This routine effects pPage only.
76214 u8 *data; /* pPage->aData */
76221 assert( idx<pPage->nCell );
76223 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76224 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76225 assert( pPage->nFree>=0 );
76226 data = pPage->aData;
76227 ptr = &pPage->aCellIdx[2*idx];
76228 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
76230 hdr = pPage->hdrOffset;
76232 testcase( pc+sz==pPage->pBt->usableSize );
76233 if( pc+sz > pPage->pBt->usableSize ){
76236 pc, sz, idx, pPage->pBt->usableSize);
76237 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(pPage->pBt->nPage, pPage->pgno, CORRUPT_TYP…
76238 -1, 0, zMsg, NULL);
76247 pPage->nCell--;
76248 if( pPage->nCell==0 ){
76251 put2byte(&data[hdr+5], pPage->pBt->usableSize);
76252 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
76253 - pPage->childPtrSize - 8;
76255 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
76256 put2byte(&data[hdr+3], pPage->nCell);
76257 pPage->nFree += 2;
76268 ** in pPage->apOvfl[] and make it point to the cell content (either
76270 ** Allocating a new entry in pPage->aCell[] implies that
76271 ** pPage->nOverflow is incremented.
76277 int i, /* New cell becomes the i-th cell of the page */
76281 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
76287 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
76290 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
76291 assert( MX_CELL(pPage->pBt)<=10921 );
76292 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
76293 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
76294 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
76295 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76296 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
76297 assert( pPage->nFree>=0 );
76298 if( pPage->nOverflow || sz+2>pPage->nFree ){
76306 j = pPage->nOverflow++;
76307 /* Comparison against ArraySize-1 since we hold back one extra slot
76310 assert( j < ArraySize(pPage->apOvfl)-1 );
76311 pPage->apOvfl[j] = pCell;
76312 pPage->aiOvfl[j] = (u16)i;
76319 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
76320 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
76322 int rc = sqlite3PagerWrite(pPage->pDbPage);
76327 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
76328 data = pPage->aData;
76329 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
76335 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
76336 assert( idx+sz <= (int)pPage->pBt->usableSize );
76337 pPage->nFree -= (u16)(2 + sz);
76344 memcpy(&data[idx+4], pCell+4, sz-4);
76349 pIns = pPage->aCellIdx + i*2;
76350 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
76352 pPage->nCell++;
76354 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
76355 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
76357 if( pPage->pBt->autoVacuum ){
76380 ** tunable - as if you could change them and recompile and it would all work.
76395 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
76407 ** -----------
76409 ** -----------
76412 ** --------- --------- ---------
76413 ** |Child-1| |Child-2| |Child-3|
76414 ** --------- --------- ---------
76418 ** 1. All cells from Child-1 in order
76420 ** 3. All cells from Child-2 in order
76422 ** 5. All cells from Child-3 in order
76424 ** For a table-btree (with rowids) the items 2 and 4 are empty because
76428 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
76432 ** ixNx[0] = Number of cells in Child-1.
76433 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
76434 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
76435 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
76438 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
76441 ** ixNx[0] = Number of cells in Child-1.
76442 ** ixNx[1] = Number of cells in Child-1 and Child-2.
76461 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
76465 assert( idx>=0 && idx+N<=p->nCell );
76467 assert( p->apCell[idx]!=0 );
76468 if( p->szCell[idx]==0 ){
76469 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
76472 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
76475 N--;
76483 assert( N>=0 && N<p->nCell );
76484 assert( p->szCell[N]==0 );
76485 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
76486 return p->szCell[N];
76489 assert( N>=0 && N<p->nCell );
76490 if( p->szCell[N] ) return p->szCell[N];
76495 ** Array apCell[] contains pointers to nCell b-tree page cells. The
76513 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
76514 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
76515 const int usableSize = pPg->pBt->usableSize;
76520 u8 *pCellptr = pPg->aCellIdx;
76521 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
76523 int k; /* Current slot in pCArray->apEnd[] */
76524 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
76529 memcpy(&pTmp[j], &aData[j], usableSize - j);
76531 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
76532 pSrcEnd = pCArray->apEnd[k];
76536 u8 *pCell = pCArray->apCell[i];
76537 u16 sz = pCArray->szCell[i];
76541 pCell = &pTmp[pCell - aData];
76548 pData -= sz;
76549 put2byte(pCellptr, (pData - aData));
76553 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
76556 if( pCArray->ixNx[k]<=i ){
76558 pSrcEnd = pCArray->apEnd[k];
76562 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
76563 pPg->nCell = nCell;
76564 pPg->nOverflow = 0;
76567 put2byte(&aData[hdr+3], pPg->nCell);
76568 put2byte(&aData[hdr+5], pData - aData);
76574 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
76577 ** will fit), non-zero is returned. Otherwise, if the cells are added
76580 ** Argument pCellptr points to the first entry in the cell-pointer array
76582 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
76584 ** that it is safe to overwrite this part of the cell-pointer array.
76592 ** end of the space required by this page for the cell-pointer area (for
76593 ** all cells - not just those inserted by the current call). If the content
76595 ** cells in apCell[], then the cells do not fit and non-zero is returned.
76599 u8 *pBegin, /* End of cell-pointer array */
76600 u8 **ppData, /* IN/OUT: Page content-area pointer */
76601 u8 *pCellptr, /* Pointer to cell-pointer area */
76606 int i = iFirst; /* Loop counter - cell index to insert */
76607 u8 *aData = pPg->aData; /* Complete page */
76610 int k; /* Current slot in pCArray->apEnd[] */
76612 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
76614 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
76615 pEnd = pCArray->apEnd[k];
76619 assert( pCArray->szCell[i]!=0 );
76620 sz = pCArray->szCell[i];
76622 if( (pData - pBegin)<sz ) return 1;
76623 pData -= sz;
76626 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
76629 assert( (pSlot+sz)<=pCArray->apCell[i]
76630 || pSlot>=(pCArray->apCell[i]+sz)
76632 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
76633 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
76639 memmove(pSlot, pCArray->apCell[i], sz);
76640 put2byte(pCellptr, (pSlot - aData));
76644 if( pCArray->ixNx[k]<=i ){
76646 pEnd = pCArray->apEnd[k];
76654 ** The pCArray object contains pointers to b-tree cells and their sizes.
76657 ** that is currently stored within the body of pPg to the pPg free-list.
76658 ** The cell-pointers and other fields of the page are not updated.
76660 ** This function returns the total number of cells added to the free-list.
76668 u8 * const aData = pPg->aData;
76669 u8 * const pEnd = &aData[pPg->pBt->usableSize];
76670 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
76678 u8 *pCell = pCArray->apCell[i];
76684 sz = pCArray->szCell[i]; assert( sz>0 );
76687 assert( pFree>aData && (pFree - aData)<65536 );
76688 freeSpace(pPg, (u16)(pFree - aData), szFree);
76703 assert( pFree>aData && (pFree - aData)<65536 );
76704 freeSpace(pPg, (u16)(pFree - aData), szFree);
76711 ** balanced. The current page, pPg, has pPg->nCell cells starting with
76712 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
76718 ** The pPg->nFree field is invalid when this function returns. It is the
76728 u8 * const aData = pPg->aData;
76729 const int hdr = pPg->hdrOffset;
76730 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
76731 int nCell = pPg->nCell; /* Cells stored on pPg */
76735 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
76739 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
76740 memcpy(pTmp, aData, pPg->pBt->usableSize);
76746 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
76748 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
76749 nCell -= nShift;
76752 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
76754 nCell -= nTail;
76759 if( pData>pPg->aDataEnd ) goto editpage_fail;
76763 int nAdd = MIN(nNew,iOld-iNew);
76764 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
76766 pCellptr = pPg->aCellIdx;
76776 for(i=0; i<pPg->nOverflow; i++){
76777 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
76779 pCellptr = &pPg->aCellIdx[iCell * 2];
76781 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
76794 pCellptr = &pPg->aCellIdx[nCell*2];
76797 iNew+nCell, nNew-nCell, pCArray
76800 pPg->nCell = nNew;
76801 pPg->nOverflow = 0;
76803 put2byte(&aData[hdr+3], pPg->nCell);
76804 put2byte(&aData[hdr+5], pData - aData);
76808 u8 *pCell = pCArray->apCell[i+iNew];
76809 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
76810 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
76811 pCell = &pTmp[pCell - aData];
76814 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
76829 ** a new entry is being inserted on the extreme right-end of the
76833 ** Instead of trying to balance the 3 right-most leaf pages, just add
76834 ** a new page to the right-hand side and put the one new entry in
76840 ** pPage is the leaf page which is the right-most page in the tree.
76842 ** which is also the right-most entry on the page.
76851 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
76856 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76857 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76858 assert( pPage->nOverflow==1 );
76860 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
76861 assert( pPage->nFree>=0 );
76862 assert( pParent->nFree>=0 );
76864 /* Allocate a new page. This page will become the right-sibling of
76873 u8 *pCell = pPage->apOvfl[0];
76874 u16 szCell = pPage->xCellSize(pPage, pCell);
76878 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
76879 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
76885 b.apEnd[0] = pPage->aDataEnd;
76892 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
76894 /* If this is an auto-vacuum database, update the pointer map
76904 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
76905 if( szCell>pNew->minLocal ){
76911 ** consists of a 4-byte page number (the page number of pPage) and
76915 ** To find the largest key value on pPage, first find the right-most
76917 ** record-length (a variable length integer at most 32-bits in size)
76919 ** The first of the while(...) loops below skips over the record-length
76923 pCell = findCell(pPage, pPage->nCell-1);
76931 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
76932 0, pPage->pgno, &rc);
76935 /* Set the right-child pointer of pParent to point to the new page. */
76936 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
76950 ** for setting pointer-map entries.
76958 BtShared *pBt = pPage->pBt;
76959 assert( pPage->isInit );
76961 for(j=0; j<pPage->nCell; j++){
76966 pPage->xParseCell(pPage, z, &info);
76968 Pgno ovfl = get4byte(&z[info.nSize-4]);
76970 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
76972 if( !pPage->leaf ){
76975 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76978 if( !pPage->leaf ){
76979 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76981 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76989 ** This function is used to copy the contents of the b-tree node stored
76991 ** the pointer-map entries for each child page are updated so that the
77007 BtShared * const pBt = pFrom->pBt;
77008 u8 * const aFrom = pFrom->aData;
77009 u8 * const aTo = pTo->aData;
77010 int const iFromHdr = pFrom->hdrOffset;
77011 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
77016 assert( pFrom->isInit );
77017 assert( pFrom->nFree>=iToHdr );
77018 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
77020 /* Copy the b-tree node content from page pFrom to page pTo. */
77022 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
77023 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
77030 pTo->isInit = 0;
77038 /* If this is an auto-vacuum database, update the pointer-map entries
77039 ** for any b-tree or overflow pages that pTo now contains the pointers to.
77081 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
77090 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
77091 int isRoot, /* True if pParent is a root-page */
77099 int nxDiv; /* Next divider slot in pParent->aCell[] */
77104 int pageFlags; /* Value of pPage->aData[0] */
77110 u8 *pRight; /* Location in parent of right-sibling pointer */
77111 u8 *apDiv[NB-1]; /* Divider cells in pParent */
77112 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
77114 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
77123 pBt = pParent->pBt;
77124 assert( sqlite3_mutex_held(pBt->mutex) );
77125 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77132 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
77133 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
77138 assert( pParent->nFree>=0 );
77151 i = pParent->nOverflow + pParent->nCell;
77159 nxDiv = i-2+bBulk;
77161 nxDiv = iParentIdx-1;
77163 i = 2-bBulk;
77166 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
77167 pRight = &pParent->aData[pParent->hdrOffset+8];
77169 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
77180 if( apOld[i]->nFree<0 ){
77187 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
77188 if( (i--)==0 ) break;
77190 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
77191 apDiv[i] = pParent->apOvfl[0];
77193 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
77194 pParent->nOverflow = 0;
77196 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
77198 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
77207 ** But not if we are in secure-delete mode. In secure-delete mode,
77212 if( pBt->btsFlags & BTS_FAST_SECURE ){
77217 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
77218 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
77220 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
77223 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
77227 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
77237 + pBt->pageSize; /* aSpace1 */
77239 assert( szScratch<=7*(int)pBt->pageSize );
77266 leafCorrection = b.pRef->leaf*4;
77267 leafData = b.pRef->intKeyLeaf;
77270 int limit = pOld->nCell;
77271 u8 *aData = pOld->aData;
77272 u16 maskPage = pOld->maskPage;
77273 u8 *piCell = aData + pOld->cellOffset;
77277 /* Verify that all sibling pages are of the same "type" (table-leaf,
77278 ** table-interior, index-leaf, or index-interior).
77280 if( pOld->aData[0]!=apOld[0]->aData[0] ){
77302 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
77303 if( pOld->nOverflow>0 ){
77304 if( NEVER(limit<pOld->aiOvfl[0]) ){
77308 limit = pOld->aiOvfl[0];
77314 for(k=0; k<pOld->nOverflow; k++){
77315 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
77316 b.apCell[b.nCell] = pOld->apOvfl[k];
77320 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
77327 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
77330 if( i<nOld-1 && !leafData){
77337 assert( sz<=pBt->maxLocal+23 );
77338 assert( iSpace1 <= (int)pBt->pageSize );
77342 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
77343 if( !pOld->leaf ){
77345 assert( pOld->hdrOffset==0 || CORRUPT_DB );
77348 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
77355 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
77367 ** size of all cells on the i-th page and cntNew[] which is the index
77374 ** szNew[i]: Spaced used on the i-th sibling page.
77376 ** the right of the i-th sibling page.
77380 usableSpace = pBt->usableSize - 12 + leafCorrection;
77383 b.apEnd[k] = p->aDataEnd;
77385 if( k && b.ixNx[k]==b.ixNx[k-1] ){
77386 k--; /* Omit b.ixNx[] entry for child pages with no cells */
77390 b.apEnd[k] = pParent->aDataEnd;
77393 assert( p->nFree>=0 );
77394 szNew[i] = usableSpace - p->nFree;
77395 for(j=0; j<p->nOverflow; j++){
77396 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
77407 szNew[k-1] = 0;
77408 cntNew[k-1] = b.nCell;
77410 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
77411 szNew[i] -= sz;
77420 cntNew[i]--;
77434 szNew[i+1] -= sz;
77438 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
77447 ** always nearly full, while the right-most sibling might be nearly empty.
77452 ** be so out of balance as to be illegal. For example, the right-most
77455 for(i=k-1; i>0; i--){
77457 int szLeft = szNew[i-1]; /* Size of sibling on the left */
77458 int r; /* Index of right-most cell in left sibling */
77461 r = cntNew[i-1] - 1;
77462 d = r + 1 - leafData;
77469 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
77473 szLeft -= b.szCell[r] + 2;
77474 cntNew[i-1] = r;
77475 r--;
77476 d--;
77479 szNew[i-1] = szLeft;
77480 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
77486 /* Sanity check: For a non-corrupt database file one of the follwing
77493 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
77495 apOld[0]->pgno, apOld[0]->nCell,
77496 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
77497 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
77503 pageFlags = apOld[0]->aData[0];
77509 rc = sqlite3PagerWrite(pNew->pDbPage);
77511 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
77526 /* Set the pointer-map entry for the new sibling page. */
77528 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
77549 aPgno[i] = apNew[i]->pgno;
77550 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
77551 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
77553 for(i=0; i<nNew-1; i++){
77556 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
77565 Pgno pgnoA = apNew[i]->pgno;
77566 Pgno pgnoB = apNew[iB]->pgno;
77567 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
77568 u16 fgA = apNew[i]->pDbPage->flags;
77569 u16 fgB = apNew[iB]->pDbPage->flags;
77570 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
77571 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
77572 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
77573 apNew[i]->pgno = pgnoB;
77574 apNew[iB]->pgno = pgnoA;
77580 apNew[0]->pgno, szNew[0], cntNew[0],
77581 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
77582 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
77583 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
77584 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
77585 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
77586 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
77587 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
77588 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
77591 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77593 assert( apNew[nNew-1]!=0 );
77594 put4byte(pRight, apNew[nNew-1]->pgno);
77596 /* If the sibling pages are not leaves, ensure that the right-child pointer
77597 ** of the right-most new sibling page is set to the value that was
77598 ** originally in the same field of the right-most old sibling page. */
77600 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
77601 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
77616 ** associated with the right-child of each sibling may also need to be
77623 int cntOldNext = pNew->nCell + pNew->nOverflow;
77634 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
77648 || pNew->pgno!=aPgno[iOld]
77649 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
77652 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
77654 if( cachedCellSize(&b,i)>pNew->minLocal ){
77663 for(i=0; i<nNew-1; i++){
77676 if( !pNew->leaf ){
77677 memcpy(&pNew->aData[8], pCell, 4);
77679 /* If the tree is a leaf-data tree, and the siblings are leaves,
77681 ** cell consists of the integer key for the right-most cell of
77682 ** the sibling-page assembled above only.
77685 j--;
77686 pNew->xParseCell(pNew, b.apCell[j], &info);
77691 pCell -= 4;
77692 /* Obscure case for non-leaf-data trees: If the cell at pCell was
77699 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
77705 sz = pParent->xCellSize(pParent, pCell);
77709 assert( sz<=pBt->maxLocal+23 );
77710 assert( iOvflSpace <= (int)pBt->pageSize );
77717 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
77719 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77726 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
77728 ** the left-hand sibling apNew[iPg-1] has been updated.
77732 ** the right-hand sibling apNew[iPg+1] has been updated.
77736 ** The iPg value in the following loop starts at nNew-1 goes down
77737 ** to 0, then back up to nNew-1 again, thus making two passes over
77744 for(i=1-nNew; i<nNew; i++){
77745 int iPg = i<0 ? -i : i;
77749 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
77756 ** only after iPg-1 has already been updated. */
77757 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
77767 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
77768 iNew = cntNew[iPg-1] + !leafData;
77769 nNewCell = cntNew[iPg] - iNew;
77775 apNew[iPg]->nFree = usableSpace-szNew[iPg];
77776 assert( apNew[iPg]->nOverflow==0 );
77777 assert( apNew[iPg]->nCell==nNewCell );
77787 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
77788 /* The root page of the b-tree now contains no cells. The only sibling
77789 ** page is the right-child of the parent. Copy the contents of the
77791 ** b-tree structure by one. This is described as the "balance-shallower"
77792 ** sub-algorithm in some documentation.
77794 ** If this is an auto-vacuum database, the call to copyNodeContent()
77795 ** sets all pointer-map entries corresponding to database image pages
77804 rc = defragmentPage(apNew[0], -1);
77806 assert( apNew[0]->nFree ==
77807 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
77808 - apNew[0]->nCell*2)
77814 /* Fix the pointer map entries associated with the right-child of each
77818 u32 key = get4byte(&apNew[i]->aData[8]);
77819 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
77823 assert( pParent->isInit );
77834 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
77861 ** This function is called when the root page of a b-tree structure is
77866 ** page is then overwritten to make it an empty page with the right-child
77869 ** Before returning, all pointer-map entries corresponding to pages
77870 ** that the new child-page now contains pointers to are updated. The
77871 ** entry corresponding to the new right-child pointer of the root
77883 BtShared *pBt = pRoot->pBt; /* The BTree */
77885 assert( pRoot->nOverflow>0 );
77886 assert( sqlite3_mutex_held(pBt->mutex) );
77888 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
77889 ** page that will become the new right-child of pPage. Copy the contents
77892 rc = sqlite3PagerWrite(pRoot->pDbPage);
77894 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
77897 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
77905 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
77906 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
77907 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
77909 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
77912 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
77913 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
77914 memcpy(pChild->apOvfl, pRoot->apOvfl,
77915 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
77916 pChild->nOverflow = pRoot->nOverflow;
77918 /* Zero the contents of pRoot. Then install pChild as the right-child. */
77919 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
77920 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
77928 ** on the same B-tree as pCur.
77931 ** pointing to the same b-tree. If an insert occurs on one SQL table
77933 ** table linked to the same b-tree. If the secondary insert causes a
77939 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
77941 && pOther->eState==CURSOR_VALID
77942 && pOther->pPage==pCur->pPage
77970 MemPage *pPage = pCur->pPage;
77972 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
77973 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
77979 }else if( (iPage = pCur->iPage)==0 ){
77980 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
77981 /* The root page of the b-tree is overfull. In this case call the
77982 ** balance_deeper() function to create a new child for the root-page
77983 ** and copy the current contents of the root-page to it. The
77984 ** next iteration of the do-loop will balance the child page.
77988 rc = balance_deeper(pPage, &pCur->apPage[1]);
77990 pCur->iPage = 1;
77991 pCur->ix = 0;
77992 pCur->aiIdx[0] = 0;
77993 pCur->apPage[0] = pPage;
77994 pCur->pPage = pCur->apPage[1];
77995 assert( pCur->pPage->nOverflow );
78000 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
78006 MemPage * const pParent = pCur->apPage[iPage-1];
78007 int const iIdx = pCur->aiIdx[iPage-1];
78009 rc = sqlite3PagerWrite(pParent->pDbPage);
78010 if( rc==SQLITE_OK && pParent->nFree<0 ){
78015 if( pPage->intKeyLeaf
78016 && pPage->nOverflow==1
78017 && pPage->aiOvfl[0]==pPage->nCell
78018 && pParent->pgno!=1
78019 && pParent->nCell==iIdx
78024 ** happens, the next iteration of the do-loop will balance pParent
78043 ** become overfull or underfull. The next iteration of the do-loop
78048 ** A subsequent iteration of the do-loop will deal with this by
78050 ** but it doesn't deal with overflow cells - just moves them to a
78057 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
78059 pCur->hints&BTREE_BULKLOAD);
78075 pPage->nOverflow = 0;
78077 /* The next iteration of the do-loop balances the parent page. */
78079 pCur->iPage--;
78080 assert( pCur->iPage>=0 );
78081 pCur->pPage = pCur->apPage[pCur->iPage];
78101 int nData = pX->nData - iOffset;
78107 int rc = sqlite3PagerWrite(pPage->pDbPage);
78109 memset(pDest + i, 0, iAmt - i);
78116 iAmt-nData);
78120 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
78121 int rc = sqlite3PagerWrite(pPage->pDbPage);
78127 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
78138 int iOffset; /* Next byte of pX->pData to write */
78139 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
78141 MemPage *pPage = pCur->pPage; /* Page being written */
78146 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
78147 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
78151 "cell payload cursor point to(%d), size:%u overlaps with non-cell content area:[%u, %d]",
78152 (int)(pCur->info.pPayload - pPage->aData), pCur->info.nLocal, pPage->cellOffset,
78153 (int)(pPage->aDataEnd - pPage->aData));
78154 …sqlite3CorruptContext context = SQLITE_CORRUPT_CONTEXT(0, pPage->pgno, CORRUPT_TYPE_PAGE_BTREE_LEA…
78155 -1, 0, zMsg, NULL);
78159 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
78160 0, pCur->info.nLocal);
78162 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
78165 iOffset = pCur->info.nLocal;
78168 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
78169 pBt = pPage->pBt;
78170 ovflPageSize = pBt->usableSize - 4;
78174 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
78178 ovflPgno = get4byte(pPage->aData);
78180 ovflPageSize = nTotal - iOffset;
78182 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
78185 sqlite3PagerUnref(pPage->pDbPage);
78208 ** If the seekResult parameter is non-zero, then a successful call to
78219 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
78220 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
78230 int loc = seekResult; /* -1: before desired location +1: after */
78234 Btree *p = pCur->pBtree;
78235 BtShared *pBt = p->pBt;
78240 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
78244 ** In some cases, the call to btreeMoveto() below is a no-op. For
78245 ** example, when inserting data into a table with auto-generated integer
78248 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
78253 if( pCur->curFlags & BTCF_Multiple ){
78254 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
78256 if( loc && pCur->iPage<0 ){
78269 if( pCur->eState>=CURSOR_REQUIRESEEK ){
78270 testcase( pCur->eState==CURSOR_REQUIRESEEK );
78271 testcase( pCur->eState==CURSOR_FAULT );
78277 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
78278 && pBt->inTransaction==TRANS_WRITE
78279 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
78280 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
78283 ** expecting an index b-tree, then the caller should be inserting blob
78287 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
78289 if( pCur->pKeyInfo==0 ){
78290 assert( pX->pKey==0 );
78291 /* If this is an insert into a table b-tree, invalidate any incrblob
78293 if( p->hasIncrblobCur ){
78294 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
78302 assert( pCur->curFlags & BTCF_ValidNKey );
78303 assert( pX->nKey==pCur->info.nKey );
78312 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
78315 assert( pX->nData>=0 && pX->nZero>=0 );
78316 if( pCur->info.nSize!=0
78317 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
78328 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
78346 if( pX->nMem ){
78348 r.pKeyInfo = pCur->pKeyInfo;
78349 r.aMem = pX->aMem;
78350 r.nField = pX->nMem;
78355 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
78367 if( pCur->info.nKey==pX->nKey ){
78369 x2.pData = pX->pKey;
78370 x2.nData = pX->nKey;
78376 assert( pCur->eState==CURSOR_VALID
78377 || (pCur->eState==CURSOR_INVALID && loc) );
78379 pPage = pCur->pPage;
78380 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
78381 assert( pPage->leaf || !pPage->intKey );
78382 if( pPage->nFree<0 ){
78383 if( NEVER(pCur->eState>CURSOR_INVALID) ){
78384 /* ^^^^^--- due to the moveToRoot() call above */
78393 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
78395 assert( pPage->isInit || CORRUPT_DB );
78396 newCell = pBt->pTmpSpace;
78400 szNew = pBt->nPreformatSize;
78402 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
78404 pPage->xParseCell(pPage, newCell, &info);
78406 Pgno ovfl = get4byte(&newCell[szNew-4]);
78407 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
78414 assert( szNew==pPage->xCellSize(pPage, newCell) );
78416 idx = pCur->ix;
78420 if( idx>=pPage->nCell ){
78423 rc = sqlite3PagerWrite(pPage->pDbPage);
78428 if( !pPage->leaf ){
78432 testcase( pCur->curFlags & BTCF_ValidOvfl );
78435 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
78445 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
78447 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
78450 if( oldCell+szNew > pPage->aDataEnd ){
78458 }else if( loc<0 && pPage->nCell>0 ){
78459 assert( pPage->leaf );
78460 idx = ++pCur->ix;
78461 pCur->curFlags &= ~BTCF_ValidNKey;
78463 assert( pPage->leaf );
78466 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
78467 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
78481 ** multiple records into an intkey b-tree using a single cursor (as can
78484 ** the b-tree if possible. If the cursor is left pointing to the last
78489 pCur->info.nSize = 0;
78490 if( pPage->nOverflow ){
78492 pCur->curFlags &= ~(BTCF_ValidNKey);
78499 pCur->pPage->nOverflow = 0;
78500 pCur->eState = CURSOR_INVALID;
78503 if( pCur->pKeyInfo ){
78504 assert( pCur->pKey==0 );
78505 pCur->pKey = sqlite3Malloc( pX->nKey );
78506 if( pCur->pKey==0 ){
78509 memcpy(pCur->pKey, pX->pKey, pX->nKey);
78512 pCur->eState = CURSOR_REQUIRESEEK;
78513 pCur->nKey = pX->nKey;
78516 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
78539 BtShared *pBt = pDest->pBt;
78540 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
78546 if( pSrc->info.nPayload<0x80 ){
78547 *(aOut++) = pSrc->info.nPayload;
78549 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
78551 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
78552 nIn = pSrc->info.nLocal;
78553 aIn = pSrc->info.pPayload;
78554 if( aIn+nIn>pSrc->pPage->aDataEnd ){
78557 nRem = pSrc->info.nPayload;
78558 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
78560 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
78562 Pager *pSrcPager = pSrc->pBt->pPager;
78569 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
78570 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
78571 if( nOut<pSrc->info.nPayload ){
78573 pBt->nPreformatSize += 4;
78577 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
78580 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
78584 nRem -= nOut;
78590 nOut -= nCopy;
78591 nIn -= nCopy;
78603 nIn = pSrc->pBt->usableSize - 4;
78614 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
78619 pPgnoOut = pPageOut->aData;
78622 nOut = MIN(pBt->usableSize - 4, nRem);
78647 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
78652 Btree *p = pCur->pBtree;
78653 BtShared *pBt = p->pBt;
78663 assert( pBt->inTransaction==TRANS_WRITE );
78664 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78665 assert( pCur->curFlags & BTCF_WriteFlag );
78666 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
78667 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
78669 if( pCur->eState!=CURSOR_VALID ){
78670 if( pCur->eState>=CURSOR_REQUIRESEEK ){
78672 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
78673 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
78678 assert( pCur->eState==CURSOR_VALID );
78680 iCellDepth = pCur->iPage;
78681 iCellIdx = pCur->ix;
78682 pPage = pCur->pPage;
78683 if( pPage->nCell<=iCellIdx ){
78687 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
78693 ** will cause a b-tree rebalance, then this is done by saving the cursor
78709 if( !pPage->leaf
78710 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
78711 (int)(pBt->usableSize*2/3)
78712 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
78714 /* A b-tree rebalance will be required after deleting this entry.
78728 ** sub-tree headed by the child page of the cell being deleted. This makes
78730 if( !pPage->leaf ){
78738 if( pCur->curFlags & BTCF_Multiple ){
78739 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
78743 /* If this is a delete operation to remove a row from a table b-tree,
78745 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
78746 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
78752 rc = sqlite3PagerWrite(pPage->pDbPage);
78759 ** is currently pointing to the largest entry in the sub-tree headed
78760 ** by the child-page of the cell that was just deleted from an internal
78763 if( !pPage->leaf ){
78764 MemPage *pLeaf = pCur->pPage;
78769 if( pLeaf->nFree<0 ){
78773 if( iCellDepth<pCur->iPage-1 ){
78774 n = pCur->apPage[iCellDepth+1]->pgno;
78776 n = pCur->pPage->pgno;
78778 pCell = findCell(pLeaf, pLeaf->nCell-1);
78779 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
78780 nCell = pLeaf->xCellSize(pLeaf, pCell);
78782 pTmp = pBt->pTmpSpace;
78784 rc = sqlite3PagerWrite(pLeaf->pDbPage);
78786 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
78788 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
78807 assert( pCur->pPage->nOverflow==0 );
78808 assert( pCur->pPage->nFree>=0 );
78809 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
78811 ** then balance() will always be a no-op. No need to invoke it. */
78816 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
78817 releasePageNotNull(pCur->pPage);
78818 pCur->iPage--;
78819 while( pCur->iPage>iCellDepth ){
78820 releasePage(pCur->apPage[pCur->iPage--]);
78822 pCur->pPage = pCur->apPage[pCur->iPage];
78828 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
78829 assert( pPage==pCur->pPage || CORRUPT_DB );
78830 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
78831 pCur->eState = CURSOR_SKIPNEXT;
78832 if( iCellIdx>=pPage->nCell ){
78833 pCur->skipNext = -1;
78834 pCur->ix = pPage->nCell-1;
78836 pCur->skipNext = 1;
78842 pCur->eState = CURSOR_REQUIRESEEK;
78862 BtShared *pBt = p->pBt;
78866 int ptfFlags; /* Page-type flage for the root page of new table */
78869 assert( pBt->inTransaction==TRANS_WRITE );
78870 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78878 if( pBt->autoVacuum ){
78879 Pgno pgnoMove; /* Move a page here to make room for the root-page */
78884 ** out to be an overflow page, delete all overflow page-map caches
78890 ** root page of the new table should go. meta[3] is the largest root-page
78891 ** created so far, so the new root-page is (meta[3]+1).
78899 /* The new root-page may not be allocated on a pointer-map page, or the
78918 /* pgnoRoot is the page that will be used for the root-page of
78962 rc = sqlite3PagerWrite(pRoot->pDbPage);
78971 /* Update the pointer-map and meta-data with the new root-page number. */
78982 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
78994 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
79001 sqlite3PagerUnref(pRoot->pDbPage);
79002 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
79031 assert( sqlite3_mutex_held(pBt->mutex) );
79037 if( (pBt->openFlags & BTREE_SINGLE)==0
79038 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
79043 hdr = pPage->hdrOffset;
79044 for(i=0; i<pPage->nCell; i++){
79046 if( !pPage->leaf ){
79053 if( !pPage->leaf ){
79054 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
79056 if( pPage->intKey ) pnChange = 0;
79059 testcase( !pPage->intKey );
79060 *pnChange += pPage->nCell;
79064 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
79065 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
79087 BtShared *pBt = p->pBt;
79089 assert( p->inTrans==TRANS_WRITE );
79095 ** is the root of a table b-tree - if it is not, the following call is
79096 ** a no-op). */
79097 if( p->hasIncrblobCur ){
79112 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
79138 BtShared *pBt = p->pBt;
79141 assert( p->inTrans==TRANS_WRITE );
79161 if( pBt->autoVacuum ){
79166 /* If the table being dropped is the table with the largest root-page
79175 /* The table being dropped does not have the largest root-page
79177 ** gap left by the deleted root-page.
79200 /* Set the new 'max-root-page' value in the database header. This
79202 ** be a root-page number, less one again if that is the
79205 maxRootPgno--;
79208 maxRootPgno--;
79230 ** This function may only be called if the b-tree connection already
79233 ** Read the meta-information out of a database file. Meta[0]
79236 ** is read-only, the others are read/write.
79250 BtShared *pBt = p->pBt;
79253 assert( p->inTrans>TRANS_NONE );
79255 assert( pBt->pPage1 );
79259 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
79261 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
79264 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
79265 ** database, mark the database as read-only. */
79268 pBt->btsFlags |= BTS_READ_ONLY;
79276 ** Write meta-information back into the database. Meta[0] is
79277 ** read-only and may not be written.
79280 BtShared *pBt = p->pBt;
79285 assert( p->inTrans==TRANS_WRITE );
79286 assert( pBt->pPage1!=0 );
79287 pP1 = pBt->pPage1->aData;
79288 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
79293 assert( pBt->autoVacuum || iMeta==0 );
79295 pBt->incrVacuum = (u8)iMeta;
79304 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
79305 ** number of entries in the b-tree and write the result to *pnEntry.
79322 ** page in the B-Tree structure (not including overflow pages).
79324 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
79326 MemPage *pPage; /* Current page of the b-tree */
79328 /* If this is a leaf page or the tree is not an int-key tree, then
79332 pPage = pCur->pPage;
79333 if( pPage->leaf || !pPage->intKey ){
79334 nEntry += pPage->nCell;
79340 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
79342 ** to visit is the right-child of its parent.
79347 if( pPage->leaf ){
79349 if( pCur->iPage==0 ){
79350 /* All pages of the b-tree have been visited. Return successfully. */
79355 }while ( pCur->ix>=pCur->pPage->nCell );
79357 pCur->ix++;
79358 pPage = pCur->pPage;
79362 ** points at. This is the right-child if (iIdx==pPage->nCell).
79364 iIdx = pCur->ix;
79365 if( iIdx==pPage->nCell ){
79366 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
79381 return p->pBt->pPager;
79394 if( !pCheck->mxErr ) return;
79395 pCheck->mxErr--;
79396 pCheck->nErr++;
79398 if( pCheck->errMsg.nChar ){
79399 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
79401 if( pCheck->zPfx ){
79402 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
79404 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
79406 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
79407 pCheck->bOomFault = 1;
79415 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
79419 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
79420 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
79427 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
79428 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
79434 ** reference to the page, add an error message to pCheck->zErrMsg.
79441 if( iPage>pCheck->nPage || iPage==0 ){
79449 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
79456 ** Check that the entry in the pointer-map for page iChild maps to
79470 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
79472 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
79497 int nErrAtStart = pCheck->nErr;
79498 while( iPage!=0 && pCheck->mxErr ){
79502 N--;
79503 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
79511 if( pCheck->pBt->autoVacuum ){
79515 if( n>pCheck->pBt->usableSize/4-2 ){
79518 N--;
79523 if( pCheck->pBt->autoVacuum ){
79529 N -= n;
79534 /* If this database supports auto-vacuum and iPage is not the last
79535 ** page in this overflow list, check that the pointer-map entry for
79538 if( pCheck->pBt->autoVacuum && N>0 ){
79547 if( N && nErrAtStart==pCheck->nErr ){
79551 expected-N, expected);
79557 ** An implementation of a min-heap.
79567 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
79594 aHeap[0]--;
79631 int depth = -1, d2; /* Depth of a subtree */
79647 u32 *heap = 0; /* Min-heap used for checking cell coverage */
79648 u32 x, prev = 0; /* Next and previous entry on the min-heap */
79649 const char *saved_zPfx = pCheck->zPfx;
79650 int saved_v1 = pCheck->v1;
79651 int saved_v2 = pCheck->v2;
79656 pBt = pCheck->pBt;
79657 usableSize = pBt->usableSize;
79660 pCheck->zPfx = "Page %u: ";
79661 pCheck->v1 = iPage;
79670 savedIsInit = pPage->isInit;
79671 pPage->isInit = 0;
79683 data = pPage->aData;
79684 hdr = pPage->hdrOffset;
79687 pCheck->zPfx = "On tree page %u cell %d: ";
79691 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
79694 assert( pPage->nCell==nCell );
79696 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
79697 ** immediately follows the b-tree page header. */
79698 cellStart = hdr + 12 - 4*pPage->leaf;
79699 assert( pPage->aCellIdx==&data[cellStart] );
79700 pCellIdx = &data[cellStart + 2*(nCell-1)];
79702 if( !pPage->leaf ){
79703 /* Analyze the right-child page of internal pages */
79706 if( pBt->autoVacuum ){
79707 pCheck->zPfx = "On page %u at right child: ";
79716 heap = pCheck->heap;
79720 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
79722 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
79726 pCheck->v2 = i;
79729 pCellIdx -= 2;
79730 if( pc<contentOffset || pc>usableSize-4 ){
79732 pc, contentOffset, usableSize-4);
79737 pPage->xParseCell(pPage, pCell, &info);
79745 if( pPage->intKey ){
79757 assert( pc + info.nSize - 4 <= usableSize );
79758 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
79759 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
79761 if( pBt->autoVacuum ){
79768 if( !pPage->leaf ){
79772 if( pBt->autoVacuum ){
79783 /* Populate the coverage-checking heap for leaf pages */
79784 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
79791 pCheck->zPfx = 0;
79792 if( doCoverageCheck && pCheck->mxErr>0 ){
79793 /* For leaf pages, the min-heap has already been initialized and the
79796 if( !pPage->leaf ){
79797 heap = pCheck->heap;
79799 for(i=nCell-1; i>=0; i--){
79802 size = pPage->xCellSize(pPage, &data[pc]);
79803 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
79806 /* Add the freeblocks to the min-heap
79808 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
79815 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79818 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
79819 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
79820 ** big-endian integer which is the offset in the b-tree page of the next
79824 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
79827 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79830 /* Analyze the min-heap looking for overlap between cells and/or
79833 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
79838 ** The loop below pulls entries from the min-heap in order and compares
79844 prev = contentOffset - 1; /* Implied first min-heap entry */
79851 nFrag += (x>>16) - (prev&0xffff) - 1;
79855 nFrag += usableSize - (prev&0xffff) - 1;
79856 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
79857 ** is stored in the fifth field of the b-tree page header.
79858 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
79869 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
79871 pCheck->zPfx = saved_zPfx;
79872 pCheck->v1 = saved_v1;
79873 pCheck->v2 = saved_v2;
79884 ** A read-only or read-write transaction must be opened before calling
79889 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
79893 ** root pages is incomplete. This is a "partial integrity-check". This
79911 BtShared *pBt = p->pBt;
79912 u64 savedDbFlags = pBt->db->flags;
79927 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
79928 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
79932 sCheck.pPager = pBt->pPager;
79953 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
79966 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
79967 get4byte(&pBt->pPage1->aData[36]));
79975 if( pBt->autoVacuum ){
79979 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
79986 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
79993 testcase( pBt->db->flags & SQLITE_CellSizeCk );
79994 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
79999 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
80005 pBt->db->flags = savedDbFlags;
80016 /* If the database supports auto-vacuum, make sure no tables contain
80017 ** references to pointer-map pages.
80020 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
80024 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
80043 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
80051 ** an empty string if the database is in-memory or a TEMP database.
80057 assert( p->pBt->pPager!=0 );
80058 return sqlite3PagerFilename(p->pBt->pPager, 1);
80070 assert( p->pBt->pPager!=0 );
80071 return sqlite3PagerJournalname(p->pBt->pPager);
80079 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
80080 return p ? p->inTrans : 0;
80088 ** transaction on the shared-cache the argument Btree is connected to.
80095 BtShared *pBt = p->pBt;
80098 if( pBt->inTransaction!=TRANS_NONE ){
80102 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
80115 assert( sqlite3_mutex_held(p->db->mutex) );
80116 return p->nBackup!=0;
80121 ** a single shared-btree. The memory is used by client code for its own
80122 ** purposes (for example, to store a high-level schema associated with
80123 ** the shared-btree). The btree layer manages reference counting issues.
80125 ** The first time this is called on a shared-btree, nBytes bytes of memory
80134 ** Just before the shared-btree is closed, the function passed as the
80140 BtShared *pBt = p->pBt;
80142 if( !pBt->pSchema && nBytes ){
80143 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
80144 pBt->xFreeSchema = xFree;
80147 return pBt->pSchema;
80157 assert( sqlite3_mutex_held(p->db->mutex) );
80174 assert( p->inTrans!=TRANS_NONE );
80175 if( p->sharable ){
80205 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
80206 assert( pCsr->curFlags & BTCF_Incrblob );
80212 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
80213 if( pCsr->eState!=CURSOR_VALID ){
80219 ** version of the b-tree page modified by the accessPayload call below.
80225 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
80231 ** (c) the connection holds a write-lock on the table (if required),
80232 ** (d) there are no conflicting read-locks, and
80235 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
80238 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
80239 && pCsr->pBt->inTransaction==TRANS_WRITE );
80240 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
80241 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
80242 assert( pCsr->pPage->intKey );
80251 pCur->curFlags |= BTCF_Incrblob;
80252 pCur->pBtree->hasIncrblobCur = 1;
80262 BtShared *pBt = pBtree->pBt;
80270 pBt->btsFlags &= ~BTS_NO_WAL;
80271 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
80275 u8 *aData = pBt->pPage1->aData;
80279 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
80288 pBt->btsFlags &= ~BTS_NO_WAL;
80297 return (pCsr->hints & mask)!=0;
80301 ** Return true if the given Btree is read-only.
80304 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
80313 ** If no transaction is active and the database is not a temp-db, clear
80314 ** the in-memory pager cache.
80317 BtShared *pBt = p->pBt;
80318 if( pBt->inTransaction==TRANS_NONE ){
80319 sqlite3PagerClearCache(pBt->pPager);
80328 return p->sharable;
80337 testcase( p->sharable );
80338 return p->pBt->nRef;
80366 Btree *pDest; /* Destination b-tree file */
80368 int bDestLocked; /* True once a write-transaction is open on pDest */
80372 Btree *pSrc; /* Source b-tree file */
80390 ** structure may be accessed via two groups of thread-safe entry points:
80404 ** backup_pagecount() are not thread-safe functions. If they are called
80412 ** Non-sharable Btrees (in-memory databases for example), do not have
80448 return pDb->aDb[i].pBt;
80457 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
80462 ** Check that there is no open read-transaction on the b-tree passed as the
80464 ** is an open read-transaction, return SQLITE_ERROR and leave an error
80506 sqlite3_mutex_enter(pSrcDb->mutex);
80507 sqlite3_mutex_enter(pDestDb->mutex);
80516 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
80527 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
80528 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
80529 p->pDestDb = pDestDb;
80530 p->pSrcDb = pSrcDb;
80531 p->iNext = 1;
80532 p->isAttached = 0;
80534 if( 0==p->pSrc || 0==p->pDest
80535 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
80547 p->pSrc->nBackup++;
80550 sqlite3_mutex_leave(pDestDb->mutex);
80551 sqlite3_mutex_leave(pSrcDb->mutex);
80575 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
80576 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
80577 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
80581 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
80582 ** guaranteed that the shared-mutex is held by this thread, handle
80583 ** p->pSrc may not actually be the owner. */
80584 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
80585 int nDestReserve = sqlite3BtreeGetRequestedReserve(p->pDest);
80590 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
80591 assert( p->bDestLocked );
80592 assert( !isFatalError(p->rc) );
80593 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
80596 /* Catch the case where the destination is an in-memory database and the
80625 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
80628 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
80646 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
80658 ** this function is a no-op.
80678 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
80679 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80680 p->pNext = *pp;
80682 p->isAttached = 1;
80686 ** Copy nPage pages from the source b-tree to the destination.
80697 sqlite3_mutex_enter(p->pSrcDb->mutex);
80698 sqlite3BtreeEnter(p->pSrc);
80699 if( p->pDestDb ){
80700 sqlite3_mutex_enter(p->pDestDb->mutex);
80703 rc = p->rc;
80705 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
80706 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
80708 int nSrcPage = -1; /* Size of source db in pages */
80711 /* If the source pager is currently in a write-transaction, return
80714 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
80720 /* If there is no open read-transaction on the source database, open
80724 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
80725 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
80735 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
80740 if( SQLITE_OK==rc && p->bDestLocked==0
80741 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
80742 (int*)&p->iDestSchema))
80744 p->bDestLocked = 1;
80749 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
80750 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
80751 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
80756 /* Now that there is a read-lock on the source database, query the
80759 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
80761 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
80762 const Pgno iSrcPg = p->iNext; /* Source page number */
80763 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
80771 p->iNext++;
80774 p->nPagecount = nSrcPage;
80775 p->nRemaining = nSrcPage+1-p->iNext;
80776 if( p->iNext>(Pgno)nSrcPage ){
80778 }else if( !p->isAttached ){
80784 ** is to make sure that the schema-version really does change in
80790 rc = sqlite3BtreeNewDb(p->pDest);
80794 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
80797 if( p->pDestDb ){
80798 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
80801 rc = sqlite3BtreeSetVersion(p->pDest, 2);
80818 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
80819 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
80822 nDestTruncate = (nSrcPage+ratio-1)/ratio;
80823 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
80824 nDestTruncate--;
80832 /* If the source page-size is smaller than the destination page-size,
80838 ** pending-byte page in the source database may need to be
80851 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
80863 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
80907 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
80917 ** "committing" a read-only transaction cannot fail.
80921 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
80922 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
80929 p->rc = rc;
80931 if( p->pDestDb ){
80932 sqlite3_mutex_leave(p->pDestDb->mutex);
80934 sqlite3BtreeLeave(p->pSrc);
80935 sqlite3_mutex_leave(p->pSrcDb->mutex);
80949 pSrcDb = p->pSrcDb;
80950 sqlite3_mutex_enter(pSrcDb->mutex);
80951 sqlite3BtreeEnter(p->pSrc);
80952 if( p->pDestDb ){
80953 sqlite3_mutex_enter(p->pDestDb->mutex);
80957 if( p->pDestDb ){
80958 p->pSrc->nBackup--;
80960 if( p->isAttached ){
80961 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80964 pp = &(*pp)->pNext;
80967 *pp = p->pNext;
80971 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
80974 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
80975 if( p->pDestDb ){
80976 sqlite3Error(p->pDestDb, rc);
80979 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
80981 sqlite3BtreeLeave(p->pSrc);
80982 if( p->pDestDb ){
80983 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
81003 return p->nRemaining;
81017 return p->nPagecount;
81039 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81040 if( !isFatalError(p->rc) && iPage<p->iNext ){
81046 assert( p->pDestDb );
81047 sqlite3_mutex_enter(p->pDestDb->mutex);
81049 sqlite3_mutex_leave(p->pDestDb->mutex);
81052 p->rc = rc;
81055 }while( (p = p->pNext)!=0 );
81074 for(p=pBackup; p; p=p->pNext){
81075 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
81076 p->iNext = 1;
81098 if( pFd->pMethods ){
81111 b.pSrcDb = pFrom->db;
81124 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
81131 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
81169 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
81182 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
81188 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
81191 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
81193 if( p->flags & MEM_Null ){
81195 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
81202 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
81205 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
81206 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
81207 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
81210 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
81218 assert( (p->flags & MEM_Cleared)==0 );
81222 assert( p->szMalloc==0
81223 || (p->flags==MEM_Undefined
81224 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
81225 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
81235 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
81237 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
81238 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
81239 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
81240 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
81253 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
81255 if( p->flags & MEM_Int ){
81257 /* Work-around for GCC bug
81260 assert( (p->flags&MEM_Int)*2==sizeof(x) );
81261 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
81264 sqlite3Int64ToText(p->u.i, zBuf);
81269 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
81288 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
81302 if( (p->flags & MEM_Str)==0 ) return 1;
81303 if( p->flags & MEM_Term ){
81304 /* Insure that the string is properly zero-terminated. Pay particular
81305 ** attention to the case where p->n is odd */
81306 if( p->szMalloc>0 && p->z==p->zMalloc ){
81307 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
81308 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
81310 assert( p->z[p->n]==0 );
81311 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
81312 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
81314 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
81316 z = p->z;
81319 if( p->enc!=SQLITE_UTF8 ){
81321 if( p->enc==SQLITE_UTF16BE ) z++;
81338 ** routine is a no-op.
81352 if( !(pMem->flags&MEM_Str) ){
81353 pMem->enc = desiredEnc;
81356 if( pMem->enc==desiredEnc ){
81359 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81369 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
81370 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
81376 ** Make sure pMem->z points to a writable allocation of at least n bytes.
81379 ** pMem->z into the new allocation. pMem must be either a string or
81381 ** in pMem->z is discarded.
81386 testcase( pMem->db==0 );
81390 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
81391 testcase( bPreserve && pMem->z==0 );
81393 assert( pMem->szMalloc==0
81394 || (pMem->flags==MEM_Undefined
81395 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
81396 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
81397 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
81398 if( pMem->db ){
81399 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
81401 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
81402 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
81403 pMem->z = pMem->zMalloc;
81407 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
81408 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
81410 if( pMem->zMalloc==0 ){
81412 pMem->z = 0;
81413 pMem->szMalloc = 0;
81416 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
81419 if( bPreserve && pMem->z ){
81420 assert( pMem->z!=pMem->zMalloc );
81421 memcpy(pMem->zMalloc, pMem->z, pMem->n);
81423 if( (pMem->flags&MEM_Dyn)!=0 ){
81424 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
81425 pMem->xDel((void *)(pMem->z));
81428 pMem->z = pMem->zMalloc;
81429 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
81434 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
81435 ** If pMem->zMalloc already meets or exceeds the requested size, this
81436 ** routine is a no-op.
81439 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
81448 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
81449 if( pMem->szMalloc<szNew ){
81452 assert( (pMem->flags & MEM_Dyn)==0 );
81453 pMem->z = pMem->zMalloc;
81454 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
81463 ** to be a double-zero byte at an even byte boundary in order to
81468 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
81471 pMem->z[pMem->n] = 0;
81472 pMem->z[pMem->n+1] = 0;
81473 pMem->z[pMem->n+2] = 0;
81474 pMem->flags |= MEM_Term;
81486 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81488 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
81490 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
81495 pMem->flags &= ~MEM_Ephem;
81497 pMem->pScopyFrom = 0;
81504 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
81511 assert( pMem->flags & MEM_Zero );
81512 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
81515 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81518 nByte = pMem->n + pMem->u.nZero;
81520 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
81526 assert( pMem->z!=0 );
81527 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
81529 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
81530 pMem->n += pMem->u.nZero;
81531 pMem->flags &= ~(MEM_Zero|MEM_Term);
81541 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81542 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
81543 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
81544 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
81569 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81570 assert( !(pMem->flags&MEM_Zero) );
81571 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
81572 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
81578 pMem->enc = 0;
81582 vdbeMemRenderNum(nByte, pMem->z, pMem);
81583 assert( pMem->z!=0 );
81584 pMem->n = sqlite3Strlen30NN(pMem->z);
81585 pMem->enc = SQLITE_UTF8;
81586 pMem->flags |= MEM_Str|MEM_Term;
81587 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
81605 assert( pMem->db!=0 );
81606 assert( pFunc->xFinalize!=0 );
81607 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
81608 assert( sqlite3_mutex_held(pMem->db->mutex) );
81612 t.db = pMem->db;
81617 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
81618 assert( (pMem->flags & MEM_Dyn)==0 );
81619 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
81636 assert( pFunc->xValue!=0 );
81637 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
81638 assert( pAccum->db!=0 );
81639 assert( sqlite3_mutex_held(pAccum->db->mutex) );
81645 ctx.enc = ENC(pAccum->db);
81646 pFunc->xValue(&ctx);
81661 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
81663 if( p->flags&MEM_Agg ){
81664 sqlite3VdbeMemFinalize(p, p->u.pDef);
81665 assert( (p->flags & MEM_Agg)==0 );
81666 testcase( p->flags & MEM_Dyn );
81668 if( p->flags&MEM_Dyn ){
81669 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
81670 p->xDel((void *)p->z);
81672 p->flags = MEM_Null;
81677 ** by p->xDel and memory in p->zMalloc.
81687 if( p->szMalloc ){
81688 sqlite3DbFreeNN(p->db, p->zMalloc);
81689 p->szMalloc = 0;
81691 p->z = 0;
81706 if( VdbeMemDynamic(p) || p->szMalloc ){
81716 if( p->szMalloc ) vdbeMemClear(p);
81720 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
81721 ** If the double is out of range of a 64-bit signed integer then
81722 ** return the closest available 64-bit signed integer.
81726 /* When floating-point is omitted, double and int64 are the same thing */
81731 ** minimum and maximum 64-bit integers, or they define them
81734 ** larger than a 32-bit integer constant.
81753 ** a floating-point then the value returned is the integer part.
81756 ** an SQL-NULL value, return 0.
81762 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
81768 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81770 flags = pMem->flags;
81773 return pMem->u.i;
81775 return doubleToInt64(pMem->u.r);
81776 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
81792 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
81797 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81799 if( pMem->flags & MEM_Real ){
81800 return pMem->u.r;
81801 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
81802 testcase( pMem->flags & MEM_IntReal );
81803 return (double)pMem->u.i;
81804 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
81817 testcase( pMem->flags & MEM_IntReal );
81818 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
81819 if( pMem->flags & MEM_Null ) return ifNull;
81830 assert( pMem->flags & MEM_Real );
81832 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81835 ix = doubleToInt64(pMem->u.r);
81839 ** (1) the round-trip conversion real->int->real is a no-op, and
81847 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
81848 pMem->u.i = ix;
81858 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81862 pMem->u.i = sqlite3VdbeIntValue(pMem);
81873 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81876 pMem->u.r = sqlite3VdbeRealValue(pMem);
81886 ** For some versions of GCC on 32-bit machines, if you do the more obvious
81888 ** though the r1 and (double)i values are bit-for-bit the same.
81894 && i >= -2251799813685248LL && i < 2251799813685248LL);
81917 testcase( pMem->flags & MEM_Int );
81918 testcase( pMem->flags & MEM_Real );
81919 testcase( pMem->flags & MEM_IntReal );
81920 testcase( pMem->flags & MEM_Null );
81921 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
81924 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
81925 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81926 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
81927 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
81928 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
81930 pMem->u.i = ix;
81936 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
81937 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
81949 if( pMem->flags & MEM_Null ) return SQLITE_OK;
81952 if( (pMem->flags & MEM_Blob)==0 ){
81954 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81955 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
81957 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
81976 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
81978 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81979 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
81980 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
81994 pMem->flags = flags;
81995 pMem->db = db;
81996 pMem->szMalloc = 0;
82016 pMem->flags = MEM_Null;
82030 pMem->flags = MEM_Blob|MEM_Zero;
82031 pMem->n = 0;
82033 pMem->u.nZero = n;
82034 pMem->enc = SQLITE_UTF8;
82035 pMem->z = 0;
82043 assert( pMem->z!=0 );
82044 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
82045 memset(pMem->z, 0, nByte);
82046 pMem->n = n>0?n:0;
82047 pMem->flags = MEM_Blob;
82048 pMem->enc = SQLITE_UTF8;
82056 ** a 64-bit integer.
82060 pMem->u.i = val;
82061 pMem->flags = MEM_Int;
82072 pMem->u.i = val;
82073 pMem->flags = MEM_Int;
82077 /* A no-op destructor */
82090 assert( pMem->flags==MEM_Null );
82092 pMem->u.zPType = zPType ? zPType : "";
82093 pMem->z = pPtr;
82094 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
82095 pMem->eSubtype = 'p';
82096 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
82107 pMem->u.r = val;
82108 pMem->flags = MEM_Real;
82119 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
82120 && pMem->xDel==sqlite3RowSetDelete;
82132 sqlite3 *db = pMem->db;
82139 pMem->z = (char*)p;
82140 pMem->flags = MEM_Blob|MEM_Dyn;
82141 pMem->xDel = sqlite3RowSetDelete;
82147 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
82150 assert( p->db!=0 );
82151 if( p->flags & (MEM_Str|MEM_Blob) ){
82152 int n = p->n;
82153 if( p->flags & MEM_Zero ){
82154 n += p->u.nZero;
82156 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
82167 ** This is used for testing and debugging only - to help ensure that shallow
82173 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
82174 if( pX->pScopyFrom==pMem ){
82176 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
82178 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
82186 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
82187 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
82190 ** undefined so that we can quickly detect the shallow-copy error */
82191 pX->flags = MEM_Undefined;
82192 pX->pScopyFrom = 0;
82195 pMem->pScopyFrom = 0;
82201 ** pTo are freed. The pFrom->z field is not duplicated. If
82202 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
82212 assert( pTo->db==pFrom->db );
82215 if( (pFrom->flags&MEM_Static)==0 ){
82216 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
82218 pTo->flags |= srcType;
82232 pTo->flags &= ~MEM_Dyn;
82233 if( pTo->flags&(MEM_Str|MEM_Blob) ){
82234 if( 0==(pFrom->flags&MEM_Static) ){
82235 pTo->flags |= MEM_Ephem;
82250 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
82251 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
82252 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
82256 pFrom->flags = MEM_Null;
82257 pFrom->szMalloc = 0;
82280 ** non-negative for blobs.
82289 i64 nByte = n; /* New value for pMem->n */
82291 u16 flags; /* New value for pMem->flags */
82294 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
82304 if( pMem->db ){
82305 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
82326 sqlite3DbFree(pMem->db, (void*)z);
82332 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
82350 memcpy(pMem->z, z, nAlloc);
82353 pMem->z = (char *)z;
82355 pMem->zMalloc = pMem->z;
82356 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
82358 pMem->xDel = xDel;
82363 pMem->n = (int)(nByte & 0x7fffffff);
82364 pMem->flags = flags;
82365 pMem->enc = enc;
82384 ** pMem->zMalloc to hold the content from the btree, if possible. New
82385 ** pMem->zMalloc space will be allocated if necessary. The calling routine
82399 pMem->flags = MEM_Null;
82404 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
82406 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
82407 pMem->flags = MEM_Blob;
82408 pMem->n = (int)amt;
82429 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
82430 assert( pMem->z!=0 );
82433 pMem->flags = MEM_Blob|MEM_Ephem;
82434 pMem->n = (int)amt;
82445 ** to a zero-terminated version of that string.
82449 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
82452 assert( (pVal->flags & (MEM_Null))==0 );
82453 if( pVal->flags & (MEM_Blob|MEM_Str) ){
82455 pVal->flags |= MEM_Str;
82456 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
82459 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
82460 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
82465 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
82468 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
82470 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
82471 || pVal->db->mallocFailed );
82472 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
82474 return pVal->z;
82486 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
82492 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
82495 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
82497 return pVal->z;
82499 if( pVal->flags&MEM_Null ){
82511 p->flags = MEM_Null;
82512 p->db = db;
82533 ** Otherwise, if the second argument is non-zero, then this function is
82542 UnpackedRecord *pRec = p->ppRec[0];
82545 Index *pIdx = p->pIdx; /* Index being probed */
82548 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
82553 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
82554 if( pRec->pKeyInfo ){
82555 assert( pRec->pKeyInfo->nAllField==nCol );
82556 assert( pRec->pKeyInfo->enc==ENC(db) );
82557 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
82559 pRec->aMem[i].flags = MEM_Null;
82560 pRec->aMem[i].db = db;
82568 p->ppRec[0] = pRec;
82571 pRec->nField = p->iVal+1;
82572 return &pRec->aMem[p->iVal];
82619 assert( (p->flags & EP_TokenOnly)==0 );
82621 pList = p->x.pList;
82622 if( pList ) nVal = pList->nExpr;
82624 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
82626 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
82627 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
82639 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
82650 testcase( pCtx->pParse->rc==SQLITE_ERROR );
82651 testcase( pCtx->pParse->rc==SQLITE_OK );
82656 pFunc->xSFunc(&ctx, nVal, apVal);
82659 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
82666 pCtx->pParse->nErr++;
82669 pCtx->pParse->rc = rc;
82715 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
82716 if( op==TK_REGISTER ) op = pExpr->op2;
82722 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
82727 aff = sqlite3AffinityType(pExpr->u.zToken,0);
82728 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
82738 ** case when the value is -9223372036854775808.
82741 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
82742 pExpr = pExpr->pLeft;
82743 op = pExpr->op;
82744 negInt = -1;
82745 zNeg = "-";
82752 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
82754 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
82756 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
82763 assert( (pVal->flags & MEM_IntReal)==0 );
82764 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
82765 testcase( pVal->flags & MEM_Int );
82766 testcase( pVal->flags & MEM_Real );
82767 pVal->flags &= ~MEM_Str;
82773 /* This branch happens for multiple negative signs. Ex: -(-5) */
82774 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
82778 if( pVal->flags & MEM_Real ){
82779 pVal->u.r = -pVal->u.r;
82780 }else if( pVal->u.i==SMALLEST_INT64 ){
82782 pVal->u.r = -(double)SMALLEST_INT64;
82784 pVal->u.r = LARGEST_INT64;
82788 pVal->u.i = -pVal->u.i;
82801 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
82802 assert( pExpr->u.zToken[1]=='\'' );
82805 zVal = &pExpr->u.zToken[2];
82806 nVal = sqlite3Strlen30(zVal)-1;
82821 pVal->flags = MEM_Int;
82822 pVal->u.i = pExpr->u.zToken[4]==0;
82831 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
82892 sqlite3 *db = pParse->db;
82897 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
82903 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
82905 int iBindVar = pExpr->iColumn;
82906 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
82907 if( (v = pParse->pReprepare)!=0 ){
82910 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
82912 pVal->db = pParse->db;
82919 assert( pVal==0 || pVal->db==db );
82970 if( pExpr==0 || pExpr->op!=TK_SELECT ){
82981 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
83013 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
83055 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
83056 pMem->enc = ENC(db);
83068 int nCol = pRec->pKeyInfo->nAllField;
83069 Mem *aMem = pRec->aMem;
83074 sqlite3KeyInfoUnref(pRec->pKeyInfo);
83099 sqlite3DbFreeNN(((Mem*)v)->db, v);
83108 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
83112 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
83113 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
83114 return p->n;
83116 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
83117 return p->n;
83119 if( (p->flags & MEM_Blob)!=0 ){
83120 if( p->flags & MEM_Zero ){
83121 return p->n + p->u.nZero;
83123 return p->n;
83126 if( p->flags & MEM_Null ) return 0;
83157 sqlite3 *db = pParse->db;
83161 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
83162 p->db = db;
83163 if( db->pVdbe ){
83164 db->pVdbe->ppVPrev = &p->pVNext;
83166 p->pVNext = db->pVdbe;
83167 p->ppVPrev = &db->pVdbe;
83168 db->pVdbe = p;
83169 assert( p->eVdbeState==VDBE_INIT_STATE );
83170 p->pParse = pParse;
83171 pParse->pVdbe = p;
83172 assert( pParse->aLabel==0 );
83173 assert( pParse->nLabel==0 );
83174 assert( p->nOpAlloc==0 );
83175 assert( pParse->szOpAlloc==0 );
83184 return p->pParse;
83192 sqlite3DbFree(p->db, p->zErrMsg);
83194 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
83203 p->prepFlags = prepFlags;
83205 p->expmask = 0;
83207 assert( p->zSql==0 );
83208 p->zSql = sqlite3DbStrNDup(p->db, z, n);
83213 ** Add a new element to the Vdbe->pDblStr list.
83219 sizeof(*pStr)+n+1-sizeof(pStr->z));
83221 pStr->pNextStr = p->pDblStr;
83222 p->pDblStr = pStr;
83223 memcpy(pStr->z, z, n+1);
83231 ** zId of length nId is a double-quoted identifier. Check to see if
83236 const char *zId /* The double-quoted identifier, already dequoted */
83240 if( pVdbe->pDblStr==0 ) return 0;
83241 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
83242 if( strcmp(zId, pStr->z)==0 ) return 1;
83249 ** Swap byte-code between two VDBE structures.
83261 assert( pA->db==pB->db );
83265 pTmp = pA->pVNext;
83266 pA->pVNext = pB->pVNext;
83267 pB->pVNext = pTmp;
83268 ppTmp = pA->ppVPrev;
83269 pA->ppVPrev = pB->ppVPrev;
83270 pB->ppVPrev = ppTmp;
83271 zTmp = pA->zSql;
83272 pA->zSql = pB->zSql;
83273 pB->zSql = zTmp;
83275 zTmp = pA->zNormSql;
83276 pA->zNormSql = pB->zNormSql;
83277 pB->zNormSql = zTmp;
83279 pB->expmask = pA->expmask;
83280 pB->prepFlags = pA->prepFlags;
83281 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
83282 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
83290 ** If an out-of-memory error occurs while resizing the array, return
83297 Parse *p = v->pParse;
83299 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
83307 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
83308 : (sqlite3_int64)v->nOpAlloc+nOp);
83310 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
83316 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
83317 sqlite3OomFault(p->db);
83322 assert( nNew>=(v->nOpAlloc+nOp) );
83323 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
83325 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
83326 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
83327 v->aOp = pNew;
83367 assert( p->nOpAlloc<=p->nOp );
83369 assert( p->nOpAlloc>p->nOp );
83376 i = p->nOp;
83377 assert( p->eVdbeState==VDBE_INIT_STATE );
83379 if( p->nOpAlloc<=i ){
83382 assert( p->aOp!=0 );
83383 p->nOp++;
83384 pOp = &p->aOp[i];
83386 pOp->opcode = (u8)op;
83387 pOp->p5 = 0;
83388 pOp->p1 = p1;
83389 pOp->p2 = p2;
83390 pOp->p3 = p3;
83391 pOp->p4.p = 0;
83392 pOp->p4type = P4_NOTUSED;
83394 pOp->zComment = 0;
83397 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83398 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
83399 test_addop_breakpoint(i, &p->aOp[i]);
83403 pOp->cycles = 0;
83404 pOp->cnt = 0;
83407 pOp->iSrcLine = 0;
83469 Vdbe *p, /* Add the opcode to this VM */
83501 Vdbe *v = pParse->pVdbe;
83506 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
83507 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
83509 assert( pParse->db->mallocFailed );
83510 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
83513 pCtx->pOut = 0;
83514 pCtx->pFunc = (FuncDef*)pFunc;
83515 pCtx->pVdbe = 0;
83516 pCtx->isError = 0;
83517 pCtx->argc = nArg;
83518 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
83531 Vdbe *p, /* Add the opcode to this VM */
83551 if( pParse->addrExplain==0 ) return 0;
83552 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
83553 return pOp->p2;
83577 if( pParse->explain==2 )
83585 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
83587 v = pParse->pVdbe;
83588 iThis = v->nOp;
83589 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
83591 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
83593 pParse->addrExplain = iThis;
83603 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
83619 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
83620 sqlite3MayAbort(p->pParse);
83627 Vdbe *p, /* Add the opcode to this VM */
83635 if( p->db->mallocFailed==0 ){
83636 VdbeOp *pOp = &p->aOp[addr];
83637 pOp->p4type = P4_INT32;
83638 pOp->p4.i = p4;
83643 /* Insert the end of a co-routine
83649 ** co-routine has its own independent set of registers, because co-routines
83651 ** that could cause problems if two or more co-routines are using the same
83654 v->pParse->nTempReg = 0;
83655 v->pParse->nRangeReg = 0;
83667 ** always negative and P2 values are suppose to be non-negative.
83674 ** Parse.aLabel[x] Stores the address that the x-th label resolves
83676 ** labels stores -1, but that is not required.
83684 return --pParse->nLabel;
83693 int nNewSize = 10 - p->nLabel;
83694 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
83695 nNewSize*sizeof(p->aLabel[0]));
83696 if( p->aLabel==0 ){
83697 p->nLabelAlloc = 0;
83701 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
83703 p->nLabelAlloc = nNewSize;
83704 p->aLabel[j] = v->nOp;
83708 Parse *p = v->pParse;
83710 assert( v->eVdbeState==VDBE_INIT_STATE );
83711 assert( j<-p->nLabel );
83714 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83715 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
83718 if( p->nLabelAlloc + p->nLabel < 0 ){
83721 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
83722 p->aLabel[j] = v->nOp;
83738 for(i=1; ALWAYS(i<p->nOp); i++){
83739 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
83740 p->aOp[1].opcode = OP_Noop;
83750 ** in a Vdbe main program and each of the sub-programs (triggers) it may
83761 ** sqlite3DbFree(v->db, sIter.apSub);
83770 int iSub; /* 0 = main program, 1 = first sub-program etc. */
83773 Vdbe *v = p->v;
83778 if( p->iSub<=p->nSub ){
83780 if( p->iSub==0 ){
83781 aOp = v->aOp;
83782 nOp = v->nOp;
83784 aOp = p->apSub[p->iSub-1]->aOp;
83785 nOp = p->apSub[p->iSub-1]->nOp;
83787 assert( p->iAddr<nOp );
83789 pRet = &aOp[p->iAddr];
83790 p->iAddr++;
83791 if( p->iAddr==nOp ){
83792 p->iSub++;
83793 p->iAddr = 0;
83796 if( pRet->p4type==P4_SUBPROGRAM ){
83797 int nByte = (p->nSub+1)*sizeof(SubProgram*);
83799 for(j=0; j<p->nSub; j++){
83800 if( p->apSub[j]==pRet->p4.pProgram ) break;
83802 if( j==p->nSub ){
83803 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
83804 if( !p->apSub ){
83807 p->apSub[p->nSub++] = pRet->p4.pProgram;
83817 ** Check if the program stored in the VM associated with pParse may
83820 ** sub-programs contains any of the following:
83837 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
83853 int opcode = pOp->opcode;
83860 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
83865 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
83869 ** where a "DELETE FROM tbl" has a statement-journal but does not
83870 ** require one. This is not so bad - it is an inefficiency, not a bug. */
83871 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
83876 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
83881 sqlite3DbFree(v->db, sIter.apSub);
83888 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
83892 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
83901 || (pC->eCurType!=CURTYPE_SORTER
83902 && pC->eCurType!=CURTYPE_PSEUDO
83903 && !pC->isEphemeral)
83905 p->nWrite++;
83916 assert( p->nWrite==0 || p->usesStmtJournal );
83944 Parse *pParse = p->pParse;
83945 int *aLabel = pParse->aLabel;
83946 p->readOnly = 1;
83947 p->bIsReader = 0;
83948 pOp = &p->aOp[p->nOp-1];
83949 assert( p->aOp[0].opcode==OP_Init );
83957 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
83960 switch( pOp->opcode ){
83962 if( pOp->p2!=0 ) p->readOnly = 0;
83967 p->bIsReader = 1;
83975 p->readOnly = 0;
83976 p->bIsReader = 1;
83980 assert( pOp->p2>=0 );
83985 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
83990 assert( (pOp - p->aOp) >= 3 );
83991 assert( pOp[-1].opcode==OP_Integer );
83992 n = pOp[-1].p1;
83999 if( pOp->p2<0 ){
84001 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
84002 ** have non-negative values for P2. */
84003 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
84004 assert( ADDR(pOp->p2)<-pParse->nLabel );
84005 pOp->p2 = aLabel[ADDR(pOp->p2)];
84011 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
84012 ** have non-negative values for P2. */
84013 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
84015 assert( pOp>p->aOp );
84016 pOp--;
84020 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
84021 pParse->aLabel = 0;
84023 pParse->nLabel = 0;
84025 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
84050 Vdbe *v, /* The byte-code program under construction */
84060 pParse = v->pParse;
84062 if( pParse->nErr ) return;
84064 assert( iLast<v->nOp );
84065 pOp = &v->aOp[iFirst];
84067 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
84068 int iDest = pOp->p2; /* Jump destination */
84070 if( pOp->opcode==OP_Gosub ) continue;
84074 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
84077 iDest = pParse->aLabel[j];
84081 for(; j<v->nOp; j++){
84082 VdbeOp *pX = &v->aOp[j];
84083 if( pX->opcode==OP_Return ){
84084 if( pX->p1==iRetReg ) break;
84087 if( pX->opcode==OP_Noop ) continue;
84088 if( pX->opcode==OP_Explain ) continue;
84116 assert( p->eVdbeState==VDBE_INIT_STATE );
84117 return p->nOp;
84126 ** sqlite3VdbeAddOpList() will always be non-NULL.
84130 assert( p->nOp + N <= p->nOpAlloc );
84135 ** Verify that the VM passed as the only argument does not contain
84144 for(i=0; i<p->nOp; i++){
84145 assert( p->aOp[i].opcode!=OP_ResultRow );
84173 VdbeOp *aOp = p->aOp;
84174 assert( aOp && !p->db->mallocFailed );
84176 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
84177 assert( DbMaskAllZero(p->btreeMask) );
84180 *pnOp = p->nOp;
84181 p->aOp = 0;
84189 ** Non-zero P2 arguments to jump instructions are automatically adjusted
84196 int iLineno /* Source-file line number of first opcode */
84201 assert( p->eVdbeState==VDBE_INIT_STATE );
84202 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
84205 pFirst = pOut = &p->aOp[p->nOp];
84207 pOut->opcode = aOp->opcode;
84208 pOut->p1 = aOp->p1;
84209 pOut->p2 = aOp->p2;
84210 assert( aOp->p2>=0 );
84211 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
84212 pOut->p2 += p->nOp;
84214 pOut->p3 = aOp->p3;
84215 pOut->p4type = P4_NOTUSED;
84216 pOut->p4.p = 0;
84217 pOut->p5 = 0;
84219 pOut->zComment = 0;
84222 pOut->iSrcLine = iLineno+i;
84227 if( p->db->flags & SQLITE_VdbeAddopTrace ){
84228 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
84232 p->nOp += nOp;
84241 Vdbe *p, /* VM to add scanstatus() to */
84248 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
84250 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
84252 ScanStatus *pNew = &aNew[p->nScan++];
84253 pNew->addrExplain = addrExplain;
84254 pNew->addrLoop = addrLoop;
84255 pNew->addrVisit = addrVisit;
84256 pNew->nEst = nEst;
84257 pNew->zName = sqlite3DbStrDup(p->db, zName);
84258 p->aScan = aNew;
84270 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
84274 sqlite3VdbeGetOp(p,addr)->p1 = val;
84277 assert( addr>=0 || p->db->mallocFailed );
84278 sqlite3VdbeGetOp(p,addr)->p2 = val;
84282 sqlite3VdbeGetOp(p,addr)->p3 = val;
84285 assert( p->nOp>0 || p->db->mallocFailed );
84286 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
84296 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
84297 pOp->p5 |= OPFLAG_TYPEOFARG;
84306 sqlite3VdbeChangeP2(p, addr, p->nOp);
84312 ** the previous opcode (and is thus a no-op) then simply back up
84317 ** strives to omit useless byte-code like this:
84323 if( addr==p->nOp-1 ){
84324 assert( p->aOp[addr].opcode==OP_Once
84325 || p->aOp[addr].opcode==OP_If
84326 || p->aOp[addr].opcode==OP_FkIfZero );
84327 assert( p->aOp[addr].p4type==0 );
84329 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
84331 p->nOp--;
84333 sqlite3VdbeChangeP2(p, addr, p->nOp);
84344 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
84353 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
84358 freeEphemeralFunction(db, p->pFunc);
84376 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
84390 if( db->pnBytesFreed==0 ){
84398 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
84413 Op *pOp = &aOp[nOp-1];
84415 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
84417 sqlite3DbFree(db, pOp->zComment);
84420 pOp--;
84428 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
84429 ** objects when the VM is no longer required.
84432 p->pNext = pVdbe->pProgram;
84433 pVdbe->pProgram = p;
84440 return pVdbe->pProgram!=0;
84448 if( p->db->mallocFailed ) return 0;
84449 assert( addr>=0 && addr<p->nOp );
84450 pOp = &p->aOp[addr];
84451 freeP4(p->db, pOp->p4type, pOp->p4.p);
84452 pOp->p4type = P4_NOTUSED;
84453 pOp->p4.z = 0;
84454 pOp->opcode = OP_Noop;
84463 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
84464 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
84482 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
84483 assert( pParse->pVdbe );
84485 assert( iFirst+N-1<=pParse->nMem );
84490 N--;
84492 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
84493 mask &= ~MASKBIT32(N-1);
84494 N--;
84498 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
84499 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
84528 if( pOp->p4type ){
84529 freeP4(p->db, pOp->p4type, pOp->p4.p);
84530 pOp->p4type = 0;
84531 pOp->p4.p = 0;
84534 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
84537 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
84538 pOp->p4type = P4_DYNAMIC;
84545 db = p->db;
84546 assert( p->eVdbeState==VDBE_INIT_STATE );
84547 assert( p->aOp!=0 || db->mallocFailed );
84548 if( db->mallocFailed ){
84552 assert( p->nOp>0 );
84553 assert( addr<p->nOp );
84555 addr = p->nOp - 1;
84557 pOp = &p->aOp[addr];
84558 if( n>=0 || pOp->p4type ){
84565 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
84566 pOp->p4type = P4_INT32;
84569 pOp->p4.p = (void*)zP4;
84570 pOp->p4type = (signed char)n;
84577 ** to the value defined by the arguments. This is a high-speed
84588 if( p->db->mallocFailed ){
84589 freeP4(p->db, n, pP4);
84592 assert( p->nOp>0 );
84593 pOp = &p->aOp[p->nOp-1];
84594 assert( pOp->p4type==P4_NOTUSED );
84595 pOp->p4type = n;
84596 pOp->p4.p = pP4;
84605 Vdbe *v = pParse->pVdbe;
84616 ** insert a No-op and add the comment to that new instruction. This
84621 assert( p->nOp>0 || p->aOp==0 );
84622 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
84623 if( p->nOp ){
84624 assert( p->aOp );
84625 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
84626 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
84653 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
84658 ** Return the opcode for a given address. The address must be non-negative.
84674 assert( p->eVdbeState==VDBE_INIT_STATE );
84675 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
84676 if( p->db->mallocFailed ){
84679 return &p->aOp[addr];
84686 return sqlite3VdbeGetOp(p, p->nOp - 1);
84695 if( c=='1' ) return pOp->p1;
84696 if( c=='2' ) return pOp->p2;
84697 if( c=='3' ) return pOp->p3;
84698 if( c=='4' ) return pOp->p4.i;
84699 return pOp->p5;
84710 ** "PX" -> "r[X]"
84711 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
84712 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
84713 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
84716 sqlite3 *db, /* Optional - Oom error reporting only */
84728 zOpName = sqlite3OpcodeName(pOp->opcode);
84744 if( pOp->zComment && pOp->zComment[0] ){
84745 sqlite3_str_appendall(&x, pOp->zComment);
84762 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
84765 sqlite3_context *pCtx = pOp->p4.pCtx;
84766 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
84768 }else if( pCtx->argc>1 ){
84769 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
84772 x.nChar -= 2;
84778 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
84787 if( !seenCom && pOp->zComment ){
84788 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
84790 }else if( pOp->zComment ){
84791 sqlite3_str_appendall(&x, pOp->zComment);
84807 switch( pExpr->op ){
84810 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
84813 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
84819 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
84823 if( pExpr->iColumn<0 ){
84826 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
84864 displayP4Expr(p, pExpr->pLeft);
84865 if( pExpr->pRight ){
84867 displayP4Expr(p, pExpr->pRight);
84885 switch( pOp->p4type ){
84888 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
84889 assert( pKeyInfo->aSortFlags!=0 );
84890 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
84891 for(j=0; j<pKeyInfo->nKeyField; j++){
84892 CollSeq *pColl = pKeyInfo->aColl[j];
84893 const char *zColl = pColl ? pColl->zName : "";
84896 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
84897 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
84905 displayP4Expr(&x, pOp->p4.pExpr);
84911 CollSeq *pColl = pOp->p4.pColl;
84912 assert( pColl->enc<4 );
84913 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
84914 encnames[pColl->enc]);
84918 FuncDef *pDef = pOp->p4.pFunc;
84919 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84923 FuncDef *pDef = pOp->p4.pCtx->pFunc;
84924 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84928 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
84932 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
84936 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
84940 Mem *pMem = pOp->p4.pMem;
84941 if( pMem->flags & MEM_Str ){
84942 zP4 = pMem->z;
84943 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84944 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
84945 }else if( pMem->flags & MEM_Real ){
84946 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
84947 }else if( pMem->flags & MEM_Null ){
84950 assert( pMem->flags & MEM_Blob );
84957 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
84964 u32 *ai = pOp->p4.ai;
84978 zP4 = pOp->p4.pTab->zName;
84982 zP4 = pOp->p4.z;
84994 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
84998 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
84999 ** p->btreeMask of databases that will require a lock.
85002 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
85003 assert( i<(int)sizeof(p->btreeMask)*8 );
85004 DbMaskSet(p->btreeMask, i);
85005 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
85006 DbMaskSet(p->lockMask, i);
85012 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
85014 ** that may be accessed by the VM passed as an argument. In doing so it also
85016 ** that the correct busy-handler callback is invoked if required.
85018 ** If SQLite is not threadsafe but does support shared-cache mode, then
85021 ** associated with the VM.
85023 ** If SQLite is not threadsafe and does not support shared-cache mode, this
85024 ** function is a no-op.
85026 ** The p->btreeMask field is a bitmask of all btrees that the prepared
85027 ** statement p will ever use. Let N be the number of bits in p->btreeMask
85037 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
85038 db = p->db;
85039 aDb = db->aDb;
85040 nDb = db->nDb;
85042 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
85058 db = p->db;
85059 aDb = db->aDb;
85060 nDb = db->nDb;
85062 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
85068 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
85081 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
85095 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
85096 zP4 ? zP4 : "", pOp->p5,
85109 ** This is a high-runner, so only those fields that really do need to
85124 p->flags = flags;
85125 p->db = db;
85126 p->szMalloc = 0;
85128 p->pScopyFrom = 0;
85131 }while( (--N)>0 );
85146 sqlite3 *db = p->db;
85147 if( db->pnBytesFreed ){
85149 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
85163 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
85164 ** sqlite3MemRelease() were called from here. With -O2, this jumps
85169 testcase( p->flags & MEM_Agg );
85170 testcase( p->flags & MEM_Dyn );
85171 if( p->flags&(MEM_Agg|MEM_Dyn) ){
85172 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
85174 p->flags = MEM_Undefined;
85175 }else if( p->szMalloc ){
85176 sqlite3DbNNFreeNN(db, p->zMalloc);
85177 p->szMalloc = 0;
85178 p->flags = MEM_Undefined;
85182 p->flags = MEM_Undefined;
85197 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
85213 pFrame->pParent = pFrame->v->pDelFrame;
85214 pFrame->v->pDelFrame = pFrame;
85234 int nSub = 0; /* Number of sub-vdbes seen so far */
85235 SubProgram **apSub = 0; /* Array of sub-vdbes */
85246 ** encountered, but p->pc will eventually catch up to nRow.
85248 nRow = p->nOp;
85250 if( pSub->flags&MEM_Blob ){
85253 nSub = pSub->n/sizeof(Vdbe*);
85254 apSub = (SubProgram **)pSub->z;
85257 nRow += apSub[i]->nOp;
85264 p->rc = SQLITE_OK;
85268 if( i<p->nOp ){
85271 aOp = p->aOp;
85276 i -= p->nOp;
85279 for(j=0; i>=apSub[j]->nOp; j++){
85280 i -= apSub[j]->nOp;
85281 assert( i<apSub[j]->nOp || j+1<nSub );
85283 aOp = apSub[j]->aOp;
85288 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
85298 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
85299 if( p->rc!=SQLITE_OK ){
85303 apSub = (SubProgram **)pSub->z;
85306 pSub->n = nSub*sizeof(SubProgram*);
85307 nRow += aOp[i].p4.pProgram->nOp;
85314 if( pOp->opcode==OP_OpenRead ) break;
85315 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
85316 if( pOp->opcode==OP_ReopenIdx ) break;
85340 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
85342 for(i=0; i<p->nChildCsr; i++){
85343 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
85345 releaseMemArray(aMem, p->nChildMem);
85346 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
85347 sqlite3DbFree(p->v->db, p);
85358 ** When p->explain==1, each instruction is listed. When
85359 ** p->explain==2, only OP_Explain instructions are listed and these
85360 ** are shown in a different format. p->explain==2 is used to implement
85362 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
85366 ** When p->explain==1, first the main program is listed, then each of
85373 sqlite3 *db = p->db; /* The database connection */
85376 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
85377 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
85381 assert( p->explain );
85382 assert( p->eVdbeState==VDBE_RUN_STATE );
85383 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
85387 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
85390 p->pResultSet = 0;
85392 if( p->rc==SQLITE_NOMEM ){
85404 assert( p->nMem>9 );
85405 pSub = &p->aMem[9];
85411 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
85415 if( AtomicLoad(&db->u1.isInterrupted) ){
85416 p->rc = SQLITE_INTERRUPT;
85418 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
85421 if( p->explain==2 ){
85422 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
85423 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
85424 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
85425 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
85426 p->nResColumn = 4;
85429 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
85430 -1, SQLITE_UTF8, SQLITE_STATIC);
85431 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
85432 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
85433 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
85435 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
85439 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
85444 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
85445 p->nResColumn = 8;
85447 p->pResultSet = pMem;
85448 if( db->mallocFailed ){
85449 p->rc = SQLITE_NOMEM;
85452 p->rc = SQLITE_OK;
85467 if( p->zSql ){
85468 z = p->zSql;
85469 }else if( p->nOp>=1 ){
85470 const VdbeOp *pOp = &p->aOp[0];
85471 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
85472 z = pOp->p4.z;
85485 int nOp = p->nOp;
85489 pOp = &p->aOp[0];
85490 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
85493 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
85497 if( z[i-1]!=' ' ){
85520 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
85539 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
85542 if( nByte <= p->nFree ){
85543 p->nFree -= nByte;
85544 pBuf = &p->pSpace[p->nFree];
85546 p->nNeeded += nByte;
85562 assert( p->eVdbeState==VDBE_INIT_STATE
85563 || p->eVdbeState==VDBE_READY_STATE
85564 || p->eVdbeState==VDBE_HALT_STATE );
85568 assert( p->nOp>0 );
85570 p->eVdbeState = VDBE_READY_STATE;
85573 for(i=0; i<p->nMem; i++){
85574 assert( p->aMem[i].db==p->db );
85577 p->pc = -1;
85578 p->rc = SQLITE_OK;
85579 p->errorAction = OE_Abort;
85580 p->nChange = 0;
85581 p->cacheCtr = 1;
85582 p->minWriteFileFormat = 255;
85583 p->iStatement = 0;
85584 p->nFkConstraint = 0;
85586 for(i=0; i<p->nOp; i++){
85587 p->aOp[i].cnt = 0;
85588 p->aOp[i].cycles = 0;
85601 ** After this routine is called the VM has been "packaged" and is ready
85617 int nMem; /* Number of VM memory registers */
85624 assert( p->nOp>0 );
85626 assert( p->eVdbeState==VDBE_INIT_STATE );
85627 assert( pParse==p->pParse );
85628 p->pVList = pParse->pVList;
85629 pParse->pVList = 0;
85630 db = p->db;
85631 assert( db->mallocFailed==0 );
85632 nVar = pParse->nVar;
85633 nMem = pParse->nMem;
85634 nCursor = pParse->nTab;
85635 nArg = pParse->nMaxArg;
85649 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
85650 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
85652 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
85657 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
85658 if( pParse->explain ){
85665 p->explain = pParse->explain;
85666 if( pParse->explain==2 ){
85676 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
85680 p->expired = 0;
85688 ** This two-pass approach that reuses as much memory as possible from
85693 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
85694 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
85695 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
85696 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
85698 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
85701 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
85703 if( !db->mallocFailed ){
85704 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
85705 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
85706 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
85707 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
85709 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
85714 if( db->mallocFailed ){
85715 p->nVar = 0;
85716 p->nCursor = 0;
85717 p->nMem = 0;
85719 p->nCursor = nCursor;
85720 p->nVar = (ynVar)nVar;
85721 initMemArray(p->aVar, nVar, db, MEM_Null);
85722 p->nMem = nMem;
85723 initMemArray(p->aMem, nMem, db, MEM_Undefined);
85724 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
85726 memset(p->anExec, 0, p->nOp*sizeof(i64));
85740 switch( pCx->eCurType ){
85742 sqlite3VdbeSorterClose(p->db, pCx);
85746 assert( pCx->uc.pCursor!=0 );
85747 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
85752 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
85753 const sqlite3_module *pModule = pVCur->pVtab->pModule;
85754 assert( pVCur->pVtab->nRef>0 );
85755 pVCur->pVtab->nRef--;
85756 pModule->xClose(pVCur);
85768 for(i=0; i<p->nCursor; i++){
85769 VdbeCursor *pC = p->apCsr[i];
85772 p->apCsr[i] = 0;
85779 ** is used, for example, when a trigger sub-program is halted to restore
85783 Vdbe *v = pFrame->v;
85786 v->anExec = pFrame->anExec;
85788 v->aOp = pFrame->aOp;
85789 v->nOp = pFrame->nOp;
85790 v->aMem = pFrame->aMem;
85791 v->nMem = pFrame->nMem;
85792 v->apCsr = pFrame->apCsr;
85793 v->nCursor = pFrame->nCursor;
85794 v->db->lastRowid = pFrame->lastRowid;
85795 v->nChange = pFrame->nChange;
85796 v->db->nChange = pFrame->nDbChange;
85797 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
85798 v->pAuxData = pFrame->pAuxData;
85799 pFrame->pAuxData = 0;
85800 return pFrame->pc;
85806 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
85812 if( p->pFrame ){
85814 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
85816 p->pFrame = 0;
85817 p->nFrame = 0;
85819 assert( p->nFrame==0 );
85821 releaseMemArray(p->aMem, p->nMem);
85822 while( p->pDelFrame ){
85823 VdbeFrame *pDel = p->pDelFrame;
85824 p->pDelFrame = pDel->pParent;
85828 /* Delete any auxdata allocations made by the VM */
85829 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
85830 assert( p->pAuxData==0 );
85841 sqlite3 *db = p->db;
85843 if( p->nResColumn ){
85844 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
85845 sqlite3DbFree(db, p->aColName);
85848 p->nResColumn = (u16)nResColumn;
85849 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
85850 if( p->aColName==0 ) return;
85851 initMemArray(p->aColName, n, db, MEM_Null);
85873 assert( idx<p->nResColumn );
85875 if( p->db->mallocFailed ){
85879 assert( p->aColName!=0 );
85880 pColName = &(p->aColName[idx+var*p->nResColumn]);
85881 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
85882 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
85889 ** write-transaction spanning more than one database file, this routine
85890 ** takes care of the super-journal trickery.
85894 int nTrans = 0; /* Number of databases with an active write-transaction
85895 ** that are candidates for a two-phase commit using a
85896 ** super-journal */
85909 ** be done before determining whether a super-journal file is
85918 ** one database file has an open write transaction, a super-journal
85921 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85922 Btree *pBt = db->aDb[i].pBt;
85924 /* Whether or not a database might need a super-journal depends upon
85926 ** journal modes use a super-journal and which do not */
85939 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
85955 /* If there are any write-transactions at all, invoke the commit hook */
85956 if( needXcommit && db->xCommitCallback ){
85957 rc = db->xCommitCallback(db->pCommitArg);
85963 /* The simple case - no more than one database file (not counting the
85965 ** super-journal.
85969 ** that case we do not support atomic multi-file commits, so use the
85972 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
85975 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85976 Btree *pBt = db->aDb[i].pBt;
85987 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85988 Btree *pBt = db->aDb[i].pBt;
85998 /* The complex case - There is a multi-file write-transaction active.
85999 ** This requires a super-journal file to ensure the transaction is
86004 sqlite3_vfs *pVfs = db->pVfs;
86005 char *zSuper = 0; /* File-name for the super-journal */
86006 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
86013 /* Select a super-journal file name */
86031 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
86033 /* The antipenultimate character of the super-journal name must
86035 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
86040 /* Open the super-journal. */
86047 sqlite3DbFree(db, zSuper-4);
86052 ** super-journal file. If an error occurs at this point close
86053 ** and delete the super-journal file. All the individual journal files
86054 ** still have 'null' as the super-journal pointer, so they will roll
86057 for(i=0; i<db->nDb; i++){
86058 Btree *pBt = db->aDb[i].pBt;
86070 sqlite3DbFree(db, zSuper-4);
86076 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
86084 sqlite3DbFree(db, zSuper-4);
86089 ** sets the super-journal pointer in each individual journal. If
86090 ** an error occurs here, do not delete the super-journal file.
86094 ** super-journal file will be orphaned. But we cannot delete it,
86095 ** in case the super-journal file name was written into the journal
86098 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
86099 Btree *pBt = db->aDb[i].pBt;
86107 sqlite3DbFree(db, zSuper-4);
86111 /* Delete the super-journal file. This commits the transaction. After
86116 sqlite3DbFree(db, zSuper-4);
86131 for(i=0; i<db->nDb; i++){
86132 Btree *pBt = db->aDb[i].pBt;
86151 ** This is an internal self-check only - it is not an essential processing
86154 ** This is a no-op if NDEBUG is defined.
86162 p = db->pVdbe;
86166 if( p->readOnly==0 ) nWrite++;
86167 if( p->bIsReader ) nRead++;
86169 p = p->pVNext;
86171 assert( cnt==db->nVdbeActive );
86172 assert( nWrite==db->nVdbeWrite );
86173 assert( nRead==db->nVdbeRead );
86180 ** If the Vdbe passed as the first argument opened a statement-transaction,
86190 sqlite3 *const db = p->db;
86193 const int iSavepoint = p->iStatement-1;
86196 assert( db->nStatement>0 );
86197 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
86199 for(i=0; i<db->nDb; i++){
86201 Btree *pBt = db->aDb[i].pBt;
86214 db->nStatement--;
86215 p->iStatement = 0;
86230 db->nDeferredCons = p->nStmtDefCons;
86231 db->nDeferredImmCons = p->nStmtDefImmCons;
86236 if( p->db->nStatement && p->iStatement ){
86245 ** handle associated with the VM passed as an argument is about to be
86250 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
86255 sqlite3 *db = p->db;
86256 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
86257 || (!deferred && p->nFkConstraint>0)
86259 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
86260 p->errorAction = OE_Abort;
86262 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
86274 ** This routine is the only way to move the sqlite3eOpenState of a VM from
86276 ** call this on a VM that is in the SQLITE_STATE_HALT state.
86284 sqlite3 *db = p->db;
86302 assert( p->eVdbeState==VDBE_RUN_STATE );
86303 if( db->mallocFailed ){
86304 p->rc = SQLITE_NOMEM_BKPT;
86311 if( p->bIsReader ){
86312 int mrc; /* Primary error code from p->rc */
86320 if( p->rc ){
86321 mrc = p->rc & 0xff;
86330 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
86335 ** Even if the statement is read-only, it is important to perform
86337 ** occurred while writing to the journal, sub-journal or database
86342 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
86343 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
86351 db->autoCommit = 1;
86352 p->nChange = 0;
86358 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
86362 /* If the auto-commit flag is set and this is the only active writer
86363 ** VM, then we do either a commit or rollback of the current transaction.
86369 && db->autoCommit
86370 && db->nVdbeWrite==(p->readOnly==0)
86372 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
86375 if( NEVER(p->readOnly) ){
86380 }else if( db->flags & SQLITE_CorruptRdOnly ){
86382 db->flags &= ~SQLITE_CorruptRdOnly;
86384 /* The auto-commit flag is true, the vdbe program was successful
86390 if( rc==SQLITE_BUSY && p->readOnly ){
86394 p->rc = rc;
86396 p->nChange = 0;
86398 db->nDeferredCons = 0;
86399 db->nDeferredImmCons = 0;
86400 db->flags &= ~(u64)SQLITE_DeferFKs;
86405 p->nChange = 0;
86407 db->nStatement = 0;
86409 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
86411 }else if( p->errorAction==OE_Abort ){
86416 db->autoCommit = 1;
86417 p->nChange = 0;
86421 /* If eStatementOp is non-zero, then a statement transaction needs to
86430 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
86431 p->rc = rc;
86432 sqlite3DbFree(db, p->zErrMsg);
86433 p->zErrMsg = 0;
86437 db->autoCommit = 1;
86438 p->nChange = 0;
86443 ** has been rolled back, update the database connection change-counter.
86445 if( p->changeCntOn ){
86447 sqlite3VdbeSetChanges(db, p->nChange);
86451 p->nChange = 0;
86458 /* We have successfully halted and closed the VM. Record this fact. */
86459 db->nVdbeActive--;
86460 if( !p->readOnly ) db->nVdbeWrite--;
86461 if( p->bIsReader ) db->nVdbeRead--;
86462 assert( db->nVdbeActive>=db->nVdbeRead );
86463 assert( db->nVdbeRead>=db->nVdbeWrite );
86464 assert( db->nVdbeWrite>=0 );
86465 p->eVdbeState = VDBE_HALT_STATE;
86467 if( db->mallocFailed ){
86468 p->rc = SQLITE_NOMEM_BKPT;
86471 /* If the auto-commit flag is set to true, then any locks that were held
86473 ** to invoke any required unlock-notify callbacks.
86475 if( db->autoCommit ){
86479 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
86480 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
86486 ** in p->rc. This routine sets that result back to SQLITE_OK.
86489 p->rc = SQLITE_OK;
86501 sqlite3 *db = p->db;
86502 int rc = p->rc;
86503 if( p->zErrMsg ){
86504 db->bBenignMalloc++;
86506 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
86507 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
86509 db->bBenignMalloc--;
86510 }else if( db->pErr ){
86511 sqlite3ValueSetNull(db->pErr);
86513 db->errCode = rc;
86514 db->errByteOffset = -1;
86520 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
86524 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
86525 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
86526 assert( v->db->init.busy==0 );
86529 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
86531 sqlite3DbFree(v->db, zExpanded);
86556 db = p->db;
86558 /* If the VM did not run to completion or if it encountered an
86562 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
86569 if( p->pc>=0 ){
86571 if( db->pErr || p->zErrMsg ){
86574 db->errCode = p->rc;
86583 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
86584 if( p->aMem ){
86585 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
86588 if( p->zErrMsg ){
86589 sqlite3DbFree(db, p->zErrMsg);
86590 p->zErrMsg = 0;
86592 p->pResultSet = 0;
86594 p->nWrite = 0;
86603 fprintf(out, "---- ");
86604 for(i=0; i<p->nOp; i++){
86605 fprintf(out, "%02x", p->aOp[i].opcode);
86608 if( p->zSql ){
86610 fprintf(out, "-- ");
86611 for(i=0; (c = p->zSql[i])!=0; i++){
86612 if( pc=='\n' ) fprintf(out, "-- ");
86618 for(i=0; i<p->nOp; i++){
86621 p->aOp[i].cnt,
86622 p->aOp[i].cycles,
86623 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
86626 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
86632 return p->rc & db->errMask;
86644 if( p->eVdbeState>=VDBE_READY_STATE ){
86646 assert( (rc & p->db->errMask)==rc );
86654 ** all auxiliary data pointers currently cached by the VM passed as
86660 ** VM pVdbe, and only then if:
86672 || (pAux->iAuxOp==iOp
86673 && pAux->iAuxArg>=0
86674 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
86676 testcase( pAux->iAuxArg==31 );
86677 if( pAux->xDeleteAux ){
86678 pAux->xDeleteAux(pAux->pAux);
86680 *pp = pAux->pNextAux;
86683 pp= &pAux->pNextAux;
86699 assert( p->db==0 || p->db==db );
86700 if( p->aColName ){
86701 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
86702 sqlite3DbNNFreeNN(db, p->aColName);
86704 for(pSub=p->pProgram; pSub; pSub=pNext){
86705 pNext = pSub->pNext;
86706 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
86709 if( p->eVdbeState!=VDBE_INIT_STATE ){
86710 releaseMemArray(p->aVar, p->nVar);
86711 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
86712 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
86714 vdbeFreeOpArray(db, p->aOp, p->nOp);
86715 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
86717 sqlite3DbFree(db, p->zNormSql);
86720 for(pThis=p->pDblStr; pThis; pThis=pNext){
86721 pNext = pThis->pNextStr;
86729 for(i=0; i<p->nScan; i++){
86730 sqlite3DbFree(db, p->aScan[i].zName);
86732 sqlite3DbFree(db, p->aScan);
86744 db = p->db;
86746 assert( sqlite3_mutex_held(db->mutex) );
86748 if( db->pnBytesFreed==0 ){
86749 assert( p->ppVPrev!=0 );
86750 *p->ppVPrev = p->pVNext;
86751 if( p->pVNext ){
86752 p->pVNext->ppVPrev = p->ppVPrev;
86768 assert( p->deferredMoveto );
86769 assert( p->isTable );
86770 assert( p->eCurType==CURTYPE_BTREE );
86771 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
86777 p->deferredMoveto = 0;
86778 p->cacheStatus = CACHE_STALE;
86791 assert( p->eCurType==CURTYPE_BTREE );
86792 assert( p->uc.pCursor!=0 );
86793 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
86794 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
86795 p->cacheStatus = CACHE_STALE;
86796 if( isDifferentRow ) p->nullRow = 1;
86805 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
86806 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
86818 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
86823 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
86830 ** serial-type and data blob separately.
86835 ** -------------- --------------- ---------------
86847 ** N>=12 and even (N-12)/2 BLOB
86848 ** N>=13 and odd (N-13)/2 text
86856 ** Return the serial-type for the value stored in pMem.
86860 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
86861 ** opcode in the byte-code engine. But by moving this routine in-line, we
86867 int flags = pMem->flags;
86877 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
86878 i64 i = pMem->u.i;
86903 ** as an integer, then we might as well make it an 8-byte floating
86905 pMem->u.r = (double)pMem->u.i;
86906 pMem->flags &= ~MEM_IntReal;
86907 pMem->flags |= MEM_Real;
86916 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
86917 assert( pMem->n>=0 );
86918 n = (u32)pMem->n;
86920 n += pMem->u.nZero;
86948 ** Return the length of the data corresponding to the supplied serial-type.
86952 return (serial_type-12)/2;
86955 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
86965 ** If we are on an architecture with mixed-endian floating
86969 ** For most architectures, this is a no-op.
86971 ** (later): It is reported to me that the mixed-endian problem
86973 ** that early versions of GCC stored the two words of a 64-bit
86981 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
86985 ** (2007-08-30) Frank van Vugt has studied this problem closely
86988 ** emulation that uses only 32-bit mantissas instead of a full
86989 ** 48-bits as required by the IEEE standard. (This is the
86992 ** the necessary byte swapping is carried out using a 64-bit integer
86993 ** rather than a 64-bit float. Frank assures us that the code here
87016 ** big-endian integer. Return the equivalent native integer
87042 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
87043 ** twos-complement integer. */
87044 pMem->u.i = *(i64*)&x;
87045 pMem->flags = MEM_Int;
87046 testcase( pMem->u.i<0 );
87048 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
87053 ** defined that 64-bit floating point values really are mixed
87062 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
87064 memcpy(&pMem->u.r, &x, sizeof(x));
87065 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
87075 ** UPDATE no-change flag set */
87076 pMem->flags = MEM_Null|MEM_Zero;
87077 pMem->n = 0;
87078 pMem->u.nZero = 0;
87083 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
87084 pMem->flags = MEM_Null;
87088 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
87090 pMem->u.i = ONE_BYTE_INT(buf);
87091 pMem->flags = MEM_Int;
87092 testcase( pMem->u.i<0 );
87095 case 2: { /* 2-byte signed integer */
87096 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
87097 ** twos-complement integer. */
87098 pMem->u.i = TWO_BYTE_INT(buf);
87099 pMem->flags = MEM_Int;
87100 testcase( pMem->u.i<0 );
87103 case 3: { /* 3-byte signed integer */
87104 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
87105 ** twos-complement integer. */
87106 pMem->u.i = THREE_BYTE_INT(buf);
87107 pMem->flags = MEM_Int;
87108 testcase( pMem->u.i<0 );
87111 case 4: { /* 4-byte signed integer */
87112 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
87113 ** twos-complement integer. */
87114 pMem->u.i = FOUR_BYTE_INT(buf);
87116 /* Work around a sign-extension bug in the HP compiler for HP/UX */
87117 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
87119 pMem->flags = MEM_Int;
87120 testcase( pMem->u.i<0 );
87123 case 5: { /* 6-byte signed integer */
87124 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
87125 ** twos-complement integer. */
87126 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
87127 pMem->flags = MEM_Int;
87128 testcase( pMem->u.i<0 );
87131 case 6: /* 8-byte signed integer */
87140 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
87141 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
87142 pMem->u.i = serial_type-8;
87143 pMem->flags = MEM_Int;
87147 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
87149 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
87150 ** (N-13)/2 bytes in length. */
87152 pMem->z = (char *)buf;
87153 pMem->n = (serial_type-12)/2;
87154 pMem->flags = aFlag[serial_type&1];
87179 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
87180 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
87182 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
87183 assert( pKeyInfo->aSortFlags!=0 );
87184 p->pKeyInfo = pKeyInfo;
87185 p->nField = pKeyInfo->nKeyField + 1;
87190 ** Given the nKey-byte encoding of a record in pKey[], populate the
87205 Mem *pMem = p->aMem;
87207 p->default_rc = 0;
87216 pMem->enc = pKeyInfo->enc;
87217 pMem->db = pKeyInfo->db;
87218 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
87219 pMem->szMalloc = 0;
87220 pMem->z = 0;
87224 if( (++u)>=p->nField ) break;
87231 sqlite3VdbeMemSetNull(pMem-1);
87233 assert( u<=pKeyInfo->nKeyField + 1 );
87234 p->nField = u;
87263 pKeyInfo = pPKey2->pKeyInfo;
87264 if( pKeyInfo->db==0 ) return 1;
87265 mem1.enc = pKeyInfo->enc;
87266 mem1.db = pKeyInfo->db;
87282 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
87283 assert( pKeyInfo->aSortFlags!=0 );
87284 assert( pKeyInfo->nKeyField>0 );
87311 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
87312 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
87315 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
87316 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
87318 rc = -rc;
87320 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
87321 rc = -rc; /* Invert the result for DESC sort order. */
87326 }while( idx1<szHdr1 && i<pPKey2->nField );
87337 rc = pPKey2->default_rc;
87344 if( pKeyInfo->db->mallocFailed ) return 1;
87353 ** limit given by pKeyInfo->nAllField.
87355 ** If this constraint is not satisfied, it means that the high-speed
87379 assert( nField <= pKeyInfo->nAllField );
87389 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
87397 if( pMem1->enc==pColl->enc ){
87400 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
87406 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
87407 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
87410 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
87411 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
87416 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
87426 ** with MEM_Zero. Return true if it could be a zero-blob.
87443 int n1 = pB1->n;
87444 int n2 = pB2->n;
87446 /* It is possible to have a Blob value that has some non-zero content
87450 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
87451 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
87453 if( (pB1->flags|pB2->flags) & MEM_Zero ){
87454 if( pB1->flags & pB2->flags & MEM_Zero ){
87455 return pB1->u.nZero - pB2->u.nZero;
87456 }else if( pB1->flags & MEM_Zero ){
87457 if( !isAllZero(pB2->z, pB2->n) ) return -1;
87458 return pB1->u.nZero - n2;
87460 if( !isAllZero(pB1->z, pB1->n) ) return +1;
87461 return n1 - pB2->u.nZero;
87464 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
87466 return n1 - n2;
87470 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
87480 if( x<r ) return -1;
87486 if( r<-9223372036854775808.0 ) return +1;
87487 if( r>=9223372036854775808.0 ) return -1;
87489 if( i<y ) return -1;
87492 if( s<r ) return -1;
87511 f1 = pMem1->flags;
87512 f2 = pMem2->flags;
87520 return (f2&MEM_Null) - (f1&MEM_Null);
87532 if( pMem1->u.i < pMem2->u.i ) return -1;
87533 if( pMem1->u.i > pMem2->u.i ) return +1;
87537 if( pMem1->u.r < pMem2->u.r ) return -1;
87538 if( pMem1->u.r > pMem2->u.r ) return +1;
87545 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
87547 if( pMem1->u.i < pMem2->u.i ) return -1;
87548 if( pMem1->u.i > pMem2->u.i ) return +1;
87551 return -1;
87558 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
87560 return -1;
87574 return -1;
87577 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
87578 assert( pMem1->enc==SQLITE_UTF8 ||
87579 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
87585 assert( !pColl || pColl->xCmp );
87600 ** The first argument passed to this function is a serial-type that
87601 ** corresponds to an integer - all values between 1 and 9 inclusive
87637 return (serial_type - 8);
87649 ** If argument bSkip is non-zero, it is assumed that the caller has already
87653 ** fields that appear in both keys are equal, then pPKey2->default_rc is
87656 ** If database corruption is discovered, set pPKey2->errCode to
87658 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
87659 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
87671 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
87700 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87705 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
87707 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
87708 assert( pPKey2->pKeyInfo->nKeyField>0 );
87710 while( 1 /*exit-by-break*/ ){
87714 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
87715 testcase( pRhs->flags & MEM_Int );
87716 testcase( pRhs->flags & MEM_IntReal );
87720 rc = serial_type==10 ? -1 : +1;
87722 rc = -1;
87725 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
87728 i64 rhs = pRhs->u.i;
87730 rc = -1;
87738 else if( pRhs->flags & MEM_Real ){
87745 rc = serial_type==10 ? -1 : +1;
87747 rc = -1;
87751 if( mem1.u.r<pRhs->u.r ){
87752 rc = -1;
87753 }else if( mem1.u.r>pRhs->u.r ){
87757 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
87763 else if( pRhs->flags & MEM_Str ){
87767 rc = -1;
87771 mem1.n = (serial_type - 12) / 2;
87775 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
87777 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87779 }else if( pKeyInfo->aColl[i] ){
87780 mem1.enc = pKeyInfo->enc;
87781 mem1.db = pKeyInfo->db;
87785 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
87788 int nCmp = MIN(mem1.n, pRhs->n);
87789 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87790 if( rc==0 ) rc = mem1.n - pRhs->n;
87796 else if( pRhs->flags & MEM_Blob ){
87797 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
87801 rc = -1;
87803 int nStr = (serial_type - 12) / 2;
87807 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87809 }else if( pRhs->flags & MEM_Zero ){
87813 rc = nStr - pRhs->u.nZero;
87816 int nCmp = MIN(nStr, pRhs->n);
87817 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87818 if( rc==0 ) rc = nStr - pRhs->n;
87830 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
87834 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
87836 rc = -rc;
87845 if( i==pPKey2->nField ) break;
87851 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87865 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
87866 || pPKey2->pKeyInfo->db->mallocFailed
87868 pPKey2->eqSeen = 1;
87869 return pPKey2->default_rc;
87882 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
87900 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
87903 case 1: { /* 1-byte signed integer */
87908 case 2: { /* 2-byte signed integer */
87913 case 3: { /* 3-byte signed integer */
87918 case 4: { /* 4-byte signed integer */
87924 case 5: { /* 6-byte signed integer */
87929 case 6: { /* 8-byte signed integer */
87956 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
87957 v = pPKey2->u.i;
87959 res = pPKey2->r1;
87961 res = pPKey2->r2;
87962 }else if( pPKey2->nField>1 ){
87968 ** fields. Return pPKey2->default_rc in this case. */
87969 res = pPKey2->default_rc;
87970 pPKey2->eqSeen = 1;
87980 ** uses the collation sequence BINARY and (c) that the size-of-header varint
87991 assert( pPKey2->aMem[0].flags & MEM_Str );
87992 assert( pPKey2->aMem[0].n == pPKey2->n );
87993 assert( pPKey2->aMem[0].z == pPKey2->u.z );
87994 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
88004 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
88006 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
88012 nStr = (serial_type-12) / 2;
88014 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
88017 nCmp = MIN( pPKey2->n, nStr );
88018 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
88021 res = pPKey2->r2;
88023 res = pPKey2->r1;
88025 res = nStr - pPKey2->n;
88027 if( pPKey2->nField>1 ){
88030 res = pPKey2->default_rc;
88031 pPKey2->eqSeen = 1;
88034 res = pPKey2->r2;
88036 res = pPKey2->r1;
88043 || pPKey2->pKeyInfo->db->mallocFailed
88055 ** that the size-of-header varint that occurs at the start of each record
88067 if( p->pKeyInfo->nAllField<=13 ){
88068 int flags = p->aMem[0].flags;
88069 if( p->pKeyInfo->aSortFlags[0] ){
88070 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
88073 p->r1 = 1;
88074 p->r2 = -1;
88076 p->r1 = -1;
88077 p->r2 = 1;
88080 p->u.i = p->aMem[0].u.i;
88087 && p->pKeyInfo->aColl[0]==0
88090 p->u.z = p->aMem[0].z;
88091 p->n = p->aMem[0].n;
88116 ** than 2GiB are support - anything large must be database corruption.
88118 ** this code can safely assume that nCellKey is 32-bits
88141 /* The last field of the index should be an integer - the ROWID.
88143 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
88162 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
88197 assert( pC->eCurType==CURTYPE_BTREE );
88198 pCur = pC->uc.pCursor;
88222 assert( sqlite3_mutex_held(db->mutex) );
88223 db->nChange = nChange;
88224 db->nTotalChange += nChange;
88232 v->changeCntOn = 1;
88241 ** programs obsolete. Removing user-defined functions or collating
88255 for(p = db->pVdbe; p; p=p->pVNext){
88256 p->expired = iCode+1;
88264 return v->db;
88271 return v->prepFlags;
88276 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
88285 Mem *pMem = &v->aVar[iVar-1];
88286 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
88287 if( 0==(pMem->flags & MEM_Null) ){
88288 sqlite3_value *pRet = sqlite3ValueNew(v->db);
88301 ** to sqlite3_reoptimize() that re-preparing the statement may result
88306 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
88308 v->expmask |= 0x80000000;
88310 v->expmask |= ((u32)1 << (iVar-1));
88319 ** throw an error if it is given inputs that would make it non-deterministic.
88320 ** This routine is invoked by date/time functions that use non-deterministic
88326 if( pCtx->pVdbe==0 ) return 1;
88328 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
88329 if( pOp->opcode==OP_PureFunc ){
88332 if( pOp->p5 & NC_IsCheck ){
88334 }else if( pOp->p5 & NC_GenCol ){
88339 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
88340 pCtx->pFunc->zName, zContext);
88341 sqlite3_result_error(pCtx, zMsg, -1);
88355 if( pVtab->zErrMsg ){
88356 sqlite3 *db = p->db;
88357 sqlite3DbFree(db, p->zErrMsg);
88358 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
88359 sqlite3_free(pVtab->zErrMsg);
88360 pVtab->zErrMsg = 0;
88369 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
88380 Mem *pMem = &p->aMem[i];
88381 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
88390 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
88396 Vdbe *v, /* Vdbe pre-update hook is invoked by */
88405 sqlite3 *db = v->db;
88408 const char *zTbl = pTab->zName;
88411 assert( db->pPreUpdate==0 );
88418 iKey2 = v->aMem[iReg].u.i;
88425 assert( pCsr->eCurType==CURTYPE_BTREE );
88426 assert( pCsr->nField==pTab->nCol
88427 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
88436 preupdate.keyinfo.nKeyField = pTab->nCol;
88443 db->pPreUpdate = &preupdate;
88444 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
88445 db->pPreUpdate = 0;
88451 for(i=0; i<pCsr->nField; i++){
88481 ** Return TRUE (non-zero) of the statement supplied as an argument needs
88490 return p==0 || p->expired;
88500 if( p->db==0 ){
88524 assert( p->startTime>0 );
88525 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
88526 assert( db->init.busy==0 );
88527 assert( p->zSql!=0 );
88528 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
88529 iElapse = (iNow - p->startTime)*1000000;
88531 if( db->xProfile ){
88532 db->xProfile(db->pProfileArg, p->zSql, iElapse);
88535 if( db->mTrace & SQLITE_TRACE_PROFILE ){
88536 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
88538 p->startTime = 0;
88545 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
88547 # define checkProfileCallback(DB,P) /*no-op*/
88562 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
88563 ** pointer is a harmless no-op. */
88567 sqlite3 *db = v->db;
88569 sqlite3_mutex_enter(db->mutex);
88571 assert( v->eVdbeState>=VDBE_READY_STATE );
88594 sqlite3 *db = v->db;
88595 sqlite3_mutex_enter(db->mutex);
88599 assert( (rc & (db->errMask))==rc );
88601 sqlite3_mutex_leave(db->mutex);
88614 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
88617 for(i=0; i<p->nVar; i++){
88618 sqlite3VdbeMemRelease(&p->aVar[i]);
88619 p->aVar[i].flags = MEM_Null;
88621 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
88622 if( p->expmask ){
88623 p->expired = 1;
88636 if( p->flags & (MEM_Blob|MEM_Str) ){
88638 assert( p->flags==MEM_Null && p->z==0 );
88641 p->flags |= MEM_Blob;
88642 return p->n ? p->z : 0;
88664 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
88668 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
88671 && p->eSubtype=='p'
88672 && strcmp(p->u.zPType, zPType)==0
88674 return (void*)p->z;
88693 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
88694 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
88767 if( pVal->flags & MEM_Null ){
88769 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
88771 }else if( pVal->flags & MEM_Int ){
88773 }else if( pVal->flags & MEM_Str ){
88776 assert( eType == aType[pVal->flags&MEM_AffMask] );
88779 return aType[pVal->flags&MEM_AffMask];
88782 return pVal->enc;
88787 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
88792 return (pVal->flags&MEM_FromBind)!=0;
88804 pNew->flags &= ~MEM_Dyn;
88805 pNew->db = 0;
88806 if( pNew->flags&(MEM_Str|MEM_Blob) ){
88807 pNew->flags &= ~(MEM_Static|MEM_Dyn);
88808 pNew->flags |= MEM_Ephem;
88813 }else if( pNew->flags & MEM_Null ){
88815 pNew->flags &= ~(MEM_Term|MEM_Subtype);
88829 ** The following routines are used by user-defined functions to specify
88846 Mem *pOut = pCtx->pOut;
88859 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
88887 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88896 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88905 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88906 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
88909 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88910 pCtx->isError = SQLITE_ERROR;
88911 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
88915 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88916 pCtx->isError = SQLITE_ERROR;
88917 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
88921 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88922 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
88925 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88926 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
88929 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88930 sqlite3VdbeMemSetNull(pCtx->pOut);
88938 Mem *pOut = pCtx->pOut;
88939 assert( sqlite3_mutex_held(pOut->db->mutex) );
88941 pOut->flags = MEM_Null;
88945 Mem *pOut = pCtx->pOut;
88946 assert( sqlite3_mutex_held(pOut->db->mutex) );
88947 pOut->eSubtype = eSubtype & 0xff;
88948 pOut->flags |= MEM_Subtype;
88956 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88966 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88982 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88991 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89000 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89005 Mem *pOut = pCtx->pOut;
89006 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89008 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
89017 Mem *pOut = pCtx->pOut;
89018 assert( sqlite3_mutex_held(pOut->db->mutex) );
89019 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
89024 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
89027 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
89031 pCtx->isError = errCode ? errCode : -1;
89033 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
89035 if( pCtx->pOut->flags & MEM_Null ){
89036 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
89043 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89044 pCtx->isError = SQLITE_TOOBIG;
89045 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
89051 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89052 sqlite3VdbeMemSetNull(pCtx->pOut);
89053 pCtx->isError = SQLITE_NOMEM_BKPT;
89054 sqlite3OomFault(pCtx->pOut->db);
89060 ** test-control.
89063 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89064 if( pCtx->pOut->flags & MEM_Int ){
89065 pCtx->pOut->flags &= ~MEM_Int;
89066 pCtx->pOut->flags |= MEM_IntReal;
89080 for(i=0; i<db->nDb; i++){
89081 Btree *pBt = db->aDb[i].pBt;
89087 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
89088 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
89111 db = p->db;
89112 if( p->eVdbeState!=VDBE_RUN_STATE ){
89114 if( p->eVdbeState==VDBE_READY_STATE ){
89115 if( p->expired ){
89116 p->rc = SQLITE_SCHEMA;
89118 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
89120 ** error has occurred, then return the error code in p->rc to the
89133 if( db->nVdbeActive==0 ){
89134 AtomicStore(&db->u1.isInterrupted, 0);
89137 assert( db->nVdbeWrite>0 || db->autoCommit==0
89138 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
89142 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
89143 && !db->init.busy && p->zSql ){
89144 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
89146 assert( p->startTime==0 );
89150 db->nVdbeActive++;
89151 if( p->readOnly==0 ) db->nVdbeWrite++;
89152 if( p->bIsReader ) db->nVdbeRead++;
89153 p->pc = 0;
89154 p->eVdbeState = VDBE_RUN_STATE;
89157 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
89162 ** This "automatic-reset" change is not technically an incompatibility,
89168 ** returns, and those were broken by the automatic-reset change. As a
89169 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
89175 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
89183 assert( p->eVdbeState==VDBE_READY_STATE );
89189 p->rcApp = SQLITE_OK;
89192 if( p->explain ){
89197 db->nVdbeExec++;
89199 db->nVdbeExec--;
89203 assert( p->rc==SQLITE_OK );
89204 assert( db->mallocFailed==0 );
89205 db->errCode = SQLITE_ROW;
89213 if( rc==SQLITE_DONE && db->autoCommit ){
89214 assert( p->rc==SQLITE_OK );
89215 p->rc = doWalCallbacks(db);
89216 if( p->rc!=SQLITE_OK ){
89219 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
89221 ** error has occurred, then return the error code in p->rc to the
89228 db->errCode = rc;
89229 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
89230 p->rc = SQLITE_NOMEM_BKPT;
89231 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
89236 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
89240 return (rc&db->errMask);
89245 sqlite3_mutex_enter(db->mutex);
89246 db->xDropTableHandle = xFunc;
89247 sqlite3_mutex_leave(db->mutex);
89253 ** This is the top-level implementation of sqlite3_step(). Call
89266 db = v->db;
89267 sqlite3_mutex_enter(db->mutex);
89270 int savedPc = v->pc;
89272 Sqlite3SharedBlockMethods *pSharedBlock = v->pSharedBlock;
89273 int totalRows = v->totalRows;
89274 int blockFull = v->blockFull;
89275 int startPos = v->startPos;
89276 int addedRows = v->addedRows;
89288 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
89289 sqlite3DbFree(db, v->zErrMsg);
89290 if( !db->mallocFailed ){
89291 v->zErrMsg = sqlite3DbStrDup(db, zErr);
89292 v->rc = rc = sqlite3ApiExit(db, rc);
89294 v->zErrMsg = 0;
89295 v->rc = rc = SQLITE_NOMEM_BKPT;
89301 v->pSharedBlock = pSharedBlock;
89302 v->totalRows = totalRows;
89303 v->blockFull = blockFull;
89304 v->startPos = startPos;
89305 v->addedRows = addedRows;
89313 ** SQLITE_SCHEMA. tag-20220401a */
89314 v->minWriteFileFormat = 254;
89316 assert( v->expired==0 );
89319 if( rc==SQLITE_DONE && db->xDropTableHandle!=NULL && db->isDropTable==1 ){
89320 db->isDropTable = 0;
89321 db->xDropTableHandle(db, db->mDropTableName, db->mDropSchemaName);
89323 if( db->mDropTableName!=NULL ){
89324 sqlite3_free(db->mDropTableName);
89325 db->mDropTableName = NULL;
89327 if( db->mDropSchemaName!=NULL ){
89328 sqlite3_free(db->mDropSchemaName);
89329 db->mDropSchemaName = NULL;
89332 sqlite3_mutex_leave(db->mutex);
89342 assert( p && p->pFunc );
89343 return p->pFunc->pUserData;
89350 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
89357 assert( p && p->pOut );
89358 return p->pOut->db;
89372 ** performance by substituting a NULL result, or some other light-weight
89377 return sqlite3_value_nochange(p->pOut);
89397 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
89400 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
89401 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
89402 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
89408 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
89409 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
89413 sqlite3_value *pOut = pRhs->pOut;
89416 pOut->enc = ENC(pOut->db);
89417 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
89454 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
89455 assert( p->pVdbe!=0 );
89458 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
89461 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
89469 ** its pMem->z element.
89472 Mem *pMem = p->pMem;
89473 assert( (pMem->flags & MEM_Agg)==0 );
89476 pMem->z = 0;
89479 pMem->flags = MEM_Agg;
89480 pMem->u.pDef = p->pFunc;
89481 if( pMem->z ){
89482 memset(pMem->z, 0, nByte);
89485 return (void*)pMem->z;
89494 assert( p && p->pFunc && p->pFunc->xFinalize );
89495 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
89497 if( (p->pMem->flags & MEM_Agg)==0 ){
89500 return (void*)p->pMem->z;
89506 ** the user-function defined by pCtx.
89508 ** The left-most argument is 0.
89517 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89519 if( pCtx->pVdbe==0 ) return 0;
89521 assert( pCtx->pVdbe!=0 );
89523 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
89524 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
89525 return pAuxData->pAux;
89533 ** argument to the user-function defined by pCtx. Any previous value is
89536 ** The left-most argument is 0.
89549 Vdbe *pVdbe = pCtx->pVdbe;
89551 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
89558 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
89559 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
89564 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
89566 pAuxData->iAuxOp = pCtx->iOp;
89567 pAuxData->iAuxArg = iArg;
89568 pAuxData->pNextAux = pVdbe->pAuxData;
89569 pVdbe->pAuxData = pAuxData;
89570 if( pCtx->isError==0 ) pCtx->isError = -1;
89571 }else if( pAuxData->xDeleteAux ){
89572 pAuxData->xDeleteAux(pAuxData->pAux);
89575 pAuxData->pAux = pAux;
89576 pAuxData->xDeleteAux = xDelete;
89596 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
89597 return p->pMem->n;
89606 return pVm ? pVm->nResColumn : 0;
89615 if( pVm==0 || pVm->pResultSet==0 ) return 0;
89616 return pVm->nResColumn;
89625 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
89626 ** instead of an 8-byte one. This all works fine, except that when
89628 ** that a Mem structure is located on an 8-byte boundary. To prevent
89630 ** using gcc, we force nullMem to be 8-byte aligned using the magical
89668 assert( pVm->db );
89669 sqlite3_mutex_enter(pVm->db->mutex);
89670 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
89671 pOut = &pVm->pResultSet[i];
89673 sqlite3Error(pVm->db, SQLITE_RANGE);
89706 assert( p->db!=0 );
89707 assert( sqlite3_mutex_held(p->db->mutex) );
89708 p->rc = sqlite3ApiExit(p->db, p->rc);
89709 sqlite3_mutex_leave(p->db->mutex);
89759 if( pOut->flags&MEM_Static ){
89760 pOut->flags &= ~MEM_Static;
89761 pOut->flags |= MEM_Ephem;
89780 ** Convert the N-th element of pStmt->pColName[] into a string using
89813 db = p->db;
89818 sqlite3_mutex_enter(db->mutex);
89819 assert( db->mallocFailed==0 );
89822 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
89826 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
89831 if( db->mallocFailed ){
89835 sqlite3_mutex_leave(db->mutex);
89934 ** The error code stored in database p->db is overwritten with the return
89942 sqlite3_mutex_enter(p->db->mutex);
89943 if( p->eVdbeState!=VDBE_READY_STATE ){
89944 sqlite3Error(p->db, SQLITE_MISUSE);
89945 sqlite3_mutex_leave(p->db->mutex);
89947 "bind on a busy prepared statement: [%s]", p->zSql);
89950 if( i>=(unsigned int)p->nVar ){
89951 sqlite3Error(p->db, SQLITE_RANGE);
89952 sqlite3_mutex_leave(p->db->mutex);
89955 pVar = &p->aVar[i];
89957 pVar->flags = MEM_Null;
89958 p->db->errCode = SQLITE_OK;
89963 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
89969 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
89970 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
89971 p->expired = 1;
89991 rc = vdbeUnbind(p, (u32)(i-1));
89994 pVar = &p->aVar[i-1];
89997 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
90000 sqlite3Error(p->db, rc);
90001 rc = sqlite3ApiExit(p->db, rc);
90004 sqlite3_mutex_leave(p->db->mutex);
90040 rc = vdbeUnbind(p, (u32)(i-1));
90042 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
90043 sqlite3_mutex_leave(p->db->mutex);
90053 rc = vdbeUnbind(p, (u32)(i-1));
90055 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
90056 sqlite3_mutex_leave(p->db->mutex);
90063 rc = vdbeUnbind(p, (u32)(i-1));
90065 sqlite3_mutex_leave(p->db->mutex);
90078 rc = vdbeUnbind(p, (u32)(i-1));
90080 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
90081 sqlite3_mutex_leave(p->db->mutex);
90123 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
90127 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
90129 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
90134 if( pValue->flags & MEM_Zero ){
90135 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
90137 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
90142 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
90143 pValue->enc);
90156 rc = vdbeUnbind(p, (u32)(i-1));
90159 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
90161 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
90163 sqlite3_mutex_leave(p->db->mutex);
90170 sqlite3_mutex_enter(p->db->mutex);
90171 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
90177 rc = sqlite3ApiExit(p->db, rc);
90178 sqlite3_mutex_leave(p->db->mutex);
90188 return p ? p->nVar : 0;
90195 ** The result is always UTF-8.
90200 return sqlite3VListNumToName(p->pVList, i);
90210 return sqlite3VListNameToNum(p->pVList, zName, nName);
90223 assert( pTo->db==pFrom->db );
90224 assert( pTo->nVar==pFrom->nVar );
90225 sqlite3_mutex_enter(pTo->db->mutex);
90226 for(i=0; i<pFrom->nVar; i++){
90227 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
90229 sqlite3_mutex_leave(pTo->db->mutex);
90249 if( pFrom->nVar!=pTo->nVar ){
90252 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
90253 if( pTo->expmask ){
90254 pTo->expired = 1;
90256 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
90257 if( pFrom->expmask ){
90258 pFrom->expired = 1;
90271 return pStmt ? ((Vdbe*)pStmt)->db : 0;
90279 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
90287 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
90295 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
90312 sqlite3_mutex_enter(pDb->mutex);
90314 pNext = (sqlite3_stmt*)pDb->pVdbe;
90316 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
90318 sqlite3_mutex_leave(pDb->mutex);
90330 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
90337 sqlite3 *db = pVdbe->db;
90338 sqlite3_mutex_enter(db->mutex);
90340 db->pnBytesFreed = (int*)&v;
90341 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
90342 db->lookaside.pEnd = db->lookaside.pStart;
90344 db->pnBytesFreed = 0;
90345 db->lookaside.pEnd = db->lookaside.pTrueEnd;
90346 sqlite3_mutex_leave(db->mutex);
90348 v = pVdbe->aCounter[op];
90349 if( resetFlag ) pVdbe->aCounter[op] = 0;
90359 return p ? p->zSql : 0;
90379 sqlite3_mutex_enter(p->db->mutex);
90381 sqlite3_mutex_leave(p->db->mutex);
90394 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
90395 sqlite3_mutex_enter(p->db->mutex);
90396 p->zNormSql = sqlite3Normalize(p, p->zSql);
90397 sqlite3_mutex_leave(p->db->mutex);
90399 return p->zNormSql;
90418 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
90425 ** This function is called from within a pre-update callback to retrieve
90429 PreUpdate *p = db->pPreUpdate;
90434 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
90435 if( !p || p->op==SQLITE_INSERT ){
90439 if( p->pPk ){
90440 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
90442 if( iIdx>=p->pCsr->nField || iIdx<0 ){
90448 if( p->pUnpacked==0 ){
90452 assert( p->pCsr->eCurType==CURTYPE_BTREE );
90453 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
90456 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
90458 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
90459 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
90465 p->aRecord = aRec;
90468 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
90469 if( iIdx==p->pTab->iPKey ){
90470 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
90471 }else if( iIdx>=p->pUnpacked->nField ){
90473 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
90474 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
90475 testcase( pMem->flags & MEM_Int );
90476 testcase( pMem->flags & MEM_IntReal );
90489 ** This function is called from within a pre-update callback to retrieve
90493 PreUpdate *p = db->pPreUpdate;
90494 return (p ? p->keyinfo.nKeyField : 0);
90500 ** This function is designed to be called from within a pre-update callback
90504 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
90505 ** top-level trigger etc.).
90511 PreUpdate *p = db->pPreUpdate;
90512 return (p ? p->v->nFrame : 0);
90518 ** This function is designed to be called from within a pre-update callback
90522 PreUpdate *p = db->pPreUpdate;
90523 return (p ? p->iBlobWrite : -1);
90529 ** This function is called from within a pre-update callback to retrieve
90533 PreUpdate *p = db->pPreUpdate;
90537 if( !p || p->op==SQLITE_DELETE ){
90541 if( p->pPk && p->op!=SQLITE_UPDATE ){
90542 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
90544 if( iIdx>=p->pCsr->nField || iIdx<0 ){
90549 if( p->op==SQLITE_INSERT ){
90550 /* For an INSERT, memory cell p->iNewReg contains the serialized record
90552 UnpackedRecord *pUnpack = p->pNewUnpacked;
90554 Mem *pData = &p->v->aMem[p->iNewReg];
90557 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
90562 p->pNewUnpacked = pUnpack;
90564 pMem = &pUnpack->aMem[iIdx];
90565 if( iIdx==p->pTab->iPKey ){
90566 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
90567 }else if( iIdx>=pUnpack->nField ){
90571 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
90576 assert( p->op==SQLITE_UPDATE );
90577 if( !p->aNew ){
90578 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
90579 if( !p->aNew ){
90584 assert( iIdx>=0 && iIdx<p->pCsr->nField );
90585 pMem = &p->aNew[iIdx];
90586 if( pMem->flags==0 ){
90587 if( iIdx==p->pTab->iPKey ){
90588 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
90590 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
90615 if( idx<0 || idx>=p->nScan ) return 1;
90616 pScan = &p->aScan[idx];
90619 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
90623 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
90628 LogEst x = pScan->nEst;
90637 *(const char**)pOut = pScan->zName;
90641 if( pScan->addrExplain ){
90642 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
90649 if( pScan->addrExplain ){
90650 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
90652 *(int*)pOut = -1;
90668 memset(p->anExec, 0, p->nOp * sizeof(i64));
90689 ** The Vdbe parse-tree explainer is also found here.
90697 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
90722 ** This function returns a pointer to a nul-terminated string in memory
90726 ** then the returned string holds a copy of zRawSql with "-- " prepended
90732 ** with large (multi-megabyte) strings and blobs.
90742 ** parameter index is known, locate the value in p->aVar[]. Then render
90761 db = p->db;
90762 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
90763 if( db->nVdbeExec>1 ){
90767 sqlite3_str_append(&out, "-- ", 3);
90768 assert( (zRawSql - zStart) > 0 );
90769 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
90771 }else if( p->nVar==0 ){
90800 assert( idx>0 && idx<=p->nVar );
90801 pVar = &p->aVar[idx-1];
90802 if( pVar->flags & MEM_Null ){
90804 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
90805 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
90806 }else if( pVar->flags & MEM_Real ){
90807 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
90808 }else if( pVar->flags & MEM_Str ){
90815 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
90823 nOut = pVar->n;
90827 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
90830 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
90832 if( nOut<pVar->n ){
90833 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90839 }else if( pVar->flags & MEM_Zero ){
90840 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
90843 assert( pVar->flags & MEM_Blob );
90845 nOut = pVar->n;
90850 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
90854 if( nOut<pVar->n ){
90855 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90944 ** use this information to make sure that the zero-blob functionality
90951 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
90952 sqlite3_max_blobsize = p->n;
90962 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
90964 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
91016 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
91017 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
91018 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
91020 ** In other words, if M is 2, then I is either 0 (for fall-through) or
91022 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
91033 ** always taken, the flags should be 0x05 since the fall-through and
91035 ** flags should be 0x06 since only the fall-through approach is allowed.
91038 ** interested in equal or not-equal. In other words, I==0 and I==2
91043 ** should be no-ops except for special builds used to measure test coverage.
91048 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
91052 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
91064 ** iSrcLine - the line number of the VdbeCoverage() macro, with
91066 ** I - Mask of bits 0x07 indicating which cases are are
91068 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
91073 ** M - Type of jump. Same as M argument above
91098 if( ((P)->flags&MEM_Ephem)!=0 \
91102 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
91129 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
91130 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
91132 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
91140 assert( iCur>=0 && iCur<p->nCursor );
91141 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
91142 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
91143 p->apCsr[iCur] = 0;
91148 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
91149 ** to hold cursors, it is faster to in-line the logic. */
91150 assert( pMem->flags==MEM_Undefined );
91151 assert( (pMem->flags & MEM_Dyn)==0 );
91152 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
91153 if( pMem->szMalloc<nByte ){
91154 if( pMem->szMalloc>0 ){
91155 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
91157 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
91158 if( pMem->zMalloc==0 ){
91159 pMem->szMalloc = 0;
91162 pMem->szMalloc = nByte;
91165 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
91167 pCx->eCurType = eCurType;
91168 pCx->nField = nField;
91169 pCx->aOffset = &pCx->aType[nField];
91171 pCx->uc.pCursor = (BtCursor*)
91172 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
91173 sqlite3BtreeCursorZero(pCx->uc.pCursor);
91191 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
91211 u8 enc = pRec->enc;
91213 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
91214 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
91216 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
91217 pRec->flags |= MEM_Int;
91219 pRec->u.r = rValue;
91220 pRec->flags |= MEM_Real;
91223 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
91226 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
91227 pRec->flags &= ~MEM_Str;
91237 ** floating-point representation if an integer representation
91247 ** No-op. pRec is unchanged.
91257 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
91258 if( (pRec->flags & MEM_Real)==0 ){
91259 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
91270 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
91271 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
91272 testcase( pRec->flags & MEM_Int );
91273 testcase( pRec->flags & MEM_Real );
91274 testcase( pRec->flags & MEM_IntReal );
91278 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
91313 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
91319 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
91320 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
91322 pMem->u.i = 0;
91325 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
91327 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
91328 pMem->u.i = ix;
91333 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
91334 pMem->u.i = ix;
91344 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
91345 ** But it does set pMem->u.r and pMem->u.i appropriately.
91348 assert( (pMem->flags & MEM_Null)==0
91349 || pMem->db==0 || pMem->db->mallocFailed );
91350 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
91351 testcase( pMem->flags & MEM_Int );
91352 testcase( pMem->flags & MEM_Real );
91353 testcase( pMem->flags & MEM_IntReal );
91354 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
91356 assert( pMem->flags & (MEM_Str|MEM_Blob) );
91357 testcase( pMem->flags & MEM_Str );
91358 testcase( pMem->flags & MEM_Blob );
91368 int f = pMem->flags;
91386 for(i=0; i<25 && i<pMem->n; i++){
91387 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
91390 for(i=0; i<25 && i<pMem->n; i++){
91391 char z = pMem->z[i];
91396 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
91413 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
91414 for(j=0; j<25 && j<pMem->n; j++){
91415 c = pMem->z[j];
91418 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
91428 if( p->flags & MEM_Undefined ){
91430 }else if( p->flags & MEM_Null ){
91431 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
91432 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
91433 printf(" si:%lld", p->u.i);
91434 }else if( (p->flags & (MEM_IntReal))!=0 ){
91435 printf(" ir:%lld", p->u.i);
91436 }else if( p->flags & MEM_Int ){
91437 printf(" i:%lld", p->u.i);
91439 }else if( p->flags & MEM_Real ){
91440 printf(" r:%.17g", p->u.r);
91451 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
91456 if( p->pScopyFrom ){
91457 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
91476 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
91482 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
91492 ** high-performance timing routines.
91502 ** the number of non-transaction savepoints currently in the
91512 for(p=db->pSavepoint; p; p=p->pNext) n++;
91513 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
91527 int rc = p->pSharedBlock->xAddRow(pCtx, p->addedRows);
91532 for(i=0; i<pOp->p2; i++){
91535 rc = p->pSharedBlock->xPutLong(pCtx, p->addedRows, i, (sqlite3_int64)pMem[i].u.i);
91539 rc = p->pSharedBlock->xPutDouble(pCtx, p->addedRows, i, pMem[i].u.r);
91546 rc = p->pSharedBlock->xPutString(pCtx, p->addedRows, i, pMem[i].z, pMem[i].n+1);
91550 rc = p->pSharedBlock->xPutBlob(pCtx, p->addedRows, i, pMem[i].z, pMem[i].n);
91554 rc = p->pSharedBlock->xPutNull(pCtx, p->addedRows, i);
91558 rc = p->pSharedBlock->xPutOther(pCtx, p->addedRows, i);
91574 ** Return the register of pOp->p2 after first preparing it to be
91579 pOut->flags = MEM_Int;
91584 assert( pOp->p2>0 );
91585 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91586 pOut = &p->aMem[pOp->p2];
91588 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
91591 pOut->flags = MEM_Int;
91597 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
91598 ** with pOp->p3. Return the hash.
91604 assert( pOp->p4type==P4_INT32 );
91605 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
91607 if( p->flags & (MEM_Int|MEM_IntReal) ){
91608 h += p->u.i;
91609 }else if( p->flags & MEM_Real ){
91611 }else if( p->flags & (MEM_Str|MEM_Blob) ){
91612 h += p->n;
91613 if( p->flags & MEM_Zero ) h += p->u.nZero;
91630 return azTypes[sqlite3_value_type(pMem)-1];
91640 Op *aOp = p->aOp; /* Copy of p->aOp */
91649 sqlite3 *db = p->db; /* The database */
91657 Mem *aMem = p->aMem; /* Copy of p->aMem */
91667 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
91670 if( db->xProgress ){
91671 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
91672 assert( 0 < db->nProgressOps );
91673 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
91678 if( p->rc==SQLITE_NOMEM ){
91683 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
91684 testcase( p->rc!=SQLITE_OK );
91685 p->rc = SQLITE_OK;
91686 assert( p->bIsReader || p->readOnly!=0 );
91687 p->iCurrentTime = 0;
91688 assert( p->explain==0 );
91689 p->pResultSet = 0;
91690 db->busyHandler.nBusy = 0;
91691 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91695 if( p->pc==0
91696 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
91701 if( p->db->flags & SQLITE_VdbeListing ){
91703 for(i=0; i<p->nOp; i++){
91707 if( p->db->flags & SQLITE_VdbeEQP ){
91708 for(i=0; i<p->nOp; i++){
91716 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
91720 for(pOp=&aOp[p->pc]; 1; pOp++){
91725 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
91731 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
91737 if( db->flags & SQLITE_VdbeTrace ){
91738 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
91739 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
91749 sqlite3_interrupt_count--;
91759 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
91761 assert( pOp->p1>0 );
91762 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
91763 assert( memIsValid(&aMem[pOp->p1]) );
91764 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
91765 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
91768 assert( pOp->p2>0 );
91769 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91770 assert( memIsValid(&aMem[pOp->p2]) );
91771 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
91772 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
91775 assert( pOp->p3>0 );
91776 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91777 assert( memIsValid(&aMem[pOp->p3]) );
91778 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
91779 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
91782 assert( pOp->p2>0 );
91783 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91784 memAboutToChange(p, &aMem[pOp->p2]);
91787 assert( pOp->p3>0 );
91788 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91789 memAboutToChange(p, &aMem[pOp->p3]);
91797 switch( pOp->opcode ){
91804 ** the switch statement will break with convention and be flush-left. Another
91842 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
91852 if( pOp->p5 ){
91853 assert( pOp->p2 < (int)(pOp - aOp) );
91854 assert( pOp->p2 > 1 );
91855 pOp = &aOp[pOp->p2 - 2];
91862 pOp = &aOp[pOp->p2 - 1];
91873 ** faster according to "valgrind --tool=cachegrind" */
91875 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91880 ** If the progress callback returns non-zero, exit the virtual machine with
91883 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
91884 assert( db->nProgressOps!=0 );
91885 nProgressLimit += db->nProgressOps;
91886 if( db->xProgress(db->pProgressArg) ){
91903 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91904 pIn1 = &aMem[pOp->p1];
91907 pIn1->flags = MEM_Int;
91908 pIn1->u.i = (int)(pOp-aOp);
91909 REGISTER_TRACE(pOp->p1, pIn1);
91920 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
91927 ** P2 is not used by the byte-code engine. However, if P2 is positive
91932 ** value is a byte-code indentation hint. See tag-20220407a in
91936 pIn1 = &aMem[pOp->p1];
91937 if( pIn1->flags & MEM_Int ){
91938 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
91939 pOp = &aOp[pIn1->u.i];
91940 }else if( ALWAYS(pOp->p3) ){
91958 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91959 assert( pOp->p2>=0 && pOp->p2<p->nOp );
91960 assert( pOp->p3>=0 && pOp->p3<p->nOp );
91961 pOut = &aMem[pOp->p1];
91963 pOut->u.i = pOp->p3 - 1;
91964 pOut->flags = MEM_Int;
91965 if( pOp->p2==0 ) break;
91970 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
91971 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
91972 pOp = &aOp[pOp->p2 - 1];
91986 pIn1 = &aMem[pOp->p1];
91987 assert( pIn1->flags==MEM_Int );
91988 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
91989 pCaller = &aOp[pIn1->u.i];
91990 assert( pCaller->opcode==OP_Yield );
91991 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
91992 pOp = &aOp[pCaller->p2 - 1];
91993 pIn1->flags = MEM_Undefined;
92012 pIn1 = &aMem[pOp->p1];
92014 pIn1->flags = MEM_Int;
92015 pcDest = (int)pIn1->u.i;
92016 pIn1->u.i = (int)(pOp - aOp);
92017 REGISTER_TRACE(pOp->p1, pIn1);
92027 ** value in register P3 is not NULL, then this routine is a no-op.
92031 pIn3 = &aMem[pOp->p3];
92033 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
92035 if( (pIn3->flags & MEM_Null)==0 ) break;
92075 if( p->pSharedBlock!=NULL ){
92076 p->pSharedBlock->xFinish(p->pSharedBlock->pContext, p->addedRows, p->totalRows);
92081 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
92083 if( p->pFrame && pOp->p1==SQLITE_OK ){
92084 /* Halt the sub-program. Return control to the parent frame. */
92085 pFrame = p->pFrame;
92086 p->pFrame = pFrame->pParent;
92087 p->nFrame--;
92088 sqlite3VdbeSetChanges(db, p->nChange);
92090 if( pOp->p2==OE_Ignore ){
92091 /* Instruction pcx is the OP_Program that invoked the sub-program
92093 ** instruction is set to OE_Ignore, then the sub-program is throwing
92096 pcx = p->aOp[pcx].p2-1;
92098 aOp = p->aOp;
92099 aMem = p->aMem;
92103 p->rc = pOp->p1;
92104 p->errorAction = (u8)pOp->p2;
92105 assert( pOp->p5<=4 );
92106 if( p->rc ){
92107 if( pOp->p5 ){
92110 testcase( pOp->p5==1 );
92111 testcase( pOp->p5==2 );
92112 testcase( pOp->p5==3 );
92113 testcase( pOp->p5==4 );
92114 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
92115 if( pOp->p4.z ){
92116 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
92119 sqlite3VdbeError(p, "%s", pOp->p4.z);
92121 pcx = (int)(pOp - aOp);
92122 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
92127 p->rc = SQLITE_BUSY;
92129 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
92130 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
92131 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
92139 ** The 32-bit integer value P1 is written into register P2.
92143 pOut->u.i = pOp->p1;
92150 ** P4 is a pointer to a 64-bit integer value.
92155 assert( pOp->p4.pI64!=0 );
92156 pOut->u.i = *pOp->p4.pI64;
92164 ** P4 is a pointer to a 64-bit floating point value.
92169 pOut->flags = MEM_Real;
92170 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
92171 pOut->u.r = *pOp->p4.pReal;
92179 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
92185 assert( pOp->p4.z!=0 );
92187 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
92191 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
92195 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
92197 pOut->szMalloc = 0;
92198 pOut->flags |= MEM_Static;
92199 if( pOp->p4type==P4_DYNAMIC ){
92200 sqlite3DbFree(db, pOp->p4.z);
92202 pOp->p4type = P4_DYNAMIC;
92203 pOp->p4.z = pOut->z;
92204 pOp->p1 = pOut->n;
92207 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
92210 pOp->opcode = OP_String;
92229 assert( pOp->p4.z!=0 );
92231 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
92232 pOut->z = pOp->p4.z;
92233 pOut->n = pOp->p1;
92234 pOut->enc = encoding;
92237 if( pOp->p3>0 ){
92238 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92239 pIn3 = &aMem[pOp->p3];
92240 assert( pIn3->flags & MEM_Int );
92241 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
92250 ** Mark the beginning of a subroutine that can be entered in-line
92254 ** If the subroutine is entered in-line, then the OP_Return will simply
92260 ** a no-op that simply falls through to the next instruction (assuming that
92262 ** entered in-line, then the OP_Return will cause in-line execution to
92277 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
92286 cnt = pOp->p3-pOp->p2;
92287 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
92288 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
92289 pOut->n = 0;
92291 pOut->uTemp = 0;
92297 pOut->flags = nullFlag;
92298 pOut->n = 0;
92299 cnt--;
92313 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
92314 pOut = &aMem[pOp->p1];
92315 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
92324 ** a zero-filled blob that is P1 bytes long in P2.
92327 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
92329 if( pOp->p4.z==0 ){
92330 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
92333 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
92335 pOut->enc = encoding;
92351 assert( pOp->p1>0 && pOp->p1<=p->nVar );
92352 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
92353 pVar = &p->aVar[pOp->p1 - 1];
92357 pOut = &aMem[pOp->p2];
92360 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
92361 pOut->flags |= MEM_Static|MEM_FromBind;
92369 ** Move the P3 values in register P1..P1+P3-1 over into
92370 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
92372 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
92380 n = pOp->p3;
92381 p1 = pOp->p1;
92382 p2 = pOp->p2;
92389 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
92390 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
92395 pIn1->pScopyFrom = 0;
92397 for(i=1; i<p->nMem; i++){
92408 }while( --n );
92428 n = pOp->p3;
92429 pIn1 = &aMem[pOp->p1];
92430 pOut = &aMem[pOp->p2];
92436 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
92437 pOut->flags &= ~MEM_Subtype;
92440 pOut->pScopyFrom = 0;
92442 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
92443 if( (n--)==0 ) break;
92464 pIn1 = &aMem[pOp->p1];
92465 pOut = &aMem[pOp->p2];
92469 pOut->pScopyFrom = pIn1;
92470 pOut->mScopyFlags = pIn1->flags;
92484 pIn1 = &aMem[pOp->p1];
92485 assert( (pIn1->flags & MEM_Int)!=0 );
92486 pOut = &aMem[pOp->p2];
92487 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
92495 ** constraint violations, this is a no-op.
92512 ** The registers P1 through P1+P2-1 contain a single row of
92515 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
92519 assert( p->nResColumn==pOp->p2 );
92520 assert( pOp->p1>0 || CORRUPT_DB );
92521 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
92524 if( p->pSharedBlock!=NULL ){
92525 void *pCtx = p->pSharedBlock->pContext;
92526 p->totalRows++;
92527 if( p->totalRows<=p->startPos || p->blockFull ){
92530 Mem *pMem = &aMem[pOp->p1];
92532 …if( rc==SQLITE_FULL && p->addedRows && (p->startPos + p->addedRows) <= p->pSharedBlock->requiredPo…
92533 p->startPos += p->addedRows;
92534 p->addedRows = 0;
92535 p->pSharedBlock->xReset(pCtx,p->startPos);
92536 p->blockFull = 0;
92541 p->addedRows++;
92543 p->blockFull = 1;
92549 if( p->blockFull && p->pSharedBlock->countAllRows==0 ){
92550 p->pSharedBlock->xFinish(pCtx, p->addedRows, p->totalRows);
92553 }else if( p->blockFull && p->pSharedBlock->countAllRows==1 ){
92561 p->cacheCtr = (p->cacheCtr + 2)|1;
92562 p->pResultSet = &aMem[pOp->p1];
92565 Mem *pMem = p->pResultSet;
92567 for(i=0; i<pOp->p2; i++){
92569 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
92579 if( db->mallocFailed ) goto no_mem;
92580 if( db->mTrace & SQLITE_TRACE_ROW ){
92581 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
92583 p->pc = (int)(pOp - aOp) + 1;
92606 pIn1 = &aMem[pOp->p1];
92607 pIn2 = &aMem[pOp->p2];
92608 pOut = &aMem[pOp->p3];
92611 flags1 = pIn1->flags;
92613 testcase( pIn2->flags & MEM_Null );
92614 if( (flags1 | pIn2->flags) & MEM_Null ){
92620 flags1 = pIn1->flags & ~MEM_Str;
92623 flags1 = pIn1->flags & ~MEM_Str;
92625 flags2 = pIn2->flags;
92628 flags2 = pIn2->flags & ~MEM_Str;
92631 flags2 = pIn2->flags & ~MEM_Str;
92633 nByte = pIn1->n + pIn2->n;
92634 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
92642 memcpy(pOut->z, pIn2->z, pIn2->n);
92643 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
92644 pIn2->flags = flags2;
92646 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
92647 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
92648 pIn1->flags = flags1;
92650 pOut->z[nByte]=0;
92651 pOut->z[nByte+1] = 0;
92652 pOut->flags |= MEM_Term;
92653 pOut->n = (int)nByte;
92654 pOut->enc = encoding;
92675 ** Synopsis: r[P3]=r[P2]-r[P1]
92709 pIn1 = &aMem[pOp->p1];
92710 type1 = pIn1->flags;
92711 pIn2 = &aMem[pOp->p2];
92712 type2 = pIn2->flags;
92713 pOut = &aMem[pOp->p3];
92716 iA = pIn1->u.i;
92717 iB = pIn2->u.i;
92718 switch( pOp->opcode ){
92724 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
92730 if( iA==-1 ) iA = 1;
92735 pOut->u.i = iB;
92746 switch( pOp->opcode ){
92748 case OP_Subtract: rB -= rA; break;
92760 if( iA==-1 ) iA = 1;
92766 pOut->u.i = rB;
92772 pOut->u.r = rB;
92787 ** be returned. This is used by the built-in min(), max() and nullif()
92796 ** publicly. Only built-in functions have access to this feature.
92799 assert( pOp->p4type==P4_COLLSEQ );
92800 if( pOp->p1 ){
92801 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
92809 ** Take the bit-wise AND of the values in register P1 and P2 and
92816 ** Take the bit-wise OR of the values in register P1 and P2 and
92845 pIn1 = &aMem[pOp->p1];
92846 pIn2 = &aMem[pOp->p2];
92847 pOut = &aMem[pOp->p3];
92848 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
92854 op = pOp->opcode;
92865 op = 2*OP_ShiftLeft + 1 - op;
92866 iB = iB>(-64) ? -iB : 64;
92870 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
92877 /* Sign-extend on a right shift of a negative number */
92878 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
92883 pOut->u.i = iA;
92897 pIn1 = &aMem[pOp->p1];
92900 pIn1->u.i += pOp->p2;
92912 pIn1 = &aMem[pOp->p1];
92913 if( (pIn1->flags & MEM_Int)==0 ){
92915 if( (pIn1->flags & MEM_Int)==0 ){
92917 if( pOp->p2==0 ){
92941 pIn1 = &aMem[pOp->p1];
92942 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
92943 testcase( pIn1->flags & MEM_Int );
92944 testcase( pIn1->flags & MEM_IntReal );
92946 REGISTER_TRACE(pOp->p1, pIn1);
92969 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
92970 testcase( pOp->p2==SQLITE_AFF_TEXT );
92971 testcase( pOp->p2==SQLITE_AFF_BLOB );
92972 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
92973 testcase( pOp->p2==SQLITE_AFF_INTEGER );
92974 testcase( pOp->p2==SQLITE_AFF_REAL );
92975 pIn1 = &aMem[pOp->p1];
92979 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
92982 REGISTER_TRACE(pOp->p1, pIn1);
92993 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
93037 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
93087 u16 flags1; /* Copy of initial value of pIn1->flags */
93088 u16 flags3; /* Copy of initial value of pIn3->flags */
93090 pIn1 = &aMem[pOp->p1];
93091 pIn3 = &aMem[pOp->p3];
93092 flags1 = pIn1->flags;
93093 flags3 = pIn3->flags;
93095 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
93097 if( pIn3->u.i > pIn1->u.i ){
93098 if( sqlite3aGTb[pOp->opcode] ){
93099 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93103 }else if( pIn3->u.i < pIn1->u.i ){
93104 if( sqlite3aLTb[pOp->opcode] ){
93105 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93108 iCompare = -1;
93110 if( sqlite3aEQb[pOp->opcode] ){
93111 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93116 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93121 if( pOp->p5 & SQLITE_NULLEQ ){
93127 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
93128 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
93134 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
93142 if( pOp->p5 & SQLITE_JUMPIFNULL ){
93149 /* Neither operand is NULL and we couldn't do the special high-speed
93150 ** integer comparison case. So do a general-case comparison. */
93151 affinity = pOp->p5 & SQLITE_AFF_MASK;
93156 testcase( flags3==pIn3->flags );
93157 flags3 = pIn3->flags;
93165 testcase( pIn1->flags & MEM_Int );
93166 testcase( pIn1->flags & MEM_Real );
93167 testcase( pIn1->flags & MEM_IntReal );
93169 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
93170 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
93174 testcase( pIn3->flags & MEM_Int );
93175 testcase( pIn3->flags & MEM_Real );
93176 testcase( pIn3->flags & MEM_IntReal );
93178 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
93179 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
93182 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
93183 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
93195 res2 = sqlite3aLTb[pOp->opcode];
93197 res2 = sqlite3aEQb[pOp->opcode];
93199 res2 = sqlite3aGTb[pOp->opcode];
93204 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
93205 pIn3->flags = flags3;
93206 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
93207 pIn1->flags = flags1;
93209 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
93231 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
93234 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
93258 assert( pOp->p4type==P4_INTARRAY );
93259 assert( pOp->p4.ai );
93266 ** Synopsis: r[P1@P3] <-> r[P2@P3]
93268 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
93269 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
93298 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
93302 assert( pOp[-1].opcode==OP_Permutation );
93303 assert( pOp[-1].p4type==P4_INTARRAY );
93304 aPermute = pOp[-1].p4.ai + 1;
93307 n = pOp->p3;
93308 pKeyInfo = pOp->p4.pKeyInfo;
93311 p1 = pOp->p1;
93312 p2 = pOp->p2;
93317 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
93318 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
93320 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
93321 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
93330 assert( i<pKeyInfo->nKeyField );
93331 pColl = pKeyInfo->aColl[i];
93332 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
93335 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
93338 iCompare = -iCompare;
93340 if( bRev ) iCompare = -iCompare;
93358 if( pOp->p5&0x80 ){
93359 if( p->pSharedBlock!=NULL ){
93360 if( p->totalRows < p->startPos || p->blockFull ){
93361 p->totalRows++;
93362 pOp = &aOp[pOp->p2 - 1];
93368 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
93370 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
93372 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
93374 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
93404 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
93405 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
93406 if( pOp->opcode==OP_And ){
93413 pOut = &aMem[pOp->p3];
93417 pOut->u.i = v1;
93444 assert( pOp->p4type==P4_INT32 );
93445 assert( pOp->p4.i==0 || pOp->p4.i==1 );
93446 assert( pOp->p3==0 || pOp->p3==1 );
93447 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
93448 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
93460 pIn1 = &aMem[pOp->p1];
93461 pOut = &aMem[pOp->p2];
93462 if( (pIn1->flags & MEM_Null)==0 ){
93474 ** ones-complement of the P1 value into register P2. If P1 holds
93478 pIn1 = &aMem[pOp->p1];
93479 pOut = &aMem[pOp->p2];
93481 if( (pIn1->flags & MEM_Null)==0 ){
93482 pOut->flags = MEM_Int;
93483 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
93491 ** encountered on each invocation of the byte-code program. Jump to P2
93494 ** Top-level programs determine first invocation by comparing the P1
93502 ** because the self-altering code trick does not work for recursive
93507 assert( p->aOp[0].opcode==OP_Init );
93508 if( p->pFrame ){
93509 iAddr = (int)(pOp - p->aOp);
93510 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
93514 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
93516 if( p->aOp[0].p1==pOp->p1 ){
93522 pOp->p1 = p->aOp[0].p1;
93529 ** is considered true if it is numeric and non-zero. If the value
93530 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
93534 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
93544 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
93548 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
93560 pIn1 = &aMem[pOp->p1];
93561 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
93562 if( (pIn1->flags & MEM_Null)!=0 ){
93581 ** If P1 is -1, then P3 is a register number and the datatype is taken
93598 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
93599 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
93600 if( pOp->p1>=0 ){
93601 pC = p->apCsr[pOp->p1];
93603 assert( pOp->p3>=0 );
93604 if( pOp->p3<pC->nHdrParsed ){
93605 serialType = pC->aType[pOp->p3];
93632 typeMask = 1 << (pOp->p4.i - 1);
93640 assert( memIsValid(&aMem[pOp->p3]) );
93641 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
93648 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
93649 if( typeMask & pOp->p5 ){
93663 if( (aMem[pOp->p1].flags & MEM_Null)!=0
93664 || (aMem[pOp->p3].flags & MEM_Null)!=0
93666 sqlite3VdbeMemSetNull(aMem + pOp->p2);
93668 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
93679 pIn1 = &aMem[pOp->p1];
93680 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
93681 if( (pIn1->flags & MEM_Null)==0 ){
93695 ** If P1 is not an open cursor, then this opcode is a no-op.
93699 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93700 pC = p->apCsr[pOp->p1];
93701 if( ALWAYS(pC) && pC->nullRow ){
93702 sqlite3VdbeMemSetNull(aMem + pOp->p3);
93722 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
93726 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93727 pC = p->apCsr[pOp->p1];
93728 pOut = &p->aMem[pOp->p3];
93729 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
93732 if( pC->deferredMoveto ){
93736 if( sqlite3BtreeEof(pC->uc.pCursor) ){
93739 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
93751 ** information about the format of the data.) Extract the P2-th column
93771 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
93772 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
93780 u64 offset64; /* 64-bit offset */
93784 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93785 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93786 pC = p->apCsr[pOp->p1];
93787 p2 = (u32)pOp->p2;
93791 assert( p2<(u32)pC->nField
93792 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
93793 aOffset = pC->aOffset;
93794 assert( aOffset==pC->aType+pC->nField );
93795 assert( pC->eCurType!=CURTYPE_VTAB );
93796 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
93797 assert( pC->eCurType!=CURTYPE_SORTER );
93799 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
93800 if( pC->nullRow ){
93801 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
93802 /* For the special case of as pseudo-cursor, the seekResult field
93804 pReg = &aMem[pC->seekResult];
93805 assert( pReg->flags & MEM_Blob );
93807 pC->payloadSize = pC->szRow = pReg->n;
93808 pC->aRow = (u8*)pReg->z;
93810 pDest = &aMem[pOp->p3];
93816 pCrsr = pC->uc.pCursor;
93817 if( pC->deferredMoveto ){
93819 assert( !pC->isEphemeral );
93820 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
93821 pC = pC->pAltCursor;
93822 p2 = iMap - 1;
93832 assert( pC->eCurType==CURTYPE_BTREE );
93835 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
93836 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
93837 assert( pC->szRow<=pC->payloadSize );
93838 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
93840 pC->cacheStatus = p->cacheCtr;
93841 if( (aOffset[0] = pC->aRow[0])<0x80 ){
93842 pC->iHdrOffset = 1;
93844 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
93846 pC->nHdrParsed = 0;
93848 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
93849 /* pC->aRow does not have to hold the entire row, but it does at least
93850 ** need to cover the header of the record. If pC->aRow does not contain
93853 pC->aRow = 0;
93854 pC->szRow = 0;
93862 ** 3-byte type for each of the maximum of 32768 columns plus three
93865 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
93870 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
93882 zData = pC->aRow;
93883 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
93887 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
93894 ** parsed and valid information is in aOffset[] and pC->aType[].
93896 if( pC->nHdrParsed<=p2 ){
93898 ** to extract additional fields up through the p2+1-th field
93900 if( pC->iHdrOffset<aOffset[0] ){
93902 if( pC->aRow==0 ){
93904 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
93908 zData = pC->aRow;
93911 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
93913 i = pC->nHdrParsed;
93915 zHdr = zData + pC->iHdrOffset;
93919 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
93924 pC->aType[i] = t;
93935 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
93936 || (offset64 > pC->payloadSize)
93942 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93947 pC->nHdrParsed = i;
93948 pC->iHdrOffset = (u32)(zHdr - zData);
93949 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93958 if( pC->nHdrParsed<=p2 ){
93959 pDest = &aMem[pOp->p3];
93961 if( pOp->p4type==P4_MEM ){
93962 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
93969 t = pC->aType[p2];
93972 /* Extract the content for the p2+1-th column. Control can only
93973 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
93976 assert( p2<pC->nHdrParsed );
93978 pDest = &aMem[pOp->p3];
93984 assert( t==pC->aType[p2] );
93985 if( pC->szRow>=aOffset[p2+1] ){
93987 ** page - where the content is not on an overflow page */
93988 zData = pC->aRow + aOffset[p2];
93993 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
93997 pDest->n = len = (t-12)/2;
93998 pDest->enc = encoding;
93999 if( pDest->szMalloc < len+2 ){
94000 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
94001 pDest->flags = MEM_Null;
94004 pDest->z = pDest->zMalloc;
94006 memcpy(pDest->z, zData, len);
94007 pDest->z[len] = 0;
94008 pDest->z[len+1] = 0;
94009 pDest->flags = aFlag[t&1];
94012 pDest->enc = encoding;
94014 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
94015 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
94033 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
94034 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
94036 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
94037 pDest->flags &= ~MEM_Ephem;
94043 REGISTER_TRACE(pOp->p3, pDest);
94048 pOp = &aOp[aOp[0].p3-1];
94068 ** is zero. When P3 is non-zero, no type checking occurs for
94075 ** <li> P2 should be the number of non-virtual columns in the
94087 assert( pOp->p4type==P4_TABLE );
94088 pTab = pOp->p4.pTab;
94089 assert( pTab->tabFlags & TF_Strict );
94090 assert( pTab->nNVCol==pOp->p2 );
94091 aCol = pTab->aCol;
94092 pIn1 = &aMem[pOp->p1];
94093 for(i=0; i<pTab->nCol; i++){
94096 if( pOp->p3 ){ pIn1++; continue; }
94098 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
94100 if( (pIn1->flags & MEM_Null)==0 ){
94103 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
94108 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
94112 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
94116 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
94117 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
94118 if( pIn1->flags & MEM_Int ){
94121 ** so that we keep the high-resolution integer value but know that
94123 testcase( pIn1->u.i==140737488355328LL );
94124 testcase( pIn1->u.i==140737488355327LL );
94125 testcase( pIn1->u.i==-140737488355328LL );
94126 testcase( pIn1->u.i==-140737488355329LL );
94127 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
94128 pIn1->flags |= MEM_IntReal;
94129 pIn1->flags &= ~MEM_Int;
94131 pIn1->u.r = (double)pIn1->u.i;
94132 pIn1->flags |= MEM_Real;
94133 pIn1->flags &= ~MEM_Int;
94135 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
94146 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
94149 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
94154 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
94155 pTab->zName, aCol[i].zCnName);
94165 ** P4 is a string that is P2 characters long. The N-th character of the
94166 ** string indicates the column affinity that should be used for the N-th
94172 zAffinity = pOp->p4.z;
94174 assert( pOp->p2>0 );
94175 assert( zAffinity[pOp->p2]==0 );
94176 pIn1 = &aMem[pOp->p1];
94177 while( 1 /*exit-by-break*/ ){
94178 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
94181 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
94184 ** so that we keep the high-resolution integer value but know that
94186 testcase( pIn1->u.i==140737488355328LL );
94187 testcase( pIn1->u.i==140737488355327LL );
94188 testcase( pIn1->u.i==-140737488355328LL );
94189 testcase( pIn1->u.i==-140737488355329LL );
94190 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
94191 pIn1->flags |= MEM_IntReal;
94192 pIn1->flags &= ~MEM_Int;
94194 pIn1->u.r = (double)pIn1->u.i;
94195 pIn1->flags |= MEM_Real;
94196 pIn1->flags &= ~MEM_Int;
94199 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
94214 ** P4 may be a string that is P2 characters long. The N-th character of the
94215 ** string indicates the column affinity that should be used for the N-th
94224 ** compile-time option is enabled:
94227 ** of the right-most table that can be null-trimmed.
94231 ** accept no-change records with serial_type 10. This value is
94253 ** ------------------------------------------------------------------------
94254 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
94255 ** ------------------------------------------------------------------------
94262 ** hdr-size field is also a varint which is the offset from the beginning
94268 nField = pOp->p1;
94269 zAffinity = pOp->p4.z;
94270 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
94272 nField = pOp->p2;
94273 pLast = &pData0[nField-1];
94276 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
94277 pOut = &aMem[pOp->p3];
94287 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
94288 pRec->flags |= MEM_IntReal;
94289 pRec->flags &= ~(MEM_Int);
94291 REGISTER_TRACE((int)(pRec-aMem), pRec);
94301 ** have a non-NULL default value. Also, the record must be left with
94303 ** index of the right-most column with a non-NULL default value */
94304 if( pOp->p5 ){
94305 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
94306 pLast--;
94307 nField--;
94314 ** the Mem.uTemp field of each term should hold the serial-type that will
94318 ** --------------- ---------------
94320 ** 1 1-byte signed integer
94321 ** 2 2-byte signed integer
94322 ** 3 3-byte signed integer
94323 ** 4 4-byte signed integer
94324 ** 5 6-byte signed integer
94325 ** 6 8-byte signed integer
94341 if( pRec->flags & MEM_Null ){
94342 if( pRec->flags & MEM_Zero ){
94346 ** Give such values a special internal-use-only serial-type of 10
94350 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
94352 pRec->uTemp = 10;
94354 pRec->uTemp = 0;
94357 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
94359 i64 i = pRec->u.i;
94361 testcase( pRec->flags & MEM_Int );
94362 testcase( pRec->flags & MEM_IntReal );
94375 if( (i&1)==i && p->minWriteFileFormat>=4 ){
94376 pRec->uTemp = 8+(u32)uu;
94379 pRec->uTemp = 1;
94383 pRec->uTemp = 2;
94386 pRec->uTemp = 3;
94389 pRec->uTemp = 4;
94392 pRec->uTemp = 5;
94395 if( pRec->flags & MEM_IntReal ){
94397 ** as an integer, then we might as well make it an 8-byte floating
94399 pRec->u.r = (double)pRec->u.i;
94400 pRec->flags &= ~MEM_IntReal;
94401 pRec->flags |= MEM_Real;
94402 pRec->uTemp = 7;
94404 pRec->uTemp = 6;
94407 }else if( pRec->flags & MEM_Real ){
94410 pRec->uTemp = 7;
94412 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
94413 assert( pRec->n>=0 );
94414 len = (u32)pRec->n;
94415 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
94416 if( pRec->flags & MEM_Zero ){
94417 serial_type += pRec->u.nZero*2;
94420 len += pRec->u.nZero;
94422 nZero += pRec->u.nZero;
94427 pRec->uTemp = serial_type;
94430 pRec--;
94433 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
94451 ** the new record. The output register (pOp->p3) is not allowed to
94455 if( nByte+nZero<=pOut->szMalloc ){
94458 pOut->z = pOut->zMalloc;
94462 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
94469 pOut->n = (int)nByte;
94470 pOut->flags = MEM_Blob;
94472 pOut->u.nZero = nZero;
94473 pOut->flags |= MEM_Zero;
94476 zHdr = (u8 *)pOut->z;
94487 while( 1 /*exit-by-break*/ ){
94488 serial_type = pRec->uTemp;
94489 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
94491 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
94501 assert( sizeof(v)==sizeof(pRec->u.r) );
94502 memcpy(&v, &pRec->u.r, sizeof(v));
94505 v = pRec->u.i;
94509 while( 1 /*exit-by-break*/ ){
94510 zPayload[--i] = (u8)(v&0xFF);
94518 if( serial_type>=14 && pRec->n>0 ){
94519 assert( pRec->z!=0 );
94520 memcpy(zPayload, pRec->z, pRec->n);
94521 zPayload += pRec->n;
94525 if( pRec->n ){
94526 assert( pRec->z!=0 );
94527 memcpy(zPayload, pRec->z, pRec->n);
94528 zPayload += pRec->n;
94534 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
94535 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
94537 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
94538 REGISTER_TRACE(pOp->p3, pOut);
94549 ** every btree page of the table. But if P3 is non-zero, an estimate
94556 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
94557 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
94559 if( pOp->p3 ){
94567 pOut->u.i = nEntry;
94588 p1 = pOp->p1;
94589 zName = pOp->p4.z;
94594 assert( db->pSavepoint==0 || db->autoCommit==0 );
94596 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
94598 assert( p->bIsReader );
94601 if( db->nVdbeWrite>0 ){
94605 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
94614 ** that the db->aVTrans[] array is empty. */
94615 assert( db->autoCommit==0 || db->nVTrans==0 );
94617 db->nStatement+db->nSavepoint);
94624 pNew->zName = (char *)&pNew[1];
94625 memcpy(pNew->zName, zName, nName+1);
94629 if( db->autoCommit ){
94630 db->autoCommit = 0;
94631 db->isTransactionSavepoint = 1;
94633 db->nSavepoint++;
94637 pNew->pNext = db->pSavepoint;
94638 db->pSavepoint = pNew;
94639 pNew->nDeferredCons = db->nDeferredCons;
94640 pNew->nDeferredImmCons = db->nDeferredImmCons;
94650 pSavepoint = db->pSavepoint;
94651 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
94652 pSavepoint = pSavepoint->pNext
94659 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
94663 sqlite3VdbeError(p, "cannot release savepoint - "
94672 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
94677 db->autoCommit = 1;
94679 p->pc = (int)(pOp - aOp);
94680 db->autoCommit = 0;
94681 p->rc = rc = SQLITE_BUSY;
94684 rc = p->rc;
94686 db->autoCommit = 0;
94688 db->isTransactionSavepoint = 0;
94692 iSavepoint = db->nSavepoint - iSavepoint - 1;
94694 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
94695 for(ii=0; ii<db->nDb; ii++){
94696 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
94705 for(ii=0; ii<db->nDb; ii++){
94706 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
94714 db->mDbFlags |= DBFLAG_SchemaChange;
94721 while( db->pSavepoint!=pSavepoint ){
94722 pTmp = db->pSavepoint;
94723 db->pSavepoint = pTmp->pNext;
94725 db->nSavepoint--;
94733 assert( pSavepoint==db->pSavepoint );
94734 db->pSavepoint = pSavepoint->pNext;
94737 db->nSavepoint--;
94741 db->nDeferredCons = pSavepoint->nDeferredCons;
94742 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
94752 if( p->eVdbeState==VDBE_HALT_STATE ){
94761 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
94766 ** This instruction causes the VM to halt.
94772 desiredAutoCommit = pOp->p1;
94773 iRollback = pOp->p2;
94776 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
94777 assert( p->bIsReader );
94779 if( desiredAutoCommit!=db->autoCommit ){
94783 db->autoCommit = 1;
94784 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
94788 sqlite3VdbeError(p, "cannot commit transaction - "
94795 db->autoCommit = (u8)desiredAutoCommit;
94798 p->pc = (int)(pOp - aOp);
94799 db->autoCommit = (u8)(1-desiredAutoCommit);
94800 p->rc = rc = SQLITE_BUSY;
94804 if( p->rc==SQLITE_OK ){
94813 (iRollback)?"cannot rollback - no transaction is active":
94814 "cannot commit - no transaction is active"));
94826 ** If P2 is non-zero, then a write-transaction is started, or if a
94827 ** read-transaction is already active, it is upgraded to a write-transaction.
94828 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
94836 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
94862 assert( p->bIsReader );
94863 assert( p->readOnly==0 || pOp->p2==0 );
94864 assert( pOp->p2>=0 && pOp->p2<=2 );
94865 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94866 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94868 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
94869 if( db->flags & SQLITE_QueryOnly ){
94879 pDb = &db->aDb[pOp->p1];
94880 pBt = pDb->pBt;
94883 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
94888 p->pc = (int)(pOp - aOp);
94889 p->rc = rc;
94895 if( p->usesStmtJournal
94896 && pOp->p2
94897 && (db->autoCommit==0 || db->nVdbeRead>1)
94900 if( p->iStatement==0 ){
94901 assert( db->nStatement>=0 && db->nSavepoint>=0 );
94902 db->nStatement++;
94903 p->iStatement = db->nSavepoint + db->nStatement;
94906 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
94908 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
94914 p->nStmtDefCons = db->nDeferredCons;
94915 p->nStmtDefImmCons = db->nDeferredImmCons;
94918 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
94920 && pOp->p5
94921 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
94924 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
94928 sqlite3DbFree(db, p->zErrMsg);
94929 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
94930 /* If the schema-cookie from the database file matches the cookie
94931 ** stored with the in-memory representation of the schema, do
94934 ** If virtual-tables are in use, this is not just an optimization.
94935 ** Often, v-tables store their data in other SQLite tables, which
94936 ** are queried from within xNext() and other v-table methods using
94937 ** prepared queries. If such a query is out-of-date, we do not want to
94939 ** v-table would have to be ready for the sqlite3_vtab structure itself
94941 ** a v-table method.
94943 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
94944 sqlite3ResetOneSchema(db, pOp->p1);
94946 p->expired = 1;
94952 p->changeCntOn = 0;
94966 ** There must be a read-lock on the database (either a transaction
94975 assert( p->bIsReader );
94976 iDb = pOp->p1;
94977 iCookie = pOp->p3;
94978 assert( pOp->p3<SQLITE_N_BTREE_META );
94979 assert( iDb>=0 && iDb<db->nDb );
94980 assert( db->aDb[iDb].pBt!=0 );
94981 assert( DbMaskTest(p->btreeMask, iDb) );
94983 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
94985 pOut->u.i = iMeta;
95000 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
95008 assert( pOp->p2<SQLITE_N_BTREE_META );
95009 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95010 assert( DbMaskTest(p->btreeMask, pOp->p1) );
95011 assert( p->readOnly==0 );
95012 pDb = &db->aDb[pOp->p1];
95013 assert( pDb->pBt!=0 );
95014 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
95016 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
95017 if( pOp->p2==BTREE_SCHEMA_VERSION ){
95019 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
95020 db->mDbFlags |= DBFLAG_SchemaChange;
95021 sqlite3FkClearTriggerCache(db, pOp->p1);
95022 }else if( pOp->p2==BTREE_FILE_FORMAT ){
95024 pDb->pSchema->file_format = pOp->p3;
95026 if( pOp->p1==1 ){
95030 p->expired = 0;
95039 ** Open a read-only cursor for the database table whose root page is
95056 ** object, then table being opened must be an [index b-tree] where the
95058 ** sequence of that index b-tree. Otherwise, if P4 is an integer
95059 ** value, then the table being opened must be a [table b-tree] with a
95069 ** b-tree and if it is this opcode becomes a no-op. In other words,
95091 ** OPFLAG_P2ISREG bit is set in P5 - see below).
95095 ** object, then table being opened must be an [index b-tree] where the
95097 ** sequence of that index b-tree. Otherwise, if P4 is an integer
95098 ** value, then the table being opened must be a [table b-tree] with a
95130 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
95131 assert( pOp->p4type==P4_KEYINFO );
95132 pCur = p->apCsr[pOp->p1];
95133 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
95134 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
95135 assert( pCur->eCurType==CURTYPE_BTREE );
95136 sqlite3BtreeClearCursor(pCur->uc.pCursor);
95144 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
95145 assert( p->bIsReader );
95146 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
95147 || p->readOnly==0 );
95149 if( p->expired==1 ){
95156 p2 = (u32)pOp->p2;
95157 iDb = pOp->p3;
95158 assert( iDb>=0 && iDb<db->nDb );
95159 assert( DbMaskTest(p->btreeMask, iDb) );
95160 pDb = &db->aDb[iDb];
95161 pX = pDb->pBt;
95163 if( pOp->opcode==OP_OpenWrite ){
95165 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
95167 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
95168 p->minWriteFileFormat = pDb->pSchema->file_format;
95173 if( pOp->p5 & OPFLAG_P2ISREG ){
95175 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
95176 assert( pOp->opcode==OP_OpenWrite );
95179 assert( (pIn2->flags & MEM_Int)!=0 );
95181 p2 = (int)pIn2->u.i;
95188 if( pOp->p4type==P4_KEYINFO ){
95189 pKeyInfo = pOp->p4.pKeyInfo;
95190 assert( pKeyInfo->enc==ENC(db) );
95191 assert( pKeyInfo->db==db );
95192 nField = pKeyInfo->nAllField;
95193 }else if( pOp->p4type==P4_INT32 ){
95194 nField = pOp->p4.i;
95196 assert( pOp->p1>=0 );
95199 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
95201 pCur->iDb = iDb;
95202 pCur->nullRow = 1;
95203 pCur->isOrdered = 1;
95204 pCur->pgnoRoot = p2;
95206 pCur->wrFlag = wrFlag;
95208 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
95209 pCur->pKeyInfo = pKeyInfo;
95211 ** SQLite used to check if the root-page flags were sane at this point
95214 pCur->isTable = pOp->p4type!=P4_KEYINFO;
95219 testcase( pOp->p5 & OPFLAG_BULKCSR );
95220 testcase( pOp->p2 & OPFLAG_SEEKEQ );
95221 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
95222 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
95233 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
95239 pOrig = p->apCsr[pOp->p2];
95241 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
95243 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
95245 pCx->nullRow = 1;
95246 pCx->isEphemeral = 1;
95247 pCx->pKeyInfo = pOrig->pKeyInfo;
95248 pCx->isTable = pOrig->isTable;
95249 pCx->pgnoRoot = pOrig->pgnoRoot;
95250 pCx->isOrdered = pOrig->isOrdered;
95251 pCx->ub.pBtx = pOrig->ub.pBtx;
95252 pCx->noReuse = 1;
95253 pOrig->noReuse = 1;
95254 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
95255 pCx->pKeyInfo, pCx->uc.pCursor);
95269 ** the main database is read-only. The ephemeral
95286 ** can be used as zero-length data for OP_Insert. This is an optimization
95308 assert( pOp->p1>=0 );
95309 assert( pOp->p2>=0 );
95310 if( pOp->p3>0 ){
95313 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
95314 assert( pOp->opcode==OP_OpenEphemeral );
95315 assert( aMem[pOp->p3].flags & MEM_Null );
95316 aMem[pOp->p3].n = 0;
95317 aMem[pOp->p3].z = "";
95319 pCx = p->apCsr[pOp->p1];
95320 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
95324 assert( pCx->isEphemeral );
95325 pCx->seqCount = 0;
95326 pCx->cacheStatus = CACHE_STALE;
95327 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
95329 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
95331 pCx->isEphemeral = 1;
95332 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
95333 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
95336 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
95341 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
95343 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
95344 assert( pOp->p4type==P4_KEYINFO );
95345 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
95346 BTREE_BLOBKEY | pOp->p5);
95348 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
95349 assert( pKeyInfo->db==db );
95350 assert( pKeyInfo->enc==ENC(db) );
95351 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
95352 pKeyInfo, pCx->uc.pCursor);
95354 pCx->isTable = 0;
95356 pCx->pgnoRoot = SCHEMA_ROOT;
95357 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
95358 0, pCx->uc.pCursor);
95359 pCx->isTable = 1;
95362 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
95364 sqlite3BtreeClose(pCx->ub.pBtx);
95369 pCx->nullRow = 1;
95377 ** tables using an external merge-sort algorithm.
95379 ** If argument P3 is non-zero, then it indicates that the sorter may
95386 assert( pOp->p1>=0 );
95387 assert( pOp->p2>=0 );
95388 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
95390 pCx->pKeyInfo = pOp->p4.pKeyInfo;
95391 assert( pCx->pKeyInfo->db==db );
95392 assert( pCx->pKeyInfo->enc==ENC(db) );
95393 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
95407 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95408 pC = p->apCsr[pOp->p1];
95410 if( (pC->seqCount++)==0 ){
95424 ** A pseudo-table created by this opcode is used to hold a single
95427 ** is the only cursor opcode that works with a pseudo-table.
95430 ** the pseudo-table.
95435 assert( pOp->p1>=0 );
95436 assert( pOp->p3>=0 );
95437 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
95439 pCx->nullRow = 1;
95440 pCx->seekResult = pOp->p2;
95441 pCx->isTable = 1;
95442 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
95444 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
95446 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
95447 assert( pOp->p5==0 );
95454 ** currently open, this instruction is a no-op.
95457 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95458 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
95459 p->apCsr[pOp->p1] = 0;
95468 ** table or index for cursor P1 are used. P4 is a 64-bit integer
95471 ** by the cursor. The high-order bit is set if any column after
95476 pC = p->apCsr[pOp->p1];
95477 assert( pC->eCurType==CURTYPE_BTREE );
95478 pC->maskUsed = *(u64*)pOp->p4.pI64;
95486 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95513 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95531 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95549 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
95585 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95586 assert( pOp->p2!=0 );
95587 pC = p->apCsr[pOp->p1];
95589 assert( pC->eCurType==CURTYPE_BTREE );
95593 assert( pC->isOrdered );
95594 assert( pC->uc.pCursor!=0 );
95595 oc = pOp->opcode;
95597 pC->nullRow = 0;
95599 pC->seekOp = pOp->opcode;
95602 pC->deferredMoveto = 0;
95603 pC->cacheStatus = CACHE_STALE;
95604 if( pC->isTable ){
95607 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
95613 pIn3 = &aMem[pOp->p3];
95614 flags3 = pIn3->flags;
95619 newType = pIn3->flags; /* Record the type after applying numeric affinity */
95620 pIn3->flags = flags3; /* But convert the type back to its original */
95631 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
95636 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
95642 ** (x > 4.9) -> (x >= 5)
95643 ** (x <= 4.9) -> (x < 5)
95646 assert( OP_SeekGE==(OP_SeekGT-1) );
95647 assert( OP_SeekLT==(OP_SeekLE-1) );
95649 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
95661 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
95662 pC->movetoTarget = iKey; /* Used by OP_Delete */
95672 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
95674 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
95676 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
95677 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
95684 nField = pOp->p4.i;
95685 assert( pOp->p4type==P4_INT32 );
95687 r.pKeyInfo = pC->pKeyInfo;
95692 ** r.default_rc = -1;
95697 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
95698 assert( oc!=OP_SeekGT || r.default_rc==-1 );
95699 assert( oc!=OP_SeekLE || r.default_rc==-1 );
95703 r.aMem = &aMem[pOp->p3];
95709 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
95714 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
95729 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95745 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
95758 res = sqlite3BtreeEof(pC->uc.pCursor);
95762 assert( pOp->p2>0 );
95775 ** Synopsis: Scan-ahead up to P1 rows
95787 ** This opcode helps to optimize IN operators on a multi-column index
95790 ** of the b-tree instead. A correct answer is obtained if this opcode
95791 ** is omitted or is a no-op.
95798 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
95810 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
95850 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
95853 assert( pOp->p2>=(int)(pOp-aOp)+2 );
95855 if( pOp->p5==0 ){
95857 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
95858 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
95859 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
95860 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
95861 || aOp[pOp->p2-1].opcode==OP_IdxGE );
95862 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
95865 assert( pOp->p2==(int)(pOp-aOp)+2 );
95866 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
95870 assert( pOp->p1>0 );
95871 pC = p->apCsr[pOp[1].p1];
95873 assert( pC->eCurType==CURTYPE_BTREE );
95874 assert( !pC->isTable );
95875 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
95877 if( db->flags&SQLITE_VdbeTrace ){
95878 printf("... cursor not valid - fall through\n");
95883 nStep = pOp->p1;
95885 r.pKeyInfo = pC->pKeyInfo;
95902 if( res>0 && pOp->p5==0 ){
95906 if( db->flags&SQLITE_VdbeTrace ){
95907 printf("... %d steps and then skip\n", pOp->p1 - nStep);
95917 if( db->flags&SQLITE_VdbeTrace ){
95918 printf("... %d steps and then success\n", pOp->p1 - nStep);
95927 if( db->flags&SQLITE_VdbeTrace ){
95928 printf("... fall through after %d steps\n", pOp->p1);
95934 nStep--;
95935 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95960 ** early, thus saving work. This is part of the IN-early-out optimization.
95962 ** P1 must be a valid b-tree cursor.
95966 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95967 pC = p->apCsr[pOp->p1];
95969 assert( pOp->p3>=pOp->p2 );
95970 if( pC->seekHit<pOp->p2 ){
95972 if( db->flags&SQLITE_VdbeTrace ){
95973 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
95976 pC->seekHit = pOp->p2;
95977 }else if( pC->seekHit>pOp->p3 ){
95979 if( db->flags&SQLITE_VdbeTrace ){
95980 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
95983 pC->seekHit = pOp->p3;
95997 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95998 pCur = p->apCsr[pOp->p1];
95999 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
96000 if( pCur==0 || pCur->nullRow ){
96061 ** This opcode is used in IN clause processing for a multi-column key.
96063 ** left-most element, and if there are no matches on the most recent
96082 ** record are not-NULL then a check is done to determine if any row in the
96098 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96099 pC = p->apCsr[pOp->p1];
96102 if( db->flags&SQLITE_VdbeTrace ){
96103 printf("seekHit is %d\n", pC->seekHit);
96106 if( pC->seekHit>=pOp->p4.i ) break;
96120 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
96123 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96124 assert( pOp->p4type==P4_INT32 );
96125 pC = p->apCsr[pOp->p1];
96128 pC->seekOp = pOp->opcode;
96130 r.aMem = &aMem[pOp->p3];
96131 assert( pC->eCurType==CURTYPE_BTREE );
96132 assert( pC->uc.pCursor!=0 );
96133 assert( pC->isTable==0 );
96134 r.nField = (u16)pOp->p4.i;
96137 r.pKeyInfo = pC->pKeyInfo;
96143 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
96146 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
96149 assert( r.aMem->flags & MEM_Blob );
96150 assert( pOp->opcode!=OP_NoConflict );
96154 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
96156 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
96157 pIdxKey->default_rc = 0;
96158 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
96164 alreadyExists = (pC->seekResult==0);
96165 pC->nullRow = 1-alreadyExists;
96166 pC->deferredMoveto = 0;
96167 pC->cacheStatus = CACHE_STALE;
96168 if( pOp->opcode==OP_Found ){
96176 if( pOp->opcode==OP_NoConflict ){
96188 if( pOp->opcode==OP_IfNoHope ){
96189 pC->seekHit = pOp->p4.i;
96211 ** (with arbitrary multi-value keys).
96230 ** P3 register to contain a non-integer value, in which case the jump is
96234 ** (with arbitrary multi-value keys).
96248 pIn3 = &aMem[pOp->p3];
96249 testcase( pIn3->flags & MEM_Int );
96250 testcase( pIn3->flags & MEM_IntReal );
96251 testcase( pIn3->flags & MEM_Real );
96252 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
96253 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
96254 /* If pIn3->u.i does not contain an integer, compute iKey as the
96268 pIn3 = &aMem[pOp->p3];
96269 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
96270 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96271 iKey = pIn3->u.i;
96273 pC = p->apCsr[pOp->p1];
96276 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
96278 assert( pC->isTable );
96279 assert( pC->eCurType==CURTYPE_BTREE );
96280 pCrsr = pC->uc.pCursor;
96285 pC->movetoTarget = iKey; /* Used by OP_Delete */
96286 pC->nullRow = 0;
96287 pC->cacheStatus = CACHE_STALE;
96288 pC->deferredMoveto = 0;
96290 pC->seekResult = res;
96293 if( pOp->p2==0 ){
96312 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96313 assert( p->apCsr[pOp->p1]!=0 );
96314 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
96316 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
96349 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96350 pC = p->apCsr[pOp->p1];
96352 assert( pC->isTable );
96353 assert( pC->eCurType==CURTYPE_BTREE );
96354 assert( pC->uc.pCursor!=0 );
96357 ** thing) is obtained in a two-step algorithm.
96369 assert( pC->isTable );
96381 if( !pC->useRandomRowid ){
96382 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
96387 v = 1; /* IMP: R-61914-48074 */
96389 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
96390 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96392 pC->useRandomRowid = 1;
96394 v++; /* IMP: R-29538-34987 */
96400 if( pOp->p3 ){
96402 assert( pOp->p3>0 );
96403 if( p->pFrame ){
96404 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
96406 assert( pOp->p3<=pFrame->nMem );
96407 pMem = &pFrame->aMem[pOp->p3];
96410 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
96411 pMem = &aMem[pOp->p3];
96416 REGISTER_TRACE(pOp->p3, pMem);
96418 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
96419 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
96420 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
96423 if( v<pMem->u.i+1 ){
96424 v = pMem->u.i + 1;
96426 pMem->u.i = v;
96429 if( pC->useRandomRowid ){
96430 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
96434 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
96440 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
96446 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
96449 assert( v>0 ); /* EV: R-40812-03570 */
96451 pC->deferredMoveto = 0;
96452 pC->cacheStatus = CACHE_STALE;
96454 pOut->u.i = v;
96483 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
96486 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
96487 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
96500 const char *zDb; /* database name - used by the update hook */
96501 Table *pTab; /* Table structure - used by update and pre-update hooks */
96504 pData = &aMem[pOp->p2];
96505 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96507 pC = p->apCsr[pOp->p1];
96509 assert( pC->eCurType==CURTYPE_BTREE );
96510 assert( pC->deferredMoveto==0 );
96511 assert( pC->uc.pCursor!=0 );
96512 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
96513 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
96514 REGISTER_TRACE(pOp->p2, pData);
96517 pKey = &aMem[pOp->p3];
96518 assert( pKey->flags & MEM_Int );
96520 REGISTER_TRACE(pOp->p3, pKey);
96521 x.nKey = pKey->u.i;
96523 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
96524 assert( pC->iDb>=0 );
96525 zDb = db->aDb[pC->iDb].zDbSName;
96526 pTab = pOp->p4.pTab;
96527 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
96534 /* Invoke the pre-update hook, if any */
96536 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
96537 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
96539 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
96540 /* Prevent post-update hook from running in cases when it should not */
96544 if( pOp->p5 & OPFLAG_ISNOOP ) break;
96547 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
96548 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
96549 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
96550 x.pData = pData->z;
96551 x.nData = pData->n;
96552 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
96553 if( pData->flags & MEM_Zero ){
96554 x.nZero = pData->u.nZero;
96559 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
96560 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
96563 pC->deferredMoveto = 0;
96564 pC->cacheStatus = CACHE_STALE;
96566 /* Invoke the update-hook if required. */
96569 assert( db->xUpdateCallback!=0 );
96570 assert( pTab->aCol!=0 );
96571 db->xUpdateCallback(db->pUpdateArg,
96572 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
96573 zDb, pTab->zName, x.nKey);
96581 ** of table - intkey or index. This opcode is used as part of copying
96594 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
96595 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
96597 pDest = p->apCsr[pOp->p1];
96598 pSrc = p->apCsr[pOp->p2];
96599 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
96600 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
96612 ** the next Next instruction will be a no-op. As a result, in this case
96626 ** P1 must not be pseudo-table. It has to be a real table with
96630 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
96632 ** this case. Specifically, if one is configured, the pre-update hook is
96633 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
96646 opflags = pOp->p2;
96647 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96648 pC = p->apCsr[pOp->p1];
96650 assert( pC->eCurType==CURTYPE_BTREE );
96651 assert( pC->uc.pCursor!=0 );
96652 assert( pC->deferredMoveto==0 );
96656 if( pOp->p4type==P4_TABLE
96657 && HasRowid(pOp->p4.pTab)
96658 && pOp->p5==0
96659 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
96662 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
96664 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96665 assert( CORRUPT_DB || pC->movetoTarget==iKey );
96669 /* If the update-hook or pre-update-hook will be invoked, set zDb to
96674 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
96675 assert( pC->iDb>=0 );
96676 assert( pOp->p4.pTab!=0 );
96677 zDb = db->aDb[pC->iDb].zDbSName;
96678 pTab = pOp->p4.pTab;
96679 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
96680 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96688 /* Invoke the pre-update-hook if required. */
96689 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
96690 if( db->xPreUpdateCallback && pTab ){
96693 || (aMem[pOp->p3].flags & MEM_Int)
96697 zDb, pTab, pC->movetoTarget,
96698 pOp->p3, -1
96705 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
96710 if( p->pFrame==0 ){
96711 if( pC->isEphemeral==0
96712 && (pOp->p5 & OPFLAG_AUXDELETE)==0
96713 && (pC->wrFlag & OPFLAG_FORDELETE)==0
96717 if( pOp->p2 & OPFLAG_NCHANGE ){
96718 nExtraDelete--;
96723 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
96724 pC->cacheStatus = CACHE_STALE;
96725 pC->seekResult = 0;
96728 /* Invoke the update-hook if required. */
96730 p->nChange++;
96731 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
96732 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
96733 pC->movetoTarget);
96734 assert( pC->iDb>=0 );
96748 sqlite3VdbeSetChanges(db, p->nChange);
96749 p->nChange = 0;
96773 pC = p->apCsr[pOp->p1];
96775 assert( pOp->p4type==P4_INT32 );
96776 pIn3 = &aMem[pOp->p3];
96777 nKeyCol = pOp->p4.i;
96793 ** a register that is the source for a pseudo-table cursor created using
96794 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
96801 pOut = &aMem[pOp->p2];
96802 pC = p->apCsr[pOp->p1];
96805 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
96806 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96808 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
96825 ** of a real table, not a pseudo-table.
96829 ** register will be invalidated as soon as the cursor moves - including
96847 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96848 pC = p->apCsr[pOp->p1];
96850 assert( pC->eCurType==CURTYPE_BTREE );
96852 assert( pC->nullRow==0 );
96853 assert( pC->uc.pCursor!=0 );
96854 pCrsr = pC->uc.pCursor;
96864 assert( pC->deferredMoveto==0 );
96868 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
96874 if( !pOp->p3 ) Deephemeralize(pOut);
96876 REGISTER_TRACE(pOp->p2, pOut);
96897 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96898 pC = p->apCsr[pOp->p1];
96900 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
96901 if( pC->nullRow ){
96902 pOut->flags = MEM_Null;
96904 }else if( pC->deferredMoveto ){
96905 v = pC->movetoTarget;
96907 }else if( pC->eCurType==CURTYPE_VTAB ){
96908 assert( pC->uc.pVCur!=0 );
96909 pVtab = pC->uc.pVCur->pVtab;
96910 pModule = pVtab->pModule;
96911 assert( pModule->xRowid );
96912 rc = pModule->xRowid(pC->uc.pVCur, &v);
96917 assert( pC->eCurType==CURTYPE_BTREE );
96918 assert( pC->uc.pCursor!=0 );
96921 if( pC->nullRow ){
96922 pOut->flags = MEM_Null;
96925 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96927 pOut->u.i = v;
96938 ** pseudo-cursor that always returns NULL for every column.
96943 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96944 pC = p->apCsr[pOp->p1];
96947 ** pseudo-cursor that always gives null rows. */
96948 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
96950 pC->seekResult = 0;
96951 pC->isTable = 1;
96952 pC->noReuse = 1;
96953 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
96955 pC->nullRow = 1;
96956 pC->cacheStatus = CACHE_STALE;
96957 if( pC->eCurType==CURTYPE_BTREE ){
96958 assert( pC->uc.pCursor!=0 );
96959 sqlite3BtreeClearCursor(pC->uc.pCursor);
96962 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
96995 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96996 pC = p->apCsr[pOp->p1];
96998 assert( pC->eCurType==CURTYPE_BTREE );
96999 pCrsr = pC->uc.pCursor;
97003 pC->seekOp = pOp->opcode;
97005 if( pOp->opcode==OP_SeekEnd ){
97006 assert( pOp->p2==0 );
97007 pC->seekResult = -1;
97013 pC->nullRow = (u8)res;
97014 pC->deferredMoveto = 0;
97015 pC->cacheStatus = CACHE_STALE;
97017 if( pOp->p2>0 ){
97035 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97036 pC = p->apCsr[pOp->p1];
97038 pCrsr = pC->uc.pCursor;
97044 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
97077 sqlite3_search_count--;
97079 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
97100 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97101 assert( pOp->p5==0 );
97102 pC = p->apCsr[pOp->p1];
97104 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
97107 pC->seekOp = OP_Rewind;
97112 assert( pC->eCurType==CURTYPE_BTREE );
97113 pCrsr = pC->uc.pCursor;
97116 pC->deferredMoveto = 0;
97117 pC->cacheStatus = CACHE_STALE;
97120 pC->nullRow = (u8)res;
97121 assert( pOp->p2>0 && pOp->p2<p->nOp );
97138 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
97147 ** number P5-1 in the prepared statement is incremented.
97163 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
97172 ** number P5-1 in the prepared statement is incremented.
97184 pC = p->apCsr[pOp->p1];
97190 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97191 assert( pOp->p5==0
97192 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
97193 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
97194 pC = p->apCsr[pOp->p1];
97196 assert( pC->deferredMoveto==0 );
97197 assert( pC->eCurType==CURTYPE_BTREE );
97198 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
97199 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
97200 || pC->seekOp==OP_NullRow);
97201 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
97205 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97206 assert( pOp->p5==0
97207 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
97208 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
97209 pC = p->apCsr[pOp->p1];
97211 assert( pC->deferredMoveto==0 );
97212 assert( pC->eCurType==CURTYPE_BTREE );
97213 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
97214 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
97215 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
97216 || pC->seekOp==OP_IfNoHope);
97217 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
97220 pC->cacheStatus = CACHE_STALE;
97223 pC->nullRow = 0;
97224 p->aCounter[pOp->p5]++;
97232 pC->nullRow = 1;
97248 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
97268 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97269 pC = p->apCsr[pOp->p1];
97273 pIn2 = &aMem[pOp->p2];
97274 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
97275 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
97276 assert( pC->eCurType==CURTYPE_BTREE );
97277 assert( pC->isTable==0 );
97280 x.nKey = pIn2->n;
97281 x.pKey = pIn2->z;
97282 x.aMem = aMem + pOp->p3;
97283 x.nMem = (u16)pOp->p4.i;
97284 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
97285 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
97286 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
97288 assert( pC->deferredMoveto==0 );
97289 pC->cacheStatus = CACHE_STALE;
97304 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97305 pC = p->apCsr[pOp->p1];
97309 pIn2 = &aMem[pOp->p2];
97310 assert( pIn2->flags & MEM_Blob );
97311 assert( pC->isTable==0 );
97332 ** this (self-correcting and non-critical) error if in writable_schema mode.
97340 assert( pOp->p3>0 );
97341 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
97342 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97343 pC = p->apCsr[pOp->p1];
97345 assert( pC->eCurType==CURTYPE_BTREE );
97347 pCrsr = pC->uc.pCursor;
97349 r.pKeyInfo = pC->pKeyInfo;
97350 r.nField = (u16)pOp->p3;
97352 r.aMem = &aMem[pOp->p2];
97358 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
97362 assert( pC->deferredMoveto==0 );
97363 pC->cacheStatus = CACHE_STALE;
97364 pC->seekResult = 0;
97381 ** is non-zero, then reading column a(i)-1 from cursor P3 is
97402 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97403 pC = p->apCsr[pOp->p1];
97405 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
97406 assert( pC->uc.pCursor!=0 );
97407 assert( pC->isTable==0 || IsNullCursor(pC) );
97408 assert( pC->deferredMoveto==0 );
97409 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
97420 if( !pC->nullRow ){
97422 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
97426 if( pOp->opcode==OP_DeferredSeek ){
97427 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
97428 pTabCur = p->apCsr[pOp->p3];
97430 assert( pTabCur->eCurType==CURTYPE_BTREE );
97431 assert( pTabCur->uc.pCursor!=0 );
97432 assert( pTabCur->isTable );
97433 pTabCur->nullRow = 0;
97434 pTabCur->movetoTarget = rowid;
97435 pTabCur->deferredMoveto = 1;
97436 pTabCur->cacheStatus = CACHE_STALE;
97437 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
97438 assert( !pTabCur->isEphemeral );
97439 pTabCur->ub.aAltMap = pOp->p4.ai;
97440 assert( !pC->isEphemeral );
97441 pTabCur->pAltCursor = pC;
97444 pOut->u.i = rowid;
97447 assert( pOp->opcode==OP_IdxRowid );
97448 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
97457 ** already occurred, this instruction is a no-op.
97462 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97463 pC = p->apCsr[pOp->p1];
97464 if( pC->deferredMoveto ){
97523 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97524 pC = p->apCsr[pOp->p1];
97526 assert( pC->isOrdered );
97527 assert( pC->eCurType==CURTYPE_BTREE );
97528 assert( pC->uc.pCursor!=0);
97529 assert( pC->deferredMoveto==0 );
97530 assert( pOp->p4type==P4_INT32 );
97531 r.pKeyInfo = pC->pKeyInfo;
97532 r.nField = (u16)pOp->p4.i;
97533 if( pOp->opcode<OP_IdxLT ){
97534 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
97535 r.default_rc = -1;
97537 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
97540 r.aMem = &aMem[pOp->p3];
97546 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
97557 assert( pC->eCurType==CURTYPE_BTREE );
97558 pCur = pC->uc.pCursor;
97576 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
97577 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
97578 res = -res;
97580 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
97601 ** value of the root page that moved - its value before the move occurred -
97612 ** and non-autovacuum modes.
97621 assert( p->readOnly==0 );
97622 assert( pOp->p1>1 );
97624 pOut->flags = MEM_Null;
97625 if( db->nVdbeRead > db->nVDestroy+1 ){
97627 p->errorAction = OE_Abort;
97630 iDb = pOp->p3;
97631 assert( DbMaskTest(p->btreeMask, iDb) );
97633 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
97634 pOut->flags = MEM_Int;
97635 pOut->u.i = iMoved;
97639 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
97659 ** If the P3 value is non-zero, then the row change count is incremented
97671 assert( p->readOnly==0 );
97672 assert( DbMaskTest(p->btreeMask, pOp->p2) );
97673 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
97674 if( pOp->p3 ){
97675 p->nChange += nChange;
97676 if( pOp->p3>0 ){
97677 assert( memIsValid(&aMem[pOp->p3]) );
97678 memAboutToChange(p, &aMem[pOp->p3]);
97679 aMem[pOp->p3].u.i += nChange;
97697 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97698 pC = p->apCsr[pOp->p1];
97701 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
97703 assert( pC->eCurType==CURTYPE_BTREE );
97704 assert( pC->isEphemeral );
97705 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
97714 ** Allocate a new b-tree in the main database file if P1==0 or in the
97718 ** The root page number of the new b-tree is stored in register P2.
97727 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
97728 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97729 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97730 assert( p->readOnly==0 );
97731 pDb = &db->aDb[pOp->p1];
97732 assert( pDb->pBt!=0 );
97733 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
97735 pOut->u.i = pgno;
97745 db->nSqlExec++;
97746 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
97747 db->nSqlExec--;
97759 ** then runs the new virtual machine. It is thus a re-entrant opcode.
97772 for(iDb=0; iDb<db->nDb; iDb++){
97773 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
97777 iDb = pOp->p1;
97778 assert( iDb>=0 && iDb<db->nDb );
97780 || db->mallocFailed
97781 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
97784 if( pOp->p4.z==0 ){
97785 sqlite3SchemaClear(db->aDb[iDb].pSchema);
97786 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
97787 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
97788 db->mDbFlags |= DBFLAG_SchemaChange;
97789 p->expired = 0;
97796 initData.pzErrMsg = &p->zErrMsg;
97798 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
97801 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
97805 assert( db->init.busy==0 );
97806 db->init.busy = 1;
97809 assert( !db->mallocFailed );
97813 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
97819 db->init.busy = 0;
97840 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97841 rc = sqlite3AnalysisLoad(db, pOp->p1);
97849 ** Remove the internal (in-memory) data structures that describe
97857 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
97863 ** Remove the internal (in-memory) data structures that describe
97871 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
97877 ** Remove the internal (in-memory) data structures that describe
97885 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
97917 assert( p->bIsReader );
97918 nRoot = pOp->p2;
97919 aRoot = pOp->p4.ai;
97922 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
97923 pnErr = &aMem[pOp->p3];
97924 assert( (pnErr->flags & MEM_Int)!=0 );
97925 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
97926 pIn1 = &aMem[pOp->p1];
97927 assert( pOp->p5<db->nDb );
97928 assert( DbMaskTest(p->btreeMask, pOp->p5) );
97929 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
97930 (int)pnErr->u.i+1, &nErr);
97937 pnErr->u.i -= nErr-1;
97938 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
97955 pIn1 = &aMem[pOp->p1];
97956 pIn2 = &aMem[pOp->p2];
97957 assert( (pIn2->flags & MEM_Int)!=0 );
97958 if( (pIn1->flags & MEM_Blob)==0 ){
97962 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
97977 pIn1 = &aMem[pOp->p1];
97978 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
97979 if( (pIn1->flags & MEM_Blob)==0
97980 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
97989 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
97997 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
98006 ** must have P4==0, the final set must have P4==-1, and for all other sets
98011 ** (b) when P4==-1 there is no need to insert the value, as it will
98021 pIn1 = &aMem[pOp->p1];
98022 pIn3 = &aMem[pOp->p3];
98023 iSet = pOp->p4.i;
98024 assert( pIn3->flags&MEM_Int );
98029 if( (pIn1->flags & MEM_Blob)==0 ){
98033 assert( pOp->p4type==P4_INT32 );
98034 assert( iSet==-1 || iSet>=0 );
98036 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
98041 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
98054 ** cell in an array of values used as arguments to the sub-program. P2
98055 ** contains the address to jump to if the sub-program throws an IGNORE
98057 ** of a memory cell in this (the parent) VM that is used to allocate the
98058 ** memory required by the sub-vdbe at runtime.
98060 ** P4 is a pointer to the VM containing the trigger program.
98062 ** If P5 is non-zero, then recursive program invocation is enabled.
98065 int nMem; /* Number of memory registers for sub-program */
98066 int nByte; /* Bytes of runtime space required for sub-program */
98071 SubProgram *pProgram; /* Sub-program to execute */
98074 pProgram = pOp->p4.pProgram;
98075 pRt = &aMem[pOp->p3];
98076 assert( pProgram->nOp>0 );
98079 ** disabled for backwards compatibility (p5 is set if this sub-program
98089 if( pOp->p5 ){
98090 t = pProgram->token;
98091 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
98095 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
98105 if( (pRt->flags&MEM_Blob)==0 ){
98111 nMem = pProgram->nMem + pProgram->nCsr;
98113 if( pProgram->nCsr==0 ) nMem++;
98116 + pProgram->nCsr * sizeof(VdbeCursor*)
98117 + (pProgram->nOp + 7)/8;
98123 pRt->flags = MEM_Blob|MEM_Dyn;
98124 pRt->z = (char*)pFrame;
98125 pRt->n = nByte;
98126 pRt->xDel = sqlite3VdbeFrameMemDel;
98128 pFrame->v = p;
98129 pFrame->nChildMem = nMem;
98130 pFrame->nChildCsr = pProgram->nCsr;
98131 pFrame->pc = (int)(pOp - aOp);
98132 pFrame->aMem = p->aMem;
98133 pFrame->nMem = p->nMem;
98134 pFrame->apCsr = p->apCsr;
98135 pFrame->nCursor = p->nCursor;
98136 pFrame->aOp = p->aOp;
98137 pFrame->nOp = p->nOp;
98138 pFrame->token = pProgram->token;
98140 pFrame->anExec = p->anExec;
98143 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
98146 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
98148 pMem->flags = MEM_Undefined;
98149 pMem->db = db;
98152 pFrame = (VdbeFrame*)pRt->z;
98153 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
98154 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
98155 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
98156 assert( pProgram->nCsr==pFrame->nChildCsr );
98157 assert( (int)(pOp - aOp)==pFrame->pc );
98160 p->nFrame++;
98161 pFrame->pParent = p->pFrame;
98162 pFrame->lastRowid = db->lastRowid;
98163 pFrame->nChange = p->nChange;
98164 pFrame->nDbChange = p->db->nChange;
98165 assert( pFrame->pAuxData==0 );
98166 pFrame->pAuxData = p->pAuxData;
98167 p->pAuxData = 0;
98168 p->nChange = 0;
98169 p->pFrame = pFrame;
98170 p->aMem = aMem = VdbeFrameMem(pFrame);
98171 p->nMem = pFrame->nChildMem;
98172 p->nCursor = (u16)pFrame->nChildCsr;
98173 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
98174 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
98175 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
98176 p->aOp = aOp = pProgram->aOp;
98177 p->nOp = pProgram->nOp;
98179 p->anExec = 0;
98186 for(i=0; i<p->nMem; i++){
98187 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
98192 pOp = &aOp[-1];
98198 ** This opcode is only ever present in sub-programs called via the
98212 pFrame = p->pFrame;
98213 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
98225 ** If P1 is non-zero, the database constraint counter is incremented
98230 if( db->flags & SQLITE_DeferFKs ){
98231 db->nDeferredImmCons += pOp->p2;
98232 }else if( pOp->p1 ){
98233 db->nDeferredCons += pOp->p2;
98235 p->nFkConstraint += pOp->p2;
98243 ** This opcode tests if a foreign key constraint-counter is currently zero.
98247 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
98249 ** zero, the jump is taken if the statement constraint-counter is zero
98253 if( pOp->p1 ){
98254 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
98255 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
98257 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
98258 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
98268 ** P1 is a register in the root frame of this VM (the root frame is
98270 ** within a sub-program). Set the value of register P1 to the maximum of
98278 if( p->pFrame ){
98279 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
98280 pIn1 = &pFrame->aMem[pOp->p1];
98282 pIn1 = &aMem[pOp->p1];
98286 pIn2 = &aMem[pOp->p2];
98288 if( pIn1->u.i<pIn2->u.i){
98289 pIn1->u.i = pIn2->u.i;
98296 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
98306 pIn1 = &aMem[pOp->p1];
98307 assert( pIn1->flags&MEM_Int );
98308 VdbeBranchTaken( pIn1->u.i>0, 2);
98309 if( pIn1->u.i>0 ){
98310 pIn1->u.i -= pOp->p3;
98317 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
98330 ** and r[P2] is set to -1.
98336 pIn1 = &aMem[pOp->p1];
98337 pIn3 = &aMem[pOp->p3];
98339 assert( pIn1->flags & MEM_Int );
98340 assert( pIn3->flags & MEM_Int );
98341 x = pIn1->u.i;
98342 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
98350 pOut->u.i = -1;
98352 pOut->u.i = x;
98358 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
98362 ** If it is non-zero (negative or positive) and then also jump to P2.
98366 pIn1 = &aMem[pOp->p1];
98367 assert( pIn1->flags&MEM_Int );
98368 VdbeBranchTaken(pIn1->u.i<0, 2);
98369 if( pIn1->u.i ){
98370 if( pIn1->u.i>0 ) pIn1->u.i--;
98377 ** Synopsis: if (--r[P1])==0 goto P2
98384 if( pOp->p5&0x80 ){
98385 if( p->pSharedBlock!=NULL ){
98386 if( p->totalRows < p->startPos || p->blockFull ){
98387 pIn1 = &aMem[pOp->p1];
98388 assert( pIn1->flags&MEM_Int );
98389 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
98390 VdbeBranchTaken(pIn1->u.i==-1, 2);
98391 if( pIn1->u.i==-1 ) goto jump_to_p2;
98397 pIn1 = &aMem[pOp->p1];
98398 assert( pIn1->flags&MEM_Int );
98399 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
98400 VdbeBranchTaken(pIn1->u.i==0, 2);
98401 if( pIn1->u.i==0 ) goto jump_to_p2;
98450 assert( pOp->p4type==P4_FUNCDEF );
98451 n = pOp->p5;
98452 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98453 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
98454 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
98456 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
98458 pCtx->pMem = 0;
98459 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
98460 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
98461 pCtx->pFunc = pOp->p4.pFunc;
98462 pCtx->iOp = (int)(pOp - aOp);
98463 pCtx->pVdbe = p;
98464 pCtx->skipFlag = 0;
98465 pCtx->isError = 0;
98466 pCtx->enc = encoding;
98467 pCtx->argc = n;
98468 pOp->p4type = P4_FUNCCTX;
98469 pOp->p4.pCtx = pCtx;
98472 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
98474 pOp->opcode = OP_AggStep1;
98483 assert( pOp->p4type==P4_FUNCCTX );
98484 pCtx = pOp->p4.pCtx;
98485 pMem = &aMem[pOp->p3];
98488 if( pOp->p1 ){
98491 assert( pMem->uTemp==0x1122e0e3 );
98494 pMem->uTemp = 0x1122e0e3;
98502 if( pCtx->pMem != pMem ){
98503 pCtx->pMem = pMem;
98504 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
98508 for(i=0; i<pCtx->argc; i++){
98509 assert( memIsValid(pCtx->argv[i]) );
98510 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
98514 pMem->n++;
98515 assert( pCtx->pOut->flags==MEM_Null );
98516 assert( pCtx->isError==0 );
98517 assert( pCtx->skipFlag==0 );
98519 if( pOp->p1 ){
98520 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
98523 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
98525 if( pCtx->isError ){
98526 if( pCtx->isError>0 ){
98527 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
98528 rc = pCtx->isError;
98530 if( pCtx->skipFlag ){
98531 assert( pOp[-1].opcode==OP_CollSeq );
98532 i = pOp[-1].p1;
98534 pCtx->skipFlag = 0;
98536 sqlite3VdbeMemRelease(pCtx->pOut);
98537 pCtx->pOut->flags = MEM_Null;
98538 pCtx->isError = 0;
98541 assert( pCtx->pOut->flags==MEM_Null );
98542 assert( pCtx->skipFlag==0 );
98575 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
98576 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
98577 pMem = &aMem[pOp->p1];
98578 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
98580 if( pOp->p3 ){
98581 memAboutToChange(p, &aMem[pOp->p3]);
98582 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
98583 pMem = &aMem[pOp->p3];
98587 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
98602 ** Checkpoint database P1. This is a no-op if P1 is not currently in
98609 ** mem[P3+2] are initialized to -1.
98616 assert( p->readOnly==0 );
98618 aRes[1] = aRes[2] = -1;
98619 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
98620 || pOp->p2==SQLITE_CHECKPOINT_FULL
98621 || pOp->p2==SQLITE_CHECKPOINT_RESTART
98622 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
98624 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
98630 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
98647 ** Write a string containing the final journal-mode to register P2.
98659 eNew = pOp->p3;
98668 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98669 assert( p->readOnly==0 );
98671 pBt = db->aDb[pOp->p1].pBt;
98686 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
98694 if( !db->autoCommit || db->nVdbeRead>1 ){
98705 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
98733 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
98734 pOut->z = (char *)sqlite3JournalModename(eNew);
98735 pOut->n = sqlite3Strlen30(pOut->z);
98736 pOut->enc = SQLITE_UTF8;
98754 assert( p->readOnly==0 );
98755 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
98756 pOp->p2 ? &aMem[pOp->p2] : 0);
98772 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98773 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98774 assert( p->readOnly==0 );
98775 pBt = db->aDb[pOp->p1].pBt;
98794 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
98804 assert( pOp->p2==0 || pOp->p2==1 );
98805 if( !pOp->p1 ){
98806 sqlite3ExpirePreparedStatements(db, pOp->p2);
98808 p->expired = pOp->p2+1;
98820 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98821 pC = p->apCsr[pOp->p1];
98823 assert( pC->eCurType==CURTYPE_BTREE );
98824 sqlite3BtreeCursorPin(pC->uc.pCursor);
98835 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98836 pC = p->apCsr[pOp->p1];
98838 assert( pC->eCurType==CURTYPE_BTREE );
98839 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
98848 ** the shared-cache feature is enabled.
98854 ** P2 contains the root-page of the table to lock.
98860 u8 isWriteLock = (u8)pOp->p3;
98861 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
98862 int p1 = pOp->p1;
98863 assert( p1>=0 && p1<db->nDb );
98864 assert( DbMaskTest(p->btreeMask, p1) );
98866 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
98869 const char *z = pOp->p4.z;
98891 pVTab = pOp->p4.pVtab;
98893 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
98913 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
98914 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
98915 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
98918 assert( zTab || db->mallocFailed );
98920 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
98935 db->nVDestroy++;
98936 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
98937 db->nVDestroy--;
98938 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
98957 assert( p->bIsReader );
98960 pVtab = pOp->p4.pVtab->pVtab;
98961 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
98965 pModule = pVtab->pModule;
98966 rc = pModule->xOpen(pVtab, &pVCur);
98971 pVCur->pVtab = pVtab;
98974 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
98976 pCur->uc.pVCur = pVCur;
98977 pVtab->nRef++;
98979 assert( db->mallocFailed );
98980 pModule->xClose(pVCur);
99002 pC = p->apCsr[pOp->p1];
99005 pRhs->pCsr = pC->uc.pCursor;
99006 pRhs->pOut = &aMem[pOp->p3];
99008 pOut->flags = MEM_Null;
99048 pQuery = &aMem[pOp->p3];
99050 pCur = p->apCsr[pOp->p1];
99052 REGISTER_TRACE(pOp->p3, pQuery);
99054 assert( pCur->eCurType==CURTYPE_VTAB );
99055 pVCur = pCur->uc.pVCur;
99056 pVtab = pVCur->pVtab;
99057 pModule = pVtab->pModule;
99060 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
99061 nArg = (int)pArgc->u.i;
99062 iQuery = (int)pQuery->u.i;
99065 apArg = p->apArg;
99069 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
99072 res = pModule->xEof(pVCur);
99073 pCur->nullRow = 0;
99084 ** Store in register P3 the value of the P2-th column of
99085 ** the current row of the virtual-table of cursor P1.
99101 VdbeCursor *pCur = p->apCsr[pOp->p1];
99103 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
99104 pDest = &aMem[pOp->p3];
99106 if( pCur->nullRow ){
99110 assert( pCur->eCurType==CURTYPE_VTAB );
99111 pVtab = pCur->uc.pVCur->pVtab;
99112 pModule = pVtab->pModule;
99113 assert( pModule->xColumn );
99117 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
99118 if( pOp->p5 & OPFLAG_NOCHNG ){
99120 pDest->flags = MEM_Null|MEM_Zero;
99121 pDest->u.nZero = 0;
99125 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
99132 REGISTER_TRACE(pOp->p3, pDest);
99153 pCur = p->apCsr[pOp->p1];
99155 assert( pCur->eCurType==CURTYPE_VTAB );
99156 if( pCur->nullRow ){
99159 pVtab = pCur->uc.pVCur->pVtab;
99160 pModule = pVtab->pModule;
99161 assert( pModule->xNext );
99169 rc = pModule->xNext(pCur->uc.pVCur);
99172 res = pModule->xEof(pCur->uc.pVCur);
99194 isLegacy = (db->flags & SQLITE_LegacyAlter);
99195 db->flags |= SQLITE_LegacyAlter;
99196 pVtab = pOp->p4.pVtab->pVtab;
99197 pName = &aMem[pOp->p1];
99198 assert( pVtab->pModule->xRename );
99200 assert( p->readOnly==0 );
99201 REGISTER_TRACE(pOp->p1, pName);
99202 assert( pName->flags & MEM_Str );
99203 testcase( pName->enc==SQLITE_UTF8 );
99204 testcase( pName->enc==SQLITE_UTF16BE );
99205 testcase( pName->enc==SQLITE_UTF16LE );
99208 rc = pVtab->pModule->xRename(pVtab, pName->z);
99209 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
99211 p->expired = 0;
99224 ** invocation. The value in register (P3+P2-1) corresponds to the
99254 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
99255 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
99257 assert( p->readOnly==0 );
99258 if( db->mallocFailed ) goto no_mem;
99260 pVtab = pOp->p4.pVtab->pVtab;
99261 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
99265 pModule = pVtab->pModule;
99266 nArg = pOp->p2;
99267 assert( pOp->p4type==P4_VTAB );
99268 if( ALWAYS(pModule->xUpdate) ){
99269 u8 vtabOnConflict = db->vtabOnConflict;
99270 apArg = p->apArg;
99271 pX = &aMem[pOp->p3];
99278 db->vtabOnConflict = pOp->p5;
99279 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
99280 db->vtabOnConflict = vtabOnConflict;
99282 if( rc==SQLITE_OK && pOp->p1 ){
99283 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
99284 db->lastRowid = rowid;
99286 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
99287 if( pOp->p5==OE_Ignore ){
99290 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
99293 p->nChange++;
99308 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
99328 pBt = db->aDb[pOp->p1].pBt;
99330 if( pOp->p3 ){
99332 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
99334 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
99349 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
99368 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
99377 ** purely non-deterministic. Some built-in date/time functions can be
99378 ** either determinitic of non-deterministic, depending on their arguments.
99379 ** When those function are used in a non-deterministic way, they will check
99390 assert( pOp->p4type==P4_FUNCCTX );
99391 pCtx = pOp->p4.pCtx;
99397 pOut = &aMem[pOp->p3];
99398 if( pCtx->pOut != pOut ){
99399 pCtx->pVdbe = p;
99400 pCtx->pOut = pOut;
99401 pCtx->enc = encoding;
99402 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
99404 assert( pCtx->pVdbe==p );
99408 for(i=0; i<pCtx->argc; i++){
99409 assert( memIsValid(pCtx->argv[i]) );
99410 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
99414 assert( pCtx->isError==0 );
99415 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
99418 if( pCtx->isError ){
99419 if( pCtx->isError>0 ){
99421 rc = pCtx->isError;
99423 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
99424 pCtx->isError = 0;
99428 assert( (pOut->flags&MEM_Str)==0
99429 || pOut->enc==encoding
99430 || db->mallocFailed );
99433 REGISTER_TRACE(pOp->p3, pOut);
99444 pIn1 = &aMem[pOp->p1];
99445 pIn1->flags &= ~MEM_Subtype;
99458 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
99459 pIn1 = &aMem[pOp->p1];
99460 assert( pIn1->flags & MEM_Blob );
99461 assert( pIn1->n>0 );
99464 if( db->flags&SQLITE_VdbeTrace ){
99466 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
99469 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
99472 h %= pIn1->n;
99473 pIn1->z[h/8] |= 1<<(h&7);
99489 ** false positive - if the jump is taken when it should fall through.
99494 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
99495 pIn1 = &aMem[pOp->p1];
99496 assert( (pIn1->flags & MEM_Blob)!=0 );
99497 assert( pIn1->n >= 1 );
99500 if( db->flags&SQLITE_VdbeTrace ){
99502 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
99505 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
99508 h %= pIn1->n;
99509 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
99511 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
99514 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
99534 ** the UTF-8 string contained in P4 is emitted on the trace callback.
99553 ** The "--" string is broken up to prevent false-positives with srcck1.c.
99556 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
99558 ** using the X argument when X begins with "--" and invoking
99561 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
99564 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
99567 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
99568 && p->minWriteFileFormat!=254 /* tag-20220401a */
99569 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
99572 if( db->mTrace & SQLITE_TRACE_LEGACY ){
99574 db->trace.xLegacy(db->pTraceArg, z);
99578 if( db->nVdbeExec>1 ){
99579 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
99580 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
99583 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
99587 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
99590 for(j=0; j<db->nDb; j++){
99591 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
99592 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
99597 if( (db->flags & SQLITE_SqlTrace)!=0
99598 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
99600 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
99604 assert( pOp->p2>0 );
99605 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
99606 if( pOp->opcode==OP_Trace ) break;
99607 for(i=1; i<p->nOp; i++){
99608 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
99610 pOp->p1 = 0;
99612 pOp->p1++;
99613 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
99623 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
99628 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99629 assert( pOp->p4type==P4_EXPR );
99630 pC = p->apCsr[pOp->p1];
99632 assert( pC->eCurType==CURTYPE_BTREE );
99633 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
99634 pOp->p4.pExpr, aMem);
99691 assert( pOp->p1>0 );
99692 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
99693 pMem = &aMem[pOp->p1];
99694 constMask = pOp->p3;
99695 for(i=0; i<pOp->p2; i++, pMem++){
99697 pMem->pScopyFrom = 0;
99698 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
99714 ** the same as a no-op. This opcodesnever appears in a real VM program.
99717 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
99724 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
99733 if( endTime>start ) pOrigOp->cycles += endTime - start;
99734 pOrigOp->cnt++;
99744 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
99747 if( db->flags & SQLITE_VdbeTrace ){
99748 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
99751 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
99754 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
99771 if( db->mallocFailed ){
99778 if( db->flags & SQLITE_VdbeTrace ){
99779 const char *zTrace = p->zSql;
99786 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
99789 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
99792 p->rc = rc;
99796 (int)(pOp - aOp), p->zSql, p->zErrMsg);
99797 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
99799 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
99800 db->flags |= SQLITE_CorruptRdOnly;
99804 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
99812 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
99813 nProgressLimit += db->nProgressOps;
99814 if( db->xProgress(db->pProgressArg) ){
99821 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
99824 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
99848 assert( AtomicLoad(&db->u1.isInterrupted) );
99894 ** the b-tree cursor associated with blob handle p to point to row iRow.
99905 ** If an error does occur, then the b-tree cursor is closed. All subsequent
99912 Vdbe *v = (Vdbe *)p->pStmt;
99917 v->aMem[1].flags = MEM_Int;
99918 v->aMem[1].u.i = iRow;
99924 if( v->pc>4 ){
99925 v->pc = 4;
99926 assert( v->aOp[v->pc].opcode==OP_NotExists );
99929 rc = sqlite3_step(p->pStmt);
99932 VdbeCursor *pC = v->apCsr[0];
99935 assert( pC->eCurType==CURTYPE_BTREE );
99936 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
99937 testcase( pC->nHdrParsed==p->iCol );
99938 testcase( pC->nHdrParsed==p->iCol+1 );
99940 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
99944 sqlite3_finalize(p->pStmt);
99945 p->pStmt = 0;
99947 p->iOffset = pC->aType[p->iCol + pC->nField];
99948 p->nByte = sqlite3VdbeSerialTypeLen(type);
99949 p->pCsr = pC->uc.pCursor;
99950 sqlite3BtreeIncrblobCursor(p->pCsr);
99956 }else if( p->pStmt ){
99957 rc = sqlite3_finalize(p->pStmt);
99958 p->pStmt = 0;
99960 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
99963 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
99983 int wrFlag, /* True -> read/write access, false -> read-only */
99987 int iCol; /* Index of zColumn in row-record */
100007 sqlite3_mutex_enter(db->mutex);
100042 pBlob->pTab = pTab;
100043 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
100046 for(iCol=0; iCol<pTab->nCol; iCol++) {
100047 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
100051 if( iCol==pTab->nCol ){
100066 if( db->flags&SQLITE_ForeignKeys ){
100073 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
100075 for(j=0; j<pFKey->nCol; j++){
100076 if( pFKey->aCol[j].iFrom==iCol ){
100083 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
100085 for(j=0; j<pIdx->nKeyCol; j++){
100087 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
100101 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
100102 assert( pBlob->pStmt || db->mallocFailed );
100103 if( pBlob->pStmt ){
100107 ** of writing code to use the b-tree layer directly is that the
100112 ** Code external to the Vdbe then "borrows" the b-tree cursor and
100117 ** which closes the b-tree cursor and (possibly) commits the
100130 Vdbe *v = (Vdbe *)pBlob->pStmt;
100131 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
100135 pTab->pSchema->schema_cookie,
100136 pTab->pSchema->iGeneration);
100138 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
100144 if( db->mallocFailed==0 ){
100151 aOp[0].p2 = pTab->tnum;
100153 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
100155 if( db->mallocFailed==0 ){
100159 ** parameter of the other to pTab->tnum. */
100161 aOp[1].p2 = pTab->tnum;
100172 aOp[1].p4.i = pTab->nCol+1;
100173 aOp[3].p2 = pTab->nCol;
100182 pBlob->iCol = iCol;
100183 pBlob->db = db;
100185 if( db->mallocFailed ){
100194 if( rc==SQLITE_OK && db->mallocFailed==0 ){
100197 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
100204 sqlite3_mutex_leave(db->mutex);
100218 sqlite3_stmt *pStmt = p->pStmt;
100219 db = p->db;
100220 sqlite3_mutex_enter(db->mutex);
100222 sqlite3_mutex_leave(db->mutex);
100246 db = p->db;
100247 sqlite3_mutex_enter(db->mutex);
100248 v = (Vdbe*)p->pStmt;
100250 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
100254 /* If there is no statement handle, then the blob-handle has
100260 ** returned, clean-up the statement handle.
100262 assert( db == v->db );
100263 sqlite3BtreeEnterCursor(p->pCsr);
100266 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
100267 /* If a pre-update hook is registered and this is a write cursor,
100270 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
100277 ** using the incremental-blob API, this works. For the sessions module
100281 iKey = sqlite3BtreeIntegerKey(p->pCsr);
100282 assert( v->apCsr[0]!=0 );
100283 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
100285 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
100290 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
100291 sqlite3BtreeLeaveCursor(p->pCsr);
100294 p->pStmt = 0;
100296 v->rc = rc;
100301 sqlite3_mutex_leave(db->mutex);
100327 return (p && p->pStmt) ? p->nByte : 0;
100346 db = p->db;
100347 sqlite3_mutex_enter(db->mutex);
100349 if( p->pStmt==0 ){
100350 /* If there is no statement handle, then the blob-handle has
100356 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
100366 assert( rc==SQLITE_OK || p->pStmt==0 );
100367 sqlite3_mutex_leave(db->mutex);
100376 ** 2011-07-09
100391 ** The VdbeSorter object implements a multi-threaded external merge sort
100395 ** Here is the (internal, non-API) interface between this module and the
100448 ** an in-memory merge sort. In this case, no temporary files are required
100455 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
100456 ** of PMAs may be created by merging existing PMAs together - for example
100457 ** merging two or more level-0 PMAs together creates a level-1 PMA.
100461 ** page-cache of the main database. Specifically, the threshold is set to
100465 ** If the sorter is running in single-threaded mode, then all PMAs generated
100467 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
100474 ** The sorter is running in multi-threaded mode if (a) the library was built
100475 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
100484 ** sorter is running in single-threaded mode, then these PMAs are merged
100489 ** Or, if running in multi-threaded mode, then a background thread is
100499 ** Rewind() is called, then a hierarchy of incremental-merges is used.
100505 ** If running in multi-threaded mode and there are more than
100518 ** characteristics of the sorter in multi-threaded mode.
100525 ** Hard-coded maximum amount of data to accumulate in memory before flushing
100538 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
100540 typedef struct SorterList SorterList; /* In-memory list of records */
100553 ** An in-memory list of objects to be sorted.
100562 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
100585 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
100599 ** aReadr[0] -> Banana
100600 ** aReadr[1] -> Feijoa
100601 ** aReadr[2] -> Elderberry
100602 ** aReadr[3] -> Currant
100603 ** aReadr[4] -> Grapefruit
100604 ** aReadr[5] -> Apple
100605 ** aReadr[6] -> Durian
100606 ** aReadr[7] -> EOF
100615 ** aReadr[5] -> Eggplant
100620 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
100643 ** single-threaded operation, there is exactly one instance of this object
100644 ** and for multi-threaded operation there are two or more instances.
100655 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
100672 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
100677 SorterFile file; /* Temp file for level-0 PMAs */
100701 SorterList list; /* List of in-memory records */
100744 ** There are two types of IncrMerger object - single (bUseThread==0) and
100745 ** multi-threaded (bUseThread==1).
100747 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
100762 ** A single-threaded IncrMerger does not open any temporary files of its
100764 ** at offset iStartOff of file pTask->file2. And instead of using a
100766 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
100784 ** size. But I/O is more efficient if it occurs in page-sized blocks where
100786 ** the PMA so that aligned, page-size blocks are written.
100789 int eFWErr; /* Non-zero if in an error state */
100803 ** by this module. If using a separate allocation for each in-memory record
100810 ** be sqlite3Realloc()ed while records are being accumulated. Once the VM
100811 ** has finished passing records to the sorter, or when the in-memory buffer
100844 sqlite3_free(pReadr->aAlloc);
100845 sqlite3_free(pReadr->aBuffer);
100846 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
100847 vdbeIncrFree(pReadr->pIncr);
100868 if( p->aMap ){
100869 *ppOut = &p->aMap[p->iReadOff];
100870 p->iReadOff += nByte;
100874 assert( p->aBuffer );
100877 ** p->nBuffer bytes of data from the file into it. Or, if there are less
100878 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
100879 iBuf = p->iReadOff % p->nBuffer;
100885 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
100886 nRead = p->nBuffer;
100888 nRead = (int)(p->iEof - p->iReadOff);
100893 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
100897 nAvail = p->nBuffer - iBuf;
100900 /* The requested data is available in the in-memory buffer. In this
100903 *ppOut = &p->aBuffer[iBuf];
100904 p->iReadOff += nByte;
100906 /* The requested data is not all available in the in-memory buffer.
100907 ** In this case, allocate space at p->aAlloc[] to copy the requested
100908 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
100911 /* Extend the p->aAlloc[] allocation if required. */
100912 if( p->nAlloc<nByte ){
100914 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
100916 aNew = sqlite3Realloc(p->aAlloc, nNew);
100918 p->nAlloc = nNew;
100919 p->aAlloc = aNew;
100923 ** p->aAlloc[]. */
100924 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
100925 p->iReadOff += nAvail;
100926 nRem = nByte - nAvail;
100928 /* The following loop copies up to p->nBuffer bytes per iteration into
100929 ** the p->aAlloc[] buffer. */
100936 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
100939 assert( aNext!=p->aAlloc );
100940 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
100941 nRem -= nCopy;
100944 *ppOut = p->aAlloc;
100957 if( p->aMap ){
100958 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
100960 iBuf = p->iReadOff % p->nBuffer;
100961 if( iBuf && (p->nBuffer-iBuf)>=9 ){
100962 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
100989 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
100990 sqlite3_file *pFd = pFile->pFd;
100991 if( pFd->pMethods->iVersion>=3 ){
100992 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
101012 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
101015 if( pReadr->aMap ){
101016 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
101017 pReadr->aMap = 0;
101019 pReadr->iReadOff = iOff;
101020 pReadr->iEof = pFile->iEof;
101021 pReadr->pFd = pFile->pFd;
101023 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
101024 if( rc==SQLITE_OK && pReadr->aMap==0 ){
101025 int pgsz = pTask->pSorter->pgsz;
101026 int iBuf = pReadr->iReadOff % pgsz;
101027 if( pReadr->aBuffer==0 ){
101028 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
101029 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
101030 pReadr->nBuffer = pgsz;
101033 int nRead = pgsz - iBuf;
101034 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
101035 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
101038 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
101056 if( pReadr->iReadOff>=pReadr->iEof ){
101057 IncrMerger *pIncr = pReadr->pIncr;
101061 if( rc==SQLITE_OK && pIncr->bEof==0 ){
101063 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
101081 pReadr->nKey = (int)nRec;
101082 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
101091 ** starting at offset iStart and ending at offset iEof-1. This function
101107 assert( pFile->iEof>iStart );
101108 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
101109 assert( pReadr->aBuffer==0 );
101110 assert( pReadr->aMap==0 );
101116 pReadr->iEof = pReadr->iReadOff + nByte;
101133 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101137 UnpackedRecord *r2 = pTask->pUnpacked;
101139 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
101147 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
101151 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
101152 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
101155 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
101160 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101164 UnpackedRecord *r2 = pTask->pUnpacked;
101166 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
101179 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101194 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
101196 res = n1 - n2;
101200 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
101206 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
101207 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
101208 res = res * -1;
101221 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
101243 if( (res = v1[i] - v2[i])!=0 ){
101245 res = v1[0] & 0x80 ? -1 : +1;
101251 res = s1 - s2;
101256 res = -1;
101258 res = s1 - s2;
101263 if( *v1 & 0x80 ) res = -1;
101270 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
101275 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
101276 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
101277 res = res * -1;
101286 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
101289 ** is non-zero and the sorter is able to guarantee a stable sort, nField
101297 ** The sorter can guarantee a stable sort when running in single-threaded
101298 ** mode, but not in multi-threaded mode.
101310 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
101311 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
101325 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
101333 nWorker = SORTER_MAX_MERGE_COUNT-1;
101337 assert( pCsr->pKeyInfo );
101338 assert( !pCsr->isEphemeral );
101339 assert( pCsr->eCurType==CURTYPE_SORTER );
101340 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
101344 pCsr->uc.pSorter = pSorter;
101348 Btree *pBt = db->aDb[0].pBt;
101349 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
101350 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
101351 pKeyInfo->db = 0;
101353 pKeyInfo->nKeyField = nField;
101356 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
101358 pSorter->nTask = nWorker + 1;
101359 pSorter->iPrev = (u8)(nWorker - 1);
101360 pSorter->bUseThreads = (pSorter->nTask>1);
101361 pSorter->db = db;
101362 for(i=0; i<pSorter->nTask; i++){
101363 SortSubtask *pTask = &pSorter->aTask[i];
101364 pTask->pSorter = pSorter;
101370 pSorter->mnPmaSize = szPma * pgsz;
101372 mxCache = db->aDb[0].pSchema->cache_size;
101374 /* A negative cache-size value C indicates that the cache is abs(C)
101376 mxCache = mxCache * -1024;
101381 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
101386 assert( pSorter->iMemory==0 );
101387 pSorter->nMemory = pgsz;
101388 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
101389 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
101393 if( pKeyInfo->nAllField<13
101394 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
101395 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
101397 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
101412 pNext = p->u.pNext;
101422 sqlite3DbFree(db, pTask->pUnpacked);
101424 /* pTask->list.aMemory can only be non-zero if it was handed memory
101426 if( pTask->list.aMemory ){
101427 sqlite3_free(pTask->list.aMemory);
101431 assert( pTask->list.aMemory==0 );
101432 vdbeSorterRecordFree(0, pTask->list.pList);
101434 if( pTask->file.pFd ){
101435 sqlite3OsCloseFree(pTask->file.pFd);
101437 if( pTask->file2.pFd ){
101438 sqlite3OsCloseFree(pTask->file2.pFd);
101446 int iTask = (pTask - pTask->pSorter->aTask);
101447 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101461 int iTask = (pTask - pTask->pSorter->aTask);
101462 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101472 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
101485 ** Join thread pTask->thread.
101489 if( pTask->pThread ){
101491 int bDone = pTask->bDone;
101495 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
101498 assert( pTask->bDone==1 );
101499 pTask->bDone = 0;
101500 pTask->pThread = 0;
101513 assert( pTask->pThread==0 && pTask->bDone==0 );
101514 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
101519 ** level-0 PMAs.
101528 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
101531 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
101532 for(i=pSorter->nTask-1; i>=0; i--){
101533 SortSubtask *pTask = &pSorter->aTask[i];
101563 pNew->nTree = N;
101564 pNew->pTask = 0;
101565 pNew->aReadr = (PmaReader*)&pNew[1];
101566 pNew->aTree = (int*)&pNew->aReadr[N];
101577 for(i=0; i<pMerger->nTree; i++){
101578 vdbePmaReaderClear(&pMerger->aReadr[i]);
101591 if( pIncr->bUseThread ){
101592 vdbeSorterJoinThread(pIncr->pTask);
101593 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
101594 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
101597 vdbeMergeEngineFree(pIncr->pMerger);
101608 assert( pSorter->bUseThreads || pSorter->pReader==0 );
101610 if( pSorter->pReader ){
101611 vdbePmaReaderClear(pSorter->pReader);
101612 sqlite3DbFree(db, pSorter->pReader);
101613 pSorter->pReader = 0;
101616 vdbeMergeEngineFree(pSorter->pMerger);
101617 pSorter->pMerger = 0;
101618 for(i=0; i<pSorter->nTask; i++){
101619 SortSubtask *pTask = &pSorter->aTask[i];
101621 pTask->pSorter = pSorter;
101623 if( pSorter->list.aMemory==0 ){
101624 vdbeSorterRecordFree(0, pSorter->list.pList);
101626 pSorter->list.pList = 0;
101627 pSorter->list.szPMA = 0;
101628 pSorter->bUsePMA = 0;
101629 pSorter->iMemory = 0;
101630 pSorter->mxKeysize = 0;
101631 sqlite3DbFree(db, pSorter->pUnpacked);
101632 pSorter->pUnpacked = 0;
101640 assert( pCsr->eCurType==CURTYPE_SORTER );
101641 pSorter = pCsr->uc.pSorter;
101644 sqlite3_free(pSorter->list.aMemory);
101646 pCsr->uc.pSorter = 0;
101652 ** The first argument is a file-handle open on a temporary file. The file
101661 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
101675 ** Allocate space for a file-handle and open a temporary file. If successful,
101676 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
101686 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
101703 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
101707 if( pTask->pUnpacked==0 ){
101708 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
101709 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
101710 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
101711 pTask->pUnpacked->errCode = 0;
101732 res = pTask->xCompare(
101733 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
101738 pp = &p1->u.pNext;
101739 p1 = p1->u.pNext;
101746 pp = &p2->u.pNext;
101747 p2 = p2->u.pNext;
101763 if( p->typeMask==SORTER_TYPE_INTEGER ){
101765 }else if( p->typeMask==SORTER_TYPE_TEXT ){
101772 ** Sort the linked list of records headed at pTask->pList. Return
101785 p = pList->pList;
101786 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
101791 if( pList->aMemory ){
101792 if( (u8*)p==pList->aMemory ){
101795 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
101796 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
101799 pNext = p->u.pNext;
101802 p->u.pNext = 0;
101816 pList->pList = p;
101818 assert( pTask->pUnpacked->errCode==SQLITE_OK
101819 || pTask->pUnpacked->errCode==SQLITE_NOMEM
101821 return pTask->pUnpacked->errCode;
101825 ** Initialize a PMA-writer object.
101834 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
101835 if( !p->aBuffer ){
101836 p->eFWErr = SQLITE_NOMEM_BKPT;
101838 p->iBufEnd = p->iBufStart = (iStart % nBuf);
101839 p->iWriteOff = iStart - p->iBufStart;
101840 p->nBuffer = nBuf;
101841 p->pFd = pFd;
101851 while( nRem>0 && p->eFWErr==0 ){
101853 if( nCopy>(p->nBuffer - p->iBufEnd) ){
101854 nCopy = p->nBuffer - p->iBufEnd;
101857 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
101858 p->iBufEnd += nCopy;
101859 if( p->iBufEnd==p->nBuffer ){
101860 p->eFWErr = sqlite3OsWrite(p->pFd,
101861 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101862 p->iWriteOff + p->iBufStart
101864 p->iBufStart = p->iBufEnd = 0;
101865 p->iWriteOff += p->nBuffer;
101867 assert( p->iBufEnd<p->nBuffer );
101869 nRem -= nCopy;
101874 ** Flush any buffered data to disk and clean up the PMA-writer object.
101875 ** The results of using the PMA-writer after this call are undefined.
101884 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
101885 p->eFWErr = sqlite3OsWrite(p->pFd,
101886 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101887 p->iWriteOff + p->iBufStart
101890 *piEof = (p->iWriteOff + p->iBufEnd);
101891 sqlite3_free(p->aBuffer);
101892 rc = p->eFWErr;
101909 ** Write the current contents of in-memory linked-list pList to a level-0
101910 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
101918 ** * One or more records packed end-to-end in order of ascending keys.
101923 sqlite3 *db = pTask->pSorter->db;
101928 /* Set iSz to the expected size of file pTask->file after writing the PMA.
101930 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
101935 assert( pList->szPMA>0 );
101938 if( pTask->file.pFd==0 ){
101939 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
101940 assert( rc!=SQLITE_OK || pTask->file.pFd );
101941 assert( pTask->file.iEof==0 );
101942 assert( pTask->nPMA==0 );
101947 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
101959 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
101960 pTask->file.iEof);
101961 pTask->nPMA++;
101962 vdbePmaWriteVarint(&writer, pList->szPMA);
101963 for(p=pList->pList; p; p=pNext){
101964 pNext = p->u.pNext;
101965 vdbePmaWriteVarint(&writer, p->nVal);
101966 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
101967 if( pList->aMemory==0 ) sqlite3_free(p);
101969 pList->pList = p;
101970 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
101974 assert( rc!=SQLITE_OK || pList->pList==0 );
101975 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
101991 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
101992 SortSubtask *pTask = pMerger->pTask;
101995 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
102006 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
102007 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
102009 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
102012 if( pReadr1->pFd==0 ){
102014 }else if( pReadr2->pFd==0 ){
102015 iRes = -1;
102017 iRes = pTask->xCompare(pTask, &bCached,
102018 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
102024 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
102029 ** was actually called above, then pTask->pUnpacked now contains
102038 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
102039 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
102042 if( pReadr1->pFd ) bCached = 0;
102043 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
102044 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
102047 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
102050 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
102055 ** The main routine for background threads that write level-0 PMAs.
102060 assert( pTask->bDone==0 );
102061 rc = vdbeSorterListToPMA(pTask, &pTask->list);
102062 pTask->bDone = 1;
102073 pSorter->bUsePMA = 1;
102074 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
102079 int nWorker = (pSorter->nTask-1);
102083 pSorter->bUsePMA = 1;
102085 /* Select a sub-task to sort and flush the current list of in-memory
102086 ** records to disk. If the sorter is running in multi-threaded mode,
102087 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
102088 ** the background thread from a sub-tasks previous turn is still running,
102089 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
102090 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
102091 ** sub-tasks are prefered as they use background threads - the final
102092 ** sub-task uses the main thread. */
102094 int iTest = (pSorter->iPrev + i + 1) % nWorker;
102095 pTask = &pSorter->aTask[iTest];
102096 if( pTask->bDone ){
102099 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
102105 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
102112 assert( pTask->pThread==0 && pTask->bDone==0 );
102113 assert( pTask->list.pList==0 );
102114 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
102116 aMem = pTask->list.aMemory;
102118 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
102119 pTask->list = pSorter->list;
102120 pSorter->list.pList = 0;
102121 pSorter->list.szPMA = 0;
102123 pSorter->list.aMemory = aMem;
102124 pSorter->nMemory = sqlite3MallocSize(aMem);
102125 }else if( pSorter->list.aMemory ){
102126 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
102127 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
102153 assert( pCsr->eCurType==CURTYPE_SORTER );
102154 pSorter = pCsr->uc.pSorter;
102155 getVarint32NR((const u8*)&pVal->z[1], t);
102157 pSorter->typeMask &= SORTER_TYPE_INTEGER;
102159 pSorter->typeMask &= SORTER_TYPE_TEXT;
102161 pSorter->typeMask = 0;
102169 ** If using the single large allocation mode (pSorter->aMemory!=0), then
102176 ** * The total memory allocated for the in-memory list is greater
102177 ** than (page-size * cache-size), or
102179 ** * The total memory allocated for the in-memory list is greater
102180 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
102182 nReq = pVal->n + sizeof(SorterRecord);
102183 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
102184 if( pSorter->mxPmaSize ){
102185 if( pSorter->list.aMemory ){
102186 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
102189 (pSorter->list.szPMA > pSorter->mxPmaSize)
102190 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
102195 pSorter->list.szPMA = 0;
102196 pSorter->iMemory = 0;
102197 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
102201 pSorter->list.szPMA += nPMA;
102202 if( nPMA>pSorter->mxKeysize ){
102203 pSorter->mxKeysize = nPMA;
102206 if( pSorter->list.aMemory ){
102207 int nMin = pSorter->iMemory + nReq;
102209 if( nMin>pSorter->nMemory ){
102211 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
102212 int iListOff = -1;
102213 if( pSorter->list.pList ){
102214 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
102217 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
102219 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
102222 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
102224 pSorter->list.aMemory = aNew;
102225 pSorter->nMemory = nNew;
102228 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
102229 pSorter->iMemory += ROUND8(nReq);
102230 if( pSorter->list.pList ){
102231 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
102238 pNew->u.pNext = pSorter->list.pList;
102241 memcpy(SRVAL(pNew), pVal->z, pVal->n);
102242 pNew->nVal = pVal->n;
102243 pSorter->list.pList = pNew;
102249 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
102251 ** except that the number-of-bytes varint is omitted from the start.
102256 i64 iStart = pIncr->iStartOff;
102257 SorterFile *pOut = &pIncr->aFile[1];
102258 SortSubtask *pTask = pIncr->pTask;
102259 MergeEngine *pMerger = pIncr->pMerger;
102261 assert( pIncr->bEof==0 );
102265 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
102268 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
102269 int nKey = pReader->nKey;
102274 if( pReader->pFd==0 ) break;
102275 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
102279 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
102280 assert( pIncr->pMerger->pTask==pTask );
102281 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
102284 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
102293 ** multi-threaded IncrMerger objects.
102298 pIncr->pTask->bDone = 1;
102307 assert( pIncr->bUseThread );
102308 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
102318 ** For single-threaded objects, this is accomplished by literally reading
102319 ** keys from pIncr->pMerger and repopulating aFile[0].
102321 ** For multi-threaded objects, all that is required is to wait until the
102333 if( pIncr->bUseThread ){
102334 rc = vdbeSorterJoinThread(pIncr->pTask);
102337 SorterFile f0 = pIncr->aFile[0];
102338 pIncr->aFile[0] = pIncr->aFile[1];
102339 pIncr->aFile[1] = f0;
102343 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
102344 pIncr->bEof = 1;
102353 pIncr->aFile[0] = pIncr->aFile[1];
102354 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
102355 pIncr->bEof = 1;
102377 pIncr->pMerger = pMerger;
102378 pIncr->pTask = pTask;
102379 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
102380 pTask->file2.iEof += pIncr->mxSz;
102391 ** Set the "use-threads" flag on object pIncr.
102394 pIncr->bUseThread = 1;
102395 pIncr->pTask->file2.iEof -= pIncr->mxSz;
102402 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
102408 int iOut /* Store the result in pMerger->aTree[iOut] */
102416 assert( iOut<pMerger->nTree && iOut>0 );
102418 if( iOut>=(pMerger->nTree/2) ){
102419 i1 = (iOut - pMerger->nTree/2) * 2;
102422 i1 = pMerger->aTree[iOut*2];
102423 i2 = pMerger->aTree[iOut*2+1];
102426 p1 = &pMerger->aReadr[i1];
102427 p2 = &pMerger->aReadr[i2];
102429 if( p1->pFd==0 ){
102431 }else if( p2->pFd==0 ){
102434 SortSubtask *pTask = pMerger->pTask;
102437 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
102438 res = pTask->xCompare(
102439 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
102448 pMerger->aTree[iOut] = iRes;
102455 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
102501 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
102505 assert( pMerger->pTask==0 );
102506 pMerger->pTask = pTask;
102508 nTree = pMerger->nTree;
102513 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
102516 ** on this PmaReader before any of the multi-threaded PmaReaders takes
102517 ** better advantage of multi-processor hardware. */
102518 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
102520 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
102525 for(i=pMerger->nTree-1; i>0; i--){
102528 return pTask->pUnpacked->errCode;
102533 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
102535 ** object at (pReadr->pIncr).
102538 ** in the sub-tree headed by pReadr are also initialized. Data is then
102543 ** to be a multi-threaded PmaReader and this function is being called in a
102544 ** background thread. In this case all PmaReaders in the sub-tree are
102552 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
102553 ** this entire function is being run by thread (pTask->thread), that will
102557 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
102558 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
102566 IncrMerger *pIncr = pReadr->pIncr;
102567 SortSubtask *pTask = pIncr->pTask;
102568 sqlite3 *db = pTask->pSorter->db;
102570 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
102573 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
102575 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
102576 ** requires two temp files to itself, whereas a single-threaded object
102577 ** only requires a region of pTask->file2. */
102579 int mxSz = pIncr->mxSz;
102581 if( pIncr->bUseThread ){
102582 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
102584 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
102588 /*if( !pIncr->bUseThread )*/{
102589 if( pTask->file2.pFd==0 ){
102590 assert( pTask->file2.iEof>0 );
102591 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
102592 pTask->file2.iEof = 0;
102595 pIncr->aFile[1].pFd = pTask->file2.pFd;
102596 pIncr->iStartOff = pTask->file2.iEof;
102597 pTask->file2.iEof += mxSz;
102603 if( rc==SQLITE_OK && pIncr->bUseThread ){
102605 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
102607 ** pIncr->pTask->thread.
102636 pReader->pIncr->pTask->bDone = 1;
102642 ** If the PmaReader passed as the first argument is not an incremental-reader
102643 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
102647 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
102653 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
102657 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
102658 if( pIncr->bUseThread ){
102660 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
102671 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
102672 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
102677 ** first PMA to read from pTask->file. Assuming no error occurs, it is
102685 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
102686 MergeEngine **ppOut /* OUT: New merge-engine */
102698 PmaReader *pReadr = &pNew->aReadr[i];
102699 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
102700 iOff = pReadr->iEof;
102717 ** nPMA<=16 -> TreeDepth() == 0
102718 ** nPMA<=256 -> TreeDepth() == 1
102719 ** nPMA<=65536 -> TreeDepth() == 2
102732 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
102760 PmaReader *pReadr = &p->aReadr[iIter];
102762 if( pReadr->pIncr==0 ){
102767 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
102771 p = pReadr->pIncr->pMerger;
102777 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
102786 ** that has already written two or more level-0 PMAs to one or more temp
102804 /* If the sorter uses more than one task, then create the top-level
102806 ** one PmaReader per sub-task. */
102807 assert( pSorter->bUseThreads || pSorter->nTask==1 );
102808 if( pSorter->nTask>1 ){
102809 pMain = vdbeMergeEngineNew(pSorter->nTask);
102814 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102815 SortSubtask *pTask = &pSorter->aTask[iTask];
102816 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
102817 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
102819 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
102822 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
102823 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
102829 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
102830 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
102831 int nReader; /* Number of level-0 PMAs to merge */
102833 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
102844 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
102869 ** (for multi-threaded sorters) so that it can be used to iterate through
102876 SortSubtask *pTask0 = &pSorter->aTask[0];
102879 sqlite3 *db = pTask0->pSorter->db;
102882 for(i=0; i<pSorter->nTask; i++){
102883 pSorter->aTask[i].xCompare = xCompare;
102890 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
102891 if( pSorter->bUseThreads ){
102894 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
102898 pSorter->pReader = pReadr;
102902 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
102904 vdbeIncrMergerSetThreads(pReadr->pIncr);
102905 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
102907 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
102909 assert( pIncr->pTask!=pLast );
102912 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102917 ** b) If it is using task (nTask-1), it is configured to run
102918 ** in single-threaded mode. This is important, as the
102922 PmaReader *p = &pMain->aReadr[iTask];
102923 assert( p->pIncr==0 || (
102924 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
102925 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
102939 pSorter->pMerger = pMain;
102960 assert( pCsr->eCurType==CURTYPE_SORTER );
102961 pSorter = pCsr->uc.pSorter;
102966 ** from the in-memory list. */
102967 if( pSorter->bUsePMA==0 ){
102968 if( pSorter->list.pList ){
102970 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
102977 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
102981 assert( pSorter->list.pList );
102991 assert( pSorter->pReader==0 );
103012 assert( pCsr->eCurType==CURTYPE_SORTER );
103013 pSorter = pCsr->uc.pSorter;
103014 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
103015 if( pSorter->bUsePMA ){
103016 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
103017 assert( pSorter->bUseThreads==0 || pSorter->pReader );
103018 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
103020 if( pSorter->bUseThreads ){
103021 rc = vdbePmaReaderNext(pSorter->pReader);
103022 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
103025 /*if( !pSorter->bUseThreads )*/ {
103027 assert( pSorter->pMerger!=0 );
103028 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
103029 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
103033 SorterRecord *pFree = pSorter->list.pList;
103034 pSorter->list.pList = pFree->u.pNext;
103035 pFree->u.pNext = 0;
103036 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
103037 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
103051 if( pSorter->bUsePMA ){
103054 if( pSorter->bUseThreads ){
103055 pReader = pSorter->pReader;
103058 /*if( !pSorter->bUseThreads )*/{
103059 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
103061 *pnKey = pReader->nKey;
103062 pKey = pReader->aKey;
103064 *pnKey = pSorter->list.pList->nVal;
103065 pKey = SRVAL(pSorter->list.pList);
103077 assert( pCsr->eCurType==CURTYPE_SORTER );
103078 pSorter = pCsr->uc.pSorter;
103083 pOut->n = nKey;
103085 memcpy(pOut->z, pKey, nKey);
103118 assert( pCsr->eCurType==CURTYPE_SORTER );
103119 pSorter = pCsr->uc.pSorter;
103120 r2 = pSorter->pUnpacked;
103121 pKeyInfo = pCsr->pKeyInfo;
103123 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
103125 r2->nField = nKeyCol;
103127 assert( r2->nField==nKeyCol );
103132 if( r2->aMem[i].flags & MEM_Null ){
103133 *pRes = -1;
103138 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
103145 ** 2020-03-23
103156 ** This file implements virtual-tables for examining the bytecode content
103163 /* An instance of the bytecode() table-valued function.
103167 sqlite3_vtab base; /* Base class - must be first */
103176 sqlite3_vtab_cursor base; /* Base class - must be first */
103191 ** Create a new bytecode() table-valued function.
103235 pNew->db = db;
103236 pNew->bTablesUsed = isTabUsed*2;
103259 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
103260 *ppCursor = &pCur->base;
103268 sqlite3_free(pCur->zP4);
103269 pCur->zP4 = 0;
103270 sqlite3VdbeMemRelease(&pCur->sub);
103271 sqlite3VdbeMemSetNull(&pCur->sub);
103272 if( pCur->needFinalize ){
103273 sqlite3_finalize(pCur->pStmt);
103275 pCur->pStmt = 0;
103276 pCur->needFinalize = 0;
103277 pCur->zType = 0;
103278 pCur->zSchema = 0;
103279 pCur->zName = 0;
103298 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
103300 if( pCur->zP4 ){
103301 sqlite3_free(pCur->zP4);
103302 pCur->zP4 = 0;
103304 if( pCur->zName ){
103305 pCur->zName = 0;
103306 pCur->zType = 0;
103307 pCur->zSchema = 0;
103310 (Vdbe*)pCur->pStmt,
103311 pCur->showSubprograms ? &pCur->sub : 0,
103312 pTab->bTablesUsed,
103313 &pCur->iRowid,
103314 &pCur->iAddr,
103315 &pCur->aOp);
103317 sqlite3VdbeMemSetNull(&pCur->sub);
103318 pCur->aOp = 0;
103329 return pCur->aOp==0;
103342 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
103343 Op *pOp = pCur->aOp + pCur->iAddr;
103344 if( pVTab->bTablesUsed ){
103348 if( i<=2 && pCur->zType==0 ){
103351 int iDb = pOp->p3;
103352 Pgno iRoot = (Pgno)pOp->p2;
103353 sqlite3 *db = pVTab->db;
103354 pSchema = db->aDb[iDb].pSchema;
103355 pCur->zSchema = db->aDb[iDb].zDbSName;
103356 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
103358 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
103359 pCur->zName = pTab->zName;
103360 pCur->zType = "table";
103364 if( pCur->zName==0 ){
103365 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
103367 if( pIdx->tnum==iRoot ){
103368 pCur->zName = pIdx->zName;
103369 pCur->zType = "index";
103379 sqlite3_result_int(ctx, pCur->iAddr);
103382 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
103383 -1, SQLITE_STATIC);
103386 sqlite3_result_int(ctx, pOp->p1);
103389 sqlite3_result_int(ctx, pOp->p2);
103392 sqlite3_result_int(ctx, pOp->p3);
103396 if( pCur->zP4==0 ){
103397 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
103400 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
103403 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
103404 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
103409 sqlite3_result_int(ctx, pOp->p5);
103412 Op *aOp = pCur->aOp;
103414 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
103415 if( pCur->iRowid==pCur->iAddr+1 ){
103418 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
103425 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
103428 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
103431 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
103434 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
103446 *pRowid = pCur->iRowid;
103462 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
103466 pCur->iRowid = 0;
103467 pCur->iAddr = 0;
103468 pCur->showSubprograms = idxNum==0;
103475 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
103476 pCur->needFinalize = 1;
103479 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
103481 if( pCur->pStmt==0 ){
103482 pVTab->base.zErrMsg = sqlite3_mprintf(
103484 pVTab->bTablesUsed ? "tables_used" : "bytecode"
103506 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
103507 pIdxInfo->estimatedCost = (double)100;
103508 pIdxInfo->estimatedRows = 100;
103509 pIdxInfo->idxNum = 0;
103510 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
103511 if( p->usable==0 ) continue;
103512 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
103514 pIdxInfo->aConstraintUsage[i].omit = 1;
103515 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
103517 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
103518 pIdxInfo->aConstraintUsage[i].omit = 1;
103519 pIdxInfo->idxNum = 1;
103583 ** This file contains code use to implement an in-memory rollback journal.
103584 ** The in-memory rollback journal is used to journal transactions for
103587 ** Update: The in-memory journal is also used to temporarily cache
103588 ** smaller journals that are not critical for power-loss recovery.
103606 ** The zChunk array is always at least 8 bytes in size - usually much more.
103623 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
103635 ** This structure is a subclass of sqlite3_file. Each open memory-journal
103640 int nChunkSize; /* In-memory chunk-size */
103643 FileChunk *pFirst; /* Head of in-memory chunk-list */
103653 ** Read data from the in-memory journal file. This is the implementation
103668 if( (iAmt+iOfst)>p->endpoint.iOffset ){
103671 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
103672 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
103674 for(pChunk=p->pFirst;
103675 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
103676 pChunk=pChunk->pNext
103678 iOff += p->nChunkSize;
103681 pChunk = p->readpoint.pChunk;
103685 iChunkOffset = (int)(iOfst%p->nChunkSize);
103687 int iSpace = p->nChunkSize - iChunkOffset;
103688 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
103689 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
103691 nRead -= iSpace;
103693 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
103694 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
103695 p->readpoint.pChunk = pChunk;
103707 pNext = pIter->pNext;
103726 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
103728 nChunk = copy.endpoint.iOffset - iOff;
103730 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
103735 /* No error has occurred. Free the in-memory buffers. */
103741 ** the original before returning. This way, SQLite uses the in-memory
103742 ** journal data to roll back changes made to the internal page-cache
103769 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
103779 /* An in-memory journal file should only ever be appended to. Random
103781 ** the in-memory journal is being used by a connection using the
103782 ** atomic-write optimization. In this case the first 28 bytes of the
103784 assert( iOfst<=p->endpoint.iOffset );
103785 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
103788 if( iOfst==0 && p->pFirst ){
103789 assert( p->nChunkSize>iAmt );
103790 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
103793 FileChunk *pChunk = p->endpoint.pChunk;
103794 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
103795 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
103800 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
103804 pNew->pNext = 0;
103806 assert( p->pFirst );
103807 pChunk->pNext = pNew;
103809 assert( !p->pFirst );
103810 p->pFirst = pNew;
103812 pChunk = p->endpoint.pChunk = pNew;
103816 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
103818 nWrite -= iSpace;
103819 p->endpoint.iOffset += iSpace;
103828 ** Truncate the in-memory file.
103832 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
103833 if( size<p->endpoint.iOffset ){
103836 memjrnlFreeChunks(p->pFirst);
103837 p->pFirst = 0;
103839 i64 iOff = p->nChunkSize;
103840 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
103841 iOff += p->nChunkSize;
103844 memjrnlFreeChunks(pIter->pNext);
103845 pIter->pNext = 0;
103849 p->endpoint.pChunk = pIter;
103850 p->endpoint.iOffset = size;
103851 p->readpoint.pChunk = 0;
103852 p->readpoint.iOffset = 0;
103862 memjrnlFreeChunks(p->pFirst);
103870 ** syncing an in-memory journal is a no-op.
103882 *pSize = (sqlite_int64) p->endpoint.iOffset;
103917 ** all content is always stored in main-memory. Finally, if nSpill is a
103918 ** positive value, then the journal file is initially created in-memory
103934 /* Zero the file-handle object. If nSpill was passed zero, initialize
103937 ** made on the journal file-handle. */
103944 p->nChunkSize = nSpill;
103946 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
103947 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
103950 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
103951 p->nSpill = nSpill;
103952 p->flags = flags;
103953 p->zJournal = zName;
103954 p->pVfs = pVfs;
103959 ** Open an in-memory journal file.
103962 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
103969 ** in-memory-only journal file (i.e. is one that was opened with a +ve
103976 if( pJfd->pMethods==&MemJournalMethods && (
103978 p->nSpill>0
103983 NEVER(p->nSpill>0)
103986 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
103996 ** The file-handle passed as the only argument is open on a journal file.
104001 return p->pMethods==&MemJournalMethods;
104006 ** pVfs to create the underlying on-disk files.
104009 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
104040 for(pWin=pList; pWin; pWin=pWin->pNextWin){
104042 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
104044 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
104046 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
104048 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
104050 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
104072 ** return from the top-level walk call.
104082 rc = pWalker->xExprCallback(pWalker, pExpr);
104085 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
104086 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
104087 if( pExpr->pRight ){
104089 pExpr = pExpr->pRight;
104093 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
104095 if( pExpr->x.pList ){
104096 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
104100 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
104121 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
104122 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
104129 ** This is a no-op callback for Walker->xSelectCallback2. If this
104130 ** callback is set, then the Select->pWinDefn list is traversed.
104135 /* No-op */
104145 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
104146 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
104147 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
104148 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
104149 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
104150 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
104152 if( p->pWinDefn ){
104154 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
104155 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
104157 || pWalker->xSelectCallback2==sqlite3SelectPopWith
104162 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
104182 pSrc = p->pSrc;
104184 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
104185 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
104188 if( pItem->fg.isTabFunc
104189 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
104201 ** on the compound select chain, p->pPrior.
104206 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
104213 ** is a no-op returning WRC_Continue.
104218 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
104220 rc = pWalker->xSelectCallback(pWalker, p);
104227 if( pWalker->xSelectCallback2 ){
104228 pWalker->xSelectCallback2(pWalker, p);
104230 p = p->pPrior;
104240 pWalker->walkerDepth++;
104245 pWalker->walkerDepth--;
104250 ** No-op routine for the parse-tree walker.
104264 ** No-op routine for the parse-tree walker for SELECT statements.
104304 ** is a helper function - a callback for the tree walker.
104309 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
104323 ** Turn the pExpr expression into an alias for the iCol-th column of the
104344 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
104348 Expr *pOrig; /* The iCol-th column of the result set */
104352 assert( iCol>=0 && iCol<pEList->nExpr );
104353 pOrig = pEList->a[iCol].pExpr;
104355 db = pParse->db;
104357 if( db->mallocFailed ){
104363 if( pExpr->op==TK_COLLATE ){
104365 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
104371 if( ALWAYS(pExpr->y.pWin!=0) ){
104372 pExpr->y.pWin->pOwner = pExpr;
104394 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
104395 zSpan = pItem->zEName;
104413 ** Return TRUE if the double-quoted string mis-feature should be supported.
104416 if( db->init.busy ) return 1; /* Always support for legacy schemas */
104417 if( pTopNC->ncFlags & NC_IsDDL ){
104419 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
104422 return (db->flags & SQLITE_DqsDDL)!=0;
104425 return (db->flags & SQLITE_DqsDML)!=0;
104430 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
104437 n = pExpr->iColumn;
104439 pExTab = pExpr->y.pTab;
104441 if( (pExTab->tabFlags & TF_HasGenerated)!=0
104442 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
104444 testcase( pExTab->nCol==BMS-1 );
104445 testcase( pExTab->nCol==BMS );
104446 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
104448 testcase( n==BMS-1 );
104450 if( n>=BMS ) n = BMS-1;
104467 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
104469 pNew->iTable = pMatch->iCursor;
104470 pNew->iColumn = iColumn;
104471 pNew->y.pTab = pMatch->pTab;
104472 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
104484 ** pExpr->iDb Set the index in db->aDb[] of the database X
104486 ** pExpr->iTable Set to the cursor number for the table obtained
104488 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
104490 ** pExpr->iColumn Set to the column number within the table.
104491 ** pExpr->op Set to TK_COLUMN.
104492 ** pExpr->pLeft Any expression this points to is deleted
104493 ** pExpr->pRight Any expression this points to is deleted.
104517 sqlite3 *db = pParse->db; /* The database connection */
104522 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
104532 /* Initialize the node to no-match */
104533 pExpr->iTable = -1;
104541 testcase( pNC->ncFlags & NC_PartIdx );
104542 testcase( pNC->ncFlags & NC_IsCheck );
104543 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
104550 for(i=0; i<db->nDb; i++){
104551 assert( db->aDb[i].zDbSName );
104552 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
104553 pSchema = db->aDb[i].pSchema;
104557 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
104560 pSchema = db->aDb[0].pSchema;
104561 zDb = db->aDb[0].zDbSName;
104566 /* Start at the inner-most context and move outward until a match is found */
104570 SrcList *pSrcList = pNC->pSrcList;
104573 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
104575 pTab = pItem->pTab;
104576 assert( pTab!=0 && pTab->zName!=0 );
104577 assert( pTab->nCol>0 || pParse->nErr );
104578 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
104579 if( pItem->fg.isNestedFrom ){
104584 ** This pItem -------------^
104587 assert( pItem->pSelect!=0 );
104588 pEList = pItem->pSelect->pEList;
104590 assert( pEList->nExpr==pTab->nCol );
104591 for(j=0; j<pEList->nExpr; j++){
104592 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
104596 if( pItem->fg.isUsing==0
104597 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104605 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104606 /* An INNER or LEFT JOIN. Use the left-most table */
104609 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104610 /* A RIGHT JOIN. Use the right-most table */
104616 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104622 pExpr->iColumn = j;
104623 pEList->a[j].fg.bUsed = 1;
104625 if( pEList->a[j].fg.bUsingTerm ) break;
104633 if( pTab->pSchema!=pSchema ) continue;
104636 zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
104642 if( IN_RENAME_OBJECT && pItem->zAlias ){
104643 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
104647 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
104648 if( pCol->hName==hCol
104649 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104652 if( pItem->fg.isUsing==0
104653 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104661 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104662 /* An INNER or LEFT JOIN. Use the left-most table */
104665 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104666 /* A RIGHT JOIN. Use the right-most table */
104672 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104677 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
104678 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
104679 if( pItem->fg.isNestedFrom ){
104691 pExpr->iTable = pMatch->iCursor;
104693 pExpr->y.pTab = pMatch->pTab;
104694 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
104697 pSchema = pExpr->y.pTab->pSchema;
104710 if( pParse->pTriggerTab!=0 ){
104711 int op = pParse->eTriggerOp;
104713 if( pParse->bReturning ){
104714 if( (pNC->ncFlags & NC_UBaseReg)!=0
104715 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
104717 pExpr->iTable = op!=TK_DELETE;
104718 pTab = pParse->pTriggerTab;
104721 pExpr->iTable = 1;
104722 pTab = pParse->pTriggerTab;
104724 pExpr->iTable = 0;
104725 pTab = pParse->pTriggerTab;
104730 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
104731 Upsert *pUpsert = pNC->uNC.pUpsert;
104733 pTab = pUpsert->pUpsertSrc->a[0].pTab;
104734 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
104742 pSchema = pTab->pSchema;
104744 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
104745 if( pCol->hName==hCol
104746 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104748 if( iCol==pTab->iPKey ){
104749 iCol = -1;
104754 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
104755 /* IMP: R-51414-32910 */
104756 iCol = -1;
104758 if( iCol<pTab->nCol ){
104762 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
104763 testcase( iCol==(-1) );
104766 pExpr->iColumn = iCol;
104767 pExpr->y.pTab = pTab;
104770 pExpr->iTable = pNC->uNC.pUpsert->regData +
104778 pExpr->y.pTab = pTab;
104779 if( pParse->bReturning ){
104781 pExpr->op2 = TK_COLUMN;
104782 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
104785 pExpr->iColumn = (i16)iCol;
104789 pExpr->affExpr = SQLITE_AFF_INTEGER;
104790 }else if( pExpr->iTable==0 ){
104793 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104797 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104813 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
104815 && ALWAYS(VisibleRowid(pMatch->pTab))
104818 pExpr->iColumn = -1;
104819 pExpr->affExpr = SQLITE_AFF_INTEGER;
104824 ** might refer to an result-set alias. This happens, for example, when
104834 ** The ability to use an output result-set column in the WHERE, GROUP BY,
104841 && (pNC->ncFlags & NC_UEList)!=0
104844 pEList = pNC->uNC.pEList;
104846 for(j=0; j<pEList->nExpr; j++){
104847 char *zAs = pEList->a[j].zEName;
104848 if( pEList->a[j].fg.eEName==ENAME_NAME
104852 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
104853 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
104854 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
104855 pOrig = pEList->a[j].pExpr;
104856 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
104861 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
104886 pNC = pNC->pNext;
104893 ** supplied) and the value of Z is enclosed in double-quotes, then
104898 ** Because no reference was made to outer contexts, the pNC->nRef
104902 assert( pExpr->op==TK_ID );
104906 /* If a double-quoted identifier does not match any known column name,
104910 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
104921 "double-quoted string literal: \"%w\"", zCol);
104923 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
104925 pExpr->op = TK_STRING;
104926 memset(&pExpr->y, 0, sizeof(pExpr->y));
104946 if( pFJMatch->nExpr==cnt-1 ){
104950 sqlite3ExprDelete(db, pExpr->pLeft);
104951 pExpr->pLeft = 0;
104952 sqlite3ExprDelete(db, pExpr->pRight);
104953 pExpr->pRight = 0;
104955 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104956 pExpr->op = TK_FUNCTION;
104957 pExpr->u.zToken = "coalesce";
104958 pExpr->x.pList = pFJMatch;
104974 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104975 pParse->checkSchema = 1;
104976 pTopNC->nNcErr++;
104982 sqlite3ExprDelete(db, pExpr->pLeft);
104983 pExpr->pLeft = 0;
104984 sqlite3ExprDelete(db, pExpr->pRight);
104985 pExpr->pRight = 0;
105003 if( pExpr->iColumn>=0 && pMatch!=0 ){
105004 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
105007 pExpr->op = eNewExprOp;
105012 if( pParse->db->xAuth
105013 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
105015 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
105022 pTopNC->nRef++;
105024 pTopNC = pTopNC->pNext;
105039 SrcItem *pItem = &pSrc->a[iSrc];
105042 pTab = p->y.pTab = pItem->pTab;
105043 p->iTable = pItem->iCursor;
105044 if( p->y.pTab->iPKey==iCol ){
105045 p->iColumn = -1;
105047 p->iColumn = (ynVar)iCol;
105048 if( (pTab->tabFlags & TF_HasGenerated)!=0
105049 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
105051 testcase( pTab->nCol==63 );
105052 testcase( pTab->nCol==64 );
105053 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
105056 testcase( iCol==BMS-1 );
105057 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
105066 ** pNC->ncFlags values determined by validMask.
105088 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
105090 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
105093 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
105096 if( pExpr ) pExpr->op = TK_NULL;
105097 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
105101 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
105105 ** Return 1024 times this value. Or return -1 if p is not a floating point
105109 double r = -1.0;
105110 if( p->op!=TK_FLOAT ) return -1;
105112 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
105114 if( r>1.0 ) return -1;
105133 pNC = pWalker->u.pNC;
105135 pParse = pNC->pParse;
105136 assert( pParse==pWalker->pParse );
105139 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
105140 SrcList *pSrcList = pNC->pSrcList;
105142 for(i=0; i<pNC->pSrcList->nSrc; i++){
105143 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
105147 switch( pExpr->op ){
105155 SrcList *pSrcList = pNC->pSrcList;
105157 assert( pSrcList && pSrcList->nSrc>=1 );
105158 pItem = pSrcList->a;
105159 pExpr->op = TK_COLUMN;
105161 pExpr->y.pTab = pItem->pTab;
105162 pExpr->iTable = pItem->iCursor;
105163 pExpr->iColumn--;
105164 pExpr->affExpr = SQLITE_AFF_INTEGER;
105170 ** "expr IS NOT NULL" --> "TRUE"
105171 ** "expr IS NULL" --> "FALSE"
105176 ** If this optimization occurs, also restore the NameContext ref-counts
105187 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
105188 anRef[i] = p->nRef;
105190 sqlite3WalkExpr(pWalker, pExpr->pLeft);
105191 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
105194 if( pExpr->op==TK_NOTNULL ){
105195 pExpr->u.zToken = "true";
105198 pExpr->u.zToken = "false";
105201 pExpr->op = TK_TRUEFALSE;
105202 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
105203 p->nRef = anRef[i];
105205 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
105206 pExpr->pLeft = 0;
105216 ** be one call to lookupName(). Then the compiler will in-line
105226 if( pExpr->op==TK_ID ){
105230 zColumn = pExpr->u.zToken;
105232 Expr *pLeft = pExpr->pLeft;
105233 testcase( pNC->ncFlags & NC_IdxExpr );
105234 testcase( pNC->ncFlags & NC_GenCol );
105237 pRight = pExpr->pRight;
105238 if( pRight->op==TK_ID ){
105241 assert( pRight->op==TK_DOT );
105243 zDb = pLeft->u.zToken;
105244 pLeft = pRight->pLeft;
105245 pRight = pRight->pRight;
105248 zTable = pLeft->u.zToken;
105249 zColumn = pRight->u.zToken;
105253 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
105262 ExprList *pList = pExpr->x.pList; /* The argument list */
105263 int n = pList ? pList->nExpr : 0; /* Number of arguments */
105269 u8 enc = ENC(pParse->db); /* The database encoding */
105270 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
105272 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
105275 zId = pExpr->u.zToken;
105276 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
105278 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
105285 is_agg = pDef->xFinalize!=0;
105286 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
105289 pExpr->iTable = exprProbability(pList->a[1].pExpr);
105290 if( pExpr->iTable<0 ){
105294 pNC->nNcErr++;
105297 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
105299 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
105300 ** short-hand for likelihood(X,0.0625).
105301 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
105303 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
105306 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
105311 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
105316 pNC->nNcErr++;
105318 pExpr->op = TK_NULL;
105323 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
105330 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
105331 /* Clearly non-deterministic functions like random(), but also
105337 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
105341 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
105342 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
105344 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
105345 && pParse->nested==0
105346 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
105348 /* Internal-use-only functions are disallowed unless the
105350 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
105355 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
105364 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
105365 || (pDef->xValue==0 && pDef->xInverse==0)
105366 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
105368 if( pDef && pDef->xValue==0 && pWin ){
105372 pNC->nNcErr++;
105374 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
105375 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
105376 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
105379 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
105385 pNC->nNcErr++;
105389 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
105391 pNC->nNcErr++;
105395 else if( no_such_func && pParse->db->init.busy==0
105397 && pParse->explain==0
105401 pNC->nNcErr++;
105405 pNC->nNcErr++;
105410 "FILTER may not be used with non-aggregate %#T()",
105413 pNC->nNcErr++;
105421 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
105423 pNC->ncFlags &= ~NC_AllowAgg;
105436 Select *pSel = pNC->pWinSelect;
105437 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
105439 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
105440 if( pParse->db->mallocFailed ) break;
105442 sqlite3WalkExprList(pWalker, pWin->pPartition);
105443 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
105444 sqlite3WalkExpr(pWalker, pWin->pFilter);
105446 pNC->ncFlags |= NC_HasWin;
105451 pExpr->op = TK_AGG_FUNCTION;
105452 pExpr->op2 = 0;
105455 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
105460 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
105462 pExpr->op2++;
105463 pNC2 = pNC2->pNext;
105469 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
105470 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
105471 pNC2->ncFlags |= NC_HasAgg
105472 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
105476 pNC->ncFlags |= savedAllowFlags;
105478 /* FIX ME: Compute pExpr->affinity based on the expected return
105485 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
105488 testcase( pExpr->op==TK_IN );
105490 int nRef = pNC->nRef;
105491 testcase( pNC->ncFlags & NC_IsCheck );
105492 testcase( pNC->ncFlags & NC_PartIdx );
105493 testcase( pNC->ncFlags & NC_IdxExpr );
105494 testcase( pNC->ncFlags & NC_GenCol );
105495 if( pNC->ncFlags & NC_SelfRef ){
105498 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
105500 assert( pNC->nRef>=nRef );
105501 if( nRef!=pNC->nRef ){
105503 pNC->ncFlags |= NC_VarSelect;
105509 testcase( pNC->ncFlags & NC_IsCheck );
105510 testcase( pNC->ncFlags & NC_PartIdx );
105511 testcase( pNC->ncFlags & NC_IdxExpr );
105512 testcase( pNC->ncFlags & NC_GenCol );
105519 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
105523 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
105526 if( pRight->op==TK_TRUEFALSE ){
105527 pExpr->op2 = pExpr->op;
105528 pExpr->op = TK_TRUTH;
105542 if( pParse->db->mallocFailed ) break;
105543 assert( pExpr->pLeft!=0 );
105544 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
105545 if( pExpr->op==TK_BETWEEN ){
105547 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
105549 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
105552 assert( pExpr->pRight!=0 );
105553 nRight = sqlite3ExprVectorSize(pExpr->pRight);
105556 testcase( pExpr->op==TK_EQ );
105557 testcase( pExpr->op==TK_NE );
105558 testcase( pExpr->op==TK_LT );
105559 testcase( pExpr->op==TK_LE );
105560 testcase( pExpr->op==TK_GT );
105561 testcase( pExpr->op==TK_GE );
105562 testcase( pExpr->op==TK_IS );
105563 testcase( pExpr->op==TK_ISNOT );
105564 testcase( pExpr->op==TK_BETWEEN );
105566 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
105571 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
105572 return pParse->nErr ? WRC_Abort : WRC_Continue;
105579 ** to the AS-name of one of the terms of the expression list. If it is,
105596 if( pE->op==TK_ID ){
105599 zCol = pE->u.zToken;
105600 for(i=0; i<pEList->nExpr; i++){
105601 if( pEList->a[i].fg.eEName==ENAME_NAME
105602 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
105620 ** Attempt to match pE against result set columns in the left-most
105622 ** as an indication to the caller that it should sort by the i-th column.
105623 ** The left-most column is 1. In other words, the value returned is the
105627 ** If there is no match, return 0. Return -1 if an error occurs.
105639 u8 savedSuppErr; /* Saved value of db->suppressErr */
105642 pEList = pSelect->pEList;
105648 nc.pSrcList = pSelect->pSrc;
105652 db = pParse->db;
105653 savedSuppErr = db->suppressErr;
105654 db->suppressErr = 1;
105656 db->suppressErr = savedSuppErr;
105660 ** in the result set. Return an 1-based index of the matching
105661 ** result-set entry.
105663 for(i=0; i<pEList->nExpr; i++){
105664 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
105674 ** Generate an ORDER BY or GROUP BY term out-of-range error.
105679 int i, /* The index (1-based) of the term out of range */
105684 "%r %s BY term out of range - should be "
105686 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
105698 ** beginning with the left-most SELECT and working toward the right.
105714 pOrderBy = pSelect->pOrderBy;
105716 db = pParse->db;
105717 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105721 for(i=0; i<pOrderBy->nExpr; i++){
105722 pOrderBy->a[i].fg.done = 0;
105724 pSelect->pNext = 0;
105725 while( pSelect->pPrior ){
105726 pSelect->pPrior->pNext = pSelect;
105727 pSelect = pSelect->pPrior;
105732 pEList = pSelect->pEList;
105734 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105735 int iCol = -1;
105737 if( pItem->fg.done ) continue;
105738 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
105741 if( iCol<=0 || iCol>pEList->nExpr ){
105742 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
105760 if( !db->mallocFailed ){
105776 pNew->flags |= EP_IntValue;
105777 pNew->u.iValue = iCol;
105778 if( pItem->pExpr==pE ){
105779 pItem->pExpr = pNew;
105781 Expr *pParent = pItem->pExpr;
105782 assert( pParent->op==TK_COLLATE );
105783 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
105784 assert( pParent->pLeft==pE );
105785 pParent->pLeft = pNew;
105788 pItem->u.x.iOrderByCol = (u16)iCol;
105790 pItem->fg.done = 1;
105795 pSelect = pSelect->pNext;
105797 for(i=0; i<pOrderBy->nExpr; i++){
105798 if( pOrderBy->a[i].fg.done==0 ){
105815 ** return non-zero. Return zero if no errors are seen.
105824 sqlite3 *db = pParse->db;
105828 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
105829 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105833 pEList = pSelect->pEList;
105835 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105836 if( pItem->u.x.iOrderByCol ){
105837 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
105838 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
105841 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
105854 Window *pWin = pExpr->y.pWin;
105865 if( pSelect->pWin ){
105883 ** If the order-by term is an integer I between 1 and N (where N is the
105885 ** in the resolution is a copy of the I-th result-set expression. If
105886 ** the order-by term is an identifier that corresponds to the AS-name of
105887 ** a result-set expression, then the term resolves to a copy of the
105888 ** result-set expression. Otherwise, the expression is resolved in
105889 ** the usual way - using sqlite3ResolveExprNames().
105908 nResult = pSelect->pEList->nExpr;
105909 pParse = pNC->pParse;
105910 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105911 Expr *pE = pItem->pExpr;
105915 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
105917 /* If an AS-name match is found, mark this ORDER BY column as being
105918 ** a copy of the iCol-th result-set column. The subsequent call to
105920 ** copy of the iCol-th result-set expression. */
105921 pItem->u.x.iOrderByCol = (u16)iCol;
105928 ** order-by term to a copy of the result-set expression */
105933 pItem->u.x.iOrderByCol = (u16)iCol;
105938 pItem->u.x.iOrderByCol = 0;
105942 for(j=0; j<pSelect->pEList->nExpr; j++){
105943 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
105948 pItem->u.x.iOrderByCol = j+1;
105966 Select *pLeftmost; /* Left-most of SELECT of a compound */
105971 if( p->selFlags & SF_Resolved ){
105974 pOuterNC = pWalker->u.pNC;
105975 pParse = pWalker->pParse;
105976 db = pParse->db;
105986 if( (p->selFlags & SF_Expanded)==0 ){
105988 return pParse->nErr ? WRC_Abort : WRC_Prune;
105991 isCompound = p->pPrior!=0;
105995 assert( (p->selFlags & SF_Expanded)!=0 );
105996 assert( (p->selFlags & SF_Resolved)==0 );
105997 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
105998 p->selFlags |= SF_Resolved;
106007 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
106013 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
106014 ** as if it were part of the sub-query, not the parent. This block
106015 ** moves the pOrderBy down to the sub-query. It will be moved back
106017 if( p->selFlags & SF_Converted ){
106018 Select *pSub = p->pSrc->a[0].pSelect;
106019 assert( p->pSrc->nSrc==1 && p->pOrderBy );
106020 assert( pSub->pPrior && pSub->pOrderBy==0 );
106021 pSub->pOrderBy = p->pOrderBy;
106022 p->pOrderBy = 0;
106027 for(i=0; i<p->pSrc->nSrc; i++){
106028 SrcItem *pItem = &p->pSrc->a[i];
106029 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
106030 int nRef = pOuterNC ? pOuterNC->nRef : 0;
106031 const char *zSavedContext = pParse->zAuthContext;
106033 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
106034 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
106035 pParse->zAuthContext = zSavedContext;
106036 if( pParse->nErr ) return WRC_Abort;
106037 assert( db->mallocFailed==0 );
106040 ** expressions in the sub-select were resolved, the sub-select
106046 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
106047 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
106052 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
106053 ** resolve the result-set expression list.
106056 sNC.pSrcList = p->pSrc;
106060 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
106063 /* If there are no aggregate functions in the result-set, and no GROUP BY
106066 assert( (p->selFlags & SF_Aggregate)==0 );
106067 pGroupBy = p->pGroupBy;
106071 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
106076 /* Add the output column list to the name-context before parsing the
106082 ** re-evaluated for each reference to it.
106085 sNC.uNC.pEList = p->pEList;
106087 if( p->pHaving ){
106088 if( (p->selFlags & SF_Aggregate)==0 ){
106089 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
106092 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
106094 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
106096 /* Resolve names in table-valued-function arguments */
106097 for(i=0; i<p->pSrc->nSrc; i++){
106098 SrcItem *pItem = &p->pSrc->a[i];
106099 if( pItem->fg.isTabFunc
106100 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
106109 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
106110 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
106111 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
106126 ** the sub-query back to the parent query. At this point each term
106130 if( p->selFlags & SF_Converted ){
106131 Select *pSub = p->pSrc->a[0].pSelect;
106132 p->pOrderBy = pSub->pOrderBy;
106133 pSub->pOrderBy = 0;
106138 ** below, after all of the result-sets for all of the elements of
106141 ** If there is an ORDER BY clause on a term of a compound-select other
106142 ** than the right-most term, then that is a syntax error. But the error
106146 if( p->pOrderBy!=0
106147 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
106148 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
106152 if( db->mallocFailed ){
106163 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
106166 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
106167 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
106177 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
106178 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
106184 p = p->pPrior;
106200 ** table columns and result-set columns. At the same time, do error
106208 ** the symbolic name assigned to an ATTACH-ed database.
106219 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
106223 ** To resolve result-set references, look for expression nodes of the
106224 ** form Z (with no X and Y prefix) where the Z matches the right-hand
106225 ** size of an AS clause in the result-set of a SELECT. The Z expression
106226 ** is replaced by a copy of the left-hand side of the result-set expression.
106227 ** Table-name and function resolution occurs on the substituted expression
106254 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106255 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106256 w.pParse = pNC->pParse;
106258 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
106262 w.pParse->nHeight += pExpr->nHeight;
106263 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
106269 w.pParse->nHeight -= pExpr->nHeight;
106273 testcase( pNC->ncFlags & NC_HasAgg );
106274 testcase( pNC->ncFlags & NC_HasWin );
106275 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
106276 pNC->ncFlags |= savedHasAgg;
106277 return pNC->nNcErr>0 || w.pParse->nErr>0;
106293 w.pParse = pNC->pParse;
106298 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106299 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106300 for(i=0; i<pList->nExpr; i++){
106301 Expr *pExpr = pList->a[i].pExpr;
106304 w.pParse->nHeight += pExpr->nHeight;
106305 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
106311 w.pParse->nHeight -= pExpr->nHeight;
106315 testcase( pNC->ncFlags & NC_HasAgg );
106316 testcase( pNC->ncFlags & NC_HasWin );
106317 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
106318 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
106319 savedHasAgg |= pNC->ncFlags &
106321 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
106323 if( w.pParse->nErr>0 ) return WRC_Abort;
106325 pNC->ncFlags |= savedHasAgg;
106331 ** decendents of the SELECT, including compounds off of p->pPrior,
106362 ** ------------
106370 ** nodes of the expression is set to -1 and the Expr.iColumn value is
106382 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
106383 NameContext sNC; /* Name context for pParse->pNewTable */
106393 sSrc.a[0].zName = pTab->zName;
106395 sSrc.a[0].iCursor = -1;
106396 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
106397 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
106436 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
106437 return pTab->aCol[iCol].affinity;
106444 ** or a sub-select with a column as the return value, then the
106459 assert( pExpr->op==TK_COLLATE
106460 || pExpr->op==TK_IF_NULL_ROW
106461 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
106462 pExpr = pExpr->pLeft;
106465 op = pExpr->op;
106466 if( op==TK_REGISTER ) op = pExpr->op2;
106469 assert( pExpr->y.pTab!=0 );
106470 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
106474 assert( pExpr->x.pSelect!=0 );
106475 assert( pExpr->x.pSelect->pEList!=0 );
106476 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
106477 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
106482 return sqlite3AffinityType(pExpr->u.zToken, 0);
106486 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
106487 assert( pExpr->iColumn < pExpr->iTable );
106488 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
106490 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
106495 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
106497 return pExpr->affExpr;
106505 ** If a memory allocation error occurs, that fact is recorded in pParse->db
106514 if( pCollName->n>0 ){
106515 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
106517 pNew->pLeft = pExpr;
106518 pNew->flags |= EP_Collate|EP_Skip;
106540 assert( pExpr->op==TK_COLLATE );
106541 pExpr = pExpr->pLeft;
106555 assert( pExpr->x.pList->nExpr>0 );
106556 assert( pExpr->op==TK_FUNCTION );
106557 pExpr = pExpr->x.pList->a[0].pExpr;
106559 assert( pExpr->op==TK_COLLATE );
106560 pExpr = pExpr->pLeft;
106581 sqlite3 *db = pParse->db;
106585 int op = p->op;
106586 if( op==TK_REGISTER ) op = p->op2;
106590 assert( p->y.pTab!=0 );
106591 if( (j = p->iColumn)>=0 ){
106592 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
106598 p = p->pLeft;
106603 p = p->x.pList->a[0].pExpr;
106608 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
106611 if( p->flags & EP_Collate ){
106612 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
106613 p = p->pLeft;
106615 Expr *pNext = p->pRight;
106618 assert( p->x.pList==0 || p->pRight==0 );
106619 if( p->x.pList!=0 && !db->mallocFailed ){
106621 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
106622 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
106623 pNext = p->x.pList->a[i].pExpr;
106652 if( p==0 ) p = pParse->db->pDfltColl;
106663 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
106695 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
106696 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
106697 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
106698 assert( pExpr->pLeft );
106699 aff = sqlite3ExprAffinity(pExpr->pLeft);
106700 if( pExpr->pRight ){
106701 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
106703 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
106760 if( pLeft->flags & EP_Collate ){
106762 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
106783 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
106785 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
106806 if( pParse->nErr ) return 0;
106813 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
106815 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
106835 ** is a sub-select, return the number of columns in the sub-select. For
106839 u8 op = pExpr->op;
106840 if( op==TK_REGISTER ) op = pExpr->op2;
106843 return pExpr->x.pList->nExpr;
106846 return pExpr->x.pSelect->pEList->nExpr;
106853 ** Return a pointer to a subexpression of pVector that is the i-th
106863 ** just the expression for the i-th term of the result set, and may
106868 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
106870 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
106871 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
106873 return pVector->x.pSelect->pEList->a[i].pExpr;
106876 return pVector->x.pList->a[i].pExpr;
106885 ** the iField-th column of the vector expression pVector.
106905 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
106910 if( pVector->op==TK_SELECT ){
106918 ** pLeft->iTable: First in an array of register holding result, or 0
106930 pRet->iTable = nField;
106931 pRet->iColumn = iField;
106932 pRet->pLeft = pVector;
106935 if( pVector->op==TK_VECTOR ){
106938 ppVector = &pVector->x.pList->a[iField].pExpr;
106946 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
106954 ** sub-select returns more than one column, the first in an array
106962 if( pExpr->op==TK_SELECT ){
106970 ** Argument pVector points to a vector expression - either a TK_VECTOR
106978 ** containing the results of the sub-select.
106995 u8 op = pVector->op;
106999 return pVector->iTable+iField;
107003 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
107008 *ppExpr = pVector->x.pList->a[iField].pExpr;
107021 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
107022 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
107023 ** otherwise: op==pExpr->op and p5==0
107032 Vdbe *v = pParse->pVdbe;
107033 Expr *pLeft = pExpr->pLeft;
107034 Expr *pRight = pExpr->pRight;
107045 if( pParse->nErr ) return;
107050 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
107051 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
107052 || pExpr->op==TK_LT || pExpr->op==TK_GT
107053 || pExpr->op==TK_LE || pExpr->op==TK_GE
107055 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
107056 || (pExpr->op==TK_ISNOT && op==TK_NE) );
107057 assert( p5==0 || pExpr->op!=op );
107058 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
107086 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
107096 if( i==nLeft-1 ){
107104 if( i==nLeft-2 ) opx = op;
107122 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
107143 if( p->nHeight>*pnHeight ){
107144 *pnHeight = p->nHeight;
107151 for(i=0; i<p->nExpr; i++){
107152 heightOfExpr(p->a[i].pExpr, pnHeight);
107158 for(p=pSelect; p; p=p->pPrior){
107159 heightOfExpr(p->pWhere, pnHeight);
107160 heightOfExpr(p->pHaving, pnHeight);
107161 heightOfExpr(p->pLimit, pnHeight);
107162 heightOfExprList(p->pEList, pnHeight);
107163 heightOfExprList(p->pGroupBy, pnHeight);
107164 heightOfExprList(p->pOrderBy, pnHeight);
107179 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
107180 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
107181 nHeight = p->pRight->nHeight;
107184 heightOfSelect(p->x.pSelect, &nHeight);
107185 }else if( p->x.pList ){
107186 heightOfExprList(p->x.pList, &nHeight);
107187 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
107189 p->nHeight = nHeight + 1;
107201 if( pParse->nErr ) return;
107203 sqlite3ExprCheckHeight(pParse, p->nHeight);
107221 if( pParse->nErr ) return;
107222 if( p && ExprUseXList(p) && p->x.pList ){
107223 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
107240 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
107244 ** can be translated into a 32-bit integer, then the token is not
107261 if( op!=TK_INTEGER || pToken->z==0
107262 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
107263 nExtra = pToken->n+1;
107270 pNew->op = (u8)op;
107271 pNew->iAgg = -1;
107274 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
107275 pNew->u.iValue = iValue;
107277 pNew->u.zToken = (char*)&pNew[1];
107278 assert( pToken->z!=0 || pToken->n==0 );
107279 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
107280 pNew->u.zToken[pToken->n] = 0;
107281 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
107287 pNew->nHeight = 1;
107294 ** Allocate a new expression node from a zero-terminated token that has
107321 assert( db->mallocFailed );
107326 assert( pRoot->x.pSelect==0 );
107328 pRoot->pRight = pRight;
107329 pRoot->flags |= EP_Propagate & pRight->flags;
107331 pRoot->nHeight = pRight->nHeight+1;
107333 pRoot->nHeight = 1;
107337 pRoot->pLeft = pLeft;
107338 pRoot->flags |= EP_Propagate & pLeft->flags;
107340 if( pLeft->nHeight>=pRoot->nHeight ){
107341 pRoot->nHeight = pLeft->nHeight+1;
107352 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
107362 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
107365 p->op = op & 0xff;
107366 p->iAgg = -1;
107367 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
107368 sqlite3ExprCheckHeight(pParse, p->nHeight);
107370 sqlite3ExprDelete(pParse->db, pLeft);
107371 sqlite3ExprDelete(pParse->db, pRight);
107382 pExpr->x.pSelect = pSelect;
107386 assert( pParse->db->mallocFailed );
107387 sqlite3SelectDelete(pParse->db, pSelect);
107414 for(ii=0; ii<pEList->nExpr; ii++){
107416 Expr *pExpr = pEList->a[ii].pExpr;
107418 if( pExpr->op==TK_VECTOR ){
107420 nExprElem = pExpr->x.pList->nExpr;
107425 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
107431 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
107432 pExpr->x.pList = 0;
107435 pSel->op = TK_ALL;
107436 pSel->pPrior = pRet;
107442 if( pRet && pRet->pPrior ){
107443 pRet->selFlags |= SF_MultiValue;
107445 sqlite3ExprListDelete(pParse->db, pEList);
107458 sqlite3 *db = pParse->db;
107485 sqlite3 *db = pParse->db;
107493 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
107495 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
107496 && !pParse->nested
107500 pNew->x.pList = pList;
107512 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
107514 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
107515 ** top-level SQL
107525 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
107527 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
107528 || (pParse->db->flags & SQLITE_TrustedSchema)==0
107559 sqlite3 *db = pParse->db;
107565 z = pExpr->u.zToken;
107572 x = (ynVar)(++pParse->nVar);
107580 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
107581 i = z[1]-'0'; /* The common case of ?N for a single digit N */
107584 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
107588 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
107589 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
107590 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
107592 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
107593 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107597 if( x>pParse->nVar ){
107598 pParse->nVar = (int)x;
107600 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
107608 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
107610 x = (ynVar)(++pParse->nVar);
107615 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
107618 pExpr->iColumn = x;
107619 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
107621 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107631 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
107633 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
107634 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
107637 assert( p->pLeft==0 );
107638 assert( p->pRight==0 );
107639 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
107640 assert( !ExprUseXList(p) || p->x.pList==0 );
107645 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
107646 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
107647 if( p->pRight ){
107649 sqlite3ExprDeleteNN(db, p->pRight);
107652 sqlite3SelectDelete(db, p->x.pSelect);
107654 sqlite3ExprListDelete(db, p->x.pList);
107657 sqlite3WindowDelete(db, p->y.pWin);
107676 }else if( p->pOn ){
107677 sqlite3ExprDeleteNN(db, p->pOn);
107678 }else if( p->pUsing ){
107679 sqlite3IdListDelete(db, p->pUsing);
107691 ** pExpr to the pParse->pConstExpr list with a register number of 0.
107707 sqlite3ExprDeleteNN(pParse->db, p);
107733 ** The dupedExprStructSize() function returns two values OR-ed together:
107746 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
107761 if( 0==flags || p->op==TK_SELECT_COLUMN
107771 if( p->pLeft || p->x.pList ){
107774 assert( p->pRight==0 );
107788 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107789 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
107812 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
107821 ** to store the copy of expression p, the copies of p->u.zToken
107822 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
107851 ** by the copy of the p->u.zToken string (if any).
107856 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107857 nToken = sqlite3Strlen30(p->u.zToken) + 1;
107868 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
107873 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
107874 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
107875 pNew->flags |= staticFlag;
107881 /* Copy the p->u.zToken string, if any. */
107883 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
107884 memcpy(zToken, p->u.zToken, nToken);
107887 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
107888 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
107890 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
107892 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
107896 /* Fill in pNew->pLeft and pNew->pRight. */
107900 pNew->pLeft = p->pLeft ?
107901 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
107902 pNew->pRight = p->pRight ?
107903 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
107907 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
107916 if( pNew->op==TK_SELECT_COLUMN ){
107917 pNew->pLeft = p->pLeft;
107918 assert( p->pRight==0 || p->pRight==p->pLeft
107919 || ExprHasProperty(p->pLeft, EP_Subquery) );
107921 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
107923 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
107933 ** and the db->mallocFailed flag set.
107939 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
107943 pRet->nCte = p->nCte;
107944 for(i=0; i<p->nCte; i++){
107945 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
107946 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
107947 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
107948 pRet->a[i].eM10d = p->a[i].eM10d;
107963 ** objects found there, assembling them onto the linked list at Select->pWin.
107966 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
107967 Select *pSelect = pWalker->u.pSelect;
107968 Window *pWin = pExpr->y.pWin;
107971 assert( pWin->ppThis==0 );
107977 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
108006 ** part of the in-memory representation of the database schema.
108023 pNew->nExpr = p->nExpr;
108024 pNew->nAlloc = p->nAlloc;
108025 pItem = pNew->a;
108026 pOldItem = p->a;
108027 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
108028 Expr *pOldExpr = pOldItem->pExpr;
108030 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
108032 && pOldExpr->op==TK_SELECT_COLUMN
108033 && (pNewExpr = pItem->pExpr)!=0
108035 if( pNewExpr->pRight ){
108036 pPriorSelectColOld = pOldExpr->pRight;
108037 pPriorSelectColNew = pNewExpr->pRight;
108038 pNewExpr->pLeft = pNewExpr->pRight;
108040 if( pOldExpr->pLeft!=pPriorSelectColOld ){
108041 pPriorSelectColOld = pOldExpr->pLeft;
108043 pNewExpr->pRight = pPriorSelectColNew;
108045 pNewExpr->pLeft = pPriorSelectColNew;
108048 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
108049 pItem->fg = pOldItem->fg;
108050 pItem->fg.done = 0;
108051 pItem->u = pOldItem->u;
108070 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
108073 pNew->nSrc = pNew->nAlloc = p->nSrc;
108074 for(i=0; i<p->nSrc; i++){
108075 SrcItem *pNewItem = &pNew->a[i];
108076 const SrcItem *pOldItem = &p->a[i];
108078 pNewItem->pSchema = pOldItem->pSchema;
108079 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
108080 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108081 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
108082 pNewItem->fg = pOldItem->fg;
108083 pNewItem->iCursor = pOldItem->iCursor;
108084 pNewItem->addrFillSub = pOldItem->addrFillSub;
108085 pNewItem->regReturn = pOldItem->regReturn;
108086 if( pNewItem->fg.isIndexedBy ){
108087 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
108089 pNewItem->u2 = pOldItem->u2;
108090 if( pNewItem->fg.isCte ){
108091 pNewItem->u2.pCteUse->nUse++;
108093 if( pNewItem->fg.isTabFunc ){
108094 pNewItem->u1.pFuncArg =
108095 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
108097 pTab = pNewItem->pTab = pOldItem->pTab;
108099 pTab->nTabRef++;
108101 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
108102 if( pOldItem->fg.isUsing ){
108103 assert( pNewItem->fg.isUsing );
108104 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
108106 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
108108 pNewItem->colUsed = pOldItem->colUsed;
108117 assert( p->eU4!=EU4_EXPR );
108118 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
108120 pNew->nId = p->nId;
108121 pNew->eU4 = p->eU4;
108122 for(i=0; i<p->nId; i++){
108123 struct IdList_item *pNewItem = &pNew->a[i];
108124 const struct IdList_item *pOldItem = &p->a[i];
108125 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108126 pNewItem->u4 = pOldItem->u4;
108137 for(p=pDup; p; p=p->pPrior){
108140 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
108141 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
108142 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
108143 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
108144 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
108145 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
108146 pNew->op = p->op;
108147 pNew->pNext = pNext;
108148 pNew->pPrior = 0;
108149 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
108150 pNew->iLimit = 0;
108151 pNew->iOffset = 0;
108152 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
108153 pNew->addrOpenEphm[0] = -1;
108154 pNew->addrOpenEphm[1] = -1;
108155 pNew->nSelectRow = p->nSelectRow;
108156 pNew->pWith = sqlite3WithDup(db, p->pWith);
108158 pNew->pWin = 0;
108159 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
108160 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
108162 pNew->selId = p->selId;
108163 if( db->mallocFailed ){
108167 pNew->pNext = 0;
108172 pp = &pNew->pPrior;
108193 ** Reason: This routine assumes that the number of slots in pList->a[]
108198 ** NULL is returned. If non-NULL is returned, then it is guaranteed
108209 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
108214 pList->nAlloc = 4;
108215 pList->nExpr = 1;
108216 pItem = &pList->a[0];
108218 pItem->pExpr = pExpr;
108228 pList->nAlloc *= 2;
108230 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
108238 pItem = &pList->a[pList->nExpr++];
108240 pItem->pExpr = pExpr;
108250 return sqlite3ExprListAppendNew(pParse->db,pExpr);
108252 if( pList->nAlloc<pList->nExpr+1 ){
108253 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
108255 pItem = &pList->a[pList->nExpr++];
108257 pItem->pExpr = pExpr;
108278 sqlite3 *db = pParse->db;
108281 int iFirst = pList ? pList->nExpr : 0;
108292 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
108294 pColumns->nId, n);
108298 for(i=0; i<pColumns->nId; i++){
108299 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
108300 assert( pSubExpr!=0 || db->mallocFailed );
108304 assert( pList->nExpr==iFirst+i+1 );
108305 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
108306 pColumns->a[i].zName = 0;
108310 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
108311 Expr *pFirst = pList->a[iFirst].pExpr;
108313 assert( pFirst->op==TK_SELECT_COLUMN );
108317 pFirst->pRight = pExpr;
108322 pFirst->iTable = pColumns->nId;
108337 assert( p->nExpr>0 );
108349 pItem = &p->a[p->nExpr-1];
108350 assert( pItem->fg.bNulls==0 );
108354 pItem->fg.sortFlags = (u8)iSortOrder;
108357 pItem->fg.bNulls = 1;
108359 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
108369 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
108378 assert( pList!=0 || pParse->db->mallocFailed!=0 );
108379 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
108382 assert( pList->nExpr>0 );
108383 pItem = &pList->a[pList->nExpr-1];
108384 assert( pItem->zEName==0 );
108385 assert( pItem->fg.eEName==ENAME_NAME );
108386 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
108388 /* If dequote==0, then pName->z does not point to part of a DDL
108390 ** to the token-map. */
108391 sqlite3Dequote(pItem->zEName);
108393 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
108404 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
108413 sqlite3 *db = pParse->db;
108414 assert( pList!=0 || db->mallocFailed!=0 );
108416 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
108417 assert( pList->nExpr>0 );
108418 if( pItem->zEName==0 ){
108419 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
108420 pItem->fg.eEName = ENAME_SPAN;
108434 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
108435 testcase( pEList && pEList->nExpr==mx );
108436 testcase( pEList && pEList->nExpr==mx+1 );
108437 if( pEList && pEList->nExpr>mx ){
108446 int i = pList->nExpr;
108447 struct ExprList_item *pItem = pList->a;
108448 assert( pList->nExpr>0 );
108451 sqlite3ExprDelete(db, pItem->pExpr);
108452 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
108454 }while( --i>0 );
108462 ** Return the bitwise-OR of all Expr.flags fields in the given
108469 for(i=0; i<pList->nExpr; i++){
108470 Expr *pExpr = pList->a[i].pExpr;
108472 m |= pExpr->flags;
108478 ** This is a SELECT-node callback for the expression walker that
108480 ** pWalker->eCode to zero and abort.
108486 pWalker->eCode = 0;
108507 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
108512 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
108514 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
108516 pExpr->op = TK_TRUEFALSE;
108529 assert( pExpr->op==TK_TRUEFALSE );
108531 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
108532 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
108533 return pExpr->u.zToken[4]==0;
108551 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
108552 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
108553 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
108555 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
108557 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
108572 ** sqlite3ExprIsConstant() pWalker->eCode==1
108573 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
108574 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
108575 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
108592 /* If pWalker->eCode is 2 then any term of the expression that comes from
108595 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
108596 pWalker->eCode = 0;
108600 switch( pExpr->op ){
108602 ** and either pWalker->eCode==4 or 5 or the function has the
108605 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
108608 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
108611 pWalker->eCode = 0;
108624 testcase( pExpr->op==TK_ID );
108625 testcase( pExpr->op==TK_COLUMN );
108626 testcase( pExpr->op==TK_AGG_FUNCTION );
108627 testcase( pExpr->op==TK_AGG_COLUMN );
108628 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
108631 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
108638 testcase( pExpr->op==TK_REGISTER );
108639 testcase( pExpr->op==TK_IF_NULL_ROW );
108640 testcase( pExpr->op==TK_DOT );
108641 pWalker->eCode = 0;
108644 if( pWalker->eCode==5 ){
108648 pExpr->op = TK_NULL;
108649 }else if( pWalker->eCode==4 ){
108652 pWalker->eCode = 0;
108657 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
108658 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
108676 ** Walk an expression tree. Return non-zero if the expression is constant
108679 ** For the purposes of this function, a double-quoted string (ex: "abc")
108680 ** is considered a variable but a single-quoted string (ex: 'abc') is
108688 ** Walk an expression tree. Return non-zero if
108697 ** can be added to the pParse->pConstExpr list and evaluated once when
108705 ** Walk an expression tree. Return non-zero if the expression is constant
108707 ** expression must not refer to any non-deterministic function nor any
108722 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
108724 ** (2) pExpr cannot use subqueries or non-deterministic functions.
108738 if( pSrc->fg.jointype & JT_LTORJ ){
108741 if( pSrc->fg.jointype & JT_LEFT ){
108743 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
108747 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
108755 ExprList *pGroupBy = pWalker->u.pGroupBy;
108760 for(i=0; i<pGroupBy->nExpr; i++){
108761 Expr *p = pGroupBy->a[i].pExpr;
108762 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
108763 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
108770 /* Check if pExpr is a sub-select. If so, consider it variable. */
108772 pWalker->eCode = 0;
108780 ** Walk the expression tree passed as the first argument. Return non-zero
108788 ** assumes that no other collating sequence will have a finer-grained
108811 ** in a CREATE TABLE statement. Return non-zero if the expression is
108812 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
108826 ** For the purposes of this function, a double-quoted string (ex: "abc")
108827 ** is considered a variable but a single-quoted string (ex: 'abc') is
108855 ** to fit in a 32-bit integer, return 1 and put the value of the integer
108857 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
108863 /* If an expression is an integer literal that fits in a signed 32-bit
108865 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
108866 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
108868 if( p->flags & EP_IntValue ){
108869 *pValue = p->u.iValue;
108872 switch( p->op ){
108874 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
108879 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
108881 *pValue = -v;
108908 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108909 p = p->pLeft;
108912 op = p->op;
108913 if( op==TK_REGISTER ) op = p->op2;
108923 p->y.pTab==0 || /* Reference to column of index on expression */
108924 (p->iColumn>=0
108925 && p->y.pTab->aCol!=0 /* Possible due to prior error */
108926 && p->y.pTab->aCol[p->iColumn].notNull==0);
108946 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108947 if( p->op==TK_UMINUS ) unaryMinus = 1;
108948 p = p->pLeft;
108950 op = p->op;
108951 if( op==TK_REGISTER ) op = p->op2;
108966 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
108967 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
108976 ** Return TRUE if the given string is a row-id column name.
109001 p = pX->x.pSelect;
109002 if( p->pPrior ) return 0; /* Not a compound SELECT */
109003 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
109004 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
109005 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
109008 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
109009 if( p->pLimit ) return 0; /* Has no LIMIT clause */
109010 if( p->pWhere ) return 0; /* Has no WHERE clause */
109011 pSrc = p->pSrc;
109013 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
109014 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
109015 pTab = pSrc->a[0].pTab;
109019 pEList = p->pEList;
109022 for(i=0; i<pEList->nExpr; i++){
109023 Expr *pRes = pEList->a[i].pExpr;
109024 if( pRes->op!=TK_COLUMN ) return 0;
109025 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
109033 ** Generate code that checks the left-most column of index table iCur to see if
109035 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
109053 ** right-hand side. Return TRUE if that list is constant.
109059 pLHS = pIn->pLeft;
109060 pIn->pLeft = 0;
109062 pIn->pLeft = pLHS;
109072 ** The job of this routine is to find or create a b-tree object that can
109076 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
109079 ** The returned value of this function indicates the b-tree type, as follows:
109081 ** IN_INDEX_ROWID - The cursor was opened on a database table.
109082 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
109083 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
109084 ** IN_INDEX_EPH - The cursor was opened on a specially created and
109086 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
109089 ** An existing b-tree might be used if the RHS expression pX is a simple
109096 ** pX->iTable made to point to the ephemeral table instead of an
109099 ** will be set on pX and the pX->y.sub fields will be set to show where
109108 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
109109 ** through the set members) then the b-tree must not contain duplicates.
109111 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
109114 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
109117 ** index can be found with the specified <columns> as its left-most.
109121 ** routine might decide that creating an ephemeral b-tree for membership
109126 ** When the b-tree is being used for membership tests, the calling function
109135 ** the value in that register will be NULL if the b-tree contains one or more
109136 ** NULL values, and it will be some non-NULL value if the b-tree contains no
109165 assert( pX->op==TK_IN );
109167 iTab = pParse->nTab++;
109176 ExprList *pEList = pX->x.pSelect->pEList;
109177 for(i=0; i<pEList->nExpr; i++){
109178 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
109180 if( i==pEList->nExpr ){
109188 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
109189 sqlite3 *db = pParse->db; /* Database connection */
109192 ExprList *pEList = p->pEList;
109193 int nExpr = pEList->nExpr;
109195 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
109196 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
109197 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
109198 pTab = p->pSrc->a[0].pTab;
109201 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
109204 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
109207 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
109215 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
109227 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
109228 int iCol = pEList->a[i].pExpr->iColumn;
109250 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
109253 if( pIdx->nColumn<nExpr ) continue;
109254 if( pIdx->pPartIdxWhere!=0 ) continue;
109255 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
109257 testcase( pIdx->nColumn==BMS-2 );
109258 testcase( pIdx->nColumn==BMS-1 );
109259 if( pIdx->nColumn>=BMS-1 ) continue;
109261 if( pIdx->nKeyCol>nExpr
109262 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
109270 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
109271 Expr *pRhs = pEList->a[i].pExpr;
109275 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
109277 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
109278 assert( pIdx->azColl[j] );
109279 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
109291 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
109292 if( colUsed==(MASKBIT(nExpr)-1) ){
109296 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
109297 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
109299 VdbeComment((v, "%s", pIdx->zName));
109301 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
109305 i64 mask = (1<<nExpr)-1;
109309 *prRhsHasNull = ++pParse->nMem;
109331 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
109333 pParse->nTab--; /* Back out the allocation of the unused cursor */
109334 iTab = -1; /* Cursor is not allocated */
109339 /* Could not find an existing table or index to use as the RHS b-tree.
109342 u32 savedNQueryLoop = pParse->nQueryLoop;
109346 pParse->nQueryLoop = 0;
109348 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
109350 assert( pX->op==TK_IN );
109355 pParse->nQueryLoop = savedNQueryLoop;
109360 n = sqlite3ExprVectorSize(pX->pLeft);
109371 ** function allocates and returns a nul-terminated string containing
109378 Expr *pLeft = pExpr->pLeft;
109380 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
109383 assert( pExpr->op==TK_IN );
109384 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
109391 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
109407 ** "sub-select returns N columns - expected M"
109410 if( pParse->nErr==0 ){
109411 const char *zFmt = "sub-select returns %d columns - expected %d";
109419 ** it is not permitted. If pExpr is a sub-select vector, this routine
109422 ** "sub-select returns N columns - expected 1"
109431 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
109445 ** x IN (4,5,11) -- IN operator with list on right-hand side
109446 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
109450 ** table is computed, the cursor number is also stored in pExpr->iTable,
109474 v = pParse->pVdbe;
109480 ** * The right-hand side is a correlated subquery
109481 ** * The right-hand side is an expression list containing variables
109487 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
109496 pExpr->x.pSelect->selId));
109499 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
109500 pExpr->y.sub.iAddr);
109501 assert( iTab!=pExpr->iTable );
109502 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
109511 pExpr->y.sub.regReturn = ++pParse->nMem;
109512 pExpr->y.sub.iAddr =
109513 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
109519 pLeft = pExpr->pLeft;
109525 pExpr->iTable = iTab;
109526 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
109529 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
109534 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
109542 Select *pSelect = pExpr->x.pSelect;
109543 ExprList *pEList = pSelect->pEList;
109546 addrOnce?"":"CORRELATED ", pSelect->selId
109550 if( ALWAYS(pEList->nExpr==nVal) ){
109557 pSelect->iLimit = 0;
109558 testcase( pSelect->selFlags & SF_Distinct );
109560 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
109561 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
109562 sqlite3SelectDelete(pParse->db, pCopy);
109563 sqlite3DbFree(pParse->db, dest.zAffSdst);
109570 assert( pEList->nExpr>0 );
109574 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
109575 pParse, p, pEList->a[i].pExpr
109579 }else if( ALWAYS(pExpr->x.pList!=0) ){
109589 ExprList *pList = pExpr->x.pList;
109600 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109606 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
109607 Expr *pE2 = pItem->pExpr;
109611 ** this code only executes once. Because for a non-constant
109615 sqlite3VdbeChangeToNoop(v, addrOnce-1);
109637 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109638 || pParse->nErr );
109639 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109640 pExpr->y.sub.iAddr, 1);
109651 ** (SELECT a FROM b) -- subquery
109652 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
109656 ** Return the register that holds the result. For a multi-column SELECT,
109658 ** return value is the register of the left-most result column.
109670 Vdbe *v = pParse->pVdbe;
109672 if( pParse->nErr ) return 0;
109673 testcase( pExpr->op==TK_EXISTS );
109674 testcase( pExpr->op==TK_SELECT );
109675 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
109677 pSel = pExpr->x.pSelect;
109682 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
109684 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
109685 pExpr->y.sub.iAddr);
109686 return pExpr->iTable;
109693 pExpr->y.sub.regReturn = ++pParse->nMem;
109694 pExpr->y.sub.iAddr =
109695 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
109700 ** * The right-hand side is a correlated subquery
109701 ** * The right-hand side is an expression list containing variables
109722 addrOnce?"":"CORRELATED ", pSel->selId));
109723 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
109724 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
109725 pParse->nMem += nReg;
109726 if( pExpr->op==TK_SELECT ){
109730 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
109737 if( pSel->pLimit ){
109738 /* The subquery already has a limit. If the pre-existing limit is X
109740 sqlite3 *db = pParse->db;
109743 pLimit->affExpr = SQLITE_AFF_NUMERIC;
109745 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
109747 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
109748 pSel->pLimit->pLeft = pLimit;
109750 /* If there is no pre-existing limit add a limit of 1 */
109751 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
109752 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
109754 pSel->iLimit = 0;
109756 pExpr->op2 = pExpr->op;
109757 pExpr->op = TK_ERROR;
109760 pExpr->iTable = rReg = dest.iSDParm;
109768 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109769 || pParse->nErr );
109770 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109771 pExpr->y.sub.iAddr, 1);
109781 ** sub-select on the RHS of the IN() operator has the same number of
109783 ** a sub-query, that the LHS is a vector of size 1.
109786 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
109787 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
109788 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
109789 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
109793 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
109807 ** The left-hand side (LHS) is a scalar or vector expression. The
109808 ** right-hand side (RHS) is an array of zero or more scalar values, or a
109823 ** See the separate in-operator.md documentation file in the canonical
109847 int addrTop; /* Top of the step-6 loop */
109849 u8 okConstFactor = pParse->okConstFactor;
109852 pLeft = pExpr->pLeft;
109855 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
109857 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
109859 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
109865 v = pParse->pVdbe;
109873 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
109878 ** nVector-1. */
109898 assert( pParse->okConstFactor==okConstFactor );
109899 pParse->okConstFactor = 0;
109901 pParse->okConstFactor = okConstFactor;
109918 ** This is step (1) in the in-operator.md optimized algorithm.
109928 pList = pExpr->x.pList;
109929 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109934 for(ii=0; ii<pList->nExpr; ii++){
109935 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
109936 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
109940 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
109944 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
109945 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
109946 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
109947 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
109978 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
109979 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
109986 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
109991 /* In this case, the RHS is the ROWID of table b-tree and so we also
109992 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
110010 /* Step 4. If the RHS is known to be non-NULL and we did not find
110070 sqlite3DbFree(pParse->db, aiMap);
110071 sqlite3DbFree(pParse->db, zAff);
110078 ** value described by z[0..n-1] into register iMem.
110080 ** The z[] string will probably not be zero-terminated. But the
110089 if( negateFlag ) value = -value;
110098 ** text z[0..n-1] into register iMem.
110100 ** Expr.u.zToken is always UTF8 and zero-terminated.
110103 Vdbe *v = pParse->pVdbe;
110104 if( pExpr->flags & EP_IntValue ){
110105 int i = pExpr->u.iValue;
110107 if( negFlag ) i = -i;
110112 const char *z = pExpr->u.zToken;
110117 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
110122 negFlag?"-":"",pExpr);
110130 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
110138 ** appropriate for the iIdxCol-th column of index pIdx.
110147 i16 iTabCol = pIdx->aiColumn[iIdxCol];
110149 assert( pIdx->aColExpr );
110150 assert( pIdx->aColExpr->nExpr>iIdxCol );
110151 pParse->iSelfTab = iTabCur + 1;
110152 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
110153 pParse->iSelfTab = 0;
110155 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
110172 Vdbe *v = pParse->pVdbe;
110174 assert( pParse->iSelfTab!=0 );
110175 if( pParse->iSelfTab>0 ){
110176 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
110181 if( pCol->affinity>=SQLITE_AFF_TEXT ){
110182 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
110189 ** Generate code to extract the value of the iCol-th column of a table.
110201 if( iCol<0 || iCol==pTab->iPKey ){
110203 VdbeComment((v, "%s.rowid", pTab->zName));
110211 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
110213 if( pCol->colFlags & COLFLAG_BUSY ){
110215 pCol->zCnName);
110217 int savedSelfTab = pParse->iSelfTab;
110218 pCol->colFlags |= COLFLAG_BUSY;
110219 pParse->iSelfTab = iTabCur+1;
110221 pParse->iSelfTab = savedSelfTab;
110222 pCol->colFlags &= ~COLFLAG_BUSY;
110241 ** Generate code that will extract the iColumn-th column from
110255 assert( pParse->pVdbe!=0 );
110256 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
110258 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
110259 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
110265 ** Generate code to move content from registers iFrom...iFrom+nReg-1
110266 ** over to iTo..iTo+nReg-1.
110269 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
110280 p->op2 = p->op;
110281 p->op = TK_REGISTER;
110282 p->iTable = iReg;
110303 if( p->op==TK_SELECT ){
110311 iResult = pParse->nMem+1;
110312 pParse->nMem += nResult;
110315 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
110323 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
110327 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
110334 ** in-line rather than by using the usual callbacks.
110343 Vdbe *v = pParse->pVdbe;
110346 nFarg = pFarg->nExpr;
110347 assert( nFarg>0 ); /* All in-line functions have at least one argument */
110350 /* Attempt a direct implementation of the built-in COALESCE() and
110352 ** arguments past the first non-NULL argument.
110357 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
110361 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
110376 Expr *pArg = pFarg->a[0].pExpr;
110377 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
110378 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
110386 /* The UNLIKELY() function is a no-op. The result is the value
110390 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
110395 ** Test-only SQL functions that are only usable if enabled
110403 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
110412 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
110421 pA1 = pFarg->a[1].pExpr;
110422 if( pA1->op==TK_COLUMN ){
110424 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
110440 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
110442 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
110451 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
110463 for(p=pParse->pIdxExpr; p; p=p->pIENext){
110464 int iDataCur = p->iDataCur;
110466 if( pParse->iSelfTab ){
110467 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
110468 iDataCur = -1;
110470 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
110471 v = pParse->pVdbe;
110473 if( p->bMaybeNullRow ){
110478 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
110480 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
110481 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
110483 p = pParse->pIdxExpr;
110484 pParse->pIdxExpr = 0;
110486 pParse->pIdxExpr = p;
110489 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
110490 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
110494 return -1; /* Not found */
110510 Vdbe *v = pParse->pVdbe; /* The VM under construction */
110513 int regFree1 = 0; /* If non-zero free this temporary register */
110514 int regFree2 = 0; /* If non-zero free this temporary register */
110519 assert( target>0 && target<=pParse->nMem );
110525 }else if( pParse->pIdxExpr!=0
110532 op = pExpr->op;
110536 AggInfo *pAggInfo = pExpr->pAggInfo;
110539 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
110540 pCol = &pAggInfo->aCol[pExpr->iAgg];
110541 if( !pAggInfo->directMode ){
110542 assert( pCol->iMem>0 );
110543 return pCol->iMem;
110544 }else if( pAggInfo->useSortingIdx ){
110545 Table *pTab = pCol->pTab;
110546 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
110547 pCol->iSorterColumn, target);
110548 if( pCol->iColumn<0 ){
110549 VdbeComment((v,"%s.rowid",pTab->zName));
110552 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
110553 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
110563 int iTab = pExpr->iTable;
110567 ** constraints, and that constant is coded by the pExpr->pLeft
110573 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
110575 assert( pExpr->y.pTab!=0 );
110576 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
110582 &zAff[(aff-'B')*2], P4_STATIC);
110587 if( pParse->iSelfTab<0 ){
110591 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
110597 int iCol = pExpr->iColumn;
110599 pTab = pExpr->y.pTab;
110602 assert( iCol<pTab->nCol );
110604 return -1-pParse->iSelfTab;
110606 pCol = pTab->aCol + iCol;
110608 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
110610 if( pCol->colFlags & COLFLAG_GENERATED ){
110611 if( pCol->colFlags & COLFLAG_BUSY ){
110613 pCol->zCnName);
110616 pCol->colFlags |= COLFLAG_BUSY;
110617 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
110620 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
110624 if( pCol->affinity==SQLITE_AFF_REAL ){
110634 iTab = pParse->iSelfTab - 1;
110638 assert( pExpr->y.pTab!=0 );
110639 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
110640 pExpr->iColumn, iTab, target,
110641 pExpr->op2);
110655 codeReal(v, pExpr->u.zToken, 0, target);
110661 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
110669 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
110679 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
110680 assert( pExpr->u.zToken[1]=='\'' );
110681 z = &pExpr->u.zToken[2];
110682 n = sqlite3Strlen30(z) - 1;
110691 assert( pExpr->u.zToken!=0 );
110692 assert( pExpr->u.zToken[0]!=0 );
110693 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
110694 if( pExpr->u.zToken[1]!=0 ){
110695 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
110696 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
110697 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
110703 return pExpr->iTable;
110708 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
110715 sqlite3AffinityType(pExpr->u.zToken, 0));
110723 /* fall-through */
110730 Expr *pLeft = pExpr->pLeft;
110735 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
110737 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
110779 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110780 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
110787 Expr *pLeft = pExpr->pLeft;
110789 if( pLeft->op==TK_INTEGER ){
110793 }else if( pLeft->op==TK_FLOAT ){
110795 codeReal(v, pLeft->u.zToken, 1, target);
110804 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
110814 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110822 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110824 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
110825 bNormal = pExpr->op2==TK_IS;
110837 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
110847 AggInfo *pInfo = pExpr->pAggInfo;
110849 || NEVER(pExpr->iAgg<0)
110850 || NEVER(pExpr->iAgg>=pInfo->nFunc)
110855 return pInfo->aFunc[pExpr->iAgg].iMem;
110866 sqlite3 *db = pParse->db; /* The database connection */
110872 return pExpr->y.pWin->regResult;
110879 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
110883 pFarg = pExpr->x.pList;
110884 nFarg = pFarg ? pFarg->nExpr : 0;
110886 zId = pExpr->u.zToken;
110889 if( pDef==0 && pParse->explain ){
110893 if( pDef==0 || pDef->xFinalize!=0 ){
110897 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
110898 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
110899 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
110901 SQLITE_PTR_TO_INT(pDef->pUserData), target);
110902 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
110907 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
110911 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
110912 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
110917 r1 = pParse->nMem+1;
110918 pParse->nMem += nFarg;
110928 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
110931 assert( pFarg->a[0].pExpr!=0 );
110932 exprOp = pFarg->a[0].pExpr->op;
110936 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
110937 pFarg->a[0].pExpr->op2 =
110938 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
110961 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
110963 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
110966 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
110967 if( !pColl ) pColl = db->pDfltColl;
110971 pDef, pExpr->op2);
110987 if( pParse->db->mallocFailed ){
110991 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
111001 Expr *pLeft = pExpr->pLeft;
111002 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
111003 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
111004 pLeft->op2 = pParse->withinRJSubrtn;
111006 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
111008 if( pExpr->iTable!=n ){
111010 pExpr->iTable, n);
111012 return pLeft->iTable + pExpr->iColumn;
111035 ** X is stored in pExpr->pLeft.
111036 ** Y is stored in pExpr->pList->a[0].pExpr.
111037 ** Z is stored in pExpr->pList->a[1].pExpr.
111045 && ALWAYS(pExpr->pLeft)
111046 && pExpr->pLeft->op==TK_FUNCTION
111048 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
111056 pExpr = pExpr->pLeft;
111057 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
111062 pExpr = pExpr->pLeft;
111063 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
111068 ** to a column in the new.* or old.* pseudo-tables available to
111070 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
111071 ** is set to the column of the pseudo-table to read, or to -1 to
111076 ** to reference another column of the old.* pseudo-table, where
111078 ** set to (n+1), where n is the number of columns in each pseudo-table.
111079 ** For a reference to any other column in the new.* pseudo-table, p1
111088 ** p1==0 -> old.rowid p1==3 -> new.rowid
111089 ** p1==1 -> old.a p1==4 -> new.a
111090 ** p1==2 -> old.b p1==5 -> new.b
111097 pTab = pExpr->y.pTab;
111098 iCol = pExpr->iColumn;
111099 p1 = pExpr->iTable * (pTab->nCol+1) + 1
111102 assert( pExpr->iTable==0 || pExpr->iTable==1 );
111103 assert( iCol>=-1 && iCol<pTab->nCol );
111104 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
111105 assert( p1>=0 && p1<(pTab->nCol*2+2) );
111109 (pExpr->iTable ? "new" : "old"),
111110 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
111117 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
111119 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
111132 ** that derive from the right-hand table of a LEFT JOIN. The
111133 ** Expr.iTable value is the table number for the right-hand table.
111139 u8 okConstFactor = pParse->okConstFactor;
111140 AggInfo *pAggInfo = pExpr->pAggInfo;
111142 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
111143 if( !pAggInfo->directMode ){
111144 inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
111147 if( pExpr->pAggInfo->useSortingIdx ){
111148 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
111149 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
111155 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
111158 ** really constant because they originate from the right-hand side
111160 pParse->okConstFactor = 0;
111161 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
111162 pParse->okConstFactor = okConstFactor;
111179 ** X (if it exists) is in pExpr->pLeft.
111180 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
111183 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
111200 sqlite3 *db = pParse->db;
111202 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
111203 assert(pExpr->x.pList->nExpr > 0);
111204 pEList = pExpr->x.pList;
111205 aListelem = pEList->a;
111206 nExpr = pEList->nExpr;
111208 if( (pX = pExpr->pLeft)!=0 ){
111210 if( db->mallocFailed ){
111214 testcase( pX->op==TK_COLUMN );
111222 ** The value in regFree1 might get SCopy-ed into the file result.
111227 for(i=0; i<nExpr-1; i=i+2){
111235 testcase( pTest->op==TK_COLUMN );
111237 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
111243 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
111254 assert( pExpr->affExpr==OE_Rollback
111255 || pExpr->affExpr==OE_Abort
111256 || pExpr->affExpr==OE_Fail
111257 || pExpr->affExpr==OE_Ignore
111259 if( !pParse->pTriggerTab && !pParse->nested ){
111261 "RAISE() may only be used within a trigger-program");
111264 if( pExpr->affExpr==OE_Abort ){
111268 if( pExpr->affExpr==OE_Ignore ){
111270 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
111274 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
111275 pExpr->affExpr, pExpr->u.zToken, 0, 0);
111311 p = pParse->pConstExpr;
111315 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
111316 if( pItem->fg.reusable
111317 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
111319 return pItem->u.iConstExprReg;
111323 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
111325 Vdbe *v = pParse->pVdbe;
111329 pParse->okConstFactor = 0;
111330 if( !pParse->db->mallocFailed ){
111331 if( regDest<0 ) regDest = ++pParse->nMem;
111334 pParse->okConstFactor = 1;
111335 sqlite3ExprDelete(pParse->db, pExpr);
111340 struct ExprList_item *pItem = &p->a[p->nExpr-1];
111341 pItem->fg.reusable = regDest<0;
111342 if( regDest<0 ) regDest = ++pParse->nMem;
111343 pItem->u.iConstExprReg = regDest;
111345 pParse->pConstExpr = p;
111368 && pExpr->op!=TK_REGISTER
111372 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
111395 assert( target>0 && target<=pParse->nMem );
111396 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
111397 if( pParse->pVdbe==0 ) return;
111406 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
111416 sqlite3 *db = pParse->db;
111418 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
111429 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
111441 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
111466 Vdbe *v = pParse->pVdbe;
111469 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
111470 n = pList->nExpr;
111472 for(pItem=pList->a, i=0; i<n; i++, pItem++){
111473 Expr *pExpr = pItem->pExpr;
111475 if( pItem->fg.bSorterRef ){
111476 i--;
111477 n--;
111480 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
111482 i--;
111483 n--;
111485 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
111496 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
111497 && pOp->p1+pOp->p3+1==inReg
111498 && pOp->p2+pOp->p3+1==target+i
111499 && pOp->p5==0 /* The do-not-merge flag must be clear */
111501 pOp->p3++;
111543 sqlite3 *db = pParse->db;
111550 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
111551 if( db->mallocFailed==0 ){
111557 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
111560 compRight.pRight = pExpr->x.pList->a[1].pExpr;
111570 pDel->flags |= EP_OuterON;
111604 Vdbe *v = pParse->pVdbe;
111614 op = pExpr->op;
111624 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
111626 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111630 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111631 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111637 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111644 isNot = pExpr->op2==TK_ISNOT;
111645 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111649 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111652 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111670 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111672 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111673 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111674 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111694 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111722 /* No-op */
111747 Vdbe *v = pParse->pVdbe;
111758 /* The value of pExpr->op and op are related as follows:
111760 ** pExpr->op op
111761 ** --------- ----------
111771 ** For other values of pExpr->op, op is undefined and unused.
111776 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
111780 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
111781 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
111782 assert( pExpr->op!=TK_NE || op==OP_Eq );
111783 assert( pExpr->op!=TK_EQ || op==OP_Ne );
111784 assert( pExpr->op!=TK_LT || op==OP_Ge );
111785 assert( pExpr->op!=TK_LE || op==OP_Gt );
111786 assert( pExpr->op!=TK_GT || op==OP_Le );
111787 assert( pExpr->op!=TK_GE || op==OP_Lt );
111789 switch( pExpr->op ){
111795 }else if( pExpr->op==TK_AND ){
111797 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111798 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111802 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
111804 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111811 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111818 isNot = pExpr->op2==TK_ISNOT;
111819 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111824 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111829 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111836 testcase( pExpr->op==TK_IS );
111837 testcase( pExpr->op==TK_ISNOT );
111838 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
111847 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111849 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111850 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
111851 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111869 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
111899 /* no-op */
111920 sqlite3 *db = pParse->db;
111922 if( db->mallocFailed==0 ){
111932 ** If pExpr is a simple SQL value - an integer, real, string, blob
111933 ** or NULL value - then the VDBE currently being prepared is configured
111934 ** to re-prepare each time a new value is bound to variable pVar.
111937 ** same as that currently bound to variable pVar, non-zero is returned.
111950 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
111952 iVar = pVar->iColumn;
111953 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
111954 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
111957 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
111972 ** other than the top-level COLLATE operator.
111974 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
111986 ** this routine is used, it does not hurt to get an extra 2 - that
111991 ** pParse->pReprepare can be matched against literals in pB. The
111992 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
112007 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
112010 combinedFlags = pA->flags | pB->flags;
112012 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
112017 if( pA->op!=pB->op || pA->op==TK_RAISE ){
112018 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
112021 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
112024 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
112025 && pB->iTable<0 && pA->iTable==iTab
112034 if( pA->u.zToken ){
112035 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
112036 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
112038 assert( pA->op==pB->op );
112043 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
112048 }else if( pA->op==TK_NULL ){
112050 }else if( pA->op==TK_COLLATE ){
112051 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
112053 if( pB->u.zToken!=0
112054 && pA->op!=TK_COLUMN
112055 && pA->op!=TK_AGG_COLUMN
112056 && strcmp(pA->u.zToken,pB->u.zToken)!=0
112061 if( (pA->flags & (EP_Distinct|EP_Commuted))
112062 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
112066 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
112067 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
112068 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
112069 if( pA->op!=TK_STRING
112070 && pA->op!=TK_TRUEFALSE
112073 if( pA->iColumn!=pB->iColumn ) return 2;
112074 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
112075 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
112088 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
112091 ** This routine might return non-zero for equivalent ExprLists. The
112097 ** always differs from a non-NULL pointer.
112103 if( pA->nExpr!=pB->nExpr ) return 1;
112104 for(i=0; i<pA->nExpr; i++){
112106 Expr *pExprA = pA->a[i].pExpr;
112107 Expr *pExprB = pB->a[i].pExpr;
112108 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
112115 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
112126 ** Return non-zero if Expr p can only be true if pNN is not NULL.
112128 ** Or if seenNot is true, return non-zero if Expr p can only be
112129 ** non-NULL if pNN is not NULL
112136 int seenNot /* Return true only if p can be any non-NULL value */
112141 return pNN->op!=TK_NULL;
112143 switch( p->op ){
112146 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
112147 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112152 pList = p->x.pList;
112154 assert( pList->nExpr==2 );
112156 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
112157 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
112161 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112181 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
112188 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
112192 if( p->op2!=TK_IS ) return 0;
112193 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112197 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
112220 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
112237 if( pE2->op==TK_OR
112238 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
112239 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
112243 if( pE2->op==TK_NOTNULL
112244 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
112253 ** If the expression node requires that the table at pWalker->iCur
112254 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
112257 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
112258 ** (never setting pWalker->eCode) is a harmless missed optimization.
112261 testcase( pExpr->op==TK_AGG_COLUMN );
112262 testcase( pExpr->op==TK_AGG_FUNCTION );
112264 switch( pExpr->op ){
112275 testcase( pExpr->op==TK_ISNOT );
112276 testcase( pExpr->op==TK_ISNULL );
112277 testcase( pExpr->op==TK_NOTNULL );
112278 testcase( pExpr->op==TK_IS );
112279 testcase( pExpr->op==TK_OR );
112280 testcase( pExpr->op==TK_VECTOR );
112281 testcase( pExpr->op==TK_CASE );
112282 testcase( pExpr->op==TK_IN );
112283 testcase( pExpr->op==TK_FUNCTION );
112284 testcase( pExpr->op==TK_TRUTH );
112287 if( pWalker->u.iCur==pExpr->iTable ){
112288 pWalker->eCode = 1;
112294 if( pWalker->eCode==0 ){
112295 sqlite3WalkExpr(pWalker, pExpr->pLeft);
112296 if( pWalker->eCode ){
112297 pWalker->eCode = 0;
112298 sqlite3WalkExpr(pWalker, pExpr->pRight);
112304 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
112305 assert( pWalker->eCode );
112319 Expr *pLeft = pExpr->pLeft;
112320 Expr *pRight = pExpr->pRight;
112321 testcase( pExpr->op==TK_EQ );
112322 testcase( pExpr->op==TK_NE );
112323 testcase( pExpr->op==TK_LT );
112324 testcase( pExpr->op==TK_LE );
112325 testcase( pExpr->op==TK_GT );
112326 testcase( pExpr->op==TK_GE );
112329 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
112330 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
112331 if( (pLeft->op==TK_COLUMN
112332 && ALWAYS(pLeft->y.pTab!=0)
112333 && IsVirtual(pLeft->y.pTab))
112334 || (pRight->op==TK_COLUMN
112335 && ALWAYS(pRight->y.pTab!=0)
112336 && IsVirtual(pRight->y.pTab))
112348 ** Return true (non-zero) if expression p can only be true if at least
112349 ** one column of table iTab is non-null. In other words, return true
112366 ** be non-NULL, then the LEFT JOIN can be safely converted into an
112373 if( p->op==TK_NOTNULL ){
112374 p = p->pLeft;
112376 while( p->op==TK_AND ){
112377 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
112378 p = p->pRight;
112404 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
112405 ** pWalker->u.pIdxCover->pIdx.
112408 if( pExpr->op==TK_COLUMN
112409 && pExpr->iTable==pWalker->u.pIdxCover->iCur
112410 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
112412 pWalker->eCode = 1;
112464 struct RefSrcList *p = pWalker->u.pRefSrcList;
112465 SrcList *pSrc = pSelect->pSrc;
112468 if( pSrc->nSrc==0 ) return WRC_Continue;
112469 j = p->nExclude;
112470 p->nExclude += pSrc->nSrc;
112471 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
112473 p->nExclude = 0;
112476 p->aiExclude = piNew;
112478 for(i=0; i<pSrc->nSrc; i++, j++){
112479 p->aiExclude[j] = pSrc->a[i].iCursor;
112484 struct RefSrcList *p = pWalker->u.pRefSrcList;
112485 SrcList *pSrc = pSelect->pSrc;
112486 if( p->nExclude ){
112487 assert( p->nExclude>=pSrc->nSrc );
112488 p->nExclude -= pSrc->nSrc;
112494 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
112497 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
112501 if( pExpr->op==TK_COLUMN
112502 || pExpr->op==TK_AGG_COLUMN
112505 struct RefSrcList *p = pWalker->u.pRefSrcList;
112506 SrcList *pSrc = p->pRef;
112507 int nSrc = pSrc ? pSrc->nSrc : 0;
112509 if( pExpr->iTable==pSrc->a[i].iCursor ){
112510 pWalker->eCode |= 1;
112514 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
112515 if( i>=p->nExclude ){
112516 pWalker->eCode |= 2;
112531 ** -1 pExpr only references no tables at all, or it only
112540 assert( pParse->db!=0 );
112547 x.db = pParse->db;
112549 assert( pExpr->op==TK_AGG_FUNCTION );
112551 sqlite3WalkExprList(&w, pExpr->x.pList);
112554 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
112557 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
112563 return -1;
112575 ** The copy is stored on pParse->pConstExpr with a register number of 0.
112582 && pExpr->pAggInfo!=0
112584 AggInfo *pAggInfo = pExpr->pAggInfo;
112585 int iAgg = pExpr->iAgg;
112586 Parse *pParse = pWalker->pParse;
112587 sqlite3 *db = pParse->db;
112588 if( pExpr->op!=TK_AGG_FUNCTION ){
112589 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_IF_NULL_ROW );
112590 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
112591 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
112594 pAggInfo->aCol[iAgg].pCExpr = pExpr;
112599 assert( pExpr->op==TK_AGG_FUNCTION );
112600 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
112601 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
112604 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
112619 pWalker->pParse = pParse;
112620 pWalker->xExprCallback = agginfoPersistExprCb;
112621 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
112625 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
112630 pInfo->aCol = sqlite3ArrayAllocate(
112632 pInfo->aCol,
112633 sizeof(pInfo->aCol[0]),
112634 &pInfo->nColumn,
112641 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
112646 pInfo->aFunc = sqlite3ArrayAllocate(
112648 pInfo->aFunc,
112649 sizeof(pInfo->aFunc[0]),
112650 &pInfo->nFunc,
112663 NameContext *pNC = pWalker->u.pNC;
112664 Parse *pParse = pNC->pParse;
112665 SrcList *pSrcList = pNC->pSrcList;
112666 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
112668 assert( pNC->ncFlags & NC_UAggInfo );
112669 switch( pExpr->op ){
112673 testcase( pExpr->op==TK_AGG_COLUMN );
112674 testcase( pExpr->op==TK_COLUMN );
112675 testcase( pExpr->op==TK_IF_NULL_ROW );
112679 SrcItem *pItem = pSrcList->a;
112680 for(i=0; i<pSrcList->nSrc; i++, pItem++){
112683 if( pExpr->iTable==pItem->iCursor ){
112687 ** Make an entry for the column in pAggInfo->aCol[] if there
112691 pCol = pAggInfo->aCol;
112692 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
112693 if( pCol->iTable==pExpr->iTable
112694 && pCol->iColumn==pExpr->iColumn
112695 && pExpr->op!=TK_IF_NULL_ROW
112700 if( (k>=pAggInfo->nColumn)
112701 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
112703 pCol = &pAggInfo->aCol[k];
112705 pCol->pTab = pExpr->y.pTab;
112706 pCol->iTable = pExpr->iTable;
112707 pCol->iColumn = pExpr->iColumn;
112708 pCol->iMem = ++pParse->nMem;
112709 pCol->iSorterColumn = -1;
112710 pCol->pCExpr = pExpr;
112711 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
112713 ExprList *pGB = pAggInfo->pGroupBy;
112714 struct ExprList_item *pTerm = pGB->a;
112715 n = pGB->nExpr;
112717 Expr *pE = pTerm->pExpr;
112718 if( pE->op==TK_COLUMN
112719 && pE->iTable==pExpr->iTable
112720 && pE->iColumn==pExpr->iColumn
112722 pCol->iSorterColumn = j;
112727 if( pCol->iSorterColumn<0 ){
112728 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
112731 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
112734 ** pAggInfo->aCol[] entry.
112737 pExpr->pAggInfo = pAggInfo;
112738 if( pExpr->op==TK_COLUMN ){
112739 pExpr->op = TK_AGG_COLUMN;
112741 pExpr->iAgg = (i16)k;
112743 } /* endif pExpr->iTable==pItem->iCursor */
112749 if( (pNC->ncFlags & NC_InAggFunc)==0
112750 && pWalker->walkerDepth==pExpr->op2
112755 struct AggInfo_func *pItem = pAggInfo->aFunc;
112756 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
112757 if( pItem->pFExpr==pExpr ) break;
112758 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
112762 if( i>=pAggInfo->nFunc ){
112763 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
112765 u8 enc = ENC(pParse->db);
112766 i = addAggInfoFunc(pParse->db, pAggInfo);
112769 pItem = &pAggInfo->aFunc[i];
112770 pItem->pFExpr = pExpr;
112771 pItem->iMem = ++pParse->nMem;
112773 pItem->pFunc = sqlite3FindFunction(pParse->db,
112774 pExpr->u.zToken,
112775 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
112776 if( pExpr->flags & EP_Distinct ){
112777 pItem->iDistinct = pParse->nTab++;
112779 pItem->iDistinct = -1;
112783 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
112787 pExpr->iAgg = (i16)i;
112788 pExpr->pAggInfo = pAggInfo;
112800 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
112815 assert( pNC->pSrcList!=0 );
112829 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
112830 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
112839 if( pParse->nTempReg==0 ){
112840 return ++pParse->nMem;
112842 return pParse->aTempReg[--pParse->nTempReg];
112852 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
112853 pParse->aTempReg[pParse->nTempReg++] = iReg;
112864 i = pParse->iRangeReg;
112865 n = pParse->nRangeReg;
112867 pParse->iRangeReg += nReg;
112868 pParse->nRangeReg -= nReg;
112870 i = pParse->nMem+1;
112871 pParse->nMem += nReg;
112881 if( nReg>pParse->nRangeReg ){
112882 pParse->nRangeReg = nReg;
112883 pParse->iRangeReg = iReg;
112890 ** Always invoke this procedure after coding a subroutine or co-routine
112892 ** the sub/co-routine does not use registers in common with the code that
112893 ** invokes the sub/co-routine.
112896 pParse->nTempReg = 0;
112897 pParse->nRangeReg = 0;
112908 if( pParse->nRangeReg>0
112909 && pParse->iRangeReg+pParse->nRangeReg > iFirst
112910 && pParse->iRangeReg <= iLast
112914 for(i=0; i<pParse->nTempReg; i++){
112915 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
112951 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
112956 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
112958 || (pTab->tabFlags & TF_Eponymous)!=0
112959 || ( (pTab->tabFlags & TF_Shadow)!=0
112960 && sqlite3ReadOnlyShadowTables(pParse->db)
112964 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
112984 pParse->colNamesSet = 1;
113008 ** Generate VM code to replace any double-quoted strings (but not double-quoted
113010 ** database zDb with their single-quoted equivalents. If argument bTemp is
113036 Vdbe *v = pParse->pVdbe;
113039 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
113040 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
113056 char *zName = 0; /* NULL-terminated version of pName */
113057 sqlite3 *db = pParse->db; /* Database connection */
113058 int nTabName; /* Number of UTF-8 characters in zTabName */
113061 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
113063 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
113064 assert( pSrc->nSrc==1 );
113065 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113067 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113069 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113070 zDb = db->aDb[iDb].zDbSName;
113100 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
113107 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113118 if( pVTab->pVtab->pModule->xRename==0 ){
113134 /* figure out how many UTF-8 characters are in zName */
113135 zTabName = pTab->zName;
113136 nTabName = sqlite3Utf8CharLen(zTabName, -1);
113173 zDb, zName, pTab->zName);
113194 ** of any resources used by the v-table implementation (including other
113199 int i = ++pParse->nMem;
113234 ** The Table structure pParse->pNewTable was extended to include
113238 Table *pNew; /* Copy of pParse->pNewTable */
113243 char *zCol; /* Null-terminated column definition */
113250 db = pParse->db;
113251 assert( db->pParse==pParse );
113252 if( pParse->nErr ) return;
113253 assert( db->mallocFailed==0 );
113254 pNew = pParse->pNewTable;
113258 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
113259 zDb = db->aDb[iDb].zDbSName;
113260 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
113261 pCol = &pNew->aCol[pNew->nCol-1];
113268 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113278 if( pCol->colFlags & COLFLAG_PRIMKEY ){
113282 if( pNew->pIndex ){
113287 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
113292 assert( pDflt==0 || pDflt->op==TK_SPAN );
113293 if( pDflt && pDflt->pLeft->op==TK_NULL ){
113297 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
113299 "Cannot add a REFERENCES column with non-NULL default value");
113301 if( pCol->notNull && !pDflt ){
113316 assert( db->mallocFailed == 1 );
113321 "Cannot add a column with non-constant default");
113325 }else if( pCol->colFlags & COLFLAG_STORED ){
113331 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
113333 char *zEnd = &zCol[pColDef->n-1];
113335 *zEnd-- = '\0';
113346 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
113361 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
113371 if( pNew->pCheck!=0
113372 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
113388 ** This function is called by the parser after the table-name in
113389 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
113390 ** pSrc is the full-name of the table being altered.
113408 sqlite3 *db = pParse->db;
113411 assert( pParse->pNewTable==0 );
113413 if( db->mallocFailed ) goto exit_begin_add_column;
113414 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113435 assert( pTab->u.tab.addColOffset>0 );
113436 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113447 pParse->pNewTable = pNew;
113448 pNew->nTabRef = 1;
113449 pNew->nCol = pTab->nCol;
113450 assert( pNew->nCol>0 );
113451 nAlloc = (((pNew->nCol-1)/8)*8)+8;
113452 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
113453 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
113454 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
113455 if( !pNew->aCol || !pNew->zName ){
113456 assert( db->mallocFailed );
113459 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
113460 for(i=0; i<pNew->nCol; i++){
113461 Column *pCol = &pNew->aCol[i];
113462 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
113463 pCol->hName = sqlite3StrIHash(pCol->zCnName);
113466 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
113467 pNew->pSchema = db->aDb[iDb].pSchema;
113468 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
113469 pNew->nTabRef = 1;
113480 ** it loads an error message into pParse and returns non-zero.
113500 zType, pTab->zName
113517 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
113521 sqlite3 *db = pParse->db; /* Database connection */
113531 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
113539 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
113541 zDb = db->aDb[iSchema].zDbSName;
113545 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
113554 for(iCol=0; iCol<pTab->nCol; iCol++){
113555 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
113557 if( iCol==pTab->nCol ){
113562 /* Ensure the schema contains no double-quoted strings */
113573 assert( pNew->n>0 );
113574 bQuote = sqlite3Isquote(pNew->z[0]);
113581 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
113582 pTab->zName
113589 zDb, pTab->zName, iCol, zNew, bQuote
113644 ** rename-token list.
113646 ** 2. Dereferences each pointer in the rename-token list.
113649 ** address-sanitizer or similar. If any of these pointers no longer
113650 ** point to valid objects, an exception is raised by the memory-checking
113664 assert( pParse==pParse->db->pParse );
113665 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
113666 if( pParse->nErr==0 ){
113669 for(p=pParse->pRename; p; p=p->pNext){
113670 if( p->p ){
113671 assert( p->p!=pPtr );
113672 i += *(u8*)(p->p);
113687 ** in pParse->pRename.
113699 assert( pPtr || pParse->db->mallocFailed );
113701 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
113702 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
113704 pNew->p = pPtr;
113705 pNew->t = *pToken;
113706 pNew->pNext = pParse->pRename;
113707 pParse->pRename = pNew;
113722 for(p=pParse->pRename; p; p=p->pNext){
113723 if( p->p==pFrom ){
113724 p->p = pTo;
113734 Parse *pParse = pWalker->pParse;
113737 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
113747 With *pWith = pSelect->pWith;
113749 Parse *pParse = pWalker->pParse;
113752 assert( pWith->nCte>0 );
113753 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
113754 /* Push a copy of the With object onto the with-stack. We use a copy
113756 ** and SF_Resolved) below. And the parser code that uses the with-stack
113759 pCopy = sqlite3WithDup(pParse->db, pWith);
113762 for(i=0; i<pWith->nCte; i++){
113763 Select *p = pWith->a[i].pSelect;
113768 if( sNC.pParse->db->mallocFailed ) return;
113770 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
113772 if( pCopy && pParse->pWith==pCopy ){
113773 pParse->pWith = pCopy->pOuter;
113787 for(ii=0; ii<pIdList->nId; ii++){
113788 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
113796 Parse *pParse = pWalker->pParse;
113798 if( pParse->nErr ) return WRC_Abort;
113799 testcase( p->selFlags & SF_View );
113800 testcase( p->selFlags & SF_CopyCte );
113801 if( p->selFlags & (SF_View|SF_CopyCte) ){
113804 if( ALWAYS(p->pEList) ){
113805 ExprList *pList = p->pEList;
113806 for(i=0; i<pList->nExpr; i++){
113807 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
113808 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
113812 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
113813 SrcList *pSrc = p->pSrc;
113814 for(i=0; i<pSrc->nSrc; i++){
113815 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
113816 if( pSrc->a[i].fg.isUsing==0 ){
113817 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
113819 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
113832 u8 eMode = pParse->eParseMode;
113838 pParse->eParseMode = PARSE_MODE_UNMAP;
113840 pParse->eParseMode = eMode;
113844 ** Remove all nodes that are part of expression-list pEList from the
113855 for(i=0; i<pEList->nExpr; i++){
113856 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
113857 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
113870 pNext = p->pNext;
113893 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
113894 if( (*pp)->p==pPtr ){
113897 *pp = pToken->pNext;
113898 pToken->pNext = pCtx->pList;
113899 pCtx->pList = pToken;
113900 pCtx->nList++;
113911 ** descend into sub-select statements.
113914 if( p->selFlags & (SF_View|SF_CopyCte) ){
113915 testcase( p->selFlags & SF_View );
113916 testcase( p->selFlags & SF_CopyCte );
113930 ** constructed in RenameCtx object at pWalker->u.pRename.
113933 RenameCtx *p = pWalker->u.pRename;
113934 if( pExpr->op==TK_TRIGGER
113935 && pExpr->iColumn==p->iCol
113936 && pWalker->pParse->pTriggerTab==p->pTab
113938 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
113939 }else if( pExpr->op==TK_COLUMN
113940 && pExpr->iColumn==p->iCol
113942 && p->pTab==pExpr->y.pTab
113944 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
113959 RenameToken *pBest = pCtx->pList;
113963 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
113964 if( pToken->t.z>pBest->t.z ) pBest = pToken;
113966 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
113967 *pp = pBest->pNext;
113974 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
113976 ** sub-routine is currently stored in pParse->zErrMsg. This function
113990 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
113992 pParse->zErrMsg
113994 sqlite3_result_error(pCtx, zErr, -1);
113995 sqlite3DbFree(pParse->db, zErr);
113999 ** For each name in the the expression-list pEList (i.e. each
114000 ** pEList->a[i].zName) that matches the string in zOld, extract the
114001 ** corresponding rename-token from Parse object pParse and add it
114012 for(i=0; i<pEList->nExpr; i++){
114013 const char *zName = pEList->a[i].zEName;
114014 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
114025 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
114026 ** that matches the string in zOld, extract the corresponding rename-token
114037 for(i=0; i<pIdList->nId; i++){
114038 const char *zName = pIdList->a[i].zName;
114067 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
114068 p->eParseMode = PARSE_MODE_RENAME;
114069 p->db = db;
114070 p->nQueryLoop = 1;
114072 if( db->mallocFailed ) rc = SQLITE_NOMEM;
114074 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
114085 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
114086 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
114091 db->init.iDb = 0;
114131 nQuot = sqlite3Strlen30(zQuot)-1;
114135 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
114144 /* At this point pRename->pList contains a list of RenameToken objects
114146 ** with the new column name, or with single-quoted versions of themselves.
114151 while( pRename->pList ){
114158 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
114164 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
114167 /* Dequote the double-quoted token. Then requote it again, this time
114170 ** add another space after the new, single-quoted version of the
114173 memcpy(zBuf1, pBest->t.z, pBest->t.n);
114174 zBuf1[pBest->t.n] = 0;
114177 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
114183 iOff = pBest->t.z - zSql;
114184 if( pBest->t.n!=nReplace ){
114185 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
114186 nOut - (iOff + pBest->t.n)
114188 nOut += nReplace - pBest->t.n;
114195 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
114206 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
114212 sqlite3 *db = pParse->db;
114213 Trigger *pNew = pParse->pNewTrigger;
114220 assert( pNew->pTabSchema );
114221 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
114222 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
114224 pParse->eTriggerOp = pNew->op;
114227 if( ALWAYS(pParse->pTriggerTab) ){
114228 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
114232 if( rc==SQLITE_OK && pNew->pWhen ){
114233 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
114236 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
114237 if( pStep->pSelect ){
114238 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
114239 if( pParse->nErr ) rc = pParse->rc;
114241 if( rc==SQLITE_OK && pStep->zTarget ){
114245 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
114248 pStep->pExprList = 0;
114253 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
114254 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
114255 assert( pSrc==pSel->pSrc );
114256 if( pStep->pExprList ) pSel->pEList = 0;
114257 pSel->pSrc = 0;
114260 if( pStep->pFrom ){
114262 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
114263 SrcItem *p = &pStep->pFrom->a[i];
114264 if( p->pSelect ){
114265 sqlite3SelectPrep(pParse, p->pSelect, 0);
114270 if( db->mallocFailed ){
114274 if( rc==SQLITE_OK && pStep->pWhere ){
114275 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
114278 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
114280 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
114281 if( pStep->pUpsert && rc==SQLITE_OK ){
114282 Upsert *pUpsert = pStep->pUpsert;
114283 pUpsert->pUpsertSrc = pSrc;
114286 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
114288 ExprList *pUpsertSet = pUpsert->pUpsertSet;
114292 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
114295 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
114317 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
114320 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
114321 sqlite3WalkSelect(pWalker, pStep->pSelect);
114322 sqlite3WalkExpr(pWalker, pStep->pWhere);
114323 sqlite3WalkExprList(pWalker, pStep->pExprList);
114324 if( pStep->pUpsert ){
114325 Upsert *pUpsert = pStep->pUpsert;
114326 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
114327 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
114328 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
114329 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
114331 if( pStep->pFrom ){
114333 for(i=0; i<pStep->pFrom->nSrc; i++){
114334 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
114345 sqlite3 *db = pParse->db;
114347 if( pParse->pVdbe ){
114348 sqlite3VdbeFinalize(pParse->pVdbe);
114350 sqlite3DeleteTable(db, pParse->pNewTable);
114351 while( (pIdx = pParse->pNewIndex)!=0 ){
114352 pParse->pNewIndex = pIdx->pNext;
114355 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
114356 sqlite3DbFree(db, pParse->zErrMsg);
114357 renameTokenFree(db, pParse->pRename);
114373 ** 7. bQuote: Non-zero if the new column name should be quoted.
114377 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
114407 sqlite3_xauth xAuth = db->xAuth;
114417 if( pTab==0 || iCol>=pTab->nCol ){
114421 zOld = pTab->aCol[iCol].zCnName;
114423 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
114426 db->xAuth = 0;
114441 Select *pSelect = sParse.pNewTable->u.view.pSelect;
114442 pSelect->selFlags &= ~SF_View;
114445 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
114452 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
114456 if( iCol<sParse.pNewTable->nCol ){
114458 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
114462 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
114464 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
114465 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
114466 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
114468 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
114469 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
114472 for(i=0; i<sParse.pNewTable->nCol; i++){
114474 &sParse.pNewTable->aCol[i]);
114481 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
114482 for(i=0; i<pFKey->nCol; i++){
114483 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
114484 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
114486 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
114487 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
114489 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
114495 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
114496 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114503 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
114504 if( pStep->zTarget ){
114505 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
114507 if( pStep->pUpsert ){
114508 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
114511 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
114512 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
114520 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
114544 db->xAuth = xAuth;
114553 RenameCtx *p = pWalker->u.pRename;
114554 if( pExpr->op==TK_COLUMN
114556 && p->pTab==pExpr->y.pTab
114558 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
114568 RenameCtx *p = pWalker->u.pRename;
114569 SrcList *pSrc = pSelect->pSrc;
114570 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
114571 testcase( pSelect->selFlags & SF_View );
114572 testcase( pSelect->selFlags & SF_CopyCte );
114576 assert( pWalker->pParse->db->mallocFailed );
114579 for(i=0; i<pSrc->nSrc; i++){
114580 SrcItem *pItem = &pSrc->a[i];
114581 if( pItem->pTab==p->pTab ){
114582 renameTokenFind(pWalker->pParse, p, pItem->zName);
114608 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
114631 sqlite3_xauth xAuth = db->xAuth;
114632 db->xAuth = 0;
114648 int isLegacy = (db->flags & SQLITE_LegacyAlter);
114654 Select *pSelect = pTab->u.view.pSelect;
114659 assert( pSelect->selFlags & SF_View );
114660 pSelect->selFlags &= ~SF_View;
114661 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
114665 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
114671 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
114676 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
114677 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
114678 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
114687 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
114690 sqlite3WalkExprList(&sWalker, pTab->pCheck);
114692 renameTokenFind(&sParse, &sCtx, pTab->zName);
114698 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
114700 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114708 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
114709 && sCtx.pTab->pSchema==pTrigger->pTabSchema
114711 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
114718 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
114719 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
114720 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
114722 if( pStep->pFrom ){
114724 for(i=0; i<pStep->pFrom->nSrc; i++){
114725 SrcItem *pItem = &pStep->pFrom->a[i];
114726 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
114727 renameTokenFind(&sParse, &sCtx, pItem->zName);
114755 db->xAuth = xAuth;
114763 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
114764 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
114772 ** double-quotes use single quotes instead.
114806 sqlite3_xauth xAuth = db->xAuth;
114807 db->xAuth = 0;
114832 Select *pSelect = sParse.pNewTable->u.view.pSelect;
114833 pSelect->selFlags &= ~SF_View;
114836 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
114842 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
114844 for(i=0; i<sParse.pNewTable->nCol; i++){
114847 &sParse.pNewTable->aCol[i]));
114852 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
114853 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114879 db->xAuth = xAuth;
114918 int isLegacy = (db->flags & SQLITE_LegacyAlter);
114923 sqlite3_xauth xAuth = db->xAuth;
114924 db->xAuth = 0;
114932 int flags = db->flags;
114933 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
114935 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
114941 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
114950 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
114968 db->xAuth = xAuth;
114977 ** argv[0]: An integer - the index of the schema containing the table
114979 ** argv[2]: An integer - the index of the column to remove.
114993 const char *zDb = db->aDb[iSchema].zDbSName;
115002 sqlite3_xauth xAuth = db->xAuth;
115003 db->xAuth = 0;
115010 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
115016 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
115017 if( iCol<pTab->nCol-1 ){
115019 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
115020 zEnd = (const char*)pEnd->t.z;
115023 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
115024 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
115027 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
115028 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
115034 db->xAuth = xAuth;
115050 sqlite3 *db = pParse->db; /* Database handle */
115055 int iCol; /* Index of column zCol in pTab->aCol[] */
115058 assert( pParse->pNewTable==0 );
115060 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
115061 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
115072 assert( db->mallocFailed );
115083 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
115085 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
115092 if( pTab->nCol<=1 ){
115098 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115100 zDb = db->aDb[iDb].zDbSName;
115103 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
115113 , zDb, iDb, iCol, pTab->zName
115121 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
115127 int nField = 0; /* Number of non-virtual columns after drop */
115130 iCur = pParse->nTab++;
115133 reg = ++pParse->nMem;
115136 pParse->nMem += pTab->nCol;
115139 pParse->nMem += pPk->nColumn;
115140 for(i=0; i<pPk->nKeyCol; i++){
115143 nField = pPk->nKeyCol;
115145 regRec = ++pParse->nMem;
115146 for(i=0; i<pTab->nCol; i++){
115147 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
115152 if( iPos<pPk->nKeyCol ) continue;
115153 regOut = reg+1+iPos-(iPos>iColPos);
115157 if( i==pTab->iPKey ){
115167 pParse->nMem++;
115173 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
115189 ** Register built-in functions used to help implement ALTER TABLE
115206 ** 2005-07-08
115256 ** columns. The N-th integer (for N>1) is the average number of rows in
115257 ** the index which have the same value for the first N-1 columns. For
115258 ** a K-column index, there will be K+1 integers in the stat column. If
115282 ** inclusive are samples of the left-most key value in the index taken at
115289 ** For i between 0 and S-1. Conceptually, the index space is divided into
115316 ** of entries in the index whose left-most column exactly matches
115317 ** the left-most column of the sample. The second integer in nEq
115323 ** left-most column is less than the left-most column of the sample.
115324 ** The K-th integer in the nLt entry is the number of index entries
115339 ** looks at the left-most column of the index. The sqlite_stat3.sample
115340 ** column contains the actual value of the left-most column instead
115361 ** appropriate compile-time options are provided.
115390 sqlite3 *db = pParse->db;
115404 pDb = &db->aDb[iDb];
115413 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
115416 ** side-effect of the CREATE TABLE statement is to leave the rootpage
115417 ** of the new table in register pParse->regRoot. This is important
115420 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
115422 aRoot[i] = (u32)pParse->regRoot;
115429 aRoot[i] = pStat->tnum;
115434 pDb->zDbSName, zTab, zWhereType, zWhere
115437 }else if( db->xPreUpdateCallback ){
115438 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
115464 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
115489 int nLimit; /* Analysis row-scan limit */
115492 u8 nSkipAhead; /* Number of times of skip-ahead */
115497 u32 iPrn; /* Pseudo-random number used for sampling */
115512 if( p->nRowid ){
115513 sqlite3DbFree(db, p->u.aRowid);
115514 p->nRowid = 0;
115524 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
115525 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
115526 if( p->u.aRowid ){
115527 p->nRowid = n;
115528 memcpy(p->u.aRowid, pData, n);
115530 p->nRowid = 0;
115540 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
115541 p->nRowid = 0;
115542 p->u.iRowid = iRowid;
115552 pTo->isPSample = pFrom->isPSample;
115553 pTo->iCol = pFrom->iCol;
115554 pTo->iHash = pFrom->iHash;
115555 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
115556 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
115557 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
115558 if( pFrom->nRowid ){
115559 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
115561 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
115572 if( p->mxSample ){
115574 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
115575 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
115576 sampleClear(p->db, &p->current);
115579 sqlite3DbFree(p->db, p);
115588 ** L: A limit on the number of rows to scan, or 0 for no-limit
115646 p->db = db;
115647 p->nEst = sqlite3_value_int64(argv[2]);
115648 p->nRow = 0;
115649 p->nLimit = sqlite3_value_int64(argv[3]);
115650 p->nCol = nCol;
115651 p->nKeyCol = nKeyCol;
115652 p->nSkipAhead = 0;
115653 p->current.anDLt = (tRowcnt*)&p[1];
115654 p->current.anEq = &p->current.anDLt[nColUp];
115657 p->mxSample = p->nLimit==0 ? mxSample : 0;
115660 int i; /* Used to iterate through p->aSample[] */
115662 p->iGet = -1;
115663 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
115664 p->current.anLt = &p->current.anEq[nColUp];
115665 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
115668 p->a = (struct StatSample*)&p->current.anLt[nColUp];
115669 p->aBest = &p->a[mxSample];
115670 pSpace = (u8*)(&p->a[mxSample+nCol]);
115672 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115673 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115674 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115676 assert( (pSpace - (u8*)p)==n );
115679 p->aBest[i].iCol = i;
115704 ** pNew and pOld are both candidate non-periodic samples selected for
115705 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
115712 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
115719 int nCol = pAccum->nCol;
115721 assert( pNew->iCol==pOld->iCol );
115722 for(i=pNew->iCol+1; i<nCol; i++){
115723 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
115724 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
115726 if( pNew->iHash>pOld->iHash ) return 1;
115736 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
115743 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
115744 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
115746 assert( pOld->isPSample==0 && pNew->isPSample==0 );
115747 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
115751 if( pNew->iCol<pOld->iCol ) return 1;
115752 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
115758 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
115759 ** remove the least desirable sample from p->a[] to make room.
115771 if( nEqZero>p->nMaxEqZero ){
115772 p->nMaxEqZero = nEqZero;
115774 if( pNew->isPSample==0 ){
115776 assert( pNew->anEq[pNew->iCol]>0 );
115783 for(i=p->nSample-1; i>=0; i--){
115784 StatSample *pOld = &p->a[i];
115785 if( pOld->anEq[pNew->iCol]==0 ){
115786 if( pOld->isPSample ) return;
115787 assert( pOld->iCol>pNew->iCol );
115795 pUpgrade->iCol = pNew->iCol;
115796 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
115802 if( p->nSample>=p->mxSample ){
115803 StatSample *pMin = &p->a[p->iMin];
115804 tRowcnt *anEq = pMin->anEq;
115805 tRowcnt *anLt = pMin->anLt;
115806 tRowcnt *anDLt = pMin->anDLt;
115807 sampleClear(p->db, pMin);
115808 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
115809 pSample = &p->a[p->nSample-1];
115810 pSample->nRowid = 0;
115811 pSample->anEq = anEq;
115812 pSample->anDLt = anDLt;
115813 pSample->anLt = anLt;
115814 p->nSample = p->mxSample-1;
115817 /* The "rows less-than" for the rowid column must be greater than that
115818 ** for the last sample in the p->a[] array. Otherwise, the samples would
115820 assert( p->nSample==0
115821 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
115824 pSample = &p->a[p->nSample];
115826 p->nSample++;
115829 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
115832 if( p->nSample>=p->mxSample ){
115833 int iMin = -1;
115834 for(i=0; i<p->mxSample; i++){
115835 if( p->a[i].isPSample ) continue;
115836 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
115841 p->iMin = iMin;
115849 ** p->current contains a sample that reflects the previous row of the
115858 for(i=(p->nCol-2); i>=iChng; i--){
115859 StatSample *pBest = &p->aBest[i];
115860 pBest->anEq[i] = p->current.anEq[i];
115861 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
115867 ** p->nMaxEqZero or greater set to zero. */
115868 for(i=p->nSample-1; i>=0; i--){
115870 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
115874 if( iChng<p->nMaxEqZero ){
115875 for(i=p->nSample-1; i>=0; i--){
115877 for(j=iChng; j<p->nCol; j++){
115878 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
115881 p->nMaxEqZero = iChng;
115891 ** C Index of left-most column to differ from previous row
115901 ** if it wants the byte-code to do special processing.
115918 assert( p->nCol>0 );
115919 assert( iChng<p->nCol );
115921 if( p->nRow==0 ){
115923 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
115927 if( p->mxSample ) samplePushPrevious(p, iChng);
115933 p->current.anEq[i]++;
115935 for(i=iChng; i<p->nCol; i++){
115936 p->current.anDLt[i]++;
115938 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
115940 p->current.anEq[i] = 1;
115944 p->nRow++;
115946 if( p->mxSample ){
115949 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
115951 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
115954 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
115956 nLt = p->current.anLt[p->nCol-1];
115958 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
115959 p->current.isPSample = 1;
115960 p->current.iCol = 0;
115961 sampleInsert(p, &p->current, p->nCol-1);
115962 p->current.isPSample = 0;
115966 for(i=0; i<(p->nCol-1); i++){
115967 p->current.iCol = i;
115968 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
115969 sampleCopy(p, &p->aBest[i], &p->current);
115974 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
115975 p->nSkipAhead++;
115976 sqlite3_result_int(context, p->current.anDLt[0]>0);
116014 ** a one-parameter function, stat_get(P), that always returns the
116031 assert( eCall==STAT_GET_STAT1 || p->mxSample );
116056 ** I = (K+D-1)/D
116066 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
116068 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
116069 for(i=0; i<p->nKeyCol; i++){
116070 u64 nDistinct = p->current.anDLt[i] + 1;
116071 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
116072 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
116074 assert( p->current.anEq[i] );
116080 if( p->iGet<0 ){
116082 p->iGet = 0;
116084 if( p->iGet<p->nSample ){
116085 StatSample *pS = p->a + p->iGet;
116086 if( pS->nRowid==0 ){
116087 sqlite3_result_int64(context, pS->u.iRowid);
116089 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
116098 assert( p->iGet<p->nSample );
116100 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
116101 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
116103 aCnt = p->a[p->iGet].anDLt;
116104 p->iGet++;
116108 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
116109 for(i=0; i<p->nCol; i++){
116112 if( sStat.nChar ) sStat.nChar--;
116134 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
116147 ** of the k-th column of the pIdx index.
116155 assert( k>=0 && k<pIdx->nColumn );
116156 i = pIdx->aiColumn[k];
116158 VdbeComment((v,"%s.rowid",pIdx->zName));
116160 assert( pIdx->bHasExpr );
116161 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
116163 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
116182 sqlite3 *db = pParse->db; /* Database handle */
116188 int jZeroRows = -1; /* Jump from here if number of rows is zero */
116205 pParse->nMem = MAX(pParse->nMem, iMem);
116214 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
116219 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116223 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
116224 db->aDb[iDb].zDbSName ) ){
116230 if( db->xPreUpdateCallback ){
116233 pStat1->zName = (char*)&pStat1[1];
116234 memcpy(pStat1->zName, "sqlite_stat1", 13);
116235 pStat1->nCol = 3;
116236 pStat1->iPKey = -1;
116237 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
116241 /* Establish a read-lock on the table at the shared-cache level.
116242 ** Open a read-only cursor on the table. Also allocate a cursor number
116245 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
116248 pParse->nTab = MAX(pParse->nTab, iTab);
116250 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
116252 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
116260 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
116262 nCol = pIdx->nKeyCol;
116263 zIdxName = pTab->zName;
116264 nColTest = nCol - 1;
116266 nCol = pIdx->nColumn;
116267 zIdxName = pIdx->zName;
116268 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
116273 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
116276 ** Pseudo-code for loop that calls stat_push():
116311 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
116313 /* Open a read-only cursor on the index being analyzed. */
116314 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
116315 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
116317 VdbeComment((v, "%s", pIdx->zName));
116328 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
116342 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
116375 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
116376 /* For a single-column UNIQUE index, once we have found a non-NULL
116383 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
116403 sqlite3VdbeJumpHere(v, addrNextRow-1);
116426 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
116428 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
116429 for(j=0; j<pPk->nKeyCol; j++){
116430 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
116431 assert( k>=0 && k<pIdx->nColumn );
116435 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
116436 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
116444 if( db->nAnalysisLimit ){
116466 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
116472 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
116483 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
116501 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
116515 VdbeComment((v, "%s", pTab->zName));
116525 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
116547 sqlite3 *db = pParse->db;
116548 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
116555 iStatCur = pParse->nTab;
116556 pParse->nTab += 3;
116558 iMem = pParse->nMem+1;
116559 iTab = pParse->nTab;
116561 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
116578 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116579 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116581 iStatCur = pParse->nTab;
116582 pParse->nTab += 3;
116584 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
116586 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
116588 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
116596 ** ANALYZE -- 1
116597 ** ANALYZE <database> -- 2
116598 ** ANALYZE ?<database>.?<tablename> -- 3
116605 sqlite3 *db = pParse->db;
116616 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116624 for(i=0; i<db->nDb; i++){
116628 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
116635 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
116639 analyzeTable(pParse, pIdx->pTable, pIdx);
116647 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
116663 ** The first argument points to a nul-terminated string containing a
116687 v = v*10 + c - '0';
116706 pIndex->bUnordered = 0;
116707 pIndex->noSkipScan = 0;
116710 pIndex->bUnordered = 1;
116711 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
116714 pIndex->szIdxRow = sqlite3LogEst(sz);
116716 pIndex->noSkipScan = 1;
116719 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
116720 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
116735 ** argv[2] = results of analysis - on integer for each column
116752 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
116761 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
116767 int nCol = pIndex->nKeyCol+1;
116772 if( pIndex->aiRowEst==0 ){
116773 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
116774 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
116776 aiRowEst = pIndex->aiRowEst;
116778 pIndex->bUnordered = 0;
116779 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
116780 pIndex->hasStat1 = 1;
116781 if( pIndex->pPartIdxWhere==0 ){
116782 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
116783 pTable->tabFlags |= TF_HasStat1;
116787 fakeIdx.szIdxRow = pTable->szTabRow;
116791 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
116792 pTable->szTabRow = fakeIdx.szIdxRow;
116793 pTable->tabFlags |= TF_HasStat1;
116805 if( pIdx->aSample ){
116807 for(j=0; j<pIdx->nSample; j++){
116808 IndexSample *p = &pIdx->aSample[j];
116809 sqlite3DbFree(db, p->p);
116811 sqlite3DbFree(db, pIdx->aSample);
116813 if( db && db->pnBytesFreed==0 ){
116814 pIdx->nSample = 0;
116815 pIdx->aSample = 0;
116825 ** Populate the pIdx->aAvgEq[] array based on the samples currently
116826 ** stored in pIdx->aSample[].
116830 IndexSample *aSample = pIdx->aSample;
116831 IndexSample *pFinal = &aSample[pIdx->nSample-1];
116834 if( pIdx->nSampleCol>1 ){
116839 nCol = pIdx->nSampleCol-1;
116840 pIdx->aAvgEq[nCol] = 1;
116843 int nSample = pIdx->nSample;
116851 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
116852 nRow = pFinal->anLt[iCol];
116853 nDist100 = (i64)100 * pFinal->anDLt[iCol];
116854 nSample--;
116856 nRow = pIdx->aiRowEst[0];
116857 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
116859 pIdx->nRowEst0 = nRow;
116866 if( i==(pIdx->nSample-1)
116875 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
116878 pIdx->aAvgEq[iCol] = avgEq;
116922 IndexSample *pSample; /* A slot in pIdx->aSample[] */
116924 assert( db->lookaside.bDisable );
116929 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
116947 assert( pIdx==0 || pIdx->nSample==0 );
116949 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
116950 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
116951 nIdxCol = pIdx->nKeyCol;
116953 nIdxCol = pIdx->nColumn;
116955 pIdx->nSampleCol = nIdxCol;
116960 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
116961 if( pIdx->aSample==0 ){
116965 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
116966 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
116967 pIdx->pTable->tabFlags |= TF_HasStat4;
116969 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
116970 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
116971 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
116973 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
116982 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
116997 nCol = pIdx->nSampleCol;
117002 pSample = &pIdx->aSample[pIdx->nSample];
117003 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
117004 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
117005 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
117013 pSample->n = sqlite3_column_bytes(pStmt, 4);
117014 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
117015 if( pSample->p==0 ){
117019 if( pSample->n ){
117020 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
117022 pIdx->nSample++;
117037 assert( db->lookaside.bDisable );
117067 ** If an OOM error occurs, this function always sets db->mallocFailed.
117076 Schema *pSchema = db->aDb[iDb].pSchema;
117079 assert( iDb>=0 && iDb<db->nDb );
117080 assert( db->aDb[iDb].pBt!=0 );
117084 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
117086 pTab->tabFlags &= ~TF_HasStat1;
117088 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117090 pIdx->hasStat1 = 0;
117093 pIdx->aSample = 0;
117099 sInfo.zDatabase = db->aDb[iDb].zDbSName;
117115 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117117 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
117127 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
117129 sqlite3_free(pIdx->aiRowEst);
117130 pIdx->aiRowEst = 0;
117183 if( pExpr->op!=TK_ID ){
117186 pExpr->op = TK_STRING;
117198 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
117204 ** An SQL user-function registered to do the work of an ATTACH statement. The
117214 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
117215 ** new database, close the database on db->init.iDb and reopen it as an
117243 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
117250 ** from sqlite3_deserialize() to close database db->init.iDb and
117254 pNew = &db->aDb[db->init.iDb];
117255 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
117256 pNew->pBt = 0;
117257 pNew->pSchema = 0;
117258 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
117268 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
117269 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
117270 db->aLimit[SQLITE_LIMIT_ATTACHED]
117274 for(i=0; i<db->nDb; i++){
117282 /* Allocate the new entry in the db->aDb[] array and initialize the schema
117285 if( db->aDb==db->aDbStatic ){
117286 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
117288 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
117290 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
117293 db->aDb = aNew;
117294 pNew = &db->aDb[db->nDb];
117301 flags = db->openFlags;
117302 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
117305 sqlite3_result_error(context, zErr, -1);
117311 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
117312 db->nDb++;
117313 pNew->zDbSName = sqlite3DbStrDup(db, zName);
117315 db->noSharedCache = 0;
117321 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
117322 if( !pNew->pSchema ){
117324 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
117329 sqlite3BtreeEnter(pNew->pBt);
117330 pPager = sqlite3BtreePager(pNew->pBt);
117331 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
117332 sqlite3BtreeSecureDelete(pNew->pBt,
117333 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
117335 sqlite3BtreeSetPagerFlags(pNew->pBt,
117336 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
117338 sqlite3BtreeLeave(pNew->pBt);
117340 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
117341 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
117363 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
117371 if( nKey || sqlite3BtreeGetRequestedReserve(db->aDb[0].pBt)>0 ){
117372 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
117383 ** remove the entry from the db->aDb[] array. i.e. put everything back the
117388 db->init.iDb = 0;
117389 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
117400 if( newAuth<db->auth.authLevel ){
117407 int iDb = db->nDb - 1;
117409 if( db->aDb[iDb].pBt ){
117410 sqlite3BtreeClose(db->aDb[iDb].pBt);
117411 db->aDb[iDb].pBt = 0;
117412 db->aDb[iDb].pSchema = 0;
117415 db->nDb = iDb;
117432 sqlite3_result_error(context, zErrDyn, -1);
117439 ** An SQL user-function registered to do the work of an DETACH statement. The
117461 for(i=0; i<db->nDb; i++){
117462 pDb = &db->aDb[i];
117463 if( pDb->pBt==0 ) continue;
117467 if( i>=db->nDb ){
117475 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
117476 || sqlite3BtreeIsInBackup(pDb->pBt)
117484 assert( db->aDb[1].pSchema );
117485 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
117488 if( pTrig->pTabSchema==pDb->pSchema ){
117489 pTrig->pTabSchema = pTrig->pSchema;
117494 sqlite3BtreeClose(pDb->pBt);
117495 pDb->pBt = 0;
117496 pDb->pSchema = 0;
117501 sqlite3_result_error(context, zErr, -1);
117520 sqlite3* db = pParse->db;
117523 if( pParse->nErr ) goto attach_end;
117538 if( pAuthArg->op==TK_STRING ){
117540 zAuthArg = pAuthArg->u.zToken;
117558 assert( v || db->mallocFailed );
117560 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
117561 pFunc->nArg, pFunc, 0);
117620 DbFixer *pFix = p->u.pFix;
117621 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
117622 if( pExpr->op==TK_VARIABLE ){
117623 if( pFix->pParse->db->init.busy ){
117624 pExpr->op = TK_NULL;
117626 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
117637 DbFixer *pFix = p->u.pFix;
117640 sqlite3 *db = pFix->pParse->db;
117641 int iDb = sqlite3FindDbName(db, pFix->zDb);
117642 SrcList *pList = pSelect->pSrc;
117645 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
117646 if( pFix->bTemp==0 ){
117647 if( pItem->zDatabase ){
117648 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
117649 sqlite3ErrorMsg(pFix->pParse,
117651 pFix->zType, pFix->pName, pItem->zDatabase);
117654 sqlite3DbFree(db, pItem->zDatabase);
117655 pItem->zDatabase = 0;
117656 pItem->fg.notCte = 1;
117658 pItem->pSchema = pFix->pSchema;
117659 pItem->fg.fromDDL = 1;
117662 if( pList->a[i].fg.isUsing==0
117663 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
117669 if( pSelect->pWith ){
117670 for(i=0; i<pSelect->pWith->nCte; i++){
117671 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
117690 sqlite3 *db = pParse->db;
117691 assert( db->nDb>iDb );
117692 pFix->pParse = pParse;
117693 pFix->zDb = db->aDb[iDb].zDbSName;
117694 pFix->pSchema = db->aDb[iDb].pSchema;
117695 pFix->zType = zType;
117696 pFix->pName = pName;
117697 pFix->bTemp = (iDb==1);
117698 pFix->w.pParse = pParse;
117699 pFix->w.xExprCallback = fixExprCb;
117700 pFix->w.xSelectCallback = fixSelectCb;
117701 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
117702 pFix->w.walkerDepth = 0;
117703 pFix->w.eCode = 0;
117704 pFix->w.u.pFix = pFix;
117718 ** pParse->zErrMsg and these routines return non-zero. If everything
117730 res = sqlite3WalkSelect(&pFix->w, &s);
117739 return sqlite3WalkSelect(&pFix->w, pSelect);
117745 return sqlite3WalkExpr(&pFix->w, pExpr);
117755 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
117756 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
117757 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
117758 || sqlite3FixSrcList(pFix, pStep->pFrom)
117765 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
117766 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
117767 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
117768 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
117769 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
117776 pStep = pStep->pNext;
117799 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
117846 ** means that the SQL statement will never-run - the sqlite3_exec() call
117862 sqlite3_mutex_enter(db->mutex);
117863 db->xAuth = (sqlite3_xauth)xAuth;
117864 db->pAuthArg = pArg;
117865 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
117866 sqlite3_mutex_leave(db->mutex);
117871 ** Write an error message into pParse->zErrMsg that explains that the
117872 ** user-supplied authorization function returned an illegal value.
117876 pParse->rc = SQLITE_ERROR;
117894 sqlite3 *db = pParse->db; /* Database handle */
117895 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
117898 if( db->init.busy ) return SQLITE_OK;
117899 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
117901 ,db->auth.zAuthUser
117906 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
117908 pParse->rc = SQLITE_AUTH;
117932 int iSrc; /* Index in pTabList->a[] of table being read */
117936 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
117938 assert( pParse->db->xAuth!=0 );
117939 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
117946 if( pExpr->op==TK_TRIGGER ){
117947 pTab = pParse->pTriggerTab;
117950 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
117951 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
117952 pTab = pTabList->a[iSrc].pTab;
117957 iCol = pExpr->iColumn;
117961 assert( iCol<pTab->nCol );
117962 zCol = pTab->aCol[iCol].zCnName;
117963 }else if( pTab->iPKey>=0 ){
117964 assert( pTab->iPKey<pTab->nCol );
117965 zCol = pTab->aCol[pTab->iPKey].zCnName;
117969 assert( iDb>=0 && iDb<pParse->db->nDb );
117970 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
117971 pExpr->op = TK_NULL;
117988 sqlite3 *db = pParse->db;
117994 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
117995 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
117999 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
118000 ** callback are either NULL pointers or zero-terminated strings that
118008 testcase( pParse->zAuthContext==0 );
118010 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
118012 ,db->auth.zAuthUser
118017 pParse->rc = SQLITE_AUTH;
118028 ** popped. Or if pParse==0, this routine is a no-op.
118036 pContext->pParse = pParse;
118037 pContext->zAuthContext = pParse->zAuthContext;
118038 pParse->zAuthContext = zContext;
118046 if( pContext->pParse ){
118047 pContext->pParse->zAuthContext = pContext->zAuthContext;
118048 pContext->pParse = 0;
118095 ** Record the fact that we want to lock a table at run-time.
118118 for(i=0; i<pToplevel->nTableLock; i++){
118119 p = &pToplevel->aTableLock[i];
118120 if( p->iDb==iDb && p->iTab==iTab ){
118121 p->isWriteLock = (p->isWriteLock || isWriteLock);
118126 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
118127 pToplevel->aTableLock =
118128 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
118129 if( pToplevel->aTableLock ){
118130 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
118131 p->iDb = iDb;
118132 p->iTab = iTab;
118133 p->isWriteLock = isWriteLock;
118134 p->zLockName = zName;
118136 pToplevel->nTableLock = 0;
118137 sqlite3OomFault(pToplevel->db);
118148 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
118158 Vdbe *pVdbe = pParse->pVdbe;
118161 for(i=0; i<pParse->nTableLock; i++){
118162 TableLock *p = &pParse->aTableLock[i];
118163 int p1 = p->iDb;
118164 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
118165 p->zLockName, P4_STATIC);
118173 ** Return TRUE if the given yDbMask object is empty - if it contains no
118200 assert( pParse->pToplevel==0 );
118201 db = pParse->db;
118202 assert( db->pParse==pParse );
118203 if( pParse->nested ) return;
118204 if( pParse->nErr ){
118205 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
118208 assert( db->mallocFailed==0 );
118213 v = pParse->pVdbe;
118215 if( db->init.busy ){
118216 pParse->rc = SQLITE_DONE;
118220 if( v==0 ) pParse->rc = SQLITE_ERROR;
118222 assert( !pParse->isMultiWrite
118223 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
118225 if( pParse->bReturning ){
118226 Returning *pReturning = pParse->u1.pReturning;
118230 if( pReturning->nRetCol ){
118233 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
118235 reg = pReturning->iRetReg;
118236 for(i=0; i<pReturning->nRetCol; i++){
118237 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
118240 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
118248 if( pParse->nTableLock>0 && db->init.busy==0 ){
118250 if( db->auth.authLevel<UAUTH_User ){
118252 pParse->rc = SQLITE_AUTH_USER;
118264 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
118266 assert( db->nDb>0 );
118270 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
118272 pSchema = db->aDb[iDb].pSchema;
118276 DbMaskTest(pParse->writeMask,iDb), /* P2 */
118277 pSchema->schema_cookie, /* P3 */
118278 pSchema->iGeneration /* P4 */
118280 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
118282 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
118283 }while( ++iDb<db->nDb );
118285 for(i=0; i<pParse->nVtabLock; i++){
118286 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
118289 pParse->nVtabLock = 0;
118293 ** obtain the required table-locks. This is a no-op unless the
118294 ** shared-cache feature is enabled.
118309 if( pParse->pConstExpr ){
118310 ExprList *pEL = pParse->pConstExpr;
118311 pParse->okConstFactor = 0;
118312 for(i=0; i<pEL->nExpr; i++){
118313 int iReg = pEL->a[i].u.iConstExprReg;
118314 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
118318 if( pParse->bReturning ){
118319 Returning *pRet = pParse->u1.pReturning;
118320 if( pRet->nRetCol ){
118321 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
118331 assert( v!=0 || pParse->nErr );
118332 assert( db->mallocFailed==0 || pParse->nErr );
118333 if( pParse->nErr==0 ){
118336 assert( pParse->pAinc==0 || pParse->nTab>0 );
118338 pParse->rc = SQLITE_DONE;
118340 pParse->rc = SQLITE_ERROR;
118353 ** * Built-in SQL functions always take precedence over application-defined
118355 ** built-in function.
118360 sqlite3 *db = pParse->db;
118361 u32 savedDbFlags = db->mDbFlags;
118364 if( pParse->nErr ) return;
118365 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
118373 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
118374 pParse->nErr++;
118377 pParse->nested++;
118380 db->mDbFlags |= DBFLAG_PreferBuiltin;
118382 db->mDbFlags = savedDbFlags;
118385 pParse->nested--;
118399 ** Locate the in-memory structure that describes a particular database
118419 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
118424 for(i=0; i<db->nDb; i++){
118425 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
118427 if( i>=db->nDb ){
118436 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
118443 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
118448 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
118455 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
118458 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
118461 for(i=2; i<db->nDb; i++){
118463 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
118468 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
118470 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
118479 ** Locate the in-memory structure that describes a particular database
118482 ** error message in pParse->zErrMsg.
118485 ** routine leaves an error message in pParse->zErrMsg where
118495 sqlite3 *db = pParse->db;
118499 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
118511 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
118512 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
118517 testcase( pMod->pEpoTab==0 );
118518 return pMod->pEpoTab;
118523 pParse->checkSchema = 1;
118524 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
118536 assert( HasRowid(p) || p->iPKey<0 );
118547 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
118548 ** non-NULL if it is part of a view or trigger program definition. See
118557 assert( p->pSchema==0 || p->zDatabase==0 );
118558 if( p->pSchema ){
118559 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
118560 zDb = pParse->db->aDb[iDb].zDbSName;
118562 zDb = p->zDatabase;
118564 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
118584 ** Locate the in-memory structure that describes
118600 for(i=OMIT_TEMPDB; i<db->nDb; i++){
118602 Schema *pSchema = db->aDb[j].pSchema;
118606 p = sqlite3HashFind(&pSchema->idxHash, zName);
118619 sqlite3ExprDelete(db, p->pPartIdxWhere);
118620 sqlite3ExprListDelete(db, p->aColExpr);
118621 sqlite3DbFree(db, p->zColAff);
118622 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
118624 sqlite3_free(p->aiRowEst);
118640 pHash = &db->aDb[iDb].pSchema->idxHash;
118643 if( pIndex->pTable->pIndex==pIndex ){
118644 pIndex->pTable->pIndex = pIndex->pNext;
118649 p = pIndex->pTable->pIndex;
118650 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
118651 if( ALWAYS(p && p->pNext==pIndex) ){
118652 p->pNext = pIndex->pNext;
118657 db->mDbFlags |= DBFLAG_SchemaChange;
118661 ** Look through the list of open database files in db->aDb[] and if
118663 ** db->aDb[] structure to a smaller size, if possible.
118670 for(i=j=2; i<db->nDb; i++){
118671 struct Db *pDb = &db->aDb[i];
118672 if( pDb->pBt==0 ){
118673 sqlite3DbFree(db, pDb->zDbSName);
118674 pDb->zDbSName = 0;
118678 db->aDb[j] = db->aDb[i];
118682 db->nDb = j;
118683 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
118684 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
118685 sqlite3DbFree(db, db->aDb);
118686 db->aDb = db->aDbStatic;
118692 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
118697 assert( iDb<db->nDb );
118703 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
118706 if( db->nSchemaLock==0 ){
118707 for(i=0; i<db->nDb; i++){
118709 sqlite3SchemaClear(db->aDb[i].pSchema);
118722 for(i=0; i<db->nDb; i++){
118723 Db *pDb = &db->aDb[i];
118724 if( pDb->pSchema ){
118725 if( db->nSchemaLock==0 ){
118726 sqlite3SchemaClear(pDb->pSchema);
118732 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
118735 if( db->nSchemaLock==0 ){
118744 db->mDbFlags &= ~DBFLAG_SchemaChange;
118760 pList = pTab->u.tab.pDfltList;
118761 if( pCol->iDflt==0
118763 || NEVER(pList->nExpr<pCol->iDflt)
118765 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
118766 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
118768 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
118769 pList->a[pCol->iDflt-1].pExpr = pExpr;
118779 if( pCol->iDflt==0 ) return 0;
118781 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
118782 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
118783 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
118798 n = sqlite3Strlen30(pCol->zCnName) + 1;
118799 if( pCol->colFlags & COLFLAG_HASTYPE ){
118800 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
118803 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
118805 pCol->zCnName = zNew;
118806 memcpy(pCol->zCnName + n, zColl, nColl);
118807 pCol->colFlags |= COLFLAG_HASCOLL;
118816 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
118817 z = pCol->zCnName;
118819 if( pCol->colFlags & COLFLAG_HASTYPE ){
118834 if( (pCol = pTable->aCol)!=0 ){
118835 for(i=0; i<pTable->nCol; i++, pCol++){
118836 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
118837 sqlite3DbFree(db, pCol->zCnName);
118839 sqlite3DbNNFreeNN(db, pTable->aCol);
118841 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
118843 if( db->pnBytesFreed==0 ){
118844 pTable->aCol = 0;
118845 pTable->nCol = 0;
118847 pTable->u.tab.pDfltList = 0;
118865 ** db parameter can be used with db->pnBytesFreed to measure the memory
118881 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
118887 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
118888 pNext = pIndex->pNext;
118889 assert( pIndex->pSchema==pTable->pSchema
118890 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
118891 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
118892 char *zName = pIndex->zName;
118894 &pIndex->pSchema->idxHash, zName, 0
118896 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
118912 sqlite3SelectDelete(db, pTable->u.view.pSelect);
118918 sqlite3DbFree(db, pTable->zName);
118919 sqlite3DbFree(db, pTable->zColAff);
118920 sqlite3ExprListDelete(db, pTable->pCheck);
118930 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
118944 assert( iDb>=0 && iDb<db->nDb );
118947 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
118948 pDb = &db->aDb[iDb];
118949 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
118951 u8 tableType = p->eTabType;
118954 db->mDbFlags |= DBFLAG_SchemaChange;
118957 db->isDropTable = 1;
118958 db->mDropTableName = sqlite3_malloc(strlen(zTabName) + 1);
118959 if( db->mDropTableName!=NULL ){
118960 memcpy(db->mDropTableName, zTabName, strlen(zTabName) + 1);
118962 db->mDropSchemaName = sqlite3_malloc(strlen(db->aDb[iDb].zDbSName) + 1);
118963 if( db->mDropSchemaName!=NULL ){
118964 memcpy(db->mDropSchemaName, db->aDb[iDb].zDbSName, strlen(db->aDb[iDb].zDbSName) + 1);
118986 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
119002 if( p->nTab==0 ){
119003 p->nTab = 1;
119008 ** Parameter zName points to a nul-terminated buffer containing the name
119010 ** function returns the index of the named database in db->aDb[], or
119011 ** -1 if the named db cannot be found.
119014 int i = -1; /* Database number */
119017 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
119018 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
119030 ** index of the named database in db->aDb[], or -1 if the named db
119065 sqlite3 *db = pParse->db;
119068 if( pName2->n>0 ){
119069 if( db->init.busy ) {
119071 return -1;
119077 return -1;
119080 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
119081 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
119082 iDb = db->init.iDb;
119092 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
119093 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119095 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119097 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
119099 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
119103 ** This routine is used to check if the UTF-8 string zName is a legal
119119 sqlite3 *db = pParse->db;
119121 || db->init.imposterTable
119127 if( db->init.busy ){
119128 if( sqlite3_stricmp(zType, db->init.azInit[0])
119129 || sqlite3_stricmp(zName, db->init.azInit[1])
119130 || sqlite3_stricmp(zTblName, db->init.azInit[2])
119136 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
119153 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
119160 ** find the (first) offset of that column in index pIdx. Or return -1
119165 for(i=0; i<pIdx->nColumn; i++){
119166 if( iCol==pIdx->aiColumn[i] ) return i;
119168 return -1;
119181 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
119184 if( pTab->tabFlags & TF_HasVirtual ){
119187 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
119199 ** the N-th virtual column (zero-based) then the storage number is
119200 ** the number of non-virtual columns in the table plus N.
119216 ** -- 0 1 2 3 4 5 6 7 8
119227 ** this routine is a no-op macro. If the pTab does not have any virtual
119228 ** columns, then this routine is no-op that always return iCol. If iCol
119234 assert( iCol<pTab->nCol );
119235 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
119237 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
119239 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
119241 return pTab->nNVCol + i - n;
119255 ** is a read-only no-op.
119258 int iReg = ++pParse->nMem;
119276 ** The new table record is initialized and put in pParse->pNewTable.
119293 sqlite3 *db = pParse->db;
119298 if( db->init.busy && db->init.newTnum==1 ){
119300 iDb = db->init.iDb;
119307 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
119319 pParse->sNameToken = *pName;
119324 if( db->init.iDb==1 ) isTemp = 1;
119335 char *zDb = db->aDb[iDb].zDbSName;
119354 char *zDb = db->aDb[iDb].zDbSName;
119364 assert( !db->init.busy || CORRUPT_DB );
119378 assert( db->mallocFailed );
119379 pParse->rc = SQLITE_NOMEM_BKPT;
119380 pParse->nErr++;
119383 pTable->zName = zName;
119384 pTable->iPKey = -1;
119385 pTable->pSchema = db->aDb[iDb].pSchema;
119386 pTable->nTabRef = 1;
119388 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
119390 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
119392 assert( pParse->pNewTable==0 );
119393 pParse->pNewTable = pTable;
119403 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
119420 reg1 = pParse->regRowid = ++pParse->nMem;
119421 reg2 = pParse->regRoot = ++pParse->nMem;
119422 reg3 = ++pParse->nMem;
119426 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
119432 /* This just creates a place-holder record in the sqlite_schema table.
119436 ** The rowid for the new entry is left in register pParse->regRowid.
119437 ** The root page number of the new table is left in reg pParse->regRoot.
119447 assert( !pParse->bReturning );
119448 pParse->u1.addrCrTab =
119459 /* Normal (non-error) return. */
119464 pParse->checkSchema = 1;
119474 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
119475 pCol->colFlags |= COLFLAG_HIDDEN;
119476 if( pTab ) pTab->tabFlags |= TF_HasHidden;
119477 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
119478 pTab->tabFlags |= TF_OOOHidden;
119486 ** with any application-generated triggers.
119495 pHash = &(db->aDb[1].pSchema->trigHash);
119497 sqlite3ExprListDelete(db, pRet->pReturnEL);
119520 sqlite3 *db = pParse->db;
119521 if( pParse->pNewTrigger ){
119524 assert( pParse->bReturning==0 );
119526 pParse->bReturning = 1;
119532 pParse->u1.pReturning = pRet;
119533 pRet->pParse = pParse;
119534 pRet->pReturnEL = pList;
119537 testcase( pParse->earlyCleanup );
119538 if( db->mallocFailed ) return;
119539 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
119540 pRet->retTrig.op = TK_RETURNING;
119541 pRet->retTrig.tr_tm = TRIGGER_AFTER;
119542 pRet->retTrig.bReturning = 1;
119543 pRet->retTrig.pSchema = db->aDb[1].pSchema;
119544 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
119545 pRet->retTrig.step_list = &pRet->retTStep;
119546 pRet->retTStep.op = TK_RETURNING;
119547 pRet->retTStep.pTrig = &pRet->retTrig;
119548 pRet->retTStep.pExprList = pList;
119549 pHash = &(db->aDb[1].pSchema->trigHash);
119550 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
119551 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
119552 ==&pRet->retTrig ){
119571 sqlite3 *db = pParse->db;
119578 if( (p = pParse->pNewTable)==0 ) return;
119579 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
119580 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
119590 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
119592 sType.n -= 6;
119593 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119595 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
119597 sType.n -= 9;
119598 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119627 for(i=0; i<p->nCol; i++){
119628 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
119634 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
119639 p->aCol = aNew;
119640 pCol = &p->aCol[p->nCol];
119641 memset(pCol, 0, sizeof(p->aCol[0]));
119642 pCol->zCnName = z;
119643 pCol->hName = hName;
119649 pCol->affinity = affinity;
119650 pCol->eCType = eType;
119651 pCol->szEst = szEst;
119655 pCol->colFlags |= COLFLAG_SORTERREF;
119664 pCol->affinity = sqlite3AffinityType(zType, pCol);
119665 pCol->colFlags |= COLFLAG_HASTYPE;
119667 p->nCol++;
119668 p->nNVCol++;
119669 pParse->constraintName.n = 0;
119681 p = pParse->pNewTable;
119682 if( p==0 || NEVER(p->nCol<1) ) return;
119683 pCol = &p->aCol[p->nCol-1];
119684 pCol->notNull = (u8)onError;
119685 p->tabFlags |= TF_HasNotNull;
119689 if( pCol->colFlags & COLFLAG_UNIQUE ){
119691 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
119692 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
119693 if( pIdx->aiColumn[0]==p->nCol-1 ){
119694 pIdx->uniqNotNull = 1;
119704 ** This routine does a case-independent search of zType for the
119712 ** --------------------------------
119770 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
119777 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
119782 pCol->colFlags |= COLFLAG_SORTERREF;
119787 pCol->szEst = v;
119810 sqlite3 *db = pParse->db;
119811 p = pParse->pNewTable;
119813 int isInit = db->init.busy && db->init.iDb!=1;
119814 pCol = &(p->aCol[p->nCol-1]);
119817 pCol->zCnName);
119819 }else if( pCol->colFlags & COLFLAG_GENERATED ){
119820 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119821 testcase( pCol->colFlags & COLFLAG_STORED );
119862 if( p->op==TK_STRING ){
119863 p->op = TK_ID;
119864 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
119865 p->pLeft->op = TK_ID;
119873 pCol->colFlags |= COLFLAG_PRIMKEY;
119875 if( pCol->colFlags & COLFLAG_GENERATED ){
119876 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119877 testcase( pCol->colFlags & COLFLAG_STORED );
119896 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
119909 Table *pTab = pParse->pNewTable;
119911 int iCol = -1, i;
119914 if( pTab->tabFlags & TF_HasPrimaryKey ){
119916 "table \"%s\" has more than one primary key", pTab->zName);
119919 pTab->tabFlags |= TF_HasPrimaryKey;
119921 iCol = pTab->nCol - 1;
119922 pCol = &pTab->aCol[iCol];
119926 nTerm = pList->nExpr;
119928 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
119931 if( pCExpr->op==TK_ID ){
119934 zCName = pCExpr->u.zToken;
119935 for(iCol=0; iCol<pTab->nCol; iCol++){
119936 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
119937 pCol = &pTab->aCol[iCol];
119947 && pCol->eCType==COLTYPE_INTEGER
119951 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
119952 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
119954 pTab->iPKey = iCol;
119955 pTab->keyConf = (u8)onError;
119957 pTab->tabFlags |= autoInc*TF_Autoincrement;
119958 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
119972 sqlite3ExprListDelete(pParse->db, pList);
119986 Table *pTab = pParse->pNewTable;
119987 sqlite3 *db = pParse->db;
119989 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
119991 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
119992 if( pParse->constraintName.n ){
119993 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
119997 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
119999 t.n = (int)(zEnd - t.z);
120000 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
120005 sqlite3ExprDelete(pParse->db, pCheckExpr);
120019 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
120020 i = p->nCol-1;
120021 db = pParse->db;
120027 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
120033 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120034 assert( pIdx->nKeyCol==1 );
120035 if( pIdx->aiColumn[0]==i ){
120036 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
120049 Table *pTab = pParse->pNewTable;
120055 pCol = &(pTab->aCol[pTab->nCol-1]);
120060 if( pCol->iDflt>0 ) goto generated_error;
120062 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
120063 /* no-op */
120064 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
120070 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
120071 pCol->colFlags |= eType;
120074 pTab->tabFlags |= eType;
120075 if( pCol->colFlags & COLFLAG_PRIMKEY ){
120084 pCol->zCnName);
120086 sqlite3ExprDelete(pParse->db, pExpr);
120089 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
120091 sqlite3ExprDelete(pParse->db, pExpr);
120105 ** This plan is not completely bullet-proof. It is possible for
120111 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
120112 ** the schema-version whenever the schema changes.
120115 sqlite3 *db = pParse->db;
120116 Vdbe *v = pParse->pVdbe;
120119 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
120142 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
120146 ** If the string zSignedIdent consists entirely of alpha-numeric
120149 ** it is quoted using double-quotes.
120185 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
120186 n += identLength(pCol->zCnName) + 5;
120188 n += identLength(p->zName);
120198 n += 35 + 6*p->nCol;
120206 identPut(zStmt, &k, p->zName);
120208 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
120219 sqlite3_snprintf(n-k, &zStmt[k], zSep);
120222 identPut(zStmt, &k, pCol->zCnName);
120223 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
120224 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
120225 testcase( pCol->affinity==SQLITE_AFF_BLOB );
120226 testcase( pCol->affinity==SQLITE_AFF_TEXT );
120227 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
120228 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
120229 testcase( pCol->affinity==SQLITE_AFF_REAL );
120231 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
120233 assert( pCol->affinity==SQLITE_AFF_BLOB
120234 || pCol->affinity==sqlite3AffinityType(zType, 0) );
120239 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
120250 if( pIdx->nColumn>=N ) return SQLITE_OK;
120251 assert( pIdx->isResized==0 );
120255 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
120256 pIdx->azColl = (const char**)zExtra;
120258 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
120259 pIdx->aiRowLogEst = (LogEst*)zExtra;
120261 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
120262 pIdx->aiColumn = (i16*)zExtra;
120264 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
120265 pIdx->aSortOrder = (u8*)zExtra;
120266 pIdx->nColumn = N;
120267 pIdx->isResized = 1;
120278 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
120279 wTable += pTabCol->szEst;
120281 if( pTab->iPKey<0 ) wTable++;
120282 pTab->szTabRow = sqlite3LogEst(wTable*4);
120291 const Column *aCol = pIdx->pTable->aCol;
120292 for(i=0; i<pIdx->nColumn; i++){
120293 i16 x = pIdx->aiColumn[i];
120294 assert( x<pIdx->pTable->nCol );
120295 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
120297 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
120305 while( nCol-- > 0 ){
120315 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
120328 assert( nKey<=pIdx->nColumn );
120329 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
120330 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
120331 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
120332 assert( pPk->pTable==pIdx->pTable );
120334 j = pPk->aiColumn[iCol];
120337 assert( pIdx->aiColumn[i]>=0 || j>=0 );
120338 if( pIdx->aiColumn[i]==j
120339 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
120352 ** high-order bit of colNotIdxed is always 1. All unindexed columns
120355 ** 2019-10-24: For the purpose of this computation, virtual columns are
120363 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
120369 Table *pTab = pIdx->pTable;
120370 for(j=pIdx->nColumn-1; j>=0; j--){
120371 int x = pIdx->aiColumn[j];
120372 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
120373 testcase( x==BMS-1 );
120374 testcase( x==BMS-2 );
120375 if( x<BMS-1 ) m |= MASKBIT(x);
120378 pIdx->colNotIdxed = ~m;
120379 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
120412 sqlite3 *db = pParse->db;
120413 Vdbe *v = pParse->pVdbe;
120417 if( !db->init.imposterTable ){
120418 for(i=0; i<pTab->nCol; i++){
120419 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
120420 && (pTab->aCol[i].notNull==OE_None)
120422 pTab->aCol[i].notNull = OE_Abort;
120425 pTab->tabFlags |= TF_HasNotNull;
120431 assert( !pParse->bReturning );
120432 if( pParse->u1.addrCrTab ){
120434 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
120440 if( pTab->iPKey>=0 ){
120443 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
120447 pTab->tabFlags &= ~TF_WithoutRowid;
120451 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
120453 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
120454 assert( pParse->pNewTable==pTab );
120455 pTab->iPKey = -1;
120456 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
120458 if( pParse->nErr ){
120459 pTab->tabFlags &= ~TF_WithoutRowid;
120462 assert( db->mallocFailed==0 );
120464 assert( pPk->nKeyCol==1 );
120474 for(i=j=1; i<pPk->nKeyCol; i++){
120476 pPk->nColumn--;
120478 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
120479 pPk->azColl[j] = pPk->azColl[i];
120480 pPk->aSortOrder[j] = pPk->aSortOrder[i];
120481 pPk->aiColumn[j++] = pPk->aiColumn[i];
120484 pPk->nKeyCol = j;
120487 pPk->isCovering = 1;
120488 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
120489 nPk = pPk->nColumn = pPk->nKeyCol;
120495 if( v && pPk->tnum>0 ){
120496 assert( db->init.busy==0 );
120497 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
120501 pPk->tnum = pTab->tnum;
120503 /* Update the in-memory representation of all UNIQUE indices by converting
120506 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120510 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
120511 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
120517 pIdx->nColumn = pIdx->nKeyCol;
120520 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
120521 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
120522 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
120523 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
120524 pIdx->aiColumn[j] = pPk->aiColumn[i];
120525 pIdx->azColl[j] = pPk->azColl[i];
120526 if( pPk->aSortOrder[i] ){
120528 pIdx->bAscKeyBug = 1;
120533 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
120534 assert( pIdx->nColumn>=j );
120540 for(i=0; i<pTab->nCol; i++){
120541 if( !hasColumn(pPk->aiColumn, nPk, i)
120542 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
120545 for(i=0, j=nPk; i<pTab->nCol; i++){
120546 if( !hasColumn(pPk->aiColumn, j, i)
120547 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
120549 assert( j<pPk->nColumn );
120550 pPk->aiColumn[j] = i;
120551 pPk->azColl[j] = sqlite3StrBINARY;
120555 assert( pPk->nColumn==j );
120556 assert( pTab->nNVCol<=j );
120571 nName = sqlite3Strlen30(pTab->zName);
120572 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
120574 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
120576 if( pMod->pModule->iVersion<3 ) return 0;
120577 if( pMod->pModule->xShadowName==0 ) return 0;
120578 return pMod->pModule->xShadowName(zName+nName+1);
120590 int nName; /* Length of pTab->zName */
120595 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
120597 if( NEVER(pMod->pModule==0) ) return;
120598 if( pMod->pModule->iVersion<3 ) return;
120599 if( pMod->pModule->xShadowName==0 ) return;
120600 assert( pTab->zName!=0 );
120601 nName = sqlite3Strlen30(pTab->zName);
120602 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
120604 assert( pOther->zName!=0 );
120606 if( pOther->tabFlags & TF_Shadow ) continue;
120607 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
120608 && pOther->zName[nName]=='_'
120609 && pMod->pModule->xShadowName(pOther->zName+nName+1)
120611 pOther->tabFlags |= TF_Shadow;
120662 #define markExprListImmutable(X) /* no-op */
120675 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
120694 sqlite3 *db = pParse->db; /* The database connection */
120701 p = pParse->pNewTable;
120704 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
120705 p->tabFlags |= TF_Shadow;
120708 /* If the db->init.busy is 1 it means we are reading the SQL off the
120711 ** for the table from the db->init.newTnum field. (The page number
120715 ** table itself. So mark it read-only.
120717 if( db->init.busy ){
120718 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
120722 p->tnum = db->init.newTnum;
120723 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
120737 p->tabFlags |= TF_Strict;
120738 for(ii=0; ii<p->nCol; ii++){
120739 Column *pCol = &p->aCol[ii];
120740 if( pCol->eCType==COLTYPE_CUSTOM ){
120741 if( pCol->colFlags & COLFLAG_HASTYPE ){
120744 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
120748 p->zName, pCol->zCnName);
120751 }else if( pCol->eCType==COLTYPE_ANY ){
120752 pCol->affinity = SQLITE_AFF_BLOB;
120754 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
120755 && p->iPKey!=ii
120756 && pCol->notNull == OE_None
120758 pCol->notNull = OE_Abort;
120759 p->tabFlags |= TF_HasNotNull;
120764 assert( (p->tabFlags & TF_HasPrimaryKey)==0
120765 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
120766 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
120767 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
120771 if( (p->tabFlags & TF_Autoincrement) ){
120776 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
120777 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
120780 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
120783 iDb = sqlite3SchemaToIndex(db, p->pSchema);
120788 if( p->pCheck ){
120789 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
120790 if( pParse->nErr ){
120793 sqlite3ExprListDelete(db, p->pCheck);
120794 p->pCheck = 0;
120796 markExprListImmutable(p->pCheck);
120801 if( p->tabFlags & TF_HasGenerated ){
120803 testcase( p->tabFlags & TF_HasVirtual );
120804 testcase( p->tabFlags & TF_HasStored );
120805 for(ii=0; ii<p->nCol; ii++){
120806 u32 colFlags = p->aCol[ii].colFlags;
120808 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
120818 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
120826 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
120834 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120844 if( !db->init.busy ){
120872 ** statement to populate the new table. The root-page number for the
120873 ** new table is in register pParse->regRoot.
120879 ** A shared-cache write-lock is not required to write to the new table,
120880 ** as a schema-lock must have already been obtained to create it. Since
120881 ** a schema-lock excludes all other database users, the write-lock would
120886 int regYield; /* Register holding co-routine entry-point */
120887 int addrTop; /* Top of the co-routine */
120894 pParse->rc = SQLITE_ERROR;
120895 pParse->nErr++;
120898 regYield = ++pParse->nMem;
120899 regRec = ++pParse->nMem;
120900 regRowid = ++pParse->nMem;
120901 assert(pParse->nTab==1);
120903 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
120905 pParse->nTab = 2;
120908 if( pParse->nErr ) return;
120911 assert( p->aCol==0 );
120912 p->nCol = p->nNVCol = pSelTab->nCol;
120913 p->aCol = pSelTab->aCol;
120914 pSelTab->nCol = 0;
120915 pSelTab->aCol = 0;
120919 if( pParse->nErr ) return;
120921 sqlite3VdbeJumpHere(v, addrTop - 1);
120937 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
120938 n = (int)(pEnd2->z - pParse->sNameToken.z);
120939 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
120941 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
120953 db->aDb[iDb].zDbSName,
120955 p->zName,
120956 p->zName,
120957 pParse->regRoot,
120959 pParse->regRowid
120968 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
120969 Db *pDb = &db->aDb[iDb];
120971 if( pDb->pSchema->pSeqTab==0 ){
120974 pDb->zDbSName
120982 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
120985 /* Add the table to the in-memory representation of the database.
120987 if( db->init.busy ){
120989 Schema *pSchema = p->pSchema;
120991 assert( HasRowid(p) || p->iPKey<0 );
120992 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
120998 pParse->pNewTable = 0;
120999 db->mDbFlags |= DBFLAG_SchemaChange;
121004 assert( !pParse->nested );
121006 if( strcmp(p->zName, "sqlite_sequence")==0 ){
121008 p->pSchema->pSeqTab = p;
121016 if( pCons->z==0 ){
121019 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
121045 sqlite3 *db = pParse->db;
121047 if( pParse->nVar>0 ){
121052 p = pParse->pNewTable;
121053 if( p==0 || pParse->nErr ) goto create_view_fail;
121058 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
121061 p->tabFlags |= TF_NoVisibleRowid;
121065 iDb = sqlite3SchemaToIndex(db, p->pSchema);
121071 ** allocated rather than point to the input string - which means that
121074 pSelect->selFlags |= SF_View;
121076 p->u.view.pSelect = pSelect;
121079 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
121081 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
121082 p->eTabType = TABTYP_VIEW;
121083 if( db->mallocFailed ) goto create_view_fail;
121088 sEnd = pParse->sLastToken;
121094 n = (int)(sEnd.z - pBegin->z);
121096 z = pBegin->z;
121097 while( sqlite3Isspace(z[n-1]) ){ n--; }
121098 sEnd.z = &z[n-1];
121118 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
121124 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
121136 db->nSchemaLock++;
121138 db->nSchemaLock--;
121148 assert( pTable->nCol<=0 );
121165 if( pTable->nCol<0 ){
121166 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
121169 assert( pTable->nCol>=0 );
121179 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
121181 u8 eParseMode = pParse->eParseMode;
121182 int nTab = pParse->nTab;
121183 int nSelect = pParse->nSelect;
121184 pParse->eParseMode = PARSE_MODE_NORMAL;
121185 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
121186 pTable->nCol = -1;
121189 xAuth = db->xAuth;
121190 db->xAuth = 0;
121192 db->xAuth = xAuth;
121196 pParse->nTab = nTab;
121197 pParse->nSelect = nSelect;
121199 pTable->nCol = 0;
121201 }else if( pTable->pCheck ){
121204 ** arglist which is stored in pTable->pCheck. The pCheck field
121208 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
121209 &pTable->nCol, &pTable->aCol);
121210 if( pParse->nErr==0
121211 && pTable->nCol==pSel->pEList->nExpr
121213 assert( db->mallocFailed==0 );
121221 assert( pTable->aCol==0 );
121222 pTable->nCol = pSelTab->nCol;
121223 pTable->aCol = pSelTab->aCol;
121224 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
121225 pSelTab->nCol = 0;
121226 pSelTab->aCol = 0;
121227 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
121229 pTable->nNVCol = pTable->nCol;
121233 pParse->eParseMode = eParseMode;
121237 pTable->pSchema->schemaFlags |= DB_UnresetViews;
121238 if( db->mallocFailed ){
121246 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
121259 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
121274 ** root-page of a table or index in database iDb has changed from iFrom
121295 pDb = &db->aDb[iDb];
121296 pHash = &pDb->pSchema->tblHash;
121299 if( pTab->tnum==iFrom ){
121300 pTab->tnum = iTo;
121303 pHash = &pDb->pSchema->idxHash;
121306 if( pIdx->tnum==iFrom ){
121307 pIdx->tnum = iTo;
121314 ** Write code to erase the table with root-page iTable from database iDb.
121316 ** if a root-page of another table is moved by the btree-layer whilst
121317 ** erasing iTable (this can happen with an auto-vacuum database).
121327 ** is non-zero, then it is the root page number of a table moved to
121338 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
121346 ** in case a root-page belonging to another table is moved by the btree layer
121347 ** is also added (this can happen with an auto-vacuum database).
121350 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
121352 ** table and index root-pages in order, starting with the numerically
121353 ** largest root-page number. This guarantees that none of the root-pages
121361 ** and root page 5 happened to be the largest root-page number in the
121364 ** a free-list page.
121366 Pgno iTab = pTab->tnum;
121376 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121377 Pgno iIdx = pIdx->tnum;
121378 assert( pIdx->pSchema==pTab->pSchema );
121386 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
121387 assert( iDb>=0 && iDb<pParse->db->nDb );
121405 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
121409 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
121423 sqlite3 *db = pParse->db;
121425 Db *pDb = &db->aDb[iDb];
121443 assert( pTrigger->pSchema==pTab->pSchema ||
121444 pTrigger->pSchema==db->aDb[1].pSchema );
121446 pTrigger = pTrigger->pNext;
121453 ** move as a result of the drop (can happen in auto-vacuum mode).
121455 if( pTab->tabFlags & TF_Autoincrement ){
121458 pDb->zDbSName, pTab->zName
121473 pDb->zDbSName, pTab->zName);
121482 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
121485 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
121491 ** Return TRUE if shadow tables should be read-only in the current
121496 if( (db->flags & SQLITE_Defensive)!=0
121497 && db->pVtabCtx==0
121498 && db->nVdbeExec==0
121511 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
121512 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
121513 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
121516 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
121519 if( pTab->tabFlags & TF_Eponymous ){
121532 sqlite3 *db = pParse->db;
121535 if( db->mallocFailed ){
121538 assert( pParse->nErr==0 );
121539 assert( pName->nSrc==1 );
121541 if( noErr ) db->suppressErr++;
121543 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
121544 if( noErr ) db->suppressErr--;
121548 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
121553 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121554 assert( iDb>=0 && iDb<db->nDb );
121566 const char *zDb = db->aDb[iDb].zDbSName;
121580 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
121589 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
121592 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
121598 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
121607 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
121611 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
121623 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
121644 ** under construction in the pParse->pNewTable field.
121656 sqlite3 *db = pParse->db;
121660 Table *p = pParse->pNewTable;
121669 int iCol = p->nCol-1;
121671 if( pToCol && pToCol->nExpr!=1 ){
121674 p->aCol[iCol].zCnName, pTo);
121678 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
121684 nCol = pFromCol->nExpr;
121686 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
121688 for(i=0; i<pToCol->nExpr; i++){
121689 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
121696 pFKey->pFrom = p;
121698 pFKey->pNextFrom = p->u.tab.pFKey;
121699 z = (char*)&pFKey->aCol[nCol];
121700 pFKey->zTo = z;
121704 memcpy(z, pTo->z, pTo->n);
121705 z[pTo->n] = 0;
121707 z += pTo->n+1;
121708 pFKey->nCol = nCol;
121710 pFKey->aCol[0].iFrom = p->nCol-1;
121714 for(j=0; j<p->nCol; j++){
121715 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
121716 pFKey->aCol[i].iFrom = j;
121720 if( j>=p->nCol ){
121723 pFromCol->a[i].zEName);
121727 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
121733 int n = sqlite3Strlen30(pToCol->a[i].zEName);
121734 pFKey->aCol[i].zCol = z;
121736 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
121738 memcpy(z, pToCol->a[i].zEName, n);
121743 pFKey->isDeferred = 0;
121744 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
121745 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
121747 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
121748 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
121749 pFKey->zTo, (void *)pFKey
121756 assert( pNextTo->pPrevTo==0 );
121757 pFKey->pNextTo = pNextTo;
121758 pNextTo->pPrevTo = pFKey;
121764 p->u.tab.pFKey = pFKey;
121785 if( (pTab = pParse->pNewTable)==0 ) return;
121787 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
121788 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
121789 pFKey->isDeferred = (u8)isDeferred;
121802 ** the root page number of the index is taken from pIndex->tnum.
121805 Table *pTab = pIndex->pTable; /* The table that is indexed */
121806 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
121807 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
121816 sqlite3 *db = pParse->db; /* The database connection */
121817 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
121820 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
121821 db->aDb[iDb].zDbSName ) ){
121826 /* Require a write-lock on the table to perform this operation */
121827 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121834 tnum = pIndex->tnum;
121837 assert( pKey!=0 || pParse->nErr );
121840 iSorter = pParse->nTab++;
121841 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
121867 pIndex->nKeyCol); VdbeCoverage(v);
121882 if( !pIndex->bAscKeyBug ){
121907 ** of 8-byte aligned space after the Index object and return a
121927 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
121928 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
121929 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
121930 p->aSortOrder = (u8*)pExtra;
121931 p->nColumn = nCol;
121932 p->nKeyCol = nCol - 1;
121941 ** pParse and return non-zero. Otherwise, return zero.
121946 for(i=0; i<pList->nExpr; i++){
121947 if( pList->a[i].fg.bNulls ){
121948 u8 sf = pList->a[i].fg.sortFlags;
121963 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
121964 ** as the table to be indexed. pParse->pNewTable is a table that is
121968 ** is a primary key or unique-constraint on the most recent column added
121975 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
121991 sqlite3 *db = pParse->db;
122001 assert( db->pParse==pParse );
122002 if( pParse->nErr ){
122005 assert( db->mallocFailed==0 );
122021 /* Use the two-part index name to determine the database
122028 assert( pName && pName->z );
122035 if( !db->init.busy ){
122037 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
122049 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
122050 assert( db->mallocFailed==0 || pTab==0 );
122052 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
122054 "cannot create a TEMP index on non-TEMP table \"%s\"",
122055 pTab->zName);
122062 pTab = pParse->pNewTable;
122064 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122066 pDb = &db->aDb[iDb];
122069 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
122070 && db->init.busy==0
122073 && sqlite3UserAuthTable(pTab->zName)==0
122076 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
122108 assert( pName->z!=0 );
122109 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
122113 if( !db->init.busy ){
122114 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
122119 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
122123 assert( !db->init.busy );
122133 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
122134 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
122151 const char *zDb = pDb->zDbSName;
122157 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
122169 Column *pCol = &pTab->aCol[pTab->nCol-1];
122170 pCol->colFlags |= COLFLAG_UNIQUE;
122171 sqlite3TokenInit(&prevCol, pCol->zCnName);
122175 assert( pList->nExpr==1 );
122179 if( pParse->nErr ) goto exit_create_index;
122185 for(i=0; i<pList->nExpr; i++){
122186 Expr *pExpr = pList->a[i].pExpr;
122188 if( pExpr->op==TK_COLLATE ){
122190 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
122198 nExtraCol = pPk ? pPk->nKeyCol : 1;
122199 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
122200 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
122202 if( db->mallocFailed ){
122205 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
122206 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
122207 pIndex->zName = zExtra;
122209 memcpy(pIndex->zName, zName, nName+1);
122210 pIndex->pTable = pTab;
122211 pIndex->onError = (u8)onError;
122212 pIndex->uniqNotNull = onError!=OE_None;
122213 pIndex->idxType = idxType;
122214 pIndex->pSchema = db->aDb[iDb].pSchema;
122215 pIndex->nKeyCol = pList->nExpr;
122218 pIndex->pPartIdxWhere = pPIWhere;
122225 if( pDb->pSchema->file_format>=4 ){
122226 sortOrderMask = -1; /* Honor DESC */
122234 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
122240 pListItem = pList->a;
122242 pIndex->aColExpr = pList;
122245 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
122246 Expr *pCExpr; /* The i-th index expression */
122247 int requestedSortOrder; /* ASC or DESC on the i-th expression */
122250 sqlite3StringToId(pListItem->pExpr);
122251 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
122252 if( pParse->nErr ) goto exit_create_index;
122253 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
122254 if( pCExpr->op!=TK_COLUMN ){
122255 if( pTab==pParse->pNewTable ){
122260 if( pIndex->aColExpr==0 ){
122261 pIndex->aColExpr = pList;
122265 pIndex->aiColumn[i] = XN_EXPR;
122266 pIndex->uniqNotNull = 0;
122267 pIndex->bHasExpr = 1;
122269 j = pCExpr->iColumn;
122272 j = pTab->iPKey;
122274 if( pTab->aCol[j].notNull==0 ){
122275 pIndex->uniqNotNull = 0;
122277 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
122278 pIndex->bHasVCol = 1;
122279 pIndex->bHasExpr = 1;
122282 pIndex->aiColumn[i] = (i16)j;
122285 if( pListItem->pExpr->op==TK_COLLATE ){
122287 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
122288 zColl = pListItem->pExpr->u.zToken;
122294 nExtra -= nColl;
122296 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
122299 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
122302 pIndex->azColl[i] = zColl;
122303 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
122304 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
122312 for(j=0; j<pPk->nKeyCol; j++){
122313 int x = pPk->aiColumn[j];
122315 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
122316 pIndex->nColumn--;
122318 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
122319 pIndex->aiColumn[i] = x;
122320 pIndex->azColl[i] = pPk->azColl[j];
122321 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
122325 assert( i==pIndex->nColumn );
122327 pIndex->aiColumn[i] = XN_ROWID;
122328 pIndex->azColl[i] = sqlite3StrBINARY;
122331 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
122336 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
122338 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
122339 pIndex->isCovering = 1;
122340 for(j=0; j<pTab->nCol; j++){
122341 if( j==pTab->iPKey ) continue;
122343 pIndex->isCovering = 0;
122348 if( pTab==pParse->pNewTable ){
122371 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122374 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
122377 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
122378 for(k=0; k<pIdx->nKeyCol; k++){
122381 assert( pIdx->aiColumn[k]>=0 );
122382 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
122383 z1 = pIdx->azColl[k];
122384 z2 = pIndex->azColl[k];
122387 if( k==pIdx->nKeyCol ){
122388 if( pIdx->onError!=pIndex->onError ){
122396 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
122400 if( pIdx->onError==OE_Default ){
122401 pIdx->onError = pIndex->onError;
122404 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
122406 pIndex->pNext = pParse->pNewIndex;
122407 pParse->pNewIndex = pIndex;
122418 ** in-memory database structures.
122420 assert( pParse->nErr==0 );
122421 if( db->init.busy ){
122424 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
122426 pIndex->tnum = db->init.newTnum;
122429 pParse->rc = SQLITE_CORRUPT_BKPT;
122433 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
122434 pIndex->zName, pIndex);
122440 db->mDbFlags |= DBFLAG_SchemaChange;
122459 int iMem = ++pParse->nMem;
122472 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
122480 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
122481 if( pName->z[n-1]==';' ) n--;
122484 onError==OE_None ? "" : " UNIQUE", n, pName->z);
122495 db->aDb[iDb].zDbSName,
122496 pIndex->zName,
122497 pTab->zName,
122504 ** to invalidate all pre-compiled statements.
122510 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
122514 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
122517 if( db->init.busy || pTblName==0 ){
122518 pIndex->pNext = pTab->pIndex;
122519 pTab->pIndex = pIndex;
122523 assert( pParse->pNewIndex==0 );
122524 pParse->pNewIndex = pIndex;
122538 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
122540 if( pThis->onError!=OE_Replace ) continue;
122541 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
122543 pThis->pNext = pNext->pNext;
122544 pNext->pNext = pThis;
122545 ppFrom = &pNext->pNext;
122553 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
122554 assert( pThis->onError!=OE_Replace
122555 || pThis->pNext==0
122556 || pThis->pNext->onError==OE_Replace );
122567 ** Fill the Index.aiRowEst[] array with default information - information
122577 ** aiRowEst[N]<=aiRowEst[N-1]
122587 LogEst *a = pIdx->aiRowLogEst;
122589 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
122593 assert( !pIdx->hasStat1 );
122599 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
122605 x = pIdx->pTable->nRowLogEst;
122608 pIdx->pTable->nRowLogEst = x = 99;
122610 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
122616 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
122621 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
122631 sqlite3 *db = pParse->db;
122634 if( db->mallocFailed ){
122637 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
122638 assert( pName->nSrc==1 );
122642 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
122645 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
122647 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
122650 pParse->checkSchema = 1;
122653 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
122658 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
122662 Table *pTab = pIndex->pTable;
122663 const char *zDb = db->aDb[iDb].zDbSName;
122669 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
122681 db->aDb[iDb].zDbSName, pIndex->zName
122683 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
122685 destroyRootPage(pParse, pIndex->tnum, iDb);
122686 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
122699 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
122707 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
122719 if( (n & (n-1))==0 ){
122723 *pIdx = -1;
122741 sqlite3 *db = pParse->db;
122749 sizeof(IdList) + pList->nId*sizeof(pList->a));
122756 i = pList->nId++;
122757 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
122758 if( IN_RENAME_OBJECT && pList->a[i].zName ){
122759 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
122771 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
122772 for(i=0; i<pList->nId; i++){
122773 sqlite3DbFree(db, pList->a[i].zName);
122779 ** Return the index in pList of the identifier named zId. Return -1
122785 for(i=0; i<pList->nId; i++){
122786 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
122788 return -1;
122797 ** appropriate for small and memory-limited applications.
122826 int nExtra, /* Number of new slots to add to pSrc->a[] */
122827 int iStart /* Index in pSrc->a[] of first new slot */
122835 assert( iStart<=pSrc->nSrc );
122838 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
122840 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
122841 sqlite3 *db = pParse->db;
122843 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
122850 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
122852 assert( db->mallocFailed );
122856 pSrc->nAlloc = nAlloc;
122861 for(i=pSrc->nSrc-1; i>=iStart; i--){
122862 pSrc->a[i+nExtra] = pSrc->a[i];
122864 pSrc->nSrc += nExtra;
122867 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
122869 pSrc->a[i].iCursor = -1;
122922 assert( pParse->db!=0 );
122923 db = pParse->db;
122925 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
122927 pList->nAlloc = 1;
122928 pList->nSrc = 1;
122929 memset(&pList->a[0], 0, sizeof(pList->a[0]));
122930 pList->a[0].iCursor = -1;
122932 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
122940 pItem = &pList->a[pList->nSrc-1];
122941 if( pDatabase && pDatabase->z==0 ){
122945 pItem->zName = sqlite3NameFromToken(db, pDatabase);
122946 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
122948 pItem->zName = sqlite3NameFromToken(db, pTable);
122949 pItem->zDatabase = 0;
122960 assert( pList || pParse->db->mallocFailed );
122962 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
122963 if( pItem->iCursor>=0 ) continue;
122964 pItem->iCursor = pParse->nTab++;
122965 if( pItem->pSelect ){
122966 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
122980 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
122981 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
122982 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
122983 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
122984 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
122985 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
122986 sqlite3DeleteTable(db, pItem->pTab);
122987 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
122988 if( pItem->fg.isUsing ){
122989 sqlite3IdListDelete(db, pItem->u3.pUsing);
122990 }else if( pItem->u3.pOn ){
122991 sqlite3ExprDelete(db, pItem->u3.pOn);
123003 ** pDatabase is NULL if the database name qualifier is missing - the
123018 Token *pAlias, /* The right-hand side of the AS subexpression */
123023 sqlite3 *db = pParse->db;
123024 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
123026 (pOnUsing->pOn ? "ON" : "USING")
123034 assert( p->nSrc>0 );
123035 pItem = &p->a[p->nSrc-1];
123037 assert( pItem->zName==0 || pDatabase!=0 );
123038 if( IN_RENAME_OBJECT && pItem->zName ){
123039 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
123040 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
123043 if( pAlias->n ){
123044 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
123047 pItem->pSelect = pSubquery;
123048 if( pSubquery->selFlags & SF_NestedFrom ){
123049 pItem->fg.isNestedFrom = 1;
123052 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
123053 assert( pItem->fg.isUsing==0 );
123055 pItem->u3.pOn = 0;
123056 }else if( pOnUsing->pUsing ){
123057 pItem->fg.isUsing = 1;
123058 pItem->u3.pUsing = pOnUsing->pUsing;
123060 pItem->u3.pOn = pOnUsing->pOn;
123073 ** element of the source-list passed as the second argument.
123077 if( p && pIndexedBy->n>0 ){
123079 assert( p->nSrc>0 );
123080 pItem = &p->a[p->nSrc-1];
123081 assert( pItem->fg.notIndexed==0 );
123082 assert( pItem->fg.isIndexedBy==0 );
123083 assert( pItem->fg.isTabFunc==0 );
123084 if( pIndexedBy->n==1 && !pIndexedBy->z ){
123087 pItem->fg.notIndexed = 1;
123089 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
123090 pItem->fg.isIndexedBy = 1;
123091 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
123102 assert( p1 && p1->nSrc==1 );
123104 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
123106 sqlite3SrcListDelete(pParse->db, p2);
123109 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
123110 sqlite3DbFree(pParse->db, p2);
123111 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
123119 ** table-valued-function.
123123 SrcItem *pItem = &p->a[p->nSrc-1];
123124 assert( pItem->fg.notIndexed==0 );
123125 assert( pItem->fg.isIndexedBy==0 );
123126 assert( pItem->fg.isTabFunc==0 );
123127 pItem->u1.pFuncArg = pList;
123128 pItem->fg.isTabFunc = 1;
123130 sqlite3ExprListDelete(pParse->db, pList);
123146 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
123151 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
123158 if( p && p->nSrc>1 ){
123159 int i = p->nSrc-1;
123162 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
123163 }while( (--i)>0 );
123164 p->a[0].fg.jointype = 0;
123169 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
123170 i--;
123173 p->a[i].fg.jointype |= JT_LTORJ;
123174 }while( (--i)>=0 );
123188 db = pParse->db;
123196 for(i=0; i<db->nDb; i++){
123198 Btree *pBt = db->aDb[i].pBt;
123223 assert( pParse->db!=0 );
123241 char *zName = sqlite3NameFromToken(pParse->db, pName);
123249 sqlite3DbFree(pParse->db, zName);
123261 sqlite3 *db = pParse->db;
123262 if( db->aDb[1].pBt==0 && !pParse->explain ){
123272 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
123276 pParse->rc = rc;
123279 db->aDb[1].pBt = pBt;
123280 assert( db->aDb[1].pSchema );
123281 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
123292 ** will occur at the end of the top-level VDBE and will be generated
123296 assert( iDb>=0 && iDb<pToplevel->db->nDb );
123297 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
123299 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
123300 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
123301 DbMaskSet(pToplevel->cookieMask, iDb);
123317 sqlite3 *db = pParse->db;
123319 for(i=0; i<db->nDb; i++){
123320 Db *pDb = &db->aDb[i];
123321 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
123343 DbMaskSet(pToplevel->writeMask, iDb);
123344 pToplevel->isMultiWrite |= setStatement;
123356 pToplevel->isMultiWrite = 1;
123377 pToplevel->mayAbort = 1;
123394 assert( pParse->pVdbe!=0 );
123396 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
123415 Table *pTab = pIdx->pTable;
123417 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
123418 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
123419 if( pIdx->aColExpr ){
123420 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
123422 for(j=0; j<pIdx->nKeyCol; j++){
123424 assert( pIdx->aiColumn[j]>=0 );
123425 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
123427 sqlite3_str_appendall(&errMsg, pTab->zName);
123441 ** Code an OP_Halt due to non-unique rowid.
123446 Table *pTab /* The table with the non-unique rowid */
123450 if( pTab->iPKey>=0 ){
123451 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
123452 pTab->aCol[pTab->iPKey].zCnName);
123455 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
123470 for(i=0; i<pIndex->nColumn; i++){
123471 const char *z = pIndex->azColl[i];
123472 assert( z!=0 || pIndex->aiColumn[i]<0 );
123473 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
123490 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
123492 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
123494 sqlite3RefillIndex(pParse, pIndex, -1);
123510 sqlite3 *db = pParse->db; /* The database connection */
123515 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
123517 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
123528 ** REINDEX -- 1
123529 ** REINDEX <collation> -- 2
123530 ** REINDEX ?<database>.?<tablename> -- 3
123531 ** REINDEX ?<database>.?<indexname> -- 4
123546 sqlite3 *db = pParse->db; /* The database connection */
123558 }else if( NEVER(pName2==0) || pName2->z==0 ){
123560 assert( pName1->z );
123561 zColl = sqlite3NameFromToken(pParse->db, pName1);
123575 zDb = db->aDb[iDb].zDbSName;
123586 sqlite3RefillIndex(pParse, pIndex, -1);
123601 int nCol = pIdx->nColumn;
123602 int nKey = pIdx->nKeyCol;
123604 if( pParse->nErr ) return 0;
123605 if( pIdx->uniqNotNull ){
123606 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
123608 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
123613 const char *zColl = pIdx->azColl[i];
123614 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
123616 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
123617 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
123619 if( pParse->nErr ){
123620 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
123621 if( pIdx->bNoQuery==0 ){
123626 ** the missing index using the collation-needed callback. For
123629 pIdx->bNoQuery = 1;
123630 pParse->rc = SQLITE_ERROR_RETRY;
123645 Token *pName, /* Name of the common-table */
123651 sqlite3 *db = pParse->db;
123654 assert( pNew!=0 || db->mallocFailed );
123656 if( db->mallocFailed ){
123660 pNew->pSelect = pQuery;
123661 pNew->pCols = pArglist;
123662 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
123663 pNew->eM10d = eM10d;
123674 sqlite3ExprListDelete(db, pCte->pCols);
123675 sqlite3SelectDelete(db, pCte->pSelect);
123676 sqlite3DbFree(db, pCte->zName);
123699 sqlite3 *db = pParse->db;
123709 zName = pCte->zName;
123712 for(i=0; i<pWith->nCte; i++){
123713 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
123720 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
123725 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
123727 if( db->mallocFailed ){
123731 pNew->a[pNew->nCte++] = *pCte;
123744 for(i=0; i<pWith->nCte; i++){
123745 cteClear(db, &pWith->a[i]);
123777 assert( !db->xCollNeeded || !db->xCollNeeded16 );
123778 if( db->xCollNeeded ){
123781 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
123785 if( db->xCollNeeded16 ){
123788 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
123791 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
123802 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
123807 char *z = pColl->zName;
123812 if( pColl2->xCmp!=0 ){
123814 pColl->xDel = 0; /* Do not copy the destructor */
123833 if( pColl && pColl->xCmp==0 ){
123834 const char *zName = pColl->zName;
123835 sqlite3 *db = pParse->db;
123854 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
123866 pColl = sqlite3HashFind(&db->aCollSeq, zName);
123880 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
123898 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
123923 if( pColl ) pColl += enc-1;
123925 pColl = db->pDfltColl;
123936 db->enc = enc;
123937 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
123940 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
123964 sqlite3 *db = pParse->db;
123970 if( !p || !p->xCmp ){
123977 if( p && !p->xCmp && synthCollSeq(db, p) ){
123980 assert( !p || p->xCmp );
123983 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
124009 sqlite3 *db = pParse->db;
124011 u8 initbusy = db->init.busy;
124015 if( !initbusy && (!pColl || !pColl->xCmp) ){
124028 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
124029 ** is also -1. In other words, we are searching for a function that
124032 ** If nArg is -2 that means that we are searching for any function
124042 ** 4: UTF8/16 conversion required - argument count matches exactly
124043 ** 5: UTF16 byte order conversion required - argument count matches exactly
124046 ** If nArg==(-2) then any function with a non-null xSFunc is
124048 ** a non-match.
124053 int nArg, /* Desired number of arguments. (-1)==any */
124057 assert( p->nArg>=-1 );
124060 if( p->nArg!=nArg ){
124061 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
124062 if( p->nArg>=0 ) return 0;
124067 if( p->nArg==nArg ){
124074 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
124076 }else if( (enc & p->funcFlags & 2)!=0 ){
124092 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
124093 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
124094 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
124117 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
124118 aDef[i].pNext = pOther->pNext;
124119 pOther->pNext = &aDef[i];
124132 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
124140 ** If nArg is -2, then the first valid function found is returned. A
124141 ** function is valid if xSFunc is non-zero. The nArg==(-2)
124143 ** of arguments. If nArg is -2, then createFlag must be 0.
124151 const char *zName, /* Name of the function. zero-terminated */
124152 int nArg, /* Number of arguments. -1 means any number */
124162 assert( nArg>=(-2) );
124163 assert( nArg>=(-1) || createFlag==0 );
124166 /* First search for a match amongst the application-defined functions.
124168 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
124175 p = p->pNext;
124178 /* If no match is found, search the built-in functions.
124180 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
124181 ** functions even if a prior app-defined function was found. And give
124182 ** priority to built-in functions.
124187 ** new function. But the FuncDefs for built-in functions are read-only.
124188 ** So we must not search for built-ins when creating a new function.
124190 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
124200 p = p->pNext;
124212 pBest->zName = (const char*)&pBest[1];
124213 pBest->nArg = (u16)nArg;
124214 pBest->funcFlags = enc;
124216 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
124217 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
124223 pBest->pNext = pOther;
124227 if( pBest && (pBest->xSFunc || createFlag) ){
124249 temp1 = pSchema->tblHash;
124250 temp2 = pSchema->trigHash;
124251 sqlite3HashInit(&pSchema->trigHash);
124252 sqlite3HashClear(&pSchema->idxHash);
124257 sqlite3HashInit(&pSchema->tblHash);
124263 sqlite3HashClear(&pSchema->fkeyHash);
124264 pSchema->pSeqTab = 0;
124265 if( pSchema->schemaFlags & DB_SchemaLoaded ){
124266 pSchema->iGeneration++;
124268 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
124284 }else if ( 0==p->file_format ){
124285 sqlite3HashInit(&p->tblHash);
124286 sqlite3HashInit(&p->idxHash);
124287 sqlite3HashInit(&p->trigHash);
124288 sqlite3HashInit(&p->fkeyHash);
124289 p->enc = SQLITE_UTF8;
124322 ** pSrc->a[0].pTab Pointer to the Table object
124323 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
124327 SrcItem *pItem = pSrc->a;
124329 assert( pItem && pSrc->nSrc>=1 );
124331 sqlite3DeleteTable(pParse->db, pItem->pTab);
124332 pItem->pTab = pTab;
124334 pTab->nTabRef++;
124335 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
124342 /* Generate byte-code that will report the number of rows modified
124352 /* Return true if table pTab is read-only.
124354 ** A table is read-only if any of the following are true:
124369 ** is for a top-level SQL statement.
124372 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
124379 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
124382 if( pParse->pToplevel!=0
124383 && pTab->u.vtab.p->eVtabRisk >
124384 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
124387 pTab->zName);
124396 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
124397 db = pParse->db;
124398 if( (pTab->tabFlags & TF_Readonly)!=0 ){
124399 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
124401 assert( pTab->tabFlags & TF_Shadow );
124408 ** If pTab is not writable -> generate an error message and return 1.
124409 ** If pTab is writable but other errors have occurred -> return 1.
124410 ** If pTab is writable and no prior errors -> return 0;
124414 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
124419 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
124444 sqlite3 *db = pParse->db;
124445 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
124449 assert( pFrom->nSrc==1 );
124450 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
124451 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
124452 assert( pFrom->a[0].fg.isUsing==0 );
124453 assert( pFrom->a[0].u3.pOn==0 );
124474 SrcList *pSrc, /* the FROM clause -- which tables to scan */
124480 sqlite3 *db = pParse->db;
124492 sqlite3ExprDelete(pParse->db, pWhere);
124493 sqlite3ExprListDelete(pParse->db, pOrderBy);
124513 pTab = pSrc->a[0].pTab;
124521 if( pPk->nKeyCol==1 ){
124522 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
124527 for(i=0; i<pPk->nKeyCol; i++){
124528 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
124533 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
124540 pSrc->a[0].pTab = 0;
124542 pSrc->a[0].pTab = pTab;
124543 if( pSrc->a[0].fg.isIndexedBy ){
124544 assert( pSrc->a[0].fg.isCte==0 );
124545 pSrc->a[0].u2.pIBIndex = 0;
124546 pSrc->a[0].fg.isIndexedBy = 0;
124547 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
124548 }else if( pSrc->a[0].fg.isCte ){
124549 pSrc->a[0].u2.pCteUse->nUse++;
124616 db = pParse->db;
124617 assert( db->pParse==pParse );
124618 if( pParse->nErr ){
124621 assert( db->mallocFailed==0 );
124622 assert( pTabList->nSrc==1 );
124651 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
124675 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124676 assert( iDb<db->nDb );
124677 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
124678 db->aDb[iDb].zDbSName);
124687 assert( pTabList->nSrc==1 );
124688 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
124689 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
124690 pParse->nTab++;
124696 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
124705 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
124734 if( (db->flags & SQLITE_CountRows)!=0
124735 && !pParse->nested
124736 && !pParse->pTriggerTab
124737 && !pParse->bReturning
124739 memCnt = ++pParse->nMem;
124750 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
124760 && db->xPreUpdateCallback==0
124764 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
124766 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
124767 pTab->zName, P4_STATIC);
124769 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124770 assert( pIdx->pSchema==pTab->pSchema );
124772 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
124774 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
124787 iRowSet = ++pParse->nMem;
124794 nPk = pPk->nKeyCol;
124795 iPk = pParse->nMem+1;
124796 pParse->nMem += nPk;
124797 iEphCur = pParse->nTab++;
124806 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
124807 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
124808 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
124828 assert( pPk->aiColumn[i]>=0 );
124830 pPk->aiColumn[i], iPk+i);
124834 iKey = ++pParse->nMem;
124835 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
124839 /* For ONEPASS, no need to store the rowid/primary-key. There is only
124850 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
124851 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
124857 iKey = ++pParse->nMem;
124860 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
124890 /* Set up a loop over the rowids/primary-keys that were found in the
124891 ** where-clause loop above.
124895 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
124924 pParse->isMultiWrite = 0;
124932 int count = (pParse->nested==0); /* True to count changes */
124937 /* End of the loop over all rowids/primary-keys. */
124948 } /* End non-truncate path */
124954 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
125000 ** cursor number iIdxCur+i for the i-th index.
125036 u8 count, /* If non-zero, increment the row change counter */
125041 Vdbe *v = pParse->pVdbe; /* Vdbe */
125075 iOld = pParse->nMem+1;
125076 pParse->nMem += (1 + pTab->nCol);
125078 /* Populate the OLD.* pseudo-table register array. These values will be
125081 for(iCol=0; iCol<pTab->nCol; iCol++){
125109 iIdxNoSeek = -1;
125122 ** If variable 'count' is non-zero, then this OP_Delete instruction should
125123 ** invoke the update-hook. The pre-update-hook, on the other hand should
125125 ** the update-hook is not invoked for rows removed by REPLACE, but the
125126 ** pre-update-hook is.
125132 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
125174 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
125175 ** index is the 0-th index.)
125189 int r1 = -1; /* Register holding an index key */
125196 v = pParse->pVdbe;
125198 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
125203 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
125207 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
125255 Vdbe *v = pParse->pVdbe;
125261 if( pIdx->pPartIdxWhere ){
125263 pParse->iSelfTab = iDataCur + 1;
125264 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
125266 pParse->iSelfTab = 0;
125267 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
125273 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
125275 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
125278 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
125279 && pPrior->aiColumn[j]!=XN_EXPR
125285 if( pIdx->aiColumn[j]>=0 ){
125303 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
125309 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
125326 ** This file contains the C-language implementations for many of the SQL
125343 assert( context->pVdbe!=0 );
125344 pOp = &context->pVdbe->aOp[context->iOp-1];
125345 assert( pOp->opcode==OP_CollSeq );
125346 assert( pOp->p4type==P4_COLLSEQ );
125347 return pOp->p4.pColl;
125355 assert( context->isError<=0 );
125356 context->isError = -1;
125357 context->skipFlag = 1;
125361 ** Implementation of the non-aggregate min() and max() functions
125374 mask = sqlite3_user_data(context)==0 ? 0 : -1;
125377 assert( mask==-1 || mask==0 );
125399 int i = sqlite3_value_type(argv[0]) - 1;
125407 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
125411 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
125456 sqlite3_result_int(context, (int)(z-z0));
125469 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
125480 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
125482 ** equivalent positive 64-bit two complement value. */
125483 sqlite3_result_error(context, "integer overflow", -1);
125486 iVal = -iVal;
125492 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
125499 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
125503 if( rVal<0 ) rVal = -rVal;
125570 nHaystack--;
125601 x.nArg = argc-1;
125604 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
125617 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
125618 ** of x. If x is text, then we actually count UTF-8 characters.
125662 ** as substr(X,1,N) - it returns the first N characters of X. This
125663 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
125664 ** from 2009-02-02 for compatibility of applications that exploited the
125671 p2 = -p2;
125675 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
125685 p1--;
125687 p2--;
125690 p1 -= p2;
125700 p1--;
125702 for(z2=z; *z2 && p2; p2--){
125705 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
125709 p2 = len-p1;
125733 /* If Y==0 and X will fit in a 64-bit int,
125737 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
125740 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
125765 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
125766 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
125767 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
125829 #define noopFunc versionFunc /* Substitute function - never called */
125844 ** (or -9223372036854775808) since when you do abs() of that
125849 ** therefore be no less than -9223372036854775807.
125851 r = -(r & LARGEST_INT64);
125891 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
125900 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
125925 /* IMP: R-11217-42568 This function is a wrapper around the
125931 ** A structure defining how to do GLOB-style comparisons.
125954 /* The correct SQL-92 behavior is for the LIKE operator to ignore
125969 ** Compare two UTF-8 strings for equality where the first string is
125989 ** range of characters can be specified using '-'. Example:
125990 ** "[a-z]" matches any single lower-case letter. To match a '-', make
126013 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
126014 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
126015 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
126032 if( pInfo->matchSet==0 ){
126038 assert( matchOther<0x80 ); /* '[' is a single-byte character */
126040 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
126053 ** For a case-insensitive search, set variable cx to be the same as
126086 if( pInfo->matchSet==0 ){
126106 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
126137 ** non-zero if there is no match.
126150 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
126151 ** a miss - like strcmp().
126175 ** the build-in LIKE operator. The first argument to the function is the
126213 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
126214 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
126215 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
126216 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
126220 /* The escape character string must consist of a single UTF-8 character.
126225 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
126227 "ESCAPE expression must be a single character", -1);
126231 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
126234 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
126235 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
126238 escape = pInfo->matchSet;
126278 /* IMP: R-48699-48617 This function is an SQL wrapper around the
126279 ** sqlite3_libversion() C-interface. */
126280 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
126294 /* IMP: R-24470-31136 This function is an SQL wrapper around the
126296 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
126302 ** its side-effects.
126328 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
126352 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
126356 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
126360 /* Array for converting from half-bytes (nybbles) into ASCII hex
126375 assert( pStr!=0 && pStr->nChar==0 );
126385 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
126402 if( pStr->accError==0 ){
126403 char *zText = pStr->zText;
126413 pStr->nChar = nBlob*2 + 3;
126435 ** Strings are surrounded by single-quotes with escapes on interior quotes
126445 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
126456 ** The unicode() function. Return the integer unicode code-point value
126508 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
126541 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
126554 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
126593 || sqlite3_context_db_handle(context)->mallocFailed );
126613 loopLimit = nStr - nPattern;
126620 nOut += nRep - nPattern;
126621 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
126622 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
126623 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
126629 if( (cntExpand&(cntExpand-1))==0 ){
126634 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
126644 i += nPattern-1;
126647 assert( j+nStr-i+1<=nOut );
126648 memcpy(&zOut[j], &zStr[i], nStr-i);
126649 j += nStr - i;
126704 aLen[nChar] = (unsigned)(z - azChar[nChar]);
126719 nIn -= len;
126727 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
126730 nIn -= len;
126745 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
126746 ** When the "sqlite3" command-line shell is built using this functionality,
126748 ** involving application-defined functions to be examined in a generic
126756 /* no-op */
126761 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
126762 ** is only available if the SQLITE_SOUNDEX compile-time option is used
126769 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
126814 /* IMP: R-64894-50321 The string "?000" is returned if the argument
126823 ** A function that loads a shared-library extension then returns NULL.
126834 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
126835 sqlite3_result_error(context, "not authorized", -1);
126845 sqlite3_result_error(context, zErrMsg, -1);
126862 u8 approx; /* True if non-integer value was input to the sum */
126883 p->cnt++;
126886 p->rSum += v;
126887 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
126888 p->approx = p->overflow = 1;
126891 p->rSum += sqlite3_value_double(argv[0]);
126892 p->approx = 1;
126904 /* p is always non-NULL because sumStep() will have been called first
126907 assert( p->cnt>0 );
126908 p->cnt--;
126909 assert( type==SQLITE_INTEGER || p->approx );
126910 if( type==SQLITE_INTEGER && p->approx==0 ){
126912 p->rSum -= v;
126913 p->iSum -= v;
126915 p->rSum -= sqlite3_value_double(argv[0]);
126925 if( p && p->cnt>0 ){
126926 if( p->overflow ){
126927 sqlite3_result_error(context,"integer overflow",-1);
126928 }else if( p->approx ){
126929 sqlite3_result_double(context, p->rSum);
126931 sqlite3_result_int64(context, p->iSum);
126938 if( p && p->cnt>0 ){
126939 sqlite3_result_double(context, p->rSum/(double)p->cnt);
126946 sqlite3_result_double(context, p ? p->rSum : (double)0);
126968 p->n++;
126975 ** expressed as a 32-bit integer. */
126976 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
126977 || p->n==sqlite3_aggregate_count(context) );
126983 sqlite3_result_int64(context, p ? p->n : 0);
126989 /* p is always non-NULL since countStep() will have been called first */
126991 p->n--;
126993 p->bInverse = 1;
127017 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
127018 }else if( pBest->flags ){
127025 ** sqlite3_user_data() function returns (void *)-1. For min() it
127038 pBest->db = sqlite3_context_db_handle(context);
127046 if( pRes->flags ){
127077 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
127079 ** (Hence, its slots in use number nAccum-1 between method calls.)
127100 int firstTerm = pGCC->str.mxAlloc==0;
127101 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
127104 sqlite3_str_appendchar(&pGCC->str, 1, ',');
127108 pGCC->nFirstSepLength = 1;
127115 sqlite3_str_append(&pGCC->str, zSep, nSep);
127121 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
127122 int *pnsl = pGCC->pnSepLengths;
127125 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
127127 int i = 0, nA = pGCC->nAccum-1;
127128 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
127131 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
127134 if( ALWAYS(pGCC->nAccum>0) ){
127135 pnsl[pGCC->nAccum-1] = nSep;
127137 pGCC->pnSepLengths = pnsl;
127139 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
127146 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
127148 pGCC->nAccum += 1;
127152 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
127167 /* pGCC is always non-NULL since groupConcatStep() will have always
127175 pGCC->nAccum -= 1;
127176 if( pGCC->pnSepLengths!=0 ){
127177 assert(pGCC->nAccum >= 0);
127178 if( pGCC->nAccum>0 ){
127179 nVS += *pGCC->pnSepLengths;
127180 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
127181 (pGCC->nAccum-1)*sizeof(int));
127184 /* If removing single accumulated string, harmlessly over-do. */
127185 nVS += pGCC->nFirstSepLength;
127187 if( nVS>=(int)pGCC->str.nChar ){
127188 pGCC->str.nChar = 0;
127190 pGCC->str.nChar -= nVS;
127191 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
127193 if( pGCC->str.nChar==0 ){
127194 pGCC->str.mxAlloc = 0;
127195 sqlite3_free(pGCC->pnSepLengths);
127196 pGCC->pnSepLengths = 0;
127207 sqlite3ResultStrAccum(context, &pGCC->str);
127209 sqlite3_free(pGCC->pnSepLengths);
127218 StrAccum *pAccum = &pGCC->str;
127219 if( pAccum->accError==SQLITE_TOOBIG ){
127221 }else if( pAccum->accError==SQLITE_NOMEM ){
127225 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
127234 ** This routine does per-connection function registration. Most
127235 ** of the built-in functions above are part of the global function set.
127257 ** Re-register the built-in LIKE functions. The caseSensitive
127273 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
127274 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
127282 ** LIKE-style function then return FALSE.
127298 assert( pExpr->op==TK_FUNCTION );
127300 if( !pExpr->x.pList ){
127303 nExpr = pExpr->x.pList->nExpr;
127305 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
127309 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
127317 memcpy(aWc, pDef->pUserData, 3);
127325 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
127327 if( pEscape->op!=TK_STRING ) return 0;
127329 zEscape = pEscape->u.zToken;
127336 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
127352 /* Extra math functions that require linking with -lm
127398 ** ln(X) - natural logarithm
127399 ** log(X) - log X base 10
127400 ** log10(X) - log X base 10
127401 ** log(B,X) - log X base B
127457 ** Implementation of 1-argument SQL math functions:
127459 ** exp(X) - Compute e to the X-th power
127479 ** Implementation of 2-argument SQL math functions:
127481 ** power(X,Y) - Compute X to the Y-th power
127504 ** Implementation of 0-argument pi() function.
127532 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
127537 ** to the global function hash table. This occurs at start-time (as
127548 ** FuncDef.pHash elements at start-time. The elements of this array
127549 ** are read-only after initialization is complete.
127588 FUNCTION(min, -1, 0, 1, minmaxFunc ),
127592 FUNCTION(max, -1, 1, 1, minmaxFunc ),
127600 FUNCTION(printf, -1, 0, 0, printfFunc ),
127601 FUNCTION(format, -1, 0, 0, printfFunc ),
127603 FUNCTION(char, -1, 0, 0, charFunc ),
127651 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
127692 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
127703 #if 0 /* Enable to print out how the built-in functions are hashed */
127708 printf("FUNC-HASH %02d:", i);
127709 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
127710 int n = sqlite3Strlen30(p->zName);
127711 int h = p->zName[0] + n;
127712 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
127713 printf(" %s(%d)", p->zName, h);
127743 ** --------------------------
127825 ** ---------------
127827 ** Before coding an UPDATE or DELETE row operation, the code-generator
127832 ** accessed). No information is required by the code-generator before
127836 ** sqlite3FkRequired() - Test to see if FK processing is required.
127837 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
127841 ** --------------------------------------
127843 ** sqlite3FkCheck() - Check for foreign key violations.
127844 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
127845 ** sqlite3FkDelete() - Delete an FKey structure.
127850 ** -----------------------
127880 ** constraint to the parent table column stored in the left-most column
127882 ** child table column that corresponds to the second left-most column of
127901 ** then non-zero is returned, and a "foreign key mismatch" error loaded
127902 ** into pParse. If an OOM error occurs, non-zero is returned and the
127903 ** pParse->db->mallocFailed flag is set.
127914 int nCol = pFKey->nCol; /* Number of columns in parent key */
127915 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
127922 /* If this is a non-composite (single column) foreign key, check if it
127928 ** Non-composite foreign keys do not require the aiCol array.
127938 if( pParent->iPKey>=0 ){
127940 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
127946 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
127951 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
127952 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
127964 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
127969 /* If zKey is non-NULL, then this foreign key was declared to
127975 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
127984 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
127986 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
127988 zIdxCol = pParent->aCol[iCol].zCnName;
127990 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
127991 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
128003 if( !pParse->disableTriggers ){
128005 "foreign key mismatch - \"%w\" referencing \"%w\"",
128006 pFKey->pFrom->zName, pFKey->zTo);
128008 sqlite3DbFree(pParse->db, aiCol);
128020 ** affected - once to "delete" the old row, and then again to "insert" the
128030 ** --------------------------------------------------------------------------
128055 int iCur = pParse->nTab - 1; /* Cursor number to use */
128059 (!pFKey->isDeferred
128060 && !(pParse->db->flags & SQLITE_DeferFKs)
128061 && !pParse->pToplevel
128062 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
128072 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
128075 for(i=0; i<pFKey->nCol; i++){
128076 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
128093 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
128098 ** to increment the constraint-counter (i.e. this is an INSERT operation),
128100 ** increment the constraint-counter. */
128101 if( pTab==pFKey->pFrom && nIncr==1 ){
128109 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
128113 int nCol = pFKey->nCol;
128116 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
128120 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
128125 ** to increment the constraint-counter (i.e. this is an INSERT operation),
128127 ** increment the constraint-counter.
128129 ** If any of the parent-key values are NULL, then the row cannot match
128131 ** of the parent-key values are NULL (at this point it is known that
128134 if( pTab==pFKey->pFrom && nIncr==1 ){
128137 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
128140 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
128141 pIdx->aiColumn[i]);
128142 assert( pIdx->aiColumn[i]>=0 );
128143 assert( aiCol[i]!=pTab->iPKey );
128144 if( pIdx->aiColumn[i]==pTab->iPKey ){
128155 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
128162 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
128163 && !pParse->pToplevel
128164 && !pParse->isMultiWrite
128168 ** incrementing a counter. This is necessary as the VM code is being
128174 if( nIncr>0 && pFKey->isDeferred==0 ){
128177 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
128202 sqlite3 *db = pParse->db;
128206 if( iCol>=0 && iCol!=pTab->iPKey ){
128207 pCol = &pTab->aCol[iCol];
128208 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
128209 pExpr->affExpr = pCol->affinity;
128211 if( zColl==0 ) zColl = db->pDfltColl->zName;
128214 pExpr->iTable = regBase;
128215 pExpr->affExpr = SQLITE_AFF_INTEGER;
128234 pExpr->y.pTab = pTab;
128235 pExpr->iTable = iCursor;
128236 pExpr->iColumn = iCol;
128245 ** code for an SQL UPDATE operation, this function may be called twice -
128248 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
128257 ** --------------------------------------------------------------------------
128279 sqlite3 *db = pParse->db; /* Database handle */
128287 assert( pIdx==0 || pIdx->pTable==pTab );
128288 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
128289 assert( pIdx!=0 || pFKey->nCol==1 );
128293 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
128299 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
128305 for(i=0; i<pFKey->nCol; i++){
128312 iCol = pIdx ? pIdx->aiColumn[i] : -1;
128314 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
128316 zCol = pFKey->pFrom->aCol[iCol].zCnName;
128336 if( pTab==pFKey->pFrom && nIncr>0 ){
128341 pLeft = exprTableRegister(pParse, pTab, regData, -1);
128342 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
128347 for(i=0; i<pIdx->nKeyCol; i++){
128348 i16 iCol = pIdx->aiColumn[i];
128351 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
128369 if( pParse->nErr==0 ){
128371 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
128399 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
128405 ** and all of its sub-components.
128407 ** The Trigger structure or any of its sub-components may be allocated from
128412 TriggerStep *pStep = p->step_list;
128413 sqlite3ExprDelete(dbMem, pStep->pWhere);
128414 sqlite3ExprListDelete(dbMem, pStep->pExprList);
128415 sqlite3SelectDelete(dbMem, pStep->pSelect);
128416 sqlite3ExprDelete(dbMem, p->pWhen);
128428 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
128433 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
128434 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
128435 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
128458 sqlite3 *db = pParse->db;
128459 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
128472 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128473 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
128480 pParse->disableTriggers = 1;
128482 pParse->disableTriggers = 0;
128493 if( (db->flags & SQLITE_DeferFKs)==0 ){
128514 ** is set to -1). If the rowid column is modified by the UPDATE statement
128515 ** the bChngRowid argument is non-zero.
128527 for(i=0; i<p->nCol; i++){
128528 int iChildKey = p->aCol[i].iFrom;
128530 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
128541 ** is set to -1). If the rowid column is modified by the UPDATE statement
128542 ** the bChngRowid argument is non-zero.
128554 for(i=0; i<p->nCol; i++){
128555 char *zKey = p->aCol[i].zCol;
128557 for(iKey=0; iKey<pTab->nCol; iKey++){
128558 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
128559 Column *pCol = &pTab->aCol[iKey];
128561 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
128562 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
128578 if( pTop->pTriggerPrg ){
128579 Trigger *p = pTop->pTriggerPrg->pTrigger;
128580 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
128581 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
128595 ** first register in an array of (pTab->nCol+1) registers containing the
128601 ** first register of an array of (pTab->nCol+1) registers containing the new
128617 sqlite3 *db = pParse->db; /* Database handle */
128621 int isIgnoreErrors = pParse->disableTriggers;
128623 /* Exactly one of regOld and regNew should be non-zero. */
128626 /* If foreign-keys are disabled, this function is a no-op. */
128627 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
128630 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128631 zDb = db->aDb[iDb].zDbSName;
128635 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
128645 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
128655 if( pParse->disableTriggers ){
128656 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
128658 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
128662 if( !isIgnoreErrors || db->mallocFailed ) return;
128669 ** FK counter for each row of the current table with non-NULL keys.
128672 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
128673 for(i=0; i<pFKey->nCol; i++){
128675 iFromCol = pFKey->aCol[i].iFrom;
128676 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
128679 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
128683 assert( pFKey->nCol==1 || (aiFree && pIdx) );
128688 iCol = pFKey->aCol[0].iFrom;
128691 for(i=0; i<pFKey->nCol; i++){
128692 if( aiCol[i]==pTab->iPKey ){
128693 aiCol[i] = -1;
128695 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
128700 if( db->xAuth ){
128702 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
128703 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
128709 /* Take a shared-cache advisory read-lock on the parent table. Allocate
128712 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
128713 pParse->nTab++;
128719 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
128738 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
128747 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
128748 && !pParse->pToplevel && !pParse->isMultiWrite
128757 if( !isIgnoreErrors || db->mallocFailed ) return;
128760 assert( aiCol || pFKey->nCol==1 );
128766 SrcItem *pItem = pSrc->a;
128767 pItem->pTab = pFKey->pFrom;
128768 pItem->zName = pFKey->pFrom->zName;
128769 pItem->pTab->nTabRef++;
128770 pItem->iCursor = pParse->nTab++;
128773 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
128776 int eAction = pFKey->aAction[aChange!=0];
128781 ** So do not set the "may-abort" flag in this case.
128784 ** may-abort flag will eventually be set on this statement anyway
128795 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
128799 pItem->zName = 0;
128817 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
128820 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128821 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
128823 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
128827 for(i=0; i<pIdx->nKeyCol; i++){
128828 assert( pIdx->aiColumn[i]>=0 );
128829 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
128844 ** entry in the aChange[] array is set to -1. If the column is modified,
128849 ** non-zero. If there is no foreign key related processing, this function
128865 int *aChange, /* Non-NULL for UPDATE operations */
128870 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
128875 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
128882 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128884 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
128890 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
128892 if( p->aAction[1]!=OE_None ) return 2;
128903 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
128911 ** require no special handling by the triggers sub-system, code for them is
128934 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
128936 sqlite3 *db = pParse->db; /* Database handle */
128941 action = pFKey->aAction[iAction];
128942 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
128945 pTrigger = pFKey->apTrigger[iAction];
128951 int *aiCol = 0; /* child table cols -> parent key cols */
128960 assert( aiCol || pFKey->nCol==1 );
128962 for(i=0; i<pFKey->nCol; i++){
128970 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
128972 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
128973 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
128975 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
128976 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
129014 Column *pCol = pFKey->pFrom->aCol + iFromCol;
129016 if( pCol->colFlags & COLFLAG_GENERATED ){
129017 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
129018 testcase( pCol->colFlags & COLFLAG_STORED );
129021 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
129037 zFrom = pFKey->pFrom->zName;
129041 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129048 tDb.z = db->aDb[iDb].zDbSName;
129053 pRaise->affExpr = OE_Abort;
129070 nFrom + 1 /* Space for pStep->zTarget */
129073 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
129074 pStep->zTarget = (char *)&pStep[1];
129075 memcpy((char *)pStep->zTarget, zFrom, nFrom);
129077 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
129078 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
129079 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
129082 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
129086 /* Re-enable the lookaside buffer, if it was disabled earlier. */
129093 if( db->mallocFailed==1 ){
129102 pStep->op = TK_SELECT;
129106 pStep->op = TK_DELETE;
129111 pStep->op = TK_UPDATE;
129113 pStep->pTrig = pTrigger;
129114 pTrigger->pSchema = pTab->pSchema;
129115 pTrigger->pTabSchema = pTab->pSchema;
129116 pFKey->apTrigger[iAction] = pTrigger;
129117 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
129130 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
129135 /* If foreign-key support is enabled, iterate through all FKs that
129138 ** trigger sub-program. */
129139 if( pParse->db->flags&SQLITE_ForeignKeys ){
129141 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
129161 FKey *pNext; /* Copy of pFKey->pNextFrom */
129165 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
129166 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
129169 if( db->pnBytesFreed==0 ){
129170 if( pFKey->pPrevTo ){
129171 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
129173 void *p = (void *)pFKey->pNextTo;
129174 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
129175 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
129177 if( pFKey->pNextTo ){
129178 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
129182 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
129185 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
129189 fkTriggerDelete(db, pFKey->apTrigger[0]);
129190 fkTriggerDelete(db, pFKey->apTrigger[1]);
129193 pNext = pFKey->pNextFrom;
129235 assert( pParse->pVdbe!=0 );
129236 v = pParse->pVdbe;
129238 sqlite3TableLock(pParse, iDb, pTab->tnum,
129239 (opcode==OP_OpenWrite)?1:0, pTab->zName);
129241 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
129242 VdbeComment((v, "%s", pTab->zName));
129246 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
129247 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
129249 VdbeComment((v, "%s", pTab->zName));
129259 ** ------------------------------
129274 if( !pIdx->zColAff ){
129284 Table *pTab = pIdx->pTable;
129285 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
129286 if( !pIdx->zColAff ){
129290 for(n=0; n<pIdx->nColumn; n++){
129291 i16 x = pIdx->aiColumn[n];
129294 aff = pTab->aCol[x].affinity;
129299 assert( pIdx->bHasExpr );
129300 assert( pIdx->aColExpr!=0 );
129301 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
129305 pIdx->zColAff[n] = aff;
129307 pIdx->zColAff[n] = 0;
129310 return pIdx->zColAff;
129320 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
129323 for(i=j=0; i<pTab->nCol; i++){
129324 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
129325 zColAff[j++] = pTab->aCol[i].affinity;
129329 zColAff[j--] = 0;
129339 ** For ordinary (legacy, non-strict) tables:
129340 ** -----------------------------------------
129346 ** which were then optimized out) then this routine becomes a no-op.
129356 ** --------- ---------------
129364 ** ------------------
129378 if( pTab->tabFlags & TF_Strict ){
129387 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
129388 pPrev->opcode = OP_TypeCheck;
129389 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
129392 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
129397 zColAff = pTab->zColAff;
129404 pTab->zColAff = zColAff;
129412 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
129413 || sqlite3VdbeDb(v)->mallocFailed );
129414 sqlite3VdbeChangeP4(v, -1, zColAff, i);
129420 ** Return non-zero if the table pTab in database iDb or any of its indices
129430 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
129436 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
129438 Pgno tnum = pOp->p2;
129439 if( tnum==pTab->tnum ){
129442 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
129443 if( tnum==pIndex->tnum ){
129449 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
129450 assert( pOp->p4.pVtab!=0 );
129451 assert( pOp->p4type==P4_VTAB );
129463 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
129464 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
129465 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
129489 assert( pTab->tabFlags & TF_HasGenerated );
129490 testcase( pTab->tabFlags & TF_HasVirtual );
129491 testcase( pTab->tabFlags & TF_HasStored );
129496 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
129497 if( (pTab->tabFlags & TF_HasStored)!=0 ){
129498 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
129499 if( pOp->opcode==OP_Affinity ){
129501 ** columns. '@' is the no-op affinity and those columns have not
129504 char *zP4 = pOp->p4.z;
129506 assert( pOp->p4type==P4_DYNAMIC );
129508 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
129511 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
129516 }else if( pOp->opcode==OP_TypeCheck ){
129520 pOp->p3 = 1;
129525 ** this is a two-pass algorithm. On the first pass, mark all generated
129528 for(i=0; i<pTab->nCol; i++){
129529 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
129530 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
129531 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
129532 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
129541 /* On the second pass, compute the value of each NOT-AVAILABLE column.
129546 pParse->iSelfTab = -iRegStore;
129550 for(i=0; i<pTab->nCol; i++){
129551 Column *pCol = pTab->aCol + i;
129552 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
129554 pCol->colFlags |= COLFLAG_BUSY;
129557 pCol->colFlags &= ~COLFLAG_BUSY;
129563 assert( pCol->colFlags & COLFLAG_GENERATED );
129566 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
129571 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
129573 pParse->iSelfTab = 0;
129608 assert( pParse->db->aDb[iDb].pSchema!=0 );
129609 if( (pTab->tabFlags & TF_Autoincrement)!=0
129610 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
129614 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
129618 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
129622 || pSeqTab->nCol!=2
129624 pParse->nErr++;
129625 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
129629 pInfo = pToplevel->pAinc;
129630 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
129632 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
129634 testcase( pParse->earlyCleanup );
129635 if( pParse->db->mallocFailed ) return 0;
129636 pInfo->pNext = pToplevel->pAinc;
129637 pToplevel->pAinc = pInfo;
129638 pInfo->pTab = pTab;
129639 pInfo->iDb = iDb;
129640 pToplevel->nMem++; /* Register to hold name of table */
129641 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
129642 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
129644 memId = pInfo->regCtr;
129655 sqlite3 *db = pParse->db; /* The database connection */
129658 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
129660 /* This routine is never called during trigger-generation. It is
129661 ** only called from the top-level */
129662 assert( pParse->pTriggerTab==0 );
129666 for(p = pParse->pAinc; p; p = p->pNext){
129683 pDb = &db->aDb[p->iDb];
129684 memId = p->regCtr;
129685 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129686 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
129687 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
129693 aOp[3].p1 = memId-1;
129702 if( pParse->nTab==0 ) pParse->nTab = 1;
129716 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
129729 Vdbe *v = pParse->pVdbe;
129730 sqlite3 *db = pParse->db;
129733 for(p = pParse->pAinc; p; p = p->pNext){
129743 Db *pDb = &db->aDb[p->iDb];
129745 int memId = p->regCtr;
129748 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129751 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
129756 aOp[2].p1 = memId-1;
129765 if( pParse->pAinc ) autoIncrementEnd(pParse);
129770 ** above are all no-ops
129794 ** then a list of all (non-hidden) columns for the table is substituted.
129799 ** first two forms shown above. A VALUES clause is really just short-hand
129805 ** insert with data coming from a single-row VALUES clause, the code executes
129806 ** once straight down through. Pseudo-code follows (we call this
129818 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
129842 ** X <- A
129850 ** end-coroutine X
129863 ** X <- A
129871 ** end co-routine R
129900 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
129929 db = pParse->db;
129930 assert( db->pParse==pParse );
129931 if( pParse->nErr ){
129934 assert( db->mallocFailed==0 );
129941 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
129942 pList = pSelect->pEList;
129943 pSelect->pEList = 0;
129950 assert( pTabList->nSrc==1 );
129955 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129956 assert( iDb<db->nDb );
129957 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
129958 db->aDb[iDb].zDbSName) ){
129983 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
129989 ** ViewGetColumnNames() is a no-op if pTab is not a view.
129995 /* Cannot insert into a read-only table.
130005 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
130037 regRowid = regIns = pParse->nMem+1;
130038 pParse->nMem += pTab->nCol + 1;
130041 pParse->nMem++;
130054 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
130055 ** loop, if ipkColumn==(-1), that means that integer primary key
130064 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
130066 assert( pColumn->eU4!=EU4_EXPR );
130067 pColumn->eU4 = EU4_IDX;
130068 for(i=0; i<pColumn->nId; i++){
130069 pColumn->a[i].u4.idx = -1;
130071 for(i=0; i<pColumn->nId; i++){
130072 for(j=0; j<pTab->nCol; j++){
130073 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
130074 pColumn->a[i].u4.idx = j;
130076 if( j==pTab->iPKey ){
130080 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
130083 pTab->aCol[j].zCnName);
130090 if( j>=pTab->nCol ){
130091 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
130096 pTabList->a, pColumn->a[i].zName);
130097 pParse->checkSchema = 1;
130105 ** is coming from a SELECT statement, then generate a co-routine that
130107 ** co-routine is the common header to the 3rd and 4th templates.
130110 /* Data is coming from a SELECT or from a multi-row VALUES clause.
130111 ** Generate a co-routine to run the SELECT. */
130112 int regYield; /* Register holding co-routine entry-point */
130113 int addrTop; /* Top of the co-routine */
130116 regYield = ++pParse->nMem;
130121 dest.nSdst = pTab->nCol;
130124 assert( db->pParse==pParse );
130125 if( rc || pParse->nErr ) goto insert_cleanup;
130126 assert( db->mallocFailed==0 );
130128 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
130129 assert( pSelect->pEList );
130130 nColumn = pSelect->pEList->nExpr;
130160 srcTab = pParse->nTab++;
130175 ** single-row VALUES clause
130180 srcTab = -1;
130183 nColumn = pList->nExpr;
130197 ipkColumn = pTab->iPKey;
130199 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
130200 testcase( pTab->tabFlags & TF_HasVirtual );
130201 testcase( pTab->tabFlags & TF_HasStored );
130202 for(i=ipkColumn-1; i>=0; i--){
130203 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
130204 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
130205 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
130206 ipkColumn--;
130218 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
130219 for(i=0; i<pTab->nCol; i++){
130220 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
130223 if( nColumn!=(pTab->nCol-nHidden) ){
130226 pTabList->a, pTab->nCol-nHidden, nColumn);
130230 if( pColumn!=0 && nColumn!=pColumn->nId ){
130231 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
130237 if( (db->flags & SQLITE_CountRows)!=0
130238 && !pParse->nested
130239 && !pParse->pTriggerTab
130240 && !pParse->bReturning
130242 regRowCount = ++pParse->nMem;
130249 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
130255 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
130257 aRegIdx[i] = ++pParse->nMem;
130258 pParse->nMem += pIdx->nColumn;
130260 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
130267 pTab->zName);
130274 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
130277 pTabList->a[0].iCursor = iDataCur;
130280 pNx->pUpsertSrc = pTabList;
130281 pNx->regData = regData;
130282 pNx->iDataCur = iDataCur;
130283 pNx->iIdxCur = iIdxCur;
130284 if( pNx->pUpsertTarget ){
130289 pNx = pNx->pNextUpsert;
130317 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
130321 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
130323 ** the value does not get overwritten by a NULL at tag-20191021-002. */
130337 for(i=0; i<pTab->nCol; i++, iRegStore++){
130341 if( i==pTab->iPKey ){
130342 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
130345 ** NULL - we cannot optimize further by skipping the column completely */
130349 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
130355 iRegStore--;
130359 ** triggers, the slots used for stored columns will be OP_Copy-ed
130370 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130376 assert( pColumn->eU4==EU4_IDX );
130377 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
130378 if( j>=pColumn->nId ){
130382 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130390 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
130394 k = i - nHidden;
130404 Expr *pX = pList->a[k].pExpr;
130418 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
130424 ** not happened yet) so we substitute a rowid of -1
130427 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
130435 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
130438 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
130444 assert( pTab->nNVCol>0 );
130445 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
130452 if( pTab->tabFlags & TF_HasGenerated ){
130453 testcase( pTab->tabFlags & TF_HasVirtual );
130454 testcase( pTab->tabFlags & TF_HasStored );
130470 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
130472 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
130485 /* Rowid already initialized at tag-20191021-001 */
130487 Expr *pIpk = pList->a[ipkColumn].pExpr;
130488 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
130492 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
130523 if( pTab->tabFlags & TF_HasGenerated ){
130535 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
130546 if( db->flags & SQLITE_ForeignKeys ){
130564 }else if( pParse->bReturning ){
130566 ** constant value -1, in case one or more of the returned expressions
130568 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
130581 pTab, regData-2-pTab->nCol, onError, endOfLoop);
130598 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
130599 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
130613 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
130649 ** Meanings of bits in of pWalker->eCode for
130656 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
130661 if( pExpr->op==TK_COLUMN ){
130662 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
130663 if( pExpr->iColumn>=0 ){
130664 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
130665 pWalker->eCode |= CKCNSTRNT_COLUMN;
130668 pWalker->eCode |= CKCNSTRNT_ROWID;
130675 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
130684 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
130685 ** The operation of this routine is the same - return true if an only if
130713 ** the indexes of a table in the order provided in the Table->pIndex list.
130714 ** However, sometimes (rarely - when there is an upsert) it wants to visit
130748 assert( pIter->i==0 );
130749 if( pIter->eType ){
130750 *pIx = pIter->u.ax.aIdx[0].ix;
130751 return pIter->u.ax.aIdx[0].p;
130754 return pIter->u.lx.pIdx;
130760 if( pIter->eType ){
130761 int i = ++pIter->i;
130762 if( i>=pIter->u.ax.nIdx ){
130766 *pIx = pIter->u.ax.aIdx[i].ix;
130767 return pIter->u.ax.aIdx[i].p;
130770 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
130771 return pIter->u.lx.pIdx;
130781 ** pTab->nCol+1 registers in this range. The first register (the one
130808 ** at pTab->pIndex.
130810 ** (2019-05-07) The generated code also creates a new record for the
130812 ** register identified by aRegIdx[nIdx] - in other words in the first
130823 ** for the first index in the pTab->pIndex list. Cursors for other indices
130824 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
130832 ** --------------- ---------- ----------------------------------------
130862 ** Or if overrideError==OE_Default, then the pParse->onError parameter
130863 ** is used. Or if pParse->onError==OE_Default then the onError value
130874 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
130908 db = pParse->db;
130909 v = pParse->pVdbe;
130912 nCol = pTab->nCol;
130923 nPkField = pPk->nKeyCol;
130932 if( pTab->tabFlags & TF_HasNotNull ){
130939 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
130940 int isGenerated; /* non-zero if column is generated */
130941 onError = pCol->notNull;
130943 if( i==pTab->iPKey ){
130946 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
130962 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
130964 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
130965 testcase( pCol->colFlags & COLFLAG_STORED );
130966 testcase( pCol->colFlags & COLFLAG_GENERATED );
130982 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
130994 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
130995 pCol->zCnName);
131020 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
131028 } /* end of 2-pass loop */
131029 } /* end if( has-not-null-constraints ) */
131034 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
131035 ExprList *pCheck = pTab->pCheck;
131036 pParse->iSelfTab = -(regNewData+1);
131038 for(i=0; i<pCheck->nExpr; i++){
131041 Expr *pExpr = pCheck->a[i].pExpr;
131056 if( !db->mallocFailed ){
131063 char *zName = pCheck->a[i].zEName;
131064 assert( zName!=0 || pParse->db->mallocFailed );
131065 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
131072 pParse->iSelfTab = 0;
131089 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
131107 sIdxIter.u.lx.pIdx = pTab->pIndex;
131109 if( pUpsert->pUpsertTarget==0 ){
131110 /* There is just on ON CONFLICT clause and it has no constraint-target */
131111 assert( pUpsert->pNextUpsert==0 );
131112 if( pUpsert->isDoUpdate==0 ){
131113 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
131121 }else if( pTab->pIndex!=0 ){
131129 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
131138 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
131139 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
131140 if( pTerm->pUpsertTarget==0 ) break;
131141 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
131143 pIdx = pTab->pIndex;
131144 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
131145 pIdx = pIdx->pNext;
131154 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
131184 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
131190 if( db->flags&SQLITE_RecTriggers ){
131200 regTrigCnt = ++pParse->nMem;
131215 onError = pTab->keyConf;
131226 if( pUpsertClause->isDoUpdate==0 ){
131247 && pTab->pIndex /* There exist other constraints */
131286 ** recursive-triggers flag is set, call GenerateRowDelete() to
131288 ** the triggers and remove both the table and index b-tree entries.
131290 ** Otherwise, if there are no triggers or the recursive-triggers
131292 ** GenerateRowIndexDelete(). This removes the index b-tree entries
131293 ** only. The table b-tree entry will be replaced by the new entry
131310 regNewData, 1, 0, OE_Replace, 1, -1);
131316 /* This OP_Delete opcode fires the pre-update-hook only. It does
131317 ** not modify the b-tree. It is more efficient to let the coming
131323 if( pTab->pIndex ){
131325 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
131348 sqlite3VdbeJumpHere(v, ipkTop-1);
131381 VdbeNoopComment((v, "prep index %s", pIdx->zName));
131386 if( pIdx->pPartIdxWhere ){
131388 pParse->iSelfTab = -(regNewData+1);
131389 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
131391 pParse->iSelfTab = 0;
131398 for(i=0; i<pIdx->nColumn; i++){
131399 int iField = pIdx->aiColumn[i];
131402 pParse->iSelfTab = -(regNewData+1);
131403 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
131404 pParse->iSelfTab = 0;
131405 VdbeComment((v, "%s column %d", pIdx->zName, i));
131406 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
131414 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
131417 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
131418 VdbeComment((v, "for %s", pIdx->zName));
131420 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
131421 sqlite3SetMakeRecordP5(v, pIdx->pTable);
131424 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
131436 onError = pIdx->onError;
131449 if( pUpsertClause->isDoUpdate==0 ){
131464 ** must be explicitly deleted in order to ensure any pre-update hook
131468 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
131471 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
131473 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
131474 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
131485 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
131493 ** is different from old-rowid */
131502 ** store it in registers regR..regR+nPk-1 */
131504 for(i=0; i<pPk->nKeyCol; i++){
131505 assert( pPk->aiColumn[i]>=0 );
131506 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
131508 VdbeComment((v, "%s.%s", pTab->zName,
131509 pTab->aCol[pPk->aiColumn[i]].zCnName));
131520 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
131524 for(i=0; i<pPk->nKeyCol; i++){
131525 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
131526 x = pPk->aiColumn[i];
131528 if( i==(pPk->nKeyCol-1) ){
131572 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
131573 assert( nConflictCk>0 || db->mallocFailed );
131601 if( pIdx->pPartIdxWhere ){
131604 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
131608 ** the constraint-ok jump destination to be the address of
131629 nConflictCk--;
131684 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
131698 ** to be the number of columns in table pTab that must not be NULL-trimmed.
131700 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
131706 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
131707 if( pTab->pSchema->file_format<2 ) return;
131709 for(i=pTab->nCol-1; i>0; i--){
131710 if( pTab->aCol[i].iDflt!=0 ) break;
131711 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
131720 ** PK index. This function adds code to invoke the pre-update hook,
131730 Vdbe *v = pParse->pVdbe;
131733 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
131773 v = pParse->pVdbe;
131776 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
131778 assert( pIdx->onError!=OE_Replace
131779 || pIdx->pNext==0
131780 || pIdx->pNext->onError==OE_Replace );
131782 if( pIdx->pPartIdxWhere ){
131796 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
131800 if( pParse->nested ){
131813 if( !pParse->nested ){
131829 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
131835 ** pTab->pIndex list.
131837 ** If pTab is a virtual table, then this routine is a no-op and the
131859 /* This routine is a no-op for virtual tables. Leave the output
131862 *piDataCur = *piIdxCur = -999;
131865 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
131866 v = pParse->pVdbe;
131868 if( iBase<0 ) iBase = pParse->nTab;
131874 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
131877 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
131879 assert( pIdx->pSchema==pTab->pSchema );
131885 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
131888 VdbeComment((v, "%s", pIdx->zName));
131891 if( iBase>pParse->nTab ) pParse->nTab = iBase;
131900 ** purposes only - to make sure the transfer optimization really
131922 assert( pDest->pTable!=pSrc->pTable );
131923 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
131926 if( pDest->onError!=pSrc->onError ){
131929 for(i=0; i<pSrc->nKeyCol; i++){
131930 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
131933 if( pSrc->aiColumn[i]==XN_EXPR ){
131934 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
131935 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
131936 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
131940 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
131943 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
131947 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
131965 ** There are lots of rules for determining compatibility - see comments
131970 ** is empty - a factor that can only be determined at run-time. In that
131987 sqlite3 *db = pParse->db;
131991 SrcItem *pItem; /* An element of pSelect->pSrc */
132004 if( pParse->pWith || pSelect->pWith ){
132016 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
132019 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
132020 if( pSelect->pSrc->nSrc!=1 ){
132023 if( pSelect->pSrc->a[0].pSelect ){
132026 if( pSelect->pWhere ){
132029 if( pSelect->pOrderBy ){
132034 if( pSelect->pGroupBy ){
132037 if( pSelect->pLimit ){
132040 if( pSelect->pPrior ){
132043 if( pSelect->selFlags & SF_Distinct ){
132046 pEList = pSelect->pEList;
132048 if( pEList->nExpr!=1 ){
132051 assert( pEList->a[0].pExpr );
132052 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
132060 pItem = pSelect->pSrc->a;
132065 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
132075 if( pDest->nCol!=pSrc->nCol ){
132078 if( pDest->iPKey!=pSrc->iPKey ){
132081 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
132082 return 0; /* Cannot feed from a non-strict into a strict table */
132084 for(i=0; i<pDest->nCol; i++){
132085 Column *pDestCol = &pDest->aCol[i];
132086 Column *pSrcCol = &pSrc->aCol[i];
132088 if( (db->mDbFlags & DBFLAG_Vacuum)==0
132089 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
132111 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
132112 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
132113 return 0; /* Both columns have the same generated-column type */
132119 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
132122 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
132123 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
132124 testcase( pDestCol->colFlags & COLFLAG_STORED );
132129 if( pDestCol->affinity!=pSrcCol->affinity ){
132136 if( pDestCol->notNull && !pSrcCol->notNull ){
132140 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
132143 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
132145 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
132148 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
132149 pSrcExpr->u.zToken)!=0)
132155 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
132159 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
132165 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
132170 return 0; /* Corrupt schema - two indexes on the same btree */
132174 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
132187 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
132191 if( (db->flags & SQLITE_CountRows)!=0 ){
132202 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
132205 iSrc = pParse->nTab++;
132206 iDest = pParse->nTab++;
132213 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
132214 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
132243 if( pDest->iPKey>=0 ){
132245 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132253 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
132257 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
132260 if( db->mDbFlags & DBFLAG_Vacuum ){
132267 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132276 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
132277 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
132285 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
132286 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
132288 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
132290 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
132294 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
132296 VdbeComment((v, "%s", pSrcIdx->zName));
132297 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
132300 VdbeComment((v, "%s", pDestIdx->zName));
132302 if( db->mDbFlags & DBFLAG_Vacuum ){
132307 ** order. In this case, instead of seeking within the b-tree as part
132309 ** OP_IdxInsert to seek to the point within the b-tree where each key
132317 for(i=0; i<pSrcIdx->nColumn; i++){
132318 const char *zColl = pSrcIdx->azColl[i];
132321 if( i==pSrcIdx->nColumn ){
132326 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
132331 if( (db->mDbFlags & DBFLAG_Vacuum)==0
132407 sqlite3_mutex_enter(db->mutex);
132414 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
132420 /* this happens for a comment or white-space */
132433 && db->flags&SQLITE_NullCallback)) ){
132460 /* EVIDENCE-OF: R-38229-40159 If the callback function to
132461 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
132499 assert( (rc&db->errMask)==rc );
132500 sqlite3_mutex_leave(db->mutex);
132906 ** (part of the main SQLite library - not an extension) so that
132913 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
132915 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
132917 #define sqlite3_bind_blob sqlite3_api->bind_blob
132918 #define sqlite3_bind_double sqlite3_api->bind_double
132919 #define sqlite3_bind_int sqlite3_api->bind_int
132920 #define sqlite3_bind_int64 sqlite3_api->bind_int64
132921 #define sqlite3_bind_null sqlite3_api->bind_null
132922 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
132923 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
132924 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
132925 #define sqlite3_bind_text sqlite3_api->bind_text
132926 #define sqlite3_bind_text16 sqlite3_api->bind_text16
132927 #define sqlite3_bind_value sqlite3_api->bind_value
132928 #define sqlite3_busy_handler sqlite3_api->busy_handler
132929 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
132930 #define sqlite3_changes sqlite3_api->changes
132931 #define sqlite3_close sqlite3_api->close
132932 #define sqlite3_collation_needed sqlite3_api->collation_needed
132933 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
132934 #define sqlite3_column_blob sqlite3_api->column_blob
132935 #define sqlite3_column_bytes sqlite3_api->column_bytes
132936 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
132937 #define sqlite3_column_count sqlite3_api->column_count
132938 #define sqlite3_column_database_name sqlite3_api->column_database_name
132939 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
132940 #define sqlite3_column_decltype sqlite3_api->column_decltype
132941 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
132942 #define sqlite3_column_double sqlite3_api->column_double
132943 #define sqlite3_column_int sqlite3_api->column_int
132944 #define sqlite3_column_int64 sqlite3_api->column_int64
132945 #define sqlite3_column_name sqlite3_api->column_name
132946 #define sqlite3_column_name16 sqlite3_api->column_name16
132947 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
132948 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
132949 #define sqlite3_column_table_name sqlite3_api->column_table_name
132950 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
132951 #define sqlite3_column_text sqlite3_api->column_text
132952 #define sqlite3_column_text16 sqlite3_api->column_text16
132953 #define sqlite3_column_type sqlite3_api->column_type
132954 #define sqlite3_column_value sqlite3_api->column_value
132955 #define sqlite3_commit_hook sqlite3_api->commit_hook
132956 #define sqlite3_complete sqlite3_api->complete
132957 #define sqlite3_complete16 sqlite3_api->complete16
132958 #define sqlite3_create_collation sqlite3_api->create_collation
132959 #define sqlite3_create_collation16 sqlite3_api->create_collation16
132960 #define sqlite3_create_function sqlite3_api->create_function
132961 #define sqlite3_create_function16 sqlite3_api->create_function16
132962 #define sqlite3_create_module sqlite3_api->create_module
132963 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
132964 #define sqlite3_data_count sqlite3_api->data_count
132965 #define sqlite3_db_handle sqlite3_api->db_handle
132966 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
132967 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
132968 #define sqlite3_errcode sqlite3_api->errcode
132969 #define sqlite3_errmsg sqlite3_api->errmsg
132970 #define sqlite3_errmsg16 sqlite3_api->errmsg16
132971 #define sqlite3_exec sqlite3_api->exec
132973 #define sqlite3_expired sqlite3_api->expired
132975 #define sqlite3_finalize sqlite3_api->finalize
132976 #define sqlite3_free sqlite3_api->free
132977 #define sqlite3_free_table sqlite3_api->free_table
132978 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
132979 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
132980 #define sqlite3_get_table sqlite3_api->get_table
132982 #define sqlite3_global_recover sqlite3_api->global_recover
132984 #define sqlite3_interrupt sqlite3_api->interruptx
132985 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
132986 #define sqlite3_libversion sqlite3_api->libversion
132987 #define sqlite3_libversion_number sqlite3_api->libversion_number
132988 #define sqlite3_malloc sqlite3_api->malloc
132989 #define sqlite3_mprintf sqlite3_api->mprintf
132990 #define sqlite3_open sqlite3_api->open
132991 #define sqlite3_open16 sqlite3_api->open16
132992 #define sqlite3_prepare sqlite3_api->prepare
132993 #define sqlite3_prepare16 sqlite3_api->prepare16
132994 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
132995 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
132996 #define sqlite3_profile sqlite3_api->profile
132997 #define sqlite3_progress_handler sqlite3_api->progress_handler
132998 #define sqlite3_realloc sqlite3_api->realloc
132999 #define sqlite3_reset sqlite3_api->reset
133000 #define sqlite3_result_blob sqlite3_api->result_blob
133001 #define sqlite3_result_double sqlite3_api->result_double
133002 #define sqlite3_result_error sqlite3_api->result_error
133003 #define sqlite3_result_error16 sqlite3_api->result_error16
133004 #define sqlite3_result_int sqlite3_api->result_int
133005 #define sqlite3_result_int64 sqlite3_api->result_int64
133006 #define sqlite3_result_null sqlite3_api->result_null
133007 #define sqlite3_result_text sqlite3_api->result_text
133008 #define sqlite3_result_text16 sqlite3_api->result_text16
133009 #define sqlite3_result_text16be sqlite3_api->result_text16be
133010 #define sqlite3_result_text16le sqlite3_api->result_text16le
133011 #define sqlite3_result_value sqlite3_api->result_value
133012 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
133013 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
133014 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
133015 #define sqlite3_snprintf sqlite3_api->xsnprintf
133016 #define sqlite3_step sqlite3_api->step
133017 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
133018 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
133019 #define sqlite3_total_changes sqlite3_api->total_changes
133020 #define sqlite3_trace sqlite3_api->trace
133022 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
133024 #define sqlite3_update_hook sqlite3_api->update_hook
133025 #define sqlite3_user_data sqlite3_api->user_data
133026 #define sqlite3_value_blob sqlite3_api->value_blob
133027 #define sqlite3_value_bytes sqlite3_api->value_bytes
133028 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
133029 #define sqlite3_value_double sqlite3_api->value_double
133030 #define sqlite3_value_int sqlite3_api->value_int
133031 #define sqlite3_value_int64 sqlite3_api->value_int64
133032 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
133033 #define sqlite3_value_text sqlite3_api->value_text
133034 #define sqlite3_value_text16 sqlite3_api->value_text16
133035 #define sqlite3_value_text16be sqlite3_api->value_text16be
133036 #define sqlite3_value_text16le sqlite3_api->value_text16le
133037 #define sqlite3_value_type sqlite3_api->value_type
133038 #define sqlite3_vmprintf sqlite3_api->vmprintf
133039 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
133040 #define sqlite3_overload_function sqlite3_api->overload_function
133041 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
133042 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
133043 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
133044 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
133045 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
133046 #define sqlite3_blob_close sqlite3_api->blob_close
133047 #define sqlite3_blob_open sqlite3_api->blob_open
133048 #define sqlite3_blob_read sqlite3_api->blob_read
133049 #define sqlite3_blob_write sqlite3_api->blob_write
133050 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
133051 #define sqlite3_file_control sqlite3_api->file_control
133052 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
133053 #define sqlite3_memory_used sqlite3_api->memory_used
133054 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
133055 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
133056 #define sqlite3_mutex_free sqlite3_api->mutex_free
133057 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
133058 #define sqlite3_mutex_try sqlite3_api->mutex_try
133059 #define sqlite3_open_v2 sqlite3_api->open_v2
133060 #define sqlite3_release_memory sqlite3_api->release_memory
133061 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
133062 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
133063 #define sqlite3_sleep sqlite3_api->sleep
133064 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
133065 #define sqlite3_vfs_find sqlite3_api->vfs_find
133066 #define sqlite3_vfs_register sqlite3_api->vfs_register
133067 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
133068 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
133069 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
133070 #define sqlite3_result_error_code sqlite3_api->result_error_code
133071 #define sqlite3_test_control sqlite3_api->test_control
133072 #define sqlite3_randomness sqlite3_api->randomness
133073 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
133074 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
133075 #define sqlite3_limit sqlite3_api->limit
133076 #define sqlite3_next_stmt sqlite3_api->next_stmt
133077 #define sqlite3_sql sqlite3_api->sql
133078 #define sqlite3_status sqlite3_api->status
133079 #define sqlite3_backup_finish sqlite3_api->backup_finish
133080 #define sqlite3_backup_init sqlite3_api->backup_init
133081 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
133082 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
133083 #define sqlite3_backup_step sqlite3_api->backup_step
133084 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
133085 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
133086 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
133087 #define sqlite3_db_config sqlite3_api->db_config
133088 #define sqlite3_db_mutex sqlite3_api->db_mutex
133089 #define sqlite3_db_status sqlite3_api->db_status
133090 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
133091 #define sqlite3_log sqlite3_api->log
133092 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
133093 #define sqlite3_sourceid sqlite3_api->sourceid
133094 #define sqlite3_stmt_status sqlite3_api->stmt_status
133095 #define sqlite3_strnicmp sqlite3_api->strnicmp
133096 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
133097 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
133098 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
133099 #define sqlite3_wal_hook sqlite3_api->wal_hook
133100 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
133101 #define sqlite3_vtab_config sqlite3_api->vtab_config
133102 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
133104 #define sqlite3_close_v2 sqlite3_api->close_v2
133105 #define sqlite3_db_filename sqlite3_api->db_filename
133106 #define sqlite3_db_readonly sqlite3_api->db_readonly
133107 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
133108 #define sqlite3_errstr sqlite3_api->errstr
133109 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
133110 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
133111 #define sqlite3_stricmp sqlite3_api->stricmp
133112 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
133113 #define sqlite3_uri_int64 sqlite3_api->uri_int64
133114 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
133115 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
133116 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
133118 #define sqlite3_auto_extension sqlite3_api->auto_extension
133119 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
133120 #define sqlite3_bind_text64 sqlite3_api->bind_text64
133121 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
133122 #define sqlite3_load_extension sqlite3_api->load_extension
133123 #define sqlite3_malloc64 sqlite3_api->malloc64
133124 #define sqlite3_msize sqlite3_api->msize
133125 #define sqlite3_realloc64 sqlite3_api->realloc64
133126 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
133127 #define sqlite3_result_blob64 sqlite3_api->result_blob64
133128 #define sqlite3_result_text64 sqlite3_api->result_text64
133129 #define sqlite3_strglob sqlite3_api->strglob
133131 #define sqlite3_value_dup sqlite3_api->value_dup
133132 #define sqlite3_value_free sqlite3_api->value_free
133133 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
133134 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
133136 #define sqlite3_value_subtype sqlite3_api->value_subtype
133137 #define sqlite3_result_subtype sqlite3_api->result_subtype
133139 #define sqlite3_status64 sqlite3_api->status64
133140 #define sqlite3_strlike sqlite3_api->strlike
133141 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
133143 #define sqlite3_system_errno sqlite3_api->system_errno
133145 #define sqlite3_trace_v2 sqlite3_api->trace_v2
133146 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
133148 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
133150 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
133151 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
133152 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
133153 #define sqlite3_result_pointer sqlite3_api->result_pointer
133154 #define sqlite3_value_pointer sqlite3_api->value_pointer
133156 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
133157 #define sqlite3_value_nochange sqlite3_api->value_nochange
133158 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
133160 #define sqlite3_keyword_count sqlite3_api->keyword_count
133161 #define sqlite3_keyword_name sqlite3_api->keyword_name
133162 #define sqlite3_keyword_check sqlite3_api->keyword_check
133163 #define sqlite3_str_new sqlite3_api->str_new
133164 #define sqlite3_str_finish sqlite3_api->str_finish
133165 #define sqlite3_str_appendf sqlite3_api->str_appendf
133166 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
133167 #define sqlite3_str_append sqlite3_api->str_append
133168 #define sqlite3_str_appendall sqlite3_api->str_appendall
133169 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
133170 #define sqlite3_str_reset sqlite3_api->str_reset
133171 #define sqlite3_str_errcode sqlite3_api->str_errcode
133172 #define sqlite3_str_length sqlite3_api->str_length
133173 #define sqlite3_str_value sqlite3_api->str_value
133175 #define sqlite3_create_window_function sqlite3_api->create_window_function
133177 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
133179 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
133180 #define sqlite3_value_frombind sqlite3_api->value_frombind
133182 #define sqlite3_drop_modules sqlite3_api->drop_modules
133184 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
133185 #define sqlite3_uri_key sqlite3_api->uri_key
133186 #define sqlite3_filename_database sqlite3_api->filename_database
133187 #define sqlite3_filename_journal sqlite3_api->filename_journal
133188 #define sqlite3_filename_wal sqlite3_api->filename_wal
133190 #define sqlite3_create_filename sqlite3_api->create_filename
133191 #define sqlite3_free_filename sqlite3_api->free_filename
133192 #define sqlite3_database_file_object sqlite3_api->database_file_object
133194 #define sqlite3_txn_state sqlite3_api->txn_state
133196 #define sqlite3_changes64 sqlite3_api->changes64
133197 #define sqlite3_total_changes64 sqlite3_api->total_changes64
133199 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
133201 #define sqlite3_error_offset sqlite3_api->error_offset
133202 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
133203 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
133204 #define sqlite3_vtab_in sqlite3_api->vtab_in
133205 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
133206 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
133209 #define sqlite3_deserialize sqlite3_api->deserialize
133210 #define sqlite3_serialize sqlite3_api->serialize
133212 #define sqlite3_db_name sqlite3_api->db_name
133214 #define sqlite3_value_encoding sqlite3_api->value_encoding
133216 #define sqlite3_set_droptable_handle sqlite3_api->set_droptable_handle
133229 # define SQLITE_EXTENSION_INIT1 /*no-op*/
133231 # define SQLITE_EXTENSION_INIT3 /*no-op*/
133773 sqlite3_vfs *pVfs = db->pVfs;
133805 if( (db->flags & SQLITE_LoadExtension)==0 ){
133814 /* tag-20210611-1. Some dlopen() implementations will segfault if given
133852 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
133872 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
133891 /* Append the new shared library handle to the db->aExtension array. */
133892 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
133896 if( db->nExtension>0 ){
133897 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
133899 sqlite3DbFree(db, db->aExtension);
133900 db->aExtension = aHandle;
133902 db->aExtension[db->nExtension++] = handle;
133913 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
133925 sqlite3_mutex_enter(db->mutex);
133928 sqlite3_mutex_leave(db->mutex);
133938 assert( sqlite3_mutex_held(db->mutex) );
133939 for(i=0; i<db->nExtension; i++){
133940 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
133942 sqlite3DbFree(db, db->aExtension);
133950 sqlite3_mutex_enter(db->mutex);
133952 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
133954 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
133956 sqlite3_mutex_leave(db->mutex);
133977 ** we have to locate the state vector at run-time. In the more common
134037 ** routine is a no-op.
134052 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
134054 wsdAutoext.nExt--;
134223 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
134227 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
134229 /* Names of columns for pragmas that return multi-column result
134230 ** or that return single-column results where the name of the
134297 /* Definitions of all built-in pragmas */
134928 ** Interpret the given string as an auto-vacuum mode value.
134946 ** backed temporary databases, 2 for the Red-Black tree in memory database
134947 ** and 0 to use the compile-time default.
134951 return z[0] - '0';
134968 sqlite3 *db = pParse->db;
134969 if( db->aDb[1].pBt!=0 ){
134970 if( !db->autoCommit
134971 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
134977 sqlite3BtreeClose(db->aDb[1].pBt);
134978 db->aDb[1].pBt = 0;
134993 sqlite3 *db = pParse->db;
134994 if( db->temp_store==ts ) return SQLITE_OK;
134998 db->temp_store = (u8)ts;
135010 u8 n = pPragma->nPragCName;
135013 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
135016 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
135050 if( db->autoCommit ){
135051 Db *pDb = db->aDb;
135052 int n = db->nDb;
135058 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
135059 while( (n--) > 0 ){
135060 if( pDb->pBt ){
135061 sqlite3BtreeSetPagerFlags(pDb->pBt,
135062 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
135069 # define setAllPagerFlags(X) /* no-op */
135074 ** Return a human-readable name for a constraint resolution action.
135095 ** journal-mode name.
135122 upr = ArraySize(aPragmaName)-1;
135128 upr = mid - 1;
135143 int isBuiltin, /* True if this is a built-in function */
135154 for(; p; p=p->pNext){
135163 if( p->xSFunc==0 ) continue;
135164 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
135169 if( p->xValue!=0 ){
135171 }else if( p->xFinalize!=0 ){
135177 p->zName, isBuiltin,
135178 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
135179 p->nArg,
135180 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
135189 ** Generate code to output a single-column result row with a value of the
135222 int minusFlag /* True if a '-' sign preceded <value> */
135224 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
135225 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
135231 sqlite3 *db = pParse->db; /* The database connection */
135244 pParse->nMem = 2;
135250 pDb = &db->aDb[iDb];
135262 zRight = sqlite3MPrintf(db, "-%T", pValue);
135268 zDb = pId2->n>0 ? pDb->zDbSName : 0;
135273 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
135275 ** handled the pragma and generate a no-op prepared statement.
135277 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
135282 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
135292 db->busyHandler.nBusy = 0;
135306 pParse->nErr++;
135307 pParse->rc = rc;
135323 /* IMP: R-43042-22504 No error messages are generated if an
135329 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
135334 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
135335 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
135341 switch( pPragma->ePragTyp ){
135376 pParse->nMem += 2;
135388 pDb->pSchema->cache_size = size;
135389 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
135406 Btree *pBt = pDb->pBt;
135412 /* Malloc may fail when setting the page-size, as there is an internal
135415 db->nextPagesize = sqlite3Atoi(zRight);
135416 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
135432 Btree *pBt = pDb->pBt;
135433 int b = -1;
135442 if( pId2->n==0 && b>=0 ){
135444 for(ii=0; ii<db->nDb; ii++){
135445 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
135474 iReg = ++pParse->nMem;
135498 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
135501 ** the locking-mode of the main database).
135503 eMode = db->dfltLockMode;
135506 if( pId2->n==0 ){
135508 ** of the PRAGMA command. In this case the locking-mode must be
135516 assert(pDb==&db->aDb[0]);
135517 for(ii=2; ii<db->nDb; ii++){
135518 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
135521 db->dfltLockMode = (u8)eMode;
135523 pPager = sqlite3BtreePager(pDb->pBt);
135560 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
135561 /* Do not allow journal-mode "OFF" in defensive since the database
135566 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
135569 pId2->n = 1;
135571 for(ii=db->nDb-1; ii>=0; ii--){
135572 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
135588 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135589 i64 iLimit = -2;
135592 if( iLimit<-1 ) iLimit = -1;
135605 ** Get or set the value of the database 'auto-vacuum' parameter.
135610 Btree *pBt = pDb->pBt;
135617 db->nextAutovac = (u8)eAuto;
135619 ** incr-vacuum flags. This is required in case this connection
135621 ** as an auto-vacuum capable db.
135628 ** that this really is an auto-vacuum capable database.
135647 aOp[4].p3 = eAuto - 1;
135670 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
135686 ** number of pages is adjusted so that the cache uses -N kibibytes
135692 returnSingleInt(v, pDb->pSchema->cache_size);
135695 pDb->pSchema->cache_size = size;
135696 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
135713 ** number of pages is adjusted so that the cache uses -N kibibytes
135727 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
135728 sqlite3BtreeSetSpillSize(pDb->pBt,0));
135732 sqlite3BtreeSetSpillSize(pDb->pBt, size);
135735 db->flags |= SQLITE_CacheSpill;
135737 db->flags &= ~(u64)SQLITE_CacheSpill;
135766 if( pId2->n==0 ) db->szMmap = sz;
135767 for(ii=db->nDb-1; ii>=0; ii--){
135768 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
135769 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
135773 sz = -1;
135782 pParse->nErr++;
135783 pParse->rc = rc;
135796 ** Note that it is possible for the library compile-time options to
135801 returnSingleInt(v, db->temp_store);
135826 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
135834 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
135835 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
135873 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
135904 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135911 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135941 returnSingleInt(v, pDb->safety_level-1);
135943 if( !db->autoCommit ){
135949 pDb->safety_level = iLevel;
135950 pDb->bSyncSet = 1;
135962 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
135964 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
135965 if( db->autoCommit==0 ){
135967 ** in auto-commit mode. */
135971 if( db->auth.authLevel==UAUTH_User ){
135972 /* Do not allow non-admin users to modify the schema arbitrarily */
135978 db->flags |= mask;
135980 db->flags &= ~mask;
135981 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
135985 /* IMP: R-60817-01178 If the argument is "RESET" then schema
135992 /* Many of the flag-pragmas modify the code generated by the SQL
136015 ** pk: Non-zero for PK fields.
136026 pParse->nMem = 7;
136028 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
136031 if( pCol->colFlags & COLFLAG_NOINSERT ){
136032 if( pPragma->iArg==0 ){
136036 if( pCol->colFlags & COLFLAG_VIRTUAL ){
136038 }else if( pCol->colFlags & COLFLAG_STORED ){
136040 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
136044 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
136049 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
136052 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
136055 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
136056 i-nHidden,
136057 pCol->zCnName,
136059 pCol->notNull ? 1 : 0,
136060 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
136083 pParse->nMem = 6;
136085 for(ii=0; ii<db->nDb; ii++){
136089 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
136096 pHash = &db->aDb[ii].pSchema->tblHash;
136098 while( initNCol-- ){
136103 if( pTab->nCol==0 ){
136104 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
136107 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
136111 if( db->mallocFailed ){
136112 sqlite3ErrorMsg(db->pParse, "out of memory");
136113 db->pParse->rc = SQLITE_NOMEM_BKPT;
136115 pHash = &db->aDb[ii].pSchema->tblHash;
136124 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
136129 }else if( pTab->tabFlags & TF_Shadow ){
136135 db->aDb[ii].zDbSName,
136136 sqlite3PreferredTableName(pTab->zName),
136138 pTab->nCol,
136139 (pTab->tabFlags & TF_WithoutRowid)!=0,
136140 (pTab->tabFlags & TF_Strict)!=0
136151 pParse->nMem = 5;
136153 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
136156 sqlite3PreferredTableName(pTab->zName),
136158 pTab->szTabRow,
136159 pTab->nRowLogEst,
136160 pTab->tabFlags);
136161 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136163 pIdx->zName,
136164 pIdx->szIdxRow,
136165 pIdx->aiRowLogEst[0],
136166 pIdx->hasStat1);
136188 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
136191 if( pPragma->iArg ){
136193 mx = pIdx->nColumn;
136194 pParse->nMem = 6;
136197 mx = pIdx->nKeyCol;
136198 pParse->nMem = 3;
136200 pTab = pIdx->pTable;
136202 assert( pParse->nMem<=pPragma->nPragCName );
136204 i16 cnum = pIdx->aiColumn[i];
136206 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
136207 if( pPragma->iArg ){
136209 pIdx->aSortOrder[i],
136210 pIdx->azColl[i],
136211 i<pIdx->nKeyCol);
136213 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
136225 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136226 pParse->nMem = 5;
136228 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
136232 pIdx->zName,
136234 azOrigin[pIdx->idxType],
136235 pIdx->pPartIdxWhere!=0);
136243 pParse->nMem = 3;
136244 for(i=0; i<db->nDb; i++){
136245 if( db->aDb[i].pBt==0 ) continue;
136246 assert( db->aDb[i].zDbSName!=0 );
136249 db->aDb[i].zDbSName,
136250 sqlite3BtreeGetFilename(db->aDb[i].pBt));
136258 pParse->nMem = 2;
136259 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
136261 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
136271 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
136272 pParse->nMem = 6;
136274 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
136275 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
136279 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
136281 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
136290 pParse->nMem = 1;
136291 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
136293 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
136316 pFK = pTab->u.tab.pFKey;
136318 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136320 pParse->nMem = 8;
136324 for(j=0; j<pFK->nCol; j++){
136328 pFK->zTo,
136329 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
136330 pFK->aCol[j].zCol,
136331 actionName(pFK->aAction[1]), /* ON UPDATE */
136332 actionName(pFK->aAction[0]), /* ON DELETE */
136336 pFK = pFK->pNextFrom;
136361 regResult = pParse->nMem+1;
136362 pParse->nMem += 4;
136363 regRow = ++pParse->nMem;
136364 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
136373 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
136374 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136375 zDb = db->aDb[iDb].zDbSName;
136377 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
136378 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
136380 sqlite3VdbeLoadString(v, regResult, pTab->zName);
136382 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
136383 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
136386 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
136392 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
136400 assert( pParse->nErr>0 || pFK==0 );
136402 if( pParse->nTab<i ) pParse->nTab = i;
136405 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
136406 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
136411 assert( x==0 || db->mallocFailed );
136419 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
136420 for(j=0; j<pFK->nCol; j++){
136421 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
136429 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
136430 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
136431 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
136437 assert( pFK->nCol==1 || db->mallocFailed );
136446 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
136485 ** without the overhead of cross-checking indexes. Quick_check
136513 ** to -1 here, to indicate that the VDBE should verify the integrity
136516 assert( iDb==0 || pId2->z );
136517 if( pId2->z==0 ) iDb = -1;
136520 pParse->nMem = 6;
136531 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
136534 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
136537 for(i=0; i<db->nDb; i++){
136549 /* Do an integrity check of the B-Tree
136555 pTbls = &db->aDb[i].pSchema->tblHash;
136562 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
136575 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
136576 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136577 aRoot[++cnt] = pIdx->tnum;
136583 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
136586 /* Do the b-tree integrity checks */
136591 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
136605 int r1 = -1;
136608 int mxCol; /* Maximum non-virtual column number */
136617 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
136618 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
136623 ** reg[8+i] counts the number of entries in the i-th index
136626 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136629 assert( pParse->nMem>=8+j );
136634 /* Fetch the right-most column from the table. This will cause
136639 mxCol = pTab->nCol-1;
136641 && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
136642 || pTab->iPKey==mxCol) ) mxCol--;
136653 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
136658 pTab->zName);
136663 for(j=0; j<pPk->nKeyCol; j++){
136671 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
136672 ** (3) Datatype for TEXT columns in non-STRICT tables must be
136674 ** (4) Datatype for numeric columns in non-STRICT tables must not
136677 bStrict = (pTab->tabFlags & TF_Strict)!=0;
136678 for(j=0; j<pTab->nCol; j++){
136680 Column *pCol = pTab->aCol + j; /* The column to be checked */
136686 if( j==pTab->iPKey ) continue;
136688 doTypeCheck = pCol->eCType>COLTYPE_ANY;
136690 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
136692 if( pCol->notNull==0 && !doTypeCheck ) continue;
136696 if( pCol->colFlags & COLFLAG_VIRTUAL ){
136698 p1 = -1;
136701 if( pCol->iDflt ){
136704 pCol->affinity, &pDfltValue);
136722 if( pCol->notNull ){
136727 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
136728 pCol->zCnName);
136738 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
136748 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
136749 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
136751 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
136752 sqlite3StdType[pCol->eCType-1],
136753 pTab->zName, pTab->aCol[j].zCnName);
136755 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
136756 /* (3) Datatype for TEXT columns in non-STRICT tables must be
136762 pTab->zName, pTab->aCol[j].zCnName);
136764 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
136765 /* (4) Datatype for numeric columns in non-STRICT tables must not
136774 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
136778 pTab->zName, pTab->aCol[j].zCnName);
136786 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
136787 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
136788 if( db->mallocFailed==0 ){
136793 pParse->iSelfTab = iDataCur + 1;
136794 for(k=pCheck->nExpr-1; k>0; k--){
136795 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
136797 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
136800 pParse->iSelfTab = 0;
136802 pTab->zName);
136811 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136821 pIdx->nColumn); VdbeCoverage(v);
136826 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
136837 for(kk=0; kk<pIdx->nKeyCol; kk++){
136838 int iCol = pIdx->aiColumn[kk];
136839 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
136840 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
136848 pIdx->nKeyCol); VdbeCoverage(v);
136849 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
136858 sqlite3VdbeJumpHere(v, loopTop-1);
136861 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136866 sqlite3VdbeLoadString(v, 4, pIdx->zName);
136872 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
136892 aOp[0].p2 = 1-mxErr;
136898 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
136907 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
136912 ** The second form of this pragma is a no-op if the main database file
136933 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
136934 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
136935 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
136938 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
136948 returnSingleText(v, encnames[ENC(pParse->db)].zName);
136955 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
136956 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
136957 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
136958 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
136964 if( !pEnc->zName ){
136989 ** the value of the schema-version and user-version, respectively. Both
136990 ** the schema-version and the user-version are 32-bit signed integers
136993 ** The schema-cookie is usually only manipulated internally by SQLite. It
137000 ** the schema-version is potentially dangerous and may lead to program
137003 ** The user-version is not used internally by SQLite. It may be used by
137007 int iCookie = pPragma->iArg; /* Which cookie to read or write */
137009 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
137024 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
137026 ** mode. Change the OP_SetCookie opcode into a no-op. */
137053 ** Return the names of all compile-time options used in this build,
137059 pParse->nMem = 1;
137076 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
137087 pParse->nMem = 3;
137106 db->xWalCallback==sqlite3WalDefaultHook ?
137107 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
137115 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
137157 ** ever added that should be off by default, those off-by-default
137167 ** (2) The query planner used sqlite_stat1-style statistics for one or
137195 iTabCur = pParse->nTab++;
137196 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
137199 pSchema = db->aDb[iDb].pSchema;
137200 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
137206 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
137209 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
137210 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
137211 if( !pIdx->hasStat1 ){
137223 db->aDb[iDb].zDbSName, pTab->zName);
137247 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
137251 returnSingleInt(v, db->busyTimeout);
137259 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
137261 ** specified and is a non-negative integer.
137262 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
137264 ** sqlite3_soft_heap_limit64(-1) C-language function.
137271 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
137282 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
137289 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
137292 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
137311 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
137325 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
137328 db->nAnalysisLimit = (int)(N&0x7fffffff);
137330 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
137343 pParse->nMem = 2;
137344 for(i=0; i<db->nDb; i++){
137348 if( db->aDb[i].zDbSName==0 ) continue;
137349 pBt = db->aDb[i].pBt;
137352 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
137356 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
137364 ** ---------- ------
137377 if( pPragma->iArg==2 || pPragma->iArg==3 ){
137387 n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
137389 if( (pPragma->iArg & 1)==0 ){
137405 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
137414 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
137418 ** instructions to the VM. */
137419 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
137470 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
137475 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
137479 if( pPragma->mPragFlg & PragFlg_Result1 ){
137483 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
137489 assert( strlen(zBuf) < sizeof(zBuf)-1 );
137497 pTab->pName = pPragma;
137498 pTab->db = db;
137499 pTab->iHidden = i;
137500 pTab->nHidden = j;
137532 pIdxInfo->estimatedCost = (double)1;
137533 if( pTab->nHidden==0 ){ return SQLITE_OK; }
137534 pConstraint = pIdxInfo->aConstraint;
137537 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
137538 if( pConstraint->usable==0 ) continue;
137539 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
137540 if( pConstraint->iColumn < pTab->iHidden ) continue;
137541 j = pConstraint->iColumn - pTab->iHidden;
137546 pIdxInfo->estimatedCost = (double)2147483647;
137547 pIdxInfo->estimatedRows = 2147483647;
137550 j = seen[0]-1;
137551 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
137552 pIdxInfo->aConstraintUsage[j].omit = 1;
137554 pIdxInfo->estimatedCost = (double)20;
137555 pIdxInfo->estimatedRows = 20;
137556 j = seen[1]-1;
137557 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
137558 pIdxInfo->aConstraintUsage[j].omit = 1;
137568 pCsr->base.pVtab = pVtab;
137569 *ppCursor = &pCsr->base;
137576 sqlite3_finalize(pCsr->pPragma);
137577 pCsr->pPragma = 0;
137578 for(i=0; i<ArraySize(pCsr->azArg); i++){
137579 sqlite3_free(pCsr->azArg[i]);
137580 pCsr->azArg[i] = 0;
137598 pCsr->iRowid++;
137599 assert( pCsr->pPragma );
137600 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
137601 rc = sqlite3_finalize(pCsr->pPragma);
137602 pCsr->pPragma = 0;
137617 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137626 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
137629 assert( j<ArraySize(pCsr->azArg) );
137630 assert( pCsr->azArg[j]==0 );
137632 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
137633 if( pCsr->azArg[j]==0 ){
137638 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
137640 if( pCsr->azArg[1] ){
137641 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
137643 sqlite3_str_appendall(&acc, pTab->pName->zName);
137644 if( pCsr->azArg[0] ){
137645 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
137649 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
137652 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
137663 return (pCsr->pPragma==0);
137675 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137676 if( i<pTab->iHidden ){
137677 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
137679 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
137689 *p = pCsr->iRowid;
137696 0, /* xCreate - create a table */
137697 pragmaVtabConnect, /* xConnect - connect to an existing table */
137698 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
137699 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
137700 0, /* xDestroy - Drop a table */
137701 pragmaVtabOpen, /* xOpen - open a cursor */
137702 pragmaVtabClose, /* xClose - close a cursor */
137703 pragmaVtabFilter, /* xFilter - configure scan constraints */
137704 pragmaVtabNext, /* xNext - advance a cursor */
137706 pragmaVtabColumn, /* xColumn - read data */
137707 pragmaVtabRowid, /* xRowid - read data */
137708 0, /* xUpdate - write data */
137709 0, /* xBegin - begin transaction */
137710 0, /* xSync - sync transaction */
137711 0, /* xCommit - commit transaction */
137712 0, /* xRollback - rollback transaction */
137713 0, /* xFindFunction - function overloading */
137714 0, /* xRename - rename the table */
137731 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
137732 assert( sqlite3HashFind(&db->aModule, zName)==0 );
137768 sqlite3 *db = pData->db;
137769 if( db->mallocFailed ){
137770 pData->rc = SQLITE_NOMEM_BKPT;
137771 }else if( pData->pzErrMsg[0]!=0 ){
137773 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
137779 *pData->pzErrMsg = sqlite3MPrintf(db,
137781 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
137784 pData->rc = SQLITE_ERROR;
137785 }else if( db->flags & SQLITE_WriteSchema ){
137786 pData->rc = SQLITE_CORRUPT_BKPT;
137791 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
137792 *pData->pzErrMsg = z;
137793 pData->rc = SQLITE_CORRUPT_BKPT;
137804 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
137805 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
137813 const char *zSql, /* UTF-8 encoded SQL statement. */
137816 Vdbe *pReprepare, /* VM being reprepared */
137838 sqlite3 *db = pData->db;
137839 int iDb = pData->iDb;
137843 assert( sqlite3_mutex_held(db->mutex) );
137844 db->mDbFlags |= DBFLAG_EncodingFixed;
137846 pData->nInitRow++;
137847 if( db->mallocFailed ){
137852 assert( iDb>=0 && iDb<db->nDb );
137859 ** But because db->init.busy is set to 1, no VDBE code is generated
137869 u8 saved_iDb = db->init.iDb;
137873 assert( db->init.busy );
137874 db->init.iDb = iDb;
137875 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
137876 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
137882 db->init.orphanTrigger = 0;
137883 db->init.azInit = (const char**)argv;
137885 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
137886 rc = db->errCode;
137888 db->init.iDb = saved_iDb;
137889 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
137891 if( db->init.orphanTrigger ){
137894 if( rc > pData->rc ) pData->rc = rc;
137902 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
137914 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
137918 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
137919 || pIndex->tnum<2
137920 || pIndex->tnum>pData->mxPage
137951 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
137953 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
137954 assert( iDb>=0 && iDb<db->nDb );
137955 assert( db->aDb[iDb].pSchema );
137956 assert( sqlite3_mutex_held(db->mutex) );
137957 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
137959 db->init.busy = 1;
137961 /* Construct the in-memory representation schema tables (sqlite_schema or
137965 ** the schema table as read-only. */
137981 db->mDbFlags &= mask;
137989 pDb = &db->aDb[iDb];
137990 if( pDb->pBt==0 ){
137997 /* If there is not already a read-only (or read-write) transaction opened
137998 ** on the b-tree database, open one now. If a transaction is opened, it
138000 sqlite3BtreeEnter(pDb->pBt);
138001 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
138002 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
138017 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
138028 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
138030 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
138033 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
138035 /* If opening a non-empty database, check the text encoding. For the
138040 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
138041 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
138045 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
138053 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
138061 pDb->pSchema->enc = ENC(db);
138063 if( pDb->pSchema->cache_size==0 ){
138065 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
138067 pDb->pSchema->cache_size = size;
138069 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
138071 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
138077 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
138080 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
138081 if( pDb->pSchema->file_format==0 ){
138082 pDb->pSchema->file_format = 1;
138084 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
138095 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
138096 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
138101 assert( db->init.busy );
138102 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
138107 db->aDb[iDb].zDbSName, zSchemaTabName);
138111 xAuth = db->xAuth;
138112 db->xAuth = 0;
138116 db->xAuth = xAuth;
138127 assert( pDb == &(db->aDb[iDb]) );
138128 if( db->mallocFailed ){
138131 pDb = &db->aDb[iDb];
138133 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
138154 sqlite3BtreeCommit(pDb->pBt);
138156 sqlite3BtreeLeave(pDb->pBt);
138165 db->init.busy = 0;
138170 ** Initialize all database files - the main database file, the file
138180 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
138182 assert( sqlite3_mutex_held(db->mutex) );
138183 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
138184 assert( db->init.busy==0 );
138186 assert( db->nDb>0 );
138193 for(i=db->nDb-1; i>0; i--){
138194 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
138207 ** This routine is a no-op if the database schema is already initialized.
138212 sqlite3 *db = pParse->db;
138213 assert( sqlite3_mutex_held(db->mutex) );
138214 if( !db->init.busy ){
138215 rc = sqlite3Init(db, &pParse->zErrMsg);
138217 pParse->rc = rc;
138218 pParse->nErr++;
138219 }else if( db->noSharedCache ){
138220 db->mDbFlags |= DBFLAG_SchemaKnownOk;
138229 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
138230 ** make no changes to pParse->rc.
138233 sqlite3 *db = pParse->db;
138238 assert( pParse->checkSchema );
138239 assert( sqlite3_mutex_held(db->mutex) );
138240 for(iDb=0; iDb<db->nDb; iDb++){
138242 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
138245 /* If there is not already a read-only (or read-write) transaction opened
138246 ** on the b-tree database, open one now. If a transaction is opened, it
138247 ** will be closed immediately after reading the meta-value. */
138252 pParse->rc = SQLITE_NOMEM;
138259 ** value stored as part of the in-memory schema representation,
138263 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
138265 pParse->rc = SQLITE_SCHEMA;
138277 ** which database file in db->aDb[] the schema refers to.
138283 int i = -32768;
138285 /* If pSchema is NULL, then return -32768. This happens when code in
138287 ** created by a sub-select). In this case the return value of this
138290 ** We return -32768 instead of the more usual -1 simply because using
138291 ** -32768 as the incorrect index into db->aDb[] is much
138292 ** more likely to cause a segfault than -1 (of course there are assert()
138294 ** -32768 will still fit into a 16-bit signed integer.
138296 assert( sqlite3_mutex_held(db->mutex) );
138299 assert( i<db->nDb );
138300 if( db->aDb[i].pSchema==pSchema ){
138304 assert( i>=0 && i<db->nDb );
138313 sqlite3 *db = pParse->db;
138315 assert( db->pParse==pParse );
138316 assert( pParse->nested==0 );
138318 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
138320 while( pParse->pCleanup ){
138321 ParseCleanup *pCleanup = pParse->pCleanup;
138322 pParse->pCleanup = pCleanup->pNext;
138323 pCleanup->xCleanup(db, pCleanup->pPtr);
138326 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
138327 if( pParse->pConstExpr ){
138328 sqlite3ExprListDelete(db, pParse->pConstExpr);
138330 assert( db->lookaside.bDisable >= pParse->disableLookaside );
138331 db->lookaside.bDisable -= pParse->disableLookaside;
138332 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
138333 assert( pParse->db->pParse==pParse );
138334 db->pParse = pParse->pOuterParse;
138335 pParse->db = 0;
138336 pParse->disableLookaside = 0;
138347 ** common cleanups, we save a single NULL-pointer comparison in
138352 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
138354 ** use-after-free errors following an OOM. The preferred way to do this is
138357 ** testcase( pParse->earlyCleanup );
138372 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
138374 pCleanup->pNext = pParse->pCleanup;
138375 pParse->pCleanup = pCleanup;
138376 pCleanup->pPtr = pPtr;
138377 pCleanup->xCleanup = xCleanup;
138379 xCleanup(pParse->db, pPtr);
138382 pParse->earlyCleanup = 1;
138400 assert( db->pParse!=pParse );
138401 pParse->pOuterParse = db->pParse;
138402 db->pParse = pParse;
138403 pParse->db = db;
138404 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
138416 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
138420 const char *zSql, /* UTF-8 encoded SQL statement. */
138423 Vdbe *pReprepare, /* VM being reprepared */
138434 sParse.pOuterParse = db->pParse;
138435 db->pParse = &sParse;
138439 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
138440 assert( sqlite3_mutex_held(db->mutex) );
138442 /* For a long-term use prepared statement avoid the use of
138453 ** some other database connection is holding a write-lock, which in
138474 if( !db->noSharedCache ){
138475 for(i=0; i<db->nDb; i++) {
138476 Btree *pBt = db->aDb[i].pBt;
138481 const char *zDb = db->aDb[i].zDbSName;
138483 testcase( db->flags & SQLITE_ReadUncommit );
138491 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
138494 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
138496 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
138507 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
138521 if( db->init.busy==0 ){
138522 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
138524 if( db->mallocFailed ){
138529 if( sParse.checkSchema && db->init.busy==0 ){
138554 sParse.pTriggerPrg = pT->pNext;
138565 const char *zSql, /* UTF-8 encoded SQL statement. */
138568 Vdbe *pOld, /* VM being reprepared */
138582 sqlite3_mutex_enter(db->mutex);
138590 if( rc==SQLITE_OK || db->mallocFailed ) break;
138592 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
138595 assert( (rc&db->errMask)==rc );
138596 db->busyHandler.nBusy = 0;
138597 sqlite3_mutex_leave(db->mutex);
138617 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
138621 assert( sqlite3_mutex_held(db->mutex) );
138623 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
138651 const char *zSql, /* UTF-8 encoded SQL statement. */
138663 const char *zSql, /* UTF-8 encoded SQL statement. */
138669 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
138681 const char *zSql, /* UTF-8 encoded SQL statement. */
138688 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
138705 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
138709 const void *zSql, /* UTF-16 encoded SQL statement. */
138715 /* This function currently works by first transforming the UTF-16
138716 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
138736 sqlite3_mutex_enter(db->mutex);
138739 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
138744 ** equivalent pointer into the UTF-16 string by counting the unicode
138746 ** the same number of characters into the UTF-16 string.
138748 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
138753 sqlite3_mutex_leave(db->mutex);
138767 const void *zSql, /* UTF-16 encoded SQL statement. */
138779 const void *zSql, /* UTF-16 encoded SQL statement. */
138791 const void *zSql, /* UTF-16 encoded SQL statement. */
138842 ** The aDefer[] array is used by the sorter-references optimization. For
138852 ** When the sorter-reference optimization is used, there is one entry in the
138861 int regReturn; /* Register holding block-output return address */
138862 int labelBkOut; /* Start label for the block-output subroutine */
138889 Select *pPrior = p->pPrior;
138890 sqlite3ExprListDelete(db, p->pEList);
138891 sqlite3SrcListDelete(db, p->pSrc);
138892 sqlite3ExprDelete(db, p->pWhere);
138893 sqlite3ExprListDelete(db, p->pGroupBy);
138894 sqlite3ExprDelete(db, p->pHaving);
138895 sqlite3ExprListDelete(db, p->pOrderBy);
138896 sqlite3ExprDelete(db, p->pLimit);
138897 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
138899 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
138900 sqlite3WindowListDelete(db, p->pWinDefn);
138902 while( p->pWin ){
138903 assert( p->pWin->ppThis==&p->pWin );
138904 sqlite3WindowUnlinkFromSelect(p->pWin);
138917 pDest->eDest = (u8)eDest;
138918 pDest->iSDParm = iParm;
138919 pDest->iSDParm2 = 0;
138920 pDest->zAffSdst = 0;
138921 pDest->iSdst = 0;
138922 pDest->nSdst = 0;
138933 SrcList *pSrc, /* the FROM clause -- which tables to scan */
138943 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
138945 assert( pParse->db->mallocFailed );
138950 sqlite3Expr(pParse->db,TK_ASTERISK,0));
138952 pNew->pEList = pEList;
138953 pNew->op = TK_SELECT;
138954 pNew->selFlags = selFlags;
138955 pNew->iLimit = 0;
138956 pNew->iOffset = 0;
138957 pNew->selId = ++pParse->nSelect;
138958 pNew->addrOpenEphm[0] = -1;
138959 pNew->addrOpenEphm[1] = -1;
138960 pNew->nSelectRow = 0;
138961 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
138962 pNew->pSrc = pSrc;
138963 pNew->pWhere = pWhere;
138964 pNew->pGroupBy = pGroupBy;
138965 pNew->pHaving = pHaving;
138966 pNew->pOrderBy = pOrderBy;
138967 pNew->pPrior = 0;
138968 pNew->pNext = 0;
138969 pNew->pLimit = pLimit;
138970 pNew->pWith = 0;
138972 pNew->pWin = 0;
138973 pNew->pWinDefn = 0;
138975 if( pParse->db->mallocFailed ) {
138976 clearSelect(pParse->db, pNew, pNew!=&standin);
138979 assert( pNew->pSrc!=0 || pParse->nErr>0 );
138993 ** Return a pointer to the right-most SELECT statement in a compound.
138996 while( p->pNext ) p = p->pNext;
139021 ** ------- ----- ----- ------------
139022 ** CROSS - - JT_CROSS
139023 ** INNER - - JT_INNER
139024 ** LEFT - - JT_LEFT|JT_OUTER
139025 ** LEFT OUTER - JT_LEFT|JT_OUTER
139026 ** RIGHT - - JT_RIGHT|JT_OUTER
139027 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
139028 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
139029 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
139030 ** NATURAL INNER - JT_NATURAL|JT_INNER
139031 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
139033 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
139035 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
139039 ** of other non-standard and in many cases non-sensical join types.
139044 ** INNER CROSS JOIN -> same as JOIN
139045 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
139046 ** OUTER LEFT JOIN -> same as LEFT JOIN
139047 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
139048 ** LEFT RIGHT JOIN -> same as FULL JOIN
139049 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
139050 ** CROSS CROSS CROSS JOIN -> same as JOIN
139089 if( p->n==aKeyword[j].nChar
139090 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
139118 ** Return the index of a column in a table. Return -1 if the column
139125 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
139126 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
139128 return -1;
139136 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
139137 if( pItem->fg.isNestedFrom ){
139139 assert( pItem->pSelect!=0 );
139140 pResults = pItem->pSelect->pEList;
139142 assert( iCol>=0 && iCol<pResults->nExpr );
139143 pResults->a[iCol].fg.bUsed = 1;
139149 ** table that has a column named zCol. The search is left-to-right.
139159 int iStart, /* First member of pSrc->a[] to check */
139160 int iEnd, /* Last member of pSrc->a[] to check */
139162 int *piTab, /* Write index of pSrc->a[] here */
139163 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
139169 assert( iEnd<pSrc->nSrc );
139174 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
139176 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
139179 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
139221 p->w.iJoin = iTable;
139222 if( p->op==TK_FUNCTION ){
139224 if( p->x.pList ){
139226 for(i=0; i<p->x.pList->nExpr; i++){
139227 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
139231 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
139232 p = p->pRight;
139252 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
139256 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
139259 if( p->op==TK_FUNCTION ){
139261 if( p->x.pList ){
139263 for(i=0; i<p->x.pList->nExpr; i++){
139264 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
139268 unsetJoinExpr(p->pLeft, iTable, nullable);
139269 p = p->pRight;
139286 ** The left most table is the first entry in Select.pSrc. The right-most
139300 pSrc = p->pSrc;
139301 pLeft = &pSrc->a[0];
139303 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
139304 Table *pRightTab = pRight->pTab;
139307 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
139308 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
139313 if( pRight->fg.jointype & JT_NATURAL ){
139315 if( pRight->fg.isUsing || pRight->u3.pOn ){
139320 for(j=0; j<pRightTab->nCol; j++){
139323 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
139324 zName = pRightTab->aCol[j].zCnName;
139328 assert( pUsing->nId>0 );
139329 assert( pUsing->a[pUsing->nId-1].zName==0 );
139330 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
139335 pRight->fg.isUsing = 1;
139336 pRight->fg.isSynthUsing = 1;
139337 pRight->u3.pUsing = pUsing;
139339 if( pParse->nErr ) return 1;
139349 if( pRight->fg.isUsing ){
139350 IdList *pList = pRight->u3.pUsing;
139351 sqlite3 *db = pParse->db;
139353 for(j=0; j<pList->nId; j++){
139362 zName = pList->a[j].zName;
139366 pRight->fg.isSynthUsing)==0
139368 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
139373 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
139374 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139377 ** contains the zName column, then this branch is a no-op.
139384 ** non-USING references to zName on the left of an INNER or LEFT
139391 pRight->fg.isSynthUsing)!=0 ){
139392 if( pSrc->a[iLeft].fg.isUsing==0
139393 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
139401 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
139416 pEq->w.iJoin = pE2->iTable;
139418 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
139425 else if( pRight->u3.pOn ){
139426 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
139427 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
139428 pRight->u3.pOn = 0;
139429 pRight->fg.isOn = 1;
139458 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
139459 0, pInfo->ecelFlags);
139461 if( pInfo->pExtra ){
139462 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
139463 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
139481 int nOBSat = pSort->nOBSat;
139482 Vdbe *v = pParse->pVdbe;
139483 int regOut = ++pParse->nMem;
139484 if( pSort->pDeferredRowLoad ){
139485 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
139487 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
139493 ** through regData+nData-1 onto the sorter.
139504 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
139505 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
139506 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
139510 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
139534 regBase = regData - nPrefixReg;
139536 regBase = pParse->nMem + 1;
139537 pParse->nMem += nBase;
139539 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
139540 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
139541 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
139542 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
139545 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
139559 regPrevKey = pParse->nMem+1;
139560 pParse->nMem += pSort->nOBSat;
139561 nKey = nExpr - pSort->nOBSat + bSeq;
139565 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
139568 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
139569 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
139570 if( pParse->db->mallocFailed ) return;
139571 pOp->p2 = nKey + nData;
139572 pKI = pOp->p4.pKeyInfo;
139573 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
139574 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
139575 testcase( pKI->nAllField > pKI->nKeyField+2 );
139576 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
139577 pKI->nAllField-pKI->nKeyField-1);
139581 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
139582 pSort->regReturn = ++pParse->nMem;
139583 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
139584 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
139586 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
139590 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
139604 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
139609 int iCsr = pSort->iECursor;
139614 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
139621 if( pSort->sortFlags & SORTFLAG_UseSorter ){
139626 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
139627 regBase+nOBSat, nBase-nOBSat);
139630 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
139638 Vdbe *v, /* Generate code into this VM */
139660 ** be opened before the VM code generated by this routine is executed.
139663 ** jump to VM address addrRepeat. Otherwise, insert a new record into
139672 ** is not distinct and control jumps to VM address addrRepeat. Otherwise,
139673 ** the VM program proceeds with processing the new row.
139700 int nResultCol = pEList->nExpr;
139701 Vdbe *v = pParse->pVdbe;
139710 iRet = regPrev = pParse->nMem+1;
139711 pParse->nMem += nResultCol;
139715 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
139716 if( i<nResultCol-1 ){
139723 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
139726 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
139727 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
139762 ** No adjustments necessary. This function is a no-op.
139781 if( pParse->nErr==0
139784 Vdbe *v = pParse->pVdbe;
139786 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
139795 pOp->opcode = OP_Null;
139796 pOp->p1 = 1;
139797 pOp->p2 = iVal;
139804 ** This function is called as part of inner-loop generation for a SELECT
139806 ** determines the expressions, if any, that the sorter-reference
139807 ** optimization should be used for. The sorter-reference optimization
139820 ** for which the sorter-reference optimization should be enabled.
139821 ** Additionally, the pSort->aDefer[] array is populated with entries
139836 for(i=0; i<pEList->nExpr; i++){
139837 struct ExprList_item *pItem = &pEList->a[i];
139838 if( pItem->u.x.iOrderByCol==0 ){
139839 Expr *pExpr = pItem->pExpr;
139841 if( pExpr->op==TK_COLUMN
139842 && pExpr->iColumn>=0
139844 && (pTab = pExpr->y.pTab)!=0
139846 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
139850 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
139853 if( nDefer==ArraySize(pSort->aDefer) ){
139861 nKey = pPk->nKeyCol;
139866 pNew->iTable = pExpr->iTable;
139868 pNew->y.pTab = pExpr->y.pTab;
139869 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
139873 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
139874 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
139875 pSort->aDefer[nDefer].nKey = nKey;
139879 pItem->fg.bSorterRef = 1;
139883 pSort->nDefer = (u8)nDefer;
139892 ** If srcTab is negative, then the p->pEList expressions
139894 ** zero or more, then data is pulled from srcTab and p->pEList is used only
139900 int srcTab, /* Pull data from this table if non-negative */
139907 Vdbe *v = pParse->pVdbe;
139910 int eDest = pDest->eDest; /* How to dispose of results */
139911 int iParm = pDest->iSDParm; /* First argument to disposal method */
139919 ** values for any expressions that are also part of the sort-key are omitted
139925 assert( p->pEList!=0 );
139926 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
139927 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
139930 if( hasDistinct && (pDistinct->eTnctType==WHERE_DISTINCT_UNIQUE) ){
139932 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
139938 codeOffset(v, p->iOffset, iContinue);
139941 if( p->iLimit ){
139942 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak);
139953 nResultCol = p->pEList->nExpr;
139955 if( pDest->iSdst==0 ){
139957 nPrefixReg = pSort->pOrderBy->nExpr;
139958 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
139959 pParse->nMem += nPrefixReg;
139961 pDest->iSdst = pParse->nMem+1;
139962 pParse->nMem += nResultCol;
139963 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
139965 ** on the right-hand side of an INSERT contains more result columns than
139969 pParse->nMem += nResultCol;
139971 pDest->nSdst = nResultCol;
139972 regOrig = regResult = pDest->iSdst;
139976 VdbeComment((v, "%s", p->pEList->a[i].zEName));
139993 /* For each expression in p->pEList that is a copy of an expression in
139994 ** the ORDER BY clause (pSort->pOrderBy), set the associated
139996 ** expression within the sort-key that pushOntoSorter() will generate.
139997 ** This allows the p->pEList field to be omitted from the sorted record,
140001 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
140003 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
140004 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
140008 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
140009 if( pExtra && pParse->db->mallocFailed==0 ){
140015 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
140016 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
140017 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
140018 pParse->nMem += pExtra->nExpr;
140024 pEList = p->pEList;
140025 for(i=0; i<pEList->nExpr; i++){
140026 if( pEList->a[i].u.x.iOrderByCol>0
140028 || pEList->a[i].fg.bSorterRef
140031 nResultCol--;
140050 if( pExtra ) nResultCol += pExtra->nExpr;
140052 if( p->iLimit
140058 pSort->pDeferredRowLoad = &sRowLoadInfo;
140070 int eType = pDistinct->eTnctType;
140071 int iTab = pDistinct->tabTnct;
140072 assert( nResultCol==p->pEList->nExpr );
140073 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
140074 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
140076 codeOffset(v, p->iOffset, iContinue);
140079 if( p->iLimit ){
140080 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak);
140126 if( pDest->zAffSdst ){
140127 sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
140162 int i2 = pDest->iSDParm2;
140171 regResult+(i2<0), nResultCol-(i2<0), r1);
140196 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
140198 r1, pDest->zAffSdst, nResultCol);
140220 assert( nResultCol<=pDest->nSdst );
140224 assert( nResultCol==pDest->nSdst );
140232 case SRT_Coroutine: /* Send data to a co-routine */
140240 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
140249 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
140250 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
140251 ** pSO->nExpr columns, then make sure all keys are unique by adding a
140260 pSO = pDest->pOrderBy;
140262 nKey = pSO->nExpr;
140281 regResult + pSO->a[i].u.x.iOrderByCol - 1,
140299 ** user-defined functions that have side effects. We do not care
140313 if( pSort==0 && p->iLimit ){
140314 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
140323 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
140326 p->aSortFlags = (u8*)&p->aColl[N+X];
140327 p->nKeyField = (u16)N;
140328 p->nAllField = (u16)(N+X);
140329 p->enc = ENC(db);
140330 p->db = db;
140331 p->nRef = 1;
140344 assert( p->db!=0 );
140345 assert( p->nRef>0 );
140346 p->nRef--;
140347 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
140356 assert( p->nRef>0 );
140357 p->nRef++;
140369 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
140395 sqlite3 *db = pParse->db;
140398 nExpr = pList->nExpr;
140399 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
140402 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
140403 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
140404 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
140427 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
140430 ** "USE TEMP B-TREE FOR xxx"
140436 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
140440 ** Assign expression b to lvalue a. A second, no-op, version of this macro
140449 /* No-op versions of the explainXXX() functions and macros. */
140456 ** If the inner loop was generated using a non-null pOrderBy argument,
140468 Vdbe *v = pParse->pVdbe; /* The prepared statement */
140469 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
140474 ExprList *pOrderBy = pSort->pOrderBy;
140475 int eDest = pDest->eDest;
140476 int iParm = pDest->iSDParm;
140485 struct ExprList_item *aOutEx = p->pEList->a;
140488 if( pSort->labelBkOut ){
140489 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
140491 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
140495 /* Open any cursors needed for sorter-reference expressions */
140496 for(i=0; i<pSort->nDefer; i++){
140497 Table *pTab = pSort->aDefer[i].pTab;
140498 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
140499 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
140500 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
140504 iTab = pSort->iECursor;
140506 if( eDest==SRT_Mem && p->iOffset ){
140507 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
140510 regRow = pDest->iSdst;
140520 nKey = pOrderBy->nExpr - pSort->nOBSat;
140521 if( pSort->sortFlags & SORTFLAG_UseSorter ){
140522 int regSortOut = ++pParse->nMem;
140523 iSortTab = pParse->nTab++;
140524 if( pSort->labelBkOut ){
140532 assert( p->iLimit==0 && p->iOffset==0 );
140543 codeOffset(v, p->iOffset, addrContinue);
140552 if( p->iOffset>0 ){
140553 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
140556 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
140563 if( pSort->nDefer ){
140567 for(i=0; i<pSort->nDefer; i++){
140568 int iCsr = pSort->aDefer[i].iCsr;
140569 Table *pTab = pSort->aDefer[i].pTab;
140570 int nKey = pSort->aDefer[i].nKey;
140580 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
140593 for(i=nColumn-1; i>=0; i--){
140602 iRead = aOutEx[i].u.x.iOrderByCol-1;
140604 iRead = iCol--;
140621 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
140623 pDest->zAffSdst, nColumn);
140633 int i2 = pDest->iSDParm2;
140635 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
140648 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
140650 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
140666 if( pSort->sortFlags & SORTFLAG_UseSorter ){
140671 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
140683 ** result-set expression in all of the following SELECT statements is
140694 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
140721 assert( pNC->pSrcList!=0 );
140722 switch( pExpr->op ){
140730 int iCol = pExpr->iColumn; /* Index of column in pTab */
140732 SrcList *pTabList = pNC->pSrcList;
140733 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
140734 if( j<pTabList->nSrc ){
140735 pTab = pTabList->a[j].pTab;
140736 pS = pTabList->a[j].pSelect;
140738 pNC = pNC->pNext;
140753 ** sub-select. In this case, set the column type to NULL, even
140763 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
140765 /* The "table" is actually a sub-select or a view in the FROM clause
140767 ** data for the result-set column of the sub-select.
140769 if( iCol<pS->pEList->nExpr
140777 ** rowid of the sub-select or view. This expression is legal (see
140778 ** test case misc2.2.2) - it always evaluates to NULL.
140781 Expr *p = pS->pEList->a[iCol].pExpr;
140782 sNC.pSrcList = pS->pSrc;
140784 sNC.pParse = pNC->pParse;
140791 if( iCol<0 ) iCol = pTab->iPKey;
140792 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140797 zOrigCol = pTab->aCol[iCol].zCnName;
140798 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
140800 zOrigTab = pTab->zName;
140801 if( pNC->pParse && pTab->pSchema ){
140802 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
140803 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
140806 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140810 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
140818 /* The expression is a sub-select. Return the declaration type and
140826 pS = pExpr->x.pSelect;
140827 p = pS->pEList->a[0].pExpr;
140828 sNC.pSrcList = pS->pSrc;
140830 sNC.pParse = pNC->pParse;
140858 Vdbe *v = pParse->pVdbe;
140864 for(i=0; i<pEList->nExpr; i++){
140865 Expr *p = pEList->a[i].pExpr;
140873 /* The vdbe must make its own copy of the column-type and other
140923 Vdbe *v = pParse->pVdbe;
140928 sqlite3 *db = pParse->db;
140934 if( pParse->explain ){
140939 if( pParse->colNamesSet ) return;
140940 /* Column names are determined by the left-most term of a compound select */
140941 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
140943 pTabList = pSelect->pSrc;
140944 pEList = pSelect->pEList;
140947 pParse->colNamesSet = 1;
140948 fullName = (db->flags & SQLITE_FullColNames)!=0;
140949 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
140950 sqlite3VdbeSetNumCols(v, pEList->nExpr);
140951 for(i=0; i<pEList->nExpr; i++){
140952 Expr *p = pEList->a[i].pExpr;
140955 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
140956 assert( p->op!=TK_COLUMN
140957 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
140958 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
140960 char *zName = pEList->a[i].zEName;
140962 }else if( srcName && p->op==TK_COLUMN ){
140964 int iCol = p->iColumn;
140965 pTab = p->y.pTab;
140967 if( iCol<0 ) iCol = pTab->iPKey;
140968 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
140972 zCol = pTab->aCol[iCol].zCnName;
140976 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
140982 const char *z = pEList->a[i].zEName;
141018 sqlite3 *db = pParse->db; /* Database connection */
141030 nCol = pEList->nExpr;
141042 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
141043 struct ExprList_item *pX = &pEList->a[i];
141047 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
141050 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
141051 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
141052 pColExpr = pColExpr->pRight;
141055 if( pColExpr->op==TK_COLUMN
141057 && ALWAYS( pColExpr->y.pTab!=0 )
141060 int iCol = pColExpr->iColumn;
141061 pTab = pColExpr->y.pTab;
141062 if( iCol<0 ) iCol = pTab->iPKey;
141063 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
141064 }else if( pColExpr->op==TK_ID ){
141066 zName = pColExpr->u.zToken;
141069 assert( zName==pX->zEName ); /* pointer comparison intended */
141083 if( pCollide->fg.bUsingTerm ){
141084 pCol->colFlags |= COLFLAG_NOEXPAND;
141088 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
141094 pCol->zCnName = zName;
141095 pCol->hName = sqlite3StrIHash(zName);
141096 if( pX->fg.bNoExpand ){
141097 pCol->colFlags |= COLFLAG_NOEXPAND;
141105 if( db->mallocFailed ){
141134 sqlite3 *db = pParse->db;
141143 assert( (pSelect->selFlags & SF_Resolved)!=0 );
141144 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
141145 if( db->mallocFailed ) return;
141147 sNC.pSrcList = pSelect->pSrc;
141148 a = pSelect->pEList->a;
141149 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
141152 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
141155 /* pCol->szEst = ... // Column size est for SELECT tables never used */
141156 pCol->affinity = sqlite3ExprAffinity(p);
141159 n = sqlite3Strlen30(pCol->zCnName);
141160 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
141161 if( pCol->zCnName ){
141162 memcpy(&pCol->zCnName[n+1], zType, m+1);
141163 pCol->colFlags |= COLFLAG_HASTYPE;
141165 testcase( pCol->colFlags & COLFLAG_HASTYPE );
141166 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
141169 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
141172 assert( pTab->pIndex==0 );
141173 sqlite3ColumnSetColl(db, pCol, pColl->zName);
141176 pTab->szTabRow = 1; /* Any non-zero value works */
141185 sqlite3 *db = pParse->db;
141188 savedFlags = db->flags;
141189 db->flags &= ~(u64)SQLITE_FullColNames;
141190 db->flags |= SQLITE_ShortColNames;
141192 db->flags = savedFlags;
141193 if( pParse->nErr ) return 0;
141194 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
141199 pTab->nTabRef = 1;
141200 pTab->zName = 0;
141201 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141202 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
141204 pTab->iPKey = -1;
141205 if( db->mallocFailed ){
141217 if( pParse->pVdbe ){
141218 return pParse->pVdbe;
141220 if( pParse->pToplevel==0
141221 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
141223 pParse->okConstFactor = 1;
141231 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
141239 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
141247 ** Only if pLimit->pLeft!=0 do the limit registers get
141257 Expr *pLimit = p->pLimit;
141259 if( p->iLimit ) return;
141262 ** "LIMIT -1" always shows all rows. There is some
141268 assert( pLimit->op==TK_LIMIT );
141269 assert( pLimit->pLeft!=0 );
141270 p->iLimit = iLimit = ++pParse->nMem;
141273 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
141278 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
141279 p->nSelectRow = sqlite3LogEst((u64)n);
141280 p->selFlags |= SF_FixedLimit;
141283 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
141288 if( pLimit->pRight ){
141289 p->iOffset = iOffset = ++pParse->nMem;
141290 pParse->nMem++; /* Allocate an extra register for limit+offset */
141291 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
141302 ** Return the appropriate collating sequence for the iCol-th column of
141303 ** the result set for the compound-select statement "p". Return NULL if
141307 ** left-most term of the select that has a collating sequence.
141311 if( p->pPrior ){
141312 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
141317 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
141320 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
141321 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
141336 ExprList *pOrderBy = p->pOrderBy;
141337 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
141338 sqlite3 *db = pParse->db;
141343 struct ExprList_item *pItem = &pOrderBy->a[i];
141344 Expr *pTerm = pItem->pExpr;
141347 if( pTerm->flags & EP_Collate ){
141350 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
141351 if( pColl==0 ) pColl = db->pDfltColl;
141352 pOrderBy->a[i].pExpr =
141353 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
141356 pRet->aColl[i] = pColl;
141357 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
141369 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
141371 ** p->pPrior p
141374 ** There is exactly one reference to the recursive-table in the FROM clause
141375 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
141377 ** The setup-query runs once to generate an initial set of rows that go
141381 ** recursive-table for a recursive-query run. The output of the recursive-query
141406 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
141407 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
141408 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
141410 Select *pFirstRec; /* Left-most recursive term */
141426 if( p->pWin ){
141437 p->nSelectRow = 320; /* 4 billion rows */
141439 pLimit = p->pLimit;
141440 regLimit = p->iLimit;
141441 regOffset = p->iOffset;
141442 p->pLimit = 0;
141443 p->iLimit = p->iOffset = 0;
141444 pOrderBy = p->pOrderBy;
141447 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
141448 if( pSrc->a[i].fg.isRecursive ){
141449 iCurrent = pSrc->a[i].iCursor;
141457 iQueue = pParse->nTab++;
141458 if( p->op==TK_UNION ){
141460 iDistinct = pParse->nTab++;
141467 regCurrent = ++pParse->nMem;
141471 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
141479 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
141480 p->selFlags |= SF_UsesEphemeral;
141484 p->pOrderBy = 0;
141490 ** iDistinct table. pFirstRec is left pointing to the left-most
141493 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
141494 if( pFirstRec->selFlags & SF_Aggregate ){
141498 pFirstRec->op = TK_ALL;
141499 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
141502 /* Store the results of the setup-query in Queue. */
141503 pSetup = pFirstRec->pPrior;
141504 pSetup->pNext = 0;
141507 pSetup->pNext = p;
141516 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
141534 ** the value for the recursive-table. Store the results in the Queue.
141536 pFirstRec->pPrior = 0;
141539 assert( pFirstRec->pPrior==0 );
141540 pFirstRec->pPrior = pSetup;
141547 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
141548 p->pOrderBy = pOrderBy;
141549 p->pLimit = pLimit;
141557 Select *p, /* The right-most of SELECTs to be coded */
141562 ** Handle the special case of a compound-select that originates from a
141575 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
141579 Select *p, /* The right-most of SELECTs to be coded */
141584 int bShowAll = p->pLimit==0;
141585 assert( p->selFlags & SF_MultiValue );
141587 assert( p->selFlags & SF_Values );
141588 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
141589 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
141591 if( p->pWin ) return -1;
141593 if( p->pPrior==0 ) break;
141594 assert( p->pPrior->pNext==p );
141595 p = p->pPrior;
141601 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
141603 p->nSelectRow = nRow;
141604 p = p->pNext;
141615 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
141624 ** "p" points to the right-most of the two queries. the query on the
141625 ** left is p->pPrior. The left query could also be a compound query
141631 ** Example 1: Consider a three-way compound SQL statement.
141639 ** `-----> SELECT b FROM t2
141641 ** `------> SELECT a FROM t1
141645 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
141652 Select *p, /* The right-most of SELECTs to be coded */
141663 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
141665 assert( p && p->pPrior ); /* Calling function guarantees this much */
141666 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
141667 assert( p->selFlags & SF_Compound );
141668 db = pParse->db;
141669 pPrior = p->pPrior;
141671 assert( pPrior->pOrderBy==0 );
141672 assert( pPrior->pLimit==0 );
141680 assert( p->pEList );
141681 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
141685 /* Special handling for a compound-select that originates as a VALUES clause.
141687 if( p->selFlags & SF_MultiValue ){
141696 assert( p->pEList && pPrior->pEList );
141697 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
141700 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
141707 if( p->pOrderBy ){
141712 if( pPrior->pPrior==0 ){
141714 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
141720 switch( p->op ){
141724 assert( !pPrior->pLimit );
141725 pPrior->iLimit = p->iLimit;
141726 pPrior->iOffset = p->iOffset;
141727 pPrior->pLimit = p->pLimit;
141730 pPrior->pLimit = 0;
141734 p->pPrior = 0;
141735 p->iLimit = pPrior->iLimit;
141736 p->iOffset = pPrior->iOffset;
141737 if( p->iLimit ){
141738 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
141740 if( p->iOffset ){
141742 p->iLimit, p->iOffset+1, p->iOffset);
141749 pDelete = p->pPrior;
141750 p->pPrior = pPrior;
141751 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141752 if( p->pLimit
141753 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
141754 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
141756 p->nSelectRow = sqlite3LogEst((u64)nLimit);
141768 Expr *pLimit; /* Saved values of p->nLimit */
141772 testcase( p->op==TK_EXCEPT );
141773 testcase( p->op==TK_UNION );
141779 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
141785 unionTab = pParse->nTab++;
141786 assert( p->pOrderBy==0 );
141788 assert( p->addrOpenEphm[0] == -1 );
141789 p->addrOpenEphm[0] = addr;
141790 findRightmost(p)->selFlags |= SF_UsesEphemeral;
141791 assert( p->pEList );
141797 assert( !pPrior->pOrderBy );
141807 if( p->op==TK_EXCEPT ){
141810 assert( p->op==TK_UNION );
141813 p->pPrior = 0;
141814 pLimit = p->pLimit;
141815 p->pLimit = 0;
141817 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
141818 sqlite3SelectOpName(p->op)));
141822 assert( p->pOrderBy==0 );
141823 pDelete = p->pPrior;
141824 p->pPrior = pPrior;
141825 p->pOrderBy = 0;
141826 if( p->op==TK_UNION ){
141827 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141829 sqlite3ExprDelete(db, p->pLimit);
141830 p->pLimit = pLimit;
141831 p->iLimit = 0;
141832 p->iOffset = 0;
141838 assert( p->pEList || db->mallocFailed );
141839 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
141855 default: assert( p->op==TK_INTERSECT ); {
141867 tab1 = pParse->nTab++;
141868 tab2 = pParse->nTab++;
141869 assert( p->pOrderBy==0 );
141872 assert( p->addrOpenEphm[0] == -1 );
141873 p->addrOpenEphm[0] = addr;
141874 findRightmost(p)->selFlags |= SF_UsesEphemeral;
141875 assert( p->pEList );
141889 assert( p->addrOpenEphm[1] == -1 );
141890 p->addrOpenEphm[1] = addr;
141891 p->pPrior = 0;
141892 pLimit = p->pLimit;
141893 p->pLimit = 0;
141895 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
141896 sqlite3SelectOpName(p->op)));
141900 pDelete = p->pPrior;
141901 p->pPrior = pPrior;
141902 if( p->nSelectRow>pPrior->nSelectRow ){
141903 p->nSelectRow = pPrior->nSelectRow;
141905 sqlite3ExprDelete(db, p->pLimit);
141906 p->pLimit = pLimit;
141912 assert( p->pEList );
141934 if( p->pNext==0 ){
141939 if( pParse->nErr ) goto multi_select_end;
141945 ** This section is run by the right-most SELECT statement only.
141946 ** SELECT statements to the left always skip this part. The right-most
141950 if( p->selFlags & SF_UsesEphemeral ){
141954 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
141957 assert( p->pNext==0 );
141958 assert( p->pEList!=0 );
141959 nCol = p->pEList->nExpr;
141965 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
141968 *apColl = db->pDfltColl;
141972 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
141974 int addr = pLoop->addrOpenEphm[i];
141978 assert( pLoop->addrOpenEphm[1]<0 );
141984 pLoop->addrOpenEphm[i] = -1;
141991 pDest->iSdst = dest.iSdst;
141992 pDest->nSdst = dest.nSdst;
142007 if( p->selFlags & SF_Values ){
142012 sqlite3SelectOpName(p->op));
142020 ** The data to be output is contained in pIn->iSdst. There are
142021 ** pIn->nSdst columns to be output. pDest is where the output should
142033 ** If the LIMIT found in p->iLimit is reached, jump immediately to
142046 Vdbe *v = pParse->pVdbe;
142058 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
142062 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
142065 if( pParse->db->mallocFailed ) return 0;
142069 codeOffset(v, p->iOffset, iContinue);
142071 assert( pDest->eDest!=SRT_Exists );
142072 assert( pDest->eDest!=SRT_Table );
142073 switch( pDest->eDest ){
142079 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
142080 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
142081 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
142093 testcase( pIn->nSdst>1 );
142095 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
142096 r1, pDest->zAffSdst, pIn->nSdst);
142097 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
142098 pIn->iSdst, pIn->nSdst);
142106 ** if it is the RHS of a row-value IN operator.
142109 testcase( pIn->nSdst>1 );
142110 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
142117 ** starting at pDest->iSdst. Then the co-routine yields.
142120 if( pDest->iSdst==0 ){
142121 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
142122 pDest->nSdst = pIn->nSdst;
142124 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
142125 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
142138 assert( pDest->eDest==SRT_Output );
142139 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
142146 if( p->iLimit ){
142147 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
142168 ** co-routines. Then run the co-routines in parallel and merge the results
142195 ** ------------- ----------------- -------------- -----------------
142246 Select *p, /* The right-most of SELECTs to be coded */
142251 Select *pSplit; /* Left-most SELECT in the right-hand group */
142256 int regAddrA; /* Address register for select-A coroutine */
142257 int regAddrB; /* Address register for select-B coroutine */
142258 int addrSelectA; /* Address of the select-A coroutine */
142259 int addrSelectB; /* Address of the select-B coroutine */
142260 int regOutA; /* Address register for the output-A subroutine */
142261 int regOutB; /* Address register for the output-B subroutine */
142262 int addrOutA; /* Address of the output-A subroutine */
142263 int addrOutB = 0; /* Address of the output-B subroutine */
142264 int addrEofA; /* Address of the select-A-exhausted subroutine */
142266 int addrEofB; /* Address of the select-B-exhausted subroutine */
142270 int regLimitA; /* Limit register for select-A */
142271 int regLimitB; /* Limit register for select-A */
142273 int savedLimit; /* Saved value of p->iLimit */
142274 int savedOffset; /* Saved value of p->iOffset */
142286 assert( p->pOrderBy!=0 );
142288 db = pParse->db;
142289 v = pParse->pVdbe;
142297 op = p->op;
142298 assert( p->pPrior->pOrderBy==0 );
142299 pOrderBy = p->pOrderBy;
142301 nOrderBy = pOrderBy->nExpr;
142308 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
142310 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
142312 assert( pItem->u.x.iOrderByCol>0 );
142313 if( pItem->u.x.iOrderByCol==i ) break;
142318 pNew->flags |= EP_IntValue;
142319 pNew->u.iValue = i;
142320 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
142321 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
142337 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
142339 assert( pItem->u.x.iOrderByCol>0 );
142340 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
142341 aPermute[i] = pItem->u.x.iOrderByCol - 1;
142355 int nExpr = p->pEList->nExpr;
142356 assert( nOrderBy>=nExpr || db->mallocFailed );
142357 regPrev = pParse->nMem+1;
142358 pParse->nMem += nExpr+1;
142364 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
142365 pKeyDup->aSortFlags[i] = 0;
142376 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
142378 assert( pSplit->pPrior->pNext==pSplit );
142385 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
142387 pPrior = pSplit->pPrior;
142389 pSplit->pPrior = 0;
142390 pPrior->pNext = 0;
142391 assert( p->pOrderBy == pOrderBy );
142392 assert( pOrderBy!=0 || db->mallocFailed );
142393 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
142394 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
142395 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
142399 if( p->iLimit && op==TK_ALL ){
142400 regLimitA = ++pParse->nMem;
142401 regLimitB = ++pParse->nMem;
142402 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
142408 sqlite3ExprDelete(db, p->pLimit);
142409 p->pLimit = 0;
142411 regAddrA = ++pParse->nMem;
142412 regAddrB = ++pParse->nMem;
142413 regOutA = ++pParse->nMem;
142414 regOutB = ++pParse->nMem;
142418 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
142421 ** left of the compound operator - the "A" select.
142426 pPrior->iLimit = regLimitA;
142433 ** the right - the "B" select
142438 savedLimit = p->iLimit;
142439 savedOffset = p->iOffset;
142440 p->iLimit = regLimitB;
142441 p->iOffset = 0;
142444 p->iLimit = savedLimit;
142445 p->iOffset = savedOffset;
142473 VdbeNoopComment((v, "eof-A subroutine"));
142478 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
142486 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
142488 VdbeNoopComment((v, "eof-B subroutine"));
142496 VdbeNoopComment((v, "A-lt-B subroutine"));
142509 VdbeNoopComment((v, "A-eq-B subroutine"));
142517 VdbeNoopComment((v, "A-gt-B subroutine"));
142546 if( pSplit->pPrior ){
142548 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
142550 pSplit->pPrior = pPrior;
142551 pPrior->pNext = pSplit;
142552 sqlite3ExprListDelete(db, pPrior->pOrderBy);
142553 pPrior->pOrderBy = 0;
142558 return pParse->nErr!=0;
142573 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
142596 ** when processing a non-matched row of the left.
142613 ** a column in table number iTable with a copy of the iColumn-th
142630 && pExpr->w.iJoin==pSubst->iTable
142633 pExpr->w.iJoin = pSubst->iNewTable;
142635 if( pExpr->op==TK_COLUMN
142636 && pExpr->iTable==pSubst->iTable
142640 if( pExpr->iColumn<0 ){
142641 pExpr->op = TK_NULL;
142646 int iColumn = pExpr->iColumn;
142647 Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
142649 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
142650 assert( pExpr->pRight==0 );
142652 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
142654 sqlite3 *db = pSubst->pParse->db;
142655 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
142659 ifNullRow.iTable = pSubst->iNewTable;
142660 ifNullRow.iColumn = -99;
142666 if( db->mallocFailed ){
142670 if( pSubst->isOuterJoin ){
142674 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
142675 pExpr->flags & (EP_OuterON|EP_InnerON));
142679 if( pExpr->op==TK_TRUEFALSE ){
142680 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
142681 pExpr->op = TK_INTEGER;
142686 ** just as it did when it was a column of a view or sub-query. */
142688 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
142689 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
142690 pSubst->pCList->a[iColumn].pExpr
142692 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
142693 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
142694 (pColl ? pColl->zName : "BINARY")
142702 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
142703 pExpr->iTable = pSubst->iNewTable;
142705 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
142706 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
142708 substSelect(pSubst, pExpr->x.pSelect, 1);
142710 substExprList(pSubst, pExpr->x.pList);
142714 Window *pWin = pExpr->y.pWin;
142715 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
142716 substExprList(pSubst, pWin->pPartition);
142717 substExprList(pSubst, pWin->pOrderBy);
142729 for(i=0; i<pList->nExpr; i++){
142730 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
142736 int doPrior /* Do substitutes on p->pPrior too */
142743 substExprList(pSubst, p->pEList);
142744 substExprList(pSubst, p->pGroupBy);
142745 substExprList(pSubst, p->pOrderBy);
142746 p->pHaving = substExpr(pSubst, p->pHaving);
142747 p->pWhere = substExpr(pSubst, p->pWhere);
142748 pSrc = p->pSrc;
142750 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
142751 substSelect(pSubst, pItem->pSelect, 1);
142752 if( pItem->fg.isTabFunc ){
142753 substExprList(pSubst, pItem->u1.pFuncArg);
142756 }while( doPrior && (p = p->pPrior)!=0 );
142766 ** pSrcItem->colUsed mask.
142770 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
142771 pItem = pWalker->u.pSrcItem;
142772 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
142773 if( pExpr->iColumn<0 ) return WRC_Continue;
142774 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
142782 if( NEVER(pSrcItem->pTab==0) ) return;
142787 pSrcItem->colUsed = 0;
142803 ** If pSrc contains any sub-selects, call this routine recursively
142804 ** on the FROM clause of each such sub-select, with iExcept set to -1.
142814 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
142817 assert( pItem->iCursor < aCsrMap[0] );
142818 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
142819 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
142821 pItem->iCursor = aCsrMap[pItem->iCursor+1];
142822 for(p=pItem->pSelect; p; p=p->pPrior){
142823 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
142833 int *aCsrMap = pWalker->u.aiCol;
142845 int op = pExpr->op;
142847 renumberCursorDoMapping(pWalker, &pExpr->iTable);
142850 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
142858 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
142880 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
142895 while( pSel->pPrior ){
142896 pSel = pSel->pPrior;
142898 return pSel->pEList;
142937 ** other than the one FROM-clause subquery that is a candidate
142939 ** from 2015-02-09.)
142953 ** sub-queries that were excluded from this optimization. Restriction
142968 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
142969 ** accidently carried the comment forward until 2014-09-15. Original
143006 ** The parent and sub-query may contain WHERE clauses. Subject to
143013 ** Also, each component of the sub-query must return the same number
143016 ** such (illegal) sub-query is flattened. The caller will detect the
143019 ** (18) If the sub-query is a compound select, then all terms of the
143026 ** (20) If the sub-query is a compound select, then it must not use
143037 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
143043 ** The subquery may not be an aggregate that uses the built-in min() or
143065 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
143068 ** If flattening is not attempted, this routine is a no-op and returns 0.
143077 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
143080 const char *zSavedAuthContext = pParse->zAuthContext;
143083 Select *pSub1; /* Pointer to the rightmost select in sub-query */
143087 int iNewParent = -1;/* Replacement table for iParent */
143092 sqlite3 *db = pParse->db;
143099 assert( p->pPrior==0 );
143101 pSrc = p->pSrc;
143102 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
143103 pSubitem = &pSrc->a[iFrom];
143104 iParent = pSubitem->iCursor;
143105 pSub = pSubitem->pSelect;
143109 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
143112 pSubSrc = pSub->pSrc;
143116 ** because they could be computed at compile-time. But when LIMIT and OFFSET
143119 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
143120 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
143121 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
143124 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
143125 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
143126 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
143129 if( p->pOrderBy && pSub->pOrderBy ){
143132 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
143133 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
143134 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
143137 if( pSub->selFlags & (SF_Recursive) ){
143156 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
143157 if( pSubSrc->nSrc>1 /* (3a) */
143158 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
143159 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
143160 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
143167 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
143168 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143171 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
143175 /* Restriction (17): If the sub-query is a compound SELECT, then it must
143180 if( pSub->pPrior ){
143182 if( pSub->pOrderBy ){
143185 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
143188 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
143189 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
143190 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
143191 assert( pSub->pSrc!=0 );
143192 assert( (pSub->selFlags & SF_Recursive)==0 );
143193 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
143194 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
143195 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
143196 || pSub1->pSrc->nSrc<1 /* (17c) */
143198 || pSub1->pWin /* (17e) */
143203 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
143205 ** omitted on left-hand tables of the right join that is being
143209 testcase( pSub1->pSrc->nSrc>1 );
143213 if( p->pOrderBy ){
143214 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
143215 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
143220 if( (p->selFlags & SF_Recursive) ) return 0;
143223 for(ii=0; ii<pSub->pEList->nExpr; ii++){
143225 assert( pSub->pEList->a[ii].pExpr!=0 );
143226 aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
143227 for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
143228 assert( pSub1->pEList!=0 );
143229 assert( pSub1->pEList->nExpr>ii );
143230 assert( pSub1->pEList->a[ii].pExpr!=0 );
143231 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
143237 if( pSrc->nSrc>1 ){
143238 if( pParse->nSelect>500 ) return 0;
143240 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
143241 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
143247 pSub->selId, pSub, iFrom));
143250 pParse->zAuthContext = pSubitem->zName;
143253 pParse->zAuthContext = zSavedAuthContext;
143256 pSub1 = pSubitem->pSelect;
143257 sqlite3DbFree(db, pSubitem->zDatabase);
143258 sqlite3DbFree(db, pSubitem->zName);
143259 sqlite3DbFree(db, pSubitem->zAlias);
143260 pSubitem->zDatabase = 0;
143261 pSubitem->zName = 0;
143262 pSubitem->zAlias = 0;
143263 pSubitem->pSelect = 0;
143264 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
143266 /* If the sub-query is a compound SELECT statement, then (by restrictions
143270 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
143273 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
143274 ** OFFSET clauses and joins them to the left-hand-side of the original
143276 ** select statements in the compound sub-query.
143297 ** We call this the "compound-subquery flattening".
143299 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
143301 ExprList *pOrderBy = p->pOrderBy;
143302 Expr *pLimit = p->pLimit;
143303 Select *pPrior = p->pPrior;
143304 Table *pItemTab = pSubitem->pTab;
143305 pSubitem->pTab = 0;
143306 p->pOrderBy = 0;
143307 p->pPrior = 0;
143308 p->pLimit = 0;
143310 p->pLimit = pLimit;
143311 p->pOrderBy = pOrderBy;
143312 p->op = TK_ALL;
143313 pSubitem->pTab = pItemTab;
143315 p->pPrior = pPrior;
143317 pNew->selId = ++pParse->nSelect;
143318 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
143321 pNew->pPrior = pPrior;
143322 if( pPrior ) pPrior->pNext = pNew;
143323 pNew->pNext = p;
143324 p->pPrior = pNew;
143325 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
143326 " creates %u as peer\n",pNew->selId));
143328 assert( pSubitem->pSelect==0 );
143331 if( db->mallocFailed ){
143332 pSubitem->pSelect = pSub1;
143341 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
143343 if( ALWAYS(pSubitem->pTab!=0) ){
143344 Table *pTabToDel = pSubitem->pTab;
143345 if( pTabToDel->nTabRef==1 ){
143350 testcase( pToplevel->earlyCleanup );
143352 pTabToDel->nTabRef--;
143354 pSubitem->pTab = 0;
143357 /* The following loop runs once for each term in a compound-subquery
143359 ** of flattening - a flattening other than a compound-subquery flattening -
143371 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
143374 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
143376 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
143377 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
143378 pSrc = pParent->pSrc; /* FROM clause of the outer query */
143381 jointype = pSubitem->fg.jointype; /* First time through the loop */
143400 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
143402 pParent->pSrc = pSrc;
143409 SrcItem *pItem = &pSrc->a[i+iFrom];
143410 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
143411 assert( pItem->fg.isTabFunc==0 );
143412 *pItem = pSubSrc->a[i];
143413 pItem->fg.jointype |= ltorj;
143414 iNewParent = pSubSrc->a[i].iCursor;
143415 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
143417 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
143418 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
143432 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
143433 /* At this point, any non-zero iOrderByCol values indicate that the
143440 ** function attempts to flatten a compound sub-query into pParent
143441 ** (the only way this can happen is if the compound sub-query is
143442 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
143443 ExprList *pOrderBy = pSub->pOrderBy;
143444 for(i=0; i<pOrderBy->nExpr; i++){
143445 pOrderBy->a[i].u.x.iOrderByCol = 0;
143447 assert( pParent->pOrderBy==0 );
143448 pParent->pOrderBy = pOrderBy;
143449 pSub->pOrderBy = 0;
143451 pWhere = pSub->pWhere;
143452 pSub->pWhere = 0;
143457 if( pParent->pWhere ){
143458 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
143460 pParent->pWhere = pWhere;
143463 if( db->mallocFailed==0 ){
143469 x.pEList = pSub->pEList;
143476 pParent->selFlags |= pSub->selFlags & SF_Compound;
143477 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
143485 if( pSub->pLimit ){
143486 pParent->pLimit = pSub->pLimit;
143487 pSub->pLimit = 0;
143493 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
143522 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
143546 assert( pColumn->op==TK_COLUMN );
143551 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
143555 /* 2018-10-25 ticket [cf5ed20f]
143557 for(i=0; i<pConst->nConst; i++){
143558 const Expr *pE2 = pConst->apExpr[i*2];
143559 assert( pE2->op==TK_COLUMN );
143560 if( pE2->iTable==pColumn->iTable
143561 && pE2->iColumn==pColumn->iColumn
143567 pConst->bHasAffBlob = 1;
143570 pConst->nConst++;
143571 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
143572 pConst->nConst*2*sizeof(Expr*));
143573 if( pConst->apExpr==0 ){
143574 pConst->nConst = 0;
143576 pConst->apExpr[pConst->nConst*2-2] = pColumn;
143577 pConst->apExpr[pConst->nConst*2-1] = pValue;
143584 ** is part of the AND-connected terms of the expression. For each term
143590 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
143595 if( pExpr->op==TK_AND ){
143596 findConstInWhere(pConst, pExpr->pRight);
143597 findConstInWhere(pConst, pExpr->pLeft);
143600 if( pExpr->op!=TK_EQ ) return;
143601 pRight = pExpr->pRight;
143602 pLeft = pExpr->pLeft;
143605 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
143608 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
143617 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
143619 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
143628 if( pConst->pOomFault[0] ) return WRC_Prune;
143629 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
143630 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
143636 for(i=0; i<pConst->nConst; i++){
143637 Expr *pColumn = pConst->apExpr[i*2];
143639 if( pColumn->iTable!=pExpr->iTable ) continue;
143640 if( pColumn->iColumn!=pExpr->iColumn ) continue;
143645 pConst->nChng++;
143648 assert( pExpr->pLeft==0 );
143649 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
143650 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
143659 ** any substitutions based on the contents of pWalker->u.pConst should
143665 ** one of the columns in pWalker->u.pConst, or
143670 ** pWalker->u.pConst.
143673 WhereConst *pConst = pWalker->u.pConst;
143678 if( pConst->bHasAffBlob ){
143679 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
143680 || pExpr->op==TK_IS
143682 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
143683 if( pConst->pOomFault[0] ) return WRC_Prune;
143684 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
143685 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
143689 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
143693 ** The WHERE-clause constant propagation optimization.
143696 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
143728 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
143751 x.pOomFault = &pParse->db->mallocFailed;
143757 if( ALWAYS(p->pSrc!=0)
143758 && p->pSrc->nSrc>0
143759 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
143768 findConstInWhere(&x, p->pWhere);
143777 sqlite3WalkExpr(&w, p->pWhere);
143778 sqlite3DbFree(x.pParse->db, x.apExpr);
143789 ** push WHERE clause expression pExpr down to FROM clause sub-query
143796 ** BY clause of all window function used by the sub-query. It is safe
143802 ** * the sub-query uses only one distinct window frame, and
143806 assert( pSubq->pWin->pPartition );
143807 assert( (pSubq->selFlags & SF_MultiPart)==0 );
143808 assert( pSubq->pPrior==0 );
143809 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
143819 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
143823 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
143831 ** (1) (** This restriction was removed on 2017-09-29. We used to
143849 ** of a LEFT JOIN where iCursor is not the right-hand table of that
143868 ** (6b) The inner query is a compound and uses window-functions.
143872 ** all window-functions used by the sub-query. It is safe to
143874 ** window over which any window-function is calculated.
143887 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
143899 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
143900 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
143903 if( pSubq->pPrior ){
143905 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
143906 u8 op = pSel->op;
143910 if( pSel->pWin ) return 0; /* restriction (6b) */
143913 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
143924 for(pX=pSubq; pX; pX=pX->pPrior){
143925 assert( (pX->selFlags & (SF_Recursive))==0 );
143930 if( pSubq->pLimit!=0 ){
143933 while( pWhere->op==TK_AND ){
143934 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
143935 pWhere = pWhere->pLeft;
143941 || pWhere->w.iJoin!=iCursor)
143946 && pWhere->w.iJoin!=iCursor
143954 pSubq->selFlags |= SF_PushDown;
143957 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
143958 unsetJoinExpr(pNew, -1, 1);
143960 x.iTable = pSrc->iCursor;
143961 x.iNewTable = pSrc->iCursor;
143963 x.pEList = pSubq->pEList;
143967 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
143968 /* Restriction 6c has prevented push-down in this case */
143969 sqlite3ExprDelete(pParse->db, pNew);
143970 nChng--;
143974 if( pSubq->selFlags & SF_Aggregate ){
143975 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
143977 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
143979 pSubq = pSubq->pPrior;
144010 assert( pFunc->op==TK_AGG_FUNCTION );
144013 pEList = pFunc->x.pList;
144015 || pEList->nExpr!=1
144022 zFunc = pFunc->u.zToken;
144025 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
144035 assert( pOrderBy!=0 || db->mallocFailed );
144036 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
144042 ** The second argument is the associated aggregate-info object. This
144047 ** where table is a database table, not a sub-select or view. If the query
144061 assert( !p->pGroupBy );
144063 if( p->pWhere
144064 || p->pEList->nExpr!=1
144065 || p->pSrc->nSrc!=1
144066 || p->pSrc->a[0].pSelect
144067 || pAggInfo->nFunc!=1
144068 || p->pHaving
144072 pTab = p->pSrc->a[0].pTab;
144076 pExpr = p->pEList->a[0].pExpr;
144078 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
144079 if( pExpr->pAggInfo!=pAggInfo ) return 0;
144080 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
144081 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
144090 ** If the source-list item passed as an argument was augmented with an
144094 ** pFrom->pIndex and return SQLITE_OK.
144097 Table *pTab = pFrom->pTab;
144098 char *zIndexedBy = pFrom->u1.zIndexedBy;
144101 assert( pFrom->fg.isIndexedBy!=0 );
144103 for(pIdx=pTab->pIndex;
144104 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
144105 pIdx=pIdx->pNext
144109 pParse->checkSchema = 1;
144112 assert( pFrom->fg.isCte==0 );
144113 pFrom->u2.pIBIndex = pIdx;
144148 if( p->pPrior==0 ) return WRC_Continue;
144149 if( p->pOrderBy==0 ) return WRC_Continue;
144150 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
144152 a = p->pOrderBy->a;
144154 /* If iOrderByCol is already non-zero, then it has already been matched
144156 ** SELECT is rewritten for window-functions processing and then passed
144161 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
144162 if( a[i].pExpr->flags & EP_Collate ) break;
144168 pParse = pWalker->pParse;
144169 db = pParse->db;
144176 p->pSrc = pNewSrc;
144177 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
144178 p->op = TK_SELECT;
144179 p->pWhere = 0;
144180 pNew->pGroupBy = 0;
144181 pNew->pHaving = 0;
144182 pNew->pOrderBy = 0;
144183 p->pPrior = 0;
144184 p->pNext = 0;
144185 p->pWith = 0;
144187 p->pWinDefn = 0;
144189 p->selFlags &= ~SF_Compound;
144190 assert( (p->selFlags & SF_Converted)==0 );
144191 p->selFlags |= SF_Converted;
144192 assert( pNew->pPrior!=0 );
144193 pNew->pPrior->pNext = pNew;
144194 pNew->pLimit = 0;
144199 ** Check to see if the FROM clause term pFrom has table-valued function
144201 ** non-zero, since pFrom is not allowed to be a table-valued function.
144204 if( pFrom->fg.isTabFunc ){
144205 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
144215 ** FROM clause element pItem is really a common-table-expression (CTE)
144219 ** If a non-NULL value is returned, set *ppContext to point to the With
144227 const char *zName = pItem->zName;
144229 assert( pItem->zDatabase==0 );
144231 for(p=pWith; p; p=p->pOuter){
144233 for(i=0; i<p->nCte; i++){
144234 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
144236 return &p->a[i];
144239 if( p->bView ) break;
144245 ** with the inner-most WITH clause being at the top of the stack.
144269 if( pParse->nErr==0 ){
144270 assert( pParse->pWith!=pWith );
144271 pWith->pOuter = pParse->pWith;
144272 pParse->pWith = pWith;
144281 ** pParse->pWith linked list). And if currently processing a CTE
144285 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
144300 assert( pFrom->pTab==0 );
144301 if( pParse->pWith==0 ){
144305 if( pParse->nErr ){
144306 /* Prior errors might have left pParse->pWith in a goofy state, so
144310 if( pFrom->zDatabase!=0 ){
144315 if( pFrom->fg.notCte ){
144323 pCte = searchWith(pParse->pWith, pFrom, &pWith);
144325 sqlite3 *db = pParse->db;
144329 Select *pLeft; /* Left-most SELECT statement */
144330 Select *pRecTerm; /* Left-most recursive term */
144332 With *pSavedWith; /* Initial value of pParse->pWith */
144333 int iRecTab = -1; /* Cursor for recursive table */
144336 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
144338 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
144340 if( pCte->zCteErr ){
144341 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
144346 assert( pFrom->pTab==0 );
144349 pCteUse = pCte->pUse;
144351 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
144358 pCteUse->eM10d = pCte->eM10d;
144360 pFrom->pTab = pTab;
144361 pTab->nTabRef = 1;
144362 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
144363 pTab->iPKey = -1;
144364 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
144365 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
144366 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
144367 if( db->mallocFailed ) return 2;
144368 pFrom->pSelect->selFlags |= SF_CopyCte;
144369 assert( pFrom->pSelect );
144370 if( pFrom->fg.isIndexedBy ){
144371 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
144374 pFrom->fg.isCte = 1;
144375 pFrom->u2.pCteUse = pCteUse;
144376 pCteUse->nUse++;
144377 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
144378 pCteUse->eM10d = M10d_Yes;
144382 pRecTerm = pSel = pFrom->pSelect;
144383 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
144384 while( bMayRecursive && pRecTerm->op==pSel->op ){
144386 SrcList *pSrc = pRecTerm->pSrc;
144387 assert( pRecTerm->pPrior!=0 );
144388 for(i=0; i<pSrc->nSrc; i++){
144389 SrcItem *pItem = &pSrc->a[i];
144390 if( pItem->zDatabase==0
144391 && pItem->zName!=0
144392 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
144394 pItem->pTab = pTab;
144395 pTab->nTabRef++;
144396 pItem->fg.isRecursive = 1;
144397 if( pRecTerm->selFlags & SF_Recursive ){
144399 "multiple references to recursive table: %s", pCte->zName
144403 pRecTerm->selFlags |= SF_Recursive;
144404 if( iRecTab<0 ) iRecTab = pParse->nTab++;
144405 pItem->iCursor = iRecTab;
144408 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
144409 pRecTerm = pRecTerm->pPrior;
144412 pCte->zCteErr = "circular reference: %s";
144413 pSavedWith = pParse->pWith;
144414 pParse->pWith = pWith;
144415 if( pSel->selFlags & SF_Recursive ){
144418 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
144419 assert( pRecTerm->pNext!=0 );
144420 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
144421 assert( pRecTerm->pWith==0 );
144422 pRecTerm->pWith = pSel->pWith;
144424 pRecTerm->pWith = 0;
144426 pParse->pWith = pSavedWith;
144431 pParse->pWith = pSavedWith;
144435 pParse->pWith = pWith;
144437 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
144438 pEList = pLeft->pEList;
144439 if( pCte->pCols ){
144440 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
144442 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
144444 pParse->pWith = pSavedWith;
144447 pEList = pCte->pCols;
144450 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
144452 if( pSel->selFlags & SF_Recursive ){
144453 pCte->zCteErr = "multiple recursive references: %s";
144455 pCte->zCteErr = "recursive reference in a subquery: %s";
144459 pCte->zCteErr = 0;
144460 pParse->pWith = pSavedWith;
144477 Parse *pParse = pWalker->pParse;
144478 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
144479 With *pWith = findRightmost(p)->pWith;
144481 assert( pParse->pWith==pWith || pParse->nErr );
144482 pParse->pWith = pWith->pOuter;
144490 ** sub-query in the FROM clause of a SELECT statement. This function
144496 Select *pSel = pFrom->pSelect;
144500 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
144502 pTab->nTabRef = 1;
144503 if( pFrom->zAlias ){
144504 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
144506 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
144508 while( pSel->pPrior ){ pSel = pSel->pPrior; }
144509 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
144510 pTab->iPKey = -1;
144511 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
144513 /* The usual case - do not allow ROWID on a subquery */
144514 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
144516 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
144518 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
144537 N--;
144539 if( pBase->fg.isUsing==0 ) continue;
144540 if( NEVER(pBase->u3.pUsing==0) ) continue;
144541 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
144554 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
144556 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
144572 Parse *pParse = pWalker->pParse;
144577 sqlite3 *db = pParse->db;
144579 u16 selFlags = p->selFlags;
144582 p->selFlags |= SF_Expanded;
144583 if( db->mallocFailed ){
144586 assert( p->pSrc!=0 );
144590 if( pWalker->eCode ){
144592 p->selId = ++pParse->nSelect;
144594 pTabList = p->pSrc;
144595 pEList = p->pEList;
144596 if( pParse->pWith && (p->selFlags & SF_View) ){
144597 if( p->pWith==0 ){
144598 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
144599 if( p->pWith==0 ){
144603 p->pWith->bView = 1;
144605 sqlite3WithPush(pParse, p->pWith, 0);
144616 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144618 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
144619 if( pFrom->pTab ) continue;
144620 assert( pFrom->fg.isRecursive==0 );
144621 if( pFrom->zName==0 ){
144623 Select *pSel = pFrom->pSelect;
144624 /* A sub-query in the FROM clause of a SELECT */
144626 assert( pFrom->pTab==0 );
144633 pTab = pFrom->pTab;
144638 assert( pFrom->pTab==0 );
144639 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
144641 if( pTab->nTabRef>=0xffff ){
144643 pTab->zName);
144644 pFrom->pTab = 0;
144647 pTab->nTabRef++;
144654 u8 eCodeOrig = pWalker->eCode;
144656 assert( pFrom->pSelect==0 );
144658 if( (db->flags & SQLITE_EnableView)==0
144659 && pTab->pSchema!=db->aDb[1].pSchema
144662 pTab->zName);
144664 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
144668 && pFrom->fg.fromDDL
144669 && ALWAYS(pTab->u.vtab.p!=0)
144670 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
144673 pTab->zName);
144677 nCol = pTab->nCol;
144678 pTab->nCol = -1;
144679 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
144680 sqlite3WalkSelect(pWalker, pFrom->pSelect);
144681 pWalker->eCode = eCodeOrig;
144682 pTab->nCol = nCol;
144688 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
144695 assert( db->mallocFailed==0 || pParse->nErr!=0 );
144696 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
144711 for(k=0; k<pEList->nExpr; k++){
144712 pE = pEList->a[k].pExpr;
144713 if( pE->op==TK_ASTERISK ) break;
144714 assert( pE->op!=TK_DOT || pE->pRight!=0 );
144715 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
144716 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
144717 elistFlags |= pE->flags;
144719 if( k<pEList->nExpr ){
144725 struct ExprList_item *a = pEList->a;
144727 int flags = pParse->db->flags;
144731 for(k=0; k<pEList->nExpr; k++){
144733 elistFlags |= pE->flags;
144734 pRight = pE->pRight;
144735 assert( pE->op!=TK_DOT || pRight!=0 );
144736 if( pE->op!=TK_ASTERISK
144737 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
144743 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
144744 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
144753 if( pE->op==TK_DOT ){
144754 assert( pE->pLeft!=0 );
144755 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
144756 zTName = pE->pLeft->u.zToken;
144758 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144759 Table *pTab = pFrom->pTab; /* Table for this data source */
144760 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
144766 if( (zTabName = pFrom->zAlias)==0 ){
144767 zTabName = pTab->zName;
144769 if( db->mallocFailed ) break;
144770 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
144771 if( pFrom->fg.isNestedFrom ){
144772 assert( pFrom->pSelect!=0 );
144773 pNestedFrom = pFrom->pSelect->pEList;
144775 assert( pNestedFrom->nExpr==pTab->nCol );
144781 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
144782 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
144784 if( i+1<pTabList->nSrc
144790 for(ii=0; ii<pUsing->nId; ii++){
144791 const char *zUName = pUsing->a[ii].zName;
144795 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
144796 assert( pX->zEName==0 );
144797 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
144798 pX->fg.eEName = ENAME_TAB;
144799 pX->fg.bUsingTerm = 1;
144805 for(j=0; j<pTab->nCol; j++){
144806 char *zName = pTab->aCol[j].zCnName;
144812 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
144818 ** result-set list unless the SELECT has the SF_IncludeHidden
144821 if( (p->selFlags & SF_IncludeHidden)==0
144822 && IsHiddenColumn(&pTab->aCol[j])
144826 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
144835 if( pFrom->fg.isUsing
144836 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
144844 if( (pTabList->nSrc>1
144845 && ( (pFrom->fg.jointype & JT_LTORJ)==0
144847 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
144855 if( IN_RENAME_OBJECT && pE->pLeft ){
144856 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
144869 pX = &pNew->a[pNew->nExpr-1];
144870 assert( pX->zEName==0 );
144873 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
144874 testcase( pX->zEName==0 );
144876 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
144878 testcase( pX->zEName==0 );
144880 pX->fg.eEName = ENAME_TAB;
144881 if( (pFrom->fg.isUsing
144882 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
144884 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
144886 pX->fg.bNoExpand = 1;
144889 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
144890 pX->fg.eEName = ENAME_NAME;
144892 pX->zEName = sqlite3DbStrDup(db, zName);
144893 pX->fg.eEName = ENAME_NAME;
144907 p->pEList = pNew;
144909 if( p->pEList ){
144910 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
144915 p->selFlags |= SF_ComplexResult;
144920 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
144947 ** The calling function can detect the problem by looking at pParse->nErr
144948 ** and/or pParse->db->mallocFailed.
144954 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
144971 ** For each FROM-clause subquery, add Column.zType and Column.zColl
144986 assert( p->selFlags & SF_Resolved );
144987 if( p->selFlags & SF_HasTypeInfo ) return;
144988 p->selFlags |= SF_HasTypeInfo;
144989 pParse = pWalker->pParse;
144990 pTabList = p->pSrc;
144991 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144992 Table *pTab = pFrom->pTab;
144994 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
144995 /* A sub-query in the FROM clause of a SELECT */
144996 Select *pSel = pFrom->pSelect;
144998 while( pSel->pPrior ) pSel = pSel->pPrior;
145010 ** the Table structures of all FROM-clause subqueries in a
145031 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
145032 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
145044 assert( p!=0 || pParse->db->mallocFailed );
145045 assert( pParse->db->pParse==pParse );
145046 if( pParse->db->mallocFailed ) return;
145047 if( p->selFlags & SF_HasTypeInfo ) return;
145049 if( pParse->nErr ) return;
145051 if( pParse->nErr ) return;
145064 Vdbe *v = pParse->pVdbe;
145067 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
145068 assert( pParse->db->pParse==pParse );
145069 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
145071 if( pParse->nErr ) return;
145075 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
145076 for(i=0; i<pAggInfo->nColumn; i++){
145077 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
145078 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
145080 for(i=0; i<pAggInfo->nFunc; i++){
145081 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
145082 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
145085 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
145086 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
145087 if( pFunc->iDistinct>=0 ){
145088 Expr *pE = pFunc->pFExpr;
145090 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
145093 pFunc->iDistinct = -1;
145095 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
145096 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
145097 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
145098 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
145099 pFunc->pFunc->zName));
145110 Vdbe *v = pParse->pVdbe;
145113 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
145115 assert( ExprUseXList(pF->pFExpr) );
145116 pList = pF->pFExpr->x.pList;
145117 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
145118 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
145127 ** If regAcc is non-zero and there are no min() or max() aggregates
145128 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
145138 Vdbe *v = pParse->pVdbe;
145145 pAggInfo->directMode = 1;
145146 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
145151 assert( ExprUseXList(pF->pFExpr) );
145152 assert( !IsWindowFunc(pF->pFExpr) );
145153 pList = pF->pFExpr->x.pList;
145154 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
145155 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
145156 if( pAggInfo->nAccumulator
145157 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
145163 if( regHit==0 ) regHit = ++pParse->nMem;
145177 nArg = pList->nExpr;
145184 if( pF->iDistinct>=0 && pList ){
145188 pF->iDistinct = codeDistinct(pParse, eDistinctType,
145189 pF->iDistinct, addrNext, pList, regAgg);
145191 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
145195 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
145196 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
145197 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
145200 pColl = pParse->db->pDfltColl;
145202 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
145205 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
145206 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
145213 if( regHit==0 && pAggInfo->nAccumulator ){
145219 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
145220 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
145223 pAggInfo->directMode = 0;
145239 if( pParse->explain==2 ){
145242 pTab->zName,
145244 bCover ? pIdx->zName : ""
145259 ** sub-expression matches the criteria for being moved to the WHERE
145260 ** clause. If so, add it to the WHERE clause and replace the sub-expression
145264 if( pExpr->op!=TK_AND ){
145265 Select *pS = pWalker->u.pSelect;
145267 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
145273 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
145275 && pExpr->pAggInfo==0
145277 sqlite3 *db = pWalker->pParse->db;
145280 Expr *pWhere = pS->pWhere;
145282 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
145283 pS->pWhere = pNew;
145284 pWalker->eCode = 1;
145313 sqlite3WalkExpr(&sWalker, p->pHaving);
145323 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
145328 SrcList *pTabList, /* Search for self-joins in this FROM clause */
145332 assert( pThis->pSelect!=0 );
145333 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
145334 for(pItem = pTabList->a; pItem<pThis; pItem++){
145336 if( pItem->pSelect==0 ) continue;
145337 if( pItem->fg.viaCoroutine ) continue;
145338 if( pItem->zName==0 ) continue;
145339 assert( pItem->pTab!=0 );
145340 assert( pThis->pTab!=0 );
145341 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
145342 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
145343 pS1 = pItem->pSelect;
145344 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
145349 if( pItem->pSelect->selFlags & SF_PushDown ){
145363 sqlite3DbFree(db, p->aCol);
145364 sqlite3DbFree(db, p->aFunc);
145393 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
145394 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
145395 if( p->pWhere ) return 0;
145396 if( p->pGroupBy ) return 0;
145397 pExpr = p->pEList->a[0].pExpr;
145398 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
145400 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
145402 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
145403 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
145404 pSub = p->pSrc->a[0].pSelect;
145406 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
145408 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
145409 if( pSub->pWhere ) return 0; /* No WHERE clause */
145410 if( pSub->pLimit ) return 0; /* No LIMIT clause */
145411 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
145412 pSub = pSub->pPrior; /* Repeat over compound */
145417 db = pParse->db;
145420 pSub = p->pSrc->a[0].pSelect;
145421 p->pSrc->a[0].pSelect = 0;
145422 sqlite3SrcListDelete(db, p->pSrc);
145423 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
145426 pPrior = pSub->pPrior;
145427 pSub->pPrior = 0;
145428 pSub->pNext = 0;
145429 pSub->selFlags |= SF_Aggregate;
145430 pSub->selFlags &= ~SF_Compound;
145431 pSub->nSelectRow = 0;
145432 sqlite3ExprListDelete(db, pSub->pEList);
145434 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
145444 p->pEList->a[0].pExpr = pExpr;
145445 p->selFlags &= ~SF_Aggregate;
145449 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
145458 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
145464 for(i=0; i<pSrc->nSrc; i++){
145465 SrcItem *p1 = &pSrc->a[i];
145467 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
145470 if( p1->pSelect
145471 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
145472 && sameSrcAlias(p0, p1->pSelect->pSrc)
145488 ** pParse->zErrMsg.
145516 db = pParse->db;
145517 assert( pParse==db->pParse );
145519 if( p==0 || pParse->nErr ){
145522 assert( db->mallocFailed==0 );
145525 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
145535 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
145536 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
145537 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
145538 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
145540 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
145541 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
145542 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
145544 if( p->pOrderBy ){
145548 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
145553 p->pOrderBy);
145554 testcase( pParse->earlyCleanup );
145555 p->pOrderBy = 0;
145557 p->selFlags &= ~SF_Distinct;
145558 p->selFlags |= SF_NoopOrderBy;
145561 if( pParse->nErr ){
145564 assert( db->mallocFailed==0 );
145565 assert( p->pEList!=0 );
145575 ** In this case, it is an error if the target object (pSrc->a[0]) name
145576 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
145582 if( p->selFlags & SF_UFSrcCheck ){
145583 SrcItem *p0 = &p->pSrc->a[0];
145584 if( sameSrcAlias(p0, p->pSrc) ){
145587 p0->zAlias ? p0->zAlias : p0->pTab->zName
145593 ** and leaving this flag set can cause errors if a compound sub-query
145594 ** in p->pSrc is flattened into this query and this function called
145596 p->selFlags &= ~SF_UFSrcCheck;
145599 if( pDest->eDest==SRT_Output ){
145605 assert( pParse->nErr );
145609 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
145615 pTabList = p->pSrc;
145616 isAgg = (p->selFlags & SF_Aggregate)!=0;
145618 sSort.pOrderBy = p->pOrderBy;
145624 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
145625 SrcItem *pItem = &pTabList->a[i];
145626 Select *pSub = pItem->pSelect;
145627 Table *pTab = pItem->pTab;
145637 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
145638 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
145642 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
145643 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
145644 assert( pItem->iCursor>=0 );
145645 unsetJoinExpr(p->pWhere, pItem->iCursor,
145646 pTabList->a[0].fg.jointype & JT_LTORJ);
145654 if( pTab->nCol!=pSub->pEList->nExpr ){
145656 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
145664 ** will be implemented as a co-routine and there is no advantage to
145667 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
145668 assert( pSub->pGroupBy==0 );
145670 /* If a FROM-clause subquery has an ORDER BY clause that is not
145679 ** (2) The subquery was added to help with window-function
145683 ** the built-in count(), min(), or max().
145690 if( pSub->pOrderBy!=0
145691 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
145692 && pSub->pLimit==0 /* Condition (1) */
145693 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
145694 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
145698 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
145701 pSub->pOrderBy);
145702 pSub->pOrderBy = 0;
145708 ** it will be implemented as a co-routine, then do not flatten. This
145723 if( pSub->pOrderBy!=0
145725 && (p->selFlags & SF_ComplexResult)!=0
145726 && (pTabList->nSrc==1
145727 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
145733 if( pParse->nErr ) goto select_end;
145735 i = -1;
145737 pTabList = p->pSrc;
145738 if( db->mallocFailed ) goto select_end;
145740 sSort.pOrderBy = p->pOrderBy;
145749 if( p->pPrior ){
145752 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
145757 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
145762 /* Do the WHERE-clause constant propagation optimization if this is
145763 ** a join. No need to speed time on this operation for non-join queries
145767 if( p->pWhere!=0
145768 && p->pWhere->op==TK_AND
145786 if( db->mallocFailed ) goto select_end;
145787 pEList = p->pEList;
145788 pTabList = p->pSrc;
145794 ** (2) Generate code for all sub-queries
145796 for(i=0; i<pTabList->nSrc; i++){
145797 SrcItem *pItem = &pTabList->a[i];
145810 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
145811 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
145818 ** assume the column name is non-NULL and segfault. The use of an empty
145821 if( pItem->colUsed==0 && pItem->zName!=0 ){
145822 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
145826 /* Generate code for all sub-queries in the FROM clause
145828 pSub = pItem->pSelect;
145832 assert( pItem->addrFillSub==0 );
145837 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
145841 pParse->nHeight += sqlite3SelectExprHeight(p);
145843 /* Make copies of constant WHERE-clause terms in the outer query down
145847 && (pItem->fg.isCte==0
145848 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
145849 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
145854 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
145858 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
145860 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
145863 zSavedAuthContext = pParse->zAuthContext;
145864 pParse->zAuthContext = pItem->zName;
145868 ** The subquery is implemented as a co-routine if all of the following are
145877 && (pTabList->nSrc==1
145878 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) /* (1) */
145879 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
145880 && (pTabList->a[0].fg.jointype & JT_LTORJ)==0 /* (3) */
145882 /* Implement a co-routine that will return a single row of the result
145887 pItem->regReturn = ++pParse->nMem;
145888 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
145890 pItem->addrFillSub = addrTop;
145891 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
145892 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
145894 pItem->pTab->nRowLogEst = pSub->nSelectRow;
145895 pItem->fg.viaCoroutine = 1;
145896 pItem->regResult = dest.iSdst;
145897 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
145898 sqlite3VdbeJumpHere(v, addrTop-1);
145900 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
145903 ** the make the pItem->iCursor be a copy of the ephemerial table that
145905 CteUse *pCteUse = pItem->u2.pCteUse;
145906 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
145907 if( pItem->iCursor!=pCteUse->iCur ){
145908 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
145911 pSub->nSelectRow = pCteUse->nRowEst;
145915 if( pPrior->addrFillSub ){
145916 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
145918 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
145919 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
145927 pItem->regReturn = ++pParse->nMem;
145929 pItem->addrFillSub = topAddr+1;
145930 pItem->fg.isMaterialized = 1;
145931 if( pItem->fg.isCorrelated==0 ){
145940 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
145942 dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
145946 pItem->pTab->nRowLogEst = pSub->nSelectRow;
145948 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
145952 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
145953 CteUse *pCteUse = pItem->u2.pCteUse;
145954 pCteUse->addrM9e = pItem->addrFillSub;
145955 pCteUse->regRtn = pItem->regReturn;
145956 pCteUse->iCur = pItem->iCursor;
145957 pCteUse->nRowEst = pSub->nSelectRow;
145960 if( db->mallocFailed ) goto select_end;
145961 pParse->nHeight -= sqlite3SelectExprHeight(p);
145962 pParse->zAuthContext = zSavedAuthContext;
145968 pEList = p->pEList;
145969 pWhere = p->pWhere;
145970 pGroupBy = p->pGroupBy;
145971 pHaving = p->pHaving;
145972 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
145976 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
145982 ** if the select-list is the same as the ORDER BY list, then this query
145991 ** The second form is preferred as a single index (or temp-table) may be
145993 ** written the query must use a temp-table for at least one of the ORDER
145994 ** BY and DISTINCT, and an index or separate temp-table for the other.
145996 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
145997 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
145999 && p->pWin==0
146002 p->selFlags &= ~SF_Distinct;
146003 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
146004 p->selFlags |= SF_Aggregate;
146005 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
146021 ** being unused if the data can be extracted in pre-sorted order.
146030 pParse, sSort.pOrderBy, 0, pEList->nExpr);
146031 sSort.iECursor = pParse->nTab++;
146034 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
146038 sSort.addrSortIndex = -1;
146043 if( pDest->eDest==SRT_EphemTab ){
146044 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
146045 if( p->selFlags & SF_NestedFrom ){
146046 /* Delete or NULL-out result columns that will never be used */
146048 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
146049 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
146050 sqlite3DbFree(db, pEList->a[ii].zEName);
146051 pEList->nExpr--;
146053 for(ii=0; ii<pEList->nExpr; ii++){
146054 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
146062 if( (p->selFlags & SF_FixedLimit)==0 ){
146063 p->nSelectRow = 320; /* 4 billion rows */
146065 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
146066 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
146073 if( p->selFlags & SF_Distinct ){
146074 sDistinct.tabTnct = pParse->nTab++;
146077 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
146088 | (p->selFlags & SF_FixedLimit);
146090 Window *pWin = p->pWin; /* Main window object (or NULL) */
146101 p->pEList, p, wctrlFlags, p->nSelectRow);
146103 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
146104 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
146112 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
146126 assert( p->pEList==pEList );
146132 int regGosub = ++pParse->nMem;
146138 VdbeNoopComment((v, "inner-loop subroutine"));
146140 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
146143 VdbeComment((v, "end inner-loop subroutine"));
146149 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
146181 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
146182 pItem->u.x.iAlias = 0;
146184 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
146185 pItem->u.x.iAlias = 0;
146188 if( p->nSelectRow>66 ) p->nSelectRow = 66;
146193 ** in the correct order. It also may not - the GROUP BY might use a
146198 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
146201 ** ASC or DESC order - only that each group is returned contiguously.
146205 for(ii=0; ii<pGroupBy->nExpr; ii++){
146207 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
146208 pGroupBy->a[ii].fg.sortFlags = sortFlags;
146210 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
146216 p->nSelectRow = 0;
146230 testcase( pParse->earlyCleanup );
146232 if( db->mallocFailed ){
146235 pAggInfo->selId = p->selId;
146241 pAggInfo->mnReg = pParse->nMem+1;
146242 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
146243 pAggInfo->pGroupBy = pGroupBy;
146248 assert( pWhere==p->pWhere );
146249 assert( pHaving==p->pHaving );
146250 assert( pGroupBy==p->pGroupBy );
146252 pWhere = p->pWhere;
146256 pAggInfo->nAccumulator = pAggInfo->nColumn;
146257 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
146258 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
146262 for(i=0; i<pAggInfo->nFunc; i++){
146263 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
146266 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
146270 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
146275 pAggInfo->mxReg = pParse->nMem;
146276 if( db->mallocFailed ) goto select_end;
146286 for(ii=0; ii<pAggInfo->nColumn; ii++){
146287 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
146289 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
146291 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
146292 pCol->iTable, pCol->iColumn, pCol->iMem,
146293 pCol->iSorterColumn);
146294 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
146296 for(ii=0; ii<pAggInfo->nFunc; ii++){
146297 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
146298 ii, pAggInfo->aFunc[ii].iMem);
146299 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
146310 int addr1; /* A-vs-B comparision jump */
146322 if( pAggInfo->nFunc==1
146323 && pAggInfo->aFunc[0].iDistinct>=0
146324 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
146325 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
146326 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
146328 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
146340 pAggInfo->sortingIdx = pParse->nTab++;
146342 0, pAggInfo->nColumn);
146344 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
146349 iUseFlag = ++pParse->nMem;
146350 iAbortFlag = ++pParse->nMem;
146351 regOutputRow = ++pParse->nMem;
146353 regReset = ++pParse->nMem;
146355 iAMem = pParse->nMem + 1;
146356 pParse->nMem += pGroupBy->nExpr;
146357 iBMem = pParse->nMem + 1;
146358 pParse->nMem += pGroupBy->nExpr;
146361 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
146380 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
146398 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
146402 nGroupBy = pGroupBy->nExpr;
146405 for(i=0; i<pAggInfo->nColumn; i++){
146406 if( pAggInfo->aCol[i].iSorterColumn>=j ){
146414 pAggInfo->directMode = 1;
146415 for(i=0; i<pAggInfo->nColumn; i++){
146416 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
146417 if( pCol->iSorterColumn>=j ){
146418 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
146422 pAggInfo->directMode = 0;
146425 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
146430 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
146433 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
146435 pAggInfo->useSortingIdx = 1;
146442 ** This is an optimization - the correct answer should result regardless.
146459 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
146462 for(j=0; j<pGroupBy->nExpr; j++){
146466 pAggInfo->directMode = 1;
146467 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
146470 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
146484 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
146503 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
146523 ** is less than or equal to zero, the subroutine is a no-op. If
146540 selectInnerLoop(pParse, p, -1, &sSort,
146546 /* Generate a subroutine that will reset the group-by accumulator
146555 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
146556 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
146575 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
146576 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
146580 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
146583 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
146587 ** (2011-04-15) Do not do a full scan of an unordered index.
146589 ** (2013-10-03) Do not count the entries in a partial index.
146595 if( !p->pSrc->a[0].fg.notIndexed ){
146596 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146597 if( pIdx->bUnordered==0
146598 && pIdx->szIdxRow<pTab->szTabRow
146599 && pIdx->pPartIdxWhere==0
146600 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
146607 iRoot = pBest->tnum;
146611 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
146614 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
146616 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
146634 if( pAggInfo->nAccumulator ){
146635 for(i=0; i<pAggInfo->nFunc; i++){
146636 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
146639 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
146643 if( i==pAggInfo->nFunc ){
146644 regAcc = ++pParse->nMem;
146647 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
146648 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
146649 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
146657 assert( p->pGroupBy==0 );
146666 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
146678 struct AggInfo_func *pF = pAggInfo->aFunc;
146680 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
146695 selectInnerLoop(pParse, p, -1, 0, 0,
146712 assert( p->pEList==pEList );
146713 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
146722 rc = (pParse->nErr>0);
146728 assert( db->mallocFailed==0 || db->mallocFailed==1 );
146729 assert( db->mallocFailed==0 || pParse->nErr!=0 );
146732 if( pAggInfo && !db->mallocFailed ){
146733 for(i=0; i<pAggInfo->nColumn; i++){
146734 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
146736 assert( pExpr->pAggInfo==pAggInfo );
146737 assert( pExpr->iAgg==i );
146739 for(i=0; i<pAggInfo->nFunc; i++){
146740 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
146742 assert( pExpr->pAggInfo==pAggInfo );
146743 assert( pExpr->iAgg==i );
146803 int need; /* Slots needed in p->azResult[] */
146807 /* Make sure there is enough space in p->azResult to hold everything
146810 if( p->nRow==0 && argv!=0 ){
146815 if( p->nData + need > p->nAlloc ){
146817 p->nAlloc = p->nAlloc*2 + need;
146818 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
146820 p->azResult = azNew;
146826 if( p->nRow==0 ){
146827 p->nColumn = nCol;
146831 p->azResult[p->nData++] = z;
146833 }else if( (int)p->nColumn!=nCol ){
146834 sqlite3_free(p->zErrMsg);
146835 p->zErrMsg = sqlite3_mprintf(
146838 p->rc = SQLITE_ERROR;
146854 p->azResult[p->nData++] = z;
146856 p->nRow++;
146861 p->rc = SQLITE_NOMEM_BKPT;
146901 db->errCode = SQLITE_NOMEM;
146917 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
146930 db->errCode = SQLITE_NOMEM;
146949 azResult--;
146982 pTriggerStep = pTriggerStep->pNext;
146984 sqlite3ExprDelete(db, pTmp->pWhere);
146985 sqlite3ExprListDelete(db, pTmp->pExprList);
146986 sqlite3SelectDelete(db, pTmp->pSelect);
146987 sqlite3IdListDelete(db, pTmp->pIdList);
146988 sqlite3UpsertDelete(db, pTmp->pUpsert);
146989 sqlite3SrcListDelete(db, pTmp->pFrom);
146990 sqlite3DbFree(db, pTmp->zSpan);
147001 ** are already attached to pTab->pTrigger. But there might be additional
147003 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
147008 ** pTab as well as the triggers lised in pTab->pTrigger.
147015 assert( pParse->disableTriggers==0 );
147016 pTmpSchema = pParse->db->aDb[1].pSchema;
147017 p = sqliteHashFirst(&pTmpSchema->trigHash);
147018 pList = pTab->pTrigger;
147021 if( pTrig->pTabSchema==pTab->pSchema
147022 && pTrig->table
147023 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
147024 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
147026 pTrig->pNext = pList;
147028 }else if( pTrig->op==TK_RETURNING ){
147030 assert( pParse->db->pVtabCtx==0 );
147032 assert( pParse->bReturning );
147033 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
147034 pTrig->table = pTab->zName;
147035 pTrig->pTabSchema = pTab->pSchema;
147036 pTrig->pNext = pList;
147044 printf("Triggers for %s:", pTab->zName);
147045 for(pX=pList; pX; pX=pX->pNext){
147046 printf(" %s", pX->zName);
147059 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
147078 sqlite3 *db = pParse->db; /* The database connection */
147083 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
147089 if( pName2->n>0 ){
147102 if( !pTableName || db->mallocFailed ){
147106 /* A long-standing parser bug is that this syntax was allowed:
147114 if( db->init.busy && iDb!=1 ){
147115 sqlite3DbFree(db, pTableName->a[0].zDatabase);
147116 pTableName->a[0].zDatabase = 0;
147125 if( db->init.busy==0 && pName2->n==0 && pTab
147126 && pTab->pSchema==db->aDb[1].pSchema ){
147131 if( db->mallocFailed ) goto trigger_cleanup;
147132 assert( pTableName->nSrc==1 );
147151 assert( db->mallocFailed );
147154 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
147159 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
147163 assert( !db->init.busy );
147171 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
147181 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
147186 " trigger on table: %S", pTableName->a);
147192 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147194 const char *zDb = db->aDb[iTabDb].zDbSName;
147195 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
147197 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
147218 pTrigger->zName = zName;
147220 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
147221 pTrigger->pSchema = db->aDb[iDb].pSchema;
147222 pTrigger->pTabSchema = pTab->pSchema;
147223 pTrigger->op = (u8)op;
147224 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
147226 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
147227 pTrigger->pWhen = pWhen;
147230 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
147232 pTrigger->pColumns = pColumns;
147234 assert( pParse->pNewTrigger==0 );
147235 pParse->pNewTrigger = pTrigger;
147242 if( !pParse->pNewTrigger ){
147245 assert( pParse->pNewTrigger==pTrigger );
147250 if( db->init.iDb==1 ){
147253 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
147257 ** "orphaned trigger" - a trigger whose associated table is missing.
147259 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
147261 db->init.orphanTrigger = 1;
147275 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
147277 sqlite3 *db = pParse->db; /* The database */
147282 pParse->pNewTrigger = 0;
147283 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
147284 zName = pTrig->zName;
147285 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
147286 pTrig->step_list = pStepList;
147288 pStepList->pTrig = pTrig;
147289 pStepList = pStepList->pNext;
147291 sqlite3TokenInit(&nameToken, pTrig->zName);
147293 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
147294 || sqlite3FixExpr(&sFix, pTrig->pWhen)
147301 assert( !db->init.busy );
147302 pParse->pNewTrigger = pTrig;
147310 if( !db->init.busy ){
147315 ** are read-only, and the trigger makes a change to a shadow table,
147316 ** then raise an error - do not allow the trigger to be created. */
147319 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
147320 if( pStep->zTarget!=0
147321 && sqlite3ShadowTableName(db, pStep->zTarget)
147325 pTrig->zName, pStep->zTarget);
147335 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
147340 db->aDb[iDb].zDbSName, zName,
147341 pTrig->table, z);
147348 if( db->init.busy ){
147350 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
147356 }else if( pLink->pSchema==pLink->pTabSchema ){
147358 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
147360 pLink->pNext = pTab->pTrigger;
147361 pTab->pTrigger = pLink;
147367 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
147400 pTriggerStep->op = TK_SELECT;
147401 pTriggerStep->pSelect = pSelect;
147402 pTriggerStep->orconf = OE_Default;
147403 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
147411 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
147420 sqlite3 *db = pParse->db;
147423 if( pParse->nErr ) return 0;
147424 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
147427 memcpy(z, pName->z, pName->n);
147429 pTriggerStep->zTarget = z;
147430 pTriggerStep->op = op;
147431 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
147433 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
147456 sqlite3 *db = pParse->db;
147459 assert(pSelect != 0 || db->mallocFailed);
147464 pTriggerStep->pSelect = pSelect;
147467 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
147469 pTriggerStep->pIdList = pColumn;
147470 pTriggerStep->pUpsert = pUpsert;
147471 pTriggerStep->orconf = orconf;
147473 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
147494 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
147501 sqlite3 *db = pParse->db;
147507 pTriggerStep->pExprList = pEList;
147508 pTriggerStep->pWhere = pWhere;
147509 pTriggerStep->pFrom = pFrom;
147514 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
147515 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
147516 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
147518 pTriggerStep->orconf = orconf;
147538 sqlite3 *db = pParse->db;
147544 pTriggerStep->pWhere = pWhere;
147547 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
147549 pTriggerStep->orconf = OE_Default;
147559 if( pTrigger==0 || pTrigger->bReturning ) return;
147560 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
147561 sqlite3DbFree(db, pTrigger->zName);
147562 sqlite3DbFree(db, pTrigger->table);
147563 sqlite3ExprDelete(db, pTrigger->pWhen);
147564 sqlite3IdListDelete(db, pTrigger->pColumns);
147581 sqlite3 *db = pParse->db;
147583 if( db->mallocFailed ) goto drop_trigger_cleanup;
147588 assert( pName->nSrc==1 );
147589 zDb = pName->a[0].zDatabase;
147590 zName = pName->a[0].zName;
147592 for(i=OMIT_TEMPDB; i<db->nDb; i++){
147596 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
147601 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
147605 pParse->checkSchema = 1;
147619 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
147629 sqlite3 *db = pParse->db;
147632 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
147633 assert( iDb>=0 && iDb<db->nDb );
147635 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
147639 const char *zDb = db->aDb[iDb].zDbSName;
147642 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
147654 db->aDb[iDb].zDbSName, pTrigger->zName
147657 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
147669 pHash = &(db->aDb[iDb].pSchema->trigHash);
147672 if( pTrigger->pSchema==pTrigger->pTabSchema ){
147676 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
147678 *pp = (*pp)->pNext;
147685 db->mDbFlags |= DBFLAG_SchemaChange;
147701 for(e=0; e<pEList->nExpr; e++){
147702 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
147711 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
147712 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
147735 || (pList->bReturning && pList->pNext==0) );
147738 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
147739 && pTab->pTrigger!=0
147744 if( pList==pTab->pTrigger ){
147748 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
147749 p->pNext = 0;
147753 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
147754 mask |= p->tr_tm;
147755 }else if( p->op==TK_RETURNING ){
147759 p->op = op;
147766 p->tr_tm = TRIGGER_BEFORE;
147768 p->tr_tm = TRIGGER_AFTER;
147770 mask |= p->tr_tm;
147771 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
147774 mask |= p->tr_tm;
147776 p = p->pNext;
147793 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
147794 || pParse->disableTriggers
147803 ** Convert the pStep->zTarget string into a SrcList and return a pointer
147816 sqlite3 *db = pParse->db;
147818 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
147820 assert( pSrc==0 || pSrc->nSrc==1 );
147823 Schema *pSchema = pStep->pTrig->pSchema;
147824 pSrc->a[0].zName = zName;
147825 if( pSchema!=db->aDb[1].pSchema ){
147826 pSrc->a[0].pSchema = pSchema;
147828 if( pStep->pFrom ){
147829 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
147830 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
147856 if( pTerm->op==TK_ASTERISK ) return 1;
147857 if( pTerm->op!=TK_DOT ) return 0;
147858 assert( pTerm->pRight!=0 );
147859 assert( pTerm->pLeft!=0 );
147860 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
147877 sqlite3 *db = pParse->db;
147880 for(i=0; i<pList->nExpr; i++){
147881 Expr *pOldExpr = pList->a[i].pExpr;
147885 for(jj=0; jj<pTab->nCol; jj++){
147887 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
147888 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
147890 if( !db->mallocFailed ){
147891 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
147892 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
147893 pItem->fg.eEName = ENAME_NAME;
147899 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
147900 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
147901 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
147902 pItem->fg.eEName = pList->a[i].fg.eEName;
147912 ** is generated in-line.
147920 Vdbe *v = pParse->pVdbe;
147921 sqlite3 *db = pParse->db;
147928 assert( pParse->bReturning );
147929 assert( db->pParse==pParse );
147930 pReturning = pParse->u1.pReturning;
147931 assert( pTrigger == &(pReturning->retTrig) );
147934 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
147938 sFrom.a[0].iCursor = -1;
147940 if( pParse->nErr==0 ){
147941 assert( db->mallocFailed==0 );
147945 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
147946 if( !db->mallocFailed ){
147949 if( pReturning->nRetCol==0 ){
147950 pReturning->nRetCol = pNew->nExpr;
147951 pReturning->iRetCur = pParse->nTab++;
147956 pParse->eTriggerOp = pTrigger->op;
147957 pParse->pTriggerTab = pTab;
147959 && ALWAYS(!db->mallocFailed)
147962 int nCol = pNew->nExpr;
147963 int reg = pParse->nMem+1;
147964 pParse->nMem += nCol+2;
147965 pReturning->iRetReg = reg;
147967 Expr *pCol = pNew->a[i].pExpr;
147968 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
147975 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
147976 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
147980 pParse->eTriggerOp = 0;
147981 pParse->pTriggerTab = 0;
147996 Vdbe *v = pParse->pVdbe;
147997 sqlite3 *db = pParse->db;
147999 assert( pParse->pTriggerTab && pParse->pToplevel );
148002 for(pStep=pStepList; pStep; pStep=pStep->pNext){
148013 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
148014 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
148016 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
148017 assert( pParse->okConstFactor==0 );
148020 if( pStep->zSpan ){
148022 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
148027 switch( pStep->op ){
148031 sqlite3ExprListDup(db, pStep->pExprList, 0),
148032 sqlite3ExprDup(db, pStep->pWhere, 0),
148033 pParse->eOrconf, 0, 0, 0
148041 sqlite3SelectDup(db, pStep->pSelect, 0),
148042 sqlite3IdListDup(db, pStep->pIdList),
148043 pParse->eOrconf,
148044 sqlite3UpsertDup(db, pStep->pUpsert)
148052 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
148057 default: assert( pStep->op==TK_SELECT ); {
148059 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
148090 ** Parse context structure pFrom has just been used to create a sub-vdbe
148095 assert( pFrom->zErrMsg==0 || pFrom->nErr );
148096 assert( pTo->zErrMsg==0 || pTo->nErr );
148097 if( pTo->nErr==0 ){
148098 pTo->zErrMsg = pFrom->zErrMsg;
148099 pTo->nErr = pFrom->nErr;
148100 pTo->rc = pFrom->rc;
148102 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
148107 ** Create and populate a new TriggerPrg object with a sub-program
148117 sqlite3 *db = pParse->db; /* Database handle */
148120 Vdbe *v; /* Temporary VM */
148121 NameContext sNC; /* Name context for sub-vdbe */
148122 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
148124 Parse sSubParse; /* Parse context for sub-vdbe */
148126 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
148127 assert( pTop->pVdbe );
148131 ** list of the top-level Parse object sooner rather than later. */
148134 pPrg->pNext = pTop->pTriggerPrg;
148135 pTop->pTriggerPrg = pPrg;
148136 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
148138 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
148139 pPrg->pTrigger = pTrigger;
148140 pPrg->orconf = orconf;
148141 pPrg->aColmask[0] = 0xffffffff;
148142 pPrg->aColmask[1] = 0xffffffff;
148145 ** trigger sub-program. */
148151 sSubParse.zAuthContext = pTrigger->zName;
148152 sSubParse.eTriggerOp = pTrigger->op;
148153 sSubParse.nQueryLoop = pParse->nQueryLoop;
148154 sSubParse.prepFlags = pParse->prepFlags;
148159 pTrigger->zName, onErrorText(orconf),
148160 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
148161 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
148162 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
148163 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
148164 pTab->zName
148167 if( pTrigger->zName ){
148168 sqlite3VdbeChangeP4(v, -1,
148169 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
148175 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
148177 if( pTrigger->pWhen ){
148178 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
148179 if( db->mallocFailed==0
148188 /* Code the trigger program into the sub-vdbe. */
148189 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
148191 /* Insert an OP_Halt at the end of the sub-program. */
148196 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
148199 if( pParse->nErr==0 ){
148200 assert( db->mallocFailed==0 );
148201 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
148203 pProgram->nMem = sSubParse.nMem;
148204 pProgram->nCsr = sSubParse.nTab;
148205 pProgram->token = (void *)pTrigger;
148206 pPrg->aColmask[0] = sSubParse.oldmask;
148207 pPrg->aColmask[1] = sSubParse.newmask;
148219 ** Return a pointer to a TriggerPrg object containing the sub-program for
148233 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
148239 for(pPrg=pRoot->pTriggerPrg;
148240 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
148241 pPrg=pPrg->pNext
148247 pParse->db->errByteOffset = -1;
148267 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
148270 assert( pPrg || pParse->nErr );
148273 ** is a pointer to the sub-vdbe containing the trigger program. */
148275 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
148277 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
148278 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
148280 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
148282 /* Set the P5 operand of the OP_Program instruction to non-zero if
148284 ** invocation is disallowed if (a) the sub-program is really a trigger,
148299 ** operation on pTab, this function is a no-op.
148304 ** (a copy of pTab->nCol), then registers are populated as follows:
148307 ** ------------------------------------------------------
148309 ** reg+1 OLD.* value of left-most column of pTab
148311 ** reg+N OLD.* value of right-most column of pTab
148313 ** reg+N+2 NEW.* value of left-most column of pTab
148315 ** reg+N+N+1 NEW.* value of right-most column of pTab
148348 for(p=pTrigger; p; p=p->pNext){
148353 assert( p->pSchema!=0 );
148354 assert( p->pTabSchema!=0 );
148355 assert( p->pSchema==p->pTabSchema
148356 || p->pSchema==pParse->db->aDb[1].pSchema );
148363 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
148364 && p->tr_tm==tr_tm
148365 && checkColumnOverlap(p->pColumns, pChanges)
148367 if( !p->bReturning ){
148377 ** Triggers may access values stored in the old.* or new.* pseudo-table.
148378 ** This function returns a 32-bit bitmask indicating which columns of the
148383 ** Bit 0 of the returned mask is set if the left-most column of the
148415 for(p=pTrigger; p; p=p->pNext){
148416 if( p->op==op
148417 && (tr_tm&p->tr_tm)
148418 && checkColumnOverlap(p->pColumns,pChanges)
148420 if( p->bReturning ){
148426 mask |= pPrg->aColmask[isNew];
148471 ** i-th column of table pTab. This routine sets the P4 parameter of the
148477 ** command. If the latter, then the row-records in the table btree on disk
148480 ** If the former, then all row-records are guaranteed to include a value
148494 ** If column as REAL affinity and the table is an ordinary b-tree table
148502 assert( pTab->nCol>i );
148503 pCol = &pTab->aCol[i];
148504 if( pCol->iDflt ){
148508 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
148509 assert( i<pTab->nCol );
148512 pCol->affinity, &pValue);
148518 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
148527 ** and false if not. This is an optimization. False-positives are a
148528 ** performance degradation, but false-negatives can result in a corrupt
148531 ** aXRef[j] will be non-negative if column j of the original table is
148541 i16 iIdxCol = pIdx->aiColumn[iCol];
148547 assert( pIdx->aColExpr!=0 );
148548 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
148549 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
148557 ** to be unchanged. This is an optimization. False-positives are a
148558 ** performance degradation, but false-negatives can result in a corrupt
148561 ** aXRef[j] will be non-negative if column j of the original table is
148570 if( pIdx->pPartIdxWhere==0 ) return 0;
148571 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
148579 ** table in the source-list (pSrc->a[0]).
148583 if( pRet ) pRet->iColumn = iCol+1;
148589 ** generates VM code to run the query:
148591 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
148599 ** SELECT <other-columns>, pChanges FROM pTabList
148601 ** GROUP BY <other-columns>
148607 ** the <other-columns> in the query above are is determined by the type
148608 ** of table pTabList->a[0].pTab.
148611 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
148615 ** If the table is actually a view, then <other-columns> are all columns of
148619 ** If the table is a virtual or ordinary intkey table, then <other-columns>
148622 ** rowid value in <other-columns> is used as the integer key, and the
148642 sqlite3 *db = pParse->db;
148643 Table *pTab = pTabList->a[0].pTab;
148663 assert( pTabList->nSrc>1 );
148665 pSrc->a[0].fg.notCte = 1;
148666 pSrc->a[0].iCursor = -1;
148667 pSrc->a[0].pTab->nTabRef--;
148668 pSrc->a[0].pTab = 0;
148671 for(i=0; i<pPk->nKeyCol; i++){
148672 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
148682 for(i=0; i<pTab->nCol; i++){
148695 assert( pChanges!=0 || pParse->db->mallocFailed );
148697 for(i=0; i<pChanges->nExpr; i++){
148699 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
148706 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
148708 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
148746 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
148747 ** an expression for the i-th column of the table.
148748 ** aXRef[i]==-1 if the i-th column is not changed. */
148754 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
148756 NameContext sNC; /* The name-context to resolve expressions in */
148778 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
148790 db = pParse->db;
148791 assert( db->pParse==pParse );
148792 if( pParse->nErr ){
148795 assert( db->mallocFailed==0 );
148801 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
148823 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
148829 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
148832 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
148857 iBaseCur = iDataCur = pParse->nTab++;
148860 testcase( pPk!=0 && pPk!=pTab->pIndex );
148861 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
148863 iDataCur = pParse->nTab;
148865 pParse->nTab++;
148869 iDataCur = pUpsert->iDataCur;
148870 iIdxCur = pUpsert->iIdxCur;
148871 pParse->nTab = iBaseCur;
148873 pTabList->a[0].iCursor = iDataCur;
148878 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
148880 aRegIdx = aXRef+pTab->nCol;
148884 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
148886 /* Initialize the name-context */
148904 for(i=0; i<pChanges->nExpr; i++){
148905 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
148908 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
148911 for(j=0; j<pTab->nCol; j++){
148912 if( pTab->aCol[j].hName==hCol
148913 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
148915 if( j==pTab->iPKey ){
148917 pRowidExpr = pChanges->a[i].pExpr;
148919 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
148923 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
148924 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
148925 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
148928 pTab->aCol[j].zCnName);
148936 if( j>=pTab->nCol ){
148937 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
148938 j = -1;
148940 pRowidExpr = pChanges->a[i].pExpr;
148943 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
148944 pParse->checkSchema = 1;
148951 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
148952 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
148953 db->aDb[iDb].zDbSName);
148957 aXRef[j] = -1;
148971 ** is non-negative, so the value of aXRef[] for generated columns can be
148972 ** set to any non-negative number. We use 99999 so that the value is
148975 if( pTab->tabFlags & TF_HasGenerated ){
148977 testcase( pTab->tabFlags & TF_HasVirtual );
148978 testcase( pTab->tabFlags & TF_HasStored );
148981 for(i=0; i<pTab->nCol; i++){
148983 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
148985 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
149001 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
149010 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
149015 reg = ++pParse->nMem;
149016 pParse->nMem += pIdx->nColumn;
149019 for(i=0; i<pIdx->nKeyCol; i++){
149021 reg = ++pParse->nMem;
149022 pParse->nMem += pIdx->nColumn;
149023 if( onError==OE_Default && pIdx->onError==OE_Replace ){
149033 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
149040 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
149049 ** two-pass update algorithm. */
149050 assert( aRegIdx[nAllIdx]==pParse->nMem );
149052 regOldRowid = regNewRowid = ++pParse->nMem;
149054 regOld = pParse->nMem + 1;
149055 pParse->nMem += pTab->nCol;
149058 regNewRowid = ++pParse->nMem;
149060 regNew = pParse->nMem + 1;
149061 pParse->nMem += pTab->nCol;
149066 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
149103 if( (db->flags&SQLITE_CountRows)!=0
149104 && !pParse->pTriggerTab
149105 && !pParse->nested
149106 && !pParse->bReturning
149109 regRowCount = ++pParse->nMem;
149115 iEph = pParse->nTab++;
149119 nPk = pPk ? pPk->nKeyCol : 0;
149120 iPk = pParse->nMem+1;
149121 pParse->nMem += nPk;
149122 pParse->nMem += nChangeFrom;
149123 regKey = ++pParse->nMem;
149125 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
149126 iEph = pParse->nTab++;
149127 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
149132 pKeyInfo->nAllField = nEphCol;
149166 ** Do not consider a single-pass strategy for a multi-row update if
149170 ** or index, causing a single-pass approach to malfunction. */
149172 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
149178 /* A one-pass strategy that might update more than one row may not
149194 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
149204 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
149208 aRegIdx[nAllIdx] = ++pParse->nMem;
149220 assert( pPk->aiColumn[i]>=0 );
149222 pPk->aiColumn[i], iPk+i);
149246 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
149247 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
149250 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
149328 /* Compute the old pre-UPDATE content of the row being changed, if that
149335 for(i=0; i<pTab->nCol; i++){
149336 u32 colFlags = pTab->aCol[i].colFlags;
149369 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
149370 if( i==pTab->iPKey ){
149372 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
149373 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
149378 int nOff = (isView ? pTab->nCol : nPk);
149382 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
149400 if( pTab->tabFlags & TF_HasGenerated ){
149401 testcase( pTab->tabFlags & TF_HasVirtual );
149402 testcase( pTab->tabFlags & TF_HasStored );
149416 /* The row-trigger may have deleted the row being updated. In this
149418 ** required. This behavior - what happens when the row being updated
149419 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
149430 /* After-BEFORE-trigger-reload-loop:
149436 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
149439 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
149440 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
149441 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
149442 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
149447 if( pTab->tabFlags & TF_HasGenerated ){
149448 testcase( pTab->tabFlags & TF_HasVirtual );
149449 testcase( pTab->tabFlags & TF_HasStored );
149481 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
149494 ** to invoke the pre-update hook.
149497 ** pre-update hook. If the caller invokes preupdate_new(), the returned
149511 if( !pParse->nested ){
149552 /* Nothing to do at end-of-loop for a single-pass */
149566 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
149604 ** There are two possible strategies - the default and the special
149632 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
149635 sqlite3 *db = pParse->db; /* Database connection */
149638 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
149642 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
149651 ephemTab = pParse->nTab++;
149653 regArg = pParse->nMem + 1;
149654 pParse->nMem += nArg;
149655 if( pSrc->nSrc>1 ){
149669 assert( pPk->nKeyCol==1 );
149670 iPk = pPk->aiColumn[0];
149672 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
149679 for(i=0; i<pTab->nCol; i++){
149682 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
149693 regRec = ++pParse->nMem;
149694 regRowid = ++pParse->nMem;
149703 for(i=0; i<pTab->nCol; i++){
149704 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
149706 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
149724 assert( pPk->nKeyCol==1 );
149725 iPk = pPk->aiColumn[0];
149736 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
149747 ** accept no-change records with serial_type 10 */
149758 if( pSrc->nSrc==1 ){
149791 ** 2018-04-12
149812 Upsert *pNext = p->pNextUpsert;
149813 sqlite3ExprListDelete(db, p->pUpsertTarget);
149814 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
149815 sqlite3ExprListDelete(db, p->pUpsertSet);
149816 sqlite3ExprDelete(db, p->pUpsertWhere);
149817 sqlite3DbFree(db, p->pToFree);
149833 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
149834 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
149835 sqlite3ExprListDup(db, p->pUpsertSet, 0),
149836 sqlite3ExprDup(db, p->pUpsertWhere, 0),
149837 sqlite3UpsertDup(db, p->pNextUpsert)
149862 pNew->pUpsertTarget = pTarget;
149863 pNew->pUpsertTargetWhere = pTargetWhere;
149864 pNew->pUpsertSet = pSet;
149865 pNew->pUpsertWhere = pWhere;
149866 pNew->isDoUpdate = pSet!=0;
149867 pNew->pNextUpsert = pNext;
149874 ** symbols in the conflict-target.
149888 ExprList *pTarget; /* The conflict-target clause */
149889 Expr *pTerm; /* One term of the conflict-target clause */
149894 assert( pTabList->nSrc==1 );
149895 assert( pTabList->a[0].pTab!=0 );
149897 assert( pUpsert->pUpsertTarget!=0 );
149899 /* Resolve all symbolic names in the conflict-target clause, which
149900 ** includes both the list of columns and the optional partial-index
149906 for(; pUpsert && pUpsert->pUpsertTarget;
149907 pUpsert=pUpsert->pNextUpsert, nClause++){
149908 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
149910 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
149914 pTab = pTabList->a[0].pTab;
149915 pTarget = pUpsert->pUpsertTarget;
149916 iCursor = pTabList->a[0].iCursor;
149918 && pTarget->nExpr==1
149919 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
149920 && pTerm->iColumn==XN_ROWID
149922 /* The conflict-target is the rowid of the primary table */
149923 assert( pUpsert->pUpsertIdx==0 );
149931 ** prior to comparing against the conflict-target expression.
149937 sCol[1].iTable = pTabList->a[0].iCursor;
149940 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149943 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
149944 if( pIdx->pPartIdxWhere ){
149945 if( pUpsert->pUpsertTargetWhere==0 ) continue;
149946 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
149947 pIdx->pPartIdxWhere, iCursor)!=0 ){
149951 nn = pIdx->nKeyCol;
149954 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
149955 if( pIdx->aiColumn[ii]==XN_EXPR ){
149956 assert( pIdx->aColExpr!=0 );
149957 assert( pIdx->aColExpr->nExpr>ii );
149958 assert( pIdx->bHasExpr );
149959 pExpr = pIdx->aColExpr->a[ii].pExpr;
149960 if( pExpr->op!=TK_COLLATE ){
149966 sCol[1].iColumn = pIdx->aiColumn[ii];
149970 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
149984 pUpsert->pUpsertIdx = pIdx;
149987 if( pUpsert->pUpsertIdx==0 ){
149989 if( nClause==0 && pUpsert->pNextUpsert==0 ){
150010 pNext = pUpsert->pNextUpsert;
150012 if( pNext->pUpsertTarget==0 ) return 1;
150013 if( pNext->pUpsertIdx==0 ) return 1;
150026 && pUpsert->pUpsertTarget!=0
150027 && pUpsert->pUpsertIdx!=pIdx
150029 pUpsert = pUpsert->pNextUpsert;
150038 ** In this case parameter iCur is a cursor open on the table b-tree that
150044 Parse *pParse, /* The parsing and code-generating context */
150050 Vdbe *v = pParse->pVdbe;
150051 sqlite3 *db = pParse->db;
150059 iDataCur = pUpsert->iDataCur;
150071 int nPk = pPk->nKeyCol;
150072 int iPk = pParse->nMem+1;
150073 pParse->nMem += nPk;
150076 assert( pPk->aiColumn[i]>=0 );
150077 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
150079 VdbeComment((v, "%s.%s", pIdx->zName,
150080 pTab->aCol[pPk->aiColumn[i]].zCnName));
150091 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
150093 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
150095 for(i=0; i<pTab->nCol; i++){
150096 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
150097 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
150100 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
150101 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
150145 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
150217 if( pParse->nErr ) goto build_vacuum_end;
150226 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
150227 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
150230 iDb = sqlite3FindDb(pParse->db, pNm);
150237 iIntoReg = ++pParse->nMem;
150244 sqlite3ExprDelete(pParse->db, pInto);
150260 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
150261 u64 saved_flags; /* Saved value of db->flags */
150262 i64 saved_nChange; /* Saved value of db->nChange */
150263 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
150264 u32 saved_openFlags; /* Saved value of db->openFlags */
150274 if( !db->autoCommit ){
150276 return SQLITE_ERROR; /* IMP: R-12218-18073 */
150278 if( db->nVdbeActive>1 ){
150279 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
150280 return SQLITE_ERROR; /* IMP: R-15610-35227 */
150282 saved_openFlags = db->openFlags;
150285 sqlite3SetString(pzErrMsg, db, "non-text filename");
150289 db->openFlags &= ~SQLITE_OPEN_READONLY;
150290 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
150296 ** restored before returning. Then set the writable-schema flag, and
150298 saved_flags = db->flags;
150299 saved_mDbFlags = db->mDbFlags;
150300 saved_nChange = db->nChange;
150301 saved_nTotalChange = db->nTotalChange;
150302 saved_mTrace = db->mTrace;
150303 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
150304 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
150305 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
150307 db->mTrace = 0;
150309 zDbMain = db->aDb[iDb].zDbSName;
150310 pMain = db->aDb[iDb].pBt;
150319 ** An optimisation would be to use a non-journaled pager.
150327 nDb = db->nDb;
150329 db->openFlags = saved_openFlags;
150331 assert( (db->nDb-1)==nDb );
150332 pDb = &db->aDb[nDb];
150333 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
150334 pTemp = pDb->pBt;
150338 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
150343 db->mDbFlags |= DBFLAG_VacuumInto;
150345 /* For a VACUUM INTO, the pager-flags are set to the same values as
150348 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
150353 if( db->nextPagesize ){
150358 if( nKey ) db->nextPagesize = 0;
150362 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
150368 ** to ensure that we do not try to change the page-size on a WAL database.
150380 db->nextPagesize = 0;
150384 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
150385 || NEVER(db->mallocFailed)
150392 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
150399 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
150413 db->init.iDb = 0;
150426 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
150427 db->mDbFlags &= ~DBFLAG_Vacuum;
150446 ** both transactions are closed by this block - the main database
150500 /* Restore the original value of db->flags */
150501 db->init.iDb = 0;
150502 db->mDbFlags = saved_mDbFlags;
150503 db->flags = saved_flags;
150504 db->nChange = saved_nChange;
150505 db->nTotalChange = saved_nTotalChange;
150506 db->mTrace = saved_mTrace;
150507 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
150516 db->autoCommit = 1;
150519 sqlite3BtreeClose(pDb->pBt);
150520 pDb->pBt = 0;
150521 pDb->pSchema = 0;
150524 /* This both clears the schemas and reduces the size of the db->aDb[]
150595 pMod->zName = zCopy;
150596 pMod->pModule = pModule;
150597 pMod->pAux = pAux;
150598 pMod->xDestroy = xDestroy;
150599 pMod->pEpoTab = 0;
150600 pMod->nRefModule = 1;
150602 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
150630 sqlite3_mutex_enter(db->mutex);
150634 sqlite3_mutex_leave(db->mutex);
150640 ** External API function used to create a new virtual-table module.
150655 ** External API function used to create a new virtual-table module.
150671 ** External API to drop all virtual-table modules, except those named
150679 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
150684 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
150687 createModule(db, pMod->zName, 0, 0, 0);
150697 assert( pMod->nRefModule>0 );
150698 pMod->nRefModule--;
150699 if( pMod->nRefModule==0 ){
150700 if( pMod->xDestroy ){
150701 pMod->xDestroy(pMod->pAux);
150703 assert( pMod->pEpoTab==0 );
150717 pVTab->nRef++;
150722 ** pTab is a pointer to a Table structure representing a virtual-table.
150724 ** this virtual-table, if one has been created, or NULL otherwise.
150729 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
150734 ** Decrement the ref-count on a virtual table object. When the ref-count
150738 sqlite3 *db = pVTab->db;
150741 assert( pVTab->nRef>0 );
150742 assert( db->eOpenState==SQLITE_STATE_OPEN
150743 || db->eOpenState==SQLITE_STATE_ZOMBIE );
150745 pVTab->nRef--;
150746 if( pVTab->nRef==0 ){
150747 sqlite3_vtab *p = pVTab->pVtab;
150748 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
150750 p->pModule->xDisconnect(p);
150758 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
150761 ** connection db is left in the p->u.vtab.p list.
150768 pVTable = p->u.vtab.p;
150769 p->u.vtab.p = 0;
150775 ** database connection that may have an entry in the p->u.vtab.p list.
150777 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
150780 sqlite3 *db2 = pVTable->db;
150781 VTable *pNext = pVTable->pNext;
150785 p->u.vtab.p = pRet;
150786 pRet->pNext = 0;
150788 pVTable->pNext = db2->pDisconnect;
150789 db2->pDisconnect = pVTable;
150801 ** list in p->pVTab. It also decrements the VTable ref count. This is
150804 ** be being used by other shared-cache connections).
150811 assert( sqlite3_mutex_held(db->mutex) );
150813 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
150814 if( (*ppVTab)->db==db ){
150816 *ppVTab = pVTab->pNext;
150828 ** shared b-tree databases opened using connection db are held by the
150838 ** or, if the virtual table is stored in a non-sharable database, then
150842 ** by multiple threads. It is thread-safe.
150845 VTable *p = db->pDisconnect;
150848 assert( sqlite3_mutex_held(db->mutex) );
150851 db->pDisconnect = 0;
150854 VTable *pNext = p->pNext;
150862 ** Clear any and all virtual-table information from the Table record.
150866 ** Since it is a virtual-table, the Table structure contains a pointer
150878 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
150879 if( p->u.vtab.azArg ){
150881 for(i=0; i<p->u.vtab.nArg; i++){
150882 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
150884 sqlite3DbFree(db, p->u.vtab.azArg);
150889 ** Add a new module argument to pTable->u.vtab.azArg[].
150890 ** The string is not copied - the pointer is stored. The
150897 sqlite3 *db = pParse->db;
150900 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
150901 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
150902 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
150904 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
150908 int i = pTable->u.vtab.nArg++;
150911 pTable->u.vtab.azArg = azModuleArg;
150931 pTable = pParse->pNewTable;
150933 assert( 0==pTable->pIndex );
150934 pTable->eTabType = TABTYP_VTAB;
150936 db = pParse->db;
150938 assert( pTable->u.vtab.nArg==0 );
150941 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
150942 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
150943 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
150945 pParse->sNameToken.n = (int)(
150946 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
150955 if( pTable->u.vtab.azArg ){
150956 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
150958 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
150959 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
150966 ** in pParse->zArg[] and appends it to the list of arguments on the
150967 ** virtual table currently under construction in pParse->pTable.
150970 if( pParse->sArg.z && pParse->pNewTable ){
150971 const char *z = (const char*)pParse->sArg.z;
150972 int n = pParse->sArg.n;
150973 sqlite3 *db = pParse->db;
150974 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
150983 Table *pTab = pParse->pNewTable; /* The table being constructed */
150984 sqlite3 *db = pParse->db; /* The database connection */
150989 pParse->sArg.z = 0;
150990 if( pTab->u.vtab.nArg<1 ) return;
150998 if( !db->init.busy ){
151009 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
151011 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
151017 ** The VM register number pParse->regRowid holds the rowid of an
151021 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151026 db->aDb[iDb].zDbSName,
151027 pTab->zName,
151028 pTab->zName,
151030 pParse->regRowid
151036 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
151040 iReg = ++pParse->nMem;
151041 sqlite3VdbeLoadString(v, iReg, pTab->zName);
151044 /* If we are rereading the sqlite_schema table create the in-memory
151047 Schema *pSchema = pTab->pSchema;
151048 const char *zName = pTab->zName;
151051 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
151057 pParse->pNewTable = 0;
151067 pParse->sArg.z = 0;
151068 pParse->sArg.n = 0;
151076 Token *pArg = &pParse->sArg;
151077 if( pArg->z==0 ){
151078 pArg->z = p->z;
151079 pArg->n = p->n;
151081 assert(pArg->z <= p->z);
151082 pArg->n = (int)(&p->z[p->n] - pArg->z);
151102 int nArg = pTab->u.vtab.nArg;
151109 azArg = (const char *const*)pTab->u.vtab.azArg;
151111 /* Check that the virtual-table is not already being initialized */
151112 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
151113 if( pCtx->pTab==pTab ){
151115 "vtable constructor called recursively: %s", pTab->zName
151121 zModuleName = sqlite3DbStrDup(db, pTab->zName);
151132 pVTable->db = db;
151133 pVTable->pMod = pMod;
151134 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
151136 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151137 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
151140 assert( &db->pVtabCtx );
151144 sCtx.pPrior = db->pVtabCtx;
151146 db->pVtabCtx = &sCtx;
151147 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
151148 db->pVtabCtx = sCtx.pPrior;
151160 }else if( ALWAYS(pVTable->pVtab) ){
151163 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
151164 pVTable->pVtab->pModule = pMod->pModule;
151165 pMod->nRefModule++;
151166 pVTable->nRef = 1;
151169 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
151176 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
151180 pVTable->pNext = pTab->u.vtab.p;
151181 pTab->u.vtab.p = pVTable;
151183 for(iCol=0; iCol<pTab->nCol; iCol++){
151184 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
151190 && (i==0 || zType[i-1]==' ')
151203 assert(zType[i-1]==' ');
151204 zType[i-1] = '\0';
151206 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
151207 pTab->tabFlags |= TF_HasHidden;
151210 pTab->tabFlags |= oooHidden;
151225 ** This call is a no-op if table pTab is not a virtual table.
151228 sqlite3 *db = pParse->db;
151240 zMod = pTab->u.vtab.azArg[0];
151241 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
151244 const char *zModule = pTab->u.vtab.azArg[0];
151249 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
151252 pParse->rc = rc;
151260 ** Grow the db->aVTrans[] array so that there is room for at least one
151261 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
151267 if( (db->nVTrans%ARRAY_INCR)==0 ){
151270 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
151271 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
151275 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
151276 db->aVTrans = aVTrans;
151288 db->aVTrans[db->nVTrans++] = pVTab;
151306 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
151307 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
151310 zMod = pTab->u.vtab.azArg[0];
151311 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
151317 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
151321 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
151353 sqlite3_mutex_enter(db->mutex);
151354 pCtx = db->pVtabCtx;
151355 if( !pCtx || pCtx->bDeclared ){
151357 sqlite3_mutex_leave(db->mutex);
151360 pTab = pCtx->pTab;
151367 ** schema. Nevertheless, defend against that (turn off db->init.busy)
151369 assert( db->init.busy==0 );
151370 initBusy = db->init.busy;
151371 db->init.busy = 0;
151375 && ALWAYS(!db->mallocFailed)
151379 if( !pTab->aCol ){
151382 pTab->aCol = pNew->aCol;
151383 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
151384 pTab->nNVCol = pTab->nCol = pNew->nCol;
151385 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
151386 pNew->nCol = 0;
151387 pNew->aCol = 0;
151388 assert( pTab->pIndex==0 );
151391 && pCtx->pVTable->pMod->pModule->xUpdate!=0
151392 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
151394 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
151395 ** or else must have a single-column PRIMARY KEY */
151398 pIdx = pNew->pIndex;
151400 assert( pIdx->pNext==0 );
151401 pTab->pIndex = pIdx;
151402 pNew->pIndex = 0;
151403 pIdx->pTable = pTab;
151406 pCtx->bDeclared = 1;
151420 db->init.busy = initBusy;
151424 sqlite3_mutex_leave(db->mutex);
151433 ** This call is a no-op if zTab is not a virtual table.
151439 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
151442 && ALWAYS(pTab->u.vtab.p!=0)
151446 for(p=pTab->u.vtab.p; p; p=p->pNext){
151447 assert( p->pVtab );
151448 if( p->pVtab->nRef>0 ){
151453 xDestroy = p->pMod->pModule->xDestroy;
151454 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
151456 pTab->nTabRef++;
151457 rc = xDestroy(p->pVtab);
151460 assert( pTab->u.vtab.p==p && p->pNext==0 );
151461 p->pVtab = 0;
151462 pTab->u.vtab.p = 0;
151481 if( db->aVTrans ){
151482 VTable **aVTrans = db->aVTrans;
151483 db->aVTrans = 0;
151484 for(i=0; i<db->nVTrans; i++){
151486 sqlite3_vtab *p = pVTab->pVtab;
151489 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
151492 pVTab->iSavepoint = 0;
151496 db->nVTrans = 0;
151505 ** If an error message is available, leave it in p->zErrMsg.
151510 VTable **aVTrans = db->aVTrans;
151512 db->aVTrans = 0;
151513 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
151515 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
151516 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
151521 db->aVTrans = aVTrans;
151555 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
151566 pModule = pVTab->pVtab->pModule;
151568 if( pModule->xBegin ){
151572 for(i=0; i<db->nVTrans; i++){
151573 if( db->aVTrans[i]==pVTab ){
151582 rc = pModule->xBegin(pVTab->pVtab);
151584 int iSvpt = db->nStatement + db->nSavepoint;
151586 if( iSvpt && pModule->xSavepoint ){
151587 pVTab->iSavepoint = iSvpt;
151588 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
151615 assert( iSavepoint>=-1 );
151616 if( db->aVTrans ){
151618 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
151619 VTable *pVTab = db->aVTrans[i];
151620 const sqlite3_module *pMod = pVTab->pMod->pModule;
151621 if( pVTab->pVtab && pMod->iVersion>=2 ){
151626 xMethod = pMod->xSavepoint;
151627 pVTab->iSavepoint = iSavepoint+1;
151630 xMethod = pMod->xRollbackTo;
151633 xMethod = pMod->xRelease;
151636 if( xMethod && pVTab->iSavepoint>iSavepoint ){
151637 rc = xMethod(pVTab->pVtab, iSavepoint);
151675 if( pExpr->op!=TK_COLUMN ) return pDef;
151677 pTab = pExpr->y.pTab;
151680 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
151682 assert( pVtab->pModule!=0 );
151683 pMod = (sqlite3_module *)pVtab->pModule;
151684 if( pMod->xFindFunction==0 ) return pDef;
151690 ** with an all lower-case function name. Continue in this tradition to
151696 for(i=0; pDef->zName[i]; i++){
151697 unsigned char x = (unsigned char)pDef->zName[i];
151702 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
151710 + sqlite3Strlen30(pDef->zName) + 1);
151715 pNew->zName = (const char*)&pNew[1];
151716 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
151717 pNew->xSFunc = xSFunc;
151718 pNew->pUserData = pArg;
151719 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
151724 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
151727 ** is a no-op.
151735 for(i=0; i<pToplevel->nVtabLock; i++){
151736 if( pTab==pToplevel->apVtabLock[i] ) return;
151738 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
151739 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
151741 pToplevel->apVtabLock = apVtabLock;
151742 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
151744 sqlite3OomFault(pToplevel->db);
151751 ** exist. Return non-zero if either the eponymous virtual table instance
151758 ** instances always exist. They cannot be DROP-ed.
151764 const sqlite3_module *pModule = pMod->pModule;
151768 sqlite3 *db = pParse->db;
151769 if( pMod->pEpoTab ) return 1;
151770 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
151773 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
151774 if( pTab->zName==0 ){
151778 pMod->pEpoTab = pTab;
151779 pTab->nTabRef = 1;
151780 pTab->eTabType = TABTYP_VTAB;
151781 pTab->pSchema = db->aDb[0].pSchema;
151782 assert( pTab->u.vtab.nArg==0 );
151783 pTab->iPKey = -1;
151784 pTab->tabFlags |= TF_Eponymous;
151785 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
151787 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
151788 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
151802 Table *pTab = pMod->pEpoTab;
151807 pTab->tabFlags |= TF_Ephemeral;
151809 pMod->pEpoTab = 0;
151829 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
151830 return (int)aMap[db->vtabOnConflict-1];
151846 sqlite3_mutex_enter(db->mutex);
151847 p = db->pVtabCtx;
151851 assert( p->pTab==0 || IsVirtual(p->pTab) );
151855 p->pVTable->bConstraint = (u8)va_arg(ap, int);
151859 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
151863 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
151875 sqlite3_mutex_leave(db->mutex);
151884 ** 2015-06-06
151897 ** This file was split off from where.c on 2015-06-06 in order to reduce the
151906 ** 2013-11-12
151975 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
151983 int addrSkip; /* Jump here for next iteration of skip-scan */
151987 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
151988 int addrBignull; /* Jump here for next part of big-null scan */
151998 union { /* Information that depends on pWLoop->wsFlags */
152004 int iBase; /* Base register of multi-key index record */
152008 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
152022 ** prevent a query solution - which is an error) and many terms of the
152024 ** potential way of implementing that FROM-clause term, together with
152040 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
152083 ** correspond to the subquery(s) of OR-clause processing. Only the
152099 ** vector, not a scalar, and because dependencies are many-to-one, not
152100 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
152116 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
152129 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
152160 ** spread out over the non-negative integers. For example, the cursor
152179 int iParent; /* Disable pWC->a[iParent] when this term disabled */
152189 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
152202 #define TERM_OK 0x0040 /* Used during OR-clause processing */
152208 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
152215 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
152226 int k; /* Resume scanning at this->pWC->a[this->k] */
152232 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
152254 int nBase; /* Number of terms through the last non-Virtual */
152292 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
152293 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
152300 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
152301 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
152340 ** routine. The limit is high enough that is should not impact real-world
152378 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
152387 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
152401 ** Private interfaces - callable only by other where.c routines.
152453 int iLevel, /* Which level of pWInfo->a[] should be coded */
152481 ** OR-ed combination of these values can be used when searching for
152493 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
152494 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
152495 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
152496 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
152500 #define WO_OR 0x0200 /* Two or more OR-connected terms */
152501 #define WO_AND 0x0400 /* Two or more AND-connected terms */
152504 #define WO_ROWVAL 0x2000 /* A row-value term */
152507 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
152522 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
152530 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
152535 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
152537 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
152540 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
152550 ** Return the name of the i-th column of the pIdx index.
152553 i = pIdx->aiColumn[i];
152556 return pIdx->pTable->aCol[i].zCnName;
152571 int iTerm, /* Zero-based index of first term. */
152572 int bAnd, /* Non-zero to append " AND " */
152612 Index *pIndex = pLoop->u.btree.pIndex;
152613 u16 nEq = pLoop->u.btree.nEq;
152614 u16 nSkip = pLoop->nSkip;
152617 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
152626 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
152627 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
152630 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
152631 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
152637 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
152639 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
152642 ** If an OP_Explain opcode is added to the VM, its address is returned.
152653 if( sqlite3ParseToplevel(pParse)->explain==2 )
152656 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
152657 Vdbe *v = pParse->pVdbe; /* VM being constructed */
152658 sqlite3 *db = pParse->db; /* Database handle */
152666 pLoop = pLevel->pWLoop;
152667 flags = pLoop->wsFlags;
152671 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
152681 assert( pLoop->u.btree.pIndex!=0 );
152682 pIdx = pLoop->u.btree.pIndex;
152684 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
152699 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
152705 const Table *pTab = pItem->pTab;
152706 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
152728 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
152731 if( pItem->fg.jointype & JT_LEFT ){
152732 sqlite3_str_appendf(&str, " LEFT-JOIN");
152735 if( pLoop->nOut>=10 ){
152737 sqlite3LogEstToInt(pLoop->nOut));
152745 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
152755 ** required and this routine is a no-op.
152757 ** If an OP_Explain opcode is added to the VM, its address is returned.
152766 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
152767 Vdbe *v = pParse->pVdbe; /* VM being constructed */
152768 sqlite3 *db = pParse->db; /* Database handle */
152778 pLoop = pLevel->pWLoop;
152779 if( pLoop->wsFlags & WHERE_IPK ){
152780 const Table *pTab = pItem->pTab;
152781 if( pTab->iPKey>=0 ){
152782 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
152787 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
152788 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
152789 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
152796 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
152803 ** Configure the VM passed as the first argument with an
152818 WhereLoop *pLoop = pLvl->pWLoop;
152819 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
152820 zObj = pLoop->u.btree.pIndex->zName;
152822 zObj = pSrclist->a[pLvl->iFrom].zName;
152825 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
152871 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
152876 while( (pTerm->wtFlags & TERM_CODED)==0
152877 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
152878 && (pLevel->notReady & pTerm->prereqAll)==0
152880 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
152881 pTerm->wtFlags |= TERM_LIKECOND;
152883 pTerm->wtFlags |= TERM_CODED;
152887 sqlite3DebugPrintf("DISABLE-");
152888 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
152891 if( pTerm->iParent<0 ) break;
152892 pTerm = &pTerm->pWC->a[pTerm->iParent];
152894 pTerm->nChild--;
152895 if( pTerm->nChild!=0 ) break;
152905 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
152912 Vdbe *v = pParse->pVdbe;
152914 assert( pParse->db->mallocFailed );
152924 n--;
152928 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
152929 n--;
153000 sqlite3 *db = pParse->db;
153003 if( db->mallocFailed==0 ){
153012 pOrigRhs = pNew->x.pSelect->pEList;
153013 assert( pNew->pLeft!=0 );
153014 assert( ExprUseXList(pNew->pLeft) );
153015 pOrigLhs = pNew->pLeft->x.pList;
153016 for(i=iEq; i<pLoop->nLTerm; i++){
153017 if( pLoop->aLTerm[i]->pExpr==pX ){
153019 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
153020 iField = pLoop->aLTerm[i]->u.x.iField - 1;
153021 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
153022 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
153023 pOrigRhs->a[iField].pExpr = 0;
153024 assert( pOrigLhs->a[iField].pExpr!=0 );
153025 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
153026 pOrigLhs->a[iField].pExpr = 0;
153031 pNew->pLeft->x.pList = pLhs;
153032 pNew->x.pSelect->pEList = pRhs;
153033 if( pLhs && pLhs->nExpr==1 ){
153037 Expr *p = pLhs->a[0].pExpr;
153038 pLhs->a[0].pExpr = 0;
153039 sqlite3ExprDelete(db, pNew->pLeft);
153040 pNew->pLeft = p;
153042 pSelect = pNew->x.pSelect;
153043 if( pSelect->pOrderBy ){
153045 ** iOrderByCol variables. These are set to non-zero when an
153047 ** result-set. Since the result-set of the SELECT statement may
153051 ExprList *pOrderBy = pSelect->pOrderBy;
153052 for(i=0; i<pOrderBy->nExpr; i++){
153053 pOrderBy->a[i].u.x.iOrderByCol = 0;
153080 ** straight-line code. For constraints of the form X IN (...)
153088 int bRev, /* True for reverse-order IN operations */
153091 Expr *pX = pTerm->pExpr;
153092 Vdbe *v = pParse->pVdbe;
153095 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
153097 if( pX->op==TK_EQ || pX->op==TK_IS ){
153098 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
153099 }else if( pX->op==TK_ISNULL ){
153107 WhereLoop *pLoop = pLevel->pWLoop;
153112 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
153113 && pLoop->u.btree.pIndex!=0
153114 && pLoop->u.btree.pIndex->aSortOrder[iEq]
153120 assert( pX->op==TK_IN );
153124 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
153129 for(i=iEq;i<pLoop->nLTerm; i++){
153130 assert( pLoop->aLTerm[i]!=0 );
153131 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
153135 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
153138 Expr *pExpr = pTerm->pExpr;
153139 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
153140 sqlite3 *db = pParse->db;
153142 if( !db->mallocFailed ){
153143 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
153145 pExpr->iTable = iTab;
153149 int n = sqlite3ExprVectorSize(pX->pLeft);
153150 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
153164 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
153165 pLoop->wsFlags |= WHERE_IN_ABLE;
153166 if( pLevel->u.in.nIn==0 ){
153167 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
153169 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
153170 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
153173 i = pLevel->u.in.nIn;
153174 pLevel->u.in.nIn += nEq;
153175 pLevel->u.in.aInLoop =
153176 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
153177 pLevel->u.in.aInLoop,
153178 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
153179 pIn = pLevel->u.in.aInLoop;
153183 for(i=iEq;i<pLoop->nLTerm; i++){
153184 if( pLoop->aLTerm[i]->pExpr==pX ){
153185 int iOut = iReg + i - iEq;
153187 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
153190 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
153194 pIn->iCur = iTab;
153195 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
153197 pIn->iBase = iReg - i;
153198 pIn->nPrefix = i;
153200 pIn->nPrefix = 0;
153203 pIn->eEndLoopOp = OP_Noop;
153209 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
153210 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
153212 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
153214 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
153217 pLevel->u.in.nIn = 0;
153219 sqlite3DbFree(pParse->db, aiMap);
153230 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
153232 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
153233 || (pTerm->eOperator & WO_EQUIV)==0
153254 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
153255 ** The only thing it does is allocate the pLevel->iMem memory cell and
153294 u16 nSkip; /* Number of left-most columns to skip */
153295 Vdbe *v = pParse->pVdbe; /* The vm under construction */
153305 pLoop = pLevel->pWLoop;
153306 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
153307 nEq = pLoop->u.btree.nEq;
153308 nSkip = pLoop->nSkip;
153309 pIdx = pLoop->u.btree.pIndex;
153314 regBase = pParse->nMem + 1;
153315 nReg = pLoop->u.btree.nEq + nExtraReg;
153316 pParse->nMem += nReg;
153318 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
153319 assert( zAff!=0 || pParse->db->mallocFailed );
153322 int iIdxCur = pLevel->iIdxCur;
153323 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
153327 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
153329 assert( pLevel->addrSkip==0 );
153330 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
153337 testcase( pIdx->aiColumn[j]==XN_EXPR );
153347 pTerm = pLoop->aLTerm[j];
153351 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
153352 testcase( pTerm->wtFlags & TERM_VIRTUAL );
153364 pTerm = pLoop->aLTerm[j];
153365 if( pTerm->eOperator & WO_IN ){
153366 if( pTerm->pExpr->flags & EP_xIsSelect ){
153373 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
153374 Expr *pRight = pTerm->pExpr->pRight;
153375 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
153376 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
153379 if( pParse->nErr==0 ){
153380 assert( pParse->db->mallocFailed==0 );
153410 ** becomes a no-op.
153417 if( pTerm->wtFlags & TERM_LIKEOPT ){
153419 assert( pLevel->iLikeRepCntr>0 );
153422 assert( pOp->opcode==OP_String8
153423 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
153424 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
153425 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
153448 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
153451 struct CCurHint *pHint = pWalker->u.pCCurHint;
153452 assert( pHint->pIdx!=0 );
153453 if( pExpr->op==TK_COLUMN
153454 && pExpr->iTable==pHint->iTabCur
153455 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
153457 pWalker->eCode = 1;
153464 ** should be included in the cursor-hint for a table that is on the rhs
153465 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
153478 if( pExpr->op==TK_IS
153479 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
153480 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
153482 pWalker->eCode = 1;
153483 }else if( pExpr->op==TK_FUNCTION ){
153486 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
153487 pWalker->eCode = 1;
153514 struct CCurHint *pHint = pWalker->u.pCCurHint;
153515 if( pExpr->op==TK_COLUMN ){
153516 if( pExpr->iTable!=pHint->iTabCur ){
153517 int reg = ++pWalker->pParse->nMem; /* Register for column value */
153518 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
153519 pExpr->op = TK_REGISTER;
153520 pExpr->iTable = reg;
153521 }else if( pHint->pIdx!=0 ){
153522 pExpr->iTable = pHint->iIdxCur;
153523 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
153524 assert( pExpr->iColumn>=0 );
153526 }else if( pExpr->op==TK_AGG_FUNCTION ){
153528 ** be a correlated sub-query, and expression pExpr is an aggregate from
153546 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
153548 Parse *pParse = pWInfo->pParse;
153549 sqlite3 *db = pParse->db;
153550 Vdbe *v = pParse->pVdbe;
153552 WhereLoop *pLoop = pLevel->pWLoop;
153561 iCur = pLevel->iTabCur;
153562 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
153564 sHint.iIdxCur = pLevel->iIdxCur;
153565 sHint.pIdx = pLoop->u.btree.pIndex;
153569 pWC = &pWInfo->sWC;
153570 for(i=0; i<pWC->nBase; i++){
153571 pTerm = &pWC->a[i];
153572 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
153573 if( pTerm->prereqAll & pLevel->notReady ) continue;
153577 ** from the cursor-hint.
153597 if( pTabItem->fg.jointype & JT_LEFT ){
153598 Expr *pExpr = pTerm->pExpr;
153600 || pExpr->w.iJoin!=pTabItem->iCursor
153604 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
153608 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
153611 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
153614 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
153615 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
153616 if( j<pLoop->nLTerm ) continue;
153619 /* No subqueries or non-deterministic functions allowed */
153620 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
153627 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
153632 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
153643 # define codeCursorHint(A,B,C,D) /* No-op */
153647 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
153658 ** However, if the scan currently being coded is a branch of an OR-loop and
153675 int iCur, /* Cursor for IPK b-tree */
153678 Parse *pParse = pWInfo->pParse; /* Parse context */
153679 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
153682 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
153684 pWInfo->bDeferredSeek = 1;
153686 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
153687 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
153690 Table *pTab = pIdx->pTable;
153691 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
153693 ai[0] = pTab->nCol;
153694 for(i=0; i<pIdx->nColumn-1; i++){
153696 assert( pIdx->aiColumn[i]<pTab->nCol );
153697 x1 = pIdx->aiColumn[i];
153702 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
153721 Vdbe *v = pParse->pVdbe;
153723 assert( p->op==TK_SELECT );
153725 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
153732 pList = p->x.pList;
153733 assert( nReg<=pList->nExpr );
153735 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
153739 assert( nReg==1 || pParse->nErr );
153758 while( pTruth->op==TK_AND ){
153759 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
153760 pTruth = pTruth->pRight;
153762 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
153764 if( pTerm->wtFlags & TERM_CODED ) continue;
153765 pExpr = pTerm->pExpr;
153767 pTerm->wtFlags |= TERM_CODED;
153777 ** inner-loop Bloom filters, then evaluate those filters now, before the
153783 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
153790 int iLevel, /* Which level of pWInfo->a[] should be coded */
153794 while( ++iLevel < pWInfo->nLevel ){
153795 WhereLevel *pLevel = &pWInfo->a[iLevel];
153796 WhereLoop *pLoop = pLevel->pWLoop;
153797 if( pLevel->regFilter==0 ) continue;
153798 if( pLevel->pWLoop->nSkip ) continue;
153799 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
153800 ** vvvvv--' pLevel->regFilter if this were true. */
153801 if( NEVER(pLoop->prereq & notReady) ) continue;
153802 assert( pLevel->addrBrk==0 );
153803 pLevel->addrBrk = addrNxt;
153804 if( pLoop->wsFlags & WHERE_IPK ){
153805 WhereTerm *pTerm = pLoop->aLTerm[0];
153808 assert( pTerm->pExpr!=0 );
153809 testcase( pTerm->wtFlags & TERM_VIRTUAL );
153812 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
153813 VdbeCoverage(pParse->pVdbe);
153814 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
153816 VdbeCoverage(pParse->pVdbe);
153818 u16 nEq = pLoop->u.btree.nEq;
153822 assert( pLoop->wsFlags & WHERE_INDEXED );
153823 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
153826 sqlite3DbFree(pParse->db, zStartAff);
153827 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
153829 VdbeCoverage(pParse->pVdbe);
153831 pLevel->regFilter = 0;
153832 pLevel->addrBrk = 0;
153837 ** Generate code for the start of the iLevel-th loop in the WHERE clause
153844 int iLevel, /* Which level of pWInfo->a[] should be coded */
153865 pWC = &pWInfo->sWC;
153866 db = pParse->db;
153867 pLoop = pLevel->pWLoop;
153868 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
153869 iCur = pTabItem->iCursor;
153870 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
153871 bRev = (pWInfo->revMask>>iLevel)&1;
153872 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
153876 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
153882 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
153884 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
153899 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
153900 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
153906 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
153907 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
153909 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
153910 pLevel->iLeftJoin = ++pParse->nMem;
153911 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
153912 VdbeComment((v, "init LEFT JOIN no-match flag"));
153917 for(j=iLevel; j>0; j--){
153918 if( pWInfo->a[j].iLeftJoin ) break;
153919 if( pWInfo->a[j].pRJ ) break;
153921 addrHalt = pWInfo->a[j].addrBrk;
153923 /* Special case of a FROM clause subquery implemented as a co-routine */
153924 if( pTabItem->fg.viaCoroutine ){
153925 int regYield = pTabItem->regReturn;
153926 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
153927 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
153929 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
153930 pLevel->op = OP_Goto;
153934 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
153935 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
153940 int nConstraint = pLoop->nLTerm;
153943 addrNotFound = pLevel->addrBrk;
153946 pTerm = pLoop->aLTerm[j];
153948 if( pTerm->eOperator & WO_IN ){
153949 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
153950 int iTab = pParse->nTab++;
153951 int iCache = ++pParse->nMem;
153952 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
153956 addrNotFound = pLevel->addrNxt;
153959 Expr *pRight = pTerm->pExpr->pRight;
153961 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
153962 && pLoop->u.vtab.bOmitOffset
153964 assert( pTerm->eOperator==WO_AUX );
153965 assert( pWInfo->pSelect!=0 );
153966 assert( pWInfo->pSelect->iOffset>0 );
153967 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
153972 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
153975 pLoop->u.vtab.idxStr,
153976 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
153978 pLoop->u.vtab.needFree = 0;
153980 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
153981 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
153982 pLevel->p1 = iCur;
153983 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
153984 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153985 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
153988 pTerm = pLoop->aLTerm[j];
153989 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
153993 if( (pTerm->eOperator & WO_IN)!=0
153994 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
153995 && !db->mallocFailed
154000 int iIn; /* IN loop corresponding to the j-th constraint */
154007 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
154008 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
154009 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
154010 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
154012 testcase( pOp->opcode==OP_Rowid );
154013 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
154022 if( !db->mallocFailed ){
154023 int iFld = pTerm->u.x.iField;
154024 Expr *pLeft = pTerm->pExpr->pLeft;
154027 assert( pLeft->op==TK_VECTOR );
154029 assert( iFld<=pLeft->x.pList->nExpr );
154030 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
154032 pCompare->pLeft = pLeft;
154034 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
154036 pRight->iTable = iReg+j+2;
154038 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
154041 pCompare->pLeft = 0;
154049 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
154057 if( (pLoop->wsFlags & WHERE_IPK)!=0
154058 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
154065 assert( pLoop->u.btree.nEq==1 );
154066 pTerm = pLoop->aLTerm[0];
154068 assert( pTerm->pExpr!=0 );
154069 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154070 iReleaseReg = ++pParse->nMem;
154073 addrNxt = pLevel->addrNxt;
154074 if( pLevel->regFilter ){
154077 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
154084 pLevel->op = OP_Noop;
154085 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
154086 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
154097 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
154098 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
154124 assert( (pStart->wtFlags & TERM_VNULL)==0 );
154125 testcase( pStart->wtFlags & TERM_VIRTUAL );
154126 pX = pStart->pExpr;
154128 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
154129 if( sqlite3ExprIsVector(pX->pRight) ){
154131 codeExprOrVector(pParse, pX->pRight, r1, 1);
154132 testcase( pX->op==TK_GT );
154133 testcase( pX->op==TK_GE );
154134 testcase( pX->op==TK_LT );
154135 testcase( pX->op==TK_LE );
154136 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
154137 assert( pX->op!=TK_GT || op==OP_SeekGE );
154138 assert( pX->op!=TK_GE || op==OP_SeekGE );
154139 assert( pX->op!=TK_LT || op==OP_SeekLE );
154140 assert( pX->op!=TK_LE || op==OP_SeekLE );
154142 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
154144 op = aMoveOp[(pX->op - TK_GT)];
154148 VdbeCoverageIf(v, pX->op==TK_GT);
154149 VdbeCoverageIf(v, pX->op==TK_LE);
154150 VdbeCoverageIf(v, pX->op==TK_LT);
154151 VdbeCoverageIf(v, pX->op==TK_GE);
154160 pX = pEnd->pExpr;
154162 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
154163 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
154164 testcase( pEnd->wtFlags & TERM_VIRTUAL );
154165 memEndValue = ++pParse->nMem;
154166 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
154167 if( 0==sqlite3ExprIsVector(pX->pRight)
154168 && (pX->op==TK_LT || pX->op==TK_GT)
154174 if( 0==sqlite3ExprIsVector(pX->pRight) ){
154179 pLevel->op = bRev ? OP_Prev : OP_Next;
154180 pLevel->p1 = iCur;
154181 pLevel->p2 = start;
154182 assert( pLevel->p5==0 );
154184 iRowidReg = ++pParse->nMem;
154193 }else if( pLoop->wsFlags & WHERE_INDEXED ){
154198 ** left-most columns of the index. It may also contain
154201 ** the right-most column can be an inequality - the rest must
154241 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
154242 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
154243 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
154259 int regBignull = 0; /* big-null flag register */
154262 pIdx = pLoop->u.btree.pIndex;
154263 iIdxCur = pLevel->iIdxCur;
154264 assert( nEq>=pLoop->nSkip );
154270 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
154271 pRangeStart = pLoop->aLTerm[j++];
154272 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
154274 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
154275 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
154277 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
154278 pRangeEnd = pLoop->aLTerm[j++];
154279 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
154281 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
154283 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
154284 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
154285 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
154287 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
154291 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
154293 pLevel->iLikeRepCntr <<=1;
154294 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
154298 j = pIdx->aiColumn[nEq];
154299 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
154304 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
154307 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
154310 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
154313 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
154314 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
154318 testcase( pLoop->nSkip>0 );
154321 pLevel->regBignull = regBignull = ++pParse->nMem;
154322 if( pLevel->iLeftJoin ){
154325 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
154332 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
154338 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
154354 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
154356 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
154357 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
154358 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
154359 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
154360 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
154361 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
154367 Expr *pRight = pRangeStart->pExpr->pRight;
154370 if( (pRangeStart->wtFlags & TERM_VNULL)==0
154380 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
154397 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
154398 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
154399 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
154405 VdbeComment((v, "NULL-scan pass ctr"));
154407 if( pLevel->regFilter ){
154408 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
154416 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
154427 (pIdx->aiRowLogEst[0]+9)/10);
154452 nConstraint-startEq);
154466 assert( pLevel->p2==0 );
154468 Expr *pRight = pRangeEnd->pExpr->pRight;
154470 /* For a seek-scan that has a range on the lowest term of the index,
154473 ** over that initialization, leaving the range-end value set to the
154474 ** range-start value, resulting in a wrong answer.
154475 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
154477 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
154481 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
154491 assert( pParse->db->mallocFailed );
154494 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
154512 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
154517 /* Except, skip the end-of-range check while doing the NULL-scan */
154519 VdbeComment((v, "If NULL-scan 2nd pass"));
154531 /* During a NULL-scan, check to see if we have reached the end of
154537 VdbeComment((v, "If NULL-scan 1st pass"));
154548 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
154553 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
154554 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
154557 }else if( HasRowid(pIdx->pTable) ){
154560 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
154561 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
154562 for(j=0; j<pPk->nKeyCol; j++){
154563 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
154567 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
154570 if( pLevel->iLeftJoin==0 ){
154575 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
154578 if( pIdx->pPartIdxWhere ){
154579 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
154582 testcase( pIdx->pPartIdxWhere );
154584 ** The OR-optimization doesn't work for the right hand table of
154586 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
154590 if( pLoop->wsFlags & WHERE_ONEROW ){
154591 pLevel->op = OP_Noop;
154593 pLevel->op = OP_Prev;
154595 pLevel->op = OP_Next;
154597 pLevel->p1 = iIdxCur;
154598 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
154599 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
154600 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
154602 assert( pLevel->p5==0 );
154608 if( pLoop->wsFlags & WHERE_MULTI_OR ){
154647 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
154652 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
154653 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
154655 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
154657 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
154665 Table *pTab = pTabItem->pTab;
154667 pTerm = pLoop->aLTerm[0];
154669 assert( pTerm->eOperator & WO_OR );
154670 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
154671 pOrWc = &pTerm->u.pOrInfo->wc;
154672 pLevel->op = OP_Return;
154673 pLevel->p1 = regReturn;
154679 if( pWInfo->nLevel>1 ){
154682 nNotReady = pWInfo->nLevel - iLevel - 1;
154684 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
154686 pOrTab->nAlloc = (u8)(nNotReady + 1);
154687 pOrTab->nSrc = pOrTab->nAlloc;
154688 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
154689 origSrc = pWInfo->pTabList->a;
154691 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
154694 pOrTab = pWInfo->pTabList;
154705 ** correct response for the end-of-loop code (the OP_Return) is to
154709 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
154711 regRowset = ++pParse->nMem;
154715 regRowset = pParse->nTab++;
154716 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
154719 regRowid = ++pParse->nMem;
154729 ** the "interesting" terms of z - terms that did not originate in the
154737 ** 2022-02-04: Do not push down slices of a row-value comparison.
154739 ** the initialization of the right-hand operand of the vector comparison
154743 ** 2022-03-03: Do not push down expressions that involve subqueries.
154744 ** The subquery might get coded as a subroutine. Any table-references
154745 ** in the subquery might be resolved to index-references for the index on
154748 ** index-references will not work. tag-20220303a
154751 if( pWC->nTerm>1 ){
154753 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
154754 Expr *pExpr = pWC->a[iTerm].pExpr;
154755 if( &pWC->a[iTerm] == pTerm ) continue;
154756 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
154757 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
154758 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
154759 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
154762 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
154763 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
154771 ** prevents sqlite3PExpr() from applying the AND short-circuit
154779 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
154781 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
154782 for(ii=0; ii<pOrWc->nTerm; ii++){
154783 WhereTerm *pOrTerm = &pOrWc->a[ii];
154784 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
154785 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
154786 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
154793 if( db->mallocFailed ){
154798 pAndExpr->pLeft = pOrExpr;
154803 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
154806 assert( pSubWInfo || pParse->nErr );
154810 pParse, pOrTab, &pSubWInfo->a[0], 0
154812 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
154814 /* This is the sub-WHERE clause body. First skip over
154815 ** duplicate rows from prior sub-WHERE clauses, and record the
154817 ** row will be skipped in subsequent sub-WHERE clauses.
154819 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
154820 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
154822 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
154828 int nPk = pPk->nKeyCol;
154835 int iCol = pPk->aiColumn[iPk];
154847 ** the temp table. And if iSet is -1, assume that there is no
154870 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
154873 /* The pSubWInfo->untestedTerms flag means that this OR term
154878 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
154880 /* If all of the OR-connected terms are optimized using the same
154886 ** uses an index, and this is either the first OR-connected term
154892 pSubLoop = pSubWInfo->a[0].pWLoop;
154893 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
154894 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
154895 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
154896 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
154898 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
154899 pCov = pSubLoop->u.btree.pIndex;
154904 pWInfo->bDeferredSeek = 1;
154915 assert( pLevel->pWLoop==pLoop );
154916 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
154917 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
154918 pLevel->u.pCoveringIdx = pCov;
154919 if( pCov ) pLevel->iIdxCur = iCovCur;
154921 pAndExpr->pLeft = 0;
154925 sqlite3VdbeGoto(v, pLevel->addrBrk);
154930 ** loop. The byte-code formatter will use that P2 value as a hint to
154932 ** See tag-20220407a in vdbe.c and shell.c */
154933 assert( pLevel->op==OP_Return );
154934 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
154936 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
154948 if( pTabItem->fg.isRecursive ){
154950 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
154951 pLevel->op = OP_Noop;
154954 pLevel->op = aStep[bRev];
154955 pLevel->p1 = iCur;
154956 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
154959 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
154964 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
154976 ** sub-queries.
154984 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
154987 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154988 testcase( pTerm->wtFlags & TERM_CODED );
154989 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
154990 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
154991 testcase( pWInfo->untestedTerms==0
154992 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
154993 pWInfo->untestedTerms = 1;
154996 pE = pTerm->pExpr;
154998 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
155001 ** join processing. tag-20220513a */
155003 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
155007 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
155008 if( m & pLevel->notReady ){
155014 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
155018 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
155023 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
155032 u32 x = pLevel->iLikeRepCntr;
155043 pWC->nTerm-j, pTerm, iLoop));
155047 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
155052 pTerm->wtFlags |= TERM_CODED;
155065 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
155068 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155069 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
155070 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
155071 if( pTerm->leftCursor!=iCur ) continue;
155072 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
155073 pE = pTerm->pExpr;
155077 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
155081 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
155082 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155083 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
155086 if( pAlt->wtFlags & (TERM_CODED) ) continue;
155087 if( (pAlt->eOperator & WO_IN)
155088 && ExprUseXSelect(pAlt->pExpr)
155089 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
155093 testcase( pAlt->eOperator & WO_EQ );
155094 testcase( pAlt->eOperator & WO_IS );
155095 testcase( pAlt->eOperator & WO_IN );
155097 sEAlt = *pAlt->pExpr;
155098 sEAlt.pLeft = pE->pLeft;
155100 pAlt->wtFlags |= TERM_CODED;
155106 if( pLevel->pRJ ){
155111 WhereRightJoin *pRJ = pLevel->pRJ;
155113 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
155118 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
155121 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
155126 nPk = pPk->nKeyCol;
155129 int iCol = pPk->aiColumn[iPk];
155133 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
155135 VdbeComment((v, "match against %s", pTab->zName));
155137 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
155138 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
155147 if( pLevel->iLeftJoin ){
155148 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
155149 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
155151 if( pLevel->pRJ==0 ){
155156 if( pLevel->pRJ ){
155159 ** be in-line with the rest of the code. But at the end, a separate
155163 WhereRightJoin *pRJ = pLevel->pRJ;
155164 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
155165 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
155166 assert( pParse->withinRJSubrtn < 255 );
155167 pParse->withinRJSubrtn++;
155172 ** appropriate WHERE clause constraint checks. tag-20220513a.
155175 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
155176 testcase( pTerm->wtFlags & TERM_VIRTUAL );
155177 testcase( pTerm->wtFlags & TERM_CODED );
155178 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
155179 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
155180 assert( pWInfo->untestedTerms );
155183 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
155184 assert( pTerm->pExpr );
155185 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
155186 pTerm->wtFlags |= TERM_CODED;
155192 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
155198 iLevel, (u64)pLevel->notReady);
155201 return pLevel->notReady;
155205 ** Generate the code for the loop that finds all non-matched terms
155213 Parse *pParse = pWInfo->pParse;
155214 Vdbe *v = pParse->pVdbe;
155215 WhereRightJoin *pRJ = pLevel->pRJ;
155217 WhereClause *pWC = &pWInfo->sWC;
155219 WhereLoop *pLoop = pLevel->pWLoop;
155220 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155225 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
155226 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
155227 pRJ->regReturn);
155230 mAll |= pWInfo->a[k].pWLoop->maskSelf;
155231 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
155232 iIdxCur = pWInfo->a[k].iIdxCur;
155237 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
155238 mAll |= pLoop->maskSelf;
155239 for(k=0; k<pWC->nTerm; k++){
155240 WhereTerm *pTerm = &pWC->a[k];
155241 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
155242 && pTerm->eOperator!=WO_ROWVAL
155246 if( pTerm->prereqAll & ~mAll ) continue;
155247 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
155249 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
155256 assert( pParse->withinRJSubrtn < 100 );
155257 pParse->withinRJSubrtn++;
155261 int iCur = pLevel->iTabCur;
155262 int r = ++pParse->nMem;
155266 Table *pTab = pTabItem->pTab;
155268 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
155273 nPk = pPk->nKeyCol;
155274 pParse->nMem += nPk - 1;
155276 int iCol = pPk->aiColumn[iPk];
155280 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
155282 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
155285 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
155288 sqlite3ExprDelete(pParse->db, pSubWhere);
155290 assert( pParse->withinRJSubrtn>0 );
155291 pParse->withinRJSubrtn--;
155297 ** 2015-06-08
155324 sqlite3WhereClauseClear(&p->wc);
155332 sqlite3WhereClauseClear(&p->wc);
155339 ** The index in pWC->a[] of the new WhereTerm is returned on success.
155342 ** the db->mallocFailed flag so that higher-level functions can detect it.
155344 ** This routine will increase the size of the pWC->a[] array as necessary.
155353 ** the pWC->a[] array.
155359 if( pWC->nTerm>=pWC->nSlot ){
155360 WhereTerm *pOld = pWC->a;
155361 sqlite3 *db = pWC->pWInfo->pParse->db;
155362 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
155363 if( pWC->a==0 ){
155367 pWC->a = pOld;
155370 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
155371 pWC->nSlot = pWC->nSlot*2;
155373 pTerm = &pWC->a[idx = pWC->nTerm++];
155374 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
155376 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
155378 pTerm->truthProb = 1;
155380 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
155381 pTerm->wtFlags = wtFlags;
155382 pTerm->pWC = pWC;
155383 pTerm->iParent = -1;
155384 memset(&pTerm->eOperator, 0,
155385 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
155407 if( pExpr->pLeft->op==TK_VECTOR
155408 || pExpr->pRight->op==TK_VECTOR
155409 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
155410 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
155412 pExpr->flags ^= EP_Commuted;
155414 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
155415 if( pExpr->op>=TK_GT ){
155420 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
155421 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
155439 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
155440 c = (u16)(WO_EQ<<(op-TK_EQ));
155478 int cnt; /* Number of non-wildcard prefix characters */
155480 sqlite3 *db = pParse->db; /* Database connection */
155492 pList = pExpr->x.pList;
155493 pLeft = pList->a[1].pExpr;
155495 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
155496 op = pRight->op;
155497 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
155498 Vdbe *pReprepare = pParse->pReprepare;
155499 int iCol = pRight->iColumn;
155504 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
155505 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
155508 z = (u8*)pRight->u.zToken;
155520 ** with a wildcard and if (2) the non-wildcard prefix does not end with
155527 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
155539 zNew = pPrefix->u.zToken;
155555 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
155556 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
155557 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
155558 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
155559 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
155561 if( pLeft->op!=TK_COLUMN
155564 && ALWAYS(pLeft->y.pTab)
155565 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
155571 if( iTo==1 && zNew[0]=='-' ){
155574 zNew[iTo-1]++;
155576 zNew[iTo-1]--;
155591 Vdbe *v = pParse->pVdbe;
155592 sqlite3VdbeSetVarmask(v, pRight->iColumn);
155594 if( *pisComplete && pRight->u.zToken[1] ){
155603 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
155625 ** ----------------------- ---------------------------------
155653 if( pExpr->op==TK_FUNCTION ){
155668 pList = pExpr->x.pList;
155669 if( pList==0 || pList->nExpr!=2 ){
155673 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
155675 ** the left-hand side operand in their in-fix form.
155680 pCol = pList->a[1].pExpr;
155681 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
155685 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
155687 *ppRight = pList->a[0].pExpr;
155700 ** Historically, xFindFunction expected to see lower-case function
155704 pCol = pList->a[0].pExpr;
155705 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
155706 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
155712 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
155714 assert( pVtab->pModule!=0 );
155716 pMod = (sqlite3_module *)pVtab->pModule;
155717 if( pMod->xFindFunction!=0 ){
155718 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
155721 *ppRight = pList->a[1].pExpr;
155727 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
155729 Expr *pLeft = pExpr->pLeft;
155730 Expr *pRight = pExpr->pRight;
155731 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
155735 assert( pRight==0 || pRight->op!=TK_COLUMN
155736 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
155743 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
155744 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
155745 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
155758 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
155759 pDerived->w.iJoin = pBase->w.iJoin;
155767 pWC->a[iChild].iParent = iParent;
155768 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
155769 pWC->a[iParent].nChild++;
155773 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
155778 if( pTerm->eOperator!=WO_AND ){
155781 if( N<pTerm->u.pAndInfo->wc.nTerm ){
155782 return &pTerm->u.pAndInfo->wc.a[N];
155789 ** two subterms are in disjunction - they are OR-ed together.
155799 ** x<y OR x=y --> x<=y
155800 ** x=y OR x=y --> x=y
155801 ** x<=y OR x<y --> x<=y
155805 ** x<y OR x>y --> x!=y
155813 u16 eOp = pOne->eOperator | pTwo->eOperator;
155819 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
155820 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
155821 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
155824 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
155825 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
155826 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
155827 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
155829 if( (eOp & (eOp-1))!=0 ){
155837 db = pWC->pWInfo->pParse->db;
155838 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
155840 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
155841 pNew->op = op;
155848 ** Analyze a term that consists of two or more OR-connected
155861 ** The term being analyzed must have two or more of OR-connected subterms.
155862 ** A single subterm might be a set of AND-connected sub-subterms.
155900 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
155937 int idxTerm /* Index of the OR-term to be analyzed */
155939 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
155940 Parse *pParse = pWInfo->pParse; /* Parser context */
155941 sqlite3 *db = pParse->db; /* Database connection */
155942 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
155943 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
155946 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
155956 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
155957 assert( pExpr->op==TK_OR );
155958 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
155960 pTerm->wtFlags |= TERM_ORINFO;
155961 pOrWc = &pOrInfo->wc;
155962 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
155966 if( db->mallocFailed ) return;
155967 assert( pOrWc->nTerm>=2 );
155974 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
155975 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
155977 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
155985 pOrTerm->u.pAndInfo = pAndInfo;
155986 pOrTerm->wtFlags |= TERM_ANDINFO;
155987 pOrTerm->eOperator = WO_AND;
155988 pOrTerm->leftCursor = -1;
155989 pAndWC = &pAndInfo->wc;
155990 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
155991 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
155992 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
155994 pAndWC->pOuter = pWC;
155995 if( !db->mallocFailed ){
155996 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
155997 assert( pAndTerm->pExpr );
155998 if( allowedOp(pAndTerm->pExpr->op)
155999 || pAndTerm->eOperator==WO_AUX
156001 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
156007 }else if( pOrTerm->wtFlags & TERM_COPIED ){
156012 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
156013 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
156014 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
156015 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
156018 if( (pOrTerm->eOperator & WO_EQ)==0 ){
156030 pOrInfo->indexable = indexable;
156031 pTerm->eOperator = WO_OR;
156032 pTerm->leftCursor = -1;
156034 pWC->hasOr = 1;
156037 /* For a two-way OR, attempt to implementation case 2.
156039 if( indexable && pOrWc->nTerm==2 ){
156042 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
156045 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
156056 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
156059 ** something other than == on a column in the single table. The 1-bit
156063 ** sure the same column is used on all terms. The 2-bit case is when
156074 int iColumn = -1; /* Column index on lhs of IN operator */
156075 int iCursor = -1; /* Table cursor common to all terms */
156086 pOrTerm = pOrWc->a;
156087 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
156088 assert( pOrTerm->eOperator & WO_EQ );
156089 pOrTerm->wtFlags &= ~TERM_OK;
156090 if( pOrTerm->leftCursor==iCursor ){
156091 /* This is the 2-bit case and we are on the second iteration and
156096 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
156097 pOrTerm->leftCursor))==0 ){
156102 testcase( pOrTerm->wtFlags & TERM_COPIED );
156103 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
156104 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
156107 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156108 iColumn = pOrTerm->u.x.leftColumn;
156109 iCursor = pOrTerm->leftCursor;
156110 pLeft = pOrTerm->pExpr->pLeft;
156118 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
156126 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
156127 assert( pOrTerm->eOperator & WO_EQ );
156128 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156129 if( pOrTerm->leftCursor!=iCursor ){
156130 pOrTerm->wtFlags &= ~TERM_OK;
156131 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
156132 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
156137 /* If the right-hand side is also a column, then the affinities
156141 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
156142 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
156146 pOrTerm->wtFlags |= TERM_OK;
156162 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
156163 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
156164 assert( pOrTerm->eOperator & WO_EQ );
156165 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
156166 assert( pOrTerm->leftCursor==iCursor );
156167 assert( pOrTerm->u.x.leftColumn==iColumn );
156168 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
156169 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
156170 pLeft = pOrTerm->pExpr->pLeft;
156179 pNew->x.pList = pList;
156183 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
156211 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
156212 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
156214 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
156215 aff2 = sqlite3ExprAffinity(pExpr->pRight);
156223 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
156234 SrcList *pSrc = pS->pSrc;
156235 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
156236 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
156237 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
156238 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
156239 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
156242 for(i=0; i<pSrc->nSrc; i++){
156243 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
156244 if( pSrc->a[i].fg.isUsing==0 ){
156245 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
156247 if( pSrc->a[i].fg.isTabFunc ){
156248 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
156252 pS = pS->pPrior;
156263 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
156280 iCur = pFrom->a[i].iCursor;
156281 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
156282 if( pIdx->aColExpr==0 ) continue;
156283 for(i=0; i<pIdx->nKeyCol; i++){
156284 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
156285 assert( pIdx->bHasExpr );
156286 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
156308 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
156310 pExpr = pExpr->x.pList->a[0].pExpr;
156314 if( pExpr->op==TK_COLUMN ){
156315 aiCurCol[0] = pExpr->iTable;
156316 aiCurCol[1] = pExpr->iColumn;
156320 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
156348 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
156352 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
156358 int op; /* Top-level operator. pExpr->op */
156359 Parse *pParse = pWInfo->pParse; /* Parsing context */
156360 sqlite3 *db = pParse->db; /* Database connection */
156364 if( db->mallocFailed ){
156367 assert( pWC->nTerm > idxTerm );
156368 pTerm = &pWC->a[idxTerm];
156369 pMaskSet = &pWInfo->sMaskSet;
156370 pExpr = pTerm->pExpr;
156372 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
156373 pMaskSet->bVarSelect = 0;
156374 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
156375 op = pExpr->op;
156377 assert( pExpr->pRight==0 );
156380 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
156382 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
156384 prereqAll = prereqLeft | pTerm->prereqRight;
156386 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
156387 if( pExpr->pLeft==0
156389 || pExpr->x.pList!=0
156393 prereqAll = prereqLeft | pTerm->prereqRight;
156396 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
156407 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
156410 extraRight = x-1; /* ON clause terms may not be used with an index
156423 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
156430 pTerm->prereqAll = prereqAll;
156431 pTerm->leftCursor = -1;
156432 pTerm->iParent = -1;
156433 pTerm->eOperator = 0;
156436 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
156437 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
156438 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
156440 if( pTerm->u.x.iField>0 ){
156442 assert( pLeft->op==TK_VECTOR );
156444 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
156448 pTerm->leftCursor = aiCurCol[0];
156449 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
156450 pTerm->u.x.leftColumn = aiCurCol[1];
156451 pTerm->eOperator = operatorMask(op) & opMask;
156453 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
156455 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
156460 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
156461 assert( pTerm->u.x.iField==0 );
156462 if( pTerm->leftCursor>=0 ){
156465 if( db->mallocFailed ){
156471 pNew = &pWC->a[idxNew];
156473 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
156474 pTerm = &pWC->a[idxTerm];
156475 pTerm->wtFlags |= TERM_COPIED;
156478 pTerm->eOperator |= WO_EQUIV;
156485 pNew->wtFlags |= exprCommute(pParse, pDup);
156486 pNew->leftCursor = aiCurCol[0];
156487 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
156488 pNew->u.x.leftColumn = aiCurCol[1];
156490 pNew->prereqRight = prereqLeft | extraRight;
156491 pNew->prereqAll = prereqAll;
156492 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
156499 pExpr->op = TK_TRUEFALSE;
156500 pExpr->u.zToken = "false";
156502 pTerm->prereqAll = 0;
156503 pTerm->eOperator = 0;
156523 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
156528 pList = pExpr->x.pList;
156530 assert( pList->nExpr==2 );
156535 sqlite3ExprDup(db, pExpr->pLeft, 0),
156536 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
156541 pTerm = &pWC->a[idxTerm];
156551 else if( pExpr->op==TK_OR ){
156552 assert( pWC->op==TK_AND );
156554 pTerm = &pWC->a[idxTerm];
156563 else if( pExpr->op==TK_NOTNULL ){
156564 if( pExpr->pLeft->op==TK_COLUMN
156565 && pExpr->pLeft->iColumn>=0
156569 Expr *pLeft = pExpr->pLeft;
156580 pNewTerm = &pWC->a[idxNew];
156581 pNewTerm->prereqRight = 0;
156582 pNewTerm->leftCursor = pLeft->iTable;
156583 pNewTerm->u.x.leftColumn = pLeft->iColumn;
156584 pNewTerm->eOperator = WO_GT;
156586 pTerm = &pWC->a[idxTerm];
156587 pTerm->wtFlags |= TERM_COPIED;
156588 pNewTerm->prereqAll = pTerm->prereqAll;
156604 ** for LIKE) then the lower-bound is made all uppercase and the upper-
156608 else if( pExpr->op==TK_FUNCTION
156609 && pWC->op==TK_AND
156613 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
156622 pLeft = pExpr->x.pList->a[1].pExpr;
156628 /* Convert the lower bound to upper-case and the upper bound to
156629 ** lower-case (upper-case is less than lower-case in ASCII) so that
156632 if( noCase && !pParse->db->mallocFailed ){
156635 pTerm->wtFlags |= TERM_LIKE;
156636 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
156637 pStr1->u.zToken[i] = sqlite3Toupper(c);
156638 pStr2->u.zToken[i] = sqlite3Tolower(c);
156642 if( !db->mallocFailed ){
156644 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
156653 if( c=='A'-1 ) isComplete = 0;
156675 pTerm = &pWC->a[idxTerm];
156683 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
156684 ** new terms for each component comparison - "a = ?" and "b = ?". The
156689 ** is not a sub-select.
156691 ** tag-20220128a
156693 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
156694 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
156695 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
156696 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
156697 || (pExpr->pRight->flags & EP_xIsSelect)==0)
156698 && pWC->op==TK_AND
156704 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
156705 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
156707 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
156712 pTerm = &pWC->a[idxTerm];
156713 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
156714 pTerm->eOperator = WO_ROWVAL;
156717 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
156726 else if( pExpr->op==TK_IN
156727 && pTerm->u.x.iField==0
156728 && pExpr->pLeft->op==TK_VECTOR
156730 && pExpr->x.pSelect->pPrior==0
156732 && pExpr->x.pSelect->pWin==0
156734 && pWC->op==TK_AND
156737 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
156740 pWC->a[idxNew].u.x.iField = i+1;
156756 else if( pWC->op==TK_AND ){
156759 while( res-- > 0 ){
156772 pNewExpr->w.iJoin = pExpr->w.iJoin;
156776 pNewTerm = &pWC->a[idxNew];
156777 pNewTerm->prereqRight = prereqExpr;
156778 pNewTerm->leftCursor = pLeft->iTable;
156779 pNewTerm->u.x.leftColumn = pLeft->iColumn;
156780 pNewTerm->eOperator = WO_AUX;
156781 pNewTerm->eMatchOp = eOp2;
156783 pTerm = &pWC->a[idxTerm];
156784 pTerm->wtFlags |= TERM_COPIED;
156785 pNewTerm->prereqAll = pTerm->prereqAll;
156795 testcase( pTerm!=&pWC->a[idxTerm] );
156796 pTerm = &pWC->a[idxTerm];
156797 pTerm->prereqRight |= extraRight;
156824 pWC->op = op;
156827 if( pE2->op!=op ){
156830 sqlite3WhereSplit(pWC, pE2->pLeft, op);
156831 sqlite3WhereSplit(pWC, pE2->pRight, op);
156838 ** where-clause passed as the first argument. The value for the term
156853 Parse *pParse = pWC->pWInfo->pParse;
156854 sqlite3 *db = pParse->db;
156862 pVal->u.iValue = iVal;
156867 pVal->iTable = iReg;
156874 pTerm = &pWC->a[idx];
156875 pTerm->leftCursor = iCsr;
156876 pTerm->eOperator = WO_AUX;
156877 pTerm->eMatchOp = eMatchOp;
156900 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
156901 if( p->pGroupBy==0
156902 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
156903 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
156905 ExprList *pOrderBy = p->pOrderBy;
156906 int iCsr = p->pSrc->a[0].iCursor;
156910 for(ii=0; ii<pWC->nTerm; ii++){
156911 if( pWC->a[ii].wtFlags & TERM_CODED ){
156913 ** other, subsequent terms. It can be ignored. See tag-20220128a */
156914 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
156915 assert( pWC->a[ii].eOperator==WO_ROWVAL );
156918 if( pWC->a[ii].leftCursor!=iCsr ) return;
156923 for(ii=0; ii<pOrderBy->nExpr; ii++){
156924 Expr *pExpr = pOrderBy->a[ii].pExpr;
156925 if( pExpr->op!=TK_COLUMN ) return;
156926 if( pExpr->iTable!=iCsr ) return;
156927 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
156931 /* All conditions are met. Add the terms to the where-clause object. */
156932 assert( p->pLimit->op==TK_LIMIT );
156933 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
156935 if( p->iOffset>0 ){
156936 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
156949 pWC->pWInfo = pWInfo;
156950 pWC->hasOr = 0;
156951 pWC->pOuter = 0;
156952 pWC->nTerm = 0;
156953 pWC->nBase = 0;
156954 pWC->nSlot = ArraySize(pWC->aStatic);
156955 pWC->a = pWC->aStatic;
156964 sqlite3 *db = pWC->pWInfo->pParse->db;
156965 assert( pWC->nTerm>=pWC->nBase );
156966 if( pWC->nTerm>0 ){
156967 WhereTerm *a = pWC->a;
156968 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
156971 /* Verify that every term past pWC->nBase is virtual */
156972 for(i=pWC->nBase; i<pWC->nTerm; i++){
156973 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
156977 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
156978 if( a->wtFlags & TERM_DYNAMIC ){
156979 sqlite3ExprDelete(db, a->pExpr);
156981 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
156982 if( a->wtFlags & TERM_ORINFO ){
156983 assert( (a->wtFlags & TERM_ANDINFO)==0 );
156984 whereOrInfoDelete(db, a->u.pOrInfo);
156986 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
156987 whereAndInfoDelete(db, a->u.pAndInfo);
157002 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
157007 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
157012 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
157018 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
157025 ** "no-inline" function to avoid the stack push overhead in the
157033 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
157034 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
157035 if( p->pRight ){
157036 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
157037 assert( p->x.pList==0 );
157039 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
157040 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
157041 }else if( p->x.pList ){
157042 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
157045 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
157046 assert( p->y.pWin!=0 );
157047 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
157048 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
157049 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
157055 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
157056 return sqlite3WhereGetMask(pMaskSet, p->iTable);
157058 assert( p->op!=TK_IF_NULL_ROW );
157070 for(i=0; i<pList->nExpr; i++){
157071 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
157091 for(i=pWC->nTerm-1; i>=0; i--){
157097 ** For table-valued-functions, transform the function arguments into
157113 if( pItem->fg.isTabFunc==0 ) return;
157114 pTab = pItem->pTab;
157116 pArgs = pItem->u1.pFuncArg;
157118 for(j=k=0; j<pArgs->nExpr; j++){
157121 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
157122 if( k>=pTab->nCol ){
157123 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
157124 pTab->zName, j);
157127 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
157129 pColRef->iTable = pItem->iCursor;
157130 pColRef->iColumn = k++;
157132 pColRef->y.pTab = pTab;
157133 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
157135 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
157137 if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ) ){
157142 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
157198 return pWInfo->nRowOut;
157206 return pWInfo->eDistinct;
157218 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
157222 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
157224 ** inner-most loop did not fit within the sorter, then we can skip all
157227 ** loop - the loop immediately outside the inner-most.
157234 ** be the continuation for the second-inner-most loop. If the ORDER BY
157236 ** be the continuation for the inner-most loop.
157239 ** inner-most loop, in the sense that a correct answer will result.
157246 if( !pWInfo->bOrderedInnerLoop ){
157248 ** continuation of the inner-most loop. */
157249 return pWInfo->iContinue;
157251 pInner = &pWInfo->a[pWInfo->nLevel-1];
157252 assert( pInner->addrNxt!=0 );
157253 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
157258 ** the aggregate-step call to min() or max(), check to see if any
157270 if( !pWInfo->bOrderedInnerLoop ) return;
157271 if( pWInfo->nOBSat==0 ) return;
157272 for(i=pWInfo->nLevel-1; i>=0; i--){
157273 pInner = &pWInfo->a[i];
157274 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
157275 sqlite3VdbeGoto(v, pInner->addrNxt);
157279 sqlite3VdbeGoto(v, pWInfo->iBreak);
157287 assert( pWInfo->iContinue!=0 );
157288 return pWInfo->iContinue;
157296 return pWInfo->iBreak;
157303 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
157310 ** Either value may be -1, indicating that cursor is not used.
157313 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
157317 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
157319 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
157321 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
157325 return pWInfo->eOnePass;
157333 return pWInfo->bDeferredSeek;
157340 pDest->n = pSrc->n;
157341 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
157354 LogEst rRun, /* Run-cost of the new entry */
157359 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
157360 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
157363 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
157367 if( pSet->n<N_OR_COST ){
157368 p = &pSet->a[pSet->n++];
157369 p->nOut = nOut;
157371 p = pSet->a;
157372 for(i=1; i<pSet->n; i++){
157373 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
157375 if( p->rRun<=rRun ) return 0;
157378 p->prereq = prereq;
157379 p->rRun = rRun;
157380 if( p->nOut>nOut ) p->nOut = nOut;
157390 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
157391 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
157392 assert( iCursor>=-1 );
157393 if( pMaskSet->ix[0]==iCursor ){
157396 for(i=1; i<pMaskSet->n; i++){
157397 if( pMaskSet->ix[i]==iCursor ){
157408 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
157410 pBlock->pNext = pWInfo->pMemToFree;
157411 pBlock->sz = nByte;
157412 pWInfo->pMemToFree = pBlock;
157421 pOldBlk--;
157422 assert( pOldBlk->sz<nByte );
157423 memcpy(pNew, pOld, pOldBlk->sz);
157433 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
157437 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
157438 pMaskSet->ix[pMaskSet->n++] = iCursor;
157442 ** If the right-hand branch of the expression is a TK_COLUMN, then return
157443 ** a pointer to the right-hand branch. Otherwise, return NULL.
157446 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
157447 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
157460 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
157462 WhereClause *pWC; /* Shorthand for pScan->pWC */
157464 int k = pScan->k; /* Where to start scanning */
157466 assert( pScan->iEquiv<=pScan->nEquiv );
157467 pWC = pScan->pWC;
157469 iColumn = pScan->aiColumn[pScan->iEquiv-1];
157470 iCur = pScan->aiCur[pScan->iEquiv-1];
157474 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
157475 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
157476 if( pTerm->leftCursor==iCur
157477 && pTerm->u.x.leftColumn==iColumn
157479 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
157480 pScan->pIdxExpr,iCur)==0)
157481 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
157483 if( (pTerm->eOperator & WO_EQUIV)!=0
157484 && pScan->nEquiv<ArraySize(pScan->aiCur)
157485 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
157488 for(j=0; j<pScan->nEquiv; j++){
157489 if( pScan->aiCur[j]==pX->iTable
157490 && pScan->aiColumn[j]==pX->iColumn ){
157494 if( j==pScan->nEquiv ){
157495 pScan->aiCur[j] = pX->iTable;
157496 pScan->aiColumn[j] = pX->iColumn;
157497 pScan->nEquiv++;
157500 if( (pTerm->eOperator & pScan->opMask)!=0 ){
157502 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
157504 Parse *pParse = pWC->pWInfo->pParse;
157505 pX = pTerm->pExpr;
157506 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
157509 assert(pX->pLeft);
157511 if( pColl==0 ) pColl = pParse->db->pDfltColl;
157512 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
157516 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
157517 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
157518 && pX->op==TK_COLUMN
157519 && pX->iTable==pScan->aiCur[0]
157520 && pX->iColumn==pScan->aiColumn[0]
157522 testcase( pTerm->eOperator & WO_IS );
157525 pScan->pWC = pWC;
157526 pScan->k = k+1;
157530 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
157531 pTerm, pScan->nEquiv);
157532 for(ii=0; ii<pScan->nEquiv; ii++){
157534 pScan->aiCur[ii], pScan->aiColumn[ii]);
157543 pWC = pWC->pOuter;
157546 if( pScan->iEquiv>=pScan->nEquiv ) break;
157547 pWC = pScan->pOrigWC;
157549 pScan->iEquiv++;
157556 ** It is factored out into a separate tail-recursion subroutine so that
157557 ** the normal whereScanInit() routine, which is a high-runner, does not
157561 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
157592 pScan->pOrigWC = pWC;
157593 pScan->pWC = pWC;
157594 pScan->pIdxExpr = 0;
157595 pScan->idxaff = 0;
157596 pScan->zCollName = 0;
157597 pScan->opMask = opMask;
157598 pScan->k = 0;
157599 pScan->aiCur[0] = iCur;
157600 pScan->nEquiv = 1;
157601 pScan->iEquiv = 1;
157604 iColumn = pIdx->aiColumn[j];
157605 if( iColumn==pIdx->pTable->iPKey ){
157608 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
157609 pScan->zCollName = pIdx->azColl[j];
157611 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
157612 pScan->zCollName = pIdx->azColl[j];
157613 pScan->aiColumn[0] = XN_EXPR;
157619 pScan->aiColumn[0] = iColumn;
157630 ** Search for terms matching the iColumn-th column of pIdx
157631 ** rather than the iColumn-th column of table iCur.
157635 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
157642 ** then try for the one with no dependencies on <expr> - in other words where
157645 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
157663 if( (p->prereqRight & notReady)==0 ){
157664 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
157665 testcase( p->eOperator & WO_IS );
157676 ** This function searches pList for an entry that matches the iCol-th column
157679 ** If such an expression is found, its index in pList->a[] is returned. If
157680 ** no expression is found, -1 is returned.
157690 const char *zColl = pIdx->azColl[iCol];
157692 for(i=0; i<pList->nExpr; i++){
157693 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
157695 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
157696 && p->iColumn==pIdx->aiColumn[iCol]
157697 && p->iTable==iBase
157699 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
157700 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
157706 return -1;
157710 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
157715 assert( iCol>=0 && iCol<pIdx->nColumn );
157716 j = pIdx->aiColumn[iCol];
157718 return pIdx->pTable->aCol[j].notNull;
157719 }else if( j==(-1) ){
157722 assert( j==(-2) );
157729 ** Return true if the DISTINCT expression-list passed as the third argument
157733 ** DISTINCT list are collectively unique and individually non-null.
157746 /* If there is more than one table or sub-select in the FROM clause of
157749 if( pTabList->nSrc!=1 ) return 0;
157750 iBase = pTabList->a[0].iCursor;
157751 pTab = pTabList->a[0].pTab;
157754 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
157755 ** current SELECT is a correlated sub-query.
157757 for(i=0; i<pDistinct->nExpr; i++){
157758 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
157760 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
157761 if( p->iTable==iBase && p->iColumn<0 ) return 1;
157772 ** comparison and select-list expressions must match those of the index.
157777 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
157779 if( pIdx->pPartIdxWhere ) continue;
157780 for(i=0; i<pIdx->nKeyCol; i++){
157786 if( i==pIdx->nKeyCol ){
157800 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
157807 ** opcodes into OP_Copy when the table is being accessed via co-routine
157820 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
157822 Vdbe *v = pParse->pVdbe;
157825 if( pParse->db->mallocFailed ) return;
157827 if( pOp->p1!=iTabCur ) continue;
157828 if( pOp->opcode==OP_Column ){
157829 pOp->opcode = OP_Copy;
157830 pOp->p1 = pOp->p2 + iRegister;
157831 pOp->p2 = pOp->p3;
157832 pOp->p3 = 0;
157833 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
157834 }else if( pOp->opcode==OP_Rowid ){
157835 pOp->opcode = OP_Sequence;
157836 pOp->p1 = iAutoidxCur;
157839 pOp->opcode = OP_Null;
157840 pOp->p3 = 0;
157851 ** are no-ops.
157857 for(i=0; i<p->nConstraint; i++){
157861 p->aConstraint[i].iColumn,
157862 p->aConstraint[i].iTermOffset,
157863 p->aConstraint[i].op,
157864 p->aConstraint[i].usable,
157867 for(i=0; i<p->nOrderBy; i++){
157870 p->aOrderBy[i].iColumn,
157871 p->aOrderBy[i].desc);
157877 for(i=0; i<p->nConstraint; i++){
157880 p->aConstraintUsage[i].argvIndex,
157881 p->aConstraintUsage[i].omit);
157883 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
157884 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
157885 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
157886 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
157887 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
157905 ** not-NULL condition on the left table of the RIGHT JOIN.
157911 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
157912 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
157913 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
157914 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
157915 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
157916 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
157917 || pTerm->pExpr->w.iJoin != pSrc->iCursor
157921 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
157922 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
157943 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
157944 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
157945 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
157946 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
157951 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
157952 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157953 if( pTerm->u.x.leftColumn<0 ) return 0;
157954 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
157955 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
157956 testcase( pTerm->pExpr->op==TK_IS );
157977 WhereTerm *pWCEnd; /* End of pWC->a[] */
157986 int mxBitCol; /* Maximum column in pSrc->colUsed */
158001 v = pParse->pVdbe;
158008 pTable = pSrc->pTab;
158009 pWCEnd = &pWC->a[pWC->nTerm];
158010 pLoop = pLevel->pWLoop;
158012 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
158013 Expr *pExpr = pTerm->pExpr;
158017 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
158021 sqlite3ExprDup(pParse->db, pExpr, 0));
158026 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158027 iCol = pTerm->u.x.leftColumn;
158028 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
158030 testcase( iCol==BMS-1 );
158033 "automatic index on %s(%s)", pTable->zName,
158034 pTable->aCol[iCol].zCnName);
158038 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
158041 pLoop->aLTerm[nKeyCol++] = pTerm;
158046 assert( nKeyCol>0 || pParse->db->mallocFailed );
158047 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
158048 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
158059 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
158060 mxBitCol = MIN(BMS-1,pTable->nCol);
158061 testcase( pTable->nCol==BMS-1 );
158062 testcase( pTable->nCol==BMS-2 );
158066 if( pSrc->colUsed & MASKBIT(BMS-1) ){
158067 nKeyCol += pTable->nCol - BMS + 1;
158071 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
158073 pLoop->u.btree.pIndex = pIdx;
158074 pIdx->zName = "auto-index";
158075 pIdx->pTable = pTable;
158078 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
158082 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158083 iCol = pTerm->u.x.leftColumn;
158084 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
158085 testcase( iCol==BMS-1 );
158088 Expr *pX = pTerm->pExpr;
158090 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
158092 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
158093 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
158098 assert( (u32)n==pLoop->u.btree.nEq );
158104 pIdx->aiColumn[n] = i;
158105 pIdx->azColl[n] = sqlite3StrBINARY;
158109 if( pSrc->colUsed & MASKBIT(BMS-1) ){
158110 for(i=BMS-1; i<pTable->nCol; i++){
158111 pIdx->aiColumn[n] = i;
158112 pIdx->azColl[n] = sqlite3StrBINARY;
158117 pIdx->aiColumn[n] = XN_ROWID;
158118 pIdx->azColl[n] = sqlite3StrBINARY;
158121 assert( pLevel->iIdxCur>=0 );
158122 pLevel->iIdxCur = pParse->nTab++;
158123 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
158125 VdbeComment((v, "for %s", pTable->zName));
158126 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){
158127 pLevel->regFilter = ++pParse->nMem;
158128 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
158132 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
158133 if( pTabItem->fg.viaCoroutine ){
158134 int regYield = pTabItem->regReturn;
158136 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
158139 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
158141 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
158146 pLoop->wsFlags |= WHERE_PARTIALIDX;
158150 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
158152 if( pLevel->regFilter ){
158153 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
158154 regBase, pLoop->u.btree.nEq);
158156 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
158159 if( pTabItem->fg.viaCoroutine ){
158161 testcase( pParse->db->mallocFailed );
158162 assert( pLevel->iIdxCur>0 );
158163 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
158164 pTabItem->regResult, pLevel->iIdxCur);
158166 pTabItem->fg.viaCoroutine = 0;
158168 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
158178 sqlite3ExprDelete(pParse->db, pPartial);
158195 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
158204 int iLevel, /* Index in pWInfo->a[] that is pLevel */
158213 Parse *pParse = pWInfo->pParse; /* Parsing context */
158214 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
158215 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
158220 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
158229 iCur = pLevel->iTabCur;
158230 pLevel->regFilter = ++pParse->nMem;
158233 ** to zero-filled blob of at least 80K bits, but maybe more if the
158235 ** measure the size of the table at run-time using OP_Count with
158240 pItem = &pWInfo->pTabList->a[pLevel->iFrom];
158242 pTab = pItem->pTab;
158244 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
158250 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
158253 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
158254 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
158255 Expr *pExpr = pTerm->pExpr;
158256 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
158259 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
158262 if( pLoop->wsFlags & WHERE_IPK ){
158265 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
158268 Index *pIdx = pLoop->u.btree.pIndex;
158269 int n = pLoop->u.btree.nEq;
158273 int iCol = pIdx->aiColumn[jj];
158274 assert( pIdx->pTable==pItem->pTab );
158275 sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
158277 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
158281 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
158284 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
158285 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
158286 while( ++iLevel < pWInfo->nLevel ){
158288 pLevel = &pWInfo->a[iLevel];
158289 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
158290 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
158291 pLoop = pLevel->pWLoop;
158293 if( pLoop->prereq & notReady ) continue;
158294 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
158297 /* This is a candidate for bloom-filter pull-down (early evaluation).
158304 }while( iLevel < pWInfo->nLevel );
158324 Parse *pParse = pWInfo->pParse;
158335 ExprList *pOrderBy = pWInfo->pOrderBy;
158338 pTab = pSrc->pTab;
158346 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
158347 pTerm->wtFlags &= ~TERM_OK;
158348 if( pTerm->leftCursor != pSrc->iCursor ) continue;
158349 if( pTerm->prereqRight & mUnusable ) continue;
158350 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
158351 testcase( pTerm->eOperator & WO_IN );
158352 testcase( pTerm->eOperator & WO_ISNULL );
158353 testcase( pTerm->eOperator & WO_IS );
158354 testcase( pTerm->eOperator & WO_ALL );
158355 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
158356 if( pTerm->wtFlags & TERM_VNULL ) continue;
158358 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158359 assert( pTerm->u.x.leftColumn>=XN_ROWID );
158360 assert( pTerm->u.x.leftColumn<pTab->nCol );
158361 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
158367 pTerm->wtFlags |= TERM_OK;
158376 int n = pOrderBy->nExpr;
158378 Expr *pExpr = pOrderBy->a[i].pExpr;
158387 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
158389 /* First case - a direct column references without a COLLATE operator */
158390 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
158391 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
158395 /* 2nd case - a column reference with a COLLATE operator. Only match
158397 if( pExpr->op==TK_COLLATE
158398 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
158399 && pE2->iTable==pSrc->iCursor
158403 assert( pExpr->u.zToken!=0 );
158404 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
158405 pExpr->iColumn = pE2->iColumn;
158406 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
158407 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
158409 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
158417 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
158418 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
158419 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
158427 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
158436 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
158439 pIdxInfo->aConstraint = pIdxCons;
158440 pIdxInfo->aOrderBy = pIdxOrderBy;
158441 pIdxInfo->aConstraintUsage = pUsage;
158442 pHidden->pWC = pWC;
158443 pHidden->pParse = pParse;
158444 pHidden->eDistinct = eDistinct;
158445 pHidden->mIn = 0;
158446 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
158448 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
158449 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
158451 op = pTerm->eOperator & WO_ALL;
158453 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
158454 pHidden->mIn |= SMASKBIT32(j);
158459 pIdxCons[j].op = pTerm->eMatchOp;
158476 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
158479 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
158491 pIdxInfo->nConstraint = j;
158493 Expr *pExpr = pOrderBy->a[i].pExpr;
158495 assert( pExpr->op==TK_COLUMN
158496 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
158497 && pExpr->iColumn==pExpr->pLeft->iColumn) );
158498 pIdxOrderBy[j].iColumn = pExpr->iColumn;
158499 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
158502 pIdxInfo->nOrderBy = j;
158517 assert( pHidden->pParse!=0 );
158518 assert( pHidden->pParse->db==db );
158519 for(i=0; i<pIdxInfo->nConstraint; i++){
158520 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
158521 pHidden->aRhs[i] = 0;
158539 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
158543 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
158547 pParse->db->nSchemaLock++;
158548 rc = pVtab->pModule->xBestIndex(pVtab, p);
158549 pParse->db->nSchemaLock--;
158554 sqlite3OomFault(pParse->db);
158555 }else if( !pVtab->zErrMsg ){
158558 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
158561 sqlite3_free(pVtab->zErrMsg);
158562 pVtab->zErrMsg = 0;
158577 ** into the aSample[] array - it is an index into a virtual set of samples
158588 IndexSample *aSample = pIdx->aSample;
158602 assert( pIdx->nSample>0 );
158603 assert( pRec->nField>0 );
158649 nField = MIN(pRec->nField, pIdx->nSample);
158651 iSample = pIdx->nSample * nField;
158663 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
158669 pRec->nField = n;
158672 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
158675 iLower = aSample[iSamp].anLt[n-1];
158677 res = -1;
158680 iCol = n-1;
158689 if( pParse->db->mallocFailed==0 ){
158692 assert( i<pIdx->nSample );
158693 assert( iCol==nField-1 );
158694 pRec->nField = nField;
158696 || pParse->db->mallocFailed
158699 /* Unless i==pIdx->nSample, indicating that pRec is larger than
158702 assert( i<=pIdx->nSample && i>=0 );
158703 pRec->nField = iCol+1;
158704 assert( i==pIdx->nSample
158706 || pParse->db->mallocFailed );
158711 ** If (i>0), then pRec must also be greater than sample (i-1). */
158713 pRec->nField = iCol;
158715 || pParse->db->mallocFailed );
158718 pRec->nField = nField;
158719 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
158720 || pParse->db->mallocFailed );
158728 assert( iCol==nField-1 );
158733 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
158736 if( i>=pIdx->nSample ){
158737 iUpper = pIdx->nRowEst0;
158745 iGap = iUpper - iLower;
158753 aStat[1] = pIdx->aAvgEq[nField-1];
158756 /* Restore the pRec->nField value before returning. */
158757 pRec->nField = nField;
158776 if( pTerm->truthProb<=0 ){
158777 nRet += pTerm->truthProb;
158778 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
158779 nRet -= 20; assert( 20==sqlite3LogEst(4) );
158791 assert( iCol>=0 && iCol<pIdx->nColumn );
158792 if( !pIdx->zColAff ){
158795 assert( pIdx->zColAff[iCol]!=0 );
158796 return pIdx->zColAff[iCol];
158804 ** range-scan on a skip-scan index. For example:
158809 ** Value pLoop->nOut is currently set to the estimated number of rows
158820 ** N is the total number of samples, the pLoop->nOut value is adjusted
158823 ** nOut = nOut * ( min(U - L, 1) / N )
158844 Index *p = pLoop->u.btree.pIndex;
158845 int nEq = pLoop->u.btree.nEq;
158846 sqlite3 *db = pParse->db;
158847 int nLower = -1;
158848 int nUpper = p->nSample+1;
158857 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
158859 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
158863 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
158864 nUpper = p2 ? 0 : p->nSample;
158870 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
158871 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
158881 nDiff = (nUpper - nLower);
158890 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
158891 pLoop->nOut -= nAdjust;
158893 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
158894 nLower, nUpper, nAdjust*-1, pLoop->nOut));
158924 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
158932 ** left-most column of the index). Or, if the query is:
158957 int nOut = pLoop->nOut;
158961 Index *p = pLoop->u.btree.pIndex;
158962 int nEq = pLoop->u.btree.nEq;
158964 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
158965 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
158967 if( nEq==pBuilder->nRecValid ){
158968 UnpackedRecord *pRec = pBuilder->pRec;
158970 int nBtm = pLoop->u.btree.nBtm;
158971 int nTop = pLoop->u.btree.nTop;
158976 ** key-prefix formed by the nEq values matched against the nEq left-most
158990 ** The number of rows between the two bounds is then just iUpper-iLower.
158994 int iLwrIdx = -2; /* aSample[] for the lower bound */
158995 int iUprIdx = -1; /* aSample[] for the upper bound */
158998 testcase( pRec->nField!=pBuilder->nRecValid );
158999 pRec->nField = pBuilder->nRecValid;
159004 iUpper = p->nRowEst0;
159006 /* Note: this call could be optimized away - since the same values must
159013 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
159014 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
159015 assert( p->aSortOrder!=0 );
159016 if( p->aSortOrder[nEq] ){
159025 Expr *pExpr = pLower->pExpr->pRight;
159032 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
159034 nOut--;
159042 Expr *pExpr = pUpper->pExpr->pRight;
159049 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
159051 nOut--;
159056 pBuilder->pRec = pRec;
159059 nNew = sqlite3LogEst(iUpper - iLower);
159064 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
159085 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
159090 ** has an application-defined likelihood(), assume the range is
159091 ** reduced by an additional 75%. This means that, by default, an open-ended
159095 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
159096 nNew -= 20;
159099 nOut -= (pLower!=0) + (pUpper!=0);
159103 if( pLoop->nOut>nOut ){
159105 pLoop->nOut, nOut));
159108 pLoop->nOut = (LogEst)nOut;
159116 ** the histogram data. This only works when x is the left-most
159123 ** non-zero.
159136 Index *p = pBuilder->pNew->u.btree.pIndex;
159137 int nEq = pBuilder->pNew->u.btree.nEq;
159138 UnpackedRecord *pRec = pBuilder->pRec;
159144 assert( nEq<=p->nColumn );
159145 assert( p->aSample!=0 );
159146 assert( p->nSample>0 );
159147 assert( pBuilder->nRecValid<nEq );
159151 if( pBuilder->nRecValid<(nEq-1) ){
159157 if( nEq>=p->nColumn ){
159162 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
159163 pBuilder->pRec = pRec;
159166 pBuilder->nRecValid = nEq;
159170 p->zName, nEq-1, (int)a[1]));
159180 ** an IN constraint where the right-hand side of the IN operator
159187 ** non-zero.
159200 Index *p = pBuilder->pNew->u.btree.pIndex;
159201 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
159202 int nRecValid = pBuilder->nRecValid;
159208 assert( p->aSample!=0 );
159209 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
159211 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
159213 pBuilder->nRecValid = nRecValid;
159221 assert( pBuilder->nRecValid==nRecValid );
159233 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
159238 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
159239 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
159240 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
159241 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
159242 if( pTerm->eOperator & WO_SINGLE ){
159243 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
159245 pTerm->leftCursor, pTerm->u.x.leftColumn);
159246 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
159248 pTerm->u.pOrInfo->indexable);
159250 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
159253 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
159254 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
159258 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
159259 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
159261 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
159262 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
159264 if( pTerm->iParent>=0 ){
159265 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
159268 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
159279 for(i=0; i<pWC->nTerm; i++){
159280 sqlite3WhereTermPrint(&pWC->a[i], i);
159290 WhereInfo *pWInfo = pWC->pWInfo;
159291 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
159292 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
159293 Table *pTab = pItem->pTab;
159294 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
159295 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
159296 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
159298 pItem->zAlias ? pItem->zAlias : pTab->zName);
159299 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
159301 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
159303 int i = sqlite3Strlen30(zName) - 1;
159304 while( zName[i]!='_' ) i--;
159307 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
159313 if( p->u.vtab.idxStr ){
159315 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
159317 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
159319 sqlite3DebugPrintf(" %-19s", z);
159322 if( p->wsFlags & WHERE_SKIPSCAN ){
159323 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
159325 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
159327 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
159328 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
159330 for(i=0; i<p->nLTerm; i++){
159331 sqlite3WhereTermPrint(p->aLTerm[i], i);
159342 p->aLTerm = p->aLTermSpace;
159343 p->nLTerm = 0;
159344 p->nLSlot = ArraySize(p->aLTermSpace);
159345 p->wsFlags = 0;
159352 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
159353 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
159354 sqlite3_free(p->u.vtab.idxStr);
159355 p->u.vtab.needFree = 0;
159356 p->u.vtab.idxStr = 0;
159357 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
159358 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
159359 sqlite3DbFreeNN(db, p->u.btree.pIndex);
159360 p->u.btree.pIndex = 0;
159370 if( p->aLTerm!=p->aLTermSpace ){
159371 sqlite3DbFreeNN(db, p->aLTerm);
159372 p->aLTerm = p->aLTermSpace;
159373 p->nLSlot = ArraySize(p->aLTermSpace);
159376 p->nLTerm = 0;
159377 p->wsFlags = 0;
159381 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
159385 if( p->nLSlot>=n ) return SQLITE_OK;
159387 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
159389 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
159390 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
159391 p->aLTerm = paNew;
159392 p->nLSlot = n;
159401 if( pFrom->nLTerm > pTo->nLSlot
159402 && whereLoopResize(db, pTo, pFrom->nLTerm)
159408 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
159409 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
159410 pFrom->u.vtab.needFree = 0;
159411 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
159412 pFrom->u.btree.pIndex = 0;
159432 sqlite3WhereClauseClear(&pWInfo->sWC);
159433 while( pWInfo->pLoops ){
159434 WhereLoop *p = pWInfo->pLoops;
159435 pWInfo->pLoops = p->pNextLoop;
159438 while( pWInfo->pMemToFree ){
159439 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
159440 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
159441 pWInfo->pMemToFree = pNext;
159460 ** was added because if X uses skip-scan less than Y it still might
159463 ** than a non-covering index even if it is a proper subset.
159470 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
159473 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
159474 if( pY->nSkip > pX->nSkip ) return 0;
159475 for(i=pX->nLTerm-1; i>=0; i--){
159476 if( pX->aLTerm[i]==0 ) continue;
159477 for(j=pY->nLTerm-1; j>=0; j--){
159478 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
159482 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
159483 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
159504 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
159505 for(; p; p=p->pNextLoop){
159506 if( p->iTab!=pTemplate->iTab ) continue;
159507 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
159512 pTemplate->rRun, pTemplate->nOut,
159513 MIN(p->rRun, pTemplate->rRun),
159514 MIN(p->nOut - 1, pTemplate->nOut)));
159515 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
159516 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
159521 pTemplate->rRun, pTemplate->nOut,
159522 MAX(p->rRun, pTemplate->rRun),
159523 MAX(p->nOut + 1, pTemplate->nOut)));
159524 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
159525 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
159549 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
159550 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
159559 assert( p->rSetup==0 || pTemplate->rSetup==0
159560 || p->rSetup==pTemplate->rSetup );
159564 ** rSetup. Call this SETUP-INVARIANT */
159565 assert( p->rSetup>=pTemplate->rSetup );
159567 /* Any loop using an appliation-defined index (or PRIMARY KEY or
159569 ** than an automatic index. Unless it is a skip-scan. */
159570 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
159571 && (pTemplate->nSkip)==0
159572 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
159573 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
159574 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
159584 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
159585 && p->rSetup<=pTemplate->rSetup /* (2a) */
159586 && p->rRun<=pTemplate->rRun /* (2b) */
159587 && p->nOut<=pTemplate->nOut /* (2c) */
159597 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
159598 && p->rRun>=pTemplate->rRun /* (2a) */
159599 && p->nOut>=pTemplate->nOut /* (2b) */
159601 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
159617 ** If pBuilder->pOrSet is not NULL then we care about only the
159619 ** information is gathered in the pBuilder->pOrSet object. This special
159622 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
159634 WhereInfo *pWInfo = pBuilder->pWInfo;
159635 sqlite3 *db = pWInfo->pParse->db;
159639 if( pBuilder->iPlanLimit==0 ){
159641 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
159644 pBuilder->iPlanLimit--;
159646 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
159648 /* If pBuilder->pOrSet is defined, then only keep track of the costs
159651 if( pBuilder->pOrSet!=0 ){
159652 if( pTemplate->nLTerm ){
159654 u16 n = pBuilder->pOrSet->n;
159657 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
159658 pTemplate->nOut);
159661 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
159662 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159671 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
159679 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159695 sqlite3WhereLoopPrint(p, pBuilder->pWC);
159700 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159708 p->pNextLoop = 0;
159713 WhereLoop **ppTail = &p->pNextLoop;
159720 *ppTail = pToDel->pNextLoop;
159724 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
159731 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
159732 Index *pIndex = p->u.btree.pIndex;
159733 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
159734 p->u.btree.pIndex = 0;
159753 ** TODO --> Perhaps this is something that could be improved by better
159757 ** value corresponds to -1 in LogEst notation, so this means decrement
159764 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
159765 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
159775 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
159777 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
159779 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
159780 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
159782 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
159783 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
159784 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
159785 for(j=pLoop->nLTerm-1; j>=0; j--){
159786 pX = pLoop->aLTerm[j];
159789 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
159792 if( pLoop->maskSelf==pTerm->prereqAll ){
159796 ** "self-culling".
159798 ** 2022-03-24: Self-culling only applies if either the extra terms
159799 ** are straight comparison operators that are non-true with NULL
159802 if( (pTerm->eOperator & 0x3f)!=0
159803 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
159806 pLoop->wsFlags |= WHERE_SELFCULL;
159809 if( pTerm->truthProb<=0 ){
159812 pLoop->nOut += pTerm->truthProb;
159816 pLoop->nOut--;
159817 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
159818 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
159820 Expr *pRight = pTerm->pExpr->pRight;
159822 testcase( pTerm->pExpr->op==TK_IS );
159823 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
159829 pTerm->wtFlags |= TERM_HEURTRUTH;
159836 if( pLoop->nOut > nRow-iReduce ){
159837 pLoop->nOut = nRow - iReduce;
159865 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
159868 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
159877 assert( ExprUseXList(pTerm->pExpr->pLeft) );
159878 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
159879 pRhs = pTerm->pExpr->pRight;
159881 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
159883 pRhs = pRhs->x.pList->a[i].pExpr;
159890 if( pLhs->op!=TK_COLUMN
159891 || pLhs->iTable!=iCur
159892 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
159893 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
159898 testcase( pLhs->iColumn==XN_ROWID );
159900 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
159905 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
159912 ** compiled with -DSQLITE_ENABLE_COSTMULT
159921 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
159924 ** When this function is called, pBuilder->pNew->nOut contains the
159929 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
159938 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
159939 Parse *pParse = pWInfo->pParse; /* Parsing context */
159940 sqlite3 *db = pParse->db; /* Database connection malloc context */
159945 Bitmask saved_prereq; /* Original value of pNew->prereq */
159946 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
159947 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
159948 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
159949 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
159950 u16 saved_nSkip; /* Original value of pNew->nSkip */
159951 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
159952 LogEst saved_nOut; /* Original value of pNew->nOut */
159958 pNew = pBuilder->pNew;
159959 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
159961 pProbe->pTable->zName,pProbe->zName,
159962 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
159964 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
159965 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
159966 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
159969 assert( pNew->u.btree.nBtm==0 );
159972 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
159974 assert( pNew->u.btree.nEq<pProbe->nColumn );
159975 assert( pNew->u.btree.nEq<pProbe->nKeyCol
159976 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
159978 saved_nEq = pNew->u.btree.nEq;
159979 saved_nBtm = pNew->u.btree.nBtm;
159980 saved_nTop = pNew->u.btree.nTop;
159981 saved_nSkip = pNew->nSkip;
159982 saved_nLTerm = pNew->nLTerm;
159983 saved_wsFlags = pNew->wsFlags;
159984 saved_prereq = pNew->prereq;
159985 saved_nOut = pNew->nOut;
159986 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
159988 pNew->rSetup = 0;
159989 rSize = pProbe->aiRowLogEst[0];
159992 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
159997 int nRecValid = pBuilder->nRecValid;
159999 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
160004 if( pTerm->prereqRight & pNew->maskSelf ) continue;
160008 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
160010 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
160015 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
160016 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
160018 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
160020 pNew->wsFlags = saved_wsFlags;
160021 pNew->u.btree.nEq = saved_nEq;
160022 pNew->u.btree.nBtm = saved_nBtm;
160023 pNew->u.btree.nTop = saved_nTop;
160024 pNew->nLTerm = saved_nLTerm;
160025 if( pNew->nLTerm>=pNew->nLSlot
160026 && whereLoopResize(db, pNew, pNew->nLTerm+1)
160028 break; /* OOM while trying to enlarge the pNew->aLTerm array */
160030 pNew->aLTerm[pNew->nLTerm++] = pTerm;
160031 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
160034 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
160035 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
160036 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
160040 Expr *pExpr = pTerm->pExpr;
160051 for(i=0; i<pNew->nLTerm-1; i++){
160052 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
160054 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
160056 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
160058 if( pProbe->hasStat1 && rLogSize>=10 ){
160065 ** the left-most index column, M==N.
160075 ** with the index, as using an index has better worst-case behavior.
160080 M = pProbe->aiRowLogEst[saved_nEq];
160082 /* TUNING v----- 10 to bias toward indexed IN */
160083 x = M + logK + 10 - (nIn + rLogSize);
160092 " nInMul=%d) prefers skip-scan\n",
160094 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
160103 pNew->wsFlags |= WHERE_COLUMN_IN;
160105 int iCol = pProbe->aiColumn[saved_nEq];
160106 pNew->wsFlags |= WHERE_COLUMN_EQ;
160107 assert( saved_nEq==pNew->u.btree.nEq );
160109 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
160111 if( iCol==XN_ROWID || pProbe->uniqNotNull
160112 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
160114 pNew->wsFlags |= WHERE_ONEROW;
160116 pNew->wsFlags |= WHERE_UNQ_WANTED;
160119 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
160121 pNew->wsFlags |= WHERE_COLUMN_NULL;
160124 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
160129 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
160130 pNew->u.btree.nBtm = nVecLen;
160133 if( pTerm->wtFlags & TERM_LIKEOPT ){
160137 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
160138 assert( pTop->wtFlags & TERM_LIKEOPT );
160139 assert( pTop->eOperator==WO_LT );
160140 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
160141 pNew->aLTerm[pNew->nLTerm++] = pTop;
160142 pNew->wsFlags |= WHERE_TOP_LIMIT;
160143 pNew->u.btree.nTop = 1;
160149 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
160150 pNew->u.btree.nTop = nVecLen;
160152 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
160153 pNew->aLTerm[pNew->nLTerm-2] : 0;
160157 /* At this point pNew->nOut is set to the number of rows expected to
160161 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
160162 assert( pNew->nOut==saved_nOut );
160163 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
160168 int nEq = ++pNew->u.btree.nEq;
160171 assert( pNew->nOut==saved_nOut );
160172 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
160175 pNew->nOut += pTerm->truthProb;
160176 pNew->nOut -= nIn;
160181 && pProbe->nSample
160182 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
160183 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
160186 Expr *pExpr = pTerm->pExpr;
160191 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
160193 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
160198 pNew->nOut = sqlite3LogEst(nOut);
160202 ** See tag-202002240-1 */
160203 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
160212 pTerm->wtFlags |= TERM_HIGHTRUTH;
160213 if( pTerm->wtFlags & TERM_HEURTRUTH ){
160217 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
160220 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
160221 pNew->nOut -= nIn;
160227 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
160232 pNew->nOut += 10;
160239 ** it to pNew->rRun, which is currently set to the cost of the index
160240 ** seek only. Then, if this is a non-covering index, add the cost of
160242 assert( pSrc->pTab->szTabRow>0 );
160243 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
160244 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
160245 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
160246 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
160248 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
160250 nOutUnadjusted = pNew->nOut;
160251 pNew->rRun += nInMul + nIn;
160252 pNew->nOut += nInMul + nIn;
160253 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
160256 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
160257 pNew->nOut = saved_nOut;
160259 pNew->nOut = nOutUnadjusted;
160262 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
160263 && pNew->u.btree.nEq<pProbe->nColumn
160264 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
160265 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
160269 pNew->nOut = saved_nOut;
160271 pBuilder->nRecValid = nRecValid;
160274 pNew->prereq = saved_prereq;
160275 pNew->u.btree.nEq = saved_nEq;
160276 pNew->u.btree.nBtm = saved_nBtm;
160277 pNew->u.btree.nTop = saved_nTop;
160278 pNew->nSkip = saved_nSkip;
160279 pNew->wsFlags = saved_wsFlags;
160280 pNew->nOut = saved_nOut;
160281 pNew->nLTerm = saved_nLTerm;
160283 /* Consider using a skip-scan if there are no WHERE clause constraints
160284 ** available for the left-most terms of the index, and if the average
160285 ** number of repeats in the left-most terms is at least 18.
160295 && saved_nEq+1<pProbe->nKeyCol
160296 && saved_nEq==pNew->nLTerm
160297 && pProbe->noSkipScan==0
160298 && pProbe->hasStat1!=0
160300 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
160301 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
160304 pNew->u.btree.nEq++;
160305 pNew->nSkip++;
160306 pNew->aLTerm[pNew->nLTerm++] = 0;
160307 pNew->wsFlags |= WHERE_SKIPSCAN;
160308 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
160309 pNew->nOut -= nIter;
160310 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
160311 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
160314 pNew->nOut = saved_nOut;
160315 pNew->u.btree.nEq = saved_nEq;
160316 pNew->nSkip = saved_nSkip;
160317 pNew->wsFlags = saved_wsFlags;
160321 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
160342 if( pIndex->bUnordered ) return 0;
160343 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
160344 for(ii=0; ii<pOB->nExpr; ii++){
160345 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
160347 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
160348 if( pExpr->iColumn<0 ) return 1;
160349 for(jj=0; jj<pIndex->nKeyCol; jj++){
160350 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
160352 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
160353 for(jj=0; jj<pIndex->nKeyCol; jj++){
160354 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
160355 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
160378 pParse = pWC->pWInfo->pParse;
160379 while( pWhere->op==TK_AND ){
160380 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
160381 pWhere = pWhere->pRight;
160383 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
160384 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
160386 pExpr = pTerm->pExpr;
160387 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
160390 && (pTerm->wtFlags & TERM_VNULL)==0
160407 ** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
160409 ** If the Expr node references the table with cursor pCk->iTabCur, then
160410 ** make sure that column is covered by the index pCk->pIdx. We know that
160411 ** all columns less than 63 (really BMS-1) are covered, so we don't need
160414 ** If the index does not cover the column, then set pWalk->eCode to
160415 ** non-zero and return WRC_Abort to stop the search.
160425 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
160426 if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
160427 if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
160428 pIdx = pWalk->u.pCovIdxCk->pIdx;
160429 aiColumn = pIdx->aiColumn;
160430 nColumn = pIdx->nColumn;
160432 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
160434 pWalk->eCode = 1;
160440 ** pIdx is an index that covers all of the low-number columns used by
160441 ** pWInfo->pSelect (columns from 0 through 62). But there are columns
160442 ** in pWInfo->pSelect beyond 62. This routine tries to answer the question
160445 ** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
160449 ** This routine is an optimization. It is always safe to return non-zero.
160450 ** But returning zero when non-zero should have been returned can lead to
160461 if( pWInfo->pSelect==0 ){
160466 for(i=0; i<pIdx->nColumn; i++){
160467 if( pIdx->aiColumn[i]>=BMS-1 ) break;
160469 if( i>=pIdx->nColumn ){
160482 sqlite3WalkSelect(&w, pWInfo->pSelect);
160488 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
160489 ** a b-tree table, not a virtual table.
160491 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
160496 ** cost = nRow * 3.0 // full-table scan
160498 ** cost = nRow * (K+3.0) // scan of non-covering index
160505 ** the index b-tree:
160508 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
160512 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
160517 ** log(nRow) factor is omitted from a non-covering index scan in order to
160518 ** bias the scoring in favor of using an index, since the worst-case
160519 ** performance of using an index is far better than the worst-case performance
160530 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
160541 pNew = pBuilder->pNew;
160542 pWInfo = pBuilder->pWInfo;
160543 pTabList = pWInfo->pTabList;
160544 pSrc = pTabList->a + pNew->iTab;
160545 pTab = pSrc->pTab;
160546 pWC = pBuilder->pWC;
160547 assert( !IsVirtual(pSrc->pTab) );
160549 if( pSrc->fg.isIndexedBy ){
160550 assert( pSrc->fg.isCte==0 );
160552 pProbe = pSrc->u2.pIBIndex;
160554 pProbe = pTab->pIndex;
160568 sPk.szIdxRow = pTab->szTabRow;
160570 aiRowEstPk[0] = pTab->nRowLogEst;
160572 pFirst = pSrc->pTab->pIndex;
160573 if( pSrc->fg.notIndexed==0 ){
160580 rSize = pTab->nRowLogEst;
160584 if( !pBuilder->pOrSet /* Not part of an OR optimization */
160585 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
160586 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
160587 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
160588 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
160590 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
160591 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
160592 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
160594 /* Generate auto-index WhereLoops */
160597 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
160599 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
160600 if( pTerm->prereqRight & pNew->maskSelf ) continue;
160602 pNew->u.btree.nEq = 1;
160603 pNew->nSkip = 0;
160604 pNew->u.btree.pIndex = 0;
160605 pNew->nLTerm = 1;
160606 pNew->aLTerm[0] = pTerm;
160607 /* TUNING: One-time cost for computing the automatic index is
160610 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
160615 pNew->rSetup = rLogSize + rSize;
160616 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
160617 pNew->rSetup += 28;
160619 pNew->rSetup -= 10;
160621 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
160622 if( pNew->rSetup<0 ) pNew->rSetup = 0;
160627 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
160628 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
160629 pNew->wsFlags = WHERE_AUTO_INDEX;
160630 pNew->prereq = mPrereq | pTerm->prereqRight;
160640 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
160642 if( pProbe->pPartIdxWhere!=0
160643 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
160644 pProbe->pPartIdxWhere)
160646 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
160649 if( pProbe->bNoQuery ) continue;
160650 rSize = pProbe->aiRowLogEst[0];
160651 pNew->u.btree.nEq = 0;
160652 pNew->u.btree.nBtm = 0;
160653 pNew->u.btree.nTop = 0;
160654 pNew->nSkip = 0;
160655 pNew->nLTerm = 0;
160656 pNew->iSortIdx = 0;
160657 pNew->rSetup = 0;
160658 pNew->prereq = mPrereq;
160659 pNew->nOut = rSize;
160660 pNew->u.btree.pIndex = pProbe;
160661 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
160664 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
160665 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
160667 pNew->wsFlags = WHERE_IPK;
160670 pNew->iSortIdx = b ? iSortIdx : 0;
160673 ** since index lookups have better worst-case performance if our
160684 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
160686 pNew->rRun = rSize + 16;
160688 if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
160689 pNew->wsFlags |= WHERE_VIEWSCAN;
160691 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
160694 pNew->nOut = rSize;
160698 if( pProbe->isCovering ){
160699 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
160702 m = pSrc->colUsed & pProbe->colNotIdxed;
160704 m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
160706 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
160712 || pProbe->pPartIdxWhere!=0
160713 || pSrc->fg.isIndexedBy
160715 && pProbe->bUnordered==0
160716 && (pProbe->szIdxRow<pTab->szTabRow)
160717 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
160719 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
160722 pNew->iSortIdx = b ? iSortIdx : 0;
160727 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
160729 /* If this is a non-covering index scan, add in the cost of
160736 int iCur = pSrc->iCursor;
160737 WhereClause *pWC2 = &pWInfo->sWC;
160738 for(ii=0; ii<pWC2->nTerm; ii++){
160739 WhereTerm *pTerm = &pWC2->a[ii];
160740 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
160745 if( pTerm->truthProb<=0 ){
160746 nLookup += pTerm->truthProb;
160748 nLookup--;
160749 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
160753 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
160755 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
160757 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
160758 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
160760 ** positioned to the correct row during the right-join no-match
160765 pNew->nOut = rSize;
160770 pBuilder->bldFlags1 = 0;
160772 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
160773 /* If a non-unique index is used, or if a prefix of the key for
160774 ** unique index is used (making the index functionally non-unique)
160777 pTab->tabFlags |= TF_StatsUsed;
160780 sqlite3Stat4ProbeFree(pBuilder->pRec);
160781 pBuilder->nRecValid = 0;
160782 pBuilder->pRec = 0;
160794 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
160795 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
160796 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
160801 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
160829 WhereClause *pWC = pBuilder->pWC;
160832 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
160836 WhereLoop *pNew = pBuilder->pNew;
160837 Parse *pParse = pBuilder->pWInfo->pParse;
160838 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
160839 int nConstraint = pIdxInfo->nConstraint;
160843 pNew->prereq = mPrereq;
160847 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
160849 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
160850 pIdxCons->usable = 0;
160851 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
160852 && (pTerm->eOperator & mExclude)==0
160855 pIdxCons->usable = 1;
160861 assert( pIdxInfo->needToFreeIdxStr==0 );
160862 pIdxInfo->idxStr = 0;
160863 pIdxInfo->idxNum = 0;
160864 pIdxInfo->orderByConsumed = 0;
160865 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
160866 pIdxInfo->estimatedRows = 25;
160867 pIdxInfo->idxFlags = 0;
160868 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
160869 pHidden->mHandleIn = 0;
160872 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
160879 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
160885 mxTerm = -1;
160886 assert( pNew->nLSlot>=nConstraint );
160887 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
160888 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
160889 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
160892 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
160894 int j = pIdxCons->iTermOffset;
160897 || j>=pWC->nTerm
160898 || pNew->aLTerm[iTerm]!=0
160899 || pIdxCons->usable==0
160901 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
160902 testcase( pIdxInfo->needToFreeIdxStr );
160905 testcase( iTerm==nConstraint-1 );
160907 testcase( j==pWC->nTerm-1 );
160908 pTerm = &pWC->a[j];
160909 pNew->prereq |= pTerm->prereqRight;
160910 assert( iTerm<pNew->nLSlot );
160911 pNew->aLTerm[iTerm] = pTerm;
160918 pNew->u.vtab.omitMask |= 1<<iTerm;
160922 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
160923 pNew->u.vtab.bOmitOffset = 1;
160926 if( SMASKBIT32(i) & pHidden->mHandleIn ){
160927 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
160928 }else if( (pTerm->eOperator & WO_IN)!=0 ){
160934 pIdxInfo->orderByConsumed = 0;
160935 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
160946 if( pIdxInfo->needToFreeIdxStr ){
160947 sqlite3_free(pIdxInfo->idxStr);
160948 pIdxInfo->idxStr = 0;
160949 pIdxInfo->needToFreeIdxStr = 0;
160957 pNew->nLTerm = mxTerm+1;
160959 if( pNew->aLTerm[i]==0 ){
160960 /* The non-zero argvIdx values must be contiguous. Raise an
160962 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
160963 testcase( pIdxInfo->needToFreeIdxStr );
160967 assert( pNew->nLTerm<=pNew->nLSlot );
160968 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
160969 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
160970 pIdxInfo->needToFreeIdxStr = 0;
160971 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
160972 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
160973 pIdxInfo->nOrderBy : 0);
160974 pNew->rSetup = 0;
160975 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
160976 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
160980 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
160981 pNew->wsFlags |= WHERE_ONEROW;
160983 pNew->wsFlags &= ~WHERE_ONEROW;
160986 if( pNew->u.vtab.needFree ){
160987 sqlite3_free(pNew->u.vtab.idxStr);
160988 pNew->u.vtab.needFree = 0;
160992 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
161015 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
161017 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
161018 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
161019 if( pX->pLeft ){
161020 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
161022 zRet = (pC ? pC->zName : sqlite3StrBINARY);
161035 if( m & pHidden->mIn ){
161037 pHidden->mHandleIn &= ~m;
161039 pHidden->mHandleIn |= m;
161050 ** on the right-hand-side of constraint iCons.
161060 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
161061 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
161063 if( pH->aRhs[iCons]==0 ){
161064 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
161066 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
161067 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
161071 pVal = pH->aRhs[iCons];
161075 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
161076 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
161087 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
161088 return pHidden->eDistinct;
161096 ** prepared is read-only, then just start read transactions on all
161100 ** This is used by the (built-in) sqlite_dbpage virtual table.
161104 Parse *pParse = pHidden->pParse;
161105 int nDb = pParse->db->nDb;
161110 if( pParse->writeMask ){
161120 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
161141 ** mUnusable should always be configured as "not-usable" for xBestIndex.
161162 pWInfo = pBuilder->pWInfo;
161163 pParse = pWInfo->pParse;
161164 pWC = pBuilder->pWC;
161165 pNew = pBuilder->pNew;
161166 pSrc = &pWInfo->pTabList->a[pNew->iTab];
161167 assert( IsVirtual(pSrc->pTab) );
161170 pNew->rSetup = 0;
161171 pNew->wsFlags = WHERE_VIRTUALTABLE;
161172 pNew->nLTerm = 0;
161173 pNew->u.vtab.needFree = 0;
161174 nConstraint = p->nConstraint;
161175 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
161176 freeIndexInfo(pParse->db, p);
161181 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
161198 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
161211 mBestNoIn = pNew->prereq & ~mPrereq;
161226 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
161237 if( pNew->prereq==mPrereq ){
161263 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
161264 freeIndexInfo(pParse->db, p);
161265 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
161279 WhereInfo *pWInfo = pBuilder->pWInfo;
161290 pWC = pBuilder->pWC;
161291 pWCEnd = pWC->a + pWC->nTerm;
161292 pNew = pBuilder->pNew;
161294 pItem = pWInfo->pTabList->a + pNew->iTab;
161295 iCur = pItem->iCursor;
161297 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
161298 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
161300 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
161301 if( (pTerm->eOperator & WO_OR)!=0
161302 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
161304 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
161305 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
161313 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
161314 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
161315 if( (pOrTerm->eOperator & WO_AND)!=0 ){
161316 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
161317 }else if( pOrTerm->leftCursor==iCur ){
161318 tempWC.pWInfo = pWC->pWInfo;
161330 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
161331 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
161337 if( IsVirtual(pItem->pTab) ){
161370 pNew->nLTerm = 1;
161371 pNew->aLTerm[0] = pTerm;
161372 pNew->wsFlags = WHERE_MULTI_OR;
161373 pNew->rSetup = 0;
161374 pNew->iSortIdx = 0;
161375 memset(&pNew->u, 0, sizeof(pNew->u));
161378 ** of all sub-scans required by the OR-scan. However, due to rounding
161379 ** errors, it may be that the cost of the OR-scan is equal to its
161380 ** most expensive sub-scan. Add the smallest possible penalty
161387 ** the planner may elect to "OR" together a full-table scan and an
161389 pNew->rRun = sSum.a[i].rRun + 1;
161390 pNew->nOut = sSum.a[i].nOut;
161391 pNew->prereq = sSum.a[i].prereq;
161394 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
161404 WhereInfo *pWInfo = pBuilder->pWInfo;
161408 SrcList *pTabList = pWInfo->pTabList;
161410 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
161411 sqlite3 *db = pWInfo->pParse->db;
161419 pNew = pBuilder->pNew;
161422 assert( pNew->nLTerm==0 );
161423 assert( pNew->wsFlags==0 );
161424 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
161425 assert( pNew->aLTerm!=0 );
161427 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
161428 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
161430 pNew->iTab = iTab;
161431 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
161432 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
161434 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
161442 ** prevent FROM-clause terms from moving from the right side of
161446 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
161448 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
161453 if( IsVirtual(pItem->pTab) ){
161456 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
161457 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
161466 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
161469 mPrior |= pNew->maskSelf;
161470 if( rc || db->mallocFailed ){
161500 ** pOrderBy terms must be matched in strict left-to-right order.
161507 u16 nLoop, /* Number of entries in pPath->aLoop[] */
161508 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
161514 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
161530 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
161533 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
161537 ** We say the WhereLoop is "one-row" if it generates no more than one
161538 ** row of output. A WhereLoop is one-row if all of the following are true:
161541 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
161542 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
161544 ** We say the WhereLoop is "order-distinct" if the set of columns from
161546 ** row of the WhereLoop. Every one-row WhereLoop is automatically
161547 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
161548 ** is not order-distinct. To be order-distinct is not quite the same as being
161550 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
161551 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
161555 ** automatically order-distinct.
161561 nOrderBy = pOrderBy->nExpr;
161562 testcase( nOrderBy==BMS-1 );
161563 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
161565 obDone = MASKBIT(nOrderBy)-1;
161573 if( iLoop>0 ) ready |= pLoop->maskSelf;
161575 pLoop = pPath->aLoop[iLoop];
161580 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
161581 if( pLoop->u.vtab.isOrdered
161588 pLoop->u.btree.nDistinctCol = 0;
161590 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
161599 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
161601 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
161602 if( pOBExpr->iTable!=iCur ) continue;
161603 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
161606 if( pTerm->eOperator==WO_IN ){
161612 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
161613 if( j>=pLoop->nLTerm ) continue;
161615 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
161616 Parse *pParse = pWInfo->pParse;
161617 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
161618 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
161620 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
161623 testcase( pTerm->pExpr->op==TK_IS );
161628 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
161629 if( pLoop->wsFlags & WHERE_IPK ){
161633 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
161636 nKeyCol = pIndex->nKeyCol;
161637 nColumn = pIndex->nColumn;
161638 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
161639 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
161640 || !HasRowid(pIndex->pTable));
161641 /* All relevant terms of the index must also be non-NULL in order
161644 ** made at tag-20210426-1 below */
161646 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
161657 assert( j>=pLoop->u.btree.nEq
161658 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
161660 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
161661 u16 eOp = pLoop->aLTerm[j]->eOperator;
161666 ** the loop need to be marked as not order-distinct because it can
161685 ** j<pLoop->u.btree.nEq constraint above. Any equality other
161688 Expr *pX = pLoop->aLTerm[j]->pExpr;
161689 for(i=j+1; i<pLoop->u.btree.nEq; i++){
161690 if( pLoop->aLTerm[i]->pExpr==pX ){
161691 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
161700 ** (revIdx) for the j-th column of the index.
161703 iColumn = pIndex->aiColumn[j];
161704 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
161705 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
161712 ** WhereLoop is not well-ordered. tag-20210426-1
161716 && j>=pLoop->u.btree.nEq
161717 && pIndex->pTable->aCol[iColumn].notNull==0
161726 /* Find the ORDER BY term that corresponds to the j-th column
161732 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
161738 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
161739 if( pOBExpr->iTable!=iCur ) continue;
161740 if( pOBExpr->iColumn!=iColumn ) continue;
161742 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
161748 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
161749 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
161752 pLoop->u.btree.nDistinctCol = j+1;
161762 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
161767 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
161772 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
161773 if( j==pLoop->u.btree.nEq ){
161774 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
161798 } /* end-if not one-row */
161802 orderDistinctMask |= pLoop->maskSelf;
161807 p = pOrderBy->a[i].pExpr;
161808 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
161815 } /* End the loop over all WhereLoops from outer-most down to inner-most */
161818 for(i=nOrderBy-1; i>0; i--){
161819 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
161824 return -1;
161831 ** BY clause - and so any order that groups rows as required satisfies the
161848 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
161849 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
161852 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
161853 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
161854 return pWInfo->sorted;
161862 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
161863 if( pLast ) zName[i++] = pLast->cId;
161885 ** Or, if the order-by clause has X terms but only the last Y
161886 ** terms are out of order, then block-sorting will reduce the
161896 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
161904 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
161905 nRow = pWInfo->iLimit;
161906 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
161909 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
161916 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
161918 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
161948 pParse = pWInfo->pParse;
161949 nLoop = pWInfo->nLevel;
161951 ** For 2-way joins, the 5 best paths are followed.
161954 assert( nLoop<=pWInfo->pTabList->nSrc );
161955 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
161962 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
161965 nOrderBy = pWInfo->pOrderBy->nExpr;
161971 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
161977 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
161978 pFrom->aLoop = pX;
161983 ** is either zero - meaning it has not yet been initialized - or the
161998 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
162006 ** -1, indicating that the result set may or may not be ordered,
162008 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
162017 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
162023 Bitmask revMask; /* Mask of rev-order loops for (..) */
162025 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
162026 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
162027 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
162038 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
162039 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
162040 nOut = pFrom->nRow + pWLoop->nOut;
162041 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
162042 isOrdered = pFrom->isOrdered;
162046 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
162049 revMask = pFrom->revLoop;
162064 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
162065 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
162069 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
162072 /* TUNING: A full-scan of a VIEW or subquery in the outer loop
162074 if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){
162075 rCost += -10;
162076 nOut += -30;
162080 ** mxChoice best-so-far paths.
162082 ** First look for an existing path among best-so-far paths
162086 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
162087 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
162088 ** of legal values for isOrdered, -1..64.
162091 if( pTo->maskLoop==maskNew
162092 && ((pTo->isOrdered^isOrdered)&0x80)==0
162094 testcase( jj==nTo-1 );
162099 /* None of the existing best-so-far paths match the candidate. */
162104 ** paths currently in the best-so-far buffer. So discard
162108 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
162116 ** needs to be added to the set of best-so-far paths. */
162127 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
162133 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
162139 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
162141 if( pTo->rCost<rCost
162142 || (pTo->rCost==rCost
162143 && (pTo->nRow<nOut
162144 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
162151 "Skip %s cost=%-3d,%3d,%3d order=%c",
162154 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
162155 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
162156 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
162160 testcase( pTo->rCost==rCost );
162163 testcase( pTo->rCost==rCost+1 );
162169 "Update %s cost=%-3d,%3d,%3d order=%c",
162172 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
162173 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
162174 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
162179 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
162180 pTo->revLoop = revMask;
162181 pTo->nRow = nOut;
162182 pTo->rCost = rCost;
162183 pTo->rUnsorted = rUnsorted;
162184 pTo->isOrdered = isOrdered;
162185 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
162186 pTo->aLoop[iLoop] = pWLoop;
162192 if( pTo->rCost>mxCost
162193 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
162195 mxCost = pTo->rCost;
162196 mxUnsorted = pTo->rUnsorted;
162206 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
162208 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
162209 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
162210 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
162211 if( pTo->isOrdered>0 ){
162212 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
162229 sqlite3StackFreeNN(pParse->db, pSpace);
162236 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
162238 assert( pWInfo->nLevel==nLoop );
162241 WhereLevel *pLevel = pWInfo->a + iLoop;
162242 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
162243 pLevel->iFrom = pWLoop->iTab;
162244 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
162246 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
162247 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
162248 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
162252 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
162253 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
162254 if( rc==pWInfo->pResultSet->nExpr ){
162255 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
162258 pWInfo->bOrderedInnerLoop = 0;
162259 if( pWInfo->pOrderBy ){
162260 pWInfo->nOBSat = pFrom->isOrdered;
162261 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
162262 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
162263 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
162266 pWInfo->revMask = pFrom->revLoop;
162267 if( pWInfo->nOBSat<=0 ){
162268 pWInfo->nOBSat = 0;
162270 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
162275 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
162276 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
162279 if( rc==pWInfo->pOrderBy->nExpr ){
162280 pWInfo->bOrderedInnerLoop = 1;
162281 pWInfo->revMask = m;
162286 && pWInfo->nOBSat==1
162287 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
162289 pWInfo->bOrderedInnerLoop = 1;
162292 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
162293 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
162296 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
162297 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
162299 assert( pWInfo->sorted==0 );
162300 if( nOrder==pWInfo->pOrderBy->nExpr ){
162301 pWInfo->sorted = 1;
162302 pWInfo->revMask = revMask;
162308 pWInfo->nRowOut = pFrom->nRow;
162311 sqlite3StackFreeNN(pParse->db, pSpace);
162319 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
162322 ** Return non-zero on success, if this query can be handled by this
162323 ** no-frills query planner. Return zero if this query needs the
162324 ** general-purpose query planner.
162338 pWInfo = pBuilder->pWInfo;
162339 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
162340 assert( pWInfo->pTabList->nSrc>=1 );
162341 pItem = pWInfo->pTabList->a;
162342 pTab = pItem->pTab;
162344 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
162345 testcase( pItem->fg.isIndexedBy );
162346 testcase( pItem->fg.notIndexed );
162349 iCur = pItem->iCursor;
162350 pWC = &pWInfo->sWC;
162351 pLoop = pBuilder->pNew;
162352 pLoop->wsFlags = 0;
162353 pLoop->nSkip = 0;
162354 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
162355 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
162357 testcase( pTerm->eOperator & WO_IS );
162358 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
162359 pLoop->aLTerm[0] = pTerm;
162360 pLoop->nLTerm = 1;
162361 pLoop->u.btree.nEq = 1;
162363 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
162365 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
162367 assert( pLoop->aLTermSpace==pLoop->aLTerm );
162369 || pIdx->pPartIdxWhere!=0
162370 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
162372 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
162373 for(j=0; j<pIdx->nKeyCol; j++){
162375 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
162377 testcase( pTerm->eOperator & WO_IS );
162378 pLoop->aLTerm[j] = pTerm;
162380 if( j!=pIdx->nKeyCol ) continue;
162381 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
162382 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
162383 pLoop->wsFlags |= WHERE_IDX_ONLY;
162385 pLoop->nLTerm = j;
162386 pLoop->u.btree.nEq = j;
162387 pLoop->u.btree.pIndex = pIdx;
162389 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
162393 if( pLoop->wsFlags ){
162394 pLoop->nOut = (LogEst)1;
162395 pWInfo->a[0].pWLoop = pLoop;
162396 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
162397 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
162398 pWInfo->a[0].iTabCur = iCur;
162399 pWInfo->nRowOut = 1;
162400 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
162401 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
162402 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
162404 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
162406 pLoop->cId = '0';
162422 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
162423 pWalker->eCode = 0;
162430 ** Return true if the expression contains no non-deterministic SQL
162431 ** functions. Do not consider non-deterministic SQL functions that are
162432 ** part of sub-select statements.
162455 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
162456 p->cId = zLabel[i%(sizeof(zLabel)-1)];
162503 assert( pWInfo->nLevel>=2 );
162504 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
162508 assert( pWInfo->pResultSet!=0 );
162509 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
162511 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
162512 if( pWInfo->pOrderBy ){
162513 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
162515 for(i=pWInfo->nLevel-1; i>=1; i--){
162519 pLoop = pWInfo->a[i].pWLoop;
162520 pItem = &pWInfo->pTabList->a[pLoop->iTab];
162521 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
162522 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
162523 && (pLoop->wsFlags & WHERE_ONEROW)==0
162527 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
162528 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
162529 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
162530 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
162531 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
162532 || pTerm->pExpr->w.iJoin!=pItem->iCursor
162539 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
162540 notReady &= ~pLoop->maskSelf;
162541 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
162542 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
162543 pTerm->wtFlags |= TERM_CODED;
162546 if( i!=pWInfo->nLevel-1 ){
162547 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
162548 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
162550 pWInfo->nLevel--;
162551 assert( pWInfo->nLevel>0 );
162565 ** (3) Bloom-filter processing is not disabled. (Checked by the
162580 assert( pWInfo->nLevel>=2 );
162581 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
162582 nSearch = pWInfo->a[0].pWLoop->nOut;
162583 for(i=1; i<pWInfo->nLevel; i++){
162584 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
162586 if( (pLoop->wsFlags & reqFlags)==reqFlags
162587 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
162588 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
162590 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
162591 Table *pTab = pItem->pTab;
162592 pTab->tabFlags |= TF_StatsUsed;
162593 if( nSearch > pTab->nRowLogEst
162594 && (pTab->tabFlags & TF_HasStat1)!=0
162596 testcase( pItem->fg.jointype & JT_LEFT );
162597 pLoop->wsFlags |= WHERE_BLOOMFILTER;
162598 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
162600 "-> use Bloom-filter on loop %c because there are ~%.1e "
162602 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
162603 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
162606 nSearch += pLoop->nOut;
162612 ** free the Parse->pIdxExpr list when the Parse object is destroyed.
162616 while( pParse->pIdxExpr!=0 ){
162617 IndexedExpr *p = pParse->pIdxExpr;
162618 pParse->pIdxExpr = p->pIENext;
162619 sqlite3ExprDelete(db, p->pExpr);
162630 ** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
162636 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
162637 Index *pIdx, /* The index-on-expression that contains the expressions */
162644 assert( pIdx->bHasExpr );
162645 pTab = pIdx->pTable;
162646 for(i=0; i<pIdx->nColumn; i++){
162648 int j = pIdx->aiColumn[i];
162651 pExpr = pIdx->aColExpr->a[i].pExpr;
162652 testcase( pTabItem->fg.jointype & JT_LEFT );
162653 testcase( pTabItem->fg.jointype & JT_RIGHT );
162654 testcase( pTabItem->fg.jointype & JT_LTORJ );
162655 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
162656 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
162657 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
162663 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
162665 p->pIENext = pParse->pIdxExpr;
162666 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
162667 p->iDataCur = pTabItem->iCursor;
162668 p->iIdxCur = iIdxCur;
162669 p->iIdxCol = i;
162670 p->bMaybeNullRow = bMaybeNullRow;
162672 p->zIdxName = pIdx->zName;
162674 pParse->pIdxExpr = p;
162675 if( p->pIENext==0 ){
162700 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
162704 ** end |-- by sqlite3WhereEnd()
162711 ** scanning through all values on the right-hand side of the IN.
162714 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
162724 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
162734 ** inner loops (or around the "..." if the test occurs within the inner-
162783 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
162787 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
162804 db = pParse->db;
162808 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
162809 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
162814 testcase( pTabList->nSrc==BMS );
162815 if( pTabList->nSrc>BMS ){
162825 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
162830 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
162831 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
162834 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
162836 if( db->mallocFailed ){
162841 pWInfo->pParse = pParse;
162842 pWInfo->pTabList = pTabList;
162843 pWInfo->pOrderBy = pOrderBy;
162845 pWInfo->pWhere = pWhere;
162847 pWInfo->pResultSet = pResultSet;
162848 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
162849 pWInfo->nLevel = nTabList;
162850 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
162851 pWInfo->wctrlFlags = wctrlFlags;
162852 pWInfo->iLimit = iAuxArg;
162853 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
162854 pWInfo->pSelect = pSelect;
162855 memset(&pWInfo->nOBSat, 0,
162856 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
162857 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
162858 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
162859 pMaskSet = &pWInfo->sMaskSet;
162860 pMaskSet->n = 0;
162861 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
162863 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
162865 sWLB.pWC = &pWInfo->sWC;
162870 sWLB.pNew->cId = '*';
162876 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
162877 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
162882 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
162886 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
162892 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
162895 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
162899 ** Note that bitmasks are created for all pTabList->nSrc tables in
162901 ** equal to pTabList->nSrc but might be shortened to 1 if the
162906 createMask(pMaskSet, pTabList->a[ii].iCursor);
162907 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
162908 }while( (++ii)<pTabList->nSrc );
162912 for(ii=0; ii<pTabList->nSrc; ii++){
162913 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
162922 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
162923 if( pSelect && pSelect->pLimit ){
162924 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
162926 if( pParse->nErr ) goto whereBeginError;
162932 ** Do not do this if the expression contains non-deterministic functions
162933 ** that are not within a sub-select. This is not strictly required, but
162936 ** FROM ... WHERE random()>0; -- eval random() once per row
162937 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
162939 for(ii=0; ii<sWLB.pWC->nBase; ii++){
162940 WhereTerm *pT = &sWLB.pWC->a[ii];
162941 if( pT->wtFlags & TERM_VIRTUAL ) continue;
162942 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
162943 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
162944 pT->wtFlags |= TERM_CODED;
162953 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
162954 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
162956 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
162959 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
162960 pWInfo->pOrderBy = pResultSet;
162984 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
163004 while( pWInfo->pLoops ){
163005 WhereLoop *p = pWInfo->pLoops;
163006 pWInfo->pLoops = p->pNextLoop;
163016 if( db->mallocFailed ) goto whereBeginError;
163017 if( pWInfo->pOrderBy ){
163018 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
163019 if( db->mallocFailed ) goto whereBeginError;
163022 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
163023 pWInfo->revMask = ALLBITS;
163025 if( pParse->nErr ){
163028 assert( db->mallocFailed==0 );
163031 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
163032 if( pWInfo->nOBSat>0 ){
163033 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
163035 switch( pWInfo->eDistinct ){
163050 for(ii=0; ii<pWInfo->nLevel; ii++){
163051 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
163059 ** This query optimization is factored out into a separate "no-inline"
163062 ** some C-compiler optimizers from in-lining the
163064 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
163067 if( pWInfo->nLevel>=2
163073 nTabList = pWInfo->nLevel;
163080 if( pWInfo->nLevel>=2
163088 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
163093 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
163096 ** to use a one-pass algorithm, determine if this is appropriate.
163098 ** A one-pass approach can be used if the caller has requested one
163102 ** * the caller has indicated that a one-pass approach can be used
163111 ** use a one-pass approach, and this is not set accurately for scans
163114 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
163116 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
163118 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
163121 && !IsVirtual(pTabList->a[0].pTab)
163124 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
163125 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
163129 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
163137 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
163142 pTabItem = &pTabList->a[pLevel->iFrom];
163143 pTab = pTabItem->pTab;
163144 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
163145 pLoop = pLevel->pWLoop;
163146 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
163150 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
163152 int iCur = pTabItem->iCursor;
163158 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
163160 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
163163 if( pWInfo->eOnePass!=ONEPASS_OFF ){
163165 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
163167 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
163168 assert( pTabItem->iCursor==pLevel->iTabCur );
163169 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
163170 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
163171 if( pWInfo->eOnePass==ONEPASS_OFF
163172 && pTab->nCol<BMS
163173 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
163174 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
163179 Bitmask b = pTabItem->colUsed;
163182 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
163183 assert( n<=pTab->nCol );
163186 if( pLoop->u.btree.pIndex!=0 ){
163194 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
163195 (const u8*)&pTabItem->colUsed, P4_INT64);
163198 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
163200 if( pLoop->wsFlags & WHERE_INDEXED ){
163201 Index *pIx = pLoop->u.btree.pIndex;
163205 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
163209 /* This is one term of an OR-optimization using the PRIMARY KEY of a
163211 iIndexCur = pLevel->iTabCur;
163213 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
163214 Index *pJ = pTabItem->pTab->pIndex;
163219 pJ = pJ->pNext;
163222 pWInfo->aiCurOnePass[1] = iIndexCur;
163227 iIndexCur = pParse->nTab++;
163228 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
163232 pLevel->iIdxCur = iIndexCur;
163234 assert( pIx->pSchema==pTab->pSchema );
163237 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
163239 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
163240 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
163241 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
163242 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
163243 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
163244 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
163248 VdbeComment((v, "%s", pIx->zName));
163253 for(ii=0; ii<pIx->nColumn; ii++){
163254 jj = pIx->aiColumn[ii];
163257 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
163267 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
163268 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
163270 WhereRightJoin *pRJ = pLevel->pRJ;
163271 pRJ->iMatch = pParse->nTab++;
163272 pRJ->regBloom = ++pParse->nMem;
163273 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
163274 pRJ->regReturn = ++pParse->nMem;
163275 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
163276 assert( pTab==pTabItem->pTab );
163279 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
163280 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
163282 pInfo->aColl[0] = 0;
163283 pInfo->aSortFlags[0] = 0;
163288 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
163291 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
163295 pWInfo->nOBSat = 0;
163296 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
163299 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
163300 if( db->mallocFailed ) goto whereBeginError;
163303 ** loop below generates code for a single nested loop of the VM
163310 if( pParse->nErr ) goto whereBeginError;
163311 pLevel = &pWInfo->a[ii];
163312 wsFlags = pLevel->pWLoop->wsFlags;
163313 pSrc = &pTabList->a[pLevel->iFrom];
163314 if( pSrc->fg.isMaterialized ){
163315 if( pSrc->fg.isCorrelated ){
163316 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
163319 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
163326 constructAutomaticIndex(pParse, &pWInfo->sWC,
163327 &pTabList->a[pLevel->iFrom], notReady, pLevel);
163332 if( db->mallocFailed ) goto whereBeginError;
163337 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
163339 pWInfo->iContinue = pLevel->addrCont;
163346 VdbeModuleComment((v, "Begin WHERE-core"));
163347 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
163353 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
163366 # define OpcodeRewriteTrace(D,K,P) /* no-op */
163374 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
163386 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
163387 if( pOp->p1!=iCur ) continue;
163388 if( pOp->opcode==OP_Close ) return 0;
163389 if( pOp->opcode==OP_OpenRead ) return 1;
163390 if( pOp->opcode==OP_OpenWrite ) return 1;
163391 if( pOp->opcode==OP_OpenDup ) return 1;
163392 if( pOp->opcode==OP_OpenAutoindex ) return 1;
163393 if( pOp->opcode==OP_OpenEphemeral ) return 1;
163404 Parse *pParse = pWInfo->pParse;
163405 Vdbe *v = pParse->pVdbe;
163409 SrcList *pTabList = pWInfo->pTabList;
163410 sqlite3 *db = pParse->db;
163416 VdbeModuleComment((v, "End WHERE-core"));
163417 for(i=pWInfo->nLevel-1; i>=0; i--){
163419 pLevel = &pWInfo->a[i];
163420 if( pLevel->pRJ ){
163423 WhereRightJoin *pRJ = pLevel->pRJ;
163424 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
163425 pLevel->addrCont = 0;
163426 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
163427 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
163431 pLoop = pLevel->pWLoop;
163432 if( pLevel->op!=OP_Noop ){
163437 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
163438 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
163439 && (pLoop->wsFlags & WHERE_INDEXED)!=0
163440 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
163441 && (n = pLoop->u.btree.nDistinctCol)>0
163442 && pIdx->aiRowLogEst[n]>=36
163444 int r1 = pParse->nMem+1;
163447 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
163449 pParse->nMem += n+1;
163450 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
163451 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
163454 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
163458 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
163459 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
163460 sqlite3VdbeChangeP5(v, pLevel->p5);
163462 VdbeCoverageIf(v, pLevel->op==OP_Next);
163463 VdbeCoverageIf(v, pLevel->op==OP_Prev);
163464 VdbeCoverageIf(v, pLevel->op==OP_VNext);
163465 if( pLevel->regBignull ){
163466 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
163467 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
163473 }else if( pLevel->addrCont ){
163474 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
163476 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
163479 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
163480 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
163481 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
163482 || pParse->db->mallocFailed );
163483 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
163484 if( pIn->eEndLoopOp!=OP_Noop ){
163485 if( pIn->nPrefix ){
163487 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
163488 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
163489 if( pLevel->iLeftJoin ){
163490 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
163495 ** return the null-row. So, if the cursor is not open yet,
163498 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
163503 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
163505 pIn->iBase, pIn->nPrefix);
163511 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
163514 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
163516 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
163517 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
163519 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
163522 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
163523 if( pLevel->pRJ ){
163524 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
163527 if( pLevel->addrSkip ){
163528 sqlite3VdbeGoto(v, pLevel->addrSkip);
163529 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
163530 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
163531 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
163534 if( pLevel->addrLikeRep ){
163535 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
163536 pLevel->addrLikeRep);
163540 if( pLevel->iLeftJoin ){
163541 int ws = pLoop->wsFlags;
163542 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
163545 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
163546 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
163549 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
163552 Index *pIx = pLevel->u.pCoveringIdx;
163553 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
163554 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
163557 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
163559 if( pLevel->op==OP_Return ){
163560 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
163562 sqlite3VdbeGoto(v, pLevel->addrFirst);
163566 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
163567 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
163570 assert( pWInfo->nLevel<=pTabList->nSrc );
163571 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
163575 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
163576 Table *pTab = pTabItem->pTab;
163578 pLoop = pLevel->pWLoop;
163584 if( pLevel->pRJ ){
163589 /* For a co-routine, change all OP_Column references to the table of
163590 ** the co-routine into OP_Copy of result contained in a register.
163593 if( pTabItem->fg.viaCoroutine ){
163594 testcase( pParse->db->mallocFailed );
163595 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
163596 pTabItem->regResult, 0);
163611 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
163612 pIdx = pLoop->u.btree.pIndex;
163613 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
163614 pIdx = pLevel->u.pCoveringIdx;
163617 && !db->mallocFailed
163619 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
163622 last = pWInfo->iEndWhere;
163624 if( pIdx->bHasExpr ){
163625 IndexedExpr *p = pParse->pIdxExpr;
163627 if( p->iIdxCur==pLevel->iIdxCur ){
163628 p->iDataCur = -1;
163629 p->iIdxCur = -1;
163631 p = p->pIENext;
163634 k = pLevel->addrBody + 1;
163636 if( db->flags & SQLITE_VdbeAddopTrace ){
163637 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
163640 pOp = sqlite3VdbeGetOp(v, k - 1);
163641 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
163642 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
163643 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
163646 pLastOp = pOp + (last - k);
163649 if( pOp->p1!=pLevel->iTabCur ){
163650 /* no-op */
163651 }else if( pOp->opcode==OP_Column
163653 || pOp->opcode==OP_Offset
163656 int x = pOp->p2;
163657 assert( pIdx->pTable==pTab );
163659 if( pOp->opcode==OP_Offset ){
163665 x = pPk->aiColumn[x];
163673 pOp->p2 = x;
163674 pOp->p1 = pLevel->iIdxCur;
163678 ** reference. Verify that this is harmless - that the
163682 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
163683 || cursorIsOpen(v,pOp->p1,k)
163684 || pOp->opcode==OP_Offset
163687 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
163688 || cursorIsOpen(v,pOp->p1,k)
163692 }else if( pOp->opcode==OP_Rowid ){
163693 pOp->p1 = pLevel->iIdxCur;
163694 pOp->opcode = OP_IdxRowid;
163696 }else if( pOp->opcode==OP_IfNullRow ){
163697 pOp->p1 = pLevel->iIdxCur;
163705 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
163713 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
163717 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
163719 pParse->withinRJSubrtn -= nRJ;
163768 ** the sub-query.
163773 ** select-list and ORDER BY expressions in the parent query are
163774 ** selected by the sub-query. For the purposes of the transformation,
163806 ** sub-query, which is always implemented as a co-routine. It then calls
163810 ** sqlite3WindowCodeStep() generates VM code so that, for each row returned
163811 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
163812 ** When the sub-routine is invoked:
163814 ** * The results of all window-functions for the row are stored
163826 ** BUILT-IN WINDOW FUNCTIONS
163828 ** This implementation features the following built-in window functions:
163842 ** These are the same built-in window functions supported by Postgres.
163845 ** be implemented using an API, built-in window functions are much more
163848 ** As such, some built-in window functions use the same API as aggregate
163859 ** As well as some of the built-in window functions, aggregate window
163866 ** Implementation of built-in window function row_number(). Assumes that the
163897 ** Implementation of built-in window function dense_rank(). Assumes that
163909 if( p ) p->nStep = 1;
163917 if( p->nStep ){
163918 p->nValue++;
163919 p->nStep = 0;
163921 sqlite3_result_int64(pCtx, p->nValue);
163926 ** Implementation of built-in window function nth_value(). This
163927 ** implementation is used in "slow mode" only - when the EXCLUDE clause
163958 p->nStep++;
163959 if( iVal==p->nStep ){
163960 p->pValue = sqlite3_value_dup(apArg[0]);
163961 if( !p->pValue ){
163972 pCtx, "second argument to nth_value must be a positive integer", -1
163978 if( p && p->pValue ){
163979 sqlite3_result_value(pCtx, p->pValue);
163980 sqlite3_value_free(p->pValue);
163981 p->pValue = 0;
163994 if( p && p->pValue==0 ){
163995 p->pValue = sqlite3_value_dup(apArg[0]);
163996 if( !p->pValue ){
164006 if( p && p->pValue ){
164007 sqlite3_result_value(pCtx, p->pValue);
164008 sqlite3_value_free(p->pValue);
164009 p->pValue = 0;
164016 ** Implementation of built-in window function rank(). Assumes that
164029 p->nStep++;
164030 if( p->nValue==0 ){
164031 p->nValue = p->nStep;
164041 sqlite3_result_int64(pCtx, p->nValue);
164042 p->nValue = 0;
164047 ** Implementation of built-in window function percent_rank(). Assumes that
164062 p->nTotal++;
164074 p->nStep++;
164080 p->nValue = p->nStep;
164081 if( p->nTotal>1 ){
164082 double r = (double)p->nValue / (double)(p->nTotal-1);
164092 ** Implementation of built-in window function cume_dist(). Assumes that
164107 p->nTotal++;
164119 p->nStep++;
164125 double r = (double)(p->nStep) / (double)(p->nTotal);
164155 if( p->nTotal==0 ){
164156 p->nParam = sqlite3_value_int64(apArg[0]);
164157 if( p->nParam<=0 ){
164159 pCtx, "argument of ntile must be a positive integer", -1
164163 p->nTotal++;
164175 p->iRow++;
164180 if( p && p->nParam>0 ){
164181 int nSize = (p->nTotal / p->nParam);
164183 sqlite3_result_int64(pCtx, p->iRow+1);
164185 i64 nLarge = p->nTotal - p->nParam*nSize;
164187 i64 iRow = p->iRow;
164189 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
164194 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
164221 sqlite3_value_free(p->pVal);
164222 p->pVal = sqlite3_value_dup(apArg[0]);
164223 if( p->pVal==0 ){
164226 p->nVal++;
164240 p->nVal--;
164241 if( p->nVal==0 ){
164242 sqlite3_value_free(p->pVal);
164243 p->pVal = 0;
164250 if( p && p->pVal ){
164251 sqlite3_result_value(pCtx, p->pVal);
164257 if( p && p->pVal ){
164258 sqlite3_result_value(pCtx, p->pVal);
164259 sqlite3_value_free(p->pVal);
164260 p->pVal = 0;
164265 ** Static names for the built-in window function names. These static
164270 ** if( pFuncDef->zName==row_valueName ){ ... }
164285 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
164286 ** for built-in window functions that never call those interfaces.
164303 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
164314 ** no-op routines for their methods */
164332 ** Register those built-in window functions that are not also aggregates.
164357 for(p=pList; p; p=p->pNextWin){
164358 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
164379 ** * If the function is a built-in window function that requires the
164380 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
164389 if( pWin->zName && pWin->eFrmType==0 ){
164390 Window *p = windowFind(pParse, pList, pWin->zName);
164392 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
164393 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
164394 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
164395 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
164396 pWin->eStart = p->eStart;
164397 pWin->eEnd = p->eEnd;
164398 pWin->eFrmType = p->eFrmType;
164399 pWin->eExclude = p->eExclude;
164403 if( (pWin->eFrmType==TK_RANGE)
164404 && (pWin->pStart || pWin->pEnd)
164405 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
164411 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
164412 sqlite3 *db = pParse->db;
164413 if( pWin->pFilter ){
164435 if( pFunc->zName==aUp[i].zFunc ){
164436 sqlite3ExprDelete(db, pWin->pStart);
164437 sqlite3ExprDelete(db, pWin->pEnd);
164438 pWin->pEnd = pWin->pStart = 0;
164439 pWin->eFrmType = aUp[i].eFrmType;
164440 pWin->eStart = aUp[i].eStart;
164441 pWin->eEnd = aUp[i].eEnd;
164442 pWin->eExclude = 0;
164443 if( pWin->eStart==TK_FOLLOWING ){
164444 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
164451 pWin->pWFunc = pFunc;
164464 Select *pSubSelect; /* Current sub-select, if any */
164469 ** this function appends to the output expression-list and updates
164473 struct WindowRewrite *p = pWalker->u.pRewrite;
164474 Parse *pParse = pWalker->pParse;
164476 assert( p->pWin!=0 );
164478 /* If this function is being called from within a scalar sub-select
164482 ** to the scalar sub-select. */
164483 if( p->pSubSelect ){
164484 if( pExpr->op!=TK_COLUMN ){
164487 int nSrc = p->pSrc->nSrc;
164490 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
164496 switch( pExpr->op ){
164503 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
164504 if( pExpr->y.pWin==pWin ){
164505 assert( pWin->pOwner==pExpr );
164514 int iCol = -1;
164515 if( pParse->db->mallocFailed ) return WRC_Abort;
164516 if( p->pSub ){
164518 for(i=0; i<p->pSub->nExpr; i++){
164519 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
164526 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
164527 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
164528 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
164530 if( p->pSub ){
164531 int f = pExpr->flags & EP_Collate;
164534 sqlite3ExprDelete(pParse->db, pExpr);
164538 pExpr->op = TK_COLUMN;
164539 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
164540 pExpr->iTable = p->pWin->iEphCsr;
164541 pExpr->y.pTab = p->pTab;
164542 pExpr->flags = f;
164544 if( pParse->db->mallocFailed ) return WRC_Abort;
164548 default: /* no-op */
164555 struct WindowRewrite *p = pWalker->u.pRewrite;
164556 Select *pSave = p->pSubSelect;
164560 p->pSubSelect = pSelect;
164562 p->pSubSelect = pSave;
164569 ** Iterate through each expression in expression-list pEList. For each:
164576 ** Append the node to output expression-list (*ppSub). And replace it
164577 ** with a TK_COLUMN that reads the (N-1)th element of table
164578 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
164587 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
164612 ** Append a copy of each expression in expression-list pAppend to
164623 int nInit = pList ? pList->nExpr : 0;
164624 for(i=0; i<pAppend->nExpr; i++){
164625 sqlite3 *db = pParse->db;
164626 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
164627 if( db->mallocFailed ){
164636 pSub->op = TK_NULL;
164637 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
164638 pSub->u.zToken = 0;
164642 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
164651 ** then we have to increase the Expr->op2 values of those nodes
164657 if( pExpr->op==TK_AGG_FUNCTION
164658 && pExpr->op2>=pWalker->walkerDepth
164660 pExpr->op2++;
164666 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
164668 sqlite3ErrorMsg(pWalker->pParse,
164669 "misuse of aggregate: %s()", pExpr->u.zToken);
164676 ** any SQL window functions, this function is a no-op. Otherwise, it
164683 if( p->pWin
164684 && p->pPrior==0
164685 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
164689 sqlite3 *db = pParse->db;
164691 SrcList *pSrc = p->pSrc;
164692 Expr *pWhere = p->pWhere;
164693 ExprList *pGroupBy = p->pGroupBy;
164694 Expr *pHaving = p->pHaving;
164697 ExprList *pSublist = 0; /* Expression list for sub-query */
164698 Window *pMWin = p->pWin; /* Main window object */
164703 u32 selFlags = p->selFlags;
164711 if( (p->selFlags & SF_Aggregate)==0 ){
164714 sqlite3WalkExprList(&w, p->pOrderBy);
164717 p->pSrc = 0;
164718 p->pWhere = 0;
164719 p->pGroupBy = 0;
164720 p->pHaving = 0;
164721 p->selFlags &= ~SF_Aggregate;
164722 p->selFlags |= SF_WinRewrite;
164724 /* Create the ORDER BY clause for the sub-select. This is the concatenation
164727 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
164728 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
164729 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
164730 int nSave = pSort->nExpr;
164731 pSort->nExpr = p->pOrderBy->nExpr;
164732 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
164733 sqlite3ExprListDelete(db, p->pOrderBy);
164734 p->pOrderBy = 0;
164736 pSort->nExpr = nSave;
164742 pMWin->iEphCsr = pParse->nTab++;
164743 pParse->nTab += 3;
164745 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
164746 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
164747 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
164750 ** sub-select expression list. They are required to figure out where
164752 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
164753 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
164756 ** sub-select expression list. Also allocate two registers for each
164757 ** window function - one for the accumulator, another for interim
164759 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164761 assert( ExprUseXList(pWin->pOwner) );
164762 assert( pWin->pWFunc!=0 );
164763 pArgs = pWin->pOwner->x.pList;
164764 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
164766 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
164767 pWin->bExprArgs = 1;
164769 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
164772 if( pWin->pFilter ){
164773 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
164776 pWin->regAccum = ++pParse->nMem;
164777 pWin->regResult = ++pParse->nMem;
164778 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
164797 ("New window-function subquery in FROM clause of (%u/%p)\n",
164798 p->selId, p));
164799 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
164800 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
164803 if( p->pSrc ){
164805 p->pSrc->a[0].pSelect = pSub;
164806 sqlite3SrcListAssignCursors(pParse, p->pSrc);
164807 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
164809 pSub->selFlags |= (selFlags & SF_Aggregate);
164812 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
164817 pTab->tabFlags |= TF_Ephemeral;
164818 p->pSrc->a[0].pTab = pTab;
164829 if( db->mallocFailed ) rc = SQLITE_NOMEM;
164833 ** result-set or ORDER BY clause of the SELECT statement p. */
164837 assert( rc==SQLITE_OK || pParse->nErr!=0 );
164846 if( p->ppThis ){
164847 *p->ppThis = p->pNextWin;
164848 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
164849 p->ppThis = 0;
164859 sqlite3ExprDelete(db, p->pFilter);
164860 sqlite3ExprListDelete(db, p->pPartition);
164861 sqlite3ExprListDelete(db, p->pOrderBy);
164862 sqlite3ExprDelete(db, p->pEnd);
164863 sqlite3ExprDelete(db, p->pStart);
164864 sqlite3DbFree(db, p->zName);
164865 sqlite3DbFree(db, p->zBase);
164875 Window *pNext = p->pNextWin;
164883 ** value should be a non-negative integer. If the value is not a
164884 ** constant, change it to NULL. The fact that it is then a non-negative
164891 sqlite3ExprDelete(pParse->db, pExpr);
164892 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
164947 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
164949 pWin->eFrmType = eType;
164950 pWin->eStart = eStart;
164951 pWin->eEnd = eEnd;
164952 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
164955 pWin->eExclude = eExclude;
164956 pWin->bImplicitFrame = bImplicitFrame;
164957 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
164958 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
164962 sqlite3ExprDelete(pParse->db, pEnd);
164963 sqlite3ExprDelete(pParse->db, pStart);
164969 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
164970 ** equivalent nul-terminated string.
164980 pWin->pPartition = pPartition;
164981 pWin->pOrderBy = pOrderBy;
164983 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
164986 sqlite3ExprListDelete(pParse->db, pPartition);
164987 sqlite3ExprListDelete(pParse->db, pOrderBy);
164995 ** stored in the linked list starting at pWin->pNextWin. This function
165000 if( pWin->zBase ){
165001 sqlite3 *db = pParse->db;
165002 Window *pExist = windowFind(pParse, pList, pWin->zBase);
165006 if( pWin->pPartition ){
165008 }else if( pExist->pOrderBy && pWin->pOrderBy ){
165010 }else if( pExist->bImplicitFrame==0 ){
165015 "cannot override %s of window: %s", zErr, pWin->zBase
165018 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
165019 if( pExist->pOrderBy ){
165020 assert( pWin->pOrderBy==0 );
165021 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
165023 sqlite3DbFree(db, pWin->zBase);
165024 pWin->zBase = 0;
165035 assert( p->op==TK_FUNCTION );
165037 p->y.pWin = pWin;
165039 pWin->pOwner = p;
165040 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
165046 sqlite3WindowDelete(pParse->db, pWin);
165051 ** Possibly link window pWin into the list at pSel->pWin (window functions
165058 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
165059 pWin->pNextWin = pSel->pWin;
165060 if( pSel->pWin ){
165061 pSel->pWin->ppThis = &pWin->pNextWin;
165063 pSel->pWin = pWin;
165064 pWin->ppThis = &pSel->pWin;
165066 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
165067 pSel->selFlags |= SF_MultiPart;
165086 if( p1->eFrmType!=p2->eFrmType ) return 1;
165087 if( p1->eStart!=p2->eStart ) return 1;
165088 if( p1->eEnd!=p2->eEnd ) return 1;
165089 if( p1->eExclude!=p2->eExclude ) return 1;
165090 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
165091 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
165092 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
165095 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
165099 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
165109 ** to begin iterating through the sub-query results. It is used to allocate
165113 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
165114 Window *pMWin = pSelect->pWin;
165118 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
165119 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
165120 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
165121 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
165125 if( pMWin->pPartition ){
165126 int nExpr = pMWin->pPartition->nExpr;
165127 pMWin->regPart = pParse->nMem+1;
165128 pParse->nMem += nExpr;
165129 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
165132 pMWin->regOne = ++pParse->nMem;
165133 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
165135 if( pMWin->eExclude ){
165136 pMWin->regStartRowid = ++pParse->nMem;
165137 pMWin->regEndRowid = ++pParse->nMem;
165138 pMWin->csrApp = pParse->nTab++;
165139 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
165140 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
165141 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
165145 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165146 FuncDef *p = pWin->pWFunc;
165147 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
165157 assert( ExprUseXList(pWin->pOwner) );
165158 pList = pWin->pOwner->x.pList;
165160 pWin->csrApp = pParse->nTab++;
165161 pWin->regApp = pParse->nMem+1;
165162 pParse->nMem += 3;
165163 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
165164 assert( pKeyInfo->aSortFlags[0]==0 );
165165 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
165167 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
165169 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
165171 else if( p->zName==nth_valueName || p->zName==first_valueName ){
165172 /* Allocate two registers at pWin->regApp. These will be used to
165174 pWin->regApp = pParse->nMem+1;
165175 pWin->csrApp = pParse->nTab++;
165176 pParse->nMem += 2;
165177 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
165179 else if( p->zName==leadName || p->zName==lagName ){
165180 pWin->csrApp = pParse->nTab++;
165181 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
165195 ** evaluated and the result left in register reg. This function generates VM
165196 ** code to check that the value is a non-negative integer and throws an
165201 "frame starting offset must be a non-negative integer",
165202 "frame ending offset must be a non-negative integer",
165204 "frame starting offset must be a non-negative number",
165205 "frame ending offset must be a non-negative number",
165243 ** Return the number of arguments passed to the window-function associated
165248 assert( ExprUseXList(pWin->pOwner) );
165249 pList = pWin->pOwner->x.pList;
165250 return (pList ? pList->nExpr : 0);
165273 ** in an array of accumulator registers - one for each window function
165280 ** order to reduce memory requirements - it would always be safe just
165295 ** Consider a window-frame similar to the following:
165315 ** Depending on the window-frame in question, all three cursors may not
165335 ** Generate VM code to read the window frames peer values from cursor csr into
165343 Window *pMWin = p->pMWin;
165344 ExprList *pOrderBy = pMWin->pOrderBy;
165346 Vdbe *v = sqlite3GetVdbe(p->pParse);
165347 ExprList *pPart = pMWin->pPartition;
165348 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
165350 for(i=0; i<pOrderBy->nExpr; i++){
165357 ** Generate VM code to invoke either xStep() (if bInverse is 0) or
165358 ** xInverse (if bInverse is non-zero) for each window function in the
165359 ** linked list starting at pMWin. Or, for built-in window functions
165361 ** inline VM code.
165370 ** already populated with all columns from the current row of the sub-query.
165372 ** If argument regPartSize is non-zero, then it is a register containing the
165382 Parse *pParse = p->pParse;
165385 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165386 FuncDef *pFunc = pWin->pWFunc;
165388 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
165391 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
165398 if( i!=1 || pFunc->zName!=nth_valueName ){
165399 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
165401 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
165406 if( pMWin->regStartRowid==0
165407 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
165408 && (pWin->eStart!=TK_UNBOUNDED)
165413 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
165414 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
165415 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
165416 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
165418 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
165420 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
165421 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
165424 }else if( pWin->regApp ){
165425 assert( pFunc->zName==nth_valueName
165426 || pFunc->zName==first_valueName
165429 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
165430 }else if( pFunc->xSFunc!=noopStepFunc ){
165432 if( pWin->pFilter ){
165434 assert( ExprUseXList(pWin->pOwner) );
165435 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
165436 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
165438 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
165444 if( pWin->bExprArgs ){
165448 assert( ExprUseXList(pWin->pOwner) );
165449 nArg = pWin->pOwner->x.pList->nExpr;
165451 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
165455 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
165456 pOp->p1 = csr;
165460 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
165463 assert( ExprUseXList(pWin->pOwner) );
165464 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
165468 bInverse, regArg, pWin->regAccum);
165471 if( pWin->bExprArgs ){
165487 ** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
165489 ** pMWin. Or, for built-in window-functions that do not use the standard
165490 ** API, generate the equivalent VM code.
165493 Parse *pParse = p->pParse;
165494 Window *pMWin = p->pMWin;
165498 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165499 if( pMWin->regStartRowid==0
165500 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
165501 && (pWin->eStart!=TK_UNBOUNDED)
165503 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
165504 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
165506 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
165507 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
165508 }else if( pWin->regApp ){
165509 assert( pMWin->regStartRowid==0 );
165513 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
165514 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
165515 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
165516 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
165518 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
165519 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
165527 ** p->pMWin list by doing a full scan of the current window frame. Store the
165533 Parse *pParse = p->pParse;
165534 Window *pMWin = p->pMWin;
165535 Vdbe *v = p->pVdbe;
165551 csr = pMWin->csrApp;
165552 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
165564 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
165565 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
165567 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165568 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
165571 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
165575 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
165578 if( pMWin->eExclude==TK_CURRENT ){
165581 }else if( pMWin->eExclude!=TK_NO ){
165586 if( pMWin->pOrderBy ){
165587 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
165589 if( pMWin->eExclude==TK_TIES ){
165606 windowAggStep(p, pMWin, csr, 0, p->regArg);
165611 sqlite3VdbeJumpHere(v, addrNext-1);
165625 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
165628 ** OP_Gosub instruction is all that this routine generates. Extra VM code
165629 ** for per-row processing is only generated for the following built-in window
165638 Window *pMWin = p->pMWin;
165639 Vdbe *v = p->pVdbe;
165641 if( pMWin->regStartRowid ){
165644 Parse *pParse = p->pParse;
165647 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165648 FuncDef *pFunc = pWin->pWFunc;
165649 assert( ExprUseXList(pWin->pOwner) );
165650 if( pFunc->zName==nth_valueName
165651 || pFunc->zName==first_valueName
165653 int csr = pWin->csrApp;
165656 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
165658 if( pFunc->zName==nth_valueName ){
165659 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
165664 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
165665 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
165669 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
165673 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
165674 int nArg = pWin->pOwner->x.pList->nExpr;
165675 int csr = pWin->csrApp;
165678 int iEph = pMWin->iEphCsr;
165681 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
165683 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
165687 int val = (pFunc->zName==leadName ? 1 : -1);
165690 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
165692 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
165699 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
165705 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
165711 ** any equivalent initialization required by any built-in window functions
165719 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165720 FuncDef *pFunc = pWin->pWFunc;
165721 assert( pWin->regAccum );
165722 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
165724 if( pMWin->regStartRowid==0 ){
165725 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
165726 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
165727 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
165730 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
165731 assert( pWin->eStart!=TK_UNBOUNDED );
165732 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
165733 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
165737 regArg = pParse->nMem+1;
165738 pParse->nMem += nArg;
165748 if( pMWin->regStartRowid ) return 1;
165749 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165750 FuncDef *pFunc = pWin->pWFunc;
165751 if( (pFunc->zName==nth_valueName)
165752 || (pFunc->zName==first_valueName)
165753 || (pFunc->zName==leadName)
165754 || (pFunc->zName==lagName)
165764 ** pOrderBy->nExpr. This function generates code to compare the two
165781 int nVal = pOrderBy->nExpr;
165789 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
165796 ** This function is called as part of generating VM programs for RANGE
165804 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
165806 ** If the sort-order for the ORDER BY term in the window is DESC, then the
165808 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
165812 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
165822 int regVal, /* Register containing non-negative number */
165826 Parse *pParse = p->pParse;
165828 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
165831 int regString = ++pParse->nMem; /* Reg. for constant value '' */
165837 /* Read the peer-value from each cursor into a register */
165842 assert( pOrderBy && pOrderBy->nExpr==1 );
165843 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
165853 reg1, (arith==OP_Add ? "+" : "-"), regVal,
165876 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
165891 default: assert( op==OP_Lt ); /* no-op */ break;
165902 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
165905 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
165908 ** reg1 = reg1 +/- regVal
165911 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
165928 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
165946 ** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
165957 Parse *pParse = p->pParse;
165958 Window *pMWin = p->pMWin;
165960 Vdbe *v = p->pVdbe;
165962 int bPeer = (pMWin->eFrmType!=TK_ROWS);
165967 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
165969 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
165975 if( pMWin->eFrmType==TK_RANGE ){
165979 if( pMWin->eStart==TK_FOLLOWING ){
165981 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
165985 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
165990 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
165999 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
166010 if( pMWin->eStart==pMWin->eEnd && regCountdown
166011 && pMWin->eFrmType==TK_RANGE
166016 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
166017 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
166020 }else if( p->regRowid ){
166021 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
166022 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
166027 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
166032 csr = p->current.csr;
166033 reg = p->current.reg;
166038 csr = p->start.csr;
166039 reg = p->start.reg;
166040 if( pMWin->regStartRowid ){
166041 assert( pMWin->regEndRowid );
166042 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
166044 windowAggStep(p, pMWin, csr, 1, p->regArg);
166050 csr = p->end.csr;
166051 reg = p->end.reg;
166052 if( pMWin->regStartRowid ){
166053 assert( pMWin->regEndRowid );
166054 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
166056 windowAggStep(p, pMWin, csr, 0, p->regArg);
166061 if( op==p->eDelete ){
166079 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
166082 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
166104 pNew->zName = sqlite3DbStrDup(db, p->zName);
166105 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
166106 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
166107 pNew->pWFunc = p->pWFunc;
166108 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
166109 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
166110 pNew->eFrmType = p->eFrmType;
166111 pNew->eEnd = p->eEnd;
166112 pNew->eStart = p->eStart;
166113 pNew->eExclude = p->eExclude;
166114 pNew->regResult = p->regResult;
166115 pNew->regAccum = p->regAccum;
166116 pNew->iArgCol = p->iArgCol;
166117 pNew->iEphCsr = p->iEphCsr;
166118 pNew->bExprArgs = p->bExprArgs;
166119 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
166120 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
166121 pNew->pOwner = pOwner;
166122 pNew->bImplicitFrame = p->bImplicitFrame;
166137 for(pWin=p; pWin; pWin=pWin->pNextWin){
166140 pp = &((*pp)->pNextWin);
166156 sqlite3 *db = pParse->db;
166158 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
166170 ** and invoke the sub-routine at instruction addrGosub once for each row.
166200 ** if( (regEnd--)<=0 ){
166202 ** if( (regStart--)<=0 ){
166213 ** if( (regStart--)<=0 ){
166219 ** The pseudo-code above uses the following shorthand:
166234 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
166253 ** if( (regEnd--)<=0 ){
166257 ** if( (regStart--)<=0 ){
166263 ** if( (regEnd--)<=0 ){
166279 ** regStart = regEnd - <expr1>
166282 ** if( (regEnd--)<=0 ){
166285 ** if( (regStart--)<=0 ){
166293 ** if( (regEnd--)<=0 ){
166297 ** if( (regStart--)<=0 ){
166309 ** This is optimized of course - branches that will never be taken and
166310 ** conditions that are always true are omitted from the VM code. The only
166330 ** if( (regStart--)<=0 ){
166346 ** To handle this case, the pseudo-code programs depicted above are modified
166366 ** of the outer loop - the one started by sqlite3WhereBegin().
166369 ** ROWS. The VM code is modified slightly so that:
166508 Window *pMWin = p->pWin;
166509 ExprList *pOrderBy = pMWin->pOrderBy;
166512 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
166513 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
166524 WindowCodeArg s; /* Context object for sub-routines */
166529 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
166530 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
166532 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
166533 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
166535 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
166536 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
166537 || pMWin->eExclude==TK_NO
166549 s.current.csr = pMWin->iEphCsr;
166555 ** are four options - they may never be deleted (eDelete==0), they may
166560 switch( pMWin->eStart ){
166562 if( pMWin->eFrmType!=TK_RANGE
166563 && windowExprGtZero(pParse, pMWin->pStart)
166570 if( pMWin->eEnd==TK_PRECEDING ){
166571 if( pMWin->eFrmType!=TK_RANGE
166572 && windowExprGtZero(pParse, pMWin->pEnd)
166586 /* Allocate registers for the array of values from the sub-query, the
166589 regNew = pParse->nMem+1;
166590 pParse->nMem += nInput;
166591 regRecord = ++pParse->nMem;
166592 s.regRowid = ++pParse->nMem;
166597 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
166598 regStart = ++pParse->nMem;
166600 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
166601 regEnd = ++pParse->nMem;
166607 if( pMWin->eFrmType!=TK_ROWS ){
166608 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
166609 regNewPeer = regNew + pMWin->nBufferCol;
166610 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
166611 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
166612 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
166613 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
166614 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
166617 /* Load the column values for the row returned by the sub-select
166627 ** VM code to check if the input row is the start of a new partition.
166630 if( pMWin->pPartition ){
166632 ExprList *pPart = pMWin->pPartition;
166633 int nPart = pPart->nExpr;
166634 int regNewPart = regNew + pMWin->nBufferCol;
166637 regFlushPart = ++pParse->nMem;
166638 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
166644 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
166650 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
166657 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
166658 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
166661 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
166662 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
166665 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
166666 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
166678 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
166679 assert( pMWin->eEnd==TK_FOLLOWING );
166683 if( pMWin->eStart!=TK_UNBOUNDED ){
166692 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
166693 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
166694 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
166695 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
166706 if( pMWin->eStart==TK_FOLLOWING ){
166708 if( pMWin->eEnd!=TK_UNBOUNDED ){
166709 if( pMWin->eFrmType==TK_RANGE ){
166723 if( pMWin->eEnd==TK_PRECEDING ){
166724 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
166732 if( pMWin->eEnd!=TK_UNBOUNDED ){
166733 if( pMWin->eFrmType==TK_RANGE ){
166763 if( pMWin->pPartition ){
166771 if( pMWin->eEnd==TK_PRECEDING ){
166772 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
166776 }else if( pMWin->eStart==TK_FOLLOWING ){
166782 if( pMWin->eFrmType==TK_RANGE ){
166787 if( pMWin->eEnd==TK_UNBOUNDED ){
166792 assert( pMWin->eEnd==TK_FOLLOWING );
166817 if( pMWin->pPartition ){
166818 if( pMWin->regStartRowid ){
166819 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
166820 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
166834 ** 2001-09-15
166848 ** That input file is processed by Lemon to generate a C-language
166850 ** this comment as part of the translated C-code. Edits should be made
166857 ** Disable all error recovery processing in the parser push-down
166910 sqlite3 *db = pParse->db;
166911 pParse->disableLookaside++;
166931 sqlite3ExprListDelete(pParse->db, pOrderBy);
166932 sqlite3ExprDelete(pParse->db, pLimit);
166938 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
166944 if( p->pPrior ){
166948 pLoop->pNext = pNext;
166949 pLoop->selFlags |= SF_Compound;
166951 pLoop = pLoop->pPrior;
166954 if( pLoop->pOrderBy || pLoop->pLimit ){
166956 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
166957 sqlite3SelectOpName(pNext->op));
166961 if( (p->selFlags & SF_MultiValue)==0 &&
166962 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
166975 pSelect->pWith = pWith;
166978 sqlite3WithDelete(pParse->db, pWith);
166986 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
166989 p->op = (u8)op;
166990 p->affExpr = 0;
166991 p->flags = EP_Leaf;
166993 /* p->iAgg = -1; // Not required */
166994 p->pLeft = p->pRight = 0;
166995 p->pAggInfo = 0;
166996 memset(&p->x, 0, sizeof(p->x));
166997 memset(&p->y, 0, sizeof(p->y));
166998 p->op2 = 0;
166999 p->iTable = 0;
167000 p->iColumn = 0;
167001 p->u.zToken = (char*)&p[1];
167002 memcpy(p->u.zToken, t.z, t.n);
167003 p->u.zToken[t.n] = 0;
167004 p->w.iOfst = (int)(t.z - pParse->zTail);
167005 if( sqlite3Isquote(p->u.zToken[0]) ){
167009 p->nHeight = 1;
167022 sqlite3 *db = pParse->db;
167023 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
167024 pA->op = (u8)op;
167025 sqlite3ExprDelete(db, pA->pRight);
167026 pA->pRight = 0;
167044 && pParse->db->init.busy==0
167047 pIdToken->n, pIdToken->z);
167250 ** that represent terminal and non-terminal symbols.
167256 ** (also known as: "terminal symbols") have fall-back
167260 ** YYACTIONTYPE is the data type used for "action codes" - numbers
167265 ** value associated with a terminal or non-terminal
167268 ** Each non-terminal can have a different minor type.
167290 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
167291 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
167294 ** YY_NO_ACTION The yy_action[] code for no-op
167340 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
167341 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
167358 /* Define the yytestcase() macro to be a no-op if is not already defined
167383 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
167392 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
167405 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
167417 ** shifting non-terminals after a reduce.
167929 #define YY_REDUCE_MIN (-271)
167932 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
167933 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
167934 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
167935 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
167936 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167937 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167938 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167939 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
167945 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167946 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
167947 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
167948 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
167949 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
167950 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
167953 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
167954 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
168034 /********** End of lemon-generated parsing tables *****************************/
168257 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
168260 YYMINORTYPE minor; /* The user-supplied minor token value. This
168270 int yyhwm; /* High-water mark of the stack */
169070 newSize = p->yystksz*2 + 100;
169071 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
169072 if( p->yystack==&p->yystk0 ){
169074 if( pNew ) pNew[0] = p->yystk0;
169076 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
169079 p->yystack = pNew;
169080 p->yytos = &p->yystack[idx];
169084 yyTracePrompt, p->yystksz, newSize);
169087 p->yystksz = newSize;
169108 yypParser->yyhwm = 0;
169111 yypParser->yytos = NULL;
169112 yypParser->yystack = NULL;
169113 yypParser->yystksz = 0;
169115 yypParser->yystack = &yypParser->yystk0;
169116 yypParser->yystksz = 1;
169120 yypParser->yyerrcnt = -1;
169122 yypParser->yytos = yypParser->yystack;
169123 yypParser->yystack[0].stateno = 0;
169124 yypParser->yystack[0].major = 0;
169126 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
169171 ** terminal or non-terminal is destroyed. This can happen
169186 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
169201 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
169218 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
169227 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
169232 sqlite3WithDelete(pParse->db, (yypminor->yy521));
169238 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
169244 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
169253 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
169259 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
169264 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
169271 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
169287 assert( pParser->yytos!=0 );
169288 assert( pParser->yytos > pParser->yystack );
169289 yytos = pParser->yytos--;
169294 yyTokenName[yytos->major]);
169297 yy_destructor(pParser, yytos->major, &yytos->minor);
169305 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
169307 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
169338 return pParser->yyhwm;
169344 ** is in state X and has a lookahead token Y. In a well-tested
169381 ** look-ahead token iLookAhead.
169384 YYCODETYPE iLookAhead, /* The look-ahead token */
169422 int j = i - iLookAhead + YYWILDCARD;
169445 ** Find the appropriate action for a parser given the non-terminal
169446 ** look-ahead token iLookAhead.
169450 YYCODETYPE iLookAhead /* The look-ahead token */
169485 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
169504 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
169508 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
169509 yyNewState - YY_MIN_REDUCE);
169527 yypParser->yytos++;
169529 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
169530 yypParser->yyhwm++;
169531 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
169535 if( yypParser->yytos>yypParser->yystackEnd ){
169536 yypParser->yytos--;
169541 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
169543 yypParser->yytos--;
169550 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
169552 yytos = yypParser->yytos;
169553 yytos->stateno = yyNewState;
169554 yytos->major = yyMajor;
169555 yytos->minor.yy0 = yyMinor;
169559 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
169970 ** of symbols on the right-hand side of that rule. */
169972 -1, /* (0) explain ::= EXPLAIN */
169973 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
169974 -1, /* (2) cmdx ::= cmd */
169975 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
169977 -1, /* (5) transtype ::= DEFERRED */
169978 -1, /* (6) transtype ::= IMMEDIATE */
169979 -1, /* (7) transtype ::= EXCLUSIVE */
169980 -2, /* (8) cmd ::= COMMIT|END trans_opt */
169981 -2, /* (9) cmd ::= ROLLBACK trans_opt */
169982 -2, /* (10) cmd ::= SAVEPOINT nm */
169983 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
169984 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
169985 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
169986 -1, /* (14) createkw ::= CREATE */
169988 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
169989 -1, /* (17) temp ::= TEMP */
169991 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
169992 -2, /* (20) create_table_args ::= AS select */
169994 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
169995 -2, /* (23) table_option ::= WITHOUT nm */
169996 -1, /* (24) table_option ::= nm */
169997 -2, /* (25) columnname ::= nm typetoken */
169999 -4, /* (27) typetoken ::= typename LP signed RP */
170000 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
170001 -2, /* (29) typename ::= typename ID|STRING */
170004 -2, /* (32) ccons ::= CONSTRAINT nm */
170005 -3, /* (33) ccons ::= DEFAULT scantok term */
170006 -4, /* (34) ccons ::= DEFAULT LP expr RP */
170007 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
170008 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
170009 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
170010 -3, /* (38) ccons ::= NOT NULL onconf */
170011 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
170012 -2, /* (40) ccons ::= UNIQUE onconf */
170013 -4, /* (41) ccons ::= CHECK LP expr RP */
170014 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
170015 -1, /* (43) ccons ::= defer_subclause */
170016 -2, /* (44) ccons ::= COLLATE ID|STRING */
170017 -3, /* (45) generated ::= LP expr RP */
170018 -4, /* (46) generated ::= LP expr RP ID */
170020 -1, /* (48) autoinc ::= AUTOINCR */
170022 -2, /* (50) refargs ::= refargs refarg */
170023 -2, /* (51) refarg ::= MATCH nm */
170024 -3, /* (52) refarg ::= ON INSERT refact */
170025 -3, /* (53) refarg ::= ON DELETE refact */
170026 -3, /* (54) refarg ::= ON UPDATE refact */
170027 -2, /* (55) refact ::= SET NULL */
170028 -2, /* (56) refact ::= SET DEFAULT */
170029 -1, /* (57) refact ::= CASCADE */
170030 -1, /* (58) refact ::= RESTRICT */
170031 -2, /* (59) refact ::= NO ACTION */
170032 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
170033 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
170035 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
170036 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
170038 -1, /* (66) tconscomma ::= COMMA */
170039 -2, /* (67) tcons ::= CONSTRAINT nm */
170040 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
170041 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
170042 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
170043-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
170046 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
170048 -2, /* (76) orconf ::= OR resolvetype */
170049 -1, /* (77) resolvetype ::= IGNORE */
170050 -1, /* (78) resolvetype ::= REPLACE */
170051 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
170052 -2, /* (80) ifexists ::= IF EXISTS */
170054 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
170055 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
170056 -1, /* (84) cmd ::= select */
170057 -3, /* (85) select ::= WITH wqlist selectnowith */
170058 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
170059 -1, /* (87) select ::= selectnowith */
170060 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
170061 -1, /* (89) multiselect_op ::= UNION */
170062 -2, /* (90) multiselect_op ::= UNION ALL */
170063 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
170064-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
170065-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
170066 -4, /* (94) values ::= VALUES LP nexprlist RP */
170067 -5, /* (95) values ::= values COMMA LP nexprlist RP */
170068 -1, /* (96) distinct ::= DISTINCT */
170069 -1, /* (97) distinct ::= ALL */
170072 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
170073 -3, /* (101) selcollist ::= sclp scanpt STAR */
170074 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
170075 -2, /* (103) as ::= AS nm */
170078 -2, /* (106) from ::= FROM seltablist */
170079 -2, /* (107) stl_prefix ::= seltablist joinop */
170081 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
170082 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
170083 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
170084 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
170085 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
170087 -2, /* (115) dbnm ::= DOT nm */
170088 -1, /* (116) fullname ::= nm */
170089 -3, /* (117) fullname ::= nm DOT nm */
170090 -1, /* (118) xfullname ::= nm */
170091 -3, /* (119) xfullname ::= nm DOT nm */
170092 -5, /* (120) xfullname ::= nm DOT nm AS nm */
170093 -3, /* (121) xfullname ::= nm AS nm */
170094 -1, /* (122) joinop ::= COMMA|JOIN */
170095 -2, /* (123) joinop ::= JOIN_KW JOIN */
170096 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
170097 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
170098 -2, /* (126) on_using ::= ON expr */
170099 -4, /* (127) on_using ::= USING LP idlist RP */
170102 -3, /* (130) indexed_by ::= INDEXED BY nm */
170103 -2, /* (131) indexed_by ::= NOT INDEXED */
170105 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
170106 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
170107 -3, /* (135) sortlist ::= expr sortorder nulls */
170108 -1, /* (136) sortorder ::= ASC */
170109 -1, /* (137) sortorder ::= DESC */
170111 -2, /* (139) nulls ::= NULLS FIRST */
170112 -2, /* (140) nulls ::= NULLS LAST */
170115 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
170117 -2, /* (145) having_opt ::= HAVING expr */
170119 -2, /* (147) limit_opt ::= LIMIT expr */
170120 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
170121 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
170122 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
170124 -2, /* (152) where_opt ::= WHERE expr */
170126 -2, /* (154) where_opt_ret ::= WHERE expr */
170127 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
170128 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
170129 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
170130 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
170131 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
170132 -3, /* (160) setlist ::= nm EQ expr */
170133 -5, /* (161) setlist ::= LP idlist RP EQ expr */
170134 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
170135 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
170137 -2, /* (165) upsert ::= RETURNING selcollist */
170138-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
170139 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
170140 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
170141 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
170142 -2, /* (170) returning ::= RETURNING selcollist */
170143 -2, /* (171) insert_cmd ::= INSERT orconf */
170144 -1, /* (172) insert_cmd ::= REPLACE */
170146 -3, /* (174) idlist_opt ::= LP idlist RP */
170147 -3, /* (175) idlist ::= idlist COMMA nm */
170148 -1, /* (176) idlist ::= nm */
170149 -3, /* (177) expr ::= LP expr RP */
170150 -1, /* (178) expr ::= ID|INDEXED */
170151 -1, /* (179) expr ::= JOIN_KW */
170152 -3, /* (180) expr ::= nm DOT nm */
170153 -5, /* (181) expr ::= nm DOT nm DOT nm */
170154 -1, /* (182) term ::= NULL|FLOAT|BLOB */
170155 -1, /* (183) term ::= STRING */
170156 -1, /* (184) term ::= INTEGER */
170157 -1, /* (185) expr ::= VARIABLE */
170158 -3, /* (186) expr ::= expr COLLATE ID|STRING */
170159 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
170160 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
170161 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
170162 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
170163 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
170164 -1, /* (192) term ::= CTIME_KW */
170165 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
170166 -3, /* (194) expr ::= expr AND expr */
170167 -3, /* (195) expr ::= expr OR expr */
170168 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
170169 -3, /* (197) expr ::= expr EQ|NE expr */
170170 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
170171 -3, /* (199) expr ::= expr PLUS|MINUS expr */
170172 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
170173 -3, /* (201) expr ::= expr CONCAT expr */
170174 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
170175 -3, /* (203) expr ::= expr likeop expr */
170176 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
170177 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
170178 -3, /* (206) expr ::= expr NOT NULL */
170179 -3, /* (207) expr ::= expr IS expr */
170180 -4, /* (208) expr ::= expr IS NOT expr */
170181 -6, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
170182 -5, /* (210) expr ::= expr IS DISTINCT FROM expr */
170183 -2, /* (211) expr ::= NOT expr */
170184 -2, /* (212) expr ::= BITNOT expr */
170185 -2, /* (213) expr ::= PLUS|MINUS expr */
170186 -3, /* (214) expr ::= expr PTR expr */
170187 -1, /* (215) between_op ::= BETWEEN */
170188 -2, /* (216) between_op ::= NOT BETWEEN */
170189 -5, /* (217) expr ::= expr between_op expr AND expr */
170190 -1, /* (218) in_op ::= IN */
170191 -2, /* (219) in_op ::= NOT IN */
170192 -5, /* (220) expr ::= expr in_op LP exprlist RP */
170193 -3, /* (221) expr ::= LP select RP */
170194 -5, /* (222) expr ::= expr in_op LP select RP */
170195 -5, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
170196 -4, /* (224) expr ::= EXISTS LP select RP */
170197 -5, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
170198 -5, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
170199 -4, /* (227) case_exprlist ::= WHEN expr THEN expr */
170200 -2, /* (228) case_else ::= ELSE expr */
170202 -1, /* (230) case_operand ::= expr */
170205 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
170206 -1, /* (234) nexprlist ::= expr */
170208 -3, /* (236) paren_exprlist ::= LP exprlist RP */
170209-12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
170210 -1, /* (238) uniqueflag ::= UNIQUE */
170213 -3, /* (241) eidlist_opt ::= LP eidlist RP */
170214 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
170215 -3, /* (243) eidlist ::= nm collate sortorder */
170217 -2, /* (245) collate ::= COLLATE ID|STRING */
170218 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
170219 -2, /* (247) cmd ::= VACUUM vinto */
170220 -3, /* (248) cmd ::= VACUUM nm vinto */
170221 -2, /* (249) vinto ::= INTO expr */
170223 -3, /* (251) cmd ::= PRAGMA nm dbnm */
170224 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
170225 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
170226 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
170227 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
170228 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
170229 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
170230 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
170231-11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
170232 -1, /* (260) trigger_time ::= BEFORE|AFTER */
170233 -2, /* (261) trigger_time ::= INSTEAD OF */
170235 -1, /* (263) trigger_event ::= DELETE|INSERT */
170236 -1, /* (264) trigger_event ::= UPDATE */
170237 -3, /* (265) trigger_event ::= UPDATE OF idlist */
170239 -2, /* (267) when_clause ::= WHEN expr */
170240 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
170241 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
170242 -3, /* (270) trnm ::= nm DOT nm */
170243 -3, /* (271) tridxby ::= INDEXED BY nm */
170244 -2, /* (272) tridxby ::= NOT INDEXED */
170245 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
170246 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
170247 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
170248 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
170249 -4, /* (277) expr ::= RAISE LP IGNORE RP */
170250 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
170251 -1, /* (279) raisetype ::= ROLLBACK */
170252 -1, /* (280) raisetype ::= ABORT */
170253 -1, /* (281) raisetype ::= FAIL */
170254 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
170255 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
170256 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
170258 -2, /* (286) key_opt ::= KEY expr */
170259 -1, /* (287) cmd ::= REINDEX */
170260 -3, /* (288) cmd ::= REINDEX nm dbnm */
170261 -1, /* (289) cmd ::= ANALYZE */
170262 -3, /* (290) cmd ::= ANALYZE nm dbnm */
170263 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
170264 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
170265 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
170266 -1, /* (294) add_column_fullname ::= fullname */
170267 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
170268 -1, /* (296) cmd ::= create_vtab */
170269 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
170270 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
170272 -1, /* (300) vtabargtoken ::= ANY */
170273 -3, /* (301) vtabargtoken ::= lp anylist RP */
170274 -1, /* (302) lp ::= LP */
170275 -2, /* (303) with ::= WITH wqlist */
170276 -3, /* (304) with ::= WITH RECURSIVE wqlist */
170277 -1, /* (305) wqas ::= AS */
170278 -2, /* (306) wqas ::= AS MATERIALIZED */
170279 -3, /* (307) wqas ::= AS NOT MATERIALIZED */
170280 -6, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
170281 -1, /* (309) wqlist ::= wqitem */
170282 -3, /* (310) wqlist ::= wqlist COMMA wqitem */
170283 -1, /* (311) windowdefn_list ::= windowdefn */
170284 -3, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
170285 -5, /* (313) windowdefn ::= nm AS LP window RP */
170286 -5, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
170287 -6, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
170288 -4, /* (316) window ::= ORDER BY sortlist frame_opt */
170289 -5, /* (317) window ::= nm ORDER BY sortlist frame_opt */
170290 -1, /* (318) window ::= frame_opt */
170291 -2, /* (319) window ::= nm frame_opt */
170293 -3, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
170294-6, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
170295 -1, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
170296 -1, /* (324) frame_bound_s ::= frame_bound */
170297 -2, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
170298 -1, /* (326) frame_bound_e ::= frame_bound */
170299 -2, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
170300 -2, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
170301 -2, /* (329) frame_bound ::= CURRENT ROW */
170303 -2, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
170304 -2, /* (332) frame_exclude ::= NO OTHERS */
170305 -2, /* (333) frame_exclude ::= CURRENT ROW */
170306 -1, /* (334) frame_exclude ::= GROUP|TIES */
170307 -2, /* (335) window_clause ::= WINDOW windowdefn_list */
170308 -2, /* (336) filter_over ::= filter_clause over_clause */
170309 -1, /* (337) filter_over ::= over_clause */
170310 -1, /* (338) filter_over ::= filter_clause */
170311 -4, /* (339) over_clause ::= OVER LP window RP */
170312 -2, /* (340) over_clause ::= OVER nm */
170313 -5, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
170314 -1, /* (342) input ::= cmdlist */
170315 -2, /* (343) cmdlist ::= cmdlist ecmd */
170316 -1, /* (344) cmdlist ::= ecmd */
170317 -1, /* (345) ecmd ::= SEMI */
170318 -2, /* (346) ecmd ::= cmdx SEMI */
170319 -3, /* (347) ecmd ::= explain cmdx SEMI */
170321 -1, /* (349) trans_opt ::= TRANSACTION */
170322 -2, /* (350) trans_opt ::= TRANSACTION nm */
170323 -1, /* (351) savepoint_opt ::= SAVEPOINT */
170325 -2, /* (353) cmd ::= create_table create_table_args */
170326 -1, /* (354) table_option_set ::= table_option */
170327 -4, /* (355) columnlist ::= columnlist COMMA columnname carglist */
170328 -2, /* (356) columnlist ::= columnname carglist */
170329 -1, /* (357) nm ::= ID|INDEXED */
170330 -1, /* (358) nm ::= STRING */
170331 -1, /* (359) nm ::= JOIN_KW */
170332 -1, /* (360) typetoken ::= typename */
170333 -1, /* (361) typename ::= ID|STRING */
170334 -1, /* (362) signed ::= plus_num */
170335 -1, /* (363) signed ::= minus_num */
170336 -2, /* (364) carglist ::= carglist ccons */
170338 -2, /* (366) ccons ::= NULL onconf */
170339 -4, /* (367) ccons ::= GENERATED ALWAYS AS generated */
170340 -2, /* (368) ccons ::= AS generated */
170341 -2, /* (369) conslist_opt ::= COMMA conslist */
170342 -3, /* (370) conslist ::= conslist tconscomma tcons */
170343 -1, /* (371) conslist ::= tcons */
170345 -1, /* (373) defer_subclause_opt ::= defer_subclause */
170346 -1, /* (374) resolvetype ::= raisetype */
170347 -1, /* (375) selectnowith ::= oneselect */
170348 -1, /* (376) oneselect ::= values */
170349 -2, /* (377) sclp ::= selcollist COMMA */
170350 -1, /* (378) as ::= ID|STRING */
170351 -1, /* (379) indexed_opt ::= indexed_by */
170353 -1, /* (381) expr ::= term */
170354 -1, /* (382) likeop ::= LIKE_KW|MATCH */
170355 -1, /* (383) exprlist ::= nexprlist */
170356 -1, /* (384) nmnum ::= plus_num */
170357 -1, /* (385) nmnum ::= nm */
170358 -1, /* (386) nmnum ::= ON */
170359 -1, /* (387) nmnum ::= DELETE */
170360 -1, /* (388) nmnum ::= DEFAULT */
170361 -1, /* (389) plus_num ::= INTEGER|FLOAT */
170363 -3, /* (391) foreach_clause ::= FOR EACH ROW */
170364 -1, /* (392) trnm ::= nm */
170366 -1, /* (394) database_kw_opt ::= DATABASE */
170369 -1, /* (397) kwcolumn_opt ::= COLUMNKW */
170370 -1, /* (398) vtabarglist ::= vtabarg */
170371 -3, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
170372 -2, /* (400) vtabarg ::= vtabarg vtabargtoken */
170374 -4, /* (402) anylist ::= anylist LP anylist RP */
170375 -2, /* (403) anylist ::= anylist ANY */
170388 ** only called from one place, optimizing compilers will in-line it, which
170405 yymsp = yypParser->yytos;
170419 { pParse->explain = 1; }
170422 { pParse->explain = 2; }
170428 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
170437 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
170441 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
170460 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[
170477 {yymsp[-2].minor.yy394 = 1;}
170480 {yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
170484 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
170490 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
170497 {yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
170498 yymsp[-2].minor.yy285 = yylhsminor.yy285;
170503 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
170505 yymsp[-1].minor.yy285 = 0;
170522 {sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
170531 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
170536 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
170540 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
170556 {pParse->constraintName = yymsp[0].minor.yy0;}
170559 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
170562 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
170565 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
170570 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
170578 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
170587 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
170594 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
170597 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
170606 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
170609 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
170615 { yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
170618 { yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy2…
170621 { yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
170624 { yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
170627 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
170630 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
170633 { yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
170636 { yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
170639 { yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
170642 { yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
170645 { yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
170648 {yymsp[-2].minor.yy394 = 0;}
170653 {yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
170660 {yymsp[-1].minor.yy394 = 1;}
170663 {yymsp[-1].minor.yy394 = 0;}
170666 {pParse->constraintName.n = 0;}
170669 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
170672 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
170676 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
170680 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
170689 {yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
170700 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
170705 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
170710 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
170717 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
170721 {yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
170724 {yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
170732 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
170738 Select *pLhs = yymsp[-2].minor.yy47;
170739 if( pRhs && pRhs->pPrior ){
170748 pRhs->op = (u8)yymsp[-1].minor.yy394;
170749 pRhs->pPrior = pLhs;
170750 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
170751 pRhs->selFlags &= ~SF_MultiValue;
170752 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
170754 sqlite3SelectDelete(pParse->db, pLhs);
170756 yymsp[-2].minor.yy47 = pRhs;
170761 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
170764 {yymsp[-1].minor.yy394 = TK_ALL;}
170768-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].min…
170773-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].min…
170774 if( yymsp[-9].minor.yy47 ){
170775 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
170777 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
170783 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
170788 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
170789 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
170790 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
170792 pRight->op = TK_ALL;
170793 pRight->pPrior = pLeft;
170794 yymsp[-4].minor.yy47 = pRight;
170796 yymsp[-4].minor.yy47 = pLeft;
170816 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528…
170817 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
170818 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
170823 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
170824 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
170830 Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0);
170832 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
170839 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
170847 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
170848 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
170853 …if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0) ) yymsp[-1].minor.yy131->a[yyms…
170858 …yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor…
170863 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor…
170864 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
170869 …yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor…
170870 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
170875 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170880 …if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0…
170881 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
170882 }else if( yymsp[-3].minor.yy131->nSrc==1 ){
170883 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170884 if( yymsp[-5].minor.yy131 ){
170885 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
170886 SrcItem *pOld = yymsp[-3].minor.yy131->a;
170887 pNew->zName = pOld->zName;
170888 pNew->zDatabase = pOld->zDatabase;
170889 pNew->pSelect = pOld->pSelect;
170890 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
170891 pNew->fg.isNestedFrom = 1;
170893 if( pOld->fg.isTabFunc ){
170894 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
170895 pOld->u1.pFuncArg = 0;
170896 pOld->fg.isTabFunc = 0;
170897 pNew->fg.isTabFunc = 1;
170899 pOld->zName = pOld->zDatabase = 0;
170900 pOld->pSelect = 0;
170902 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
170905 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
170906 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom,0);
170907 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170918 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
170924 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
170925 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
170927 yymsp[-2].minor.yy131 = yylhsminor.yy131;
170930 {yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
170933 {yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
170937 …yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
170938 …if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
170943 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
170944 …if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
170951 {yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
170954 {yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
170957 {yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
170960 {yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
170963 {yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
170969 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
170972 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
170976 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
170980 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
170981 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
170986 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
170987 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
171001 {yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
171004 {yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
171020 {yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
171023 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,0);}
171026 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
171029 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
171033 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
171034 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
171038 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
171041 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
171045 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
171046 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
171047 if( yymsp[-1].minor.yy131 ){
171048 SrcList *pFromClause = yymsp[-1].minor.yy131;
171049 if( pFromClause->nSrc>1 ){
171057 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
171059 …sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].mi…
171064 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
171065 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
171070 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
171076 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
171078 yymsp[-2].minor.yy322 = yylhsminor.yy322;
171082 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
171087 …sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5…
171092 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
171099 { yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
171102 { yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,…
171105 { yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0…
171108 { yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
171111 { yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy5…
171120 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
171123 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
171126 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
171129 {yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
171133 {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
171137 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
171141 yymsp[-2].minor.yy528 = yylhsminor.yy528;
171145 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0);
171146 Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
171154 yymsp[-4].minor.yy528 = yylhsminor.yy528;
171158 {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
171162 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
171163 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
171176 ** in the virtual machine. #N is the N-th register. */
171177 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
171179 if( pParse->nested==0 ){
171184 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
171191 …yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.…
171196 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
171197 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
171202 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[
171204 yymsp[-4].minor.yy528 = yylhsminor.yy528;
171208 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
171210 yymsp[-3].minor.yy528 = yylhsminor.yy528;
171214 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[
171217 yymsp[-5].minor.yy528 = yylhsminor.yy528;
171221 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
171224 yymsp[-4].minor.yy528 = yylhsminor.yy528;
171234 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
171235 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
171236 if( yymsp[-4].minor.yy528 ){
171237 yymsp[-4].minor.yy528->x.pList = pList;
171238 if( ALWAYS(pList->nExpr) ){
171239 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
171242 sqlite3ExprListDelete(pParse->db, pList);
171247 {yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
171256 {yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy5…
171259 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
171264 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
171265 yymsp[-1].minor.yy0.n &= 0x7fffffff;
171267 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
171268 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
171269 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy528, 0);
171270 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
171276 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
171277 yymsp[-3].minor.yy0.n &= 0x7fffffff;
171278 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
171279 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
171281 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
171282 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy528, 0);
171283 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
171287 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
171290 {yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy528,0);}
171294 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
171295 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL);
171300 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
171301 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL);
171306 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
171307 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL);
171312 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
171313 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL);
171318 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwri…
171322 …yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
171323 /*A-overwrites-B*/
171328 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
171330 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
171332 yymsp[-2].minor.yy528 = yylhsminor.yy528;
171340 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
171342 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy528, 0);
171343 if( yymsp[-4].minor.yy528 ){
171344 yymsp[-4].minor.yy528->x.pList = pList;
171346 sqlite3ExprListDelete(pParse->db, pList);
171348 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
171353 if( yymsp[-1].minor.yy322==0 ){
171362 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
171363 …yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false…
171364 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
171366 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
171367 …if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=T…
171368 yymsp[-1].minor.yy322->a[0].pExpr = 0;
171369 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
171371 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy528, pRHS);
171373 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
171374 if( yymsp[-4].minor.yy528==0 ){
171375 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
171376 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
171377 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
171378 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
171381 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
171384 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
171385 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
171388 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
171394 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
171395 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
171400 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
171401 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
171402 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
171407 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
171410 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
171411 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
171412 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
171418 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
171419 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
171424 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy528, 0);
171425 if( yymsp[-4].minor.yy528 ){
171426 …yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
171427 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
171429 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
171430 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
171436 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
171437 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
171442 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
171443 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
171447 {yymsp[0].minor.yy528 = yymsp[0].minor.yy528; /*A-overwrites-X*/}
171450 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
171453 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
171457 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
171461 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
171462 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy3…
171463 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_…
171464 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
171465 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
171478 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
171483 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
171487 {sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
171493 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
171496 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
171499 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
171502 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
171505 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
171508 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
171513 all.z = yymsp[-3].minor.yy0.z;
171514 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
171515 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
171520-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].min…
171521 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
171525 { yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
171528 { yymsp[-1].minor.yy394 = TK_INSTEAD;}
171535 {yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
171538 {yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
171546 { yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
171550 assert( yymsp[-2].minor.yy33!=0 );
171551 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
171552 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
171557 assert( yymsp[-1].minor.yy33!=0 );
171558 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
171563 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
171584 …tep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy…
171585 yymsp[-8].minor.yy33 = yylhsminor.yy33;
171589-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy4…
171591 yymsp[-7].minor.yy33 = yylhsminor.yy33;
171594 …sminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[
171595 yymsp[-5].minor.yy33 = yylhsminor.yy33;
171598 …ggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*y…
171599 yymsp[-2].minor.yy33 = yylhsminor.yy33;
171603 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
171604 if( yymsp[-3].minor.yy528 ){
171605 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
171611 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
171612 if( yymsp[-5].minor.yy528 ) {
171613 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
171625 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
171630 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
171642 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
171648 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
171652 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
171657 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
171658 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
171663 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
171674 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
171685 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
171704 {yymsp[-1].minor.yy516 = M10d_Yes;}
171707 {yymsp[-2].minor.yy516 = M10d_No;}
171711 …sp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].…
171716 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
171721 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
171731 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
171732 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
171735 yymsp[-2].minor.yy41 = yylhsminor.yy41;
171739 if( ALWAYS(yymsp[-1].minor.yy41) ){
171740 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
171742 yylhsminor.yy41 = yymsp[-1].minor.yy41;
171744 yymsp[-4].minor.yy41 = yylhsminor.yy41;
171748 …yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, y…
171753 …e3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp…
171755 yymsp[-5].minor.yy41 = yylhsminor.yy41;
171759 …yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322…
171764 …1 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.…
171766 yymsp[-4].minor.yy41 = yylhsminor.yy41;
171777 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
171779 yymsp[-1].minor.yy41 = yylhsminor.yy41;
171788 …ylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yy…
171790 yymsp[-2].minor.yy41 = yylhsminor.yy41;
171794 …wAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yy…
171796 yymsp[-5].minor.yy41 = yylhsminor.yy41;
171806 {yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
171807 yymsp[-1].minor.yy595 = yylhsminor.yy595;
171810 {yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
171811 yymsp[-1].minor.yy595 = yylhsminor.yy595;
171817 {yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
171821 {yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
171824 {yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
171827 { yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
171832 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
171834 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
171838 yymsp[-1].minor.yy41 = yylhsminor.yy41;
171842 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
171844 yylhsminor.yy41->eFrmType = TK_FILTER;
171845 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
171847 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
171854 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
171855 assert( yymsp[-3].minor.yy41!=0 );
171860 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
171861 if( yymsp[-1].minor.yy41 ){
171862 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
171867 { yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
171949 yypParser->yytos = yymsp;
171950 yymsp->stateno = (YYACTIONTYPE)yyact;
171951 yymsp->major = (YYCODETYPE)yygoto;
171970 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
172018 yypParser->yyerrcnt = -1;
172020 assert( yypParser->yytos==yypParser->yystack );
172042 ** <li> An option argument of a grammar-specified type.
172066 assert( yypParser->yytos!=0 );
172071 yyact = yypParser->yytos->stateno;
172079 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
172085 assert( yypParser->yytos>=yypParser->yystack );
172086 assert( yyact==yypParser->yytos->stateno );
172089 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
172099 yypParser->yytos[yysize].stateno);
172113 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
172114 yypParser->yyhwm++;
172115 assert( yypParser->yyhwm ==
172116 (int)(yypParser->yytos - yypParser->yystack));
172120 if( yypParser->yytos>=yypParser->yystackEnd ){
172125 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
172137 yypParser->yyerrcnt--;
172141 yypParser->yytos--;
172175 if( yypParser->yyerrcnt<0 ){
172178 yymx = yypParser->yytos->major;
172189 while( yypParser->yytos > yypParser->yystack ){
172190 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
172195 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
172199 yypParser->yyerrcnt = -1;
172206 yypParser->yyerrcnt = 3;
172209 yyact = yypParser->yytos->stateno;
172231 if( yypParser->yyerrcnt<=0 ){
172234 yypParser->yyerrcnt = 3;
172239 yypParser->yyerrcnt = -1;
172251 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
172252 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
172291 ** individual tokens and sends those tokens one-by-one over to the
172316 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
172321 #define CC_SLASH 16 /* '/'. / or c-style comment */
172380 ** lower-case ASCII equivalent. On ASCII machines, this is just
172381 ** an upper-to-lower case map. On EBCDIC machines we also need
172434 ** might be implemented more directly using a hand-written hash table.
172492 /* aKWHash[i] is the hash value for the i-th keyword */
172506 ** then the i-th keyword has no more hash collisions. Otherwise,
172507 ** the next keyword with the same hash is aKWHash[i]-1. */
172522 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
172538 ** the text for the i-th keyword. */
172553 /* aKWCode[i] is the parser symbol code for the i-th keyword */
172715 /* Check to see if z[0..n-1] is a keyword. If it is, write the
172722 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
172723 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
172917 ** For ASCII, any character with the high-order bit set is
172918 ** allowed in an identifier. For 7-bit characters,
172948 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
172990 ** impossible to call a window-function without a FILTER clause.
173037 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
173051 if( z[1]=='-' ){
173053 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
173089 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
173215 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
173331 void *pEngine; /* The LEMON-generated LALR(1) parser */
173334 int lastTokenParsed = -1; /* type of the previous token */
173335 sqlite3 *db = pParse->db; /* The database connection */
173339 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
173341 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
173344 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
173345 if( db->nVdbeActive==0 ){
173346 AtomicStore(&db->u1.isInterrupted, 0);
173348 pParse->rc = SQLITE_OK;
173349 pParse->zTail = zSql;
173351 if( db->flags & SQLITE_ParserTrace ){
173368 assert( pParse->pNewTable==0 );
173369 assert( pParse->pNewTrigger==0 );
173370 assert( pParse->nVar==0 );
173371 assert( pParse->pVList==0 );
173372 pParentParse = db->pParse;
173373 db->pParse = pParse;
173376 mxSqlLen -= n;
173378 pParse->rc = SQLITE_TOOBIG;
173379 pParse->nErr++;
173391 if( AtomicLoad(&db->u1.isInterrupted) ){
173392 pParse->rc = SQLITE_INTERRUPT;
173393 pParse->nErr++;
173430 pParse->sLastToken.z = zSql;
173431 pParse->sLastToken.n = n;
173432 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
173435 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
173436 if( pParse->rc!=SQLITE_OK ) break;
173451 if( db->mallocFailed ){
173452 pParse->rc = SQLITE_NOMEM_BKPT;
173454 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
173455 if( pParse->zErrMsg==0 ){
173456 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
173458 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
173461 pParse->zTail = zSql;
173463 sqlite3_free(pParse->apVtabLock);
173466 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
173467 /* If the pParse->declareVtab flag is set, do not delete any table
173468 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
173471 sqlite3DeleteTable(db, pParse->pNewTable);
173473 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
173474 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
173476 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
173477 db->pParse = pParentParse;
173478 assert( nErr==0 || pParse->rc!=SQLITE_OK );
173489 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
173495 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
173500 Vdbe *pVdbe, /* VM being reprepared */
173507 int prevType = 0; /* Previous non-whitespace token */
173515 tokenType = -1;
173519 for(i=0; zSql[i] && pStr->accError==0; i+=n){
173547 iStartIN = pStr->nChar;
173555 assert( pStr->nChar>=(u32)iStartIN );
173556 pStr->nChar = iStartIN+1;
173560 nParen--;
173566 j = pStr->nChar;
173590 while( j<pStr->nChar ){
173591 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
173602 j = pStr->nChar;
173604 while( j<pStr->nChar ){
173605 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
173649 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
173678 ** (0) INVALID We have not yet seen a non-whitespace character.
173776 case '/': { /* C-style comments */
173788 case '-': { /* SQL-style comments from "--" to end of line */
173789 if( zSql[1]!='-' ){
173798 case '[': { /* Microsoft-style identifiers in [...] */
173805 case '`': /* Grave-accent quoted symbols used by MySQL */
173806 case '"': /* single- and double-quoted strings */
173867 zSql += nId-1;
173884 ** above, except that the parameter is required to be UTF-16 encoded, not
173885 ** UTF-8.
173897 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
174077 ** This is an extension initializer that is a no-op and always
174078 ** succeeds, except that it fails if the fault-simulation is set
174106 ** built-in extensions.
174146 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
174152 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
174157 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
174165 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
174170 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
174172 ** the SQLITE_THREADSAFE compile-time option being set to 0.
174178 ** this variable being set to non-zero will cause OSTRACE macros to emit
174225 ** This routine is a no-op except on its very first call for the process,
174241 ** * Calls to this routine from Y must block until the outer-most
174262 ** combination, the work-around is to set the correct pointer
174263 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
174267 ** to sqlite3_initialize() should be a no-op. But the initialization
174290 ** malloc subsystem - this implies that the allocation of a static
174327 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
174374 sqlite3GlobalConfig.nRefInitMutex--;
174391 u64 x = (((u64)1)<<63)-1;
174402 ** compile-time option.
174420 ** when this routine is invoked, then this routine is a harmless no-op.
174469 ** the SQLite library at run-time.
174502 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
174504 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
174505 ** Single-thread. */
174511 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
174513 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
174514 ** Multi-thread. */
174520 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
174522 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
174529 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
174536 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
174545 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
174548 ** low-level memory allocation routines to be used in place of the memory
174554 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
174563 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
174574 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
174575 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
174584 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
174596 /* no-op */
174606 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
174614 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
174625 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
174630 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
174631 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
174646 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
174656 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
174691 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
174692 ** can be changed at start-time using the
174697 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
174698 ** argument of type int. If non-zero, then URI handling is globally
174706 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
174724 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
174730 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
174731 ** negative, then that argument is changed to its compile-time default.
174733 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
174735 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
174736 ** compile-time option.
174748 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
174750 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
174816 int nBig; /* Number of full-size slots */
174817 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
174826 if( db->lookaside.bMalloced ){
174827 sqlite3_free(db->lookaside.pStart);
174832 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
174840 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
174849 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
174852 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
174861 db->lookaside.pStart = pStart;
174862 db->lookaside.pInit = 0;
174863 db->lookaside.pFree = 0;
174864 db->lookaside.sz = (u16)sz;
174865 db->lookaside.szTrue = (u16)sz;
174872 p->pNext = db->lookaside.pInit;
174873 db->lookaside.pInit = p;
174877 db->lookaside.pSmallInit = 0;
174878 db->lookaside.pSmallFree = 0;
174879 db->lookaside.pMiddle = p;
174881 p->pNext = db->lookaside.pSmallInit;
174882 db->lookaside.pSmallInit = p;
174887 db->lookaside.pEnd = p;
174888 db->lookaside.bDisable = 0;
174889 db->lookaside.bMalloced = pBuf==0 ?1:0;
174890 db->lookaside.nSlot = nBig+nSm;
174892 db->lookaside.pStart = 0;
174894 db->lookaside.pSmallInit = 0;
174895 db->lookaside.pSmallFree = 0;
174896 db->lookaside.pMiddle = 0;
174898 db->lookaside.pEnd = 0;
174899 db->lookaside.bDisable = 1;
174900 db->lookaside.sz = 0;
174901 db->lookaside.bMalloced = 0;
174902 db->lookaside.nSlot = 0;
174904 db->lookaside.pTrueEnd = db->lookaside.pEnd;
174920 return db->mutex;
174933 sqlite3_mutex_enter(db->mutex);
174935 for(i=0; i<db->nDb; i++){
174936 Btree *pBt = db->aDb[i].pBt;
174943 sqlite3_mutex_leave(db->mutex);
174948 ** Flush any dirty pages in the pager-cache for any attached database
174959 sqlite3_mutex_enter(db->mutex);
174961 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
174962 Btree *pBt = db->aDb[i].pBt;
174973 sqlite3_mutex_leave(db->mutex);
174983 sqlite3_mutex_enter(db->mutex);
174987 /* IMP: R-06824-28531 */
174988 /* IMP: R-36257-52125 */
174989 db->aDb[0].zDbSName = va_arg(ap,char*);
174994 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
174995 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
174996 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
175008 pVdbe->pSharedBlock = pSharedBlock;
175010 pVdbe->startPos = pSharedBlock->startPos;
175012 pVdbe->totalRows = 0;
175013 pVdbe->blockFull = 0;
175014 pVdbe->addedRows = 0;
175047 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
175052 u64 oldFlags = db->flags;
175054 db->flags |= aFlagOp[i].mask;
175056 db->flags &= ~(u64)aFlagOp[i].mask;
175058 if( oldFlags!=db->flags ){
175062 *pRes = (db->flags & aFlagOp[i].mask)!=0;
175072 sqlite3_mutex_leave(db->mutex);
175088 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
175094 rc = nKey1 - nKey2;
175110 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
175111 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
175116 ** Return true if CollSeq is the default built-in BINARY.
175119 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
175120 return p==0 || p->xCmp==binCollFunc;
175124 ** Another built-in collating sequence: NOCASE.
175130 ** At the moment there is only a UTF-8 implementation.
175141 r = nKey1-nKey2;
175156 return db->lastRowid;
175169 sqlite3_mutex_enter(db->mutex);
175170 db->lastRowid = iRowid;
175171 sqlite3_mutex_leave(db->mutex);
175184 return db->nChange;
175200 return db->nTotalChange;
175209 ** at the b-tree/pager level.
175212 while( db->pSavepoint ){
175213 Savepoint *pTmp = db->pSavepoint;
175214 db->pSavepoint = pTmp->pNext;
175217 db->nSavepoint = 0;
175218 db->nStatement = 0;
175219 db->isTransactionSavepoint = 0;
175230 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
175231 pDestructor = p->u.pDestructor;
175233 pDestructor->nRef--;
175234 if( pDestructor->nRef==0 ){
175235 pDestructor->xDestroy(pDestructor->pUserData);
175250 for(i=0; i<db->nDb; i++){
175251 Schema *pSchema = db->aDb[i].pSchema;
175253 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
175259 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
175261 if( pMod->pEpoTab ){
175262 sqlite3VtabDisconnect(db, pMod->pEpoTab);
175278 assert( sqlite3_mutex_held(db->mutex) );
175279 if( db->pVdbe ) return 1;
175280 for(j=0; j<db->nDb; j++){
175281 Btree *pBt = db->aDb[j].pBt;
175292 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
175293 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
175299 sqlite3_mutex_enter(db->mutex);
175300 if( db->mTrace & SQLITE_TRACE_CLOSE ){
175301 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
175309 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
175311 ** SQL statements below, as the v-table implementation may be storing
175322 sqlite3_mutex_leave(db->mutex);
175335 db->eOpenState = SQLITE_STATE_ZOMBIE;
175346 int iTxn = -1;
175350 return -1;
175353 sqlite3_mutex_enter(db->mutex);
175356 if( iDb<0 ) nDb--;
175359 nDb = db->nDb-1;
175362 Btree *pBt = db->aDb[iDb].pBt;
175366 sqlite3_mutex_leave(db->mutex);
175399 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
175400 sqlite3_mutex_leave(db->mutex);
175412 ** they are reset. And that the required b-tree mutex is held to make
175420 for(j=0; j<db->nDb; j++){
175421 struct Db *pDb = &db->aDb[j];
175422 if( pDb->pBt ){
175423 sqlite3BtreeClose(pDb->pBt);
175424 pDb->pBt = 0;
175426 pDb->pSchema = 0;
175431 if( db->aDb[1].pSchema ){
175432 sqlite3SchemaClear(db->aDb[1].pSchema);
175438 assert( db->nDb<=2 );
175439 assert( db->aDb==db->aDbStatic );
175442 ** locks and does not require any further unlock-notify callbacks.
175446 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
175451 pNext = p->pNext;
175456 sqlite3HashClear(&db->aFunc);
175457 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
175467 sqlite3HashClear(&db->aCollSeq);
175469 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
175474 sqlite3HashClear(&db->aModule);
175478 sqlite3ValueFree(db->pErr);
175481 sqlite3_free(db->auth.zAuthUser);
175482 sqlite3_free(db->auth.zAuthPW);
175485 db->eOpenState = SQLITE_STATE_ERROR;
175487 /* The temp-database schema is allocated differently from the other schema
175493 sqlite3DbFree(db, db->aDb[1].pSchema);
175494 if( db->xAutovacDestr ){
175495 db->xAutovacDestr(db->pAutovacPagesArg);
175497 sqlite3_mutex_leave(db->mutex);
175498 db->eOpenState = SQLITE_STATE_CLOSED;
175499 sqlite3_mutex_free(db->mutex);
175501 if( db->lookaside.bMalloced ){
175502 sqlite3_free(db->lookaside.pStart);
175509 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
175518 assert( sqlite3_mutex_held(db->mutex) );
175521 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
175523 ** modified the database schema. If the b-tree mutexes are not taken
175524 ** here, then another shared-cache connection might sneak in between
175528 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
175530 for(i=0; i<db->nDb; i++){
175531 Btree *p = db->aDb[i].pBt;
175549 db->nDeferredCons = 0;
175550 db->nDeferredImmCons = 0;
175551 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
175553 /* If one has been configured, invoke the rollback-hook callback */
175554 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
175555 db->xRollbackCallback(db->pRollbackArg);
175742 ** Return non-zero to retry the lock. Return zero to stop trying
175758 int tmout = db->busyTimeout;
175766 delay = delays[NDELAY-1];
175767 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
175770 delay = tmout - prior;
175773 sqlite3OsSleep(db->pVfs, delay*1000);
175779 int tmout = ((sqlite3 *)ptr)->busyTimeout;
175783 sqlite3OsSleep(db->pVfs, 1000000);
175794 ** If this routine returns non-zero, the lock is retried. If it